1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-09-27 02:56:18 -04:00

[spidermonkey] Some code related to compartments. Progress.

This commit is contained in:
Witold Filipczyk 2020-11-16 22:00:48 +01:00
parent b0ced9308b
commit 873797935c
7 changed files with 554 additions and 652 deletions

View File

@ -54,6 +54,7 @@ struct ecmascript_interpreter {
* to redraw. */
unsigned int onload_snippets_cache_id;
void *ac;
void *ac2;
void *ar;
};

View File

@ -261,7 +261,8 @@ spidermonkey_get_interpreter(struct ecmascript_interpreter *interpreter)
JS::RootedObject window_obj(ctx, JS_NewGlobalObject(ctx, &window_class, NULL, JS::FireOnNewGlobalHook, options));
if (window_obj) {
interpreter->ac = new JSAutoCompartment(ctx, window_obj);
interpreter->ac = window_obj;
interpreter->ac2 = new JSAutoCompartment(ctx, window_obj);
} else {
goto release_and_fail;
}
@ -277,7 +278,7 @@ spidermonkey_get_interpreter(struct ecmascript_interpreter *interpreter)
if (!spidermonkey_DefineFunctions(ctx, window_obj, window_funcs)) {
goto release_and_fail;
}
JS_SetPrivate(window_obj, interpreter->vs); /* to @window_class */
//JS_SetPrivate(window_obj, interpreter); /* to @window_class */
document_obj = spidermonkey_InitClass(ctx, window_obj, NULL,
&document_class, NULL, 0,
@ -287,7 +288,6 @@ spidermonkey_get_interpreter(struct ecmascript_interpreter *interpreter)
if (!document_obj) {
goto release_and_fail;
}
JS_SetPrivate(document_obj, interpreter->vs);
forms_obj = spidermonkey_InitClass(ctx, document_obj, NULL,
&forms_class, NULL, 0,
@ -297,7 +297,6 @@ spidermonkey_get_interpreter(struct ecmascript_interpreter *interpreter)
if (!forms_obj) {
goto release_and_fail;
}
// JS_SetPrivate(forms_obj, interpreter->vs);
history_obj = spidermonkey_InitClass(ctx, window_obj, NULL,
&history_class, NULL, 0,
@ -307,8 +306,6 @@ spidermonkey_get_interpreter(struct ecmascript_interpreter *interpreter)
if (!history_obj) {
goto release_and_fail;
}
// JS_SetPrivate(history_obj, interpreter->vs);
location_obj = spidermonkey_InitClass(ctx, window_obj, NULL,
&location_class, NULL, 0,
@ -318,8 +315,6 @@ spidermonkey_get_interpreter(struct ecmascript_interpreter *interpreter)
if (!location_obj) {
goto release_and_fail;
}
// JS_SetPrivate(location_obj, interpreter->vs);
menubar_obj = JS_InitClass(ctx, window_obj, nullptr,
&menubar_class, NULL, 0,
@ -346,8 +341,6 @@ spidermonkey_get_interpreter(struct ecmascript_interpreter *interpreter)
if (!navigator_obj) {
goto release_and_fail;
}
// JS_SetPrivate(navigator_obj, interpreter->vs);
JS_SetCompartmentPrivate(js::GetContextCompartment(ctx), interpreter);
return ctx;
@ -367,7 +360,7 @@ spidermonkey_put_interpreter(struct ecmascript_interpreter *interpreter)
ctx = interpreter->backend_data;
if (interpreter->ac) {
delete (JSAutoCompartment *)interpreter->ac;
//delete (JSAutoCompartment *)interpreter->ac;
}
if (interpreter->ar) {
delete (JSAutoRequest *)interpreter->ar;
@ -391,6 +384,8 @@ spidermonkey_eval(struct ecmascript_interpreter *interpreter,
return;
}
ctx = interpreter->backend_data;
JS_BeginRequest(ctx);
JSCompartment *comp = JS_EnterCompartment(ctx, interpreter->ac);
interpreter->heartbeat = add_heartbeat(interpreter);
interpreter->ret = ret;
@ -401,6 +396,8 @@ spidermonkey_eval(struct ecmascript_interpreter *interpreter,
JS::Evaluate(ctx, options, code->source, code->length, &r_val);
done_heartbeat(interpreter->heartbeat);
JS_LeaveCompartment(ctx, comp);
JS_EndRequest(ctx);
}
@ -411,6 +408,7 @@ spidermonkey_eval_stringback(struct ecmascript_interpreter *interpreter,
bool ret;
JSContext *ctx;
JS::Value rval;
unsigned char *result = NULL;
assert(interpreter);
if (!js_module_init_ok) return NULL;
@ -418,6 +416,9 @@ spidermonkey_eval_stringback(struct ecmascript_interpreter *interpreter,
interpreter->ret = NULL;
interpreter->heartbeat = add_heartbeat(interpreter);
JS_BeginRequest(ctx);
JSCompartment *comp = JS_EnterCompartment(ctx, interpreter->ac);
JS::RootedObject cg(ctx, JS::CurrentGlobalOrNull(ctx));
JS::RootedValue r_rval(ctx, rval);
JS::CompileOptions options(ctx);
@ -431,14 +432,17 @@ spidermonkey_eval_stringback(struct ecmascript_interpreter *interpreter,
done_heartbeat(interpreter->heartbeat);
if (ret == false) {
return NULL;
result = NULL;
}
if (r_rval.isNullOrUndefined()) {
else if (r_rval.isNullOrUndefined()) {
/* Undefined value. */
return NULL;
result = NULL;
} else {
result = stracpy(JS_EncodeString(ctx, r_rval.toString()));
}
return stracpy(JS_EncodeString(ctx, r_rval.toString()));
JS_LeaveCompartment(ctx, comp);
JS_EndRequest(ctx);
return result;
}
@ -449,12 +453,16 @@ spidermonkey_eval_boolback(struct ecmascript_interpreter *interpreter,
JSContext *ctx;
JS::Value rval;
int ret;
int result = 0;
assert(interpreter);
if (!js_module_init_ok) return 0;
ctx = interpreter->backend_data;
interpreter->ret = NULL;
JSCompartment *comp = JS_EnterCompartment(ctx, interpreter->ac);
JS_BeginRequest(ctx);
JS::RootedFunction fun(ctx);
JS::CompileOptions options(ctx);
@ -471,17 +479,22 @@ spidermonkey_eval_boolback(struct ecmascript_interpreter *interpreter,
done_heartbeat(interpreter->heartbeat);
if (ret == 2) { /* onClick="history.back()" */
return 0;
result = 0;
}
if (ret == false) {
return -1;
else if (ret == false) {
result = -1;
}
if (r_val.isUndefined()) {
else if (r_val.isUndefined()) {
/* Undefined value. */
return -1;
result = -1;
} else {
result = r_val.toBoolean();
}
return r_val.toBoolean();
JS_LeaveCompartment(ctx, comp);
JS_EndRequest(ctx);
return result;
}
struct module spidermonkey_module = struct_module(

View File

@ -10,6 +10,7 @@
#include "elinks.h"
#include "ecmascript/ecmascript.h"
#include "ecmascript/spidermonkey/util.h"
#include <jsfriendapi.h>
@ -70,15 +71,20 @@ document_get_property_cookie(JSContext *ctx, unsigned int argc, JS::Value *vp)
{
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
JS::RootedObject parent_win(ctx, js::GetGlobalForObjectCrossCompartment(hobj));
JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp) {
return false;
}
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
struct view_state *vs;
struct string *cookies;
assert(JS_InstanceOf(ctx, parent_win, &window_class, NULL));
if_assert_failed return false;
vs = interpreter->vs;
vs = JS_GetInstancePrivate(ctx, parent_win,
&window_class, NULL);
if (!vs) {
return false;
}
@ -102,15 +108,19 @@ document_set_property_cookie(JSContext *ctx, unsigned int argc, JS::Value *vp)
{
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
JS::RootedObject parent_win(ctx, js::GetGlobalForObjectCrossCompartment(hobj));
JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp) {
return false;
}
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
struct view_state *vs;
struct string *cookies;
assert(JS_InstanceOf(ctx, parent_win, &window_class, NULL));
if_assert_failed return false;
vs = JS_GetInstancePrivate(ctx, parent_win,
&window_class, NULL);
vs = interpreter->vs;
if (!vs) {
return false;
}
@ -141,15 +151,20 @@ document_set_property_location(JSContext *ctx, unsigned int argc, JS::Value *vp)
{
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
JS::RootedObject parent_win(ctx, js::GetGlobalForObjectCrossCompartment(hobj));
JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp) {
return false;
}
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
struct view_state *vs;
struct document_view *doc_view;
assert(JS_InstanceOf(ctx, parent_win, &window_class, NULL));
if_assert_failed return false;
vs = interpreter->vs;
vs = JS_GetInstancePrivate(ctx, parent_win,
&window_class, NULL);
if (!vs) {
return false;
}
@ -165,17 +180,21 @@ document_get_property_referrer(JSContext *ctx, unsigned int argc, JS::Value *vp)
{
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
JS::RootedObject parent_win(ctx, js::GetGlobalForObjectCrossCompartment(hobj));
JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp) {
return false;
}
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
struct view_state *vs;
struct document_view *doc_view;
struct document *document;
struct session *ses;
assert(JS_InstanceOf(ctx, parent_win, &window_class, NULL));
if_assert_failed return false;
vs = JS_GetInstancePrivate(ctx, parent_win,
&window_class, NULL);
vs = interpreter->vs;
if (!vs) {
return false;
@ -229,16 +248,20 @@ document_get_property_title(JSContext *ctx, unsigned int argc, JS::Value *vp)
{
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
JS::RootedObject parent_win(ctx, js::GetGlobalForObjectCrossCompartment(hobj));
JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp) {
return false;
}
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
struct view_state *vs;
struct document_view *doc_view;
struct document *document;
assert(JS_InstanceOf(ctx, parent_win, &window_class, NULL));
if_assert_failed return false;
vs = interpreter->vs;
vs = JS_GetInstancePrivate(ctx, parent_win,
&window_class, NULL);
if (!vs) {
return false;
}
@ -254,19 +277,23 @@ document_set_property_title(JSContext *ctx, int argc, JS::Value *vp)
{
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
// JS::RootedObject parent_win(ctx, js::GetGlobalForObjectCrossCompartment(hobj));
JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp) {
return false;
}
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
JS::RootedObject parent_win(ctx, js::GetGlobalForObjectCrossCompartment(hobj));
struct view_state *vs;
struct document_view *doc_view;
struct document *document;
assert(JS_InstanceOf(ctx, hobj, &document_class, NULL));
if_assert_failed return false;
vs = interpreter->vs;
// assert(JS_InstanceOf(ctx, parent_win, &window_class, NULL));
// if_assert_failed return false;
vs = JS_GetInstancePrivate(ctx, hobj,
&document_class, NULL);
if (!vs || !vs->doc_view) {
return false;
}
@ -283,16 +310,20 @@ document_get_property_url(JSContext *ctx, unsigned int argc, JS::Value *vp)
{
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
JS::RootedObject parent_win(ctx, js::GetGlobalForObjectCrossCompartment(hobj));
JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp) {
return false;
}
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
struct view_state *vs;
struct document_view *doc_view;
struct document *document;
assert(JS_InstanceOf(ctx, parent_win, &window_class, NULL));
if_assert_failed return false;
vs = interpreter->vs;
vs = JS_GetInstancePrivate(ctx, parent_win,
&window_class, NULL);
if (!vs) {
return false;
}
@ -315,16 +346,19 @@ document_set_property_url(JSContext *ctx, int argc, JS::Value *vp)
{
JS::CallArgs args = CallArgsFromVp(argc, vp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
JS::RootedObject parent_win(ctx, js::GetGlobalForObjectCrossCompartment(hobj));
JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp) {
return false;
}
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
struct view_state *vs;
struct document_view *doc_view;
struct document *document;
assert(JS_InstanceOf(ctx, parent_win, &window_class, NULL));
if_assert_failed return false;
vs = JS_GetInstancePrivate(ctx, parent_win,
&window_class, NULL);
vs = interpreter->vs;
if (!vs) {
return false;
}
@ -359,18 +393,21 @@ document_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, J
struct document *document;
struct form *form;
unsigned char *string;
JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp) {
return false;
}
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
JSClass* classPtr = JS_GetClass(hobj);
if (classPtr != &document_class)
return false;
parent_win = js::GetGlobalForObjectCrossCompartment(hobj);
assert(JS_InstanceOf(ctx, parent_win, &window_class, NULL));
if_assert_failed return false;
vs = JS_GetInstancePrivate(ctx, parent_win,
&window_class, NULL);
vs = interpreter->vs;
doc_view = vs->doc_view;
document = doc_view->document;
@ -410,7 +447,6 @@ document_write_do(JSContext *ctx, unsigned int argc, JS::Value *rval, int newlin
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
JS::Value val;
// struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx);
struct string *ret = interpreter->ret;
JS::CallArgs args = JS::CallArgsFromVp(argc, rval);

File diff suppressed because it is too large Load Diff

View File

@ -190,6 +190,13 @@ location_get_property_href(JSContext *ctx, unsigned int argc, JS::Value *vp)
JS::RootedObject hobj(ctx, &args.thisv().toObject());
struct view_state *vs;
JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp) {
return false;
}
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
@ -197,12 +204,7 @@ location_get_property_href(JSContext *ctx, unsigned int argc, JS::Value *vp)
if (!JS_InstanceOf(ctx, hobj, &location_class, NULL))
return false;
JS::RootedObject parent_win(ctx, GetGlobalForObjectCrossCompartment(hobj));
assert(JS_InstanceOf(ctx, parent_win, &window_class, NULL));
if_assert_failed return false;
vs = JS_GetInstancePrivate(ctx, parent_win,
&window_class, NULL);
vs = interpreter->vs;
if (!vs) {
return false;
}
@ -227,6 +229,13 @@ location_set_property_href(JSContext *ctx, unsigned int argc, JS::Value *vp)
struct view_state *vs;
struct document_view *doc_view;
JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp) {
return false;
}
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
@ -234,12 +243,7 @@ location_set_property_href(JSContext *ctx, unsigned int argc, JS::Value *vp)
if (!JS_InstanceOf(ctx, hobj, &location_class, NULL))
return false;
JS::RootedObject parent_win(ctx, GetGlobalForObjectCrossCompartment(hobj));
assert(JS_InstanceOf(ctx, parent_win, &window_class, NULL));
if_assert_failed return false;
vs = JS_GetInstancePrivate(ctx, parent_win,
&window_class, NULL);
vs = interpreter->vs;
if (!vs) {
return;
}

View File

@ -97,6 +97,13 @@ unibar_get_property_visible(JSContext *ctx, unsigned int argc, JS::Value *vp)
struct document_view *doc_view;
struct session_status *status;
unsigned char *bar;
JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp) {
return false;
}
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
/* This can be called if @obj if not itself an instance of either
* appropriate class but has one in its prototype chain. Fail
@ -105,12 +112,7 @@ unibar_get_property_visible(JSContext *ctx, unsigned int argc, JS::Value *vp)
&& !JS_InstanceOf(ctx, hobj, &statusbar_class, NULL))
return false;
JS::RootedObject parent_win(ctx, js::GetGlobalForObjectCrossCompartment(hobj));
assert(JS_InstanceOf(ctx, parent_win, &window_class, NULL));
if_assert_failed return false;
vs = JS_GetInstancePrivate(ctx, parent_win,
&window_class, NULL);
vs = interpreter->vs;
if (!vs) {
return false;
}
@ -148,6 +150,13 @@ unibar_set_property_visible(JSContext *ctx, unsigned int argc, JS::Value *vp)
struct document_view *doc_view;
struct session_status *status;
unsigned char *bar;
JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp) {
return false;
}
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
/* This can be called if @obj if not itself an instance of either
* appropriate class but has one in its prototype chain. Fail
@ -156,12 +165,7 @@ unibar_set_property_visible(JSContext *ctx, unsigned int argc, JS::Value *vp)
&& !JS_InstanceOf(ctx, hobj, &statusbar_class, NULL))
return false;
JS::RootedObject parent_win(ctx, js::GetGlobalForObjectCrossCompartment(hobj));
assert(JS_InstanceOf(ctx, parent_win, &window_class, NULL));
if_assert_failed return false;
vs = JS_GetInstancePrivate(ctx, parent_win,
&window_class, NULL);
vs = interpreter->vs;
if (!vs) {
return false;
}

View File

@ -135,14 +135,20 @@ static bool
window_get_property(JSContext *ctx, JS::HandleObject hobj, JS::HandleId hid, JS::MutableHandleValue hvp)
{
struct view_state *vs;
JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp) {
return false;
}
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
/* This can be called if @obj if not itself an instance of the
* appropriate class but has one in its prototype chain. Fail
* such calls. */
if (!JS_InstanceOf(ctx, hobj, &window_class, NULL))
return false;
vs = JS_GetInstancePrivate(ctx, hobj, &window_class, NULL);
vs = interpreter->vs;
/* No need for special window.location measurements - when
* location is then evaluated in string context, toString()
@ -188,6 +194,13 @@ window_alert(JSContext *ctx, unsigned int argc, JS::Value *rval)
JSObject *obj = JS_THIS_OBJECT(ctx, rval);
JS::RootedObject hobj(ctx, obj);
JS::CallArgs args = JS::CallArgsFromVp(argc, rval);
JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp) {
return false;
}
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
// JS::Value *argv = JS_ARGV(ctx, rval);
struct view_state *vs;
@ -197,7 +210,7 @@ window_alert(JSContext *ctx, unsigned int argc, JS::Value *rval)
return false;
}
vs = JS_GetInstancePrivate(ctx, hobj, &window_class, nullptr);
vs = interpreter->vs;
if (argc != 1)
return true;
@ -232,10 +245,17 @@ window_open(JSContext *ctx, unsigned int argc, JS::Value *rval)
struct uri *uri;
static time_t ratelimit_start;
static int ratelimit_count;
JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp) {
return false;
}
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
if (!JS_InstanceOf(ctx, hobj, &window_class, &args)) return false;
vs = JS_GetInstancePrivate(ctx, hobj, &window_class, &args);
vs = interpreter->vs;
doc_view = vs->doc_view;
ses = doc_view->session;
@ -438,8 +458,14 @@ window_set_property_status(JSContext *ctx, unsigned int argc, JS::Value *vp)
}
JS::RootedObject hobj(ctx, &args.thisv().toObject());
JSCompartment *comp = js::GetContextCompartment(ctx);
struct view_state *vs = JS_GetInstancePrivate(ctx, hobj, &window_class, NULL);
if (!comp) {
return false;
}
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
struct view_state *vs = interpreter->vs;
if (!vs) {
return true;
@ -460,10 +486,17 @@ window_get_property_top(JSContext *ctx, unsigned int argc, JS::Value *vp)
struct document_view *doc_view;
struct document_view *top_view;
JSObject *newjsframe;
JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp) {
return false;
}
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
JS::RootedObject hobj(ctx, &args.thisv().toObject());
vs = JS_GetInstancePrivate(ctx, hobj, &window_class, NULL);
vs = interpreter->vs;
if (!vs) {
return false;