@@ -1302,6 +1302,8 @@ static void js_new_callsite_data(JSContext *ctx, JSCallSiteData *csd, JSStackFra
1302
1302
static void js_new_callsite_data2(JSContext *ctx, JSCallSiteData *csd, const char *filename, int line_num, int col_num);
1303
1303
static void _JS_AddIntrinsicCallSite(JSContext *ctx);
1304
1304
1305
+ static void JS_SetOpaqueInternal(JSValue obj, void *opaque);
1306
+
1305
1307
static const JSClassExoticMethods js_arguments_exotic_methods;
1306
1308
static const JSClassExoticMethods js_string_exotic_methods;
1307
1309
static const JSClassExoticMethods js_proxy_exotic_methods;
@@ -5226,7 +5228,7 @@ JSValue JS_NewCFunctionData(JSContext *ctx, JSCFunctionData *func,
5226
5228
s->magic = magic;
5227
5229
for(i = 0; i < data_len; i++)
5228
5230
s->data[i] = js_dup(data[i]);
5229
- JS_SetOpaque (func_obj, s);
5231
+ JS_SetOpaqueInternal (func_obj, s);
5230
5232
js_function_set_properties(ctx, func_obj,
5231
5233
JS_ATOM_empty_string, length);
5232
5234
return func_obj;
@@ -10072,13 +10074,29 @@ void JS_ResetUncatchableError(JSContext *ctx)
10072
10074
JS_SetUncatchableError(ctx, ctx->rt->current_exception, FALSE);
10073
10075
}
10074
10076
10075
- void JS_SetOpaque(JSValue obj, void *opaque)
10077
+ JS_BOOL JS_SetOpaque(JSValue obj, void *opaque)
10076
10078
{
10077
- JSObject *p;
10079
+ JSObject *p;
10078
10080
if (JS_VALUE_GET_TAG(obj) == JS_TAG_OBJECT) {
10079
10081
p = JS_VALUE_GET_OBJ(obj);
10080
- p->u.opaque = opaque;
10082
+ // User code can't set the opaque of internal objects.
10083
+ if (p->class_id >= JS_CLASS_INIT_COUNT) {
10084
+ p->u.opaque = opaque;
10085
+ return 0;
10086
+ }
10081
10087
}
10088
+
10089
+ return 1;
10090
+ }
10091
+
10092
+ /* |obj| must be a JSObject of an internal class. */
10093
+ static void JS_SetOpaqueInternal(JSValue obj, void *opaque)
10094
+ {
10095
+ JSObject *p;
10096
+ assert(JS_VALUE_GET_TAG(obj) == JS_TAG_OBJECT);
10097
+ p = JS_VALUE_GET_OBJ(obj);
10098
+ assert(p->class_id < JS_CLASS_INIT_COUNT);
10099
+ p->u.opaque = opaque;
10082
10100
}
10083
10101
10084
10102
/* return NULL if not an object of class class_id */
@@ -17807,7 +17825,7 @@ static JSValue js_generator_function_call(JSContext *ctx, JSValue func_obj,
17807
17825
obj = js_create_from_ctor(ctx, func_obj, JS_CLASS_GENERATOR);
17808
17826
if (JS_IsException(obj))
17809
17827
goto fail;
17810
- JS_SetOpaque (obj, s);
17828
+ JS_SetOpaqueInternal (obj, s);
17811
17829
return obj;
17812
17830
fail:
17813
17831
free_generator_stack_rt(ctx->rt, s);
@@ -18438,7 +18456,7 @@ static JSValue js_async_generator_function_call(JSContext *ctx, JSValue func_obj
18438
18456
if (JS_IsException(obj))
18439
18457
goto fail;
18440
18458
s->generator = JS_VALUE_GET_OBJ(obj);
18441
- JS_SetOpaque (obj, s);
18459
+ JS_SetOpaqueInternal (obj, s);
18442
18460
return obj;
18443
18461
fail:
18444
18462
js_async_generator_free(ctx->rt, s);
@@ -39862,7 +39880,7 @@ static JSValue js_create_array_iterator(JSContext *ctx, JSValue this_val,
39862
39880
it->obj = arr;
39863
39881
it->kind = kind;
39864
39882
it->idx = 0;
39865
- JS_SetOpaque (enum_obj, it);
39883
+ JS_SetOpaqueInternal (enum_obj, it);
39866
39884
return enum_obj;
39867
39885
fail1:
39868
39886
JS_FreeValue(ctx, enum_obj);
@@ -43862,7 +43880,7 @@ static JSValue js_regexp_Symbol_matchAll(JSContext *ctx, JSValue this_val,
43862
43880
it->global = string_indexof_char(strp, 'g', 0) >= 0;
43863
43881
it->unicode = string_indexof_char(strp, 'u', 0) >= 0;
43864
43882
it->done = FALSE;
43865
- JS_SetOpaque (iter, it);
43883
+ JS_SetOpaqueInternal (iter, it);
43866
43884
43867
43885
JS_FreeValue(ctx, C);
43868
43886
JS_FreeValue(ctx, flags);
@@ -46112,7 +46130,7 @@ static JSValue js_proxy_constructor(JSContext *ctx, JSValue this_val,
46112
46130
s->handler = js_dup(handler);
46113
46131
s->is_func = JS_IsFunction(ctx, target);
46114
46132
s->is_revoked = FALSE;
46115
- JS_SetOpaque (obj, s);
46133
+ JS_SetOpaqueInternal (obj, s);
46116
46134
JS_SetConstructorBit(ctx, obj, JS_IsConstructor(ctx, target));
46117
46135
return obj;
46118
46136
}
@@ -46345,7 +46363,7 @@ static JSValue js_map_constructor(JSContext *ctx, JSValue new_target,
46345
46363
goto fail;
46346
46364
init_list_head(&s->records);
46347
46365
s->is_weak = is_weak;
46348
- JS_SetOpaque (obj, s);
46366
+ JS_SetOpaqueInternal (obj, s);
46349
46367
s->hash_size = 1;
46350
46368
s->hash_table = js_malloc(ctx, sizeof(s->hash_table[0]) * s->hash_size);
46351
46369
if (!s->hash_table)
@@ -46993,7 +47011,7 @@ static JSValue js_create_map_iterator(JSContext *ctx, JSValue this_val,
46993
47011
it->obj = js_dup(this_val);
46994
47012
it->kind = kind;
46995
47013
it->cur_record = NULL;
46996
- JS_SetOpaque (enum_obj, it);
47014
+ JS_SetOpaqueInternal (enum_obj, it);
46997
47015
return enum_obj;
46998
47016
fail:
46999
47017
return JS_EXCEPTION;
@@ -48091,7 +48109,7 @@ static int js_create_resolving_functions(JSContext *ctx,
48091
48109
sr->ref_count++;
48092
48110
s->presolved = sr;
48093
48111
s->promise = js_dup(promise);
48094
- JS_SetOpaque (obj, s);
48112
+ JS_SetOpaqueInternal (obj, s);
48095
48113
js_function_set_properties(ctx, obj, JS_ATOM_empty_string, 1);
48096
48114
resolving_funcs[i] = obj;
48097
48115
}
@@ -48236,7 +48254,7 @@ static JSValue js_promise_constructor(JSContext *ctx, JSValue new_target,
48236
48254
for(i = 0; i < 2; i++)
48237
48255
init_list_head(&s->promise_reactions[i]);
48238
48256
s->promise_result = JS_UNDEFINED;
48239
- JS_SetOpaque (obj, s);
48257
+ JS_SetOpaqueInternal (obj, s);
48240
48258
if (js_create_resolving_functions(ctx, args, obj))
48241
48259
goto fail;
48242
48260
ret = JS_Call(ctx, executor, JS_UNDEFINED, 2, args);
@@ -48991,7 +49009,7 @@ static JSValue JS_CreateAsyncFromSyncIterator(JSContext *ctx,
48991
49009
}
48992
49010
s->sync_iter = js_dup(sync_iter);
48993
49011
s->next_method = next_method;
48994
- JS_SetOpaque (async_iter, s);
49012
+ JS_SetOpaqueInternal (async_iter, s);
48995
49013
return async_iter;
48996
49014
}
48997
49015
@@ -51202,7 +51220,7 @@ static JSValue js_array_buffer_constructor3(JSContext *ctx,
51202
51220
abuf->free_func = free_func;
51203
51221
if (alloc_flag && buf)
51204
51222
memcpy(abuf->data, buf, len);
51205
- JS_SetOpaque (obj, abuf);
51223
+ JS_SetOpaqueInternal (obj, abuf);
51206
51224
return obj;
51207
51225
fail:
51208
51226
JS_FreeValue(ctx, obj);
@@ -54771,7 +54789,7 @@ static JSValue js_weakref_constructor(JSContext *ctx, JSValue new_target, int ar
54771
54789
wr->u.weak_ref_data = wrd;
54772
54790
insert_weakref_record(arg, wr);
54773
54791
54774
- JS_SetOpaque (obj, wrd);
54792
+ JS_SetOpaqueInternal (obj, wrd);
54775
54793
return obj;
54776
54794
}
54777
54795
@@ -54881,7 +54899,7 @@ static JSValue js_finrec_constructor(JSContext *ctx, JSValue new_target, int arg
54881
54899
init_list_head(&frd->entries);
54882
54900
frd->ctx = ctx;
54883
54901
frd->cb = js_dup(cb);
54884
- JS_SetOpaque (obj, frd);
54902
+ JS_SetOpaqueInternal (obj, frd);
54885
54903
return obj;
54886
54904
}
54887
54905
@@ -55039,7 +55057,7 @@ static void reset_weak_ref(JSRuntime *rt, JSWeakRefRecord **first_weak_ref)
55039
55057
break;
55040
55058
case JS_WEAK_REF_KIND_WEAK_REF:
55041
55059
wrd = wr->u.weak_ref_data;
55042
- JS_SetOpaque (wrd->obj, &js_weakref_sentinel);
55060
+ JS_SetOpaqueInternal (wrd->obj, &js_weakref_sentinel);
55043
55061
js_free_rt(rt, wrd);
55044
55062
break;
55045
55063
case JS_WEAK_REF_KIND_FINALIZATION_REGISTRY_ENTRY: {
@@ -55271,7 +55289,7 @@ static JSValue js_new_callsite(JSContext *ctx, JSCallSiteData *csd) {
55271
55289
55272
55290
memcpy(csd1, csd, sizeof(*csd));
55273
55291
55274
- JS_SetOpaque (obj, csd1);
55292
+ JS_SetOpaqueInternal (obj, csd1);
55275
55293
55276
55294
return obj;
55277
55295
}
0 commit comments