/
event_target.cc
469 lines (375 loc) · 18.5 KB
/
event_target.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
* Copyright (C) 2020 Alibaba Inc. All rights reserved.
* Author: Kraken Team.
*/
#include "event_target.h"
#include "dart_methods.h"
#include "document.h"
#include "event.h"
#include <codecvt>
namespace kraken::binding::jsc {
static std::atomic<int64_t> globalEventTargetId{0};
void bindEventTarget(std::unique_ptr<JSContext> &context) {
auto eventTarget = JSEventTarget::instance(context.get());
JSC_GLOBAL_SET_PROPERTY(context, "EventTarget", eventTarget->classObject);
}
std::unordered_map<JSContext *, JSEventTarget *> JSEventTarget::instanceMap{};
JSEventTarget *JSEventTarget::instance(JSContext *context) {
if (instanceMap.count(context) == 0) {
instanceMap[context] = new JSEventTarget(context, nullptr, nullptr);
}
return instanceMap[context];
}
JSEventTarget::~JSEventTarget() {
instanceMap.erase(context);
}
JSEventTarget::JSEventTarget(JSContext *context, const char *name) : HostClass(context, name) {}
JSEventTarget::JSEventTarget(JSContext *context, const JSStaticFunction *staticFunction,
const JSStaticValue *staticValue)
: HostClass(context, nullptr, "EventTarget", staticFunction, staticValue) {}
JSObjectRef JSEventTarget::instanceConstructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount,
const JSValueRef *arguments, JSValueRef *exception) {
if (argumentCount == 1) {
const JSValueRef jsOnlyEventsValueRef = arguments[0];
if (!JSValueIsArray(ctx, jsOnlyEventsValueRef)) {
throwJSError(ctx, "Failed to new Event: jsOnlyEvents is not an array.", exception);
return nullptr;
}
JSObjectRef jsOnlyEvents = JSValueToObject(ctx, jsOnlyEventsValueRef, exception);
JSStringRef lengthStr = JSStringCreateWithUTF8CString("length");
JSValueRef lengthValue = JSObjectGetProperty(ctx, jsOnlyEvents, lengthStr, exception);
size_t length = JSValueToNumber(ctx, lengthValue, exception);
for (size_t i = 0; i < length; i++) {
JSValueRef jsOnlyEvent = JSObjectGetPropertyAtIndex(ctx, jsOnlyEvents, i, exception);
std::string event = JSStringToStdString(JSValueToStringCopy(ctx, jsOnlyEvent, exception));
m_jsOnlyEvents.emplace_back(event);
}
}
auto eventTarget = new EventTargetInstance(this);
setProto(ctx, eventTarget->object, constructor, exception);
return constructor;
}
EventTargetInstance::EventTargetInstance(JSEventTarget *eventTarget) : Instance(eventTarget) {
eventTargetId = globalEventTargetId;
globalEventTargetId++;
nativeEventTarget = new NativeEventTarget(this);
}
EventTargetInstance::EventTargetInstance(JSEventTarget *eventTarget, int64_t id)
: Instance(eventTarget), eventTargetId(id) {
nativeEventTarget = new NativeEventTarget(this);
}
EventTargetInstance::~EventTargetInstance() {
// Recycle eventTarget object could be triggered by hosting JSContext been released or reference count set to 0.
foundation::UICommandBuffer::instance(_hostClass->contextId)
->addCommand(eventTargetId, UICommand::disposeEventTarget, nullptr, false);
// Release handler callbacks.
if (context->isValid()) {
for (auto &it : _eventHandlers) {
for (auto &handler : it.second) {
JSValueUnprotect(_hostClass->ctx, handler);
}
}
}
foundation::UICommandCallbackQueue::instance()->registerCallback([](void *ptr) {
delete reinterpret_cast<NativeEventTarget *>(ptr);
}, nativeEventTarget);
}
// target.addEventListener(type, listener [, options]);
// target.addEventListener(type, listener [, useCapture]);
JSValueRef JSEventTarget::addEventListener(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject,
size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception) {
if (argumentCount < 2) {
throwJSError(ctx, "Failed to addEventListener: type and listener are required.", exception);
return nullptr;
}
auto *eventTargetInstance = static_cast<EventTargetInstance *>(JSObjectGetPrivate(thisObject));
if (eventTargetInstance == nullptr) {
JSObjectRef prototypeObject = getProto(ctx, thisObject, exception);
eventTargetInstance = static_cast<EventTargetInstance *>(JSObjectGetPrivate(prototypeObject));
}
assert_m(eventTargetInstance != nullptr, "this object is not a instance of eventTarget.");
const JSValueRef eventNameValueRef = arguments[0];
const JSValueRef callback = arguments[1];
if (!JSValueIsString(ctx, eventNameValueRef)) {
throwJSError(ctx, "Failed to addEventListener: eventName should be an string.", exception);
return nullptr;
}
if (!JSValueIsObject(ctx, callback)) {
throwJSError(ctx, "Failed to addEventListener: callback should be an function.", exception);
return nullptr;
}
JSObjectRef callbackObjectRef = JSValueToObject(ctx, callback, exception);
if (!JSObjectIsFunction(ctx, callbackObjectRef)) {
throwJSError(ctx, "Failed to addEventListener: callback should be an function.", exception);
return nullptr;
}
std::string eventType = JSStringToStdString(JSValueToStringCopy(ctx, eventNameValueRef, exception));
// Init list.
if (eventTargetInstance->_eventHandlers.count(eventType) == 0) {
eventTargetInstance->_eventHandlers[eventType] = std::forward_list<JSObjectRef>();
}
JSObjectRef &propertyHandlers = eventTargetInstance->_propertyEventHandler[eventType];
// Dart needs to be notified for the first registration event.
if (eventTargetInstance->_eventHandlers[eventType].empty() || JSObjectIsFunction(ctx, propertyHandlers)) {
int32_t contextId = eventTargetInstance->_hostClass->contextId;
NativeString args_01{};
buildUICommandArgs(eventType, args_01);
auto EventTarget = reinterpret_cast<JSEventTarget *>(eventTargetInstance->_hostClass);
auto isJsOnlyEvent =
std::find(EventTarget->m_jsOnlyEvents.begin(), EventTarget->m_jsOnlyEvents.end(), eventType) != EventTarget->m_jsOnlyEvents.end();
if (!isJsOnlyEvent) {
foundation::UICommandBuffer::instance(contextId)->addCommand(
eventTargetInstance->eventTargetId, UICommand::addEvent, args_01, nullptr);
};
}
std::forward_list<JSObjectRef> &handlers = eventTargetInstance->_eventHandlers[eventType];
JSValueProtect(ctx, callbackObjectRef);
handlers.emplace_after(handlers.cbefore_begin(), callbackObjectRef);
return nullptr;
}
JSValueRef JSEventTarget::prototypeGetProperty(std::string &name, JSValueRef *exception) {
auto &propertyMap = getEventTargetPropertyMap();
if (propertyMap.count(name) > 0) {
auto &property = propertyMap[name];
switch (property) {
default:
break;
}
}
return HostClass::prototypeGetProperty(name, exception);
}
// removeEventListener(type, listener[, options]);
// removeEventListener(type, listener[, useCapture]);
JSValueRef JSEventTarget::removeEventListener(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject,
size_t argumentCount, const JSValueRef *arguments,
JSValueRef *exception) {
if (argumentCount < 2) {
throwJSError(ctx, "Failed to removeEventListener: at least type and listener are required.", exception);
return nullptr;
}
auto *eventTargetInstance = static_cast<EventTargetInstance *>(JSObjectGetPrivate(thisObject));
if (eventTargetInstance == nullptr) {
JSObjectRef prototypeObject = getProto(ctx, thisObject, exception);
eventTargetInstance = static_cast<EventTargetInstance *>(JSObjectGetPrivate(prototypeObject));
}
assert_m(eventTargetInstance != nullptr, "this object is not a instance of eventTarget.");
const JSValueRef eventNameValueRef = arguments[0];
const JSValueRef callback = arguments[1];
if (!JSValueIsString(ctx, eventNameValueRef)) {
throwJSError(ctx, "Failed to removeEventListener: eventName should be an string.", exception);
return nullptr;
}
if (!JSValueIsObject(ctx, callback)) {
throwJSError(ctx, "Failed to removeEventListener: callback should be an function.", exception);
return nullptr;
}
JSObjectRef callbackObjectRef = JSValueToObject(ctx, callback, exception);
if (!JSObjectIsFunction(ctx, callbackObjectRef)) {
throwJSError(ctx, "Failed to removeEventListener: callback should be an function.", exception);
return nullptr;
}
std::string eventType = JSStringToStdString(JSValueToStringCopy(ctx, eventNameValueRef, exception));
if (eventTargetInstance->_eventHandlers.count(eventType) == 0) {
return nullptr;
}
std::forward_list<JSObjectRef> &handlers = eventTargetInstance->_eventHandlers[eventType];
handlers.remove_if([&callbackObjectRef, &ctx](JSObjectRef function) {
if (function == callbackObjectRef) {
JSValueUnprotect(ctx, callbackObjectRef);
return true;
}
return false;
});
JSObjectRef &propertyHandlers = eventTargetInstance->_propertyEventHandler[eventType];
if (handlers.empty() && JSObjectIsFunction(ctx, propertyHandlers)) {
// Dart needs to be notified for handles is empty.
int32_t contextId = eventTargetInstance->_hostClass->contextId;
NativeString args_01{};
buildUICommandArgs(eventType, args_01);
auto EventTarget = reinterpret_cast<JSEventTarget *>(eventTargetInstance->_hostClass);
auto isJsOnlyEvent =
std::find(EventTarget->m_jsOnlyEvents.begin(), EventTarget->m_jsOnlyEvents.end(), eventType) != EventTarget->m_jsOnlyEvents.end();
if (!isJsOnlyEvent) {
foundation::UICommandBuffer::instance(contextId)->addCommand(
eventTargetInstance->eventTargetId, UICommand::removeEvent, args_01, nullptr);
};
}
return nullptr;
}
JSValueRef JSEventTarget::dispatchEvent(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject,
size_t argumentCount, const JSValueRef *arguments, JSValueRef *exception) {
if (argumentCount != 1) {
throwJSError(ctx, "Failed to dispatchEvent: first arguments should be an event object", exception);
return nullptr;
}
auto *eventTargetInstance = static_cast<EventTargetInstance *>(JSObjectGetPrivate(thisObject));
if (eventTargetInstance == nullptr) {
JSObjectRef prototypeObject = getProto(ctx, thisObject, exception);
eventTargetInstance = static_cast<EventTargetInstance *>(JSObjectGetPrivate(prototypeObject));
}
assert_m(eventTargetInstance != nullptr, "this object is not a instance of eventTarget.");
const JSValueRef eventObjectValueRef = arguments[0];
JSObjectRef eventObjectRef = JSValueToObject(ctx, eventObjectValueRef, exception);
auto eventInstance = reinterpret_cast<EventInstance *>(JSObjectGetPrivate(eventObjectRef));
return JSValueMakeBoolean(ctx, eventTargetInstance->dispatchEvent(eventInstance));
}
bool EventTargetInstance::dispatchEvent(EventInstance *event) {
std::u16string u16EventType = std::u16string(reinterpret_cast<const char16_t *>(event->nativeEvent->type->string),
event->nativeEvent->type->length);
std::string eventType = toUTF8(u16EventType);
// Modify the currentTarget to this.
event->nativeEvent->currentTarget = this;
internalDispatchEvent(event);
// Bubble event to root event target.
if (event->nativeEvent->bubbles == 1 && !event->_propagationStopped) {
auto node = reinterpret_cast<NodeInstance *>(event->nativeEvent->currentTarget);
NodeInstance* parent = node->parentNode;
if (parent != nullptr) {
parent->dispatchEvent(event);
}
}
return event->_cancelled;
}
JSValueRef JSEventTarget::clearListeners(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject,
size_t argumentCount, const JSValueRef *arguments, JSValueRef *exception) {
auto eventTargetInstance = static_cast<EventTargetInstance *>(JSObjectGetPrivate(thisObject));
assert_m(eventTargetInstance != nullptr, "this object is not a instance of eventTarget.");
for (auto &it : eventTargetInstance->_eventHandlers) {
for (auto &handler : it.second) {
JSValueUnprotect(eventTargetInstance->_hostClass->ctx, handler);
}
}
eventTargetInstance->_eventHandlers.clear();
return nullptr;
}
JSValueRef EventTargetInstance::getProperty(std::string &name, JSValueRef *exception) {
auto &propertyMap = JSEventTarget::getEventTargetPropertyMap();
auto &prototypePropertyMap = JSEventTarget::getEventTargetPrototypePropertyMap();
if (prototypePropertyMap.count(name) > 0) {
JSStringHolder nameStringHolder = JSStringHolder(context, name);
return JSObjectGetProperty(ctx, prototype<JSEventTarget>()->prototypeObject, nameStringHolder.getString(), exception);
}
if (propertyMap.count(name) > 0) {
auto &property = propertyMap[name];
switch (property) {
case JSEventTarget::EventTargetProperty::eventTargetId: {
return JSValueMakeNumber(_hostClass->ctx, eventTargetId);
}
}
} else if (name.substr(0, 2) == "on") {
return getPropertyHandler(name, exception);
}
return Instance::getProperty(name, exception);
}
bool EventTargetInstance::setProperty(std::string &name, JSValueRef value, JSValueRef *exception) {
auto &staticPropertyMap = JSEventTarget::getEventTargetPrototypePropertyMap();
if (staticPropertyMap.count(name) > 0) return false;
if (name.substr(0, 2) == "on") {
setPropertyHandler(name, value, exception);
return true;
} else {
return Instance::setProperty(name, value, exception);
}
}
JSValueRef EventTargetInstance::getPropertyHandler(std::string &name, JSValueRef *exception) {
std::string eventType = name.substr(2);
if (_propertyEventHandler.count(eventType) == 0) {
return JSValueMakeNull(ctx);
}
return _propertyEventHandler[eventType];
}
void EventTargetInstance::setPropertyHandler(std::string &name, JSValueRef value,
JSValueRef *exception) {
std::string eventType = name.substr(2);
// We need to remove previous eventHandler when setting new eventHandler with same eventType.
if (_propertyEventHandler.count(eventType) > 0) {
JSValueUnprotect(ctx, _propertyEventHandler[eventType]);
_propertyEventHandler.erase(eventType);
}
// When evaluate scripts like 'element.onclick = null', we needs to remove the event handlers callbacks
if (JSValueIsNull(ctx, value)) {
return;
}
JSObjectRef handlerObjectRef = JSValueToObject(_hostClass->ctx, value, exception);
JSValueProtect(_hostClass->ctx, handlerObjectRef);
_propertyEventHandler[eventType] = handlerObjectRef;
auto Event = reinterpret_cast<JSEventTarget *>(_hostClass);
auto isJsOnlyEvent = std::find(Event->m_jsOnlyEvents.begin(), Event->m_jsOnlyEvents.end(), name.substr(2)) !=
Event->m_jsOnlyEvents.end();
if (isJsOnlyEvent) return;
if (_eventHandlers.empty()) {
int32_t contextId = _hostClass->contextId;
NativeString args_01{};
buildUICommandArgs(eventType, args_01);
int32_t type = JSObjectIsFunction(ctx, handlerObjectRef) ? UICommand::addEvent : UICommand::removeEvent;
foundation::UICommandBuffer::instance(contextId)->addCommand(eventTargetId, type, args_01, nullptr);
}
}
void EventTargetInstance::getPropertyNames(JSPropertyNameAccumulatorRef accumulator) {
for (auto &propertyName : JSEventTarget::getEventTargetPropertyNames()) {
JSPropertyNameAccumulatorAddName(accumulator, propertyName);
}
for (auto &propertyName : JSEventTarget::getEventTargetPrototypePropertyNames()) {
JSPropertyNameAccumulatorAddName(accumulator, propertyName);
}
}
bool EventTargetInstance::internalDispatchEvent(EventInstance *eventInstance) {
std::u16string u16EventType = std::u16string(reinterpret_cast<const char16_t *>(eventInstance->nativeEvent->type->string),
eventInstance->nativeEvent->type->length);
std::string eventType = toUTF8(u16EventType);
auto stack = _eventHandlers[eventType];
// Dispatch event listeners writen by addEventListener
auto _dispatchEvent = [&eventInstance, this](JSObjectRef& handler) {
if (eventInstance->_propagationImmediatelyStopped) return;
JSValueRef exception = nullptr;
const JSValueRef arguments[] = {eventInstance->object};
// The third params `thisObject` to null equals global object.
JSObjectCallAsFunction(_hostClass->ctx, handler, nullptr, 1, arguments, &exception);
context->handleException(exception);
};
for (auto &handler : stack) {
_dispatchEvent(handler);
}
// Dispatch event listener white by 'on' prefix property.
if (_propertyEventHandler.count(eventType) > 0) {
if (eventType == "error") {
auto _dispatchErrorEvent = [&eventInstance, this, eventType](JSObjectRef &handler) {
JSValueRef exception = nullptr;
JSValueRef errorObjectValue = getObjectPropertyValue(ctx, eventType, eventInstance->object, &exception);
JSObjectRef errorObject = JSValueToObject(ctx, errorObjectValue, &exception);
JSValueRef messageValue = getObjectPropertyValue(ctx, "message", errorObject, &exception);
JSValueRef sourceURLValue = getObjectPropertyValue(ctx, "sourceURL", errorObject, &exception);
JSValueRef lineValue = getObjectPropertyValue(ctx, "line", errorObject, &exception);
JSValueRef columnValue = getObjectPropertyValue(ctx, "column", errorObject, &exception);
const JSValueRef arguments[] = {
messageValue,
sourceURLValue,
lineValue,
columnValue,
errorObjectValue
};
JSObjectCallAsFunction(_hostClass->ctx, handler, nullptr, 5, arguments, &exception);
context->handleException(exception);
};
_dispatchErrorEvent(_propertyEventHandler[eventType]);
} else {
_dispatchEvent(_propertyEventHandler[eventType]);
}
}
// do not dispatch event when event has been canceled
// true is prevented.
return eventInstance->_cancelled;
}
// This function will be called back by dart side when trigger events.
void NativeEventTarget::dispatchEventImpl(NativeEventTarget *nativeEventTarget, NativeString *nativeEventType, void *nativeEvent, int32_t isCustomEvent) {
assert_m(nativeEventTarget->instance != nullptr, "NativeEventTarget should have owner");
EventTargetInstance *eventTargetInstance = nativeEventTarget->instance;
JSContext *context = eventTargetInstance->context;
std::u16string u16EventType = std::u16string(reinterpret_cast<const char16_t *>(nativeEventType->string),
nativeEventType->length);
std::string eventType = toUTF8(u16EventType);
EventInstance *eventInstance = JSEvent::buildEventInstance(eventType, context, nativeEvent, isCustomEvent == 1);
eventInstance->nativeEvent->target = eventTargetInstance;
eventTargetInstance->dispatchEvent(eventInstance);
}
} // namespace kraken::binding::jsc