|
| 1 | +#include "custom-event.h" |
| 2 | + |
| 3 | +namespace builtins { |
| 4 | +namespace web { |
| 5 | +namespace event { |
| 6 | + |
| 7 | +const JSFunctionSpec CustomEvent::static_methods[] = { |
| 8 | + JS_FS_END, |
| 9 | +}; |
| 10 | + |
| 11 | +const JSPropertySpec CustomEvent::static_properties[] = { |
| 12 | + JS_PS_END, |
| 13 | +}; |
| 14 | + |
| 15 | +const JSFunctionSpec CustomEvent::methods[] = { |
| 16 | + JS_FS_END, |
| 17 | +}; |
| 18 | + |
| 19 | +const JSPropertySpec CustomEvent::properties[] = { |
| 20 | + JS_PSG("detail", CustomEvent::detail_get, JSPROP_ENUMERATE), |
| 21 | + JS_STRING_SYM_PS(toStringTag, "CustomEvent", JSPROP_READONLY), |
| 22 | + JS_PS_END, |
| 23 | +}; |
| 24 | + |
| 25 | +bool CustomEvent::detail_get(JSContext *cx, unsigned argc, JS::Value *vp) { |
| 26 | + METHOD_HEADER(0); |
| 27 | + // TODO: Change this class so that its prototype isn't an instance of the class |
| 28 | + if (self == proto_obj) { |
| 29 | + return api::throw_error(cx, api::Errors::WrongReceiver, "name get", "CustomEvent"); |
| 30 | + } |
| 31 | + |
| 32 | + args.rval().set(JS::GetReservedSlot(self, Slots::Detail)); |
| 33 | + return true; |
| 34 | +} |
| 35 | + |
| 36 | +// https://dom.spec.whatwg.org/#interface-customevent |
| 37 | +bool CustomEvent::constructor(JSContext *cx, unsigned argc, JS::Value *vp) { |
| 38 | + CTOR_HEADER("CustomEvent", 2); |
| 39 | + |
| 40 | + RootedValue type(cx, args.get(0)); |
| 41 | + RootedValue opts(cx, args.get(1)); |
| 42 | + RootedValue detail(cx); |
| 43 | + |
| 44 | + RootedObject self(cx, JS_NewObjectForConstructor(cx, &class_, args)); |
| 45 | + if (!self) { |
| 46 | + return false; |
| 47 | + } |
| 48 | + |
| 49 | + if (!Event::init(cx, self, type, opts)) { |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + if (opts.isObject()) { |
| 54 | + JS::RootedObject obj(cx, &opts.toObject()); |
| 55 | + if (!JS_GetProperty(cx, obj, "detail", &detail)) { |
| 56 | + return false; |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + SetReservedSlot(self, Slots::Detail, detail); |
| 61 | + |
| 62 | + args.rval().setObject(*self); |
| 63 | + return true; |
| 64 | +} |
| 65 | + |
| 66 | +bool CustomEvent::init_class(JSContext *cx, JS::HandleObject global) { |
| 67 | + Event::register_subclass(&class_); |
| 68 | + return init_class_impl(cx, global, Event::proto_obj); |
| 69 | +} |
| 70 | + |
| 71 | +} // namespace event |
| 72 | +} // namespace web |
| 73 | +} // namespace builtins |
0 commit comments