From ba1b92aa78371159a461507b23f4fad2c193aa27 Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Mon, 2 Feb 2026 06:27:08 -0600 Subject: [PATCH] rm freevalue and dupvalue --- source/quickjs.c | 985 ++++++++++++----------------------------------- 1 file changed, 241 insertions(+), 744 deletions(-) diff --git a/source/quickjs.c b/source/quickjs.c index b84ec78b..61f9820f 100644 --- a/source/quickjs.c +++ b/source/quickjs.c @@ -1347,7 +1347,7 @@ static JSValue rec_get_own (JSContext *ctx, JSRecord *rec, JSValue k) { if (rec_key_is_empty (k) || rec_key_is_tomb (k)) return JS_NULL; int slot = rec_find_slot (rec, k); - if (slot > 0) { return JS_DupValue (ctx, rec->slots[slot].val); } + if (slot > 0) { return rec->slots[slot].val; } return JS_NULL; } @@ -1359,7 +1359,7 @@ static JSValue rec_get (JSContext *ctx, JSRecord *rec, JSValue k) { JSRecord *p = rec; while (p) { int slot = rec_find_slot (p, k); - if (slot > 0) { return JS_DupValue (ctx, p->slots[slot].val); } + if (slot > 0) { return p->slots[slot].val; } p = p->proto; } return JS_NULL; @@ -1395,7 +1395,6 @@ static int rec_set_own (JSContext *ctx, JSRecord *rec, JSValue k, JSValue val) { /* Over 75% load factor - try resize (may fail with inline slots) */ uint32_t new_mask = (mask + 1) * 2 - 1; if (rec_resize (ctx, rec, new_mask) < 0) { - JS_FreeValue (ctx, val); return -1; } /* Re-find slot after resize */ @@ -1404,7 +1403,7 @@ static int rec_set_own (JSContext *ctx, JSRecord *rec, JSValue k, JSValue val) { /* Insert at -slot */ int insert_slot = -slot; - rec->slots[insert_slot].key = JS_DupValue (ctx, k); + rec->slots[insert_slot].key = k; rec->slots[insert_slot].val = val; rec->len++; @@ -1687,7 +1686,6 @@ static inline objhdr_t objhdr_set_cap56 (objhdr_t h, uint64_t cap); Internal use only. */ int JS_SetPropertyInternal (JSContext *ctx, JSValue this_obj, JSValue prop, JSValue val) { if (!JS_IsRecord (this_obj)) { - JS_FreeValue (ctx, val); return -1; } JSRecord *rec = (JSRecord *)JS_VALUE_GET_OBJ (this_obj); @@ -2647,7 +2645,6 @@ static inline void set_value (JSContext *ctx, JSValue *pval, JSValue new_val) { JSValue old_val; old_val = *pval; *pval = new_val; - JS_FreeValue (ctx, old_val); } void JS_SetClassProto (JSContext *ctx, JSClassID class_id, JSValue obj) { @@ -2659,7 +2656,7 @@ void JS_SetClassProto (JSContext *ctx, JSClassID class_id, JSValue obj) { JSValue JS_GetClassProto (JSContext *ctx, JSClassID class_id) { JSRuntime *rt = ctx->rt; assert (class_id < rt->class_count); - return JS_DupValue (ctx, ctx->class_proto[class_id]); + return ctx->class_proto[class_id]; } JSContext *JS_DupContext (JSContext *ctx) { @@ -2716,22 +2713,13 @@ void JS_FreeContext (JSContext *ctx) { } #endif - JS_FreeValue (ctx, ctx->global_obj); - JS_FreeValue (ctx, ctx->global_var_obj); - JS_FreeValue (ctx, ctx->throw_type_error); - JS_FreeValue (ctx, ctx->eval_obj); - JS_FreeValue (ctx, ctx->array_proto_values); for (i = 0; i < JS_NATIVE_ERROR_COUNT; i++) { - JS_FreeValue (ctx, ctx->native_error_proto[i]); } for (i = 0; i < rt->class_count; i++) { - JS_FreeValue (ctx, ctx->class_proto[i]); } js_free_rt (rt, ctx->class_proto); - JS_FreeValue (ctx, ctx->array_ctor); - JS_FreeValue (ctx, ctx->regexp_ctor); /* Free VM stacks */ if (ctx->frame_stack) js_free_rt (rt, ctx->frame_stack); @@ -2944,7 +2932,7 @@ static JSValue js_sub_string (JSContext *ctx, JSText *p, int start, int end) { int i; int len = end - start; if (start == 0 && end == (int)JSText_len (p)) { - return JS_DupValue (ctx, JS_MKPTR (p)); + return JS_MKPTR (p); } JSText *str = js_alloc_string (ctx, len); if (!str) return JS_EXCEPTION; @@ -3079,13 +3067,11 @@ static JSText *pretext_concat_value (JSContext *ctx, JSText *s, JSValue v) { for (int i = 0; i < len; i++) buf[i] = MIST_GetImmediateASCIIChar (v1, i); s = pretext_write8 (ctx, s, (const uint8_t *)buf, len); - JS_FreeValue (ctx, v1); return s; } JSText *p = JS_VALUE_GET_STRING (v1); s = pretext_concat (ctx, s, p, 0, (uint32_t)JSText_len (p)); - JS_FreeValue (ctx, v1); return s; } @@ -3098,7 +3084,6 @@ static JSText *pretext_concat_value_free (JSContext *ctx, JSText *s, JSValue v) } p = JS_VALUE_GET_STRING (v); s = pretext_concat (ctx, s, p, 0, (uint32_t)JSText_len (p)); - JS_FreeValue (ctx, v); return s; } @@ -3209,11 +3194,9 @@ static JSValue JS_ConcatString3 (JSContext *ctx, const char *str1, JSValue str2, b = pretext_write8 (ctx, b, (const uint8_t *)str3, len3); if (!b) goto fail; - JS_FreeValue (ctx, str2); return pretext_end (ctx, b); fail: - JS_FreeValue (ctx, str2); return JS_EXCEPTION; } @@ -3232,7 +3215,7 @@ const char *JS_ToCStringLen2 (JSContext *ctx, size_t *plen, JSValue val1, BOOL c val = JS_ToString (ctx, val1); if (JS_IsException (val)) goto fail; } else { - val = JS_DupValue (ctx, val1); + val = val1; } str = JS_VALUE_GET_STRING (val); @@ -3264,12 +3247,10 @@ const char *JS_ToCStringLen2 (JSContext *ctx, size_t *plen, JSValue val1, BOOL c if (plen) *plen = size; - JS_FreeValue (ctx, val); return ret; fail: if (plen) *plen = 0; - JS_FreeValue (ctx, val); return NULL; } @@ -3349,13 +3330,10 @@ static JSValue JS_ConcatString2 (JSContext *ctx, JSValue op1, JSValue op2) { JSText *p1, *p2; p1 = JS_VALUE_GET_STRING (op1); if (JS_ConcatStringInPlace (ctx, p1, op2)) { - JS_FreeValue (ctx, op2); return op1; } p2 = JS_VALUE_GET_STRING (op2); ret = JS_ConcatString1 (ctx, p1, p2); - JS_FreeValue (ctx, op1); - JS_FreeValue (ctx, op2); return ret; } @@ -3385,14 +3363,12 @@ static JSValue JS_ConcatString (JSContext *ctx, JSValue op1, JSValue op2) { if (unlikely (!JS_IsText (op1))) { op1 = JS_ToStringFree (ctx, op1); if (JS_IsException (op1)) { - JS_FreeValue (ctx, op2); return JS_EXCEPTION; } } if (unlikely (!JS_IsText (op2))) { op2 = JS_ToStringFree (ctx, op2); if (JS_IsException (op2)) { - JS_FreeValue (ctx, op1); return JS_EXCEPTION; } } @@ -3426,8 +3402,6 @@ static JSValue JS_ConcatString (JSContext *ctx, JSValue op1, JSValue op2) { if (JS_IsNull (ret_val)) { JSText *p = js_alloc_string (ctx, new_len); if (!p) { - JS_FreeValue (ctx, op1); - JS_FreeValue (ctx, op2); return JS_EXCEPTION; } /* Copy characters using string_put/get */ @@ -3440,8 +3414,6 @@ static JSValue JS_ConcatString (JSContext *ctx, JSValue op1, JSValue op2) { ret_val = JS_MKPTR (p); } - JS_FreeValue (ctx, op1); - JS_FreeValue (ctx, op2); return ret_val; } @@ -3537,7 +3509,7 @@ static int js_method_set_properties (JSContext *ctx, JSValue func_obj, JSValue n if (JS_VALUE_GET_TAG (func_obj) != JS_TAG_FUNCTION) return -1; JSFunction *f = JS_VALUE_GET_FUNCTION (func_obj); /* name is now JSValue text */ - if (JS_IsText (name)) { f->name = JS_DupValue (ctx, name); } + if (JS_IsText (name)) { f->name = name; } return 0; } @@ -3602,7 +3574,6 @@ JSValue JS_NewCFunctionData (JSContext *ctx, JSCFunctionData *func, int length, if (JS_IsException (func_obj)) return func_obj; s = js_malloc (ctx, sizeof (*s) + data_len * sizeof (JSValue)); if (!s) { - JS_FreeValue (ctx, func_obj); return JS_EXCEPTION; } s->func = func; @@ -3610,7 +3581,7 @@ JSValue JS_NewCFunctionData (JSContext *ctx, JSCFunctionData *func, int length, s->data_len = data_len; s->magic = magic; for (i = 0; i < data_len; i++) - s->data[i] = JS_DupValue (ctx, data[i]); + s->data[i] = data[i]; f = JS_VALUE_GET_FUNCTION (func_obj); f->u.c_function_data_record = s; f->length = length; @@ -3673,7 +3644,6 @@ static int js_intrinsic_array_ensure_capacity (JSContext *ctx, JSArray *arr, uin static int js_intrinsic_array_push (JSContext *ctx, JSArray *arr, JSValue val) { if (js_intrinsic_array_ensure_capacity (ctx, arr, arr->len + 1) < 0) { - JS_FreeValue (ctx, val); return -1; } arr->values[arr->len++] = val; @@ -3682,13 +3652,11 @@ static int js_intrinsic_array_push (JSContext *ctx, JSArray *arr, JSValue val) { static int js_intrinsic_array_set (JSContext *ctx, JSArray *arr, uint32_t idx, JSValue val) { if (objhdr_s (arr->mist_hdr)) { - JS_FreeValue (ctx, val); JS_ThrowInternalError (ctx, "cannot set on a stoned array"); return -1; } if (js_intrinsic_array_ensure_capacity (ctx, arr, idx + 1) < 0) { - JS_FreeValue (ctx, val); return -1; } @@ -4039,13 +4007,12 @@ void JS_DumpMemoryUsage (FILE *fp, const JSMemoryUsage *s, JSRuntime *rt) { } JSValue JS_GetGlobalObject (JSContext *ctx) { - return JS_DupValue (ctx, ctx->global_obj); + return ctx->global_obj; } /* WARNING: obj is freed */ JSValue JS_Throw (JSContext *ctx, JSValue obj) { JSRuntime *rt = ctx->rt; - JS_FreeValue (ctx, rt->current_exception); rt->current_exception = obj; /* uncatchable flag removed - not needed with copying GC */ return JS_EXCEPTION; @@ -4244,14 +4211,8 @@ static void build_backtrace (JSContext *ctx, JSValue error_obj, const char *file < 0 || JS_SetPropertyInternal (ctx, error_obj, key_columnNumber, JS_NewInt32 (ctx, col_num)) < 0) { - JS_FreeValue (ctx, key_fileName); - JS_FreeValue (ctx, key_lineNumber); - JS_FreeValue (ctx, key_columnNumber); return; } - JS_FreeValue (ctx, key_fileName); - JS_FreeValue (ctx, key_lineNumber); - JS_FreeValue (ctx, key_columnNumber); } for (sf = ctx->rt->current_stack_frame; sf != NULL; sf = sf->prev_frame) { if (sf->js_mode & JS_MODE_BACKTRACE_BARRIER) break; @@ -4484,7 +4445,7 @@ JSValue JS_GetPrototype (JSContext *ctx, JSValue obj) { if (!p) val = JS_NULL; else - val = JS_DupValue (ctx, JS_MKPTR (p)); + val = JS_MKPTR (p); } else { /* Primitives have no prototype */ val = JS_NULL; @@ -4495,7 +4456,6 @@ JSValue JS_GetPrototype (JSContext *ctx, JSValue obj) { static JSValue JS_GetPrototypeFree (JSContext *ctx, JSValue obj) { JSValue obj1; obj1 = JS_GetPrototype (ctx, obj); - JS_FreeValue (ctx, obj); return obj1; } @@ -4537,7 +4497,7 @@ JSValue JS_GetOwnPropertyNames (JSContext *ctx, JSValue obj) { for (i = 1; i <= mask; i++) { JSValue k = rec->slots[i].key; if (!rec_key_is_empty (k) && !rec_key_is_tomb (k) && JS_IsText (k)) { - JS_SetPropertyUint32 (ctx, arr, count, JS_DupValue (ctx, k)); + JS_SetPropertyUint32 (ctx, arr, count, k); count++; } } @@ -4558,7 +4518,7 @@ static int JS_GetOwnPropertyInternal (JSContext *ctx, if (slot > 0) { if (desc) - *desc = JS_DupValue (ctx, rec->slots[slot].val); + *desc = rec->slots[slot].val; return TRUE; } return FALSE; @@ -4580,9 +4540,9 @@ int JS_HasProperty (JSContext *ctx, JSValue obj, JSValue prop) { p = JS_VALUE_GET_OBJ (obj); for (;;) { /* JS_GetOwnPropertyInternal can free the prototype */ - JS_DupValue (ctx, JS_MKPTR (p)); + JS_MKPTR (p); ret = JS_GetOwnPropertyInternal (ctx, NULL, p, prop); - JS_FreeValue (ctx, JS_MKPTR (p)); + /* JS_FreeValue removed - copying GC handles memory */ if (ret != 0) return ret; p = JS_OBJ_GET_PROTO (p); if (!p) break; @@ -4612,20 +4572,17 @@ static JSValue JS_GetPropertyValue (JSContext *ctx, JSValue this_obj, JSValue pr uint32_t prop_tag = JS_VALUE_GET_TAG (prop); if (JS_IsNull (this_obj)) { - JS_FreeValue (ctx, prop); return JS_NULL; } if (prop_tag == JS_TAG_INT) { int idx = JS_VALUE_GET_INT (prop); - JS_FreeValue (ctx, prop); return JS_GetPropertyNumber (ctx, this_obj, idx); } if (prop_tag == JS_TAG_SHORT_FLOAT) { double d = JS_VALUE_GET_FLOAT64 (prop); uint32_t idx = (uint32_t)d; - JS_FreeValue (ctx, prop); if (d != (double)idx) return JS_NULL; return JS_GetPropertyNumber (ctx, this_obj, idx); } @@ -4634,12 +4591,10 @@ static JSValue JS_GetPropertyValue (JSContext *ctx, JSValue this_obj, JSValue pr if (JS_IsText (prop)) { /* Intrinsic arrays don't support string keys */ if (JS_IsArray (this_obj)) { - JS_FreeValue (ctx, prop); return JS_NULL; } /* Create an interned key from the string */ JSValue key = js_key_from_string (ctx, prop); - JS_FreeValue (ctx, prop); ret = JS_GetProperty (ctx, this_obj, key); /* key is interned or immediate, no need to free */ return ret; @@ -4649,36 +4604,31 @@ static JSValue JS_GetPropertyValue (JSContext *ctx, JSValue this_obj, JSValue pr if (JS_IsRecord (prop)) { /* Intrinsic arrays don't support object keys */ if (!JS_IsRecord (this_obj)) { - JS_FreeValue (ctx, prop); return JS_NULL; } JSRecord *rec = JS_VALUE_GET_RECORD (this_obj); JSValue val = rec_get (ctx, rec, prop); - JS_FreeValue (ctx, prop); return val; } /* Unknown type -> null */ - JS_FreeValue (ctx, prop); return JS_NULL; } JSValue JS_SetPropertyNumber (JSContext *js, JSValue obj, int idx, JSValue val) { if (!JS_IsArray (obj)) { - JS_FreeValue (js, val); return JS_ThrowInternalError (js, "cannot set with a number on a non array"); } JSArray *a = JS_VALUE_GET_ARRAY (obj); if (idx < 0) { - JS_FreeValue (js, val); return JS_ThrowRangeError (js, "array index out of bounds"); } if (js_intrinsic_array_set (js, a, (uint32_t)idx, val) < 0) return JS_EXCEPTION; - return JS_DupValue (js, val); + return val; } JSValue JS_GetPropertyNumber (JSContext *js, JSValue obj, int idx) { @@ -4686,7 +4636,7 @@ JSValue JS_GetPropertyNumber (JSContext *js, JSValue obj, int idx) { JSArray *a = JS_VALUE_GET_ARRAY (obj); int len = a->len; if (idx < 0 || idx >= len) { return JS_NULL; } - return JS_DupValue (js, a->values[idx]); + return a->values[idx]; } if (JS_IsText (obj)) { @@ -4722,7 +4672,6 @@ JSValue JS_GetPropertyStr (JSContext *ctx, JSValue this_obj, const char *prop) { } if (JS_IsException (key)) return JS_EXCEPTION; ret = JS_GetProperty (ctx, this_obj, key); - JS_FreeValue (ctx, key); return ret; } @@ -4731,11 +4680,9 @@ static JSValue JS_Invoke (JSContext *ctx, JSValue this_val, JSValue method, int JSValue func = JS_GetProperty (ctx, this_val, method); if (JS_IsException (func)) return JS_EXCEPTION; if (!JS_IsFunction (func)) { - JS_FreeValue (ctx, func); return JS_NULL; /* Method not found or not callable */ } JSValue ret = JS_Call (ctx, func, this_val, argc, argv); - JS_FreeValue (ctx, func); return ret; } @@ -4752,8 +4699,6 @@ static int delete_property (JSContext *ctx, JSRecord *rec, JSValue key) { } /* Free key and value */ - JS_FreeValue (ctx, rec->slots[slot].key); - JS_FreeValue (ctx, rec->slots[slot].val); /* Mark as tombstone */ rec->slots[slot].key = JS_EXCEPTION; /* tombstone marker */ @@ -4766,7 +4711,6 @@ static int delete_property (JSContext *ctx, JSRecord *rec, JSValue key) { int JS_SetProperty (JSContext *ctx, JSValue this_obj, JSValue prop, JSValue val) { if (!JS_IsRecord (this_obj)) { - JS_FreeValue (ctx, val); if (JS_IsNull (this_obj)) { JS_ThrowTypeError (ctx, "cannot set property of null"); } else { @@ -4779,7 +4723,6 @@ int JS_SetProperty (JSContext *ctx, JSValue this_obj, JSValue prop, JSValue val) JSRecord *rec = (JSRecord *)JS_VALUE_GET_OBJ (this_obj); if (unlikely (obj_is_stone (rec))) { - JS_FreeValue (ctx, val); JS_ThrowTypeError (ctx, "object is stone"); return -1; } @@ -4790,19 +4733,16 @@ int JS_SetProperty (JSContext *ctx, JSValue this_obj, JSValue prop, JSValue val) int JS_SetPropertyUint32 (JSContext *ctx, JSValue this_obj, uint32_t idx, JSValue val) { JSValue ret = JS_SetPropertyNumber (ctx, (JSValue)this_obj, (int)idx, val); if (JS_IsException (ret)) return -1; - JS_FreeValue (ctx, ret); return 0; } int JS_SetPropertyInt64 (JSContext *ctx, JSValue this_obj, int64_t idx, JSValue val) { if (idx < INT32_MIN || idx > INT32_MAX) { - JS_FreeValue (ctx, val); JS_ThrowRangeError (ctx, "array index out of bounds"); return -1; } JSValue ret = JS_SetPropertyNumber (ctx, (JSValue)this_obj, (int)idx, val); if (JS_IsException (ret)) return -1; - JS_FreeValue (ctx, ret); return 0; } @@ -4817,7 +4757,6 @@ int JS_SetPropertyStr (JSContext *ctx, JSValue this_obj, const char *prop, JSVal key = js_new_string8_len (ctx, prop, len); } if (JS_IsException (key)) { - JS_FreeValue (ctx, val); return -1; } return JS_SetProperty (ctx, this_obj, key, val); @@ -4829,26 +4768,20 @@ static int JS_SetPropertyValue (JSContext *ctx, JSValue this_obj, JSValue prop, if (prop_tag == JS_TAG_INT) { int idx = JS_VALUE_GET_INT (prop); - JS_FreeValue (ctx, prop); JSValue ret = JS_SetPropertyNumber (ctx, this_obj, idx, val); if (JS_IsException (ret)) return -1; - JS_FreeValue (ctx, ret); return 0; } if (JS_IsText (prop)) { JSValue key = js_key_from_string (ctx, prop); - JS_FreeValue (ctx, prop); return JS_SetProperty (ctx, this_obj, key, val); } if (JS_IsRecord (prop)) { - JS_FreeValue (ctx, prop); return JS_SetProperty (ctx, this_obj, prop, val); } - JS_FreeValue (ctx, prop); - JS_FreeValue (ctx, val); return -1; } @@ -4873,13 +4806,11 @@ JSValue JS_GetPropertyKey (JSContext *ctx, JSValue this_obj, JSValue key) { int JS_SetPropertyKey (JSContext *ctx, JSValue this_obj, JSValue key, JSValue val) { if (JS_IsRecord (key)) { if (!JS_IsRecord (this_obj)) { - JS_FreeValue (ctx, val); JS_ThrowTypeError (ctx, "cannot set property on this value"); return -1; } JSRecord *rec = JS_VALUE_GET_RECORD (this_obj); if (obj_is_stone (rec)) { - JS_FreeValue (ctx, val); JS_ThrowTypeError (ctx, "cannot modify frozen object"); return -1; } @@ -4931,8 +4862,6 @@ int JS_DeletePropertyKey (JSContext *ctx, JSValue obj, JSValue key) { int slot = rec_find_slot (rec, key); if (slot <= 0) return FALSE; /* Delete by marking as tombstone */ - JS_FreeValue (ctx, rec->slots[slot].key); - JS_FreeValue (ctx, rec->slots[slot].val); rec->slots[slot].key = JS_EXCEPTION; /* tombstone */ rec->slots[slot].val = JS_NULL; rec->len--; @@ -4972,7 +4901,7 @@ static int JS_DefineObjectName (JSContext *ctx, JSValue obj, JSValue name) { if (!JS_IsNull (name) && JS_IsObject (obj) && !js_object_has_name (ctx, obj)) { JSValue name_key = MIST_TryNewImmediateASCII ("name", 4); - if (JS_SetPropertyInternal (ctx, obj, name_key, JS_DupValue (ctx, name)) + if (JS_SetPropertyInternal (ctx, obj, name_key, name) < 0) return -1; } @@ -4982,7 +4911,7 @@ static int JS_DefineObjectName (JSContext *ctx, JSValue obj, JSValue name) { static int JS_DefineObjectNameComputed (JSContext *ctx, JSValue obj, JSValue str) { if (JS_IsObject (obj) && !js_object_has_name (ctx, obj)) { JSValue name_key = MIST_TryNewImmediateASCII ("name", 4); - if (JS_SetPropertyInternal (ctx, obj, name_key, JS_DupValue (ctx, str)) + if (JS_SetPropertyInternal (ctx, obj, name_key, str) < 0) return -1; } @@ -5048,7 +4977,7 @@ static int JS_DefineGlobalVar (JSContext *ctx, JSValue prop, int def_flags) { static int JS_DefineGlobalFunction (JSContext *ctx, JSValue prop, JSValue func, int def_flags) { (void)def_flags; /* JS_SetPropertyInternal consumes the value, so we must dup it */ - if (JS_SetPropertyInternal (ctx, ctx->global_obj, prop, JS_DupValue (ctx, func)) + if (JS_SetPropertyInternal (ctx, ctx->global_obj, prop, func) < 0) return -1; return 0; @@ -5064,7 +4993,7 @@ static JSValue JS_GetGlobalVar (JSContext *ctx, JSValue prop, BOOL throw_ref_err JSValue val = rec->slots[slot].val; if (unlikely (JS_IsUninitialized (val))) return JS_ThrowReferenceErrorUninitialized (ctx, prop); - return JS_DupValue (ctx, val); + return val; } /* Fall back to global object */ JSValue val @@ -5087,17 +5016,17 @@ static int JS_GetGlobalVarRef (JSContext *ctx, JSValue prop, JSValue *sp) { JS_ThrowReferenceErrorUninitialized (ctx, prop); return -1; } - sp[0] = JS_DupValue (ctx, ctx->global_var_obj); + sp[0] = ctx->global_var_obj; } else { int ret = JS_HasProperty (ctx, ctx->global_obj, prop); if (ret < 0) return -1; if (ret) { - sp[0] = JS_DupValue (ctx, ctx->global_obj); + sp[0] = ctx->global_obj; } else { sp[0] = JS_NULL; } } - sp[1] = JS_DupValue (ctx, prop); + sp[1] = prop; return 0; } @@ -5130,12 +5059,10 @@ static int JS_SetGlobalVar (JSContext *ctx, JSValue prop, JSValue val, int flag) if (slot > 0) { if (flag != 1) { if (unlikely (JS_IsUninitialized (rec->slots[slot].val))) { - JS_FreeValue (ctx, val); JS_ThrowReferenceErrorUninitialized (ctx, prop); return -1; } } - JS_FreeValue (ctx, rec->slots[slot].val); rec->slots[slot].val = val; return 0; } @@ -5185,8 +5112,6 @@ int JS_DeleteProperty (JSContext *ctx, JSValue obj, JSValue prop) { slot = rec_find_slot (rec, prop); if (slot > 0) { /* Delete by marking as tombstone */ - JS_FreeValue (ctx, rec->slots[slot].key); - JS_FreeValue (ctx, rec->slots[slot].val); rec->slots[slot].key = JS_EXCEPTION; /* tombstone */ rec->slots[slot].val = JS_NULL; rec->len--; @@ -5266,11 +5191,9 @@ static int JS_ToBoolFree (JSContext *ctx, JSValue val) { if (objhdr_type (hdr) == OBJ_TEXT) { /* String (JSText or JSText) - truthy if non-empty */ BOOL ret = objhdr_cap56 (hdr) != 0; - JS_FreeValue (ctx, val); return ret; } /* Objects (record, array, function) are truthy */ - JS_FreeValue (ctx, val); return 1; } @@ -5291,14 +5214,13 @@ static int JS_ToBoolFree (JSContext *ctx, JSValue val) { double d = JS_VALUE_GET_FLOAT64 (val); return !isnan (d) && d != 0; } else { - JS_FreeValue (ctx, val); return TRUE; } } } int JS_ToBool (JSContext *ctx, JSValue val) { - return JS_ToBoolFree (ctx, JS_DupValue (ctx, val)); + return JS_ToBoolFree (ctx, val); } static int skip_spaces (const char *pc) { @@ -5515,11 +5437,9 @@ static JSValue JS_ToNumberFree (JSContext *ctx, JSValue val) { objhdr_t hdr = *((objhdr_t *)((char *)ptr + 8)); if (objhdr_type (hdr) == OBJ_TEXT) { /* String */ - JS_FreeValue (ctx, val); return JS_ThrowTypeError (ctx, "cannot convert text to a number"); } /* Objects */ - JS_FreeValue (ctx, val); return JS_ThrowTypeError (ctx, "cannot convert object to number"); } @@ -5537,7 +5457,6 @@ static JSValue JS_ToNumberFree (JSContext *ctx, JSValue val) { case JS_TAG_STRING_IMM: return JS_ThrowTypeError (ctx, "cannot convert text to a number"); default: - JS_FreeValue (ctx, val); ret = JS_NAN; break; } @@ -5545,7 +5464,7 @@ static JSValue JS_ToNumberFree (JSContext *ctx, JSValue val) { } static JSValue JS_ToNumber (JSContext *ctx, JSValue val) { - return JS_ToNumberFree (ctx, JS_DupValue (ctx, val)); + return JS_ToNumberFree (ctx, val); } static __exception int __JS_ToFloat64Free (JSContext *ctx, double *pres, JSValue val) { @@ -5588,7 +5507,7 @@ static inline int JS_ToFloat64Free (JSContext *ctx, double *pres, JSValue val) { } int JS_ToFloat64 (JSContext *ctx, double *pres, JSValue val) { - return JS_ToFloat64Free (ctx, pres, JS_DupValue (ctx, val)); + return JS_ToFloat64Free (ctx, pres, val); } /* Note: the integer value is satured to 32 bits */ @@ -5633,11 +5552,11 @@ redo: } int JS_ToInt32Sat (JSContext *ctx, int *pres, JSValue val) { - return JS_ToInt32SatFree (ctx, pres, JS_DupValue (ctx, val)); + return JS_ToInt32SatFree (ctx, pres, val); } int JS_ToInt32Clamp (JSContext *ctx, int *pres, JSValue val, int min, int max, int min_offset) { - int res = JS_ToInt32SatFree (ctx, pres, JS_DupValue (ctx, val)); + int res = JS_ToInt32SatFree (ctx, pres, val); if (res == 0) { if (*pres < min) { *pres += min_offset; @@ -5689,11 +5608,11 @@ redo: } int JS_ToInt64Sat (JSContext *ctx, int64_t *pres, JSValue val) { - return JS_ToInt64SatFree (ctx, pres, JS_DupValue (ctx, val)); + return JS_ToInt64SatFree (ctx, pres, val); } int JS_ToInt64Clamp (JSContext *ctx, int64_t *pres, JSValue val, int64_t min, int64_t max, int64_t neg_offset) { - int res = JS_ToInt64SatFree (ctx, pres, JS_DupValue (ctx, val)); + int res = JS_ToInt64SatFree (ctx, pres, val); if (res == 0) { if (*pres < 0) *pres += neg_offset; if (*pres < min) @@ -5753,7 +5672,7 @@ redo: } int JS_ToInt64 (JSContext *ctx, int64_t *pres, JSValue val) { - return JS_ToInt64Free (ctx, pres, JS_DupValue (ctx, val)); + return JS_ToInt64Free (ctx, pres, val); } /* return (<0, 0) in case of exception */ @@ -5807,7 +5726,7 @@ redo: } int JS_ToInt32 (JSContext *ctx, int32_t *pres, JSValue val) { - return JS_ToInt32Free (ctx, pres, JS_DupValue (ctx, val)); + return JS_ToInt32Free (ctx, pres, val); } static inline int JS_ToUint32Free (JSContext *ctx, uint32_t *pres, JSValue val) { @@ -5845,7 +5764,6 @@ static __exception int JS_ToArrayLengthFree (JSContext *ctx, uint32_t *plen, JSV } else { /* legacy behavior: must do the conversion twice and compare */ if (JS_ToUint32 (ctx, &len, val)) { - JS_FreeValue (ctx, val); return -1; } val = JS_ToNumberFree (ctx, val); @@ -5871,7 +5789,6 @@ static __exception int JS_ToArrayLengthFree (JSContext *ctx, uint32_t *plen, JSV return -1 for exception */ static __exception int JS_ToLengthFree (JSContext *ctx, int64_t *plen, JSValue val) { int res = JS_ToInt64Clamp (ctx, plen, val, 0, MAX_SAFE_INTEGER, 0); - JS_FreeValue (ctx, val); return res; } @@ -5909,7 +5826,7 @@ static JSValue JS_ToStringInternal (JSContext *ctx, JSValue val, BOOL is_ToPrope uint8_t mist_type = objhdr_type (hdr); if (mist_type == OBJ_TEXT) { /* String - return as-is */ - return JS_DupValue (ctx, val); + return val; } /* For GC objects, check gc_obj_type */ JSGCObjectHeader *p = (JSGCObjectHeader *)ptr; @@ -5923,7 +5840,7 @@ static JSValue JS_ToStringInternal (JSContext *ctx, JSValue val, BOOL is_ToPrope tag = JS_VALUE_GET_NORM_TAG (val); switch (tag) { case JS_TAG_STRING_IMM: - return JS_DupValue (ctx, val); + return val; case JS_TAG_INT: { size_t len; len = i32toa (buf, JS_VALUE_GET_INT (val)); @@ -5949,13 +5866,12 @@ JSValue JS_ToString (JSContext *ctx, JSValue val) { static JSValue JS_ToStringFree (JSContext *ctx, JSValue val) { JSValue ret; ret = JS_ToString (ctx, val); - JS_FreeValue (ctx, val); return ret; } JSValue JS_ToPropertyKey (JSContext *ctx, JSValue val) { /* Objects are handled directly via objkey map, not through atoms */ - if (JS_IsRecord (val)) { return JS_DupValue (ctx, val); } + if (JS_IsRecord (val)) { return val; } return JS_ToStringInternal (ctx, val, TRUE); } @@ -6023,10 +5939,8 @@ static JSValue JS_ToQuotedString (JSContext *ctx, JSValue val1) { } b = pretext_putc (ctx, b, '\"'); if (!b) goto fail; - JS_FreeValue (ctx, val); return pretext_end (ctx, b); fail: - JS_FreeValue (ctx, val); return JS_EXCEPTION; } @@ -6181,21 +6095,18 @@ static void js_print_object (JSPrintValueState *s, JSRecord *p) { = js_regexp_toString (s->ctx, JS_MKPTR (p), 0, NULL); if (JS_IsException (str)) goto default_obj; js_print_raw_string (s, str); - JS_FreeValueRT (s->rt, str); comma_state = 2; } else if (REC_GET_CLASS_ID(p) == JS_CLASS_ERROR && s->ctx && !s->options.raw_dump) { JSValue str = js_error_toString (s->ctx, JS_MKPTR (p), 0, NULL); if (JS_IsException (str)) goto default_obj; js_print_raw_string (s, str); - JS_FreeValueRT (s->rt, str); /* dump the stack if present */ str = JS_GetProperty (s->ctx, JS_MKPTR (p), JS_KEY_stack); if (JS_IsText (str)) { js_putc (s, '\n'); js_print_raw_string2 (s, str, TRUE); } - JS_FreeValueRT (s->rt, str); comma_state = 2; } else { default_obj: @@ -6543,7 +6454,7 @@ js_unary_arith_slow (JSContext *ctx, JSValue *sp, OPCodeEnum op) { } static __exception int js_post_inc_slow (JSContext *ctx, JSValue *sp, OPCodeEnum op) { - sp[0] = JS_DupValue (ctx, sp[-1]); + sp[0] = sp[-1]; return js_unary_arith_slow (ctx, sp + 1, op - OP_post_dec + OP_dec); } @@ -6594,12 +6505,10 @@ js_binary_arith_slow (JSContext *ctx, JSValue *sp, OPCodeEnum op) { op1 = JS_ToNumberFree (ctx, op1); if (JS_IsException (op1)) { - JS_FreeValue (ctx, op2); goto exception; } op2 = JS_ToNumberFree (ctx, op2); if (JS_IsException (op2)) { - JS_FreeValue (ctx, op1); goto exception; } tag1 = JS_VALUE_GET_NORM_TAG (op1); @@ -6643,7 +6552,6 @@ js_binary_arith_slow (JSContext *ctx, JSValue *sp, OPCodeEnum op) { double dr; /* float64 result */ if (JS_ToFloat64Free (ctx, &d1, op1)) { - JS_FreeValue (ctx, op2); goto exception; } if (JS_ToFloat64Free (ctx, &d2, op2)) goto exception; @@ -6736,14 +6644,10 @@ static no_inline int js_relational_slow (JSContext *ctx, JSValue *sp, OPCodeEnum JS_ThrowTypeError ( ctx, "Relational operators only supported on two strings or two numbers"); - JS_FreeValue (ctx, op1); - JS_FreeValue (ctx, op2); goto exception; } /* free the two input values and push the result */ - JS_FreeValue (ctx, op1); - JS_FreeValue (ctx, op2); sp[-2] = JS_NewBool (ctx, res); return 0; @@ -6836,14 +6740,12 @@ static BOOL js_strict_eq2 (JSContext *ctx, JSValue op1, JSValue op2, JSStrictEqM res = FALSE; break; } - JS_FreeValue (ctx, op1); - JS_FreeValue (ctx, op2); done_no_free: return res; } static BOOL js_strict_eq (JSContext *ctx, JSValue op1, JSValue op2) { - return js_strict_eq2 (ctx, JS_DupValue (ctx, op1), JS_DupValue (ctx, op2), JS_EQ_STRICT); + return js_strict_eq2 (ctx, op1, op2, JS_EQ_STRICT); } BOOL JS_StrictEq (JSContext *ctx, JSValue op1, JSValue op2) { @@ -6851,7 +6753,7 @@ BOOL JS_StrictEq (JSContext *ctx, JSValue op1, JSValue op2) { } static BOOL js_same_value (JSContext *ctx, JSValue op1, JSValue op2) { - return js_strict_eq2 (ctx, JS_DupValue (ctx, op1), JS_DupValue (ctx, op2), JS_EQ_SAME_VALUE); + return js_strict_eq2 (ctx, op1, op2, JS_EQ_SAME_VALUE); } BOOL JS_SameValue (JSContext *ctx, JSValue op1, JSValue op2) { @@ -6878,8 +6780,6 @@ static __exception int js_operator_in (JSContext *ctx, JSValue *sp) { } ret = JS_HasPropertyKey (ctx, op2, op1); if (ret < 0) return -1; - JS_FreeValue (ctx, op1); - JS_FreeValue (ctx, op2); sp[-2] = JS_NewBool (ctx, ret); return 0; } @@ -6893,8 +6793,6 @@ static __exception int js_operator_delete (JSContext *ctx, JSValue *sp) { ret = JS_DeletePropertyKey (ctx, op1, op2); if (unlikely (ret < 0)) return -1; - JS_FreeValue (ctx, op1); - JS_FreeValue (ctx, op2); sp[-2] = JS_NewBool (ctx, ret); return 0; } @@ -6932,7 +6830,6 @@ static __exception int JS_CopyDataProperties (JSContext *ctx, JSValue target, JS keys = JS_GetOwnPropertyNames (ctx, source); if (JS_IsException (keys)) return -1; if (js_get_length32 (ctx, &key_count, keys)) { - JS_FreeValue (ctx, keys); return -1; } @@ -6945,17 +6842,13 @@ static __exception int JS_CopyDataProperties (JSContext *ctx, JSValue target, JS /* Check if key exists in excluded object */ JSValue test = JS_GetProperty (ctx, excluded, key); if (!JS_IsNull (test) && !JS_IsException (test)) { - JS_FreeValue (ctx, test); - JS_FreeValue (ctx, key); continue; } - JS_FreeValue (ctx, test); } /* Get property value from source */ val = JS_GetProperty (ctx, source, key); if (JS_IsException (val)) { - JS_FreeValue (ctx, key); goto exception; } @@ -6964,11 +6857,9 @@ static __exception int JS_CopyDataProperties (JSContext *ctx, JSValue target, JS if (ret < 0) goto exception; } - JS_FreeValue (ctx, keys); return 0; exception: - JS_FreeValue (ctx, keys); return -1; } @@ -7034,7 +6925,6 @@ static JSValue js_closure2 (JSContext *ctx, JSValue func_obj, JSFunctionBytecode return func_obj; fail: /* bfunc is freed when func_obj is freed */ - JS_FreeValue (ctx, func_obj); return JS_EXCEPTION; } @@ -7046,7 +6936,6 @@ static JSValue js_closure (JSContext *ctx, JSValue bfunc, JSVarRef **cur_var_ref b = JS_VALUE_GET_PTR (bfunc); func_obj = js_new_function (ctx, JS_FUNC_KIND_BYTECODE); if (JS_IsException (func_obj)) { - JS_FreeValue (ctx, bfunc); return JS_EXCEPTION; } func_obj = js_closure2 (ctx, func_obj, b, cur_var_refs, sf); @@ -7056,13 +6945,12 @@ static JSValue js_closure (JSContext *ctx, JSValue bfunc, JSVarRef **cur_var_ref } f = JS_VALUE_GET_FUNCTION (func_obj); /* Use bytecode func_name if valid, otherwise empty string */ - f->name = JS_IsText (b->func_name) ? JS_DupValue (ctx, b->func_name) + f->name = JS_IsText (b->func_name) ? b->func_name : JS_KEY_empty; f->length = b->arg_count; /* arity = total parameter count */ return func_obj; fail: /* bfunc is freed when func_obj is freed */ - JS_FreeValue (ctx, func_obj); return JS_EXCEPTION; } @@ -7074,7 +6962,7 @@ static void close_var_refs (JSRuntime *rt, JSStackFrame *sf) { var_ref = list_entry (el, JSVarRef, var_ref_link); /* no need to unlink var_ref->var_ref_link as the list is never used * afterwards */ - var_ref->value = JS_DupValueRT (rt, *var_ref->pvalue); + var_ref->value = *var_ref->pvalue; var_ref->pvalue = &var_ref->value; /* the reference is no longer to a local variable */ var_ref->is_detached = TRUE; @@ -7091,7 +6979,7 @@ static void close_lexical_var (JSContext *ctx, JSStackFrame *sf, int var_idx) { var_ref = list_entry (el, JSVarRef, var_ref_link); if (var_ref->pvalue == pvalue) { list_del (&var_ref->var_ref_link); - var_ref->value = JS_DupValue (ctx, *var_ref->pvalue); + var_ref->value = *var_ref->pvalue; var_ref->pvalue = &var_ref->value; /* the reference is no longer to a local variable */ var_ref->is_detached = TRUE; @@ -7329,7 +7217,7 @@ static JSValue JS_CallInternal (JSContext *caller_ctx, JSValue func_obj, JSValue int n = min_int (argc, b->arg_count); arg_buf = local_buf; for (i = 0; i < n; i++) - arg_buf[i] = JS_DupValue (caller_ctx, argv[i]); + arg_buf[i] = argv[i]; for (; i < b->arg_count; i++) arg_buf[i] = JS_NULL; sf->arg_count = b->arg_count; @@ -7357,7 +7245,7 @@ restart: CASE (OP_push_i32) : *sp++ = JS_NewInt32 (ctx, get_u32 (pc)); pc += 4; BREAK; - CASE (OP_push_const) : *sp++ = JS_DupValue (ctx, b->cpool[get_u32 (pc)]); + CASE (OP_push_const) : *sp++ = b->cpool[get_u32 (pc)]; pc += 4; BREAK; #if SHORT_OPCODES @@ -7377,10 +7265,10 @@ restart: CASE (OP_push_i16) : *sp++ = JS_NewInt32 (ctx, get_i16 (pc)); pc += 2; BREAK; - CASE (OP_push_const8) : *sp++ = JS_DupValue (ctx, b->cpool[*pc++]); + CASE (OP_push_const8) : *sp++ = b->cpool[*pc++]; BREAK; CASE (OP_fclosure8) - : *sp++ = js_closure (ctx, JS_DupValue (ctx, b->cpool[*pc++]), var_refs, sf); + : *sp++ = js_closure (ctx, b->cpool[*pc++], var_refs, sf); if (unlikely (JS_IsException (sp[-1]))) goto exception; BREAK; CASE (OP_push_empty_string) : *sp++ = JS_KEY_empty; @@ -7391,7 +7279,7 @@ restart: CASE (OP_push_this) : /* OP_push_this is only called at the start of a function */ { - JSValue val = JS_DupValue (ctx, this_obj); + JSValue val = this_obj; *sp++ = val; } BREAK; @@ -7406,7 +7294,7 @@ restart: int arg = *pc++; switch (arg) { case OP_SPECIAL_OBJECT_THIS_FUNC: - *sp++ = JS_DupValue (ctx, sf->cur_func); + *sp++ = sf->cur_func; break; case OP_SPECIAL_OBJECT_VAR_OBJECT: *sp++ = JS_NewObjectProto (ctx, JS_NULL); @@ -7417,47 +7305,46 @@ restart: } } BREAK; - CASE (OP_drop) : JS_FreeValue (ctx, sp[-1]); + CASE (OP_drop) : ; sp--; BREAK; - CASE (OP_nip) : JS_FreeValue (ctx, sp[-2]); + CASE (OP_nip) : ; sp[-2] = sp[-1]; sp--; BREAK; CASE (OP_nip1) : /* a b c -> b c */ - JS_FreeValue (ctx, sp[-3]); sp[-3] = sp[-2]; sp[-2] = sp[-1]; sp--; BREAK; - CASE (OP_dup) : sp[0] = JS_DupValue (ctx, sp[-1]); + CASE (OP_dup) : sp[0] = sp[-1]; sp++; BREAK; CASE (OP_dup2) : /* a b -> a b a b */ - sp[0] = JS_DupValue (ctx, sp[-2]); - sp[1] = JS_DupValue (ctx, sp[-1]); + sp[0] = sp[-2]; + sp[1] = sp[-1]; sp += 2; BREAK; CASE (OP_dup3) : /* a b c -> a b c a b c */ - sp[0] = JS_DupValue (ctx, sp[-3]); - sp[1] = JS_DupValue (ctx, sp[-2]); - sp[2] = JS_DupValue (ctx, sp[-1]); + sp[0] = sp[-3]; + sp[1] = sp[-2]; + sp[2] = sp[-1]; sp += 3; BREAK; CASE (OP_dup1) : /* a b -> a a b */ sp[0] = sp[-1]; - sp[-1] = JS_DupValue (ctx, sp[-2]); + sp[-1] = sp[-2]; sp++; BREAK; CASE (OP_insert2) : /* obj a -> a obj a (dup_x1) */ sp[0] = sp[-1]; sp[-1] = sp[-2]; - sp[-2] = JS_DupValue (ctx, sp[0]); + sp[-2] = sp[0]; sp++; BREAK; CASE (OP_insert3) @@ -7465,7 +7352,7 @@ restart: sp[0] = sp[-1]; sp[-1] = sp[-2]; sp[-2] = sp[-3]; - sp[-3] = JS_DupValue (ctx, sp[0]); + sp[-3] = sp[0]; sp++; BREAK; CASE (OP_insert4) @@ -7474,7 +7361,7 @@ restart: sp[-1] = sp[-2]; sp[-2] = sp[-3]; sp[-3] = sp[-4]; - sp[-4] = JS_DupValue (ctx, sp[0]); + sp[-4] = sp[0]; sp++; BREAK; CASE (OP_perm3) @@ -7573,7 +7460,7 @@ restart: BREAK; CASE (OP_fclosure) : { - JSValue bfunc = JS_DupValue (ctx, b->cpool[get_u32 (pc)]); + JSValue bfunc = b->cpool[get_u32 (pc)]; pc += 4; *sp++ = js_closure (ctx, bfunc, var_refs, sf); if (unlikely (JS_IsException (sp[-1]))) goto exception; @@ -7597,7 +7484,6 @@ restart: if (unlikely (JS_IsException (ret_val))) goto exception; if (opcode == OP_tail_call) goto done; for (i = -1; i < call_argc; i++) - JS_FreeValue (ctx, call_argv[i]); sp -= call_argc + 1; *sp++ = ret_val; } @@ -7637,14 +7523,12 @@ restart: ret_val = JS_CallInternal (ctx, call_argv[-2], JS_NULL, 2, proxy_argv, 0); - JS_FreeValue (ctx, args); } else { ret_val = JS_CallInternal (ctx, call_argv[-1], call_argv[-2], call_argc, call_argv, 0); } if (unlikely (JS_IsException (ret_val))) goto exception; if (opcode == OP_tail_call_method) goto done; for (i = -2; i < call_argc; i++) - JS_FreeValue (ctx, call_argv[i]); sp -= call_argc + 2; *sp++ = ret_val; } @@ -7716,7 +7600,6 @@ restart: } if (unlikely (JS_IsException (ret_val))) goto exception; for (i = -1; i < call_argc; i++) - JS_FreeValue (ctx, call_argv[i]); sp -= call_argc + 1; *sp++ = ret_val; } @@ -7812,7 +7695,6 @@ restart: pc += 5; sf->cur_pc = pc; if (JS_DefineGlobalFunction (ctx, key, sp[-1], flags)) goto exception; - JS_FreeValue (ctx, sp[-1]); sp--; } BREAK; @@ -7821,7 +7703,7 @@ restart: int idx; idx = get_u16 (pc); pc += 2; - sp[0] = JS_DupValue (ctx, var_buf[idx]); + sp[0] = var_buf[idx]; sp++; } BREAK; @@ -7837,14 +7719,14 @@ restart: int idx; idx = get_u16 (pc); pc += 2; - set_value (ctx, &var_buf[idx], JS_DupValue (ctx, sp[-1])); + set_value (ctx, &var_buf[idx], sp[-1]); } BREAK; CASE (OP_get_arg) : { int idx; idx = get_u16 (pc); pc += 2; - sp[0] = JS_DupValue (ctx, arg_buf[idx]); + sp[0] = arg_buf[idx]; sp++; } BREAK; @@ -7860,26 +7742,26 @@ restart: int idx; idx = get_u16 (pc); pc += 2; - set_value (ctx, &arg_buf[idx], JS_DupValue (ctx, sp[-1])); + set_value (ctx, &arg_buf[idx], sp[-1]); } BREAK; #if SHORT_OPCODES - CASE (OP_get_loc8) : *sp++ = JS_DupValue (ctx, var_buf[*pc++]); + CASE (OP_get_loc8) : *sp++ = var_buf[*pc++]; BREAK; CASE (OP_put_loc8) : set_value (ctx, &var_buf[*pc++], *--sp); BREAK; CASE (OP_set_loc8) - : set_value (ctx, &var_buf[*pc++], JS_DupValue (ctx, sp[-1])); + : set_value (ctx, &var_buf[*pc++], sp[-1]); BREAK; - CASE (OP_get_loc0) : *sp++ = JS_DupValue (ctx, var_buf[0]); + CASE (OP_get_loc0) : *sp++ = var_buf[0]; BREAK; - CASE (OP_get_loc1) : *sp++ = JS_DupValue (ctx, var_buf[1]); + CASE (OP_get_loc1) : *sp++ = var_buf[1]; BREAK; - CASE (OP_get_loc2) : *sp++ = JS_DupValue (ctx, var_buf[2]); + CASE (OP_get_loc2) : *sp++ = var_buf[2]; BREAK; - CASE (OP_get_loc3) : *sp++ = JS_DupValue (ctx, var_buf[3]); + CASE (OP_get_loc3) : *sp++ = var_buf[3]; BREAK; CASE (OP_put_loc0) : set_value (ctx, &var_buf[0], *--sp); BREAK; @@ -7890,24 +7772,24 @@ restart: CASE (OP_put_loc3) : set_value (ctx, &var_buf[3], *--sp); BREAK; CASE (OP_set_loc0) - : set_value (ctx, &var_buf[0], JS_DupValue (ctx, sp[-1])); + : set_value (ctx, &var_buf[0], sp[-1]); BREAK; CASE (OP_set_loc1) - : set_value (ctx, &var_buf[1], JS_DupValue (ctx, sp[-1])); + : set_value (ctx, &var_buf[1], sp[-1]); BREAK; CASE (OP_set_loc2) - : set_value (ctx, &var_buf[2], JS_DupValue (ctx, sp[-1])); + : set_value (ctx, &var_buf[2], sp[-1]); BREAK; CASE (OP_set_loc3) - : set_value (ctx, &var_buf[3], JS_DupValue (ctx, sp[-1])); + : set_value (ctx, &var_buf[3], sp[-1]); BREAK; - CASE (OP_get_arg0) : *sp++ = JS_DupValue (ctx, arg_buf[0]); + CASE (OP_get_arg0) : *sp++ = arg_buf[0]; BREAK; - CASE (OP_get_arg1) : *sp++ = JS_DupValue (ctx, arg_buf[1]); + CASE (OP_get_arg1) : *sp++ = arg_buf[1]; BREAK; - CASE (OP_get_arg2) : *sp++ = JS_DupValue (ctx, arg_buf[2]); + CASE (OP_get_arg2) : *sp++ = arg_buf[2]; BREAK; - CASE (OP_get_arg3) : *sp++ = JS_DupValue (ctx, arg_buf[3]); + CASE (OP_get_arg3) : *sp++ = arg_buf[3]; BREAK; CASE (OP_put_arg0) : set_value (ctx, &arg_buf[0], *--sp); BREAK; @@ -7918,24 +7800,24 @@ restart: CASE (OP_put_arg3) : set_value (ctx, &arg_buf[3], *--sp); BREAK; CASE (OP_set_arg0) - : set_value (ctx, &arg_buf[0], JS_DupValue (ctx, sp[-1])); + : set_value (ctx, &arg_buf[0], sp[-1]); BREAK; CASE (OP_set_arg1) - : set_value (ctx, &arg_buf[1], JS_DupValue (ctx, sp[-1])); + : set_value (ctx, &arg_buf[1], sp[-1]); BREAK; CASE (OP_set_arg2) - : set_value (ctx, &arg_buf[2], JS_DupValue (ctx, sp[-1])); + : set_value (ctx, &arg_buf[2], sp[-1]); BREAK; CASE (OP_set_arg3) - : set_value (ctx, &arg_buf[3], JS_DupValue (ctx, sp[-1])); + : set_value (ctx, &arg_buf[3], sp[-1]); BREAK; - CASE (OP_get_var_ref0) : *sp++ = JS_DupValue (ctx, *var_refs[0]->pvalue); + CASE (OP_get_var_ref0) : *sp++ = *var_refs[0]->pvalue; BREAK; - CASE (OP_get_var_ref1) : *sp++ = JS_DupValue (ctx, *var_refs[1]->pvalue); + CASE (OP_get_var_ref1) : *sp++ = *var_refs[1]->pvalue; BREAK; - CASE (OP_get_var_ref2) : *sp++ = JS_DupValue (ctx, *var_refs[2]->pvalue); + CASE (OP_get_var_ref2) : *sp++ = *var_refs[2]->pvalue; BREAK; - CASE (OP_get_var_ref3) : *sp++ = JS_DupValue (ctx, *var_refs[3]->pvalue); + CASE (OP_get_var_ref3) : *sp++ = *var_refs[3]->pvalue; BREAK; CASE (OP_put_var_ref0) : set_value (ctx, var_refs[0]->pvalue, *--sp); BREAK; @@ -7946,16 +7828,16 @@ restart: CASE (OP_put_var_ref3) : set_value (ctx, var_refs[3]->pvalue, *--sp); BREAK; CASE (OP_set_var_ref0) - : set_value (ctx, var_refs[0]->pvalue, JS_DupValue (ctx, sp[-1])); + : set_value (ctx, var_refs[0]->pvalue, sp[-1]); BREAK; CASE (OP_set_var_ref1) - : set_value (ctx, var_refs[1]->pvalue, JS_DupValue (ctx, sp[-1])); + : set_value (ctx, var_refs[1]->pvalue, sp[-1]); BREAK; CASE (OP_set_var_ref2) - : set_value (ctx, var_refs[2]->pvalue, JS_DupValue (ctx, sp[-1])); + : set_value (ctx, var_refs[2]->pvalue, sp[-1]); BREAK; CASE (OP_set_var_ref3) - : set_value (ctx, var_refs[3]->pvalue, JS_DupValue (ctx, sp[-1])); + : set_value (ctx, var_refs[3]->pvalue, sp[-1]); BREAK; #endif @@ -7965,7 +7847,7 @@ restart: idx = get_u16 (pc); pc += 2; val = *var_refs[idx]->pvalue; - sp[0] = JS_DupValue (ctx, val); + sp[0] = val; sp++; } BREAK; @@ -7981,7 +7863,7 @@ restart: int idx; idx = get_u16 (pc); pc += 2; - set_value (ctx, var_refs[idx]->pvalue, JS_DupValue (ctx, sp[-1])); + set_value (ctx, var_refs[idx]->pvalue, sp[-1]); } BREAK; CASE (OP_get_var_ref_check) : { @@ -7994,7 +7876,7 @@ restart: JS_ThrowReferenceErrorUninitialized2 (ctx, b, idx, TRUE); goto exception; } - sp[0] = JS_DupValue (ctx, val); + sp[0] = val; sp++; } BREAK; @@ -8037,7 +7919,7 @@ restart: JS_ThrowReferenceErrorUninitialized2 (ctx, b, idx, FALSE); goto exception; } - sp[0] = JS_DupValue (ctx, var_buf[idx]); + sp[0] = var_buf[idx]; sp++; } BREAK; @@ -8049,7 +7931,7 @@ restart: JS_ThrowReferenceErrorUninitialized2 (caller_ctx, b, idx, FALSE); goto exception; } - sp[0] = JS_DupValue (ctx, var_buf[idx]); + sp[0] = var_buf[idx]; sp++; } BREAK; @@ -8108,7 +7990,7 @@ restart: /* Store var_ref as opaque data */ JS_SetOpaque (sp[-1], var_ref); - *sp++ = JS_DupValue (ctx, key); + *sp++ = key; } BREAK; CASE (OP_make_var_ref) : { @@ -8240,11 +8122,9 @@ restart: ret_val = *--sp; while (sp > stack_buf && JS_VALUE_GET_TAG (sp[-1]) != JS_TAG_CATCH_OFFSET) { - JS_FreeValue (ctx, *--sp); } if (unlikely (sp == stack_buf)) { JS_ThrowInternalError (ctx, "nip_catch"); - JS_FreeValue (ctx, ret_val); goto exception; } sp[-1] = ret_val; @@ -8285,11 +8165,9 @@ restart: if (JS_IsRecord (obj)) { JSRecord *rec = JS_VALUE_GET_RECORD (obj); val = rec_get (ctx, rec, key); - JS_FreeValue (ctx, obj); sp[-1] = val; } else { /* Non-record: return null */ - JS_FreeValue (ctx, sp[-1]); sp[-1] = JS_NULL; } } @@ -8314,7 +8192,7 @@ restart: /* Proxy method-call sugar: func.name(...) -> func("name", [args...]) OP_get_field2 is only emitted when a call immediately follows. */ if (JS_IsFunction (obj)) { - val = JS_DupValue (ctx, key); /* "name" as JSValue string */ + val = key; /* "name" as JSValue string */ *sp++ = val; /* stack becomes [func, "name"] */ } else if (JS_IsRecord (obj)) { /* Record property access - use JSValue key directly */ @@ -8342,8 +8220,6 @@ restart: /* Must be a record to set property */ if (!JS_IsRecord (sp[-2])) { - JS_FreeValue (ctx, sp[-1]); - JS_FreeValue (ctx, sp[-2]); sp -= 2; JS_ThrowTypeError (ctx, "cannot set property of non-record"); goto exception; @@ -8352,7 +8228,6 @@ restart: /* Record property set - use JSValue key directly */ JSRecord *rec = JS_VALUE_GET_RECORD (sp[-2]); ret = rec_set_own (ctx, rec, key, sp[-1]); - JS_FreeValue (ctx, sp[-2]); sp -= 2; if (unlikely (ret < 0)) goto exception; } @@ -8371,7 +8246,6 @@ restart: /* Must be a record */ if (!JS_IsRecord (sp[-2])) { - JS_FreeValue (ctx, sp[-1]); sp--; JS_ThrowTypeError (ctx, "cannot define field on non-record"); goto exception; @@ -8430,9 +8304,8 @@ restart: /* JS_SetProperty consumes value, so don't free sp[-1] on success */ ret = JS_SetProperty (ctx, obj, key, value); } else { - JS_FreeValue (ctx, sp[-1]); } - if (is_computed) { JS_FreeValue (ctx, sp[-2]); } + if (is_computed) { ; } sp -= 1 + is_computed; if (unlikely (ret < 0)) goto exception; } @@ -8447,7 +8320,6 @@ restart: JSValue val; sf->cur_pc = pc; val = JS_GetPropertyValue (ctx, sp[-2], sp[-1]); - JS_FreeValue (ctx, sp[-2]); sp[-2] = val; sp--; if (unlikely (JS_IsException (val))) goto exception; @@ -8478,12 +8350,11 @@ restart: sf->cur_pc = pc; ret_val = JS_ToPropertyKey (ctx, sp[-1]); if (JS_IsException (ret_val)) goto exception; - JS_FreeValue (ctx, sp[-1]); sp[-1] = ret_val; break; } sf->cur_pc = pc; - val = JS_GetPropertyValue (ctx, sp[-2], JS_DupValue (ctx, sp[-1])); + val = JS_GetPropertyValue (ctx, sp[-2], sp[-1]); *sp++ = val; if (unlikely (JS_IsException (val))) goto exception; } @@ -8503,7 +8374,7 @@ restart: JSVarRef *var_ref = (JSVarRef *)REC_GET_OPAQUE(p); if (var_ref) { val = var_ref->is_detached ? var_ref->value : *var_ref->pvalue; - *sp++ = JS_DupValue (ctx, val); + *sp++ = val; break; } } @@ -8540,7 +8411,6 @@ restart: sf->cur_pc = pc; ret = JS_SetPropertyValue (ctx, sp[-3], sp[-2], sp[-1]); - JS_FreeValue (ctx, sp[-3]); sp -= 3; if (unlikely (ret < 0)) goto exception; } @@ -8562,8 +8432,6 @@ restart: JSValue *pval = var_ref->is_detached ? &var_ref->value : var_ref->pvalue; set_value (ctx, pval, val); - JS_FreeValue (ctx, ref_obj); - JS_FreeValue (ctx, key); sp -= 3; break; } @@ -8583,8 +8451,6 @@ restart: goto exception; } ret = JS_SetPropertyInternal (ctx, ref_obj, key, val); - JS_FreeValue (ctx, sp[-2]); - JS_FreeValue (ctx, sp[-3]); sp -= 3; if (unlikely (ret < 0)) goto exception; } @@ -8592,7 +8458,7 @@ restart: CASE (OP_define_array_el) : { int ret; - ret = JS_SetPropertyValue (ctx, sp[-3], JS_DupValue (ctx, sp[-2]), sp[-1]); + ret = JS_SetPropertyValue (ctx, sp[-3], sp[-2], sp[-1]); sp -= 1; if (unlikely (ret < 0)) goto exception; } @@ -8897,7 +8763,7 @@ restart: sf->cur_pc = pc; /* must duplicate otherwise the variable value may be destroyed before JS code accesses it */ - op1 = JS_DupValue (ctx, op1); + op1 = op1; if (js_unary_arith_slow (ctx, &op1 + 1, OP_inc)) goto exception; set_value (ctx, &var_buf[idx], op1); } @@ -8920,7 +8786,7 @@ restart: sf->cur_pc = pc; /* must duplicate otherwise the variable value may be destroyed before JS code accesses it */ - op1 = JS_DupValue (ctx, op1); + op1 = op1; if (js_unary_arith_slow (ctx, &op1 + 1, OP_dec)) goto exception; set_value (ctx, &var_buf[idx], op1); } @@ -9164,7 +9030,6 @@ restart: sf->cur_pc = pc; ret_val = JS_ToObject (ctx, sp[-1]); if (JS_IsException (ret_val)) goto exception; - JS_FreeValue (ctx, sp[-1]); sp[-1] = ret_val; } BREAK; @@ -9177,7 +9042,6 @@ restart: sf->cur_pc = pc; ret_val = JS_ToPropertyKey (ctx, sp[-1]); if (JS_IsException (ret_val)) goto exception; - JS_FreeValue (ctx, sp[-1]); sp[-1] = ret_val; break; } @@ -9205,7 +9069,6 @@ restart: goto exception; } b = pretext_concat_value (ctx, b, s); - JS_FreeValue (ctx, s); if (!b) { sp -= n; goto exception; @@ -9219,7 +9082,6 @@ restart: } for (i = 0; i < n; i++) - JS_FreeValue (ctx, sp[i - n]); sp -= n; *sp++ = out; } @@ -9234,7 +9096,6 @@ restart: sp[-1] = JS_TRUE; BREAK; free_and_set_false: - JS_FreeValue (ctx, sp[-1]); sp[-1] = JS_FALSE; BREAK; CASE (OP_invalid) @@ -9254,7 +9115,6 @@ exception: /* All exceptions are catchable in the simplified runtime */ while (sp > stack_buf) { JSValue val = *--sp; - JS_FreeValue (ctx, val); if (JS_VALUE_GET_TAG (val) == JS_TAG_CATCH_OFFSET) { int pos = JS_VALUE_GET_INT (val); if (pos != 0) { @@ -9273,7 +9133,6 @@ done: } /* free the local variables and stack */ for (pval = local_buf; pval < sp; pval++) { - JS_FreeValue (ctx, *pval); } rt->current_stack_frame = sf->prev_frame; @@ -9290,7 +9149,6 @@ JSValue JS_Call (JSContext *ctx, JSValue func_obj, JSValue this_obj, int argc, J static JSValue JS_CallFree (JSContext *ctx, JSValue func_obj, JSValue this_obj, int argc, JSValue *argv) { JSValue res = JS_CallInternal (ctx, func_obj, this_obj, argc, (JSValue *)argv, JS_CALL_FLAG_COPY_ARGV); - JS_FreeValue (ctx, func_obj); return res; } @@ -9675,22 +9533,16 @@ static __exception int next_token (JSParseState *s); static void free_token (JSParseState *s, JSToken *token) { switch (token->val) { case TOK_NUMBER: - JS_FreeValue (s->ctx, token->u.num.val); break; case TOK_STRING: case TOK_TEMPLATE: - JS_FreeValue (s->ctx, token->u.str.str); break; case TOK_REGEXP: - JS_FreeValue (s->ctx, token->u.regexp.body); - JS_FreeValue (s->ctx, token->u.regexp.flags); break; case TOK_IDENT: - JS_FreeValue (s->ctx, token->u.ident.str); break; default: if (token->val >= TOK_FIRST_KEYWORD && token->val <= TOK_LAST_KEYWORD) { - JS_FreeValue (s->ctx, token->u.ident.str); } break; } @@ -10127,8 +9979,6 @@ static __exception int js_parse_regexp (JSParseState *s) { body_str = pretext_end (s->ctx, b); flags_str = pretext_end (s->ctx, b2); if (JS_IsException (body_str) || JS_IsException (flags_str)) { - JS_FreeValue (s->ctx, body_str); - JS_FreeValue (s->ctx, flags_str); return -1; } s->token.val = TOK_REGEXP; @@ -10544,7 +10394,6 @@ redo: if (JS_IsNull (ret) || lre_js_is_ident_next ( unicode_from_utf8 (p, UTF8_CHAR_LEN_MAX, &p1))) { - JS_FreeValue (s->ctx, ret); js_parse_error (s, "invalid number literal"); goto fail; } @@ -11317,7 +11166,7 @@ static int cpool_add (JSParseState *s, JSValue val); /* Emit a key (JSValue) by adding to cpool and emitting the index. Used for variable opcodes (get_var, put_var, etc.) and property ops. */ static int emit_key (JSParseState *s, JSValue name) { - int idx = cpool_add (s, JS_DupValue (s->ctx, name)); + int idx = cpool_add (s, name); if (idx < 0) return -1; emit_u32 (s, idx); return 0; @@ -11410,7 +11259,7 @@ static int cpool_add (JSParseState *s, JSValue val) { static __exception int emit_push_const (JSParseState *s, JSValue val) { int idx; - idx = cpool_add (s, JS_DupValue (s->ctx, val)); + idx = cpool_add (s, val); if (idx < 0) return -1; emit_op (s, OP_push_const); emit_u32 (s, idx); @@ -11567,7 +11416,7 @@ static int add_var (JSContext *ctx, JSFunctionDef *fd, JSValue name) { return -1; vd = &fd->vars[fd->var_count++]; memset (vd, 0, sizeof (*vd)); - vd->var_name = JS_DupValue (ctx, name); + vd->var_name = name; vd->func_pool_idx = -1; return fd->var_count - 1; } @@ -11607,7 +11456,7 @@ static int add_arg (JSContext *ctx, JSFunctionDef *fd, JSValue name) { return -1; vd = &fd->args[fd->arg_count++]; memset (vd, 0, sizeof (*vd)); - vd->var_name = JS_DupValue (ctx, name); + vd->var_name = name; vd->func_pool_idx = -1; return fd->arg_count - 1; } @@ -11624,7 +11473,7 @@ static JSGlobalVar *add_global_var (JSContext *ctx, JSFunctionDef *s, JSValue na hf->is_lexical = FALSE; hf->is_const = FALSE; hf->scope_level = s->scope_level; - hf->var_name = JS_DupValue (ctx, name); + hf->var_name = name; return hf; } @@ -11776,7 +11625,6 @@ static __exception int js_parse_template (JSParseState *s, int call, int *argc) if (JS_IsException (template_object)) return -1; // pool_idx = s->cur_func->cpool_count; ret = emit_push_const (s, template_object); - JS_FreeValue (ctx, template_object); if (ret) return -1; raw_array = JS_NewArray (ctx); if (JS_IsException (raw_array)) return -1; @@ -11791,7 +11639,7 @@ static __exception int js_parse_template (JSParseState *s, int call, int *argc) const uint8_t *p = s->token.ptr + 1; cooked = s->token; if (call) { - if (JS_SetPropertyUint32 (ctx, raw_array, depth, JS_DupValue (ctx, s->token.u.str.str)) + if (JS_SetPropertyUint32 (ctx, raw_array, depth, s->token.u.str.str) < 0) { return -1; } @@ -11810,19 +11658,16 @@ static __exception int js_parse_template (JSParseState *s, int call, int *argc) /* re-parse the string with escape sequences and throw a syntax error if it contains invalid sequences */ - JS_FreeValue (ctx, s->token.u.str.str); s->token.u.str.str = JS_NULL; if (js_parse_string (s, '`', TRUE, p, &cooked, &p)) return -1; str = JS_VALUE_GET_STRING (cooked.u.str.str); if (JSText_len (str) != 0 || depth == 0) { ret = emit_push_const (s, cooked.u.str.str); - JS_FreeValue (s->ctx, cooked.u.str.str); if (ret) return -1; /* For single-literal templates, go directly to done1 */ if (depth == 0 && s->token.u.str.sep == '`') goto done1; depth++; } else { - JS_FreeValue (s->ctx, cooked.u.str.str); } } if (s->token.u.str.sep == '`') goto done; @@ -11874,7 +11719,7 @@ static int __exception js_parse_property_name (JSParseState *s, JSValue *pname, is_non_reserved_ident = (s->token.val == TOK_IDENT && !s->token.u.ident.is_reserved); /* keywords and reserved words have a valid JSValue key */ - name = JS_DupValue (s->ctx, s->token.u.ident.str); + name = s->token.u.ident.str; if (next_token (s)) goto fail1; if (is_non_reserved_ident && prop_type == PROP_TYPE_IDENT && allow_var) { if (!(s->token.val == ':' || (s->token.val == '(' && allow_method))) { @@ -11883,7 +11728,7 @@ static int __exception js_parse_property_name (JSParseState *s, JSValue *pname, } } else if (s->token.val == TOK_STRING) { /* String literal as property name - already interned from parsing */ - name = JS_DupValue (s->ctx, s->token.u.str.str); + name = s->token.u.str.str; if (next_token (s)) goto fail1; } else if (s->token.val == TOK_NUMBER) { /* Numeric property name - convert to string key */ @@ -11902,7 +11747,6 @@ static int __exception js_parse_property_name (JSParseState *s, JSValue *pname, } if (prop_type != PROP_TYPE_IDENT && prop_type != PROP_TYPE_VAR && s->token.val != '(') { - JS_FreeValue (s->ctx, name); invalid_prop: js_parse_error (s, "invalid property name"); goto fail; @@ -11910,7 +11754,6 @@ static int __exception js_parse_property_name (JSParseState *s, JSValue *pname, *pname = name; return prop_type; fail1: - JS_FreeValue (s->ctx, name); fail: *pname = JS_NULL; return -1; @@ -12091,7 +11934,7 @@ static void set_object_name (JSParseState *s, JSValue name) { - get_u32 (fd->byte_code.buf + fd->last_opcode_pos + 1); assert (fd->byte_code.buf[define_class_pos] == OP_define_class); /* Update the cpool index in the bytecode to point to the new name */ - cpool_idx = cpool_add (s, JS_DupValue (s->ctx, name)); + cpool_idx = cpool_add (s, name); put_u32 (fd->byte_code.buf + define_class_pos + 1, cpool_idx); fd->last_opcode_pos = -1; } @@ -12172,7 +12015,6 @@ static __exception int js_parse_object_literal (JSParseState *s) { emit_prop_key (s, name); } } - JS_FreeValue (s->ctx, name); next: name = JS_NULL; if (s->token.val != ',') break; @@ -12181,7 +12023,6 @@ static __exception int js_parse_object_literal (JSParseState *s) { if (js_parse_expect (s, '}')) goto fail; return 0; fail: - JS_FreeValue (s->ctx, name); return -1; } @@ -12267,14 +12108,12 @@ static __exception int get_lvalue (JSParseState *s, int *popcode, int *pscope, J /* Read cpool index and get key from cpool */ uint32_t idx = get_u32 (fd->byte_code.buf + fd->last_opcode_pos + 1); scope = get_u16 (fd->byte_code.buf + fd->last_opcode_pos + 5); - name = JS_DupValue (s->ctx, fd->cpool[idx]); + name = fd->cpool[idx]; if (js_key_equal_str (name, "eval")) { - JS_FreeValue (s->ctx, name); return js_parse_error (s, "invalid lvalue in strict mode"); } if (js_key_equal_str (name, "this") || js_key_equal_str (name, "new.target")) { - JS_FreeValue (s->ctx, name); goto invalid_lvalue; } depth = 2; /* will generate OP_get_ref_value */ @@ -12283,7 +12122,7 @@ static __exception int get_lvalue (JSParseState *s, int *popcode, int *pscope, J case OP_get_field: { /* Read cpool index and get property name from cpool */ uint32_t idx = get_u32 (fd->byte_code.buf + fd->last_opcode_pos + 1); - name = JS_DupValue (s->ctx, fd->cpool[idx]); + name = fd->cpool[idx]; depth = 1; break; } @@ -12393,7 +12232,6 @@ static void put_lvalue (JSParseState *s, int opcode, int scope, JSValue name, in case OP_get_ref_value: /* depth = 2 */ if (opcode == OP_get_ref_value) { - JS_FreeValue (s->ctx, name); emit_label (s, label); } switch (special) { @@ -12506,13 +12344,12 @@ static JSValue js_parse_destructuring_var (JSParseState *s, int tok, int is_arg) js_parse_error (s, "invalid destructuring target"); return JS_NULL; } - name = JS_DupValue (s->ctx, s->token.u.ident.str); + name = s->token.u.ident.str; if (is_arg && js_parse_check_duplicate_parameter (s, name)) goto fail; if (next_token (s)) goto fail; return name; fail: - JS_FreeValue (s->ctx, name); return JS_NULL; } @@ -12661,7 +12498,7 @@ static int js_parse_destructuring_element (JSParseState *s, int tok, int is_arg, goto lvalue1; } else { /* no need to make a reference for let/const */ - var_name = JS_DupValue (s->ctx, prop_name); + var_name = prop_name; opcode = OP_scope_get_var; scope = s->cur_func->scope_level; label_lvalue = -1; @@ -12728,9 +12565,7 @@ static int js_parse_destructuring_element (JSParseState *s, int tok, int is_arg, return has_initializer; prop_error: - JS_FreeValue (s->ctx, prop_name); var_error: - JS_FreeValue (s->ctx, var_name); return -1; } @@ -12810,7 +12645,6 @@ static __exception int js_parse_postfix_expr (JSParseState *s, return -1; } ret = emit_push_const (s, str); - JS_FreeValue (s->ctx, str); if (ret) return -1; /* we use a specific opcode to be sure the correct function is called (otherwise the bytecode would have @@ -12850,16 +12684,14 @@ static __exception int js_parse_postfix_expr (JSParseState *s, return js_parse_error_reserved_identifier (s); } source_ptr = s->token.ptr; - name = JS_DupValue (s->ctx, s->token.u.ident.str); + name = s->token.u.ident.str; if (next_token (s)) { - JS_FreeValue (s->ctx, name); return -1; } emit_source_pos (s, source_ptr); emit_op (s, OP_scope_get_var); emit_key (s, name); emit_u16 (s, s->cur_func->scope_level); - JS_FreeValue (s->ctx, name); } break; case '{': case '[': @@ -13499,7 +13331,6 @@ next: return -1; if (js_parse_assign_expr2 (s, parse_flags)) { - JS_FreeValue (s->ctx, name); return -1; } @@ -13542,7 +13373,6 @@ next: emit_op (s, OP_drop); if (js_parse_assign_expr2 (s, parse_flags)) { - JS_FreeValue (s->ctx, name); return -1; } @@ -13735,7 +13565,7 @@ static __exception int js_parse_var (JSParseState *s, int parse_flags, int tok, if (s->token.u.ident.is_reserved) { return js_parse_error_reserved_identifier (s); } - name = JS_DupValue (ctx, s->token.u.ident.str); + name = s->token.u.ident.str; if (js_key_equal (name, JS_KEY_let) && tok == TOK_DEF) { js_parse_error (s, "'let' is not a valid lexical identifier"); goto var_error; @@ -13764,7 +13594,6 @@ static __exception int js_parse_var (JSParseState *s, int parse_flags, int tok, emit_u16 (s, fd->scope_level); } } - JS_FreeValue (ctx, name); } else { int skip_bits; if ((s->token.val == '[' || s->token.val == '{') @@ -13783,7 +13612,6 @@ static __exception int js_parse_var (JSParseState *s, int parse_flags, int tok, return 0; var_error: - JS_FreeValue (ctx, name); return -1; } @@ -13859,7 +13687,7 @@ static __exception int js_parse_statement_or_decl (JSParseState *s, if (is_label (s)) { BlockEnv *be; - label_name = JS_DupValue (ctx, s->token.u.ident.str); + label_name = s->token.u.ident.str; for (be = s->cur_func->top_break; be; be = be->prev) { if (js_key_equal (be->label_name, label_name)) { @@ -14280,16 +14108,14 @@ static __exception int js_parse_statement_or_decl (JSParseState *s, goto fail; } } else { - name = JS_DupValue (ctx, s->token.u.ident.str); + name = s->token.u.ident.str; if (next_token (s) || js_define_var (s, name, TOK_CATCH) < 0) { - JS_FreeValue (ctx, name); goto fail; } /* store the exception value in the catch variable */ emit_op (s, OP_scope_put_var); emit_key (s, name); emit_u16 (s, s->cur_func->scope_level); - JS_FreeValue (ctx, name); } if (js_parse_expect (s, ')')) goto fail; } @@ -14427,10 +14253,8 @@ static __exception int js_parse_statement_or_decl (JSParseState *s, break; } done: - JS_FreeValue (ctx, label_name); return 0; fail: - JS_FreeValue (ctx, label_name); return -1; } @@ -14548,35 +14372,28 @@ static void js_free_function_def (JSContext *ctx, JSFunctionDef *fd) { js_free (ctx, fd->line_number_slots); for (i = 0; i < fd->cpool_count; i++) { - JS_FreeValue (ctx, fd->cpool[i]); } js_free (ctx, fd->cpool); - JS_FreeValue (ctx, fd->func_name); for (i = 0; i < fd->var_count; i++) { - JS_FreeValue (ctx, fd->vars[i].var_name); } js_free (ctx, fd->vars); for (i = 0; i < fd->arg_count; i++) { - JS_FreeValue (ctx, fd->args[i].var_name); } js_free (ctx, fd->args); for (i = 0; i < fd->global_var_count; i++) { - JS_FreeValue (ctx, fd->global_vars[i].var_name); } js_free (ctx, fd->global_vars); for (i = 0; i < fd->closure_var_count; i++) { JSClosureVar *cv = &fd->closure_var[i]; - JS_FreeValue (ctx, cv->var_name); } js_free (ctx, fd->closure_var); if (fd->scopes != fd->def_scope_array) js_free (ctx, fd->scopes); - JS_FreeValue (ctx, fd->filename); dbuf_free (&fd->pc2line); js_free (ctx, fd->source); @@ -14993,7 +14810,7 @@ static int add_closure_var (JSContext *ctx, JSFunctionDef *s, BOOL is_local, BOO cv->is_lexical = is_lexical; cv->var_kind = var_kind; cv->var_idx = var_idx; - cv->var_name = JS_DupValue (ctx, var_name); + cv->var_name = var_name; return s->closure_var_count - 1; } @@ -15076,7 +14893,7 @@ static int optimize_scope_make_ref (JSContext *ctx, JSFunctionDef *s, DynBuf *bc static int optimize_scope_make_global_ref (JSContext *ctx, JSFunctionDef *s, DynBuf *bc, uint8_t *bc_buf, LabelSlot *ls, int pos_next, JSValue var_name) { int label_pos, end_pos, pos, op; - int cpool_idx = fd_cpool_add (ctx, s, JS_DupValue (ctx, var_name)); + int cpool_idx = fd_cpool_add (ctx, s, var_name); /* replace the reference get/put with normal variable accesses */ @@ -15157,7 +14974,7 @@ static int resolve_pseudo_var (JSContext *ctx, JSFunctionDef *s, JSValue var_nam /* test if 'var_name' is in the variable object on the stack. If is it the case, handle it and jump to 'label_done' */ static void var_object_test (JSContext *ctx, JSFunctionDef *s, JSValue var_name, int op, DynBuf *bc, int *plabel_done, BOOL is_with) { - int cpool_idx = fd_cpool_add (ctx, s, JS_DupValue (ctx, var_name)); + int cpool_idx = fd_cpool_add (ctx, s, var_name); dbuf_putc (bc, op); dbuf_put_u32 (bc, cpool_idx); } @@ -15186,7 +15003,7 @@ static int resolve_scope_var (JSContext *ctx, JSFunctionDef *s, JSValue var_name if (js_key_equal (vd->var_name, var_name)) { if (op == OP_scope_put_var || op == OP_scope_make_ref) { if (vd->is_const) { - int cpool_idx = fd_cpool_add (ctx, s, JS_DupValue (ctx, var_name)); + int cpool_idx = fd_cpool_add (ctx, s, var_name); dbuf_putc (bc, OP_throw_error); dbuf_put_u32 (bc, cpool_idx); dbuf_putc (bc, JS_THROW_VAR_RO); @@ -15218,7 +15035,7 @@ static int resolve_scope_var (JSContext *ctx, JSFunctionDef *s, JSValue var_name && !(var_idx & ARGUMENT_VAR_OFFSET) && s->vars[var_idx].is_const) { /* only happens when assigning a function expression name in strict mode */ - int cpool_idx = fd_cpool_add (ctx, s, JS_DupValue (ctx, var_name)); + int cpool_idx = fd_cpool_add (ctx, s, var_name); dbuf_putc (bc, OP_throw_error); dbuf_put_u32 (bc, cpool_idx); dbuf_putc (bc, JS_THROW_VAR_RO); @@ -15232,7 +15049,7 @@ static int resolve_scope_var (JSContext *ctx, JSFunctionDef *s, JSValue var_name if (!(var_idx & ARGUMENT_VAR_OFFSET) && s->vars[var_idx].var_kind == JS_VAR_FUNCTION_NAME) { /* Create a dummy object reference for the func_var */ - int cpool_idx = fd_cpool_add (ctx, s, JS_DupValue (ctx, var_name)); + int cpool_idx = fd_cpool_add (ctx, s, var_name); if (cpool_idx < 0) return -1; dbuf_putc (bc, OP_object); dbuf_putc (bc, OP_get_loc); @@ -15256,7 +15073,7 @@ static int resolve_scope_var (JSContext *ctx, JSFunctionDef *s, JSValue var_name } else { /* Create a dummy object with a named slot that is a reference to the local variable */ - int cpool_idx = fd_cpool_add (ctx, s, JS_DupValue (ctx, var_name)); + int cpool_idx = fd_cpool_add (ctx, s, var_name); if (var_idx & ARGUMENT_VAR_OFFSET) { dbuf_putc (bc, OP_make_arg_ref); dbuf_put_u32 (bc, cpool_idx); @@ -15338,7 +15155,7 @@ static int resolve_scope_var (JSContext *ctx, JSFunctionDef *s, JSValue var_name if (js_key_equal (vd->var_name, var_name)) { if (op == OP_scope_put_var || op == OP_scope_make_ref) { if (vd->is_const) { - int cpool_idx = fd_cpool_add (ctx, s, JS_DupValue (ctx, var_name)); + int cpool_idx = fd_cpool_add (ctx, s, var_name); dbuf_putc (bc, OP_throw_error); dbuf_put_u32 (bc, cpool_idx); dbuf_putc (bc, JS_THROW_VAR_RO); @@ -15433,7 +15250,7 @@ static int resolve_scope_var (JSContext *ctx, JSFunctionDef *s, JSValue var_name has_idx: if ((op == OP_scope_put_var || op == OP_scope_make_ref) && s->closure_var[idx].is_const) { - int cpool_idx = fd_cpool_add (ctx, s, JS_DupValue (ctx, var_name)); + int cpool_idx = fd_cpool_add (ctx, s, var_name); dbuf_putc (bc, OP_throw_error); dbuf_put_u32 (bc, cpool_idx); dbuf_putc (bc, JS_THROW_VAR_RO); @@ -15443,7 +15260,7 @@ static int resolve_scope_var (JSContext *ctx, JSFunctionDef *s, JSValue var_name case OP_scope_make_ref: if (s->closure_var[idx].var_kind == JS_VAR_FUNCTION_NAME) { /* Create a dummy object reference for the func_var */ - int cpool_idx = fd_cpool_add (ctx, s, JS_DupValue (ctx, var_name)); + int cpool_idx = fd_cpool_add (ctx, s, var_name); if (cpool_idx < 0) return -1; dbuf_putc (bc, OP_object); dbuf_putc (bc, OP_get_var_ref); @@ -15463,7 +15280,7 @@ static int resolve_scope_var (JSContext *ctx, JSFunctionDef *s, JSValue var_name } else { /* Create a dummy object with a named slot that is a reference to the closure variable */ - int cpool_idx = fd_cpool_add (ctx, s, JS_DupValue (ctx, var_name)); + int cpool_idx = fd_cpool_add (ctx, s, var_name); dbuf_putc (bc, OP_make_var_ref_ref); dbuf_put_u32 (bc, cpool_idx); dbuf_put_u16 (bc, idx); @@ -15512,7 +15329,7 @@ static int resolve_scope_var (JSContext *ctx, JSFunctionDef *s, JSValue var_name /* global variable access */ { - int cpool_idx = fd_cpool_add (ctx, s, JS_DupValue (ctx, var_name)); + int cpool_idx = fd_cpool_add (ctx, s, var_name); switch (op) { case OP_scope_make_ref: @@ -15664,7 +15481,7 @@ static void set_closure_from_var (JSContext *ctx, JSClosureVar *cv, JSVarDef *vd cv->is_lexical = vd->is_lexical; cv->var_kind = vd->var_kind; cv->var_idx = var_idx; - cv->var_name = JS_DupValue (ctx, vd->var_name); + cv->var_name = vd->var_name; } /* for direct eval compilation: add references to the variables of the @@ -15702,7 +15519,7 @@ static __exception int add_closure_variables (JSContext *ctx, JSFunctionDef *s, cv->is_lexical = FALSE; cv->var_kind = JS_VAR_NORMAL; cv->var_idx = i; - cv->var_name = JS_DupValue (ctx, vd->var_name); + cv->var_name = vd->var_name; } /* Add local non lexical variables */ for (i = 0; i < b->var_count; i++) { @@ -15731,7 +15548,7 @@ static __exception int add_closure_variables (JSContext *ctx, JSFunctionDef *s, cv->is_lexical = cv0->is_lexical; cv->var_kind = cv0->var_kind; cv->var_idx = i; - cv->var_name = JS_DupValue (ctx, cv0->var_name); + cv->var_name = cv0->var_name; } return 0; } @@ -15920,7 +15737,7 @@ static void instantiate_hoisted_definitions (JSContext *ctx, JSFunctionDef *s, D dbuf_put_u32 (bc, hf->cpool_idx); { - int key_idx = fd_cpool_add (ctx, s, JS_DupValue (ctx, hf->var_name)); + int key_idx = fd_cpool_add (ctx, s, hf->var_name); dbuf_putc (bc, OP_define_func); dbuf_put_u32 (bc, key_idx); dbuf_putc (bc, flags); @@ -15929,7 +15746,7 @@ static void instantiate_hoisted_definitions (JSContext *ctx, JSFunctionDef *s, D goto done_global_var; } else { if (hf->is_lexical) { flags |= DEFINE_GLOBAL_LEX_VAR; } - int key_idx = fd_cpool_add (ctx, s, JS_DupValue (ctx, hf->var_name)); + int key_idx = fd_cpool_add (ctx, s, hf->var_name); dbuf_putc (bc, OP_define_var); dbuf_put_u32 (bc, key_idx); dbuf_putc (bc, flags); @@ -15952,19 +15769,18 @@ static void instantiate_hoisted_definitions (JSContext *ctx, JSFunctionDef *s, D dbuf_putc (bc, OP_put_var_ref); dbuf_put_u16 (bc, idx); } else if (has_closure == 1) { - int key_idx = fd_cpool_add (ctx, s, JS_DupValue (ctx, hf->var_name)); + int key_idx = fd_cpool_add (ctx, s, hf->var_name); dbuf_putc (bc, OP_define_field); dbuf_put_u32 (bc, key_idx); dbuf_putc (bc, OP_drop); } else { /* XXX: Check if variable is writable and enumerable */ - int key_idx = fd_cpool_add (ctx, s, JS_DupValue (ctx, hf->var_name)); + int key_idx = fd_cpool_add (ctx, s, hf->var_name); dbuf_putc (bc, OP_put_var); dbuf_put_u32 (bc, key_idx); } } done_global_var: - JS_FreeValue (ctx, hf->var_name); } js_free (ctx, s->global_vars); @@ -16063,7 +15879,7 @@ static __exception int resolve_variables (JSContext *ctx, JSFunctionDef *s) { compilation here, but for consistency with the other checks, we delay the error generation. */ - int key_idx = fd_cpool_add (ctx, s, JS_DupValue (ctx, hf->var_name)); + int key_idx = fd_cpool_add (ctx, s, hf->var_name); dbuf_putc (&bc_out, OP_throw_error); dbuf_put_u32 (&bc_out, key_idx); dbuf_putc (&bc_out, JS_THROW_VAR_REDECL); @@ -16076,7 +15892,7 @@ static __exception int resolve_variables (JSContext *ctx, JSFunctionDef *s) { } { - int key_idx = fd_cpool_add (ctx, s, JS_DupValue (ctx, hf->var_name)); + int key_idx = fd_cpool_add (ctx, s, hf->var_name); dbuf_putc (&bc_out, OP_check_define_var); dbuf_put_u32 (&bc_out, key_idx); } @@ -17589,13 +17405,10 @@ static JSValue js_create_function (JSContext *ctx, JSFunctionDef *fd) { /* Strip variable definitions not needed at runtime */ int i; for (i = 0; i < fd->var_count; i++) { - JS_FreeValue (ctx, fd->vars[i].var_name); } for (i = 0; i < fd->arg_count; i++) { - JS_FreeValue (ctx, fd->args[i].var_name); } for (i = 0; i < fd->closure_var_count; i++) { - JS_FreeValue (ctx, fd->closure_var[i].var_name); fd->closure_var[i].var_name = JS_NULL; } } else { @@ -17620,7 +17433,6 @@ static JSValue js_create_function (JSContext *ctx, JSFunctionDef *fd) { b->stack_size = stack_size; if (fd->strip_debug) { - JS_FreeValue (ctx, fd->filename); dbuf_free (&fd->pc2line); // probably useless } else { /* XXX: source and pc2line info should be packed at the end of the @@ -17813,9 +17625,8 @@ static __exception int js_parse_function_decl2 (JSParseState *s, if (s->token.val == TOK_IDENT) { if (s->token.u.ident.is_reserved) return js_parse_error_reserved_identifier (s); - func_name = JS_DupValue (ctx, s->token.u.ident.str); + func_name = s->token.u.ident.str; if (next_token (s)) { - JS_FreeValue (ctx, func_name); return -1; } } else { @@ -17824,7 +17635,7 @@ static __exception int js_parse_function_decl2 (JSParseState *s, } } else if (func_type != JS_PARSE_FUNC_ARROW) { - func_name = JS_DupValue (ctx, func_name); + func_name = func_name; } if (func_type == JS_PARSE_FUNC_VAR) { @@ -17839,7 +17650,6 @@ static __exception int js_parse_function_decl2 (JSParseState *s, /* XXX: should check scope chain */ if (hf && hf->scope_level == fd->scope_level) { js_parse_error (s, "invalid redefinition of global identifier"); - JS_FreeValue (ctx, func_name); return -1; } } else { @@ -17849,7 +17659,6 @@ static __exception int js_parse_function_decl2 (JSParseState *s, lexical_func_idx = define_var (s, fd, func_name, JS_VAR_DEF_FUNCTION_DECL); if (lexical_func_idx < 0) { - JS_FreeValue (ctx, func_name); return -1; } } @@ -17857,7 +17666,6 @@ static __exception int js_parse_function_decl2 (JSParseState *s, fd = js_new_function_def (ctx, fd, FALSE, is_expr, s->filename, ptr, &s->get_line_col_cache); if (!fd) { - JS_FreeValue (ctx, func_name); return -1; } if (pfd) *pfd = fd; @@ -18233,7 +18041,6 @@ static JSValue JS_EvalFunctionInternal (JSContext *ctx, JSValue fun_obj, JSValue fun_obj = js_closure (ctx, fun_obj, var_refs, sf); ret_val = JS_CallFree (ctx, fun_obj, this_obj, 0, NULL); } else { - JS_FreeValue (ctx, fun_obj); ret_val = JS_ThrowTypeError (ctx, "bytecode function expected"); } return ret_val; @@ -18332,7 +18139,7 @@ static JSValue JS_EvalObject (JSContext *ctx, JSValue this_obj, JSValue val, int const char *str; size_t len; - if (!JS_IsText (val)) return JS_DupValue (ctx, val); + if (!JS_IsText (val)) return val; str = JS_ToCStringLen (ctx, &len, val); if (!str) return JS_EXCEPTION; ret = JS_EvalInternal (ctx, this_obj, str, len, "", flags, scope_idx); @@ -18847,7 +18654,6 @@ static int JS_WriteObjectRec (BCWriterState *s, JSValue obj) { JSText *p = JS_VALUE_GET_STRING (tmp); bc_put_u8 (s, BC_TAG_STRING); JS_WriteString (s, p); - JS_FreeValue (s->ctx, tmp); } } } break; @@ -19388,7 +19194,6 @@ static JSValue JS_ReadFunctionTag (BCReaderState *s) { b->realm = JS_DupContext (ctx); return obj; fail: - JS_FreeValue (ctx, obj); return JS_EXCEPTION; } @@ -19412,16 +19217,13 @@ static JSValue JS_ReadObjectTag (BCReaderState *s) { #endif val = JS_ReadObjectRec (s); if (JS_IsException (val)) { - JS_FreeValue (ctx, key); goto fail; } ret = JS_SetPropertyInternal (ctx, obj, key, val); - JS_FreeValue (ctx, key); if (ret < 0) goto fail; } return obj; fail: - JS_FreeValue (ctx, obj); return JS_EXCEPTION; } @@ -19449,7 +19251,6 @@ static JSValue JS_ReadArray (BCReaderState *s, int tag) { val = JS_ReadObjectRec (s); if (JS_IsException (val)) goto fail; /* Replace the null placeholder with the read value */ - JS_FreeValue (ctx, arr->values[i]); arr->values[i] = val; } @@ -19459,11 +19260,9 @@ static JSValue JS_ReadArray (BCReaderState *s, int tag) { val = JS_ReadObjectRec (s); if (JS_IsException (val)) goto fail; /* Skip the raw property for intrinsic arrays */ - JS_FreeValue (ctx, val); } return obj; fail: - JS_FreeValue (ctx, obj); return JS_EXCEPTION; } @@ -19476,11 +19275,8 @@ static JSValue JS_ReadObjectValue (BCReaderState *s) { obj = JS_ToObject (ctx, val); if (JS_IsException (obj)) goto fail; if (BC_add_object_ref (s, obj)) goto fail; - JS_FreeValue (ctx, val); return obj; fail: - JS_FreeValue (ctx, val); - JS_FreeValue (ctx, obj); return JS_EXCEPTION; } @@ -19544,7 +19340,7 @@ static JSValue JS_ReadObjectRec (BCReaderState *s) { if (val >= s->objects_count) { return JS_ThrowSyntaxError (ctx, "invalid object reference (%u >= %u)", val, s->objects_count); } - obj = JS_DupValue (ctx, JS_MKPTR (s->objects[val])); + obj = JS_MKPTR (s->objects[val]); } break; default: invalid_tag: @@ -19612,7 +19408,6 @@ static int check_function (JSContext *ctx, JSValue obj) { } static int check_exception_free (JSContext *ctx, JSValue obj) { - JS_FreeValue (ctx, obj); return JS_IsException (obj); } @@ -19694,7 +19489,7 @@ static JSValue JS_ToObject (JSContext *ctx, JSValue val) { switch (tag) { case JS_TAG_OBJECT: case JS_TAG_EXCEPTION: - return JS_DupValue (ctx, val); + return val; default: /* Primitives cannot be converted to objects - no boxing */ return JS_ThrowTypeError (ctx, "cannot convert to record"); @@ -19703,7 +19498,6 @@ static JSValue JS_ToObject (JSContext *ctx, JSValue val) { static JSValue JS_ToObjectFree (JSContext *ctx, JSValue val) { JSValue obj = JS_ToObject (ctx, val); - JS_FreeValue (ctx, val); return obj; } @@ -19794,7 +19588,7 @@ static JSValue *build_arg_list (JSContext *ctx, uint32_t *plen, JSValue array_ar tab = js_mallocz (ctx, sizeof (tab[0]) * max_uint32 (1, len)); if (!tab) return NULL; for (i = 0; i < len; i++) { - tab[i] = JS_DupValue (ctx, arr->values[i]); + tab[i] = arr->values[i]; } *plen = len; return tab; @@ -19878,11 +19672,9 @@ static JSValue js_error_constructor (JSContext *ctx, JSValue this_val, int argc, for (i = 0; i < len; i++) { JSValue item = JS_GetPropertyUint32 (ctx, argv[0], i); if (JS_IsException (item)) { - JS_FreeValue (ctx, error_list); goto exception; } if (JS_SetPropertyUint32 (ctx, error_list, i, item) < 0) { - JS_FreeValue (ctx, error_list); goto exception; } } @@ -19897,7 +19689,6 @@ static JSValue js_error_constructor (JSContext *ctx, JSValue this_val, int argc, build_backtrace (ctx, obj, NULL, 0, 0, JS_BACKTRACE_FLAG_SKIP_FIRST_LEVEL); return obj; exception: - JS_FreeValue (ctx, obj); return JS_EXCEPTION; } @@ -19919,7 +19710,6 @@ static JSValue js_error_toString (JSContext *ctx, JSValue this_val, int argc, JS else msg = JS_ToStringFree (ctx, msg); if (JS_IsException (msg)) { - JS_FreeValue (ctx, name); return JS_EXCEPTION; } if (!JS_IsEmptyString (name) && !JS_IsEmptyString (msg)) @@ -19950,10 +19740,9 @@ static int js_is_regexp (JSContext *ctx, JSValue obj); /* RegExp */ static void js_regexp_finalizer (JSRuntime *rt, JSValue val) { - JSRecord *p = JS_VALUE_GET_OBJ (val); - JSRegExp *re = (JSRegExp *)REC_GET_OPAQUE(p); - JS_FreeValueRT (rt, JS_MKPTR (re->bytecode)); - JS_FreeValueRT (rt, JS_MKPTR (re->pattern)); + /* With copying GC, memory is reclaimed automatically */ + (void)rt; + (void)val; } /* create a string containing the RegExp bytecode */ @@ -20044,8 +19833,6 @@ static JSValue js_regexp_constructor_internal (JSContext *ctx, JSValue pattern, || JS_VALUE_GET_TAG (pattern) != JS_TAG_STRING) { JS_ThrowTypeError (ctx, "string expected"); fail: - JS_FreeValue (ctx, bc); - JS_FreeValue (ctx, pattern); return JS_EXCEPTION; } @@ -20061,7 +19848,6 @@ static JSValue js_regexp_constructor_internal (JSContext *ctx, JSValue pattern, { JSValue key = JS_KEY_STR (ctx, "lastIndex"); JS_SetPropertyInternal (ctx, obj, key, JS_NewInt32 (ctx, 0)); - JS_FreeValue (ctx, key); } return obj; } @@ -20099,13 +19885,13 @@ static JSValue js_regexp_constructor (JSContext *ctx, JSValue this_val, int argc /* If called with a regexp and no flags, just return a copy */ if (pat_is_regexp && JS_IsNull (flags1)) { re = js_get_regexp (ctx, pat, FALSE); - if (re) return JS_DupValue (ctx, pat); + if (re) return pat; } re = js_get_regexp (ctx, pat, FALSE); if (re) { - pattern = JS_DupValue (ctx, JS_MKPTR (re->pattern)); + pattern = JS_MKPTR (re->pattern); if (JS_IsNull (flags1)) { - bc = JS_DupValue (ctx, JS_MKPTR (re->bytecode)); + bc = JS_MKPTR (re->bytecode); goto no_compilation; } else { flags = JS_ToString (ctx, flags1); @@ -20120,29 +19906,25 @@ static JSValue js_regexp_constructor (JSContext *ctx, JSValue this_val, int argc flags = JS_GetProperty (ctx, pat, JS_KEY_flags); if (JS_IsException (flags)) goto fail; } else { - flags = JS_DupValue (ctx, flags1); + flags = flags1; } } else { - pattern = JS_DupValue (ctx, pat); - flags = JS_DupValue (ctx, flags1); + pattern = pat; + flags = flags1; } if (JS_IsNull (pattern)) { pattern = JS_KEY_empty; } else { val = pattern; pattern = JS_ToString (ctx, val); - JS_FreeValue (ctx, val); if (JS_IsException (pattern)) goto fail; } } bc = js_compile_regexp (ctx, pattern, flags); if (JS_IsException (bc)) goto fail; - JS_FreeValue (ctx, flags); no_compilation: return js_regexp_constructor_internal (ctx, pattern, bc); fail: - JS_FreeValue (ctx, pattern); - JS_FreeValue (ctx, flags); return JS_EXCEPTION; } @@ -20159,8 +19941,8 @@ static JSValue js_regexp_compile (JSContext *ctx, JSValue this_val, int argc, JS if (re1) { if (!JS_IsNull (flags1)) return JS_ThrowTypeError (ctx, "flags must be undefined"); - pattern = JS_DupValue (ctx, JS_MKPTR (re1->pattern)); - bc = JS_DupValue (ctx, JS_MKPTR (re1->bytecode)); + pattern = JS_MKPTR (re1->pattern); + bc = JS_MKPTR (re1->bytecode); } else { bc = JS_NULL; if (JS_IsNull (pattern1)) @@ -20171,20 +19953,16 @@ static JSValue js_regexp_compile (JSContext *ctx, JSValue this_val, int argc, JS bc = js_compile_regexp (ctx, pattern, flags1); if (JS_IsException (bc)) goto fail; } - JS_FreeValue (ctx, JS_MKPTR (re->pattern)); - JS_FreeValue (ctx, JS_MKPTR (re->bytecode)); + /* No need to free old values - copying GC handles it */ re->pattern = JS_VALUE_GET_STRING (pattern); re->bytecode = JS_VALUE_GET_STRING (bc); { JSValue key = JS_KEY_STR (ctx, "lastIndex"); int ret = JS_SetProperty (ctx, this_val, key, JS_NewInt32 (ctx, 0)); - JS_FreeValue (ctx, key); if (ret < 0) return JS_EXCEPTION; } - return JS_DupValue (ctx, this_val); + return this_val; fail: - JS_FreeValue (ctx, pattern); - JS_FreeValue (ctx, bc); return JS_EXCEPTION; } @@ -20200,13 +19978,11 @@ static JSValue js_regexp_toString (JSContext *ctx, JSValue this_val, int argc, J if (!b) return JS_EXCEPTION; pattern = JS_GetProperty (ctx, this_val, JS_KEY_source); b = pretext_concat_value (ctx, b, pattern); - JS_FreeValue (ctx, pattern); if (!b) return JS_EXCEPTION; b = pretext_putc (ctx, b, '/'); if (!b) return JS_EXCEPTION; flags = JS_GetProperty (ctx, this_val, JS_KEY_flags); b = pretext_concat_value (ctx, b, flags); - JS_FreeValue (ctx, flags); if (!b) return JS_EXCEPTION; return pretext_end (ctx, b); } @@ -20379,8 +20155,7 @@ static JSValue js_regexp_exec (JSContext *ctx, JSValue this_val, int argc, JSVal } if (name) { - if (JS_SetPropertyStr (ctx, groups, name, JS_DupValue (ctx, s)) < 0) { - JS_FreeValue (ctx, s); + if (JS_SetPropertyStr (ctx, groups, name, s) < 0) { goto fail; } } @@ -20416,21 +20191,11 @@ static JSValue js_regexp_exec (JSContext *ctx, JSValue this_val, int argc, JSVal res = JS_NULL; done: - JS_FreeValue (ctx, str_val); - JS_FreeValue (ctx, groups); - JS_FreeValue (ctx, captures_arr); - JS_FreeValue (ctx, match0); - JS_FreeValue (ctx, res); js_free (ctx, capture); js_free (ctx, utf16_buf); return ret; fail: - JS_FreeValue (ctx, str_val); - JS_FreeValue (ctx, groups); - JS_FreeValue (ctx, captures_arr); - JS_FreeValue (ctx, match0); - JS_FreeValue (ctx, res); js_free (ctx, capture); js_free (ctx, utf16_buf); return JS_EXCEPTION; @@ -20533,12 +20298,10 @@ static JSValue JS_RegExpDelete (JSContext *ctx, JSValue this_val, JSValue arg) { } b = pretext_concat (ctx, b, str, next_src_pos, (uint32_t)JSText_len (str)); if (!b) goto fail; - JS_FreeValue (ctx, str_val); js_free (ctx, capture); js_free (ctx, utf16_buf); return pretext_end (ctx, b); fail: - JS_FreeValue (ctx, str_val); js_free (ctx, capture); js_free (ctx, utf16_buf); return JS_EXCEPTION; @@ -20553,13 +20316,11 @@ static JSValue JS_RegExpExec (JSContext *ctx, JSValue r, JSValue s) { ret = JS_CallFree (ctx, method, r, 1, &s); if (JS_IsException (ret)) return ret; if (!JS_IsObject (ret) && !JS_IsNull (ret)) { - JS_FreeValue (ctx, ret); return JS_ThrowTypeError ( ctx, "RegExp exec method must return an object or null"); } return ret; } - JS_FreeValue (ctx, method); return js_regexp_exec (ctx, r, 1, &s); } @@ -20571,13 +20332,11 @@ static int js_is_standard_regexp (JSContext *ctx, JSValue rx) { if (JS_IsException (val)) return -1; // rx.constructor === RegExp res = js_same_value (ctx, val, ctx->regexp_ctor); - JS_FreeValue (ctx, val); if (res) { val = JS_GetProperty (ctx, rx, JS_KEY_exec); if (JS_IsException (val)) return -1; // rx.exec === RE_exec res = JS_IsCFunction (ctx, val, js_regexp_exec, 0); - JS_FreeValue (ctx, val); } return res; } @@ -20600,7 +20359,7 @@ void JS_AddIntrinsicRegExp (JSContext *ctx) { ctx->class_proto[JS_CLASS_REGEXP] = JS_NewObject (ctx); JS_SetPropertyFunctionList (ctx, ctx->class_proto[JS_CLASS_REGEXP], js_regexp_proto_funcs, countof (js_regexp_proto_funcs)); obj = JS_NewCFunction2 (ctx, js_regexp_constructor, "RegExp", 2, JS_CFUNC_generic, 0); - JS_SetPropertyStr (ctx, ctx->global_obj, "RegExp", JS_DupValue (ctx, obj)); + JS_SetPropertyStr (ctx, ctx->global_obj, "RegExp", obj); ctx->regexp_ctor = obj; } @@ -20633,7 +20392,7 @@ static JSValue json_parse_value (JSParseState *s) { prop_name = js_key_from_string (ctx, s->token.u.str.str); if (JS_IsNull (prop_name)) goto fail; } else if (s->ext_json && s->token.val == TOK_IDENT) { - prop_name = JS_DupValue (ctx, s->token.u.ident.str); + prop_name = s->token.u.ident.str; } else { js_parse_error (s, "expecting property name"); goto fail; @@ -20643,11 +20402,9 @@ static JSValue json_parse_value (JSParseState *s) { prop_val = json_parse_value (s); if (JS_IsException (prop_val)) { fail1: - JS_FreeValue (ctx, prop_name); goto fail; } ret = JS_SetPropertyInternal (ctx, val, prop_name, prop_val); - JS_FreeValue (ctx, prop_name); if (ret < 0) goto fail; if (s->token.val != ',') break; @@ -20680,7 +20437,7 @@ static JSValue json_parse_value (JSParseState *s) { if (json_parse_expect (s, ']')) goto fail; } break; case TOK_STRING: - val = JS_DupValue (ctx, s->token.u.str.str); + val = s->token.u.str.str; if (json_next_token (s)) goto fail; break; case TOK_NUMBER: @@ -20716,7 +20473,6 @@ static JSValue json_parse_value (JSParseState *s) { } return val; fail: - JS_FreeValue (ctx, val); return JS_EXCEPTION; } @@ -20734,7 +20490,6 @@ JSValue JS_ParseJSON2 (JSContext *ctx, const char *buf, size_t buf_len, const ch } return val; fail: - JS_FreeValue (ctx, val); free_token (s, &s->token); return JS_EXCEPTION; } @@ -20779,14 +20534,11 @@ static JSValue internalize_json_property (JSContext *ctx, JSValue holder, JSValu } } /* name is already a JSValue, use it directly */ - args[0] = JS_DupValue (ctx, name); + args[0] = name; args[1] = val; res = JS_Call (ctx, reviver, holder, 2, args); - JS_FreeValue (ctx, args[0]); - JS_FreeValue (ctx, val); return res; fail: - JS_FreeValue (ctx, val); return JS_EXCEPTION; } @@ -20805,15 +20557,12 @@ static JSValue js_json_parse (JSContext *ctx, JSValue this_val, int argc, JSValu reviver = argv[1]; root = JS_NewObject (ctx); if (JS_IsException (root)) { - JS_FreeValue (ctx, obj); return JS_EXCEPTION; } if (JS_SetPropertyInternal (ctx, root, JS_KEY_empty, obj) < 0) { - JS_FreeValue (ctx, root); return JS_EXCEPTION; } obj = internalize_json_property (ctx, root, JS_KEY_empty, reviver); - JS_FreeValue (ctx, root); } return obj; } @@ -20830,7 +20579,6 @@ typedef struct JSONStringifyContext { static JSValue JS_ToQuotedStringFree (JSContext *ctx, JSValue val) { JSValue r = JS_ToQuotedString (ctx, val); - JS_FreeValue (ctx, val); return r; } @@ -20845,11 +20593,9 @@ static JSValue js_json_check (JSContext *ctx, JSONStringifyContext *jsc, JSValue if (JS_IsException (f)) goto exception; if (JS_IsFunction (f)) { v = JS_CallFree (ctx, f, val, 1, &key); - JS_FreeValue (ctx, val); val = v; if (JS_IsException (val)) goto exception; } else { - JS_FreeValue (ctx, f); } } @@ -20857,7 +20603,6 @@ static JSValue js_json_check (JSContext *ctx, JSONStringifyContext *jsc, JSValue args[0] = key; args[1] = val; v = JS_Call (ctx, jsc->replacer_func, holder, 2, args); - JS_FreeValue (ctx, val); val = v; if (JS_IsException (val)) goto exception; } @@ -20876,11 +20621,9 @@ static JSValue js_json_check (JSContext *ctx, JSONStringifyContext *jsc, JSValue default: break; } - JS_FreeValue (ctx, val); return JS_NULL; exception: - JS_FreeValue (ctx, val); return JS_EXCEPTION; } @@ -20910,16 +20653,16 @@ static int js_json_to_str (JSContext *ctx, JSONStringifyContext *jsc, JSValue ho JS_ThrowTypeError (ctx, "circular reference"); goto exception; } - indent1 = JS_ConcatString (ctx, JS_DupValue (ctx, indent), JS_DupValue (ctx, jsc->gap)); + indent1 = JS_ConcatString (ctx, indent, jsc->gap); if (JS_IsException (indent1)) goto exception; if (!JS_IsEmptyString (jsc->gap)) { - sep = JS_ConcatString3 (ctx, "\n", JS_DupValue (ctx, indent1), ""); + sep = JS_ConcatString3 (ctx, "\n", indent1, ""); if (JS_IsException (sep)) goto exception; sep1 = js_new_string8 (ctx, " "); if (JS_IsException (sep1)) goto exception; } else { - sep = JS_DupValue (ctx, jsc->empty); - sep1 = JS_DupValue (ctx, jsc->empty); + sep = jsc->empty; + sep1 = jsc->empty; } v = js_cell_push (ctx, jsc->stack, 1, (JSValue *)&val); if (check_exception_free (ctx, v)) goto exception; @@ -20942,7 +20685,6 @@ static int js_json_to_str (JSContext *ctx, JSONStringifyContext *jsc, JSValue ho prop = JS_ToStringFree (ctx, JS_NewInt64 (ctx, i)); if (JS_IsException (prop)) goto exception; v = js_json_check (ctx, jsc, val, v, prop); - JS_FreeValue (ctx, prop); prop = JS_NULL; if (JS_IsException (v)) goto exception; if (JS_IsNull (v)) v = JS_NULL; @@ -20958,7 +20700,7 @@ static int js_json_to_str (JSContext *ctx, JSONStringifyContext *jsc, JSValue ho if (!jsc->b) goto exception; } else { if (!JS_IsNull (jsc->property_list)) - tab = JS_DupValue (ctx, jsc->property_list); + tab = jsc->property_list; else tab = JS_GetOwnPropertyNames (ctx, val); if (JS_IsException (tab)) goto exception; @@ -20967,10 +20709,9 @@ static int js_json_to_str (JSContext *ctx, JSONStringifyContext *jsc, JSValue ho if (!jsc->b) goto exception; has_content = FALSE; for (i = 0; i < len; i++) { - JS_FreeValue (ctx, prop); prop = JS_GetPropertyInt64 (ctx, tab, i); if (JS_IsException (prop)) goto exception; - v = JS_GetPropertyValue (ctx, val, JS_DupValue (ctx, prop)); + v = JS_GetPropertyValue (ctx, val, prop); if (JS_IsException (v)) goto exception; v = js_json_check (ctx, jsc, val, v, prop); if (JS_IsException (v)) goto exception; @@ -20981,7 +20722,6 @@ static int js_json_to_str (JSContext *ctx, JSONStringifyContext *jsc, JSValue ho } prop = JS_ToQuotedStringFree (ctx, prop); if (JS_IsException (prop)) { - JS_FreeValue (ctx, v); goto exception; } jsc->b = pretext_concat_value (ctx, jsc->b, sep); @@ -21007,12 +20747,6 @@ static int js_json_to_str (JSContext *ctx, JSONStringifyContext *jsc, JSValue ho } if (check_exception_free (ctx, js_cell_pop (ctx, jsc->stack, 0, NULL))) goto exception; - JS_FreeValue (ctx, val); - JS_FreeValue (ctx, tab); - JS_FreeValue (ctx, sep); - JS_FreeValue (ctx, sep1); - JS_FreeValue (ctx, indent1); - JS_FreeValue (ctx, prop); return 0; } concat_primitive: @@ -21031,17 +20765,10 @@ concat_primitive: jsc->b = pretext_concat_value_free (ctx, jsc->b, val); return jsc->b ? 0 : -1; default: - JS_FreeValue (ctx, val); return 0; } exception: - JS_FreeValue (ctx, val); - JS_FreeValue (ctx, tab); - JS_FreeValue (ctx, sep); - JS_FreeValue (ctx, sep1); - JS_FreeValue (ctx, indent1); - JS_FreeValue (ctx, prop); return -1; } @@ -21081,30 +20808,26 @@ JSValue JS_JSONStringify (JSContext *ctx, JSValue obj, JSValue replacer, JSValue if (JS_IsException (v)) goto exception; if (JS_IsObject (v)) { /* Objects are not valid property list items */ - JS_FreeValue (ctx, v); continue; } else if (JS_IsNumber (v)) { v = JS_ToStringFree (ctx, v); if (JS_IsException (v)) goto exception; } else if (!JS_IsText (v)) { - JS_FreeValue (ctx, v); continue; } present = js_array_includes (ctx, jsc->property_list, 1, (JSValue *)&v); if (JS_IsException (present)) { - JS_FreeValue (ctx, v); goto exception; } if (!JS_ToBoolFree (ctx, present)) { JS_SetPropertyInt64 (ctx, jsc->property_list, j++, v); } else { - JS_FreeValue (ctx, v); } } } } - space = JS_DupValue (ctx, space0); + space = space0; if (JS_IsNumber (space)) { int n; if (JS_ToInt32Clamp (ctx, &n, space, 0, 10, 0)) goto exception; @@ -21113,16 +20836,15 @@ JSValue JS_JSONStringify (JSContext *ctx, JSValue obj, JSValue replacer, JSValue JSText *p = JS_VALUE_GET_STRING (space); jsc->gap = js_sub_string (ctx, p, 0, min_int ((int)JSText_len (p), 10)); } else { - jsc->gap = JS_DupValue (ctx, jsc->empty); + jsc->gap = jsc->empty; } - JS_FreeValue (ctx, space); if (JS_IsException (jsc->gap)) goto exception; wrapper = JS_NewObject (ctx); if (JS_IsException (wrapper)) goto exception; - if (JS_SetPropertyInternal (ctx, wrapper, JS_KEY_empty, JS_DupValue (ctx, obj)) + if (JS_SetPropertyInternal (ctx, wrapper, JS_KEY_empty, obj) < 0) goto exception; - val = JS_DupValue (ctx, obj); + val = obj; val = js_json_check (ctx, jsc, wrapper, val, jsc->empty); if (JS_IsException (val)) goto exception; @@ -21140,11 +20862,6 @@ exception: ret = JS_EXCEPTION; done1: done: - JS_FreeValue (ctx, wrapper); - JS_FreeValue (ctx, jsc->empty); - JS_FreeValue (ctx, jsc->gap); - JS_FreeValue (ctx, jsc->property_list); - JS_FreeValue (ctx, jsc->stack); return ret; } @@ -21201,7 +20918,7 @@ static JSValue js_cell_number (JSContext *ctx, JSValue this_val, int argc, JSVal /* Handle number - return as-is */ if (tag == JS_TAG_INT || tag == JS_TAG_FLOAT64) { - return JS_DupValue (ctx, val); + return val; } /* Handle string */ @@ -21921,7 +21638,6 @@ static JSValue js_cell_text (JSContext *ctx, JSValue this_val, int argc, JSValue int from, to; if (JS_ToInt32 (ctx, &from, argv[1])) { - JS_FreeValue (ctx, str); return JS_EXCEPTION; } @@ -21932,7 +21648,6 @@ static JSValue js_cell_text (JSContext *ctx, JSValue this_val, int argc, JSValue to = len; if (argc >= 3) { if (JS_ToInt32 (ctx, &to, argv[2])) { - JS_FreeValue (ctx, str); return JS_EXCEPTION; } if (to < 0) to += len; @@ -21941,12 +21656,10 @@ static JSValue js_cell_text (JSContext *ctx, JSValue this_val, int argc, JSValue } if (from > to) { - JS_FreeValue (ctx, str); return JS_NULL; } JSValue sub = js_sub_string (ctx, p, from, to); - JS_FreeValue (ctx, str); return sub; } } @@ -22097,13 +21810,11 @@ static JSValue js_cell_text (JSContext *ctx, JSValue this_val, int argc, JSValue if (JS_IsException (item)) goto array_fail; if (!JS_VALUE_IS_TEXT (item)) { - JS_FreeValue (ctx, item); if (sep_alloc) JS_FreeCString (ctx, separator); return JS_ThrowTypeError (ctx, "text: array element is not a string"); } JSValue item_str = JS_ToString (ctx, item); - JS_FreeValue (ctx, item); if (JS_IsException (item_str)) goto array_fail; b = pretext_concat_value_free (ctx, b, item_str); @@ -22297,7 +22008,6 @@ static JSValue js_cell_text_codepoint (JSContext *ctx, JSValue this_val, int arg static JSText *pt_concat_value_to_string_free (JSContext *ctx, JSText *b, JSValue v) { JSValue s = JS_ToString (ctx, v); - JS_FreeValue (ctx, v); if (JS_IsException (s)) return NULL; b = pretext_concat_value_free (ctx, b, s); return b; @@ -22320,14 +22030,11 @@ static JSValue make_replacement (JSContext *ctx, int argc, JSValue *argv, int fo args[0] = match_val; args[1] = JS_NewInt32 (ctx, found); rep = JS_Call (ctx, argv[2], JS_NULL, 2, args); - JS_FreeValue (ctx, args[0]); - JS_FreeValue (ctx, args[1]); return rep; } - JS_FreeValue (ctx, match_val); - if (argc > 2) return JS_DupValue (ctx, argv[2]); + if (argc > 2) return argv[2]; return JS_KEY_empty; } @@ -22338,7 +22045,6 @@ static int JS_IsRegExp (JSContext *ctx, JSValue v) { if (JS_IsException (exec)) return -1; int ok = JS_IsFunction (exec); - JS_FreeValue (ctx, exec); return ok; } @@ -22417,7 +22123,6 @@ static JSValue js_cell_text_replace (JSContext *ctx, JSValue this_val, int argc, b = pt_concat_value_to_string_free (ctx, b, rep); if (!b) goto fail_str_target; } else { - JS_FreeValue (ctx, rep); } if (boundary < len) { @@ -22464,7 +22169,6 @@ static JSValue js_cell_text_replace (JSContext *ctx, JSValue this_val, int argc, b = pt_concat_value_to_string_free (ctx, b, rep); if (!b) goto fail_str_target; } else { - JS_FreeValue (ctx, rep); } pos = found + t_len; @@ -22501,58 +22205,43 @@ static JSValue js_cell_text_replace (JSContext *ctx, JSValue this_val, int argc, JSValue exec_res = JS_Invoke (ctx, rx, JS_KEY_exec, 1, (JSValue *)&sub_str); - JS_FreeValue (ctx, sub_str); if (JS_IsException (exec_res)) goto fail_rx; if (JS_IsNull (exec_res)) { - JS_FreeValue (ctx, exec_res); break; } JSValue idx_val = JS_GetPropertyStr (ctx, exec_res, "index"); if (JS_IsException (idx_val)) { - JS_FreeValue (ctx, exec_res); goto fail_rx; } int32_t local_index = 0; if (JS_ToInt32 (ctx, &local_index, idx_val)) { - JS_FreeValue (ctx, idx_val); - JS_FreeValue (ctx, exec_res); goto fail_rx; } - JS_FreeValue (ctx, idx_val); if (local_index < 0) local_index = 0; int found = pos + local_index; if (found < pos) found = pos; if (found > len) { - JS_FreeValue (ctx, exec_res); break; } JSValue match = JS_GetPropertyStr (ctx, exec_res, "match"); if (JS_IsException (match)) { - JS_FreeValue (ctx, exec_res); goto fail_rx; } JSValue end_val = JS_GetPropertyStr (ctx, exec_res, "end"); if (JS_IsException (end_val)) { - JS_FreeValue (ctx, match); - JS_FreeValue (ctx, exec_res); goto fail_rx; } int32_t end = 0; if (JS_ToInt32 (ctx, &end, end_val)) { - JS_FreeValue (ctx, end_val); - JS_FreeValue (ctx, match); - JS_FreeValue (ctx, exec_res); goto fail_rx; } - JS_FreeValue (ctx, end_val); - JS_FreeValue (ctx, exec_res); int match_len = end - local_index; if (match_len < 0) match_len = 0; @@ -22573,7 +22262,6 @@ static JSValue js_cell_text_replace (JSContext *ctx, JSValue this_val, int argc, b = pt_concat_value_to_string_free (ctx, b, rep); if (!b) goto fail_rx; } else { - JS_FreeValue (ctx, rep); } pos = found + match_len; @@ -22601,7 +22289,6 @@ fail_rx: if (!JS_IsNull (orig_last_index) && !JS_IsException (orig_last_index)) { JS_SetPropertyStr (ctx, rx, "lastIndex", orig_last_index); } else { - JS_FreeValue (ctx, orig_last_index); } return JS_EXCEPTION; } @@ -22664,7 +22351,6 @@ static JSValue js_cell_text_search (JSContext *ctx, JSValue this_val, int argc, } /* str removed - arg not owned */ - JS_FreeValue (ctx, target); if (result == -1) return JS_NULL; return JS_NewInt32 (ctx, result); @@ -22686,11 +22372,9 @@ static JSValue js_cell_text_search (JSContext *ctx, JSValue this_val, int argc, if (JS_IsException (sub_str)) goto fail_rx_search; JSValue exec_res = JS_Invoke (ctx, rx, JS_KEY_exec, 1, (JSValue *)&sub_str); - JS_FreeValue (ctx, sub_str); if (JS_IsException (exec_res)) goto fail_rx_search; if (JS_IsNull (exec_res)) { - JS_FreeValue (ctx, exec_res); if (have_orig_last_index) JS_SetPropertyStr (ctx, rx, "lastIndex", orig_last_index); /* str removed - arg not owned */ @@ -22699,18 +22383,13 @@ static JSValue js_cell_text_search (JSContext *ctx, JSValue this_val, int argc, JSValue idx_val = JS_GetPropertyStr (ctx, exec_res, "index"); if (JS_IsException (idx_val)) { - JS_FreeValue (ctx, exec_res); goto fail_rx_search; } int32_t local_index = 0; if (JS_ToInt32 (ctx, &local_index, idx_val)) { - JS_FreeValue (ctx, idx_val); - JS_FreeValue (ctx, exec_res); goto fail_rx_search; } - JS_FreeValue (ctx, idx_val); - JS_FreeValue (ctx, exec_res); if (local_index < 0) local_index = 0; @@ -22724,7 +22403,6 @@ fail_rx_search: if (!JS_IsNull (orig_last_index) && !JS_IsException (orig_last_index)) { JS_SetPropertyStr (ctx, rx, "lastIndex", orig_last_index); } else { - JS_FreeValue (ctx, orig_last_index); } /* str removed - arg not owned */ return JS_EXCEPTION; @@ -22803,7 +22481,7 @@ static JSValue js_cell_text_extract (JSContext *ctx, JSValue this_val, int argc, JSValue sub_str; if (from == 0 && to == len) { - sub_str = JS_DupValue (ctx, str); + sub_str = str; } else { sub_str = js_sub_string (ctx, p, from, to); if (JS_IsException (sub_str)) goto fail_rx; @@ -22811,11 +22489,9 @@ static JSValue js_cell_text_extract (JSContext *ctx, JSValue this_val, int argc, JSValue exec_res = JS_Invoke (ctx, rx, JS_KEY_exec, 1, (JSValue *)&sub_str); - JS_FreeValue (ctx, sub_str); if (JS_IsException (exec_res)) goto fail_rx; if (JS_IsNull (exec_res)) { - JS_FreeValue (ctx, exec_res); JS_SetPropertyStr (ctx, rx, "lastIndex", orig_last_index); return JS_NULL; } @@ -22823,20 +22499,15 @@ static JSValue js_cell_text_extract (JSContext *ctx, JSValue this_val, int argc, /* Build result array */ JSValue out = JS_NewArray (ctx); if (JS_IsException (out)) { - JS_FreeValue (ctx, exec_res); goto fail_rx; } /* out[0] = exec_res.match */ JSValue match0 = JS_GetPropertyStr (ctx, exec_res, "match"); if (JS_IsException (match0)) { - JS_FreeValue (ctx, exec_res); - JS_FreeValue (ctx, out); goto fail_rx; } if (JS_SetPropertyUint32 (ctx, out, 0, match0) < 0) { - JS_FreeValue (ctx, exec_res); - JS_FreeValue (ctx, out); goto fail_rx; } @@ -22853,23 +22524,15 @@ static JSValue js_cell_text_extract (JSContext *ctx, JSValue this_val, int argc, for (int64_t i = 0; i < caps_len; i++) { JSValue cap = JS_GetPropertyInt64 (ctx, caps, i); if (JS_IsException (cap)) { - JS_FreeValue (ctx, caps); - JS_FreeValue (ctx, exec_res); - JS_FreeValue (ctx, out); goto fail_rx; } if (JS_SetPropertyInt64 (ctx, out, i + 1, cap) < 0) { - JS_FreeValue (ctx, caps); - JS_FreeValue (ctx, exec_res); - JS_FreeValue (ctx, out); goto fail_rx; } } } } - JS_FreeValue (ctx, caps); - JS_FreeValue (ctx, exec_res); JS_SetPropertyStr (ctx, rx, "lastIndex", orig_last_index); return out; @@ -22877,7 +22540,6 @@ static JSValue js_cell_text_extract (JSContext *ctx, JSValue this_val, int argc, if (!JS_IsNull (orig_last_index) && !JS_IsException (orig_last_index)) { JS_SetPropertyStr (ctx, rx, "lastIndex", orig_last_index); } else { - JS_FreeValue (ctx, orig_last_index); } return JS_EXCEPTION; } @@ -22890,7 +22552,6 @@ static JSValue js_cell_text_extract (JSContext *ctx, JSValue this_val, int argc, int needle_len = (int)JSText_len (needle); int pos = js_str_find_range (p, from, to, needle); - JS_FreeValue (ctx, needle_val); if (pos < 0) return JS_NULL; @@ -22899,12 +22560,10 @@ static JSValue js_cell_text_extract (JSContext *ctx, JSValue this_val, int argc, JSValue match = js_sub_string (ctx, p, pos, pos + needle_len); if (JS_IsException (match)) { - JS_FreeValue (ctx, arr); return JS_EXCEPTION; } if (JS_SetPropertyUint32 (ctx, arr, 0, match) < 0) { - JS_FreeValue (ctx, arr); return JS_EXCEPTION; } @@ -22945,9 +22604,7 @@ static JSValue js_cell_array (JSContext *ctx, JSValue this_val, int argc, JSValu for (int i = 0; i < len; i++) { JSValue idx_arg = JS_NewInt32 (ctx, i); JSValue val = JS_CallInternal (ctx, func, JS_NULL, 1, &idx_arg, 0); - JS_FreeValue (ctx, idx_arg); if (JS_IsException (val)) { - JS_FreeValue (ctx, result); return JS_EXCEPTION; } out->values[i] = val; @@ -22956,7 +22613,6 @@ static JSValue js_cell_array (JSContext *ctx, JSValue this_val, int argc, JSValu for (int i = 0; i < len; i++) { JSValue val = JS_CallInternal (ctx, func, JS_NULL, 0, NULL, 0); if (JS_IsException (val)) { - JS_FreeValue (ctx, result); return JS_EXCEPTION; } out->values[i] = val; @@ -22965,7 +22621,7 @@ static JSValue js_cell_array (JSContext *ctx, JSValue this_val, int argc, JSValu } else if (argc > 1) { /* Fill with value */ for (int i = 0; i < len; i++) - out->values[i] = JS_DupValue (ctx, argv[1]); + out->values[i] = argv[1]; } return result; } @@ -22984,7 +22640,7 @@ static JSValue js_cell_array (JSContext *ctx, JSValue this_val, int argc, JSValu if (JS_IsException (result)) return result; JSArray *out = JS_VALUE_GET_ARRAY (result); for (int i = 0; i < len; i++) { - out->values[i] = JS_DupValue (ctx, arr->values[i]); + out->values[i] = arr->values[i]; } out->len = len; return result; @@ -23006,16 +22662,12 @@ static JSValue js_cell_array (JSContext *ctx, JSValue this_val, int argc, JSValu if (reverse) { for (int i = len - 1; i >= 0; i--) { JSValue args[2] - = { JS_DupValue (ctx, arr->values[i]), JS_NewInt32 (ctx, i) }; + = { arr->values[i], JS_NewInt32 (ctx, i) }; JSValue val = JS_CallInternal (ctx, func, JS_NULL, 2, args, 0); - JS_FreeValue (ctx, args[0]); - JS_FreeValue (ctx, args[1]); if (JS_IsException (val)) { - JS_FreeValue (ctx, result); return JS_EXCEPTION; } if (!JS_IsNull (exit_val) && js_strict_eq (ctx, val, exit_val)) { - JS_FreeValue (ctx, val); break; } js_intrinsic_array_push (ctx, out, val); @@ -23023,16 +22675,12 @@ static JSValue js_cell_array (JSContext *ctx, JSValue this_val, int argc, JSValu } else { for (int i = 0; i < len; i++) { JSValue args[2] - = { JS_DupValue (ctx, arr->values[i]), JS_NewInt32 (ctx, i) }; + = { arr->values[i], JS_NewInt32 (ctx, i) }; JSValue val = JS_CallInternal (ctx, func, JS_NULL, 2, args, 0); - JS_FreeValue (ctx, args[0]); - JS_FreeValue (ctx, args[1]); if (JS_IsException (val)) { - JS_FreeValue (ctx, result); return JS_EXCEPTION; } if (!JS_IsNull (exit_val) && js_strict_eq (ctx, val, exit_val)) { - JS_FreeValue (ctx, val); break; } js_intrinsic_array_push (ctx, out, val); @@ -23041,30 +22689,24 @@ static JSValue js_cell_array (JSContext *ctx, JSValue this_val, int argc, JSValu } else { if (reverse) { for (int i = len - 1; i >= 0; i--) { - JSValue item = JS_DupValue (ctx, arr->values[i]); + JSValue item = arr->values[i]; JSValue val = JS_CallInternal (ctx, func, JS_NULL, 1, &item, 0); - JS_FreeValue (ctx, item); if (JS_IsException (val)) { - JS_FreeValue (ctx, result); return JS_EXCEPTION; } if (!JS_IsNull (exit_val) && js_strict_eq (ctx, val, exit_val)) { - JS_FreeValue (ctx, val); break; } js_intrinsic_array_push (ctx, out, val); } } else { for (int i = 0; i < len; i++) { - JSValue item = JS_DupValue (ctx, arr->values[i]); + JSValue item = arr->values[i]; JSValue val = JS_CallInternal (ctx, func, JS_NULL, 1, &item, 0); - JS_FreeValue (ctx, item); if (JS_IsException (val)) { - JS_FreeValue (ctx, result); return JS_EXCEPTION; } if (!JS_IsNull (exit_val) && js_strict_eq (ctx, val, exit_val)) { - JS_FreeValue (ctx, val); break; } js_intrinsic_array_push (ctx, out, val); @@ -23084,10 +22726,10 @@ static JSValue js_cell_array (JSContext *ctx, JSValue this_val, int argc, JSValu JSArray *out = JS_VALUE_GET_ARRAY (result); for (int i = 0; i < len; i++) { - out->values[i] = JS_DupValue (ctx, arr->values[i]); + out->values[i] = arr->values[i]; } for (int i = 0; i < len2; i++) { - out->values[len + i] = JS_DupValue (ctx, arr2->values[i]); + out->values[len + i] = arr2->values[i]; } out->len = len + len2; return result; @@ -23116,7 +22758,7 @@ static JSValue js_cell_array (JSContext *ctx, JSValue this_val, int argc, JSValu JSArray *out = JS_VALUE_GET_ARRAY (result); for (int i = 0; i < slice_len; i++) { - out->values[i] = JS_DupValue (ctx, arr->values[from + i]); + out->values[i] = arr->values[from + i]; } out->len = slice_len; return result; @@ -23146,7 +22788,6 @@ static JSValue js_cell_array (JSContext *ctx, JSValue this_val, int argc, JSValu for (int i = 0; i < len; i++) { JSValue ch = js_sub_string (ctx, p, i, i + 1); if (JS_IsException (ch)) { - JS_FreeValue (ctx, result); return JS_EXCEPTION; } out->values[i] = ch; @@ -23224,7 +22865,6 @@ static JSValue js_cell_array (JSContext *ctx, JSValue this_val, int argc, JSValu /* Save & restore lastIndex to avoid mutating caller-visible state */ JSValue orig_last_index = JS_GetPropertyStr (ctx, rx, "lastIndex"); if (JS_IsException (orig_last_index)) { - JS_FreeValue (ctx, result); return JS_EXCEPTION; } @@ -23242,7 +22882,6 @@ static JSValue js_cell_array (JSContext *ctx, JSValue this_val, int argc, JSValu JSValue exec_res = JS_Invoke (ctx, rx, JS_KEY_exec, 1, (JSValue *)&sub_str); - JS_FreeValue (ctx, sub_str); if (JS_IsException (exec_res)) goto fail_rx_split; if (JS_IsNull (exec_res)) { @@ -23256,17 +22895,13 @@ static JSValue js_cell_array (JSContext *ctx, JSValue this_val, int argc, JSValu /* local match index within sub_str */ JSValue idx_val = JS_GetPropertyStr (ctx, exec_res, "index"); if (JS_IsException (idx_val)) { - JS_FreeValue (ctx, exec_res); goto fail_rx_split; } int32_t local_index = 0; if (JS_ToInt32 (ctx, &local_index, idx_val)) { - JS_FreeValue (ctx, idx_val); - JS_FreeValue (ctx, exec_res); goto fail_rx_split; } - JS_FreeValue (ctx, idx_val); if (local_index < 0) local_index = 0; @@ -23274,7 +22909,6 @@ static JSValue js_cell_array (JSContext *ctx, JSValue this_val, int argc, JSValu if (found < pos) found = pos; if (found > len) { /* treat as no more matches */ - JS_FreeValue (ctx, exec_res); JSValue tail = js_sub_string (ctx, p, pos, len); if (JS_IsException (tail)) goto fail_rx_split; JS_SetPropertyInt64 (ctx, result, out_idx++, tail); @@ -23283,18 +22917,13 @@ static JSValue js_cell_array (JSContext *ctx, JSValue this_val, int argc, JSValu JSValue end_val = JS_GetPropertyStr (ctx, exec_res, "end"); if (JS_IsException (end_val)) { - JS_FreeValue (ctx, exec_res); goto fail_rx_split; } int32_t end = 0; if (JS_ToInt32 (ctx, &end, end_val)) { - JS_FreeValue (ctx, end_val); - JS_FreeValue (ctx, exec_res); goto fail_rx_split; } - JS_FreeValue (ctx, end_val); - JS_FreeValue (ctx, exec_res); int match_len = end - local_index; if (match_len < 0) match_len = 0; @@ -23329,9 +22958,7 @@ static JSValue js_cell_array (JSContext *ctx, JSValue this_val, int argc, JSValu if (!JS_IsException (orig_last_index)) { JS_SetPropertyStr (ctx, rx, "lastIndex", orig_last_index); } else { - JS_FreeValue (ctx, orig_last_index); } - JS_FreeValue (ctx, result); /* str removed - arg not owned */ return JS_EXCEPTION; } @@ -23362,7 +22989,6 @@ static JSValue js_cell_array (JSContext *ctx, JSValue this_val, int argc, JSValu JSValue chunk = js_sub_string (ctx, p, i, end); if (JS_IsException (chunk)) { /* str removed - arg not owned */ - JS_FreeValue (ctx, result); return JS_EXCEPTION; } JS_SetPropertyInt64 (ctx, result, idx++, chunk); @@ -23394,48 +23020,40 @@ static JSValue js_cell_array_reduce (JSContext *ctx, JSValue this_val, int argc, if (argc < 3 || JS_IsNull (argv[2])) { if (len == 0) return JS_NULL; - if (len == 1) return JS_DupValue (ctx, arr->values[0]); + if (len == 1) return arr->values[0]; if (reverse) { - acc = JS_DupValue (ctx, arr->values[len - 1]); + acc = arr->values[len - 1]; for (int i = len - 2; i >= 0; i--) { - JSValue args[2] = { acc, JS_DupValue (ctx, arr->values[i]) }; + JSValue args[2] = { acc, arr->values[i] }; JSValue new_acc = JS_CallInternal (ctx, func, JS_NULL, 2, args, 0); - JS_FreeValue (ctx, args[0]); - JS_FreeValue (ctx, args[1]); if (JS_IsException (new_acc)) return JS_EXCEPTION; acc = new_acc; } } else { - acc = JS_DupValue (ctx, arr->values[0]); + acc = arr->values[0]; for (int i = 1; i < len; i++) { - JSValue args[2] = { acc, JS_DupValue (ctx, arr->values[i]) }; + JSValue args[2] = { acc, arr->values[i] }; JSValue new_acc = JS_CallInternal (ctx, func, JS_NULL, 2, args, 0); - JS_FreeValue (ctx, args[0]); - JS_FreeValue (ctx, args[1]); if (JS_IsException (new_acc)) return JS_EXCEPTION; acc = new_acc; } } } else { - if (len == 0) return JS_DupValue (ctx, argv[2]); - acc = JS_DupValue (ctx, argv[2]); + if (len == 0) return argv[2]; + acc = argv[2]; if (reverse) { for (int i = len - 1; i >= 0; i--) { - JSValue args[2] = { acc, JS_DupValue (ctx, arr->values[i]) }; + JSValue args[2] = { acc, arr->values[i] }; JSValue new_acc = JS_CallInternal (ctx, func, JS_NULL, 2, args, 0); - JS_FreeValue (ctx, args[0]); - JS_FreeValue (ctx, args[1]); if (JS_IsException (new_acc)) return JS_EXCEPTION; acc = new_acc; } } else { for (int i = 0; i < len; i++) { - JSValue args[2] = { acc, JS_DupValue (ctx, arr->values[i]) }; + JSValue args[2] = { acc, arr->values[i] }; JSValue new_acc = JS_CallInternal (ctx, func, JS_NULL, 2, args, 0); - JS_FreeValue (ctx, args[0]); - JS_FreeValue (ctx, args[1]); if (JS_IsException (new_acc)) return JS_EXCEPTION; acc = new_acc; } @@ -23467,45 +23085,37 @@ static JSValue js_cell_array_for (JSContext *ctx, JSValue this_val, int argc, JS for (int i = len - 1; i >= 0; i--) { JSValue result; if (arity == 1) { - JSValue item = JS_DupValue (ctx, arr->values[i]); + JSValue item = arr->values[i]; result = JS_CallInternal (ctx, func, JS_NULL, 1, &item, 0); - JS_FreeValue (ctx, item); } else { JSValue args[2]; - args[0] = JS_DupValue (ctx, arr->values[i]); + args[0] = arr->values[i]; args[1] = JS_NewInt32 (ctx, i); result = JS_CallInternal (ctx, func, JS_NULL, 2, args, 0); - JS_FreeValue (ctx, args[0]); - JS_FreeValue (ctx, args[1]); } if (JS_IsException (result)) return JS_EXCEPTION; if (!JS_IsNull (exit_val) && js_strict_eq (ctx, result, exit_val)) { return result; } - JS_FreeValue (ctx, result); } } else { for (int i = 0; i < len; i++) { JSValue result; if (arity == 1) { - JSValue item = JS_DupValue (ctx, arr->values[i]); + JSValue item = arr->values[i]; result = JS_CallInternal (ctx, func, JS_NULL, 1, &item, 0); - JS_FreeValue (ctx, item); } else { JSValue args[2]; - args[0] = JS_DupValue (ctx, arr->values[i]); + args[0] = arr->values[i]; args[1] = JS_NewInt32 (ctx, i); result = JS_CallInternal (ctx, func, JS_NULL, 2, args, 0); - JS_FreeValue (ctx, args[0]); - JS_FreeValue (ctx, args[1]); } if (JS_IsException (result)) return JS_EXCEPTION; if (!JS_IsNull (exit_val) && js_strict_eq (ctx, result, exit_val)) { return result; } - JS_FreeValue (ctx, result); } } @@ -23553,56 +23163,42 @@ static JSValue js_cell_array_find (JSContext *ctx, JSValue this_val, int argc, J if (reverse) { for (int i = from; i >= 0; i--) { JSValue args[2] - = { JS_DupValue (ctx, arr->values[i]), JS_NewInt32 (ctx, i) }; + = { arr->values[i], JS_NewInt32 (ctx, i) }; JSValue result = JS_CallInternal (ctx, func, JS_NULL, 2, args, 0); - JS_FreeValue (ctx, args[0]); - JS_FreeValue (ctx, args[1]); if (JS_IsException (result)) return JS_EXCEPTION; if (JS_ToBool (ctx, result)) { - JS_FreeValue (ctx, result); return JS_NewInt32 (ctx, i); } - JS_FreeValue (ctx, result); } } else { for (int i = from; i < len; i++) { JSValue args[2] - = { JS_DupValue (ctx, arr->values[i]), JS_NewInt32 (ctx, i) }; + = { arr->values[i], JS_NewInt32 (ctx, i) }; JSValue result = JS_CallInternal (ctx, func, JS_NULL, 2, args, 0); - JS_FreeValue (ctx, args[0]); - JS_FreeValue (ctx, args[1]); if (JS_IsException (result)) return JS_EXCEPTION; if (JS_ToBool (ctx, result)) { - JS_FreeValue (ctx, result); return JS_NewInt32 (ctx, i); } - JS_FreeValue (ctx, result); } } } else { if (reverse) { for (int i = from; i >= 0; i--) { - JSValue item = JS_DupValue (ctx, arr->values[i]); + JSValue item = arr->values[i]; JSValue result = JS_CallInternal (ctx, func, JS_NULL, 1, &item, 0); - JS_FreeValue (ctx, item); if (JS_IsException (result)) return JS_EXCEPTION; if (JS_ToBool (ctx, result)) { - JS_FreeValue (ctx, result); return JS_NewInt32 (ctx, i); } - JS_FreeValue (ctx, result); } } else { for (int i = from; i < len; i++) { - JSValue item = JS_DupValue (ctx, arr->values[i]); + JSValue item = arr->values[i]; JSValue result = JS_CallInternal (ctx, func, JS_NULL, 1, &item, 0); - JS_FreeValue (ctx, item); if (JS_IsException (result)) return JS_EXCEPTION; if (JS_ToBool (ctx, result)) { - JS_FreeValue (ctx, result); return JS_NewInt32 (ctx, i); } - JS_FreeValue (ctx, result); } } } @@ -23629,68 +23225,49 @@ static JSValue js_cell_array_filter (JSContext *ctx, JSValue this_val, int argc, if (arity >= 2) { for (int i = 0; i < len; i++) { JSValue args[2] - = { JS_DupValue (ctx, arr->values[i]), JS_NewInt32 (ctx, i) }; + = { arr->values[i], JS_NewInt32 (ctx, i) }; JSValue val = JS_CallInternal (ctx, func, JS_NULL, 2, args, 0); - JS_FreeValue (ctx, args[1]); if (JS_IsException (val)) { - JS_FreeValue (ctx, args[0]); - JS_FreeValue (ctx, result); return JS_EXCEPTION; } if (JS_VALUE_GET_TAG (val) == JS_TAG_BOOL) { if (JS_VALUE_GET_BOOL (val)) { js_intrinsic_array_push (ctx, out, args[0]); } else { - JS_FreeValue (ctx, args[0]); } } else { - JS_FreeValue (ctx, args[0]); - JS_FreeValue (ctx, val); - JS_FreeValue (ctx, result); return JS_NULL; } - JS_FreeValue (ctx, val); } } else if (arity == 1) { for (int i = 0; i < len; i++) { - JSValue item = JS_DupValue (ctx, arr->values[i]); + JSValue item = arr->values[i]; JSValue val = JS_CallInternal (ctx, func, JS_NULL, 1, &item, 0); if (JS_IsException (val)) { - JS_FreeValue (ctx, item); - JS_FreeValue (ctx, result); return JS_EXCEPTION; } if (JS_VALUE_GET_TAG (val) == JS_TAG_BOOL) { if (JS_VALUE_GET_BOOL (val)) { js_intrinsic_array_push (ctx, out, item); } else { - JS_FreeValue (ctx, item); } } else { - JS_FreeValue (ctx, item); - JS_FreeValue (ctx, val); - JS_FreeValue (ctx, result); return JS_NULL; } - JS_FreeValue (ctx, val); } } else { for (int i = 0; i < len; i++) { JSValue val = JS_CallInternal (ctx, func, JS_NULL, 0, NULL, 0); if (JS_IsException (val)) { - JS_FreeValue (ctx, result); return JS_EXCEPTION; } if (JS_VALUE_GET_TAG (val) == JS_TAG_BOOL) { if (JS_VALUE_GET_BOOL (val)) { - js_intrinsic_array_push (ctx, out, JS_DupValue (ctx, arr->values[i])); + js_intrinsic_array_push (ctx, out, arr->values[i]); } } else { - JS_FreeValue (ctx, val); - JS_FreeValue (ctx, result); return JS_NULL; } - JS_FreeValue (ctx, val); } } @@ -23720,7 +23297,6 @@ static JSValue js_cell_array_sort (JSContext *ctx, JSValue this_val, int argc, J if (!items || !keys) { if (items) js_free (ctx, items); if (keys) js_free (ctx, keys); - JS_FreeValue (ctx, result); return JS_EXCEPTION; } @@ -23731,11 +23307,11 @@ static JSValue js_cell_array_sort (JSContext *ctx, JSValue this_val, int argc, J /* Extract keys */ for (int i = 0; i < len; i++) { - items[i] = JS_DupValue (ctx, arr->values[i]); + items[i] = arr->values[i]; JSValue key; if (argc < 2 || JS_IsNull (argv[1])) { - key = JS_DupValue (ctx, items[i]); + key = items[i]; } else if (JS_VALUE_GET_TAG (argv[1]) == JS_TAG_INT) { /* Numeric index - use for nested arrays */ int32_t idx; @@ -23743,7 +23319,7 @@ static JSValue js_cell_array_sort (JSContext *ctx, JSValue this_val, int argc, J if (JS_IsArray (items[i])) { JSArray *nested = JS_VALUE_GET_ARRAY (items[i]); if (idx >= 0 && idx < (int)nested->len) - key = JS_DupValue (ctx, nested->values[idx]); + key = nested->values[idx]; else key = JS_NULL; } else { @@ -23755,19 +23331,17 @@ static JSValue js_cell_array_sort (JSContext *ctx, JSValue this_val, int argc, J key = JS_GetProperty (ctx, items[i], prop_key); } else if (key_arr) { if (i < (int)key_arr->len) - key = JS_DupValue (ctx, key_arr->values[i]); + key = key_arr->values[i]; else key = JS_NULL; } else { - key = JS_DupValue (ctx, items[i]); + key = items[i]; } if (JS_IsException (key)) { for (int j = 0; j <= i; j++) - JS_FreeValue (ctx, items[j]); js_free (ctx, items); js_free (ctx, keys); - JS_FreeValue (ctx, result); return JS_EXCEPTION; } @@ -23780,20 +23354,15 @@ static JSValue js_cell_array_sort (JSContext *ctx, JSValue this_val, int argc, J is_string = 1; str_keys = js_malloc (ctx, sizeof (char *) * len); if (!str_keys) { - JS_FreeValue (ctx, key); for (int j = 0; j <= i; j++) - JS_FreeValue (ctx, items[j]); js_free (ctx, items); js_free (ctx, keys); - JS_FreeValue (ctx, result); return JS_EXCEPTION; } } if (is_string) { str_keys[i] = (char *)JS_ToCString (ctx, key); } } else { - JS_FreeValue (ctx, key); for (int j = 0; j <= i; j++) - JS_FreeValue (ctx, items[j]); js_free (ctx, items); js_free (ctx, keys); if (str_keys) { @@ -23801,17 +23370,14 @@ static JSValue js_cell_array_sort (JSContext *ctx, JSValue this_val, int argc, J JS_FreeCString (ctx, str_keys[j]); js_free (ctx, str_keys); } - JS_FreeValue (ctx, result); return JS_NULL; } - JS_FreeValue (ctx, key); } /* Create index array */ int *indices = js_malloc (ctx, sizeof (int) * len); if (!indices) { for (int j = 0; j < len; j++) - JS_FreeValue (ctx, items[j]); js_free (ctx, items); js_free (ctx, keys); if (str_keys) { @@ -23819,7 +23385,6 @@ static JSValue js_cell_array_sort (JSContext *ctx, JSValue this_val, int argc, J JS_FreeCString (ctx, str_keys[j]); js_free (ctx, str_keys); } - JS_FreeValue (ctx, result); return JS_EXCEPTION; } for (int i = 0; i < len; i++) @@ -23846,13 +23411,12 @@ static JSValue js_cell_array_sort (JSContext *ctx, JSValue this_val, int argc, J /* Build sorted array directly into output */ for (int i = 0; i < len; i++) { - out->values[i] = JS_DupValue (ctx, items[indices[i]]); + out->values[i] = items[indices[i]]; } out->len = len; /* Cleanup */ for (int i = 0; i < len; i++) - JS_FreeValue (ctx, items[i]); js_free (ctx, items); js_free (ctx, keys); js_free (ctx, indices); @@ -23885,13 +23449,10 @@ static JSValue js_cell_object (JSContext *ctx, JSValue this_val, int argc, JSVal JSValue keys = JS_GetOwnPropertyNames (ctx, arg); if (JS_IsException (keys)) { - JS_FreeValue (ctx, result); return JS_EXCEPTION; } uint32_t len; if (js_get_length32 (ctx, &len, keys)) { - JS_FreeValue (ctx, keys); - JS_FreeValue (ctx, result); return JS_EXCEPTION; } @@ -23899,14 +23460,10 @@ static JSValue js_cell_object (JSContext *ctx, JSValue this_val, int argc, JSVal JSValue key = JS_GetPropertyUint32 (ctx, keys, i); JSValue val = JS_GetProperty (ctx, arg, key); if (JS_IsException (val)) { - JS_FreeValue (ctx, key); - JS_FreeValue (ctx, keys); - JS_FreeValue (ctx, result); return JS_EXCEPTION; } JS_SetProperty (ctx, result, key, val); } - JS_FreeValue (ctx, keys); return result; } @@ -23918,51 +23475,37 @@ static JSValue js_cell_object (JSContext *ctx, JSValue this_val, int argc, JSVal /* Copy from first object */ JSValue keys = JS_GetOwnPropertyNames (ctx, arg); if (JS_IsException (keys)) { - JS_FreeValue (ctx, result); return JS_EXCEPTION; } uint32_t len; if (js_get_length32 (ctx, &len, keys)) { - JS_FreeValue (ctx, keys); - JS_FreeValue (ctx, result); return JS_EXCEPTION; } for (uint32_t i = 0; i < len; i++) { JSValue key = JS_GetPropertyUint32 (ctx, keys, i); JSValue val = JS_GetProperty (ctx, arg, key); if (JS_IsException (val)) { - JS_FreeValue (ctx, key); - JS_FreeValue (ctx, keys); - JS_FreeValue (ctx, result); return JS_EXCEPTION; } JS_SetProperty (ctx, result, key, val); } - JS_FreeValue (ctx, keys); /* Copy from second object */ keys = JS_GetOwnPropertyNames (ctx, argv[1]); if (JS_IsException (keys)) { - JS_FreeValue (ctx, result); return JS_EXCEPTION; } if (js_get_length32 (ctx, &len, keys)) { - JS_FreeValue (ctx, keys); - JS_FreeValue (ctx, result); return JS_EXCEPTION; } for (uint32_t i = 0; i < len; i++) { JSValue key = JS_GetPropertyUint32 (ctx, keys, i); JSValue val = JS_GetProperty (ctx, argv[1], key); if (JS_IsException (val)) { - JS_FreeValue (ctx, key); - JS_FreeValue (ctx, keys); - JS_FreeValue (ctx, result); return JS_EXCEPTION; } JS_SetProperty (ctx, result, key, val); } - JS_FreeValue (ctx, keys); return result; } @@ -24016,15 +23559,13 @@ static JSValue js_cell_object (JSContext *ctx, JSValue this_val, int argc, JSVal if (argc < 2 || JS_IsNull (argv[1])) { val = JS_TRUE; } else if (is_func) { - JSValue arg_key = JS_DupValue (ctx, key); + JSValue arg_key = key; val = JS_CallInternal (ctx, argv[1], JS_NULL, 1, &arg_key, 0); - JS_FreeValue (ctx, arg_key); if (JS_IsException (val)) { - JS_FreeValue (ctx, result); return JS_EXCEPTION; } } else { - val = JS_DupValue (ctx, argv[1]); + val = argv[1]; } JS_SetProperty (ctx, result, prop_key, val); /* prop_key is interned, no need to free */ @@ -24044,7 +23585,7 @@ static JSValue js_cell_object (JSContext *ctx, JSValue this_val, int argc, JSVal /* fn.apply(func, args) - arity is enforced in JS_CallInternal */ static JSValue js_cell_fn_apply (JSContext *ctx, JSValue this_val, int argc, JSValue *argv) { if (argc < 1) return JS_NULL; - if (!JS_IsFunction (argv[0])) return JS_DupValue (ctx, argv[0]); + if (!JS_IsFunction (argv[0])) return argv[0]; JSValue func = argv[0]; @@ -24053,9 +23594,8 @@ static JSValue js_cell_fn_apply (JSContext *ctx, JSValue this_val, int argc, JSV JSValue args_val = argv[1]; if (!JS_IsArray (args_val)) { /* Wrap single value in array */ - JSValue arg = JS_DupValue (ctx, args_val); + JSValue arg = args_val; JSValue result = JS_CallInternal (ctx, func, JS_NULL, 1, &arg, 0); - JS_FreeValue (ctx, arg); return result; } @@ -24068,13 +23608,12 @@ static JSValue js_cell_fn_apply (JSContext *ctx, JSValue this_val, int argc, JSV if (!args) return JS_EXCEPTION; for (int i = 0; i < len; i++) { - args[i] = JS_DupValue (ctx, arr->values[i]); + args[i] = arr->values[i]; } JSValue result = JS_CallInternal (ctx, func, JS_NULL, len, args, 0); for (int i = 0; i < len; i++) - JS_FreeValue (ctx, args[i]); js_free (ctx, args); return result; @@ -24152,7 +23691,6 @@ static JSValue js_blob_constructor (JSContext *ctx, JSValue this_val, int argc, int64_t fitval; JS_ToInt64 (ctx, &fitval, randval); - JS_FreeValue (ctx, randval); size_t bits_to_use = length_bits - bits_written; if (bits_to_use > 52) bits_to_use = 52; @@ -24653,19 +24191,19 @@ static JSValue js_cell_stone (JSContext *ctx, JSValue this_val, int argc, JSValu blob *bd = js_get_blob (ctx, obj); if (bd) { bd->is_stone = true; - return JS_DupValue (ctx, obj); + return obj; } if (JS_IsObject (obj)) { JSRecord *rec = JS_VALUE_GET_RECORD (obj); obj_set_stone (rec); - return JS_DupValue (ctx, obj); + return obj; } if (JS_IsArray (obj)) { JSArray *arr = JS_VALUE_GET_ARRAY (obj); arr->mist_hdr = objhdr_set_s (arr->mist_hdr, true); - return JS_DupValue (ctx, obj); + return obj; } return JS_NULL; @@ -24690,7 +24228,7 @@ static JSValue js_cell_reverse (JSContext *ctx, JSValue this_val, int argc, JSVa if (JS_IsException (result)) return result; JSArray *out = JS_VALUE_GET_ARRAY (result); for (int i = len - 1, j = 0; i >= 0; i--, j++) { - out->values[j] = JS_DupValue (ctx, arr->values[i]); + out->values[j] = arr->values[i]; } out->len = len; return result; @@ -24761,7 +24299,7 @@ static JSValue js_cell_push (JSContext *ctx, JSValue this_val, int argc, JSValue JSArray *arr = JS_VALUE_GET_ARRAY (obj); for (int i = 1; i < argc; i++) { - if (js_intrinsic_array_push (ctx, arr, JS_DupValue (ctx, argv[i])) < 0) + if (js_intrinsic_array_push (ctx, arr, argv[i]) < 0) return JS_EXCEPTION; } @@ -24775,7 +24313,7 @@ static JSValue js_cell_proto (JSContext *ctx, JSValue this_val, int argc, JSValu /* Intrinsic arrays return the Object prototype */ if (JS_IsArray (obj)) { - return JS_DupValue (ctx, ctx->class_proto[JS_CLASS_OBJECT]); + return ctx->class_proto[JS_CLASS_OBJECT]; } if (!JS_IsObject (obj)) return JS_NULL; @@ -24789,11 +24327,8 @@ static JSValue js_cell_proto (JSContext *ctx, JSValue this_val, int argc, JSValu = JS_GetPropertyStr (ctx, ctx->class_proto[JS_CLASS_OBJECT], ""); if (JS_VALUE_GET_OBJ (proto) == JS_VALUE_GET_OBJ (ctx->class_proto[JS_CLASS_OBJECT])) { - JS_FreeValue (ctx, proto); - JS_FreeValue (ctx, obj_proto); return JS_NULL; } - JS_FreeValue (ctx, obj_proto); } return proto; @@ -24815,27 +24350,27 @@ static JSValue js_cell_meme (JSContext *ctx, JSValue this_val, int argc, JSValue break; \ JSValue _keys = JS_GetOwnPropertyNames (ctx, mix); \ if (JS_IsException (_keys)) { \ - JS_FreeValue (ctx, result); \ + ; \ return JS_EXCEPTION; \ } \ uint32_t _len; \ if (js_get_length32 (ctx, &_len, _keys)) { \ - JS_FreeValue (ctx, _keys); \ - JS_FreeValue (ctx, result); \ + ; \ + ; \ return JS_EXCEPTION; \ } \ for (uint32_t j = 0; j < _len; j++) { \ JSValue _key = JS_GetPropertyUint32 (ctx, _keys, j); \ JSValue val = JS_GetProperty (ctx, mix, _key); \ if (JS_IsException (val)) { \ - JS_FreeValue (ctx, _key); \ - JS_FreeValue (ctx, _keys); \ - JS_FreeValue (ctx, result); \ + ; \ + ; \ + ; \ return JS_EXCEPTION; \ } \ JS_SetProperty (ctx, result, _key, val); \ } \ - JS_FreeValue (ctx, _keys); \ + ; \ } while (0) /* Process all arguments starting from argv[1] as mixins */ @@ -24846,18 +24381,15 @@ static JSValue js_cell_meme (JSContext *ctx, JSValue this_val, int argc, JSValue /* Array of mixins */ int64_t len; if (js_get_length64 (ctx, &len, mixins)) { - JS_FreeValue (ctx, result); return JS_EXCEPTION; } for (int64_t j = 0; j < len; j++) { JSValue mix = JS_GetPropertyInt64 (ctx, mixins, j); if (JS_IsException (mix)) { - JS_FreeValue (ctx, result); return JS_EXCEPTION; } APPLY_MIXIN (mix); - JS_FreeValue (ctx, mix); } } else if (JS_IsObject (mixins) && !JS_IsNull (mixins)) { /* Single mixin object */ @@ -24884,21 +24416,16 @@ static JSValue js_cell_splat (JSContext *ctx, JSValue this_val, int argc, JSValu JSValue result = JS_NewObject (ctx); if (JS_IsException (result)) return JS_EXCEPTION; - JSValue current = JS_DupValue (ctx, obj); + JSValue current = obj; /* Walk prototype chain and collect text keys */ while (!JS_IsNull (current)) { JSValue keys = JS_GetOwnPropertyNames (ctx, current); if (JS_IsException (keys)) { - JS_FreeValue (ctx, current); - JS_FreeValue (ctx, result); return JS_EXCEPTION; } uint32_t len; if (js_get_length32 (ctx, &len, keys)) { - JS_FreeValue (ctx, keys); - JS_FreeValue (ctx, current); - JS_FreeValue (ctx, result); return JS_EXCEPTION; } @@ -24907,19 +24434,11 @@ static JSValue js_cell_splat (JSContext *ctx, JSValue this_val, int argc, JSValu /* Check if property not already in result */ int has = JS_HasProperty (ctx, result, key); if (has < 0) { - JS_FreeValue (ctx, key); - JS_FreeValue (ctx, keys); - JS_FreeValue (ctx, current); - JS_FreeValue (ctx, result); return JS_EXCEPTION; } if (!has) { JSValue val = JS_GetProperty (ctx, current, key); if (JS_IsException (val)) { - JS_FreeValue (ctx, key); - JS_FreeValue (ctx, keys); - JS_FreeValue (ctx, current); - JS_FreeValue (ctx, result); return JS_EXCEPTION; } /* Only include serializable types */ @@ -24928,17 +24447,12 @@ static JSValue js_cell_splat (JSContext *ctx, JSValue this_val, int argc, JSValu || tag == JS_TAG_STRING_IMM || tag == JS_TAG_BOOL) { JS_SetProperty (ctx, result, key, val); } else { - JS_FreeValue (ctx, key); - JS_FreeValue (ctx, val); } } else { - JS_FreeValue (ctx, key); } } - JS_FreeValue (ctx, keys); JSValue next = JS_GetPrototype (ctx, current); - JS_FreeValue (ctx, current); current = next; } @@ -24958,12 +24472,9 @@ static JSValue js_cell_splat (JSContext *ctx, JSValue this_val, int argc, JSValu JS_SetProperty (ctx, result, key, val); } } - JS_FreeValue (ctx, keys2); } } - JS_FreeValue (ctx, extra); } - JS_FreeValue (ctx, to_data); return result; } @@ -25011,11 +24522,9 @@ static JSValue js_cell_length (JSContext *ctx, JSValue this_val, int argc, JSVal if (!JS_IsException (len) && !JS_IsNull (len)) { if (JS_IsFunction (len)) { JSValue result = JS_Call (ctx, len, val, 0, NULL); - JS_FreeValue (ctx, len); return result; } if (JS_VALUE_IS_NUMBER (len)) return len; - JS_FreeValue (ctx, len); } else if (JS_IsException (len)) { return len; } @@ -25163,13 +24672,10 @@ static JSValue js_cell_is_proto (JSContext *ctx, JSValue this_val, int argc, JSV if (!JS_IsException (master_proto) && !JS_IsNull (master_proto)) { JSRecord *p1 = JS_VALUE_GET_OBJ (proto); JSRecord *p2 = JS_VALUE_GET_OBJ (master_proto); - JS_FreeValue (ctx, master_proto); if (p1 == p2) { - JS_FreeValue (ctx, proto); return JS_TRUE; } } else if (!JS_IsException (master_proto)) { - JS_FreeValue (ctx, master_proto); } } /* Also check if proto == master directly */ @@ -25177,13 +24683,11 @@ static JSValue js_cell_is_proto (JSContext *ctx, JSValue this_val, int argc, JSV JSRecord *p1 = JS_VALUE_GET_OBJ (proto); JSRecord *p2 = JS_VALUE_GET_OBJ (master); if (p1 == p2) { - JS_FreeValue (ctx, proto); return JS_TRUE; } } JSValue next = JS_GetPrototype (ctx, proto); - JS_FreeValue (ctx, proto); proto = next; } if (JS_IsException (proto)) return proto; @@ -25240,8 +24744,7 @@ void JS_AddIntrinsicBaseObjects (JSContext *ctx) { /* Error */ obj1 = JS_NewCFunctionMagic (ctx, js_error_constructor, "Error", 1, JS_CFUNC_generic_magic, -1); - JS_SetPropertyStr (ctx, ctx->global_obj, "Error", JS_DupValue (ctx, obj1)); - JS_FreeValue (ctx, obj1); + JS_SetPropertyStr (ctx, ctx->global_obj, "Error", obj1); for (i = 0; i < JS_NATIVE_ERROR_COUNT; i++) { JSValue func_obj; @@ -25249,15 +24752,13 @@ void JS_AddIntrinsicBaseObjects (JSContext *ctx) { n_args = 1 + (i == JS_AGGREGATE_ERROR); func_obj = JS_NewCFunctionMagic (ctx, js_error_constructor, native_error_name[i], n_args, JS_CFUNC_generic_magic, i); - JS_SetPropertyStr (ctx, ctx->global_obj, native_error_name[i], JS_DupValue (ctx, func_obj)); - JS_FreeValue (ctx, func_obj); + JS_SetPropertyStr (ctx, ctx->global_obj, native_error_name[i], func_obj); } /* global properties */ { JSValue key = JS_KEY_STR (ctx, "globalThis"); - JS_SetPropertyInternal (ctx, ctx->global_obj, key, JS_DupValue (ctx, ctx->global_obj)); - JS_FreeValue (ctx, key); + JS_SetPropertyInternal (ctx, ctx->global_obj, key, ctx->global_obj); } /* Cell Script global functions: text, number, array, object, fn */ @@ -25471,7 +24972,7 @@ JSValue js_debugger_backtrace_fns (JSContext *ctx, const uint8_t *cur_pc) { for (sf = ctx->rt->current_stack_frame; sf != NULL; sf = sf->prev_frame) { uint32_t id = stack_index++; - JS_SetPropertyUint32 (ctx, ret, id, JS_DupValue (ctx, sf->cur_func)); + JS_SetPropertyUint32 (ctx, ret, id, sf->cur_func); } return ret; } @@ -25510,7 +25011,7 @@ JSValue js_debugger_build_backtrace (JSContext *ctx, const uint8_t *cur_pc) { int col_num; line_num1 = find_line_num (ctx, b, pc - b->byte_code_buf - 1, &col_num); - JS_SetPropertyStr (ctx, current_frame, "filename", JS_DupValue (ctx, b->debug.filename)); + JS_SetPropertyStr (ctx, current_frame, "filename", b->debug.filename); if (line_num1 != -1) JS_SetPropertyStr (ctx, current_frame, "line", JS_NewUint32 (ctx, line_num1)); } @@ -26056,7 +25557,7 @@ JSValue js_debugger_local_variables (JSContext *ctx, int stack_index) { JSValue this_obj = sf->var_buf[b->var_count]; // only provide a this if it is not the global object. if (JS_VALUE_GET_OBJ (this_obj) != JS_VALUE_GET_OBJ (ctx->global_obj)) - JS_SetPropertyStr (ctx, ret, "this", JS_DupValue (ctx, this_obj)); + JS_SetPropertyStr (ctx, ret, "this", this_obj); } } @@ -26079,7 +25580,7 @@ JSValue js_debugger_local_variables (JSContext *ctx, int stack_index) { if (JS_IsUninitialized (var_val)) continue; JSVarDef *vd = b->vardefs + i; - JS_SetProperty (ctx, ret, vd->var_name, JS_DupValue (ctx, var_val)); + JS_SetProperty (ctx, ret, vd->var_name, var_val); } break; @@ -26107,8 +25608,7 @@ void js_debugger_set_closure_variable (JSContext *ctx, JSValue fn, JSValue var_n JSVarRef *var_ref = NULL; if (f->u.func.var_refs) var_ref = f->u.func.var_refs[i]; if (var_ref && var_ref->pvalue) { - JS_FreeValue (ctx, *var_ref->pvalue); - *var_ref->pvalue = JS_DupValue (ctx, val); + *var_ref->pvalue = val; } break; } @@ -26135,7 +25635,7 @@ JSValue js_debugger_closure_variables (JSContext *ctx, JSValue fn) { if (JS_IsUninitialized (var_val)) continue; - JS_SetProperty (ctx, ret, cvar->var_name, JS_DupValue (ctx, var_val)); + JS_SetProperty (ctx, ret, cvar->var_name, var_val); } done: @@ -26159,7 +25659,6 @@ static JSValue js_cell_json_encode (JSContext *ctx, JSValue this_val, int argc, JSValue replacer = argc > 1 ? argv[1] : JS_NULL; JSValue space = argc > 2 ? argv[2] : JS_NewInt32 (ctx, 1); JSValue result = JS_JSONStringify (ctx, argv[0], replacer, space); - if (argc <= 2) JS_FreeValue (ctx, space); return result; } @@ -26192,8 +25691,6 @@ static JSValue js_cell_json_decode (JSContext *ctx, JSValue this_val, int argc, JSValue key = JS_KEY_empty; JSValue args[2] = { key, JS_GetProperty (ctx, holder, key) }; JSValue final = JS_Call (ctx, argv[1], holder, 2, args); - JS_FreeValue (ctx, args[1]); - JS_FreeValue (ctx, wrapper); result = final; }