Skip to content

Commit 7dffa13

Browse files
Jake ChampionJakeChampion
authored andcommitted
extract WorkerLocation namespace into its own files
1 parent a54a137 commit 7dffa13

File tree

3 files changed

+93
-79
lines changed

3 files changed

+93
-79
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include "builtin.h"
2+
#include "builtins/url.h"
3+
#include "js-compute-builtins.h"
4+
5+
/**
6+
* The `WorkerLocation` builtin, added to the global object as the data property
7+
* `location`.
8+
* https://html.spec.whatwg.org/multipage/workers.html#worker-locations
9+
*/
10+
namespace WorkerLocation {
11+
namespace Slots {
12+
enum { Count };
13+
};
14+
15+
static JS::PersistentRooted<JSObject *> url;
16+
17+
const unsigned ctor_length = 1;
18+
19+
bool constructor(JSContext *cx, unsigned argc, JS::Value *vp) {
20+
JS_ReportErrorLatin1(cx, "Illegal constructor WorkerLocation");
21+
return false;
22+
}
23+
24+
bool check_receiver(JSContext *cx, JS::HandleValue receiver, const char *method_name);
25+
26+
#define ACCESSOR_GET(field) \
27+
bool field##_get(JSContext *cx, unsigned argc, JS::Value *vp) { \
28+
METHOD_HEADER(0) \
29+
REQUEST_HANDLER_ONLY("location." #field) \
30+
return URL::field(cx, url, args.rval()); \
31+
}
32+
33+
ACCESSOR_GET(href)
34+
ACCESSOR_GET(origin)
35+
ACCESSOR_GET(protocol)
36+
ACCESSOR_GET(host)
37+
ACCESSOR_GET(hostname)
38+
ACCESSOR_GET(port)
39+
ACCESSOR_GET(pathname)
40+
ACCESSOR_GET(search)
41+
ACCESSOR_GET(hash)
42+
43+
#undef ACCESSOR_GET
44+
45+
bool toString(JSContext *cx, unsigned argc, JS::Value *vp) {
46+
METHOD_HEADER(0)
47+
return href_get(cx, argc, vp);
48+
}
49+
50+
const JSFunctionSpec methods[] = {JS_FN("toString", toString, 0, JSPROP_ENUMERATE), JS_FS_END};
51+
52+
const JSPropertySpec properties[] = {JS_PSG("href", href_get, JSPROP_ENUMERATE),
53+
JS_PSG("origin", origin_get, JSPROP_ENUMERATE),
54+
JS_PSG("protocol", protocol_get, JSPROP_ENUMERATE),
55+
JS_PSG("host", host_get, JSPROP_ENUMERATE),
56+
JS_PSG("hostname", hostname_get, JSPROP_ENUMERATE),
57+
JS_PSG("port", port_get, JSPROP_ENUMERATE),
58+
JS_PSG("pathname", pathname_get, JSPROP_ENUMERATE),
59+
JS_PSG("search", search_get, JSPROP_ENUMERATE),
60+
JS_PSG("hash", hash_get, JSPROP_ENUMERATE),
61+
JS_STRING_SYM_PS(toStringTag, "Location", JSPROP_READONLY),
62+
JS_PS_END};
63+
64+
CLASS_BOILERPLATE_CUSTOM_INIT(WorkerLocation)
65+
66+
bool init_class(JSContext *cx, JS::HandleObject global) {
67+
if (!init_class_impl(cx, global)) {
68+
return false;
69+
}
70+
71+
url.init(cx);
72+
73+
JS::RootedObject location(cx, JS_NewObjectWithGivenProto(cx, &class_, proto_obj));
74+
if (!location) {
75+
return false;
76+
}
77+
78+
return JS_DefineProperty(cx, global, "location", location, JSPROP_ENUMERATE);
79+
}
80+
} // namespace WorkerLocation
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef JS_COMPUTE_RUNTIME_WROKER_LOCATION_H
2+
#define JS_COMPUTE_RUNTIME_WROKER_LOCATION_H
3+
4+
#include "builtin.h"
5+
6+
namespace WorkerLocation {
7+
JS::PersistentRooted<JSObject *> url;
8+
// Register the class.
9+
bool init_class(JSContext *cx, JS::HandleObject global);
10+
} // namespace WorkerLocation
11+
12+
#endif

c-dependencies/js-compute-runtime/js-compute-builtins.cpp

Lines changed: 1 addition & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "builtins/transform-stream-default-controller.h"
4848
#include "builtins/transform-stream.h"
4949
#include "builtins/url.h"
50+
#include "builtins/worker-location.h"
5051

5152
using JS::CallArgs;
5253
using JS::CallArgsFromVp;
@@ -3217,10 +3218,6 @@ CLASS_BOILERPLATE_NO_CTOR(ClientInfo)
32173218
JSObject *create(JSContext *cx) { return JS_NewObjectWithGivenProto(cx, &class_, proto_obj); }
32183219
} // namespace ClientInfo
32193220

3220-
namespace WorkerLocation {
3221-
static PersistentRooted<JSObject *> url;
3222-
}
3223-
32243221
namespace FetchEvent {
32253222
namespace Slots {
32263223
enum {
@@ -3626,81 +3623,6 @@ bool response_started(JSObject *self) {
36263623
}
36273624
} // namespace FetchEvent
36283625

3629-
/**
3630-
* The `WorkerLocation` builtin, added to the global object as the data property
3631-
* `location`.
3632-
* https://html.spec.whatwg.org/multipage/workers.html#worker-locations
3633-
*/
3634-
namespace WorkerLocation {
3635-
namespace Slots {
3636-
enum { Count };
3637-
};
3638-
3639-
const unsigned ctor_length = 1;
3640-
3641-
bool constructor(JSContext *cx, unsigned argc, Value *vp) {
3642-
JS_ReportErrorLatin1(cx, "Illegal constructor WorkerLocation");
3643-
return false;
3644-
}
3645-
3646-
bool check_receiver(JSContext *cx, HandleValue receiver, const char *method_name);
3647-
3648-
#define ACCESSOR_GET(field) \
3649-
bool field##_get(JSContext *cx, unsigned argc, Value *vp) { \
3650-
METHOD_HEADER(0) \
3651-
REQUEST_HANDLER_ONLY("location." #field) \
3652-
return URL::field(cx, url, args.rval()); \
3653-
}
3654-
3655-
ACCESSOR_GET(href)
3656-
ACCESSOR_GET(origin)
3657-
ACCESSOR_GET(protocol)
3658-
ACCESSOR_GET(host)
3659-
ACCESSOR_GET(hostname)
3660-
ACCESSOR_GET(port)
3661-
ACCESSOR_GET(pathname)
3662-
ACCESSOR_GET(search)
3663-
ACCESSOR_GET(hash)
3664-
3665-
#undef ACCESSOR_GET
3666-
3667-
bool toString(JSContext *cx, unsigned argc, Value *vp) {
3668-
METHOD_HEADER(0)
3669-
return href_get(cx, argc, vp);
3670-
}
3671-
3672-
const JSFunctionSpec methods[] = {JS_FN("toString", toString, 0, JSPROP_ENUMERATE), JS_FS_END};
3673-
3674-
const JSPropertySpec properties[] = {JS_PSG("href", href_get, JSPROP_ENUMERATE),
3675-
JS_PSG("origin", origin_get, JSPROP_ENUMERATE),
3676-
JS_PSG("protocol", protocol_get, JSPROP_ENUMERATE),
3677-
JS_PSG("host", host_get, JSPROP_ENUMERATE),
3678-
JS_PSG("hostname", hostname_get, JSPROP_ENUMERATE),
3679-
JS_PSG("port", port_get, JSPROP_ENUMERATE),
3680-
JS_PSG("pathname", pathname_get, JSPROP_ENUMERATE),
3681-
JS_PSG("search", search_get, JSPROP_ENUMERATE),
3682-
JS_PSG("hash", hash_get, JSPROP_ENUMERATE),
3683-
JS_STRING_SYM_PS(toStringTag, "Location", JSPROP_READONLY),
3684-
JS_PS_END};
3685-
3686-
CLASS_BOILERPLATE_CUSTOM_INIT(WorkerLocation)
3687-
3688-
bool init_class(JSContext *cx, HandleObject global) {
3689-
if (!init_class_impl(cx, global)) {
3690-
return false;
3691-
}
3692-
3693-
url.init(cx);
3694-
3695-
RootedObject location(cx, JS_NewObjectWithGivenProto(cx, &class_, proto_obj));
3696-
if (!location) {
3697-
return false;
3698-
}
3699-
3700-
return JS_DefineProperty(cx, global, "location", location, JSPROP_ENUMERATE);
3701-
}
3702-
} // namespace WorkerLocation
3703-
37043626
namespace GlobalProperties {
37053627
// TODO: throw in all Request methods/getters that rely on host calls once a
37063628
// request has been sent. The host won't let us act on them anymore anyway.

0 commit comments

Comments
 (0)