1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-12-04 14:46:47 -05:00

[spidermonkey] JSAutoRealm

This commit is contained in:
Witold Filipczyk 2023-01-19 18:52:18 +01:00
parent 54f1426e43
commit c8138381a4
7 changed files with 33 additions and 47 deletions

View File

@ -88,8 +88,9 @@ struct ecmascript_interpreter {
* is reloaded in another tab and then you just cause the current tab * is reloaded in another tab and then you just cause the current tab
* to redraw. */ * to redraw. */
unsigned int onload_snippets_cache_id; unsigned int onload_snippets_cache_id;
void *ac; #ifdef CONFIG_ECMASCRIPT_SMJS
void *ac2; JS::Heap<JSObject*> *ac;
#endif
#ifdef CONFIG_QUICKJS #ifdef CONFIG_QUICKJS
JSValue document_obj; JSValue document_obj;
JSValue location_obj; JSValue location_obj;

View File

@ -166,7 +166,6 @@ mujs_put_interpreter(struct ecmascript_interpreter *interpreter)
JS_FreeContext(ctx); JS_FreeContext(ctx);
interpreter->backend_data = nullptr; interpreter->backend_data = nullptr;
interpreter->ac = nullptr; interpreter->ac = nullptr;
interpreter->ac2 = nullptr;
#endif #endif
} }

View File

@ -137,7 +137,6 @@ quickjs_put_interpreter(struct ecmascript_interpreter *interpreter)
JS_FreeContext(ctx); JS_FreeContext(ctx);
interpreter->backend_data = nullptr; interpreter->backend_data = nullptr;
interpreter->ac = nullptr; interpreter->ac = nullptr;
interpreter->ac2 = nullptr;
} }
static void static void

View File

@ -167,30 +167,33 @@ spidermonkey_get_interpreter(struct ecmascript_interpreter *interpreter)
JS_AddInterruptCallback(ctx, heartbeat_callback); JS_AddInterruptCallback(ctx, heartbeat_callback);
JS::RealmOptions options; JS::RealmOptions options;
JS::RootedObject global(ctx);
JS::RootedObject window_obj(ctx, JS_NewGlobalObject(ctx, &window_class, NULL, JS::FireOnNewGlobalHook, options)); JS::Heap<JSObject*> *window_obj = new JS::Heap<JSObject*>(JS_NewGlobalObject(ctx, &window_class, NULL, JS::FireOnNewGlobalHook, options));
if (window_obj) { global = window_obj->get();
interpreter->ac = window_obj; JSAutoRealm ar(ctx, global);
interpreter->ac2 = new JSAutoRealm(ctx, window_obj);
} else { if (!global) {
goto release_and_fail; goto release_and_fail;
} }
interpreter->ac = window_obj;
if (!JS::InitRealmStandardClasses(ctx)) { if (!JS::InitRealmStandardClasses(ctx)) {
goto release_and_fail; goto release_and_fail;
} }
if (!JS_DefineProperties(ctx, window_obj, window_props)) { if (!JS_DefineProperties(ctx, global, window_props)) {
goto release_and_fail; goto release_and_fail;
} }
if (!spidermonkey_DefineFunctions(ctx, window_obj, window_funcs)) { if (!spidermonkey_DefineFunctions(ctx, global, window_funcs)) {
goto release_and_fail; goto release_and_fail;
} }
//JS_SetPrivate(window_obj, interpreter); /* to @window_class */ //JS_SetPrivate(window_obj, interpreter); /* to @window_class */
document_obj = spidermonkey_InitClass(ctx, window_obj, NULL, document_obj = spidermonkey_InitClass(ctx, global, NULL,
&document_class, NULL, 0, &document_class, NULL, 0,
document_props, document_props,
document_funcs, document_funcs,
@ -212,7 +215,7 @@ spidermonkey_get_interpreter(struct ecmascript_interpreter *interpreter)
} }
*/ */
history_obj = spidermonkey_InitClass(ctx, window_obj, NULL, history_obj = spidermonkey_InitClass(ctx, global, NULL,
&history_class, NULL, 0, &history_class, NULL, 0,
(JSPropertySpec *) NULL, (JSPropertySpec *) NULL,
history_funcs, history_funcs,
@ -221,7 +224,7 @@ spidermonkey_get_interpreter(struct ecmascript_interpreter *interpreter)
goto release_and_fail; goto release_and_fail;
} }
location_obj = spidermonkey_InitClass(ctx, window_obj, NULL, location_obj = spidermonkey_InitClass(ctx, global, NULL,
&location_class, NULL, 0, &location_class, NULL, 0,
location_props, location_props,
location_funcs, location_funcs,
@ -232,7 +235,7 @@ spidermonkey_get_interpreter(struct ecmascript_interpreter *interpreter)
interpreter->location_obj = location_obj; interpreter->location_obj = location_obj;
screen_obj = spidermonkey_InitClass(ctx, window_obj, NULL, screen_obj = spidermonkey_InitClass(ctx, global, NULL,
&screen_class, NULL, 0, &screen_class, NULL, 0,
screen_props, screen_props,
NULL, NULL,
@ -242,7 +245,7 @@ spidermonkey_get_interpreter(struct ecmascript_interpreter *interpreter)
goto release_and_fail; goto release_and_fail;
} }
menubar_obj = JS_InitClass(ctx, window_obj, nullptr, menubar_obj = JS_InitClass(ctx, global, nullptr,
&menubar_class, NULL, 0, &menubar_class, NULL, 0,
unibar_props, NULL, unibar_props, NULL,
NULL, NULL); NULL, NULL);
@ -251,7 +254,7 @@ spidermonkey_get_interpreter(struct ecmascript_interpreter *interpreter)
} }
JS::SetReservedSlot(menubar_obj, 0, JS::PrivateValue((char *)"t")); /* to @menubar_class */ JS::SetReservedSlot(menubar_obj, 0, JS::PrivateValue((char *)"t")); /* to @menubar_class */
statusbar_obj = JS_InitClass(ctx, window_obj, nullptr, statusbar_obj = JS_InitClass(ctx, global, nullptr,
&statusbar_class, NULL, 0, &statusbar_class, NULL, 0,
unibar_props, NULL, unibar_props, NULL,
NULL, NULL); NULL, NULL);
@ -260,7 +263,7 @@ spidermonkey_get_interpreter(struct ecmascript_interpreter *interpreter)
} }
JS::SetReservedSlot(statusbar_obj, 0, JS::PrivateValue((char *)"s")); /* to @statusbar_class */ JS::SetReservedSlot(statusbar_obj, 0, JS::PrivateValue((char *)"s")); /* to @statusbar_class */
navigator_obj = JS_InitClass(ctx, window_obj, nullptr, navigator_obj = JS_InitClass(ctx, global, nullptr,
&navigator_class, NULL, 0, &navigator_class, NULL, 0,
navigator_props, NULL, navigator_props, NULL,
NULL, NULL); NULL, NULL);
@ -268,7 +271,7 @@ spidermonkey_get_interpreter(struct ecmascript_interpreter *interpreter)
goto release_and_fail; goto release_and_fail;
} }
console_obj = spidermonkey_InitClass(ctx, window_obj, NULL, console_obj = spidermonkey_InitClass(ctx, global, NULL,
&console_class, NULL, 0, &console_class, NULL, 0,
nullptr, nullptr,
console_funcs, console_funcs,
@ -277,7 +280,7 @@ spidermonkey_get_interpreter(struct ecmascript_interpreter *interpreter)
goto release_and_fail; goto release_and_fail;
} }
localstorage_obj = spidermonkey_InitClass(ctx, window_obj, NULL, localstorage_obj = spidermonkey_InitClass(ctx, global, NULL,
&localstorage_class, NULL, 0, &localstorage_class, NULL, 0,
nullptr, nullptr,
localstorage_funcs, localstorage_funcs,
@ -286,7 +289,7 @@ spidermonkey_get_interpreter(struct ecmascript_interpreter *interpreter)
goto release_and_fail; goto release_and_fail;
} }
xhr_obj = spidermonkey_InitClass(ctx, window_obj, NULL, xhr_obj = spidermonkey_InitClass(ctx, global, NULL,
&xhr_class, xhr_constructor, 0, &xhr_class, xhr_constructor, 0,
xhr_props, xhr_props,
xhr_funcs, xhr_funcs,
@ -311,13 +314,8 @@ spidermonkey_put_interpreter(struct ecmascript_interpreter *interpreter)
assert(interpreter); assert(interpreter);
if (!js_module_init_ok) return; if (!js_module_init_ok) return;
if (interpreter->ac2) {
delete (JSAutoRealm *)interpreter->ac2;
}
// JS_DestroyContext(ctx);
interpreter->backend_data = NULL; interpreter->backend_data = NULL;
interpreter->ac = nullptr; interpreter->ac = nullptr;
interpreter->ac2 = nullptr;
} }
void void
@ -366,7 +364,7 @@ spidermonkey_eval(struct ecmascript_interpreter *interpreter,
return; return;
} }
ctx = (JSContext *)interpreter->backend_data; ctx = (JSContext *)interpreter->backend_data;
JS::Realm *comp = JS::EnterRealm(ctx, (JSObject *)interpreter->ac); JSAutoRealm ar(ctx, (JSObject *)interpreter->ac->get());
interpreter->heartbeat = add_heartbeat(interpreter); interpreter->heartbeat = add_heartbeat(interpreter);
interpreter->ret = ret; interpreter->ret = ret;
@ -384,7 +382,6 @@ spidermonkey_eval(struct ecmascript_interpreter *interpreter,
spidermonkey_check_for_exception(ctx); spidermonkey_check_for_exception(ctx);
done_heartbeat(interpreter->heartbeat); done_heartbeat(interpreter->heartbeat);
JS::LeaveRealm(ctx, comp);
} }
void void
@ -399,7 +396,7 @@ spidermonkey_call_function(struct ecmascript_interpreter *interpreter,
return; return;
} }
ctx = (JSContext *)interpreter->backend_data; ctx = (JSContext *)interpreter->backend_data;
JS::Realm *comp = JS::EnterRealm(ctx, (JSObject *)interpreter->ac); JSAutoRealm ar(ctx, (JSObject *)interpreter->ac->get());
interpreter->heartbeat = add_heartbeat(interpreter); interpreter->heartbeat = add_heartbeat(interpreter);
interpreter->ret = ret; interpreter->ret = ret;
@ -408,7 +405,6 @@ spidermonkey_call_function(struct ecmascript_interpreter *interpreter,
JS::RootedObject cg(ctx, JS::CurrentGlobalOrNull(ctx)); JS::RootedObject cg(ctx, JS::CurrentGlobalOrNull(ctx));
JS_CallFunctionValue(ctx, cg, fun, JS::HandleValueArray::empty(), &r_val); JS_CallFunctionValue(ctx, cg, fun, JS::HandleValueArray::empty(), &r_val);
done_heartbeat(interpreter->heartbeat); done_heartbeat(interpreter->heartbeat);
JS::LeaveRealm(ctx, comp);
} }
@ -427,7 +423,7 @@ spidermonkey_eval_stringback(struct ecmascript_interpreter *interpreter,
interpreter->ret = NULL; interpreter->ret = NULL;
interpreter->heartbeat = add_heartbeat(interpreter); interpreter->heartbeat = add_heartbeat(interpreter);
JS::Realm *comp = JS::EnterRealm(ctx, (JSObject *)interpreter->ac); JSAutoRealm ar(ctx, (JSObject *)interpreter->ac->get());
JS::RootedObject cg(ctx, JS::CurrentGlobalOrNull(ctx)); JS::RootedObject cg(ctx, JS::CurrentGlobalOrNull(ctx));
JS::RootedValue r_rval(ctx, rval); JS::RootedValue r_rval(ctx, rval);
@ -454,7 +450,6 @@ spidermonkey_eval_stringback(struct ecmascript_interpreter *interpreter,
} else { } else {
result = jsval_to_string(ctx, r_rval); result = jsval_to_string(ctx, r_rval);
} }
JS::LeaveRealm(ctx, comp);
return result; return result;
} }
@ -473,7 +468,7 @@ spidermonkey_eval_boolback(struct ecmascript_interpreter *interpreter,
ctx = (JSContext *)interpreter->backend_data; ctx = (JSContext *)interpreter->backend_data;
interpreter->ret = NULL; interpreter->ret = NULL;
JS::Realm *comp = JS::EnterRealm(ctx, (JSObject *)interpreter->ac); JSAutoRealm ar(ctx, (JSObject *)interpreter->ac->get());
JS::CompileOptions options(ctx); JS::CompileOptions options(ctx);
JS::RootedObjectVector ag(ctx); JS::RootedObjectVector ag(ctx);
@ -508,8 +503,6 @@ spidermonkey_eval_boolback(struct ecmascript_interpreter *interpreter,
result = r_val.toBoolean(); result = r_val.toBoolean();
} }
JS::LeaveRealm(ctx, comp);
return result; return result;
} }

View File

@ -3614,7 +3614,7 @@ check_element_event(void *elem, const char *event_name, struct term_event *ev)
struct element_private *el_private = el->second; struct element_private *el_private = el->second;
struct ecmascript_interpreter *interpreter = el_private->interpreter; struct ecmascript_interpreter *interpreter = el_private->interpreter;
JSContext *ctx = (JSContext *)interpreter->backend_data; JSContext *ctx = (JSContext *)interpreter->backend_data;
JS::Realm *comp = JS::EnterRealm(ctx, (JSObject *)interpreter->ac); JSAutoRealm ar(ctx, (JSObject *)interpreter->ac->get());
JS::RootedValue r_val(ctx); JS::RootedValue r_val(ctx);
interpreter->heartbeat = add_heartbeat(interpreter); interpreter->heartbeat = add_heartbeat(interpreter);
@ -3637,7 +3637,6 @@ check_element_event(void *elem, const char *event_name, struct term_event *ev)
} }
} }
done_heartbeat(interpreter->heartbeat); done_heartbeat(interpreter->heartbeat);
JS::LeaveRealm(ctx, comp);
check_for_rerender(interpreter, event_name); check_for_rerender(interpreter, event_name);
} }

View File

@ -203,7 +203,7 @@ onmessage_run(void *data)
struct ecmascript_interpreter *interpreter = elwin->interpreter; struct ecmascript_interpreter *interpreter = elwin->interpreter;
JSContext *ctx = (JSContext *)interpreter->backend_data; JSContext *ctx = (JSContext *)interpreter->backend_data;
JS::Realm *comp = JS::EnterRealm(ctx, (JSObject *)interpreter->ac); JSAutoRealm ar(ctx, (JSObject *)interpreter->ac->get());
JS::RootedValue r_val(ctx); JS::RootedValue r_val(ctx);
interpreter->heartbeat = add_heartbeat(interpreter); interpreter->heartbeat = add_heartbeat(interpreter);
@ -223,7 +223,6 @@ onmessage_run(void *data)
} }
JS_CallFunctionValue(ctx, elwin->thisval, elwin->onmessage, argv, &r_val); JS_CallFunctionValue(ctx, elwin->thisval, elwin->onmessage, argv, &r_val);
done_heartbeat(interpreter->heartbeat); done_heartbeat(interpreter->heartbeat);
JS::LeaveRealm(ctx, comp);
mem_free(mess); mem_free(mess);
check_for_rerender(interpreter, "window_onmessage"); check_for_rerender(interpreter, "window_onmessage");
} }

View File

@ -716,7 +716,7 @@ onload_run(void *data)
if (xhr) { if (xhr) {
struct ecmascript_interpreter *interpreter = xhr->interpreter; struct ecmascript_interpreter *interpreter = xhr->interpreter;
JSContext *ctx = (JSContext *)interpreter->backend_data; JSContext *ctx = (JSContext *)interpreter->backend_data;
JS::Realm *comp = JS::EnterRealm(ctx, (JSObject *)interpreter->ac); JSAutoRealm ar(ctx, (JSObject *)interpreter->ac->get());
JS::RootedValue r_val(ctx); JS::RootedValue r_val(ctx);
interpreter->heartbeat = add_heartbeat(interpreter); interpreter->heartbeat = add_heartbeat(interpreter);
@ -730,7 +730,6 @@ onload_run(void *data)
} }
JS_CallFunctionValue(ctx, xhr->thisval, xhr->onload, JS::HandleValueArray::empty(), &r_val); JS_CallFunctionValue(ctx, xhr->thisval, xhr->onload, JS::HandleValueArray::empty(), &r_val);
done_heartbeat(interpreter->heartbeat); done_heartbeat(interpreter->heartbeat);
JS::LeaveRealm(ctx, comp);
check_for_rerender(interpreter, "xhr_onload"); check_for_rerender(interpreter, "xhr_onload");
} }
@ -744,7 +743,7 @@ onloadend_run(void *data)
if (xhr) { if (xhr) {
struct ecmascript_interpreter *interpreter = xhr->interpreter; struct ecmascript_interpreter *interpreter = xhr->interpreter;
JSContext *ctx = (JSContext *)interpreter->backend_data; JSContext *ctx = (JSContext *)interpreter->backend_data;
JS::Realm *comp = JS::EnterRealm(ctx, (JSObject *)interpreter->ac); JSAutoRealm ar(ctx, (JSObject *)interpreter->ac->get());
JS::RootedValue r_val(ctx); JS::RootedValue r_val(ctx);
interpreter->heartbeat = add_heartbeat(interpreter); interpreter->heartbeat = add_heartbeat(interpreter);
@ -758,7 +757,6 @@ onloadend_run(void *data)
} }
JS_CallFunctionValue(ctx, xhr->thisval, xhr->onloadend, JS::HandleValueArray::empty(), &r_val); JS_CallFunctionValue(ctx, xhr->thisval, xhr->onloadend, JS::HandleValueArray::empty(), &r_val);
done_heartbeat(interpreter->heartbeat); done_heartbeat(interpreter->heartbeat);
JS::LeaveRealm(ctx, comp);
check_for_rerender(interpreter, "xhr_onloadend"); check_for_rerender(interpreter, "xhr_onloadend");
} }
@ -772,7 +770,7 @@ onreadystatechange_run(void *data)
if (xhr) { if (xhr) {
struct ecmascript_interpreter *interpreter = xhr->interpreter; struct ecmascript_interpreter *interpreter = xhr->interpreter;
JSContext *ctx = (JSContext *)interpreter->backend_data; JSContext *ctx = (JSContext *)interpreter->backend_data;
JS::Realm *comp = JS::EnterRealm(ctx, (JSObject *)interpreter->ac); JSAutoRealm ar(ctx, (JSObject *)interpreter->ac->get());
JS::RootedValue r_val(ctx); JS::RootedValue r_val(ctx);
interpreter->heartbeat = add_heartbeat(interpreter); interpreter->heartbeat = add_heartbeat(interpreter);
@ -786,7 +784,6 @@ onreadystatechange_run(void *data)
} }
JS_CallFunctionValue(ctx, xhr->thisval, xhr->onreadystatechange, JS::HandleValueArray::empty(), &r_val); JS_CallFunctionValue(ctx, xhr->thisval, xhr->onreadystatechange, JS::HandleValueArray::empty(), &r_val);
done_heartbeat(interpreter->heartbeat); done_heartbeat(interpreter->heartbeat);
JS::LeaveRealm(ctx, comp);
check_for_rerender(interpreter, "xhr_onreadystatechange"); check_for_rerender(interpreter, "xhr_onreadystatechange");
} }
@ -800,7 +797,7 @@ ontimeout_run(void *data)
if (xhr) { if (xhr) {
struct ecmascript_interpreter *interpreter = xhr->interpreter; struct ecmascript_interpreter *interpreter = xhr->interpreter;
JSContext *ctx = (JSContext *)interpreter->backend_data; JSContext *ctx = (JSContext *)interpreter->backend_data;
JS::Realm *comp = JS::EnterRealm(ctx, (JSObject *)interpreter->ac); JSAutoRealm ar(ctx, (JSObject *)interpreter->ac->get());
JS::RootedValue r_val(ctx); JS::RootedValue r_val(ctx);
interpreter->heartbeat = add_heartbeat(interpreter); interpreter->heartbeat = add_heartbeat(interpreter);
@ -814,7 +811,6 @@ ontimeout_run(void *data)
} }
JS_CallFunctionValue(ctx, xhr->thisval, xhr->ontimeout, JS::HandleValueArray::empty(), &r_val); JS_CallFunctionValue(ctx, xhr->thisval, xhr->ontimeout, JS::HandleValueArray::empty(), &r_val);
done_heartbeat(interpreter->heartbeat); done_heartbeat(interpreter->heartbeat);
JS::LeaveRealm(ctx, comp);
check_for_rerender(interpreter, "xhr_ontimeout"); check_for_rerender(interpreter, "xhr_ontimeout");
} }