jstext properly used for oncat

This commit is contained in:
2026-02-18 17:58:36 -06:00
parent 81561d426b
commit 34521e44f1
2 changed files with 20 additions and 18 deletions

View File

@@ -906,8 +906,9 @@ typedef struct JSBlob {
} JSBlob;
typedef struct JSText {
objhdr_t hdr; /* mist header */
word_t length; /* length (or hash for stoned text) */
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;
@@ -1029,7 +1030,6 @@ static inline uint64_t fash64_hash_one (uint64_t word) {
}
static inline word_t JSText_len (const JSText *text) {
if (objhdr_s (text->hdr)) return objhdr_cap56 (text->hdr);
return text->length;
}

View File

@@ -110,16 +110,16 @@ JS_BOOL JS_IsFrame(JSValue v) {
}
uint64_t get_text_hash (JSText *text) {
uint64_t len = objhdr_cap56 (text->hdr);
uint64_t len = text->length;
size_t word_count = (len + 1) / 2;
if (objhdr_s (text->hdr)) {
/* Stoned text: check for cached hash */
if (text->length != 0) return text->length;
/* Compute and cache hash using content length from header */
text->length = fash64_hash_words (text->packed, word_count, len);
if (!text->length) text->length = 1;
return text->length;
if (text->hash != 0) return text->hash;
/* Compute and cache hash */
text->hash = fash64_hash_words (text->packed, word_count, len);
if (!text->hash) text->hash = 1;
return text->hash;
} else {
/* Pre-text: compute hash on the fly */
return fash64_hash_words (text->packed, word_count, len);
@@ -137,7 +137,7 @@ void pack_utf32_to_words (const uint32_t *utf32, uint32_t len, uint64_t *packed)
/* Compare two packed UTF-32 texts for equality */
int text_equal (JSText *a, const uint64_t *packed_b, uint32_t len_b) {
uint32_t len_a = (uint32_t)objhdr_cap56 (a->hdr);
uint32_t len_a = (uint32_t)a->length;
if (len_a != len_b) return 0;
size_t word_count = (len_a + 1) / 2;
return memcmp (a->packed, packed_b, word_count * sizeof (uint64_t)) == 0;
@@ -331,7 +331,8 @@ JSValue intern_text_to_value (JSContext *ctx, const uint32_t *utf32, uint32_t le
/* Initialize the text */
text->hdr = objhdr_make (len, OBJ_TEXT, false, false, false, true); /* s=1 for stoned */
text->length = hash; /* Store hash in length field for stoned text */
text->length = len;
text->hash = hash;
memcpy (text->packed, packed, word_count * sizeof (uint64_t));
/* Add to intern table */
@@ -911,7 +912,8 @@ JSValue ppretext_end (JSContext *ctx, PPretext *p) {
for (int i = 0; i < len; i++) {
string_put (str, i, p->data[i]);
}
str->hdr = objhdr_set_cap56 (str->hdr, len);
str->length = len;
str->hash = 0;
str->hdr = objhdr_set_s (str->hdr, true);
ppretext_free (p);
@@ -1917,7 +1919,8 @@ JSText *js_alloc_string (JSContext *ctx, int max_len) {
}
/* Initialize objhdr_t with OBJ_TEXT type and capacity in cap56 */
str->hdr = objhdr_make (max_len, OBJ_TEXT, false, false, false, false);
str->length = 0; /* length starts at 0, capacity is in hdr */
str->length = 0;
str->hash = 0;
/* Zero packed data so odd-length strings have deterministic padding.
js_malloc is a bump allocator and does not zero memory; without this,
the last word's unused low 32 bits contain garbage, causing
@@ -2511,9 +2514,8 @@ JSValue pretext_end (JSContext *ctx, JSText *s) {
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;
/* length is already set by caller; cap56 stays as allocated capacity */
s->hash = 0;
s->hdr = objhdr_set_s (s->hdr, true); /* mark as stone */
return JS_MKPTR (s);
}
@@ -5455,8 +5457,8 @@ static JSValue js_regexp_exec (JSContext *ctx, JSValue this_val, int argc, JSVal
}
for (int ci = 0; ci < imm_len; ci++)
string_put (hs, ci, MIST_GetImmediateASCIIChar (str_ref.val, ci));
hs->hdr = objhdr_set_cap56 (hs->hdr, imm_len);
hs->length = 0;
hs->length = imm_len;
hs->hash = 0;
hs->hdr = objhdr_set_s (hs->hdr, true);
str_ref.val = JS_MKPTR (hs);
}