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.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
/* GENERATED by @estella/compiler — do not edit. */
/* The Estella ABI, C side. */
#ifndef ESTELLA_ABI_H
#define ESTELLA_ABI_H
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
/* A fused multiply-add is MORE accurate than the double rounding JS does, and
more accurate is still a different number. GCC does not implement this pragma
and warns about it, so the flag in CFLAGS is the one that has to be there. */
#if defined(__clang__)
#pragma STDC FP_CONTRACT OFF
#endif
/* An address is a POINTER, and on wasm32 a pointer IS a 32-bit offset into
linear memory — which is why one emitted source serves both back ends. The
OFFSETS below are the contract; the address width is the machine, and it is
mixed into the handshake so a 32-bit artifact cannot load into a 64-bit host.
ES_ADDR_BASE is the other model: a host that owns one block and addresses it
by offset. A wasm2c deployment is one, and so is the differential
harness. Same structs, same offsets, one word wider or narrower. */
#if defined(ES_ADDR_BASE)
typedef uint32_t es_addr_t;
extern unsigned char *es_memory;
#define ES_PTR(a) (es_memory + (a))
#else
typedef uintptr_t es_addr_t;
#define ES_PTR(a) ((unsigned char *)(a))
#endif
/* A host that LOADS this asks the platform for a symbol by name, and on
Windows nothing is exported from a DLL unless it says so — the module
builds, loads, and answers null to every lookup. A wasm link names its
exports on the command line instead, so this is inert there. */
#if defined(_WIN32)
#define ES_EXPORT __declspec(dllexport)
#elif defined(__GNUC__)
#define ES_EXPORT __attribute__((visibility("default")))
#else
#define ES_EXPORT
#endif
/* The packed rows the host materialised: for each row, the entity and
then one base address per component the query named, in that order. */
typedef struct EsQueryRows {
es_addr_t rows;
es_addr_t count;
} EsQueryRows;
/* A command: four u32, sixteen bytes, at every address width. */
typedef struct EsCmd {
uint32_t kind;
uint32_t a;
uint32_t b;
uint32_t c;
} EsCmd;
#define ES_CMD_DESPAWN 1u
#define ES_CMD_REMOVE 2u
/* 6 addresses, and a number that needs a reason to grow. */
typedef struct EsSysCtx {
es_addr_t queries;
es_addr_t resources;
es_addr_t cmdBuf;
es_addr_t cmdCap;
es_addr_t cmdCount;
es_addr_t events;
} EsSysCtx;
/* What a host needs in order to CALL a system without glue written by hand.
The compiler knows a system's queries and resources -- that is what _params
is -- so it says so here rather than leaving each host to be told twice. */
/* Where a system APPENDS events: f64 records, because a payload is fields and
not four words. A record is [writer slot, ...fields], and how long one is
comes from the manifest — the host declared it.
Its own queue rather than a wider EsCmd: fixed-width records and
variable-width ones cannot share a buffer without stride guesswork.
3 addresses. A host lays this block out from that same
number, so a field appended here without it is a header one side walks past. */
typedef struct EsEventOut {
es_addr_t buf; /* double * */
es_addr_t cap; /* capacity, in doubles */
es_addr_t count; /* address of the used-doubles word */
} EsEventOut;
typedef struct EsQueryDecl {
const char *const *comps; /* component names, in the order the query names them */
const unsigned char *mut; /* 1 where the system may write that component */
uint32_t count;
} EsQueryDecl;
typedef struct EsSystemDecl {
const char *name;
void (*fn)(es_addr_t);
const EsQueryDecl *queries;
const char *const *resources;
/* 1 where the system declared ResMut: a host that MIRRORS a resource into
the block has to write that block back, or the write is dropped. */
const unsigned char *resourcesMut;
uint32_t queryCount;
uint32_t resourceCount;
} EsSystemDecl;
/* The C and TS sides of these structs have ONE author, and a side that drifts
from it must fail to compile. The numbers below are written by that author
(sdk/src/ecs/aot/abiDigest.ts, through compiler/src/abi.ts) into this file;
these four declarations are how C says no if a struct above ever stops
matching them. Every struct a host addresses by word owes one: EsEventOut
went without for as long as it existed, and a fourth field would have been
read as the next struct by whichever side had not been told. */
typedef char es_check_sysctx[sizeof(EsSysCtx) == 6 * sizeof(es_addr_t) ? 1 : -1];
typedef char es_check_rows[sizeof(EsQueryRows) == 2 * sizeof(es_addr_t) ? 1 : -1];
typedef char es_check_cmd[sizeof(EsCmd) == 4 * 4 ? 1 : -1];
typedef char es_check_eventout[sizeof(EsEventOut) == 3 * sizeof(es_addr_t) ? 1 : -1];
/* memcpy, not a cast through a typed pointer: a component base is whatever the
host handed over, and a wider load through an under-aligned pointer is
undefined even where the hardware would have allowed it. */
static inline double es_f32(const unsigned char *p) { float v; memcpy(&v, p, 4); return (double)v; }
static inline void es_set_f32(unsigned char *p, double v) { float f = (float)v; memcpy(p, &f, 4); }
static inline double es_f64(const unsigned char *p) { double v; memcpy(&v, p, 8); return v; }
static inline void es_set_f64(unsigned char *p, double v) { memcpy(p, &v, 8); }
/* A bool in the C++ structs is ONE byte. Reading it as a float would read three
bytes of whatever follows it, which is a value rather than an error. */
static inline int es_bool(const unsigned char *p) { return *p != 0; }
/* One question of a service, as the host mirrored it: a byte and a mask. */
static inline int es_bit(const unsigned char *p, unsigned bit) { return (*p >> bit) & 1u; }
static inline void es_set_bool(unsigned char *p, int v) { *p = (unsigned char)(v ? 1 : 0); }
/* ---------------------------------------------------------------------------
Math. Only the operations ECMAScript specifies EXACTLY are in the subset, but
"exactly specified" is not the same as "what libm does" — three of them
differ, and each difference is one bit of one pixel that would take a day to
find. So each is written out rather than forwarded.
--------------------------------------------------------------------------- */
static inline double es_abs(double x) { return fabs(x); }
static inline double es_floor(double x) { return floor(x); }
static inline double es_ceil(double x) { return ceil(x); }
static inline double es_trunc(double x) { return trunc(x); }
static inline double es_sqrt(double x) { return sqrt(x); }
/* ---------------------------------------------------------------------------
Trigonometry, SPECIFIED. Math.sin is implementation-defined, so an
interpreted build and a compiled one may both be right and disagree — which
for a differential pixel gate is a red frame with no bug behind it.
These are the same range reduction, the same polynomial and the same ORDER as
sdk/src/math/exact.ts, line for line. Reordering either side is a change to
the result, and a differential compares them bit for bit.
The COEFFICIENTS come from that file, written in below: what is implemented
twice here is the structure, which a differential can compare, and not
fifteen decimals, which retyping only puts at risk.
--------------------------------------------------------------------------- */
#define ES_PIO2_HI 1.5707963267341256
#define ES_PIO2_LO 6.077100506506192e-11
#define ES_TWO_OVER_PI 0.6366197723675814
static inline double es_kernel_sin(double x) {
double z = x * x;
double r = -0.16666666666666632 + z * (0.008333333333324894
+ z * (-0.0001984126982985795 + z * (0.000002755731370707007
+ z * (-2.505076025340686e-8 + z * 1.58969099521155e-10))));
return x + x * z * r;
}
static inline double es_kernel_cos(double x) {
double z = x * x;
double r = 0.0416666666666666 + z * (-0.001388888888887411
+ z * (0.00002480158728947673 + z * (-2.7557314351390663e-7
+ z * (2.087572321298175e-9 + z * -1.1359647557788195e-11))));
return 1.0 - 0.5 * z + z * z * r;
}
/* Math.round's rule, as floor(x + 0.5) rather than es_round: the TS side
reduces with that same expression, and the two must agree on a tie. */
static inline double es_trig_quadrant(double x, double *r) {
double n = floor(x * ES_TWO_OVER_PI + 0.5);
*r = (x - n * ES_PIO2_HI) - n * ES_PIO2_LO;
double q = fmod(n, 4.0);
if (q < 0.0) q += 4.0;
return q;
}
static inline double es_exact_sin(double x) {
if (!isfinite(x)) return (double)NAN;
double r;
double q = es_trig_quadrant(x, &r);
if (q == 0.0) return es_kernel_sin(r);
if (q == 1.0) return es_kernel_cos(r);
if (q == 2.0) return -es_kernel_sin(r);
return -es_kernel_cos(r);
}
static inline double es_exact_cos(double x) {
if (!isfinite(x)) return (double)NAN;
double r;
double q = es_trig_quadrant(x, &r);
if (q == 0.0) return es_kernel_cos(r);
if (q == 1.0) return -es_kernel_sin(r);
if (q == 2.0) return -es_kernel_cos(r);
return es_kernel_sin(r);
}
/* Math.sign returns x itself for NaN and for both zeroes, so the sign of zero
survives — and the sign of zero is a distinct bit pattern in the image. */
static inline double es_sign(double x) {
if (isnan(x) || x == 0.0) return x;
return x > 0.0 ? 1.0 : -1.0;
}
/* C's round() breaks ties AWAY from zero; ECMAScript breaks them toward +Inf.
And floor(x + 0.5) — the usual fix — is wrong for the double just below 0.5,
where the addition itself rounds up to 1. Subtracting the floor is exact for
every finite x, so the comparison sees the true fraction. */
static inline double es_round(double x) {
if (!isfinite(x) || x == 0.0) return x;
double f = floor(x);
double r = (x - f) < 0.5 ? f : f + 1.0;
/* Math.round(-0.2) is -0, and -0 is not the same bytes as +0. */
return (r == 0.0 && x < 0.0) ? -0.0 : r;
}
/* fmin/fmax return the non-NaN operand and are free to pick either zero;
Math.min/max propagate NaN and distinguish -0 from +0. */
static inline double es_min(double a, double b) {
if (isnan(a) || isnan(b)) return (double)NAN;
if (a == b) return signbit(a) ? a : b;
return a < b ? a : b;
}
static inline double es_max(double a, double b) {
if (isnan(a) || isnan(b)) return (double)NAN;
if (a == b) return signbit(a) ? b : a;
return a > b ? a : b;
}
/* JS % on doubles is the truncated remainder, which is fmod. */
static inline double es_rem(double a, double b) { return fmod(a, b); }
#endif /* ESTELLA_ABI_H */