Files
jsrt/lib/jsrt.h
T
2026-10-01 13:58:50 +00:00

106 lines
4.3 KiB
C

// The runtime that emitted C is compiled against. Declarations only for now.
// The natives are declared by the emitted C itself, from compiler/src/native.rs
// See docs/model.md for the representation of values
#pragma once
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
typedef struct { char _; } undefined_t;
typedef struct { char _; } null_t;
typedef struct jsrt_string *string_t;
// ────────────────────────────────────────────── cells ──────────────────────────────────────────────
// The start of every object: the site that created it, which tells its shape
// and, for a fn, its code
typedef struct { uint32_t site; } jsrt_header;
// The sites below 3 are tags. An object of a value shape is a struct, not a
// pointer, so its header tells if it is undefined, null or absent instead
#define JSRT_SITE_UNDEFINED 0
#define JSRT_SITE_NULL 1
#define JSRT_SITE_ABSENT 2
// A pointer to an object or a string can also be one of these. NULL is null
extern jsrt_header jsrt_undefined_cell, jsrt_absent_cell;
#define JSRT_UNDEFINED ((void *)&jsrt_undefined_cell)
#define JSRT_ABSENT ((void *)&jsrt_absent_cell) // a field that an object lacks
// A new heap cell of `size` bytes
void *jsrt_alloc(size_t size);
// Throws a TypeError. Exceptions are not supported yet, so it stops the program
_Noreturn void jsrt_type_error(void);
// ───────────────────────────────────────────── strings ─────────────────────────────────────────────
string_t jsrt_string_lit(const char *s);
int32_t jsrt_string_length(string_t s);
bool jsrt_string_eq(string_t a, string_t b);
int32_t jsrt_string_cmp(string_t a, string_t b);
// ─────────────────────────────────────────────── any ───────────────────────────────────────────────
// A value of any kind: a NaN-boxed 64-bit word. An object in it is the pointer
// to its cell, so it keeps its identity. A field can also hold absent
typedef uint64_t any_t;
// Box a value. A pointer that is a sentinel (NULL, JSRT_UNDEFINED,
// JSRT_ABSENT) boxes as null, undefined or absent
any_t __any_undefined(void);
any_t __any_null(void);
any_t __any_absent(void);
any_t __any_from_bool(bool b);
any_t __any_from_int(int32_t n);
any_t __any_from_float(double x);
any_t __any_from_string(string_t s);
any_t __any_from_object(void *o);
// Unbox a value. A value of another kind throws. A string or an object unboxes
// null, undefined and absent to their sentinels
bool __any_as_bool(any_t a);
int32_t __any_as_int(any_t a);
double __any_as_float(any_t a); // an int too
string_t __any_as_string(any_t a);
void *__any_as_object(any_t a);
// The kind of a value
bool __any_is_absent(any_t a);
bool __any_is_undefined(any_t a);
bool __any_is_null(any_t a);
bool __any_is_nullish(any_t a); // undefined or null
bool __any_is_boolean(any_t a);
bool __any_is_number(any_t a);
bool __any_is_string(any_t a);
bool __any_is_object(any_t a); // a fn too
bool __any_is_function(any_t a);
// JS's operators, with JS's conversions
any_t __any_add(any_t a, any_t b);
any_t __any_sub(any_t a, any_t b);
any_t __any_mul(any_t a, any_t b);
any_t __any_div(any_t a, any_t b);
any_t __any_mod(any_t a, any_t b);
any_t __any_pow(any_t a, any_t b);
any_t __any_neg(any_t a);
any_t __any_plus(any_t a); // ToNumber
any_t __any_inc(any_t a);
any_t __any_dec(any_t a);
bool __any_lt(any_t a, any_t b);
bool __any_lte(any_t a, any_t b);
bool __any_gt(any_t a, any_t b);
bool __any_gte(any_t a, any_t b);
bool __any_eq(any_t a, any_t b);
bool __any_neq(any_t a, any_t b);
bool __any_strict_eq(any_t a, any_t b);
bool __any_strict_neq(any_t a, any_t b);
// JS's abstract operations
bool __any_truthy(any_t a); // ToBoolean
int32_t __any_to_int32(any_t a); // ToInt32
string_t __any_to_string(any_t a); // ToString
string_t __any_typeof(any_t a);