diff --git a/src/ecmascript/libdom/quickjs/document.c b/src/ecmascript/libdom/quickjs/document.c index 2d6aea6d..c5b29617 100644 --- a/src/ecmascript/libdom/quickjs/document.c +++ b/src/ecmascript/libdom/quickjs/document.c @@ -533,10 +533,7 @@ js_document_get_property_location(JSContext *ctx, JSValueConst this_val) fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); #endif REF_JS(this_val); - - struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS_GetContextOpaque(ctx); - - JSValue ret = JS_DupValue(ctx, interpreter->location_obj); + JSValue ret = getLocation(ctx); RETURN_JS(ret); } diff --git a/src/ecmascript/libdom/quickjs/location.c b/src/ecmascript/libdom/quickjs/location.c index 60627041..b08cb11e 100644 --- a/src/ecmascript/libdom/quickjs/location.c +++ b/src/ecmascript/libdom/quickjs/location.c @@ -647,3 +647,25 @@ js_location_init(JSContext *ctx) RETURN_JS(location_proto); } + +JSValue +getLocation(JSContext *ctx) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + static int initialized; + + if (!initialized) { + JS_NewClassID(&js_location_class_id); + JS_NewClass(JS_GetRuntime(ctx), js_location_class_id, &js_location_class); + initialized = 1; + } + JSValue location_obj = JS_NewObjectClass(ctx, js_location_class_id); + REF_JS(location_obj); + JS_SetPropertyFunctionList(ctx, location_obj, js_location_proto_funcs, countof(js_location_proto_funcs)); + JS_SetClassProto(ctx, js_location_class_id, location_obj); + + JSValue rr = JS_DupValue(ctx, location_obj); + RETURN_JS(rr); +} diff --git a/src/ecmascript/libdom/quickjs/window.c b/src/ecmascript/libdom/quickjs/window.c index febc4a3b..318de274 100644 --- a/src/ecmascript/libdom/quickjs/window.c +++ b/src/ecmascript/libdom/quickjs/window.c @@ -19,6 +19,7 @@ #include "ecmascript/quickjs.h" #include "ecmascript/quickjs/heartbeat.h" #include "ecmascript/quickjs/keyboard.h" +#include "ecmascript/quickjs/location.h" #include "ecmascript/quickjs/message.h" #include "ecmascript/quickjs/window.h" #include "ecmascript/timer.h" @@ -346,6 +347,55 @@ js_window_get_property_event(JSContext *ctx, JSValueConst this_val) return get_keyboardEvent(ctx, &last_event); } +static JSValue +js_window_get_property_location(JSContext *ctx, JSValueConst this_val) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + REF_JS(this_val); + JSValue ret = getLocation(ctx); + + RETURN_JS(ret); +} + +static JSValue +js_window_set_property_location(JSContext *ctx, JSValueConst this_val, JSValue val) +{ +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); +#endif + REF_JS(this_val); + REF_JS(val); + + struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS_GetContextOpaque(ctx); + struct view_state *vs; + struct document_view *doc_view; + vs = interpreter->vs; + + if (!vs) { +#ifdef ECMASCRIPT_DEBUG + fprintf(stderr, "%s:%s %d\n", __FILE__, __FUNCTION__, __LINE__); +#endif + return JS_NULL; + } + doc_view = vs->doc_view; + const char *url; + size_t len; + + url = JS_ToCStringLen(ctx, &len, val); + + if (!url) { + return JS_EXCEPTION; + } + + location_goto_const(doc_view, url); + JS_FreeCString(ctx, url); + + return JS_UNDEFINED; +} + + static JSValue js_window_get_property_parent(JSContext *ctx, JSValueConst this_val) { @@ -725,6 +775,7 @@ js_window_postMessage(JSContext *ctx, JSValueConst this_val, int argc, JSValueCo static const JSCFunctionListEntry js_window_proto_funcs[] = { JS_CGETSET_DEF("closed", js_window_get_property_closed, NULL), JS_CGETSET_DEF("event", js_window_get_property_event, NULL), + JS_CGETSET_DEF("location", js_window_get_property_location, js_window_set_property_location), JS_CGETSET_DEF("parent", js_window_get_property_parent, NULL), JS_CGETSET_DEF("self", js_window_get_property_self, NULL), JS_CGETSET_DEF("status", js_window_get_property_status, js_window_set_property_status), diff --git a/src/ecmascript/quickjs.cpp b/src/ecmascript/quickjs.cpp index 8cd41c0c..ef19b443 100644 --- a/src/ecmascript/quickjs.cpp +++ b/src/ecmascript/quickjs.cpp @@ -184,7 +184,6 @@ quickjs_get_interpreter(struct ecmascript_interpreter *interpreter) js_xhr_init(ctx); interpreter->document_obj = js_document_init(ctx); - interpreter->location_obj = js_location_init(ctx); return ctx; } diff --git a/src/ecmascript/quickjs/location.h b/src/ecmascript/quickjs/location.h index 840361d1..1127f4f1 100644 --- a/src/ecmascript/quickjs/location.h +++ b/src/ecmascript/quickjs/location.h @@ -8,6 +8,7 @@ extern "C" { #endif JSValue js_location_init(JSContext *ctx); +JSValue getLocation(JSContext *ctx); #ifdef __cplusplus }