From 7c618d3a6f71684a11efc4998429bec289880c72 Mon Sep 17 00:00:00 2001 From: Witold Filipczyk Date: Thu, 6 May 2021 15:40:17 +0200 Subject: [PATCH] [js] location.origin --- src/ecmascript/spidermonkey/location.c | 40 ++++++++++++++++++++++++++ test/ecmascript/location.html | 1 + 2 files changed, 41 insertions(+) diff --git a/src/ecmascript/spidermonkey/location.c b/src/ecmascript/spidermonkey/location.c index e8fdf705..5c57d5a5 100644 --- a/src/ecmascript/spidermonkey/location.c +++ b/src/ecmascript/spidermonkey/location.c @@ -156,6 +156,7 @@ history_go(JSContext *ctx, unsigned int argc, JS::Value *rval) static bool location_get_property_href(JSContext *ctx, unsigned int argc, JS::Value *vp); static bool location_set_property_href(JSContext *ctx, unsigned int argc, JS::Value *vp); +static bool location_get_property_origin(JSContext *ctx, unsigned int argc, JS::Value *vp); static bool location_get_property_pathname(JSContext *ctx, unsigned int argc, JS::Value *vp); static bool location_set_property_pathname(JSContext *ctx, unsigned int argc, JS::Value *vp); static bool location_get_property_port(JSContext *ctx, unsigned int argc, JS::Value *vp); @@ -186,6 +187,7 @@ enum location_prop { }; JSPropertySpec location_props[] = { JS_PSGS("href", location_get_property_href, location_set_property_href, JSPROP_ENUMERATE), + JS_PSG("origin", location_get_property_origin, JSPROP_ENUMERATE), JS_PSGS("pathname", location_get_property_pathname, location_set_property_pathname, JSPROP_ENUMERATE), JS_PSGS("port", location_get_property_port, location_set_property_port, JSPROP_ENUMERATE), JS_PSGS("protocol", location_get_property_protocol, location_set_property_protocol, JSPROP_ENUMERATE), @@ -232,6 +234,44 @@ location_get_property_href(JSContext *ctx, unsigned int argc, JS::Value *vp) return true; } +static bool +location_get_property_origin(JSContext *ctx, unsigned int argc, JS::Value *vp) +{ + JS::CallArgs args = CallArgsFromVp(argc, 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 + * such calls. */ + if (!JS_InstanceOf(ctx, hobj, &location_class, NULL)) + return false; + + vs = interpreter->vs; + if (!vs) { + return false; + } + + char *str = get_uri_string(vs->uri, URI_SERVER); + + if (!str) { + return false; + } + + args.rval().setString(JS_NewStringCopyZ(ctx, str)); + mem_free(str); + + return true; +} + static bool location_get_property_pathname(JSContext *ctx, unsigned int argc, JS::Value *vp) { diff --git a/test/ecmascript/location.html b/test/ecmascript/location.html index 59bfaec0..333e01c0 100644 --- a/test/ecmascript/location.html +++ b/test/ecmascript/location.html @@ -1,3 +1,4 @@ +