1
0
mirror of https://github.com/rkd77/elinks.git synced 2024-09-30 03:26:23 -04:00

[quickjs] location

This commit is contained in:
Witold Filipczyk 2023-09-25 14:35:53 +02:00
parent c9359669bd
commit 637bcb725c
5 changed files with 75 additions and 5 deletions

View File

@ -533,10 +533,7 @@ js_document_get_property_location(JSContext *ctx, JSValueConst this_val)
fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__); fprintf(stderr, "%s:%s\n", __FILE__, __FUNCTION__);
#endif #endif
REF_JS(this_val); REF_JS(this_val);
JSValue ret = getLocation(ctx);
struct ecmascript_interpreter *interpreter = (struct ecmascript_interpreter *)JS_GetContextOpaque(ctx);
JSValue ret = JS_DupValue(ctx, interpreter->location_obj);
RETURN_JS(ret); RETURN_JS(ret);
} }

View File

@ -647,3 +647,25 @@ js_location_init(JSContext *ctx)
RETURN_JS(location_proto); 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);
}

View File

@ -19,6 +19,7 @@
#include "ecmascript/quickjs.h" #include "ecmascript/quickjs.h"
#include "ecmascript/quickjs/heartbeat.h" #include "ecmascript/quickjs/heartbeat.h"
#include "ecmascript/quickjs/keyboard.h" #include "ecmascript/quickjs/keyboard.h"
#include "ecmascript/quickjs/location.h"
#include "ecmascript/quickjs/message.h" #include "ecmascript/quickjs/message.h"
#include "ecmascript/quickjs/window.h" #include "ecmascript/quickjs/window.h"
#include "ecmascript/timer.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); 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 static JSValue
js_window_get_property_parent(JSContext *ctx, JSValueConst this_val) 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[] = { static const JSCFunctionListEntry js_window_proto_funcs[] = {
JS_CGETSET_DEF("closed", js_window_get_property_closed, NULL), JS_CGETSET_DEF("closed", js_window_get_property_closed, NULL),
JS_CGETSET_DEF("event", js_window_get_property_event, 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("parent", js_window_get_property_parent, NULL),
JS_CGETSET_DEF("self", js_window_get_property_self, 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), JS_CGETSET_DEF("status", js_window_get_property_status, js_window_set_property_status),

View File

@ -184,7 +184,6 @@ quickjs_get_interpreter(struct ecmascript_interpreter *interpreter)
js_xhr_init(ctx); js_xhr_init(ctx);
interpreter->document_obj = js_document_init(ctx); interpreter->document_obj = js_document_init(ctx);
interpreter->location_obj = js_location_init(ctx);
return ctx; return ctx;
} }

View File

@ -8,6 +8,7 @@ extern "C" {
#endif #endif
JSValue js_location_init(JSContext *ctx); JSValue js_location_init(JSContext *ctx);
JSValue getLocation(JSContext *ctx);
#ifdef __cplusplus #ifdef __cplusplus
} }