Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

4.0 KB
Raw Blame
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
/**
 * @file    outside-subset.ts
 * @brief   Systems that must NOT compile.
 *
 * @details Each names one rule of the subset; subset.test.ts asserts the
 *          diagnostic points at the line and says why.
 */
import { defineComponent, defineSystem, Query, Mut, Res, Camera, Time, Transform } from 'esengine';

export const Speed = defineComponent('FixtureSpeed', { value: 100 });

/** A loop of its own: only the row loop is lowered. */
export const looping = defineSystem(
    [Query(Mut(Transform), Speed)],
    (query) => {
        for (const [_e, t, s] of query) {
            while (t.position.x < s.value) {
                t.position.x += 1;
            }
        }
    },
    { name: 'FixtureLooping' },
);

/** A field the component does not declare — a typo, caught at compile time. */
export const typoField = defineSystem(
    [Query(Mut(Transform), Speed)],
    (query) => {
        for (const [_e, t, s] of query) {
            t.position.x += s.valeu;
        }
    },
    { name: 'FixtureTypo' },
);

/**
 * A Math operation ECMAScript leaves implementation-defined. Refused on purpose:
 * a native backend and the interpreter would be free to disagree, and a pixel
 * gate would then go red on trig instead of on a bug.
 */
export const trig = defineSystem(
    [Query(Mut(Transform), Speed), Res(Time)],
    (query, time) => {
        for (const [_e, t, s] of query) {
            t.position.x += Math.sin(s.value * time.delta);
        }
    },
    { name: 'FixtureTrig' },
);

// No annotation: the subset takes a parameter's type from the source, and there
// is nothing here to take.
function damp(v): number {
    return v * 0.5;
}

/** A call: nothing outside the intrinsics is reachable from a compiled system. */
export const calling = defineSystem(
    [Query(Mut(Transform), Speed), Res(Time)],
    (query, time) => {
        for (const [_e, t, s] of query) {
            t.position.x += damp(s.value) * time.delta;
        }
    },
    { name: 'FixtureCalling' },
);

/** Writing a component the query did not ask to write. */
export const writesReadOnly = defineSystem(
    [Query(Transform, Speed)],
    (query) => {
        for (const [_e, t, s] of query) {
            t.position.x += s.value;
        }
    },
    { name: 'FixtureWritesReadOnly' },
);

const LIMIT = 42;

/** Assigning to a module constant: it is a value, not storage. */
export const writesConst = defineSystem(
    [Query(Mut(Transform), Speed)],
    (query) => {
        for (const [_e, t, s] of query) {
            t.position.x = LIMIT + s.value;
            LIMIT.foo = 1;
        }
    },
    { name: 'FixtureWritesConst' },
);

/**
 * The same refusal as `FixtureTrig`, but PROMISED.
 *
 * @compiled
 * Without the marker a system that stops compiling just gets slower and nothing
 * goes red; with it, the refusal is an error naming this line.
 */
export const promised = defineSystem(
    [Query(Mut(Transform), Speed), Res(Time)],
    (query, time) => {
        for (const [_e, t, s] of query) {
            t.position.x += Math.cos(s.value * time.delta);
        }
    },
    { name: 'FixturePromised' },
);

/**
 * Marked, and inside the subset: the promise is kept.
 *
 * @compiled
 */
export const promiseKept = defineSystem(
    [Query(Mut(Transform), Speed), Res(Time)],
    (query, time) => {
        for (const [_e, t, s] of query) {
            t.position.x += s.value * time.delta;
        }
    },
    { name: 'FixturePromiseKept' },
);

/**
 * An integer leaf. `Camera.priority` is an i32 in the C++ struct, and this
 * subset has no integer type — narrowing a double back into one is undefined in
 * C where it wraps in JS, so the two could not be held to the same bits.
 */
export const integerField = defineSystem(
    [Query(Mut(Camera))],
    (query) => {
        for (const [, camera] of query) {
            camera.fov = camera.priority + 1;
        }
    },
    { name: 'FixtureIntegerField' },
);