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

[js] Small progress.

It segfaults less often, but still, especially on non utf-8 pages.
And sometimes it did not work.
This commit is contained in:
Witold Filipczyk 2020-10-28 22:32:20 +01:00
parent aeeb5c69a2
commit 8ad7d68b28
8 changed files with 93 additions and 26 deletions

View File

@ -54,6 +54,7 @@ struct ecmascript_interpreter {
* to redraw. */ * to redraw. */
unsigned int onload_snippets_cache_id; unsigned int onload_snippets_cache_id;
void *ac; void *ac;
void *ar;
}; };
/* Why is the interpreter bound to {struct view_state} instead of {struct /* Why is the interpreter bound to {struct view_state} instead of {struct

View File

@ -15,7 +15,8 @@
- * object, so scripts cannot be evaluated in it. - * object, so scripts cannot be evaluated in it.
- */ - */
JSContext *spidermonkey_empty_context; JSContext *main_ctx;
/** A reference count for ::spidermonkey_runtime so that modules using /** A reference count for ::spidermonkey_runtime so that modules using
* it can be initialized and shut down in arbitrary order. */ * it can be initialized and shut down in arbitrary order. */
static int spidermonkey_runtime_refcount; static int spidermonkey_runtime_refcount;
@ -34,8 +35,15 @@ spidermonkey_runtime_addref(void)
return 0; return 0;
} }
spidermonkey_empty_context = JS_NewContext(0); main_ctx = JS_NewContext(16 * 1024 * 1024);
if (!spidermonkey_empty_context) {
if (!main_ctx) {
JS_ShutDown();
return 0;
}
if (!JS::InitSelfHostedCode(main_ctx)) {
JS_DestroyContext(main_ctx);
JS_ShutDown(); JS_ShutDown();
return 0; return 0;
} }
@ -57,7 +65,7 @@ spidermonkey_runtime_release(void)
--spidermonkey_runtime_refcount; --spidermonkey_runtime_refcount;
if (spidermonkey_runtime_refcount == 0) { if (spidermonkey_runtime_refcount == 0) {
JS_DestroyContext(spidermonkey_empty_context); JS_DestroyContext(main_ctx);
JS_ShutDown(); JS_ShutDown();
} }
} }

View File

@ -24,7 +24,7 @@
#include "util/string.h" #include "util/string.h"
extern JSRuntime *spidermonkey_runtime; extern JSRuntime *spidermonkey_runtime;
extern JSContext *spidermonkey_empty_context; extern JSContext *main_ctx;
int spidermonkey_runtime_addref(void); int spidermonkey_runtime_addref(void);
void spidermonkey_runtime_release(void); void spidermonkey_runtime_release(void);

View File

@ -141,7 +141,14 @@ PrintError(JSContext* cx, FILE* file, JS::ConstUTF8CharsZ toStringResult,
static void static void
error_reporter(JSContext *ctx, JSErrorReport *report) error_reporter(JSContext *ctx, JSErrorReport *report)
{ {
struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx); JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp) {
return;
}
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
// struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx);
struct session *ses = interpreter->vs->doc_view->session; struct session *ses = interpreter->vs->doc_view->session;
struct terminal *term; struct terminal *term;
unsigned char *strict, *exception, *warning, *error; unsigned char *strict, *exception, *warning, *error;
@ -229,25 +236,28 @@ spidermonkey_get_interpreter(struct ecmascript_interpreter *interpreter)
JSObject *document_obj, *forms_obj, *history_obj, *location_obj, JSObject *document_obj, *forms_obj, *history_obj, *location_obj,
*statusbar_obj, *menubar_obj, *navigator_obj; *statusbar_obj, *menubar_obj, *navigator_obj;
static int initialized = 0;
assert(interpreter); assert(interpreter);
if (!js_module_init_ok) return NULL; if (!js_module_init_ok) return NULL;
// ctx = JS_NewContext(JS::DefaultHeapMaxBytes, JS::DefaultNurseryBytes); ctx = main_ctx;
ctx = JS_NewContext(8L * 1024 * 1024);
if (!ctx) if (!ctx) {
return NULL; return nullptr;
}
interpreter->backend_data = ctx; interpreter->backend_data = ctx;
JSAutoRequest ar(ctx); interpreter->ar = new JSAutoRequest(ctx);
JS_SetContextPrivate(ctx, interpreter); //JSAutoRequest ar(ctx);
//JS_SetOptions(ctx, JSOPTION_VAROBJFIX | JS_METHODJIT);
// JS_SetContextPrivate(ctx, interpreter);
//JS_SetOptions(main_ctx, JSOPTION_VAROBJFIX | JS_METHODJIT);
JS::SetWarningReporter(ctx, error_reporter); JS::SetWarningReporter(ctx, error_reporter);
JS_AddInterruptCallback(ctx, heartbeat_callback); JS_AddInterruptCallback(ctx, heartbeat_callback);
JS::CompartmentOptions options; JS::CompartmentOptions options;
if (!JS::InitSelfHostedCode(ctx)) {
return NULL;
}
JS::RootedObject window_obj(ctx, JS_NewGlobalObject(ctx, &window_class, NULL, JS::FireOnNewGlobalHook, options)); JS::RootedObject window_obj(ctx, JS_NewGlobalObject(ctx, &window_class, NULL, JS::FireOnNewGlobalHook, options));
if (window_obj) { if (window_obj) {
@ -338,6 +348,8 @@ spidermonkey_get_interpreter(struct ecmascript_interpreter *interpreter)
} }
// JS_SetPrivate(navigator_obj, interpreter->vs); // JS_SetPrivate(navigator_obj, interpreter->vs);
JS_SetCompartmentPrivate(js::GetContextCompartment(ctx), interpreter);
return ctx; return ctx;
release_and_fail: release_and_fail:
@ -352,13 +364,18 @@ spidermonkey_put_interpreter(struct ecmascript_interpreter *interpreter)
assert(interpreter); assert(interpreter);
if (!js_module_init_ok) return; if (!js_module_init_ok) return;
ctx = interpreter->backend_data; ctx = interpreter->backend_data;
if (interpreter->ac) { if (interpreter->ac) {
delete (JSAutoCompartment *)interpreter->ac; delete (JSAutoCompartment *)interpreter->ac;
} }
JS_DestroyContext(ctx); if (interpreter->ar) {
delete (JSAutoRequest *)interpreter->ar;
}
// JS_DestroyContext(ctx);
interpreter->backend_data = NULL; interpreter->backend_data = NULL;
interpreter->ac = nullptr; interpreter->ac = nullptr;
interpreter->ar = nullptr;
} }

View File

@ -267,7 +267,7 @@ document_set_property_title(JSContext *ctx, int argc, JS::Value *vp)
vs = JS_GetInstancePrivate(ctx, hobj, vs = JS_GetInstancePrivate(ctx, hobj,
&document_class, NULL); &document_class, NULL);
if (!vs) { if (!vs || !vs->doc_view) {
return false; return false;
} }
doc_view = vs->doc_view; doc_view = vs->doc_view;
@ -401,8 +401,15 @@ const spidermonkeyFunctionSpec document_funcs[] = {
static bool static bool
document_write_do(JSContext *ctx, unsigned int argc, JS::Value *rval, int newline) document_write_do(JSContext *ctx, unsigned int argc, JS::Value *rval, int newline)
{ {
JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp) {
return false;
}
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
JS::Value val; JS::Value val;
struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx); // struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx);
struct string *ret = interpreter->ret; struct string *ret = interpreter->ret;
JS::CallArgs args = JS::CallArgsFromVp(argc, rval); JS::CallArgs args = JS::CallArgsFromVp(argc, rval);

View File

@ -36,7 +36,14 @@ static struct itimerval heartbeat_timer = { { 1, 0 }, { 1, 0 } };
bool bool
heartbeat_callback(JSContext *ctx) heartbeat_callback(JSContext *ctx)
{ {
struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx); JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp) {
return true;
}
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
// struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx);
if (!interpreter->heartbeat || interpreter->heartbeat->ttl > 0) { if (!interpreter->heartbeat || interpreter->heartbeat->ttl > 0) {
return true; return true;

View File

@ -73,7 +73,13 @@ const spidermonkeyFunctionSpec history_funcs[] = {
static bool static bool
history_back(JSContext *ctx, unsigned int argc, JS::Value *rval) history_back(JSContext *ctx, unsigned int argc, JS::Value *rval)
{ {
struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx); JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp) {
return false;
}
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
struct document_view *doc_view = interpreter->vs->doc_view; struct document_view *doc_view = interpreter->vs->doc_view;
struct session *ses = doc_view->session; struct session *ses = doc_view->session;
JS::CallArgs args = JS::CallArgsFromVp(argc, rval); JS::CallArgs args = JS::CallArgsFromVp(argc, rval);
@ -92,7 +98,14 @@ history_back(JSContext *ctx, unsigned int argc, JS::Value *rval)
static bool static bool
history_forward(JSContext *ctx, unsigned int argc, JS::Value *rval) history_forward(JSContext *ctx, unsigned int argc, JS::Value *rval)
{ {
struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx); JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp) {
return false;
}
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
// struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx);
struct document_view *doc_view = interpreter->vs->doc_view; struct document_view *doc_view = interpreter->vs->doc_view;
struct session *ses = doc_view->session; struct session *ses = doc_view->session;
JS::CallArgs args = JS::CallArgsFromVp(argc, rval); JS::CallArgs args = JS::CallArgsFromVp(argc, rval);
@ -107,7 +120,14 @@ history_forward(JSContext *ctx, unsigned int argc, JS::Value *rval)
static bool static bool
history_go(JSContext *ctx, unsigned int argc, JS::Value *rval) history_go(JSContext *ctx, unsigned int argc, JS::Value *rval)
{ {
struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx); JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp) {
return false;
}
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
// struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx);
struct document_view *doc_view = interpreter->vs->doc_view; struct document_view *doc_view = interpreter->vs->doc_view;
struct session *ses = doc_view->session; struct session *ses = doc_view->session;
JS::CallArgs args = JS::CallArgsFromVp(argc, rval); JS::CallArgs args = JS::CallArgsFromVp(argc, rval);

View File

@ -427,9 +427,16 @@ end:
static bool static bool
window_setTimeout(JSContext *ctx, unsigned int argc, JS::Value *rval) window_setTimeout(JSContext *ctx, unsigned int argc, JS::Value *rval)
{ {
JSCompartment *comp = js::GetContextCompartment(ctx);
if (!comp) {
return false;
}
struct ecmascript_interpreter *interpreter = JS_GetCompartmentPrivate(comp);
JS::CallArgs args = JS::CallArgsFromVp(argc, rval); JS::CallArgs args = JS::CallArgsFromVp(argc, rval);
// JS::Value *argv = JS_ARGV(ctx, rval); // struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx);
struct ecmascript_interpreter *interpreter = JS_GetContextPrivate(ctx);
unsigned char *code; unsigned char *code;
int timeout; int timeout;