parse.ce and tokenize.ce

This commit is contained in:
2026-02-09 11:56:09 -06:00
parent 45556c344d
commit 368511f666
6 changed files with 3002 additions and 19 deletions

View File

@@ -6204,6 +6204,13 @@ static int js_json_to_str (JSContext *ctx, JSONStringifyContext *jsc, JSValue ho
goto exception;
}
/* Heap strings are JS_TAG_PTR but must be quoted, not iterated as objects */
if (JS_IsText (val_ref.val) && !MIST_IsImmediateASCII (val_ref.val)) {
val_ref.val = JS_ToQuotedString (ctx, val_ref.val);
if (JS_IsException (val_ref.val)) goto exception;
goto concat_value;
}
if (JS_IsObject (
val_ref.val)) { /* includes arrays (OBJ_ARRAY) since they have JS_TAG_PTR */
v = js_array_includes (ctx, jsc->stack, 1, &val_ref.val);
@@ -9013,17 +9020,28 @@ static JSValue js_cell_array (JSContext *ctx, JSValue this_val, int argc, JSValu
if (argc < 2 || JS_IsNull (argv[1])) {
/* Split into characters */
JSValue result = JS_NewArrayLen (ctx, len);
if (JS_IsException (result)) { return result; }
JSArray *out = JS_VALUE_GET_ARRAY (result);
JSGCRef arr_ref, str_ref;
JS_PushGCRef (ctx, &arr_ref);
JS_PushGCRef (ctx, &str_ref);
str_ref.val = arg;
arr_ref.val = JS_NewArray (ctx);
if (JS_IsException (arr_ref.val)) {
JS_PopGCRef (ctx, &str_ref);
JS_PopGCRef (ctx, &arr_ref);
return JS_EXCEPTION;
}
for (int i = 0; i < len; i++) {
JSValue ch = js_sub_string_val (ctx, arg, i, i + 1);
JSValue ch = js_sub_string_val (ctx, str_ref.val, i, i + 1);
if (JS_IsException (ch)) {
JS_PopGCRef (ctx, &str_ref);
JS_PopGCRef (ctx, &arr_ref);
return JS_EXCEPTION;
}
out->values[i] = ch;
JS_ArrayPush (ctx, &arr_ref.val, ch);
}
out->len = len;
JSValue result = arr_ref.val;
JS_PopGCRef (ctx, &str_ref);
JS_PopGCRef (ctx, &arr_ref);
return result;
}
@@ -11404,11 +11422,11 @@ static JSValue js_cell_length (JSContext *ctx, JSValue this_val, int argc, JSVal
int tag = JS_VALUE_GET_TAG (val);
/* Strings return codepoint count */
if (tag == JS_TAG_STRING_IMM) {
if (MIST_IsImmediateASCII (val)) {
return JS_NewInt32 (ctx, MIST_GetImmediateASCIILen (val));
}
if (tag == JS_TAG_STRING) {
JSText *p = JS_VALUE_GET_STRING (val);
if (JS_IsPtr (val) && objhdr_type (*chase (val)) == OBJ_TEXT) {
JSText *p = (JSText *)chase (val);
return JS_NewInt32 (ctx, (int)JSText_len (p));
}
@@ -11582,8 +11600,7 @@ static JSValue js_cell_is_stone (JSContext *ctx, JSValue this_val, int argc, JSV
/* is_text(val) */
static JSValue js_cell_is_text (JSContext *ctx, JSValue this_val, int argc, JSValue *argv) {
if (argc < 1) return JS_FALSE;
int tag = JS_VALUE_GET_TAG (argv[0]);
return JS_NewBool (ctx, tag == JS_TAG_STRING || tag == JS_TAG_STRING_IMM);
return JS_NewBool (ctx, JS_IsText (argv[0]));
}
/* is_proto(val, master) - check if val has master in prototype chain */
@@ -11737,6 +11754,10 @@ static JSValue js_cell_some(JSContext *ctx, JSValue this_val, int argc, JSValue
/* GC-SAFE: Helper to set a global function. Creates function first, then reads
ctx->global_obj to ensure it's not stale if GC ran during function creation. */
int JS_SetGlobalStr (JSContext *ctx, const char *prop, JSValue val) {
return JS_SetPropertyStr(ctx, ctx->global_obj, prop, val);
}
static void js_set_global_cfunc(JSContext *ctx, const char *name, JSCFunction *func, int length) {
JSGCRef ref;
JS_PushGCRef(ctx, &ref);
@@ -11799,7 +11820,7 @@ static void JS_AddIntrinsicBaseObjects (JSContext *ctx) {
/* Core functions - using GC-safe helper */
js_set_global_cfunc(ctx, "eval", js_cell_eval, 2);
js_set_global_cfunc(ctx, "mach_eval", js_mach_eval, 2);
js_set_global_cfunc(ctx, "mach_eval", js_mach_eval, 3);
js_set_global_cfunc(ctx, "stone", js_cell_stone, 1);
js_set_global_cfunc(ctx, "length", js_cell_length, 1);
js_set_global_cfunc(ctx, "call", js_cell_call, 3);