diff --git a/src/ecmascript/ecmascript.c b/src/ecmascript/ecmascript.c index 2635aa7b..2e92951c 100644 --- a/src/ecmascript/ecmascript.c +++ b/src/ecmascript/ecmascript.c @@ -336,10 +336,15 @@ ecmascript_eval(struct ecmascript_interpreter *interpreter, interpreter->backend_nesting--; } -#ifdef CONFIG_ECMASCRIPT_SMJS +#ifdef CONFIG_QUICKJS +static void +ecmascript_call_function(struct ecmascript_interpreter *interpreter, + JSValueConst fun, struct string *ret) +#else static void ecmascript_call_function(struct ecmascript_interpreter *interpreter, JS::HandleValue fun, struct string *ret) +#endif { if (!get_ecmascript_enable(interpreter)) return; @@ -352,7 +357,6 @@ ecmascript_call_function(struct ecmascript_interpreter *interpreter, #endif interpreter->backend_nesting--; } -#endif char * ecmascript_eval_stringback(struct ecmascript_interpreter *interpreter, @@ -556,7 +560,7 @@ ecmascript_timeout_handler(void *i) check_for_rerender(interpreter, "handler"); } -#ifdef CONFIG_ECMASCRIPT_SMJS +#if defined(CONFIG_ECMASCRIPT_SMJS) || defined(CONFIG_QUICKJS) /* Timer callback for @interpreter->vs->doc_view->document->timeout. * As explained in @install_timer, this function must erase the * expired timer ID from all variables. */ @@ -605,17 +609,14 @@ ecmascript_set_timeout2(struct ecmascript_interpreter *interpreter, JS::HandleVa #ifdef CONFIG_QUICKJS void -ecmascript_set_timeout2q(struct ecmascript_interpreter *interpreter, JSValue f, int timeout) +ecmascript_set_timeout2q(struct ecmascript_interpreter *interpreter, JSValueConst fun, int timeout) { -#if 0 assert(interpreter && interpreter->vs->doc_view->document); done_string(&interpreter->code); init_string(&interpreter->code); kill_timer(&interpreter->vs->doc_view->document->timeout); - JS::RootedValue fun((JSContext *)interpreter->backend_data, f); interpreter->fun = fun; install_timer(&interpreter->vs->doc_view->document->timeout, timeout, ecmascript_timeout_handler2, interpreter); -#endif } #endif diff --git a/src/ecmascript/ecmascript.h b/src/ecmascript/ecmascript.h index f206d735..3d36e12e 100644 --- a/src/ecmascript/ecmascript.h +++ b/src/ecmascript/ecmascript.h @@ -82,7 +82,9 @@ struct ecmascript_interpreter { void *document_obj; void *location_obj; #endif -#ifdef CONFIG_ECMASCRIPT_SMJS +#ifdef CONFIG_QUICKJS + JSValueConst fun; +#else JS::RootedValue fun; #endif bool changed; diff --git a/src/ecmascript/quickjs.c b/src/ecmascript/quickjs.c index 257e37b7..07c328ef 100644 --- a/src/ecmascript/quickjs.c +++ b/src/ecmascript/quickjs.c @@ -382,33 +382,26 @@ quickjs_eval(struct ecmascript_interpreter *interpreter, done_heartbeat(interpreter->heartbeat); } -#if 0 void quickjs_call_function(struct ecmascript_interpreter *interpreter, - JS::HandleValue fun, struct string *ret) + JSValueConst fun, struct string *ret) { -#if 0 JSContext *ctx; - JS::Value rval; assert(interpreter); - if (!js_module_init_ok) { - return; - } +// if (!js_module_init_ok) { +// return; +// } ctx = interpreter->backend_data; - JS::Realm *comp = JS::EnterRealm(ctx, interpreter->ac); +// JS::Realm *comp = JS::EnterRealm(ctx, interpreter->ac); interpreter->heartbeat = add_heartbeat(interpreter); interpreter->ret = ret; - JS::RootedValue r_val(ctx, rval); - JS::RootedObject cg(ctx, JS::CurrentGlobalOrNull(ctx)); - JS_CallFunctionValue(ctx, cg, fun, JS::HandleValueArray::empty(), &r_val); + JS_Call(ctx, fun, JS_GetGlobalObject(ctx), 0, nullptr); + done_heartbeat(interpreter->heartbeat); - JS::LeaveRealm(ctx, comp); -#endif } -#endif char * quickjs_eval_stringback(struct ecmascript_interpreter *interpreter, diff --git a/src/ecmascript/quickjs.h b/src/ecmascript/quickjs.h index 865e02df..905dd394 100644 --- a/src/ecmascript/quickjs.h +++ b/src/ecmascript/quickjs.h @@ -19,7 +19,7 @@ void quickjs_eval(struct ecmascript_interpreter *interpreter, struct string *cod char *quickjs_eval_stringback(struct ecmascript_interpreter *interpreter, struct string *code); int quickjs_eval_boolback(struct ecmascript_interpreter *interpreter, struct string *code); -//void quickjs_call_function(struct ecmascript_interpreter *interpreter, JS::HandleValue fun, struct string *ret); +void quickjs_call_function(struct ecmascript_interpreter *interpreter, JSValueConst fun, struct string *ret); extern struct module quickjs_module; #endif diff --git a/src/ecmascript/quickjs/window.c b/src/ecmascript/quickjs/window.c index e3e90828..5949adc2 100644 --- a/src/ecmascript/quickjs/window.c +++ b/src/ecmascript/quickjs/window.c @@ -201,34 +201,24 @@ js_window_setTimeout(JSContext *ctx, JSValueConst this_val, int argc, JSValueCon #endif struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS_GetContextOpaque(ctx); const char *code; - int timeout = 0; + int64_t timeout = 0; + JSValueConst func; + func = argv[0]; - if (argc != 2) { - return JS_UNDEFINED; - } - - JS_ToInt32(ctx, &timeout, argv[1]); + if (!JS_IsFunction(ctx, func)) + return JS_ThrowTypeError(ctx, "not a function"); + if (JS_ToInt64(ctx, &timeout, argv[1])) + return JS_EXCEPTION; if (timeout <= 0) { return JS_UNDEFINED; } - if (JS_IsString(argv[0])) { - size_t len; - code = JS_ToCStringLen(ctx, &len, argv[0]); - - if (!code) { - return JS_EXCEPTION; - } - - ecmascript_set_timeout(interpreter, code, timeout); - return JS_UNDEFINED; - } - - ecmascript_set_timeout2q(interpreter, argv[0], timeout); + ecmascript_set_timeout2q(interpreter, func, timeout); return JS_UNDEFINED; } + static JSValue js_window_get_property_closed(JSContext *ctx, JSValueConst this_val) { diff --git a/test/ecmascript/scroll3.html b/test/ecmascript/scroll3.html index 5f6f22cf..a7be872d 100644 --- a/test/ecmascript/scroll3.html +++ b/test/ecmascript/scroll3.html @@ -8,7 +8,7 @@ function scrollText() { } function scroll() { document.getElementById('s').innerHTML = scrollText(); - setTimeout(scroll, 100); + window.setTimeout(scroll, 100); }