#ifndef QUICKJS_INTERNAL_H #define QUICKJS_INTERNAL_H #include #include #include #include #include #include #include #include #include #include #include #ifndef TARGET_PLAYDATE #include #endif #if defined(__APPLE__) #include #elif defined(__linux__) || defined(__GLIBC__) #include #elif defined(__FreeBSD__) #include #endif #include "cutils.h" #include "dtoa.h" #include "libregexp.h" #include "libunicode.h" #include "list.h" #include "cell.h" #include "cJSON.h" #include "blob.h" #include "nota.h" #include "wota.h" /* ============================================================ Internal API — not for C module authors ============================================================ */ /* Object header types */ enum mist_obj_type { OBJ_ARRAY = 0, OBJ_BLOB = 1, OBJ_TEXT = 2, OBJ_RECORD = 3, // js style objects OBJ_FUNCTION = 4, OBJ_CODE = 5, OBJ_FRAME = 6, OBJ_FORWARD = 7 }; /* Object header bits */ #define OBJHDR_S_BIT 3u #define OBJHDR_P_BIT 4u #define OBJHDR_A_BIT 5u #define OBJHDR_R_BIT 7u #define OBJHDR_FLAG(bit) ((objhdr_t)1ull << (bit)) #define OBJHDR_S_MASK OBJHDR_FLAG (OBJHDR_S_BIT) #define OBJHDR_P_MASK OBJHDR_FLAG (OBJHDR_P_BIT) #define OBJHDR_A_MASK OBJHDR_FLAG (OBJHDR_A_BIT) #define OBJHDR_R_MASK OBJHDR_FLAG (OBJHDR_R_BIT) typedef uint64_t word_t; // one actor-memory word typedef uint64_t objhdr_t; // header word typedef uint64_t objref_t; // 56-bit word address (0 = null) static inline uint8_t objhdr_type (objhdr_t h) { return (uint8_t)(h & 7u); } static inline int objhdr_s (objhdr_t h) { return (h & OBJHDR_S_MASK) != 0; } /* Word size constant */ #define JSW 8 /* Runtime / Context lifecycle */ JSRuntime *JS_NewRuntime (void); void JS_FreeRuntime (JSRuntime *rt); void JS_SetMemoryLimit (JSRuntime *rt, size_t limit); void JS_SetPoolSize (JSRuntime *rt, size_t initial, size_t cap); JSContext *JS_NewContext (JSRuntime *rt); JSContext *JS_NewContextWithHeapSize (JSRuntime *rt, size_t heap_size); void JS_FreeContext (JSContext *s); typedef void (*JS_GCScanFn)(JSContext *ctx, uint8_t *from_base, uint8_t *from_end, uint8_t *to_base, uint8_t **to_free, uint8_t *to_end); void JS_SetGCScanExternal(JSContext *ctx, JS_GCScanFn fn); void JS_SetActorSym (JSContext *ctx, JSValue sym); JSValue JS_GetActorSym (JSContext *ctx); JSRuntime *JS_GetRuntime (JSContext *ctx); void JS_SetMaxStackSize (JSContext *ctx, size_t stack_size); void JS_UpdateStackTop (JSContext *ctx); int JS_GetVMCallDepth(JSContext *ctx); void JS_SetHeapMemoryLimit(JSContext *ctx, size_t limit); void JS_SetPauseFlag(JSContext *ctx, int value); JS_BOOL JS_IsLiveObject (JSRuntime *rt, JSValue obj); /* Suspended state */ #define JS_TAG_SUSPENDED 0x13 /* 10011 - distinct special tag */ #define JS_SUSPENDED ((JSValue)JS_TAG_SUSPENDED) static inline JS_BOOL JS_IsSuspended(JSValue v) { return JS_VALUE_GET_TAG(v) == JS_TAG_SUSPENDED; } #ifndef JS_DEFAULT_STACK_SIZE #define JS_DEFAULT_STACK_SIZE (1024 * 1024) #endif /* Internal compile flags */ #define JS_EVAL_FLAG_COMPILE_ONLY (1 << 5) /* Compilation and MachCode */ struct cJSON; typedef struct MachCode MachCode; void JS_FreeMachCode(MachCode *mc); uint8_t *JS_SerializeMachCode(MachCode *mc, size_t *out_size); MachCode *JS_DeserializeMachCode(const uint8_t *data, size_t size); struct JSCodeRegister *JS_LoadMachCode(JSContext *ctx, MachCode *mc, JSValue env); JSValue JS_RunMachBin(JSContext *ctx, const uint8_t *data, size_t size, JSValue env); JSValue JS_RunMachMcode(JSContext *ctx, const char *json_str, size_t len, JSValue env); void JS_DumpMachBin(JSContext *ctx, const uint8_t *data, size_t size, JSValue env); MachCode *mach_compile_mcode(struct cJSON *mcode_json); /* Debug / Dump utilities */ typedef struct JSMemoryUsage { int64_t malloc_size, malloc_limit, memory_used_size; int64_t malloc_count; int64_t memory_used_count; int64_t str_count, str_size; int64_t obj_count, obj_size; int64_t prop_count, prop_size; int64_t shape_count, shape_size; int64_t js_func_count, js_func_size, js_func_code_size; int64_t js_func_pc2line_count, js_func_pc2line_size; int64_t c_func_count, array_count; int64_t fast_array_count, fast_array_elements; int64_t binary_object_count, binary_object_size; } JSMemoryUsage; void JS_ComputeMemoryUsage (JSRuntime *rt, JSMemoryUsage *s); void JS_DumpMemoryUsage (FILE *fp, const JSMemoryUsage *s, JSRuntime *rt); typedef struct { JS_BOOL show_hidden : 8; JS_BOOL raw_dump : 8; uint32_t max_depth; uint32_t max_string_length; uint32_t max_item_count; } JSPrintValueOptions; typedef void JSPrintValueWrite (void *opaque, const char *buf, size_t len); void JS_PrintValueSetDefaultOptions (JSPrintValueOptions *options); void JS_PrintValueRT (JSRuntime *rt, JSPrintValueWrite *write_func, void *write_opaque, JSValue val, const JSPrintValueOptions *options); void JS_PrintValue (JSContext *ctx, JSPrintValueWrite *write_func, void *write_opaque, JSValue val, const JSPrintValueOptions *options); void js_debug_info (JSContext *js, JSValue fn, js_debug *dbg); uint32_t js_debugger_stack_depth (JSContext *ctx); JSValue js_debugger_backtrace_fns (JSContext *ctx); JSValue js_debugger_closure_variables (JSContext *ctx, JSValue fn); JSValue js_debugger_local_variables (JSContext *ctx, int stack_index); void js_debugger_set_closure_variable (JSContext *js, JSValue fn, JSValue var_name, JSValue val); JSValue js_debugger_build_backtrace (JSContext *ctx); JSValue js_debugger_fn_info (JSContext *ctx, JSValue fn); JSValue js_debugger_fn_bytecode (JSContext *js, JSValue fn); void *js_debugger_val_address (JSContext *js, JSValue val); /* Stack trace */ JSValue JS_GetStack (JSContext *ctx); void JS_CrashPrintStack(JSContext *ctx); /* Serialization (internal) */ JSValue wota2value(JSContext *js, void *v); void *value2wota(JSContext *js, JSValue v, JSValue replacer, size_t *bytes); JSValue nota2value(JSContext *js, void *nota); void *value2nota(JSContext *js, JSValue v); /* Internal module init (called during context init) */ JSValue js_core_blob_use(JSContext *js); JSValue js_core_json_use(JSContext *js); /* ============================================================ End internal API declarations ============================================================ */ void *js_malloc (JSContext *ctx, size_t size); void *js_mallocz (JSContext *ctx, size_t size); #if !defined(_WIN32) /* define it if printf uses the RNDN rounding mode instead of RNDNA */ #define CONFIG_PRINTF_RNDN #endif #if !defined(EMSCRIPTEN) /* enable stack limitation */ #define CONFIG_STACK_CHECK #endif /* dump object free */ // #define DUMP_FREE // #define DUMP_CLOSURE /* dump the bytecode of the compiled functions: combination of bits 1: dump pass 3 final byte code 2: dump pass 2 code 4: dump pass 1 code 8: dump stdlib functions 16: dump bytecode in hex 32: dump line number table 64: dump compute_stack_size */ // #define DUMP_BYTECODE (1) /* dump GC summary: old/new heap, recovery %, heap growth */ // #define DUMP_GC /* dump detailed GC: roots, scanning, object traversal (implies DUMP_GC) */ // #define DUMP_GC_DETAIL #ifdef DUMP_GC_DETAIL #define DUMP_GC #endif /* dump objects freed by the garbage collector */ // #define DUMP_GC_FREE /* dump memory usage before running the garbage collector */ // #define DUMP_MEM // #define DUMP_OBJECTS /* dump objects in JS_FreeContext */ // #define DUMP_READ_OBJECT // #define DUMP_ROPE_REBALANCE /* dump GC ref stack push/pop with C backtrace on mismatch. When HAVE_ASAN is also set, uses __sanitizer_print_stack_trace() for symbolized output; otherwise falls back to execinfo backtrace(). */ // #define DUMP_GC_REFS /* test the GC by forcing it before each object allocation */ // #define FORCE_GC_AT_MALLOC #include #include /* HEAP_CHECK: validate heap pointers at JS_VALUE_GET_* macros */ // #define HEAP_CHECK #if defined(__has_feature) #if __has_feature(address_sanitizer) #define HAVE_ASAN 1 #endif #elif defined(__SANITIZE_ADDRESS__) #define HAVE_ASAN 1 #endif #ifdef HAVE_ASAN #include static struct JSContext *__asan_js_ctx; #endif /* Forward declarations for heap object types */ typedef struct JSArray JSArray; typedef struct JSBlob JSBlob; typedef struct JSText JSText; typedef struct JSRecord JSRecord; typedef struct JSFunction JSFunction; typedef struct JSCode JSCode; typedef struct JSFrame JSFrame; #define OBJHDR_CAP_SHIFT 8u #define OBJHDR_CAP_MASK (((objhdr_t)1ull << 56) - 1ull) #define JS_MKPTR(ptr) (((JSValue)(uintptr_t)(ptr)) | JS_TAG_PTR) #define JS_MKFWD(ptr) (((objhdr_t)(uintptr_t)(ptr) << OBJHDR_FWD_PTR_SHIFT) | OBJ_FORWARD) #define JS_VALUE_GET_FWD_PTR(v) ((void*)(uintptr_t)(((v) >> OBJHDR_FWD_PTR_SHIFT) & OBJHDR_FWD_PTR_MASK)) /* For OBJ_FORWARD type: bits 3-63 contain a 61-bit pointer */ #define OBJHDR_FWD_PTR_SHIFT 3u #define OBJHDR_FWD_PTR_MASK (((objhdr_t)1ull << 61) - 1ull) #define objhdr_fwd_ptr(h) ((void*)(uintptr_t)(((h) >> OBJHDR_FWD_PTR_SHIFT) & OBJHDR_FWD_PTR_MASK)) #define objhdr_make_fwd(ptr) (((objhdr_t)(uintptr_t)(ptr) << OBJHDR_FWD_PTR_SHIFT) | OBJ_FORWARD) /* Extract pointer (clear low bits) */ #define JS_VALUE_GET_PTR(v) ((void *)((v) & ~((JSValue)(JSW - 1)))) static inline JS_BOOL JS_VALUE_IS_TEXT (JSValue v) { int tag = JS_VALUE_GET_TAG (v); return tag == JS_TAG_STRING_IMM || (JS_IsPtr(v) && objhdr_type(*(objhdr_t *)JS_VALUE_GET_PTR(v)) == OBJ_TEXT); } static inline JS_BOOL JS_VALUE_IS_NUMBER (JSValue v) { int tag = JS_VALUE_GET_TAG (v); return tag == JS_TAG_INT || tag == JS_TAG_SHORT_FLOAT; } /* JS_KEY_* macros: JSValue immediate ASCII strings for common property names. These replace JS_ATOM_* for use with the new JSValue-based property API. */ #define _JS_KEY1(c1) \ ((JSValue)JS_TAG_STRING_IMM | ((JSValue)1 << 5) | ((JSValue)(c1) << 8)) #define _JS_KEY2(c1, c2) \ ((JSValue)JS_TAG_STRING_IMM | ((JSValue)2 << 5) | ((JSValue)(c1) << 8) \ | ((JSValue)(c2) << 16)) #define _JS_KEY3(c1, c2, c3) \ ((JSValue)JS_TAG_STRING_IMM | ((JSValue)3 << 5) | ((JSValue)(c1) << 8) \ | ((JSValue)(c2) << 16) | ((JSValue)(c3) << 24)) #define _JS_KEY4(c1, c2, c3, c4) \ ((JSValue)JS_TAG_STRING_IMM | ((JSValue)4 << 5) | ((JSValue)(c1) << 8) \ | ((JSValue)(c2) << 16) | ((JSValue)(c3) << 24) | ((JSValue)(c4) << 32)) #define _JS_KEY5(c1, c2, c3, c4, c5) \ ((JSValue)JS_TAG_STRING_IMM | ((JSValue)5 << 5) | ((JSValue)(c1) << 8) \ | ((JSValue)(c2) << 16) | ((JSValue)(c3) << 24) | ((JSValue)(c4) << 32) \ | ((JSValue)(c5) << 40)) #define _JS_KEY6(c1, c2, c3, c4, c5, c6) \ ((JSValue)JS_TAG_STRING_IMM | ((JSValue)6 << 5) | ((JSValue)(c1) << 8) \ | ((JSValue)(c2) << 16) | ((JSValue)(c3) << 24) | ((JSValue)(c4) << 32) \ | ((JSValue)(c5) << 40) | ((JSValue)(c6) << 48)) #define _JS_KEY7(c1, c2, c3, c4, c5, c6, c7) \ ((JSValue)JS_TAG_STRING_IMM | ((JSValue)7 << 5) | ((JSValue)(c1) << 8) \ | ((JSValue)(c2) << 16) | ((JSValue)(c3) << 24) | ((JSValue)(c4) << 32) \ | ((JSValue)(c5) << 40) | ((JSValue)(c6) << 48) | ((JSValue)(c7) << 56)) /* Common property keys as JSValue immediate strings (max 7 ASCII chars) */ /* Empty string: tag with length 0 */ #define JS_KEY_empty ((JSValue)JS_TAG_STRING_IMM) #define JS_KEY_name _JS_KEY4 ('n', 'a', 'm', 'e') #define JS_KEY_length _JS_KEY6 ('l', 'e', 'n', 'g', 't', 'h') #define JS_KEY_message _JS_KEY7 ('m', 'e', 's', 's', 'a', 'g', 'e') #define JS_KEY_stack _JS_KEY5 ('s', 't', 'a', 'c', 'k') #define JS_KEY_cause _JS_KEY5 ('c', 'a', 'u', 's', 'e') #define JS_KEY_errors _JS_KEY6 ('e', 'r', 'r', 'o', 'r', 's') #define JS_KEY_Error _JS_KEY5 ('E', 'r', 'r', 'o', 'r') #define JS_KEY_raw _JS_KEY3 ('r', 'a', 'w') #define JS_KEY_flags _JS_KEY5 ('f', 'l', 'a', 'g', 's') #define JS_KEY_source _JS_KEY6 ('s', 'o', 'u', 'r', 'c', 'e') #define JS_KEY_exec _JS_KEY4 ('e', 'x', 'e', 'c') #define JS_KEY_toJSON _JS_KEY6 ('t', 'o', 'J', 'S', 'O', 'N') #define JS_KEY_eval _JS_KEY4 ('e', 'v', 'a', 'l') #define JS_KEY_this _JS_KEY4 ('t', 'h', 'i', 's') #define JS_KEY_true _JS_KEY4 ('t', 'r', 'u', 'e') #define JS_KEY_false _JS_KEY5 ('f', 'a', 'l', 's', 'e') #define JS_KEY_null _JS_KEY4 ('n', 'u', 'l', 'l') #define JS_KEY_NaN _JS_KEY3 ('N', 'a', 'N') #define JS_KEY_default _JS_KEY7 ('d', 'e', 'f', 'a', 'u', 'l', 't') #define JS_KEY_value _JS_KEY5 ('v', 'a', 'l', 'u', 'e') #define JS_KEY_index _JS_KEY5 ('i', 'n', 'd', 'e', 'x') #define JS_KEY_input _JS_KEY5 ('i', 'n', 'p', 'u', 't') #define JS_KEY_groups _JS_KEY6 ('g', 'r', 'o', 'u', 'p', 's') #define JS_KEY_indices _JS_KEY7 ('i', 'n', 'd', 'i', 'c', 'e', 's') #define JS_KEY_let _JS_KEY3 ('l', 'e', 't') #define JS_KEY_var _JS_KEY3 ('v', 'a', 'r') #define JS_KEY_new _JS_KEY3 ('n', 'e', 'w') #define JS_KEY_of _JS_KEY2 ('o', 'f') #define JS_KEY_yield _JS_KEY5 ('y', 'i', 'e', 'l', 'd') #define JS_KEY_async _JS_KEY5 ('a', 's', 'y', 'n', 'c') #define JS_KEY_target _JS_KEY6 ('t', 'a', 'r', 'g', 'e', 't') #define JS_KEY_from _JS_KEY4 ('f', 'r', 'o', 'm') #define JS_KEY_meta _JS_KEY4 ('m', 'e', 't', 'a') #define JS_KEY_as _JS_KEY2 ('a', 's') #define JS_KEY_get _JS_KEY3 ('g', 'e', 't') #define JS_KEY_set _JS_KEY3 ('s', 'e', 't') #define JS_KEY_with _JS_KEY4 ('w', 'i', 't', 'h') /* Internal variable names */ #define JS_KEY__ret_ _JS_KEY5 ('<', 'r', 'e', 't', '>') #define JS_KEY__eval_ _JS_KEY6 ('<', 'e', 'v', 'a', 'l', '>') #define JS_KEY__var_ _JS_KEY5 ('<', 'v', 'a', 'r', '>') /* Keys for strings > 7 chars - these use string literals and are created at runtime. The caller must free the returned JSValue if it's a heap string. */ #define JS_KEY_STR(ctx, str) JS_NewStringLen ((ctx), (str), sizeof (str) - 1) #define KEY_GET_STR_BUF_SIZE 256 /* JS_IsPretext, JS_KeyGetStr, JS_PushGCRef, JS_PopGCRef, JS_AddGCRef, JS_DeleteGCRef are defined after JSContext (they need its fields) */ /* Forward declaration for string_get */ static inline int string_get (const JSText *p, int idx); #undef JS_PUSH_VALUE #undef JS_POP_VALUE #define JS_PUSH_VALUE(ctx, v) \ do { \ v##_ref.prev = ctx->top_gc_ref; \ ctx->top_gc_ref = &v##_ref; \ v##_ref.val = v; \ } while (0) #define JS_POP_VALUE(ctx, v) \ do { \ v = v##_ref.val; \ ctx->top_gc_ref = v##_ref.prev; \ } while (0) /* JS_Invoke - invoke method on object using JSValue key */ static JSValue JS_Invoke (JSContext *ctx, JSValue this_val, JSValue method, int argc, JSValue *argv); enum { /* classid tag */ /* union usage | properties */ JS_CLASS_OBJECT = 1, /* must be first */ JS_CLASS_REGEXP = 3, /* u.regexp */ JS_CLASS_BLOB, /* u.opaque (blob *) */ JS_CLASS_INIT_COUNT, /* last entry for predefined classes */ }; /* Log callback — set by engine.cm via $_set_log to route JS_Log through ƿit. For "memory" channel (OOM), the callback is skipped (can't allocate). */ typedef void (*JSLogCallback)(JSContext *ctx, const char *channel, const char *msg); /* the variable and scope indexes must fit on 16 bits. The (-1) and ARG_SCOPE_END values are reserved. */ #define JS_MAX_LOCAL_VARS 65534 #define JS_STACK_SIZE_MAX 65534 /* Max string length matches header capacity (56 bits on 64-bit builds) */ #define JS_STRING_LEN_MAX OBJHDR_CAP_MASK #define __exception __attribute__ ((warn_unused_result)) /* Forward declaration for bytecode freeing */ #define JS_VALUE_GET_BLOB(v) ((JSBlob *)JS_VALUE_GET_PTR (v)) #ifdef HEAP_CHECK void heap_check_fail(void *ptr, struct JSContext *ctx); #define JS_VALUE_GET_ARRAY(v) ((JSArray *)heap_check_chase(ctx, v)) #define JS_VALUE_GET_OBJ(v) ((JSRecord *)heap_check_chase(ctx, v)) #define JS_VALUE_GET_TEXT(v) ((JSText *)heap_check_chase(ctx, v)) #define JS_VALUE_GET_FUNCTION(v) ((JSFunction *)heap_check_chase(ctx, v)) #define JS_VALUE_GET_CODE(v) ((JSCode *)heap_check_chase(ctx, v)) #define JS_VALUE_GET_FRAME(v) ((JSFrame *)heap_check_chase(ctx, v)) #define JS_VALUE_GET_STRING(v) ((JSText *)heap_check_chase(ctx, v)) #define JS_VALUE_GET_RECORD(v) ((JSRecord *)heap_check_chase(ctx, v)) #else #define JS_VALUE_GET_ARRAY(v) ((JSArray *)chase (v)) #define JS_VALUE_GET_OBJ(v) ((JSRecord *)chase (v)) #define JS_VALUE_GET_TEXT(v) ((JSText *)chase (v)) #define JS_VALUE_GET_FUNCTION(v) ((JSFunction *)chase (v)) #define JS_VALUE_GET_CODE(v) ((JSCode *)chase (v)) #define JS_VALUE_GET_FRAME(v) ((JSFrame *)chase (v)) #define JS_VALUE_GET_STRING(v) ((JSText *)chase (v)) #define JS_VALUE_GET_RECORD(v) ((JSRecord *)chase (v)) #endif /* Compatibility: JS_TAG_STRING is an alias for text type checks */ #define JS_TAG_STRING JS_TAG_STRING_IMM /* JS_ThrowMemoryError is an alias for JS_RaiseOOM */ #define JS_ThrowMemoryError(ctx) JS_RaiseOOM(ctx) /* Helper to set cap in objhdr */ static inline objhdr_t objhdr_set_cap56 (objhdr_t h, uint64_t cap) { return (h & 0xFF) | ((cap & OBJHDR_CAP_MASK) << OBJHDR_CAP_SHIFT); } /* ============================================================ Buddy Allocator for Actor Memory Blocks ============================================================ */ /* Platform-specific minimum block size */ #if defined(__LP64__) || defined(_WIN64) #define BUDDY_MIN_ORDER 10 /* 1KB minimum on 64-bit */ #else #define BUDDY_MIN_ORDER 9 /* 512B minimum on 32-bit */ #endif #define BUDDY_MAX_LEVELS 40 /* supports pools up to 2^(BUDDY_MIN_ORDER+39) */ #define BUDDY_DEFAULT_POOL (1ULL << 24) /* 16MB initial pool */ typedef struct BuddyBlock { struct BuddyBlock *next; struct BuddyBlock *prev; uint8_t order; /* log2 of size */ uint8_t is_free; } BuddyBlock; typedef struct BuddyPool { struct BuddyPool *next; uint8_t *base; size_t total_size; uint8_t max_order; /* log2(total_size) */ uint32_t alloc_count; /* outstanding allocations */ BuddyBlock *free_lists[BUDDY_MAX_LEVELS]; } BuddyPool; typedef struct BuddyAllocator { BuddyPool *pools; /* linked list, newest first */ size_t next_pool_size; /* next pool doubles from this */ size_t initial_size; /* starting pool size */ size_t cap; /* 0 = no cap */ size_t total_mapped; /* sum of all pool sizes */ } BuddyAllocator; /* Forward declarations for buddy allocator functions */ static void buddy_destroy (BuddyAllocator *b); static size_t buddy_max_block (BuddyAllocator *b); /* controls a host of contexts, handing out memory and scheduling */ struct JSRuntime { size_t malloc_limit; BuddyAllocator buddy; }; struct JSClass { const char *class_name; JSClassFinalizer *finalizer; uint32_t class_id; /* 0 means free entry */ }; #define JS_MODE_BACKTRACE_BARRIER \ (1 << 3) /* stop backtrace before this frame */ /* JSFrameRegister is now an alias for JSFrame — see JSFrame definition below */ /* ============================================================ Register-Based VM Data Structures ============================================================ */ /* 32-bit instruction encoding (Lua-style) Formats: iABC: [op:8][A:8][B:8][C:8] — register ops iABx: [op:8][A:8][Bx:16] — constant/global loads (unsigned) iAsBx: [op:8][A:8][sBx:16] — conditional jumps (signed offset) isJ: [op:8][sJ:24] — unconditional jump (signed offset) */ typedef uint32_t MachInstr32; typedef struct { uint16_t line; uint16_t col; } MachLineEntry; /* Compiled register-based code (off-heap, never GC'd). Created by JS_CompileMach from AST JSON. */ typedef struct JSCodeRegister { uint16_t arity; /* number of arguments */ uint16_t nr_close_slots; /* closed-over variable count */ uint16_t nr_slots; /* total frame size */ uint16_t entry_point; /* start instruction (usually 0) */ /* Constant pool */ uint32_t cpool_count; JSValue *cpool; /* allocated via js_malloc_rt */ /* Compiled 32-bit instructions */ uint32_t instr_count; MachInstr32 *instructions; /* Nested functions (for closure) */ uint32_t func_count; struct JSCodeRegister **functions; /* Debug info */ JSValue name; /* function name (stone text) */ MachLineEntry *line_table; /* [instr_count], parallel to instructions[] */ char *filename_cstr; /* plain C string for stack trace (js_malloc_rt) */ char *name_cstr; /* plain C string for stack trace (js_malloc_rt) */ uint16_t disruption_pc; /* start of disruption handler (0 = none) */ } JSCodeRegister; /* Unified frame struct — used by the register VM and closures. All fields are JSValues so the GC can scan them uniformly. */ typedef struct JSFrame { objhdr_t header; /* OBJ_FRAME, cap56 = slot count */ JSValue function; /* JSFunction, function object being invoked */ JSValue caller; /* JSFrame, the frame that called this one */ JSValue address; /* return PC stored as JS_NewInt32 */ JSValue slots[]; /* [this][args][captured][locals][temps] */ } JSFrame; typedef JSFrame JSFrameRegister; static inline objhdr_t objhdr_set_s (objhdr_t h, bool s) { return s ? (h | OBJHDR_S_MASK) : (h & ~OBJHDR_S_MASK); } static inline uint64_t objhdr_cap56 (objhdr_t h) { return (uint64_t)((h >> OBJHDR_CAP_SHIFT) & OBJHDR_CAP_MASK); } static inline objhdr_t objhdr_make (uint64_t cap56, uint8_t type, bool r, bool a, bool p, bool s) { objhdr_t h = 0; h |= ((objhdr_t)(cap56 & OBJHDR_CAP_MASK)) << OBJHDR_CAP_SHIFT; h |= (objhdr_t)(type & 7u); if (s) h |= OBJHDR_S_MASK; if (p) h |= OBJHDR_P_MASK; if (a) h |= OBJHDR_A_MASK; if (r) h |= OBJHDR_R_MASK; return h; } static inline objhdr_t *chase(JSValue v) { objhdr_t *oh = JS_VALUE_GET_PTR(v); if (objhdr_type(*oh) != OBJ_FORWARD) return oh; do { objhdr_t *next = (objhdr_t*)objhdr_fwd_ptr(*oh); if (!next) return oh; /* NULL forward pointer (stale reference bug) */ oh = next; } while (objhdr_type(*oh) == OBJ_FORWARD); return oh; } /* Resolve a forward pointer in-place. After rec_resize the old record gets a forward header; any JSValue slot still pointing at it must be updated to follow the chain to the live copy. */ static inline void mach_resolve_forward(JSValue *slot) { if (JS_IsPtr(*slot)) { objhdr_t *oh = (objhdr_t *)JS_VALUE_GET_PTR(*slot); if (objhdr_type(*oh) == OBJ_FORWARD) { do { objhdr_t *next = (objhdr_t *)objhdr_fwd_ptr(*oh); if (!next) break; oh = next; } while (objhdr_type(*oh) == OBJ_FORWARD); *slot = JS_MKPTR(oh); } } } /* Stone a mutable (unstoned) heap text in-place. Used at escape points in the VM to enforce the invariant that an unstoned text is uniquely referenced by exactly one slot. */ static inline void stone_mutable_text(JSValue v) { if (JS_IsPtr(v)) { objhdr_t *oh = (objhdr_t *)JS_VALUE_GET_PTR(v); if (objhdr_type(*oh) == OBJ_TEXT && !(*oh & OBJHDR_S_MASK)) *oh = objhdr_set_s(*oh, true); } } /* Inline type checks — use these in the VM dispatch loop to avoid function call overhead. The public API (JS_IsArray etc. in quickjs.h) remains non-inline for external callers; those wrappers live in runtime.c. */ static inline JS_BOOL mist_is_gc_object(JSValue v) { return JS_IsPtr(v); } static inline JS_BOOL mist_is_array(JSValue v) { return mist_is_gc_object(v) && objhdr_type(*chase(v)) == OBJ_ARRAY; } static inline JS_BOOL mist_is_record(JSValue v) { return mist_is_gc_object(v) && objhdr_type(*chase(v)) == OBJ_RECORD; } static inline JS_BOOL mist_is_function(JSValue v) { return mist_is_gc_object(v) && objhdr_type(*chase(v)) == OBJ_FUNCTION; } static inline JS_BOOL mist_is_text(JSValue v) { return MIST_IsImmediateASCII(v) || (mist_is_gc_object(v) && objhdr_type(*chase(v)) == OBJ_TEXT); } static inline JS_BOOL mist_is_blob(JSValue v) { return mist_is_gc_object(v) && objhdr_type(*chase(v)) == OBJ_BLOB; } static inline JS_BOOL mist_is_stone(JSValue v) { return !mist_is_gc_object(v) || objhdr_s(*chase(v)); } /* Intrinsic array type - tagged as JS_TAG_PTR with mist_hdr type OBJ_ARRAY */ typedef struct JSArray { objhdr_t mist_hdr; /* mist header at offset 0 */ word_t len; /* current length */ JSValue values[]; /* inline flexible array member */ } JSArray; /* JSBlob — inline bit data on the GC heap. cap56 = capacity in bits, S bit = stone (immutable). */ typedef struct JSBlob { objhdr_t mist_hdr; /* type=OBJ_BLOB, cap56=capacity_bits, S=stone */ word_t length; /* used bits */ word_t bits[]; /* inline bit data, ceil(cap56/64) words */ } JSBlob; typedef struct JSText { objhdr_t hdr; /* mist header — cap56 = allocated capacity */ word_t length; /* character count (always) */ word_t hash; /* cached hash (stoned text only) */ word_t packed[]; /* two chars per packed word */ } JSText; typedef struct slot { JSValue key; JSValue val; /* use 'val' to match existing code */ } slot; /* Compatibility alias - old code uses JSRecordEntry */ typedef slot JSRecordEntry; typedef struct JSRecord { objhdr_t mist_hdr; JSValue proto; /* prototype as JSValue (JS_NULL if none) */ word_t len; /* number of entries */ slot slots[]; /* slots[0] reserved: key low32=class_id, key high32=rec_id, val=opaque */ } JSRecord; /* Helper macros to access class_id, rec_id, opaque from slots[0] per memory.md */ #define REC_GET_CLASS_ID(rec) ((uint32_t)((rec)->slots[0].key & 0xFFFFFFFF)) #define REC_GET_REC_ID(rec) ((uint32_t)((rec)->slots[0].key >> 32)) #define REC_SET_CLASS_ID(rec, cid) do { \ (rec)->slots[0].key = ((rec)->slots[0].key & 0xFFFFFFFF00000000ULL) | (uint64_t)(cid); \ } while(0) #define REC_SET_REC_ID(rec, rid) do { \ (rec)->slots[0].key = ((rec)->slots[0].key & 0xFFFFFFFF) | ((uint64_t)(rid) << 32); \ } while(0) #define REC_GET_OPAQUE(rec) ((void*)(uintptr_t)(rec)->slots[0].val) #define REC_SET_OPAQUE(rec, ptr) do { (rec)->slots[0].val = (JSValue)(uintptr_t)(ptr); } while(0) /* ============================================================ Record key helpers (JSValue keys) ============================================================ */ /* GC-SAFE: No allocations. */ static inline JS_BOOL rec_key_is_empty (JSValue key) { return JS_IsNull (key); } /* GC-SAFE: No allocations. */ static inline JS_BOOL rec_key_is_tomb (JSValue key) { return JS_IsException (key); } typedef struct fash64_state { uint64_t result; uint64_t sum; } fash64_state; enum { FASH64_PRIME_11 = 11111111111111111027ull, FASH64_PRIME_8 = 8888888888888888881ull, FASH64_PRIME_3 = 3333333333333333271ull }; static inline void fash64_mul_hi_lo (uint64_t a, uint64_t b, uint64_t *hi, uint64_t *lo) { #if defined(__SIZEOF_INT128__) __uint128_t p = (__uint128_t)a * (__uint128_t)b; *lo = (uint64_t)p; *hi = (uint64_t)(p >> 64); #elif defined(_MSC_VER) && defined(_M_X64) *lo = _umul128 (a, b, hi); #else /* Portable fallback (no 128-bit type, no _umul128). */ uint64_t a0 = (uint32_t)a; uint64_t a1 = a >> 32; uint64_t b0 = (uint32_t)b; uint64_t b1 = b >> 32; uint64_t p00 = a0 * b0; uint64_t p01 = a0 * b1; uint64_t p10 = a1 * b0; uint64_t p11 = a1 * b1; uint64_t mid = (p00 >> 32) + (uint32_t)p01 + (uint32_t)p10; *lo = (p00 & 0xffffffffull) | (mid << 32); *hi = p11 + (p01 >> 32) + (p10 >> 32) + (mid >> 32); #endif } static inline void fash64_begin (fash64_state *s) { s->result = (uint64_t)FASH64_PRIME_8; s->sum = (uint64_t)FASH64_PRIME_3; } static inline void fash64_word (fash64_state *s, uint64_t word) { uint64_t high, low; uint64_t mixed = s->result ^ word; fash64_mul_hi_lo (mixed, (uint64_t)FASH64_PRIME_11, &high, &low); s->sum += high; s->result = low ^ s->sum; } static inline void fash64_block (fash64_state *s, const uint64_t *block, size_t word_count) { for (size_t i = 0; i < word_count; i++) fash64_word (s, block[i]); } static inline uint64_t fash64_end (const fash64_state *s) { return s->result; } /* Convenience one-shot helper */ static inline uint64_t fash64_hash_words (const uint64_t *words, size_t word_count, uint64_t extra_word) { fash64_state s; fash64_begin (&s); fash64_block (&s, words, word_count); fash64_word (&s, extra_word); return fash64_end (&s); } static inline uint64_t fash64_hash_one (uint64_t word) { uint64_t high, low; uint64_t mixed = (uint64_t)FASH64_PRIME_8 ^ word; fash64_mul_hi_lo (mixed, (uint64_t)FASH64_PRIME_11, &high, &low); uint64_t sum = (uint64_t)FASH64_PRIME_3 + high; return low ^ sum; } static inline word_t JSText_len (const JSText *text) { return text->length; } static inline JS_BOOL JSText_equal (const JSText *a, const JSText *b) { word_t len_a = JSText_len (a); word_t len_b = JSText_len (b); if (len_a != len_b) return FALSE; size_t word_count = (len_a + 1) / 2; return memcmp (a->packed, b->packed, word_count * sizeof (word_t)) == 0; } static JS_BOOL JSText_equal_ascii (const JSText *text, JSValue imm) { int len = MIST_GetImmediateASCIILen (imm); if (len != JSText_len (text)) return FALSE; for (int i = 0; i < len; i++) { uint32_t c = string_get (text, i); if (c >= 0x80) return FALSE; if ((uint8_t)c != (uint8_t)MIST_GetImmediateASCIIChar (imm, i)) return FALSE; } return TRUE; } /* Get hash for a JSText value. For stoned text (s=1): hash is cached in length field (computed on first access). For pre-text (s=0): compute hash on the fly. */ /* Max length for key strings (identifiers, property names) */ #define JS_KEY_MAX_LEN 4096 /* Forward declarations for stone arena functions (defined after JSContext) */ /* Auto-rooted C call argv — GC updates values in-place */ typedef struct CCallRoot { JSValue *argv; /* points to C-stack-local array */ int argc; struct CCallRoot *prev; /* stack for nesting (C -> JS -> C -> ...) */ } CCallRoot; /* ============================================================ Actor Types (merged from cell_internal.h) ============================================================ */ /* Letter type for unified message queue */ typedef enum { LETTER_BLOB, LETTER_CALLBACK } letter_type; typedef struct letter { letter_type type; union { blob *blob_data; JSValue callback; }; } letter; /* I/O watch entry — one per watched file descriptor */ typedef struct { int fd; short events; /* POLLIN, POLLOUT */ JSContext *actor; JSValue callback; } io_watch; /* Actor state machine constants */ #define ACTOR_IDLE 0 #define ACTOR_READY 1 #define ACTOR_RUNNING 2 #define ACTOR_EXHAUSTED 3 #define ACTOR_RECLAIMING 4 #define ACTOR_SLOW 5 #define ACTOR_REFRESHED 6 #define ACTOR_FAST_TIMER_NS (1000ULL * 1000000) #define ACTOR_SLOW_TIMER_NS (60000ULL * 1000000) #define ACTOR_SLOW_STRIKES_MAX 3 #define ACTOR_MEMORY_LIMIT (1024ULL * 1024 * 1024) struct JSContext { JSRuntime *rt; /* Actor memory block (bump allocation) */ uint8_t *heap_base; /* start of current block */ uint8_t *heap_free; /* bump pointer */ uint8_t *heap_end; /* end of block */ size_t current_block_size; /* current block size (64KB initially) */ size_t next_block_size; /* doubles if <10% recovered after GC */ int gc_poor_streak; /* consecutive poor-recovery GC cycles */ /* GC stats (lightweight, always on) */ uint64_t gc_count; /* number of GC cycles */ uint64_t gc_bytes_copied; /* total bytes copied across all GCs */ /* Constant text pool — compilation constants */ uint8_t *ct_base; /* pool base */ uint8_t *ct_free; /* pool bump pointer */ uint8_t *ct_end; /* pool end */ /* Constant text intern table */ void *ct_pages; /* page list for large allocations */ uint32_t *ct_hash; /* hash table (slot -> id) */ JSText **ct_array; /* array of JSText pointers indexed by id */ uint32_t ct_size; /* hash table size (power of 2) */ uint32_t ct_count; /* number of interned texts */ uint32_t ct_resize_threshold; /* threshold for resize */ uint16_t binary_object_count; int binary_object_size; /* Record key allocator */ uint32_t rec_key_next; JSGCRef *top_gc_ref; /* used to reference temporary GC roots (stack top) */ JSGCRef *last_gc_ref; /* used to reference temporary GC roots (list) */ JSLocalRef *top_local_ref; /* for JS_LOCAL macro - GC updates C locals through pointers */ CCallRoot *c_call_root; /* stack of auto-rooted C call argv arrays */ void *native_state; /* qbe_helpers.c per-actor native runtime state */ int class_count; /* size of class_array and class_proto */ JSClass *class_array; JSValue *class_proto; JSValue regexp_ctor; JSValue throw_type_error; JSValue global_obj; /* global object (immutable intrinsics) */ uint64_t random_state; /* 0 = normal, 1 = suspend (fast timer), 2 = kill (slow timer) */ _Atomic int pause_flag; /* if NULL, RegExp compilation is not supported */ JSValue (*compile_regexp) (JSContext *ctx, JSValue pattern, JSValue flags); /* GC callback to scan external C-side roots (actor letters, timers) */ void (*gc_scan_external)(JSContext *ctx, uint8_t *from_base, uint8_t *from_end, uint8_t *to_base, uint8_t **to_free, uint8_t *to_end); js_hook trace_hook; int trace_type; void *trace_data; /* Register VM frame root (updated by GC when frame moves) */ JSValue reg_current_frame; /* current JSFrameRegister being executed */ uint32_t current_register_pc; /* PC at exception time */ /* VM suspend/resume state */ int suspended; /* 1 = VM was suspended (not exception) */ JSGCRef suspended_frame_ref; /* GC-rooted saved frame for resume */ uint32_t suspended_pc; /* saved PC for resume */ int vm_call_depth; /* 0 = pure bytecode, >0 = C frames on stack */ size_t heap_memory_limit; /* 0 = no limit, else max heap bytes */ const char *actor_label; /* human-readable label for OOM diagnostics */ JSValue current_exception; JS_BOOL disruption_reported; /* Log routing — set by $_set_log intrinsic */ JSLogCallback log_callback; JSValue log_callback_js; /* the ƿit log function (rooted) */ /* Actor identity key — used by wota/nota PRIVATE serialization */ JSValue actor_sym; /* Stack overflow protection */ // todo: want this, but should be a simple increment/decrement counter while frames are pushed size_t stack_depth; size_t stack_limit; /* === Actor fields (merged from cell_rt) === */ JSGCRef idx_buffer_ref; JSGCRef on_exception_ref; JSGCRef message_handle_ref; JSGCRef unneeded_ref; JSGCRef actor_sym_ref; void *init_wota; #ifndef TARGET_PLAYDATE pthread_mutex_t *mutex; pthread_mutex_t *msg_mutex; #endif char *id; int idx_count; letter *letters; struct { uint32_t key; JSValue value; } *timers; int state; uint32_t ar; double ar_secs; int disrupt; int is_quiescent; int main_thread_only; int affinity; uint64_t turn_start_ns; _Atomic uint32_t turn_gen; int slow_strikes; int vm_suspended; const char *name; cell_hook actor_trace_hook; }; /* ============================================================ Functions that need JSContext definition ============================================================ */ /* Convert JSValue key (string) to C string in buffer. Returns buf, filled with the string content or "[?]" if not a string. */ static inline const char *JS_KeyGetStr (JSContext *ctx, char *buf, size_t buf_size, JSValue key) { if (JS_IsText (key)) { const char *cstr = JS_ToCString (ctx, key); if (cstr) { snprintf (buf, buf_size, "%s", cstr); JS_FreeCString (ctx, cstr); return buf; } } snprintf (buf, buf_size, "[?]"); return buf; } /* ============================================================ Actor / Scheduler Declarations (merged from cell_internal.h) ============================================================ */ /* Set by actor_turn/CLI before entering the VM, cleared after. Read by signal_handler to print JS stack on crash. */ extern volatile JSContext *g_crash_ctx; JSContext *create_actor(void *wota); const char *register_actor(const char *id, JSContext *actor, int mainthread, double ar); void actor_disrupt(JSContext *actor); const char *send_message(const char *id, void *msg); void actor_unneeded(JSContext *actor, JSValue fn, double seconds); void script_startup(JSContext *ctx); int uncaught_exception(JSContext *js, JSValue v); int actor_exists(const char *id); void set_actor_state(JSContext *actor); void enqueue_actor_priority(JSContext *actor); void actor_clock(JSContext *actor, JSValue fn); uint32_t actor_delay(JSContext *actor, JSValue fn, double seconds); JSValue actor_remove_timer(JSContext *actor, uint32_t timer_id); void actor_watch(JSContext *actor, int fd, short events, JSValue fn); void actor_watch_readable(JSContext *actor, int fd, JSValue fn); void actor_watch_writable(JSContext *actor, int fd, JSValue fn); void actor_unwatch(JSContext *actor, int fd); void exit_handler(void); void actor_loop(void); void actor_initialize(void); void actor_free(JSContext *actor); void actor_gc_scan(JSContext *ctx, uint8_t *fb, uint8_t *fe, uint8_t *tb, uint8_t **tf, uint8_t *te); int scheduler_actor_count(void); void scheduler_enable_quiescence(void); JSValue JS_ResumeRegisterVM(JSContext *ctx); uint64_t cell_ns(void); void cell_sleep(double seconds); int randombytes(void *buf, size_t n); /* ============================================================ Constant Text Pool Functions ============================================================ */ /* Constant text page for large allocations */ typedef struct CTPage { struct CTPage *next; size_t size; uint8_t data[]; } CTPage; /* Initial constant text table size */ #define CT_INITIAL_SIZE 256 /* Allocate from constant text pool */ /* Resize the constant text intern hash table */ /* Realloc with slack reporting (for bump allocator) WARNING: This function is NOT GC-safe! The caller must protect the source object with a GC ref before calling, and re-chase the pointer after. */ /* ============================================================ GC Public API ============================================================ */ /* Forward declaration for ctx_gc */ static int ctx_gc (JSContext *ctx, int allow_grow, size_t alloc_size); /* JS_MarkValue - mark a value during GC traversal. With copying GC, this is a no-op as we discover live objects by tracing. */ /* Helper to check if a pointer is in constant text pool memory */ static inline int is_ct_ptr (JSContext *ctx, void *ptr) { uint8_t *p = (uint8_t *)ptr; if (p >= ctx->ct_base && p < ctx->ct_end) return 1; /* Also check overflow pages */ CTPage *page = (CTPage *)ctx->ct_pages; while (page) { if (p >= page->data && p < page->data + page->size) return 1; page = page->next; } return 0; } #ifdef HEAP_CHECK static inline objhdr_t *heap_check_chase(JSContext *ctx, JSValue v) { objhdr_t *oh = chase(v); uint8_t *p = (uint8_t *)oh; if (!((p >= ctx->heap_base && p < ctx->heap_free) || (p >= ctx->ct_base && p < ctx->ct_end))) heap_check_fail(oh, ctx); return oh; } #endif /* Intern a UTF-32 string as a stone text, returning a JSValue string */ /* Create a stoned, interned key from a UTF-8 C string. Returns immediate ASCII text if ≤7 ASCII chars, otherwise stoned interned text. The returned JSValue does NOT need to be freed (it's either immediate or part of the stone arena). */ /* Create a key from a UTF-8 string with explicit length */ typedef union JSFloat64Union { double d; uint64_t u64; uint32_t u32[2]; } JSFloat64Union; /* JS_IsText is defined in quickjs.h */ /* Helper to get array capacity from mist_hdr */ static inline uint32_t js_array_cap (JSArray *arr) { return objhdr_cap56 (arr->mist_hdr); } /* JSRegExp: regular expression object data (must come before JSRecord/JSRecord) */ typedef struct JSRegExp { char *pattern; /* UTF-8, null-terminated, js_malloc_rt'd */ uint32_t pattern_len; uint8_t *bytecode; /* raw lre bytecode, js_malloc_rt'd */ uint32_t bytecode_len; } JSRegExp; #define obj_is_stone(rec) objhdr_s ((rec)->mist_hdr) #define obj_set_stone(rec) ((rec)->mist_hdr = objhdr_set_s ((rec)->mist_hdr, true)) /* Get prototype from object (works for both JSRecord and JSRecord since they * share layout) */ #define JS_OBJ_GET_PROTO(p) (JS_IsNull(((JSRecord *)(p))->proto) ? NULL : (JSRecord *)JS_VALUE_GET_PTR(((JSRecord *)(p))->proto)) /* Initial capacity for new records (mask = 7, 8 slots total) */ #define JS_RECORD_INITIAL_MASK 7 /* ============================================================ JSRecord Core Operations ============================================================ */ // can check if key by checking for 0 here /* Compare a JSValue key with a C string literal. Used for comparing with internal names that are too long for immediate ASCII. */ /* GC-SAFE: No allocations. Caller must pass freshly-chased rec. Find slot for a key in record's own table. Returns slot index (>0) if found, or -(insert_slot) if not found. */ /* GC-SAFE: No allocations. Walks prototype chain via direct pointers. Caller must pass freshly-chased rec. */ /* GC-SAFE: Resize record by allocating a new larger record and copying all data. Uses GC ref to protect source during allocation. Updates *pobj to point to the new record. Returns 0 on success, -1 on failure. */ /* GC-SAFE: May call rec_resize which allocates. Takes JSValue* so the object can be tracked through GC. Updates *pobj if the record is resized. */ /* Helper macros to access class_id and rec_id from slots[0].key per memory.md: - Low 32 bits of slots[0].key = class_id - High 32 bits of slots[0].key = rec_id - slots[0].val = opaque C pointer */ #define REC_GET_CLASS_ID(rec) ((uint32_t)((rec)->slots[0].key & 0xFFFFFFFF)) #define REC_GET_REC_ID(rec) ((uint32_t)((rec)->slots[0].key >> 32)) #define REC_SET_CLASS_ID(rec, cid) do { \ (rec)->slots[0].key = ((rec)->slots[0].key & 0xFFFFFFFF00000000ULL) | (uint64_t)(cid); \ } while(0) #define REC_SET_REC_ID(rec, rid) do { \ (rec)->slots[0].key = ((rec)->slots[0].key & 0xFFFFFFFF) | ((uint64_t)(rid) << 32); \ } while(0) #define REC_GET_OPAQUE(rec) ((void*)(uintptr_t)(rec)->slots[0].val) #define REC_SET_OPAQUE(rec, ptr) do { (rec)->slots[0].val = (JSValue)(uintptr_t)(ptr); } while(0) /* Allocate a new record with specified class_id. Uses bump allocation. Slots are inline (flexible array member). Per memory.md: slots[0] is reserved for class_id, rec_id, and opaque. */ typedef enum { JS_FUNC_KIND_C, JS_FUNC_KIND_BYTECODE, JS_FUNC_KIND_C_DATA, JS_FUNC_KIND_REGISTER, /* register-based VM function */ JS_FUNC_KIND_NATIVE, /* QBE-compiled native function */ } JSFunctionKind; typedef enum { JS_CODE_KIND_REGISTER = 1, JS_CODE_KIND_NATIVE = 2, } JSCodeKind; typedef struct JSCode { objhdr_t header; /* OBJ_CODE */ uint8_t kind; int16_t arity; union { struct { JSCodeRegister *code; } reg; struct { void *fn_ptr; /* compiled cell_fn_N pointer */ void *dl_handle; /* dylib handle for dlsym lookups */ uint16_t nr_slots; /* frame size for this function */ } native; } u; } JSCode; typedef struct JSFunction { objhdr_t header; /* must come first */ JSValue name; /* function name as JSValue text */ int16_t length; /* arity: max allowed arguments (-1 = variadic) */ uint8_t kind; union { struct { JSCFunctionType c_function; uint8_t cproto; int16_t magic; } cfunc; struct { JSValue code; /* JSCode object (OBJ_CODE) */ JSValue env_record; /* stone record, module environment */ JSValue outer_frame; /* JSFrame JSValue, for closures */ } cell; } u; } JSFunction; #define FN_READ_CODE(fn) ((fn)->u.cell.code) /* ============================================================ Context-Neutral Module Format (Phase 2+) Struct definitions are in quickjs.h ============================================================ */ #define JS_PROP_INITIAL_SIZE 2 #define JS_PROP_INITIAL_HASH_SIZE 4 /* must be a power of two */ #define JS_ARRAY_INITIAL_SIZE 2 /* Max capacity depends on platform - cap field size varies */ #ifdef JS_PTR64 #define JS_ARRAY_MAX_CAP ((word_t)((1ULL << 56) - 1)) #else #define JS_ARRAY_MAX_CAP ((word_t)((1UL << 24) - 1)) #endif JSValue js_call_c_function (JSContext *ctx, JSValue func_obj, JSValue this_obj, int argc, JSValue *argv); JSValue JS_CallInternal (JSContext *ctx, JSValue func_obj, JSValue this_obj, int argc, JSValue *argv, int flags); JSValue JS_CallRegisterVM(JSContext *ctx, JSCodeRegister *code, JSValue this_obj, int argc, JSValue *argv, JSValue env, JSValue outer_frame); JSValue cell_native_dispatch(JSContext *ctx, JSValue func_obj, JSValue this_obj, int argc, JSValue *argv); int JS_DeleteProperty (JSContext *ctx, JSValue obj, JSValue prop); JSValue __attribute__ ((format (printf, 2, 3))) JS_RaiseDisrupt (JSContext *ctx, const char *fmt, ...); __maybe_unused void JS_DumpString (JSRuntime *rt, const JSText *text); __maybe_unused void JS_DumpObjectHeader (JSRuntime *rt); __maybe_unused void JS_DumpObject (JSRuntime *rt, JSRecord *rec); __maybe_unused void JS_DumpGCObject (JSRuntime *rt, objhdr_t *p); __maybe_unused void JS_DumpValue (JSContext *ctx, const char *str, JSValue val); void js_dump_value_write (void *opaque, const char *buf, size_t len); void js_regexp_finalizer (JSRuntime *rt, JSValue val); JSValue js_new_function (JSContext *ctx, JSFunctionKind kind); /* Forward declaration - helper to set cap in objhdr */ static inline objhdr_t objhdr_set_cap56 (objhdr_t h, uint64_t cap); /* GC-SAFE: JS_SetPropertyInternal: same as JS_SetProperty but doesn't check stone. Internal use only. May trigger GC if record needs to resize. */ blob *js_get_blob (JSContext *ctx, JSValue val); JSValue js_new_string8_len (JSContext *ctx, const char *buf, int len); JSValue pretext_end (JSContext *ctx, JSText *s); JSValue js_compile_regexp (JSContext *ctx, JSValue pattern, JSValue flags); JSValue js_regexp_constructor_internal (JSContext *ctx, JSValue pattern, JSValue bc); int JS_NewClass1 (JSContext *ctx, JSClassID class_id, const JSClassDef *class_def, const char *name); BOOL js_strict_eq (JSContext *ctx, JSValue op1, JSValue op2); static JSValue js_cell_text (JSContext *ctx, JSValue this_val, int argc, JSValue *argv); static JSValue js_cell_push (JSContext *ctx, JSValue this_val, int argc, JSValue *argv); static JSValue js_cell_pop (JSContext *ctx, JSValue this_val, int argc, JSValue *argv); static JSValue js_cell_array_find (JSContext *ctx, JSValue this_val, int argc, JSValue *argv); static JSValue js_cell_stone (JSContext *ctx, JSValue this_val, int argc, JSValue *argv); static JSValue js_cell_length (JSContext *ctx, JSValue this_val, int argc, JSValue *argv); static JSValue js_cell_reverse (JSContext *ctx, JSValue this_val, int argc, JSValue *argv); static JSValue js_cell_proto (JSContext *ctx, JSValue this_val, int argc, JSValue *argv); static JSValue js_cell_splat (JSContext *ctx, JSValue this_val, int argc, JSValue *argv); static JSValue js_cell_meme (JSContext *ctx, JSValue this_val, int argc, JSValue *argv); static JSValue js_cell_fn_apply (JSContext *ctx, JSValue this_val, int argc, JSValue *argv); static JSValue js_cell_call (JSContext *ctx, JSValue this_val, int argc, JSValue *argv); static JSValue js_cell_not (JSContext *ctx, JSValue this_val, int argc, JSValue *argv); JSValue js_cell_text_lower (JSContext *ctx, JSValue this_val, int argc, JSValue *argv); JSValue js_cell_text_upper (JSContext *ctx, JSValue this_val, int argc, JSValue *argv); static JSValue js_cell_text_trim (JSContext *ctx, JSValue this_val, int argc, JSValue *argv); JSValue js_cell_text_codepoint (JSContext *ctx, JSValue this_val, int argc, JSValue *argv); static JSValue js_cell_text_replace (JSContext *ctx, JSValue this_val, int argc, JSValue *argv); static JSValue js_cell_text_search (JSContext *ctx, JSValue this_val, int argc, JSValue *argv); static JSValue js_cell_text_extract (JSContext *ctx, JSValue this_val, int argc, JSValue *argv); JSValue js_cell_character (JSContext *ctx, JSValue this_val, int argc, JSValue *argv); static JSValue js_cell_number (JSContext *ctx, JSValue this_val, int argc, JSValue *argv); static JSValue js_cell_object (JSContext *ctx, JSValue this_val, int argc, JSValue *argv); static JSValue js_cell_text_format (JSContext *ctx, JSValue this_val, int argc, JSValue *argv); JSValue JS_GetStack(JSContext *ctx); JSValue JS_RaiseOOM (JSContext *ctx); JSValue JS_ToNumber (JSContext *ctx, JSValue val); int JS_SetPropertyValue (JSContext *ctx, JSValue this_obj, JSValue prop, JSValue val); int JS_GetOwnPropertyInternal (JSContext *ctx, JSValue *desc, JSRecord *p, JSValue prop); /* JS_AddIntrinsicBasicObjects is declared in quickjs.h */ __exception int js_get_length32 (JSContext *ctx, uint32_t *pres, JSValue obj); __exception int js_get_length64 (JSContext *ctx, int64_t *pres, JSValue obj); void free_arg_list (JSContext *ctx, JSValue *tab, uint32_t len); JSValue *build_arg_list (JSContext *ctx, uint32_t *plen, JSValue *parray_arg); JSValue js_regexp_toString (JSContext *ctx, JSValue this_val, int argc, JSValue *argv); /* === Inline utility functions (used across modules) === */ static inline int is_digit (int c) { return c >= '0' && c <= '9'; } static inline int string_get (const JSText *p, int idx) { int word_idx = idx >> 1; int shift = (1 - (idx & 1)) * 32; return (uint32_t)((p->packed[word_idx] >> shift) & 0xFFFFFFFF); } static inline void string_put (JSText *p, int idx, uint32_t c) { int word_idx = idx >> 1; int shift = (1 - (idx & 1)) * 32; uint64_t mask = 0xFFFFFFFFULL << shift; p->packed[word_idx] = (p->packed[word_idx] & ~mask) | ((uint64_t)c << shift); } /* Get character from any string value (immediate ASCII or JSText) */ static inline uint32_t js_string_value_get (JSValue v, int idx) { if (MIST_IsImmediateASCII (v)) { return MIST_GetImmediateASCIIChar (v, idx); } else { JSText *s = JS_VALUE_GET_TEXT (v); return string_get (s, idx); } } /* Get length from any string value */ static inline int js_string_value_len (JSValue v) { if (MIST_IsImmediateASCII (v)) { return MIST_GetImmediateASCIILen (v); } else { return JSText_len (JS_VALUE_GET_TEXT (v)); } } /* System allocator wrappers (not GC-managed) */ static inline void sys_free (void *ptr) { free (ptr); } static inline void *sys_malloc (size_t size) { return malloc (size); } static inline void *sys_realloc (void *ptr, size_t size) { return realloc (ptr, size); } /* Parser system allocator wrappers (not GC-managed) */ static inline void *pjs_malloc (size_t size) { return malloc (size); } static inline void *pjs_mallocz (size_t size) { void *ptr = malloc (size); if (ptr) memset (ptr, 0, size); return ptr; } static inline void *pjs_realloc (void *ptr, size_t size) { return realloc (ptr, size); } static inline void pjs_free (void *ptr) { free (ptr); } static inline int pjs_realloc_array (void **parray, int elem_size, int *psize, int req_size) { int new_size; void *new_array; size_t old_size; new_size = max_int (req_size, *psize * 3 / 2); old_size = (size_t)(*psize) * elem_size; new_array = pjs_realloc (*parray, (size_t)new_size * elem_size); if (!new_array) return -1; if (new_size > *psize) { memset ((char *)new_array + old_size, 0, (size_t)(new_size - *psize) * elem_size); } *psize = new_size; *parray = new_array; return 0; } static inline int pjs_resize_array (void **parray, int elem_size, int *psize, int req_size) { if (unlikely (req_size > *psize)) return pjs_realloc_array (parray, elem_size, psize, req_size); else return 0; } /* set the new value and free the old value after */ static inline void set_value (JSContext *ctx, JSValue *pval, JSValue new_val) { (void)ctx; *pval = new_val; } int cell_rt_native_active(JSContext *ctx); static inline __exception int js_poll_interrupts (JSContext *ctx) { if (unlikely (atomic_load_explicit (&ctx->pause_flag, memory_order_relaxed) >= 2)) { JS_RaiseDisrupt (ctx, "interrupted"); return -1; } return 0; } /* === PPretext (parser pretext, system-malloc, used by cell_js.c parser) === */ typedef struct PPretext { uint32_t *data; int len; int cap; } PPretext; extern JSClassID js_class_id_alloc; /* === Forward declarations for functions split across modules === */ /* runtime.c — line/column, GC, and VM dispatch */ int ctx_gc (JSContext *ctx, int allow_grow, size_t alloc_size); JSValue JS_CallInternal (JSContext *ctx, JSValue func_obj, JSValue this_obj, int argc, JSValue *argv, int flags); /* runtime.c exports */ int js_string_compare_value (JSContext *ctx, JSValue op1, JSValue op2, BOOL eq_only); int js_string_compare_value_nocase (JSContext *ctx, JSValue op1, JSValue op2); JSValue js_regexp_constructor (JSContext *ctx, JSValue this_val, int argc, JSValue *argv); int JS_HasProperty (JSContext *ctx, JSValue obj, JSValue prop); int JS_HasPropertyKey (JSContext *ctx, JSValue obj, JSValue key); JSValue JS_GetPropertyKey (JSContext *ctx, JSValue this_obj, JSValue key); int JS_SetPropertyKey (JSContext *ctx, JSValue this_obj, JSValue key, JSValue val); void *js_realloc_rt (void *ptr, size_t size); char *js_strdup_rt (const char *str); JSValue JS_ConcatString (JSContext *ctx, JSValue op1, JSValue op2); JSValue JS_ConcatStringGrow (JSContext *ctx, JSValue op1, JSValue op2); JSText *pretext_init (JSContext *ctx, int capacity); JSText *pretext_putc (JSContext *ctx, JSText *s, uint32_t c); JSText *pretext_concat_value (JSContext *ctx, JSText *s, JSValue v); JSValue js_new_blob (JSContext *ctx, blob *b); /* Functions from header region (defined in runtime.c) */ void *ct_alloc (JSContext *ctx, size_t bytes, size_t align); void ct_free_all (JSContext *ctx); int ct_resize (JSContext *ctx); JSValue intern_text_to_value (JSContext *ctx, const uint32_t *utf32, uint32_t len); JSValue js_key_new (JSContext *ctx, const char *str); JSValue js_key_new_len (JSContext *ctx, const char *str, size_t len); uint64_t js_key_hash (JSValue key); JS_BOOL js_key_equal (JSValue a, JSValue b); JS_BOOL js_key_equal_str (JSValue a, const char *str); int rec_find_slot (JSRecord *rec, JSValue k); JSValue rec_get (JSContext *ctx, JSRecord *rec, JSValue k); int rec_resize (JSContext *ctx, JSValue *pobj, uint64_t new_mask); int rec_set_own (JSContext *ctx, JSValue *pobj, JSValue k, JSValue val); JSRecord *js_new_record_class (JSContext *ctx, uint32_t initial_mask, JSClassID class_id); int JS_SetPropertyInternal (JSContext *ctx, JSValue this_obj, JSValue prop, JSValue val); uint64_t get_text_hash (JSText *text); void pack_utf32_to_words (const uint32_t *utf32, uint32_t len, uint64_t *packed); int text_equal (JSText *a, const uint64_t *packed_b, uint32_t len_b); JSValue gc_copy_value (JSContext *ctx, JSValue v, uint8_t *from_base, uint8_t *from_end, uint8_t *to_base, uint8_t **to_free, uint8_t *to_end); PPretext *ppretext_init (int capacity); PPretext *ppretext_putc (PPretext *p, uint32_t c); void ppretext_free (PPretext *p); JSValue ppretext_end (JSContext *ctx, PPretext *p); PPretext *ppretext_append_jsvalue (PPretext *p, JSValue str); PPretext *ppretext_append_int (PPretext *p, int n); JSValue js_atof (JSContext *ctx, const char *str, const char **pp, int radix, int flags); #define ATOD_INT_ONLY (1 << 0) /* accept Oo and Ob prefixes in addition to 0x prefix if radix = 0 */ #define ATOD_ACCEPT_BIN_OCT (1 << 2) /* accept O prefix as octal if radix == 0 and properly formed (Annex B) */ #define ATOD_ACCEPT_LEGACY_OCTAL (1 << 4) /* accept _ between digits as a digit separator */ #define ATOD_ACCEPT_UNDERSCORES (1 << 5) /* allow a suffix to override the type */ #define ATOD_ACCEPT_SUFFIX (1 << 6) /* default type */ #define ATOD_TYPE_MASK (3 << 7) #define ATOD_TYPE_FLOAT64 (0 << 7) #define ATOD_TYPE_BIG_INT (1 << 7) /* accept -0x1 */ #define ATOD_ACCEPT_PREFIX_AFTER_SIGN (1 << 10) static inline int to_digit (int c) { if (c >= '0' && c <= '9') return c - '0'; else if (c >= 'A' && c <= 'Z') return c - 'A' + 10; else if (c >= 'a' && c <= 'z') return c - 'a' + 10; else return 36; } JSText *js_alloc_string (JSContext *ctx, int max_len); JSValue js_key_from_string (JSContext *ctx, JSValue val); /* mach.c exports */ JSValue JS_CallRegisterVM(JSContext *ctx, JSCodeRegister *code, JSValue this_obj, int argc, JSValue *argv, JSValue env, JSValue outer_frame); JSValue js_new_native_function(JSContext *ctx, void *fn_ptr, void *dl_handle, uint16_t nr_slots, int arity, JSValue outer_frame, JSValue env); JSValue js_new_native_function_with_code(JSContext *ctx, JSValue code_obj, int arity, JSValue outer_frame, JSValue env_record); JSFrameRegister *alloc_frame_register(JSContext *ctx, int slot_count); void cell_rt_free_native_state(JSContext *ctx); #endif /* QUICKJS_INTERNAL_H */