From 77c02bf9bf1f2d562038e28007da37c9191b369d Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Fri, 13 Feb 2026 05:59:01 -0600 Subject: [PATCH] simplify text --- source/runtime.c | 101 ++++++++++------------------------------------- 1 file changed, 20 insertions(+), 81 deletions(-) diff --git a/source/runtime.c b/source/runtime.c index 232aa778..c02e2d3d 100644 --- a/source/runtime.c +++ b/source/runtime.c @@ -1732,21 +1732,6 @@ static JSValue js_sub_string (JSContext *ctx, JSText *p, int start, int end) { return JS_MKPTR (p); } - /* Try immediate ASCII for short substrings (avoids heap allocation) */ - if (len <= MIST_ASCII_MAX_LEN && len > 0) { - char buf[MIST_ASCII_MAX_LEN]; - int all_ascii = 1; - for (i = 0; i < len; i++) { - uint32_t c = string_get (p, start + i); - if (c >= 128) { all_ascii = 0; break; } - buf[i] = (char)c; - } - if (all_ascii) { - JSValue imm = MIST_TryNewImmediateASCII (buf, len); - if (!JS_IsNull (imm)) return imm; - } - } - /* Root the source string as a JSValue so it survives js_alloc_string GC */ JSGCRef src_ref; JS_PushGCRef (ctx, &src_ref); @@ -1775,20 +1760,10 @@ static JSValue js_sub_string_val (JSContext *ctx, JSValue src, int start, int en if (len <= 0) return JS_NewString (ctx, ""); if (MIST_IsImmediateASCII (src)) { - /* IMM: extract chars directly, try to return IMM */ - if (len <= MIST_ASCII_MAX_LEN) { - char buf[MIST_ASCII_MAX_LEN + 1]; - for (int i = 0; i < len; i++) - buf[i] = (char)MIST_GetImmediateASCIIChar (src, start + i); - return js_new_string8_len (ctx, buf, len); - } - /* Longer than 7 — shouldn't happen for IMM (max 7 chars) but handle it */ - JSText *str = js_alloc_string (ctx, len); - if (!str) return JS_EXCEPTION; + char buf[MIST_ASCII_MAX_LEN + 1]; for (int i = 0; i < len; i++) - string_put (str, i, MIST_GetImmediateASCIIChar (src, start + i)); - str->length = len; - return pretext_end (ctx, str); + buf[i] = (char)MIST_GetImmediateASCIIChar (src, start + i); + return js_new_string8_len (ctx, buf, len); } /* Heap string — delegate to existing js_sub_string */ @@ -1914,7 +1889,7 @@ JSText *pretext_concat_value (JSContext *ctx, JSText *s, JSValue v) { return pretext_write8 (ctx, s, (const uint8_t *)buf, len); } if (JS_IsText (v)) { - JSText *p = JS_VALUE_GET_PTR (v); + JSText *p = JS_VALUE_GET_STRING (v); return pretext_concat (ctx, s, p, 0, (uint32_t)JSText_len (p)); } JSValue v1 = JS_ToString (ctx, v); @@ -1942,6 +1917,20 @@ JSValue pretext_end (JSContext *ctx, JSText *s) { js_free (ctx, s); return JS_KEY_empty; } + /* Promote short ASCII strings to immediate values */ + if (len <= MIST_ASCII_MAX_LEN) { + char buf[MIST_ASCII_MAX_LEN]; + int all_ascii = 1; + for (int i = 0; i < len; i++) { + uint32_t c = string_get (s, i); + if (c >= 0x80) { all_ascii = 0; break; } + buf[i] = (char)c; + } + if (all_ascii) { + JSValue imm = MIST_TryNewImmediateASCII (buf, len); + if (!JS_IsNull (imm)) return imm; + } + } /* Set final length in capacity field and clear length for hash storage */ s->hdr = objhdr_set_cap56 (s->hdr, len); s->length = 0; @@ -2120,50 +2109,6 @@ void JS_FreeCString (JSContext *ctx, const char *ptr) { (void)ptr; } -JSValue JS_ConcatString1 (JSContext *ctx, const JSText *p1, const JSText *p2) { - JSText *p; - uint32_t len; - int len1 = (int)JSText_len (p1); - int len2 = (int)JSText_len (p2); - - len = len1 + len2; - /* len is uint32_t, JS_STRING_LEN_MAX is 56 bits, so this always fits */ - p = js_alloc_string (ctx, len); - if (!p) return JS_EXCEPTION; - /* Pack first string */ - { - int i; - for (i = 0; i < len1; i++) - string_put (p, i, string_get (p1, i)); - for (i = 0; i < len2; i++) - string_put (p, len1 + i, string_get (p2, i)); - } - return JS_MKPTR (p); -} - -// TODO: this function is fucked. -BOOL JS_ConcatStringInPlace (JSContext *ctx, JSText *p1, JSValue op2) { - (void)ctx; - if (JS_VALUE_GET_TAG (op2) == JS_TAG_STRING) { - JSText *p2 = JS_VALUE_GET_STRING (op2); - int64_t new_len; - int64_t len1 = JSText_len (p1); - int64_t len2 = JSText_len (p2); - - if (len2 == 0) return TRUE; - - new_len = len1 + len2; - - /* Append p2's characters using string_put/string_get */ - for (int64_t i = 0; i < len2; i++) { - string_put (p1, len1 + i, string_get (p2, i)); - } - p1->hdr = objhdr_set_cap56 (p1->hdr, new_len); - return TRUE; - } - return FALSE; -} - /* Helper for string value comparison (handles immediate and heap strings) */ int js_string_compare_value (JSContext *ctx, JSValue op1, JSValue op2, BOOL eq_only) { (void)ctx; @@ -2270,7 +2215,7 @@ JSValue JS_ConcatString (JSContext *ctx, JSValue op1, JSValue op2) { string_put (p, len1 + i, js_string_value_get (op2, i)); } p->length = new_len; - ret_val = JS_MKPTR (p); + ret_val = pretext_end (ctx, p); } return ret_val; @@ -2895,13 +2840,7 @@ JSValue JS_GetPropertyStr (JSContext *ctx, JSValue this_obj, const char *prop) { JS_PushGCRef (ctx, &obj_ref); obj_ref.val = this_obj; - /* Try immediate ASCII first */ - if (len <= MIST_ASCII_MAX_LEN) { - key = MIST_TryNewImmediateASCII (prop, len); - if (JS_IsNull (key)) { key = JS_NewStringLen (ctx, prop, len); } - } else { - key = JS_NewStringLen (ctx, prop, len); - } + key = JS_NewStringLen (ctx, prop, len); if (JS_IsException (key)) { JS_PopGCRef (ctx, &obj_ref); return JS_EXCEPTION;