Skip to content

Commit 618d248

Browse files
committed
Implement EventTarget builtin
1 parent cd72251 commit 618d248

File tree

11 files changed

+1181
-0
lines changed

11 files changed

+1181
-0
lines changed

builtins/web/event/event-target.cpp

Lines changed: 518 additions & 0 deletions
Large diffs are not rendered by default.

builtins/web/event/event-target.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#ifndef BUILTINS_WEB_EVENT_TARGET_H_
2+
#define BUILTINS_WEB_EVENT_TARGET_H_
3+
4+
#include "builtin.h"
5+
#include "extension-api.h"
6+
7+
namespace builtins {
8+
namespace web {
9+
namespace event {
10+
11+
struct EventListener {
12+
void trace(JSTracer *trc) {
13+
TraceEdge(trc, &callback, "EventListener callback");
14+
TraceEdge(trc, &signal, "EventListener signal");
15+
}
16+
17+
JS::Heap<JS::Value> callback;
18+
JS::Heap<JS::Value> signal;
19+
20+
std::string type;
21+
22+
bool passive;
23+
bool capture;
24+
bool once;
25+
bool removed;
26+
27+
// Define equality: only callback, type, and capture matter.
28+
bool operator==(const EventListener &other) const {
29+
return (callback == other.callback) && (type == other.type) && (capture == other.capture);
30+
}
31+
};
32+
33+
class EventTarget : public TraceableBuiltinImpl<EventTarget> {
34+
static bool addEventListener(JSContext *cx, unsigned argc, JS::Value *vp);
35+
static bool removeEventListener(JSContext *cx, unsigned argc, JS::Value *vp);
36+
static bool dispatchEvent(JSContext *cx, unsigned argc, JS::Value *vp);
37+
38+
using ListenerList = JS::GCVector<EventListener, 0, js::SystemAllocPolicy>;
39+
static ListenerList *listeners(JSObject *self);
40+
41+
static bool dispatch(JSContext *cx, HandleObject self, HandleObject event,
42+
HandleObject target_override, MutableHandleValue rval);
43+
44+
static bool inner_invoke(JSContext *cx, HandleObject event, JS::HandleVector<EventListener> list,
45+
bool *found);
46+
47+
static bool invoke_listeners(JSContext *cx, HandleObject target, HandleObject event);
48+
49+
public:
50+
static constexpr const char *class_name = "EventTarget";
51+
static constexpr unsigned ctor_length = 0;
52+
53+
static const JSFunctionSpec static_methods[];
54+
static const JSPropertySpec static_properties[];
55+
static const JSFunctionSpec methods[];
56+
static const JSPropertySpec properties[];
57+
58+
enum Slots { Listeners, Count };
59+
60+
static bool init_class(JSContext *cx, HandleObject global);
61+
static bool constructor(JSContext *cx, unsigned argc, Value *vp);
62+
static void finalize(JS::GCContext *gcx, JSObject *self);
63+
static void trace(JSTracer *trc, JSObject *self);
64+
};
65+
66+
} // namespace event
67+
} // namespace web
68+
} // namespace builtins
69+
70+
#endif // BUILTINS_WEB_EVENT_TARGET_H_

0 commit comments

Comments
 (0)