/
window.cc
190 lines (160 loc) · 7.68 KB
/
window.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
/*
* Copyright (C) 2021 Alibaba Inc. All rights reserved.
* Author: Kraken Team.
*/
#include "window.h"
#include "bindings/qjs/dom/events/.gen/message_event.h"
#include "bindings/qjs/qjs_patch.h"
#include "dart_methods.h"
namespace kraken::binding::qjs {
std::once_flag kWindowInitOnceFlag;
void bindWindow(std::unique_ptr<JSContext>& context) {
// Set globalThis and Window's prototype to EventTarget's prototype to support EventTarget methods in global.
auto* windowConstructor = new Window(context.get());
JS_SetPrototype(context->ctx(), context->global(), windowConstructor->prototype());
context->defineGlobalProperty("Window", windowConstructor->classObject);
auto* window = new WindowInstance(windowConstructor);
JS_SetOpaque(context->global(), window);
context->defineGlobalProperty("__window__", window->instanceObject);
}
JSClassID Window::kWindowClassId{0};
Window::Window(JSContext* context) : EventTarget(context, "Window") {
std::call_once(kWindowInitOnceFlag, []() { JS_NewClassID(&kWindowClassId); });
JS_SetPrototype(m_ctx, m_prototypeObject, EventTarget::instance(m_context)->prototype());
}
JSClassID Window::classId() {
return 1;
}
JSValue Window::open(QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
JSValue url = argv[0];
auto window = static_cast<WindowInstance*>(JS_GetOpaque(this_val, Window::classId()));
NativeValue arguments[] = {jsValueToNativeValue(ctx, argv[0])};
return window->callNativeMethods("open", 1, arguments);
}
JSValue Window::scrollTo(QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
auto window = static_cast<WindowInstance*>(JS_GetOpaque(this_val, Window::classId()));
NativeValue arguments[] = {jsValueToNativeValue(ctx, argv[0]), jsValueToNativeValue(ctx, argv[1])};
return window->callNativeMethods("scroll", 2, arguments);
}
JSValue Window::scrollBy(QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
JSValue x = argv[0];
JSValue y = argv[1];
auto window = static_cast<WindowInstance*>(JS_GetOpaque(this_val, Window::classId()));
NativeValue arguments[] = {jsValueToNativeValue(ctx, argv[0]), jsValueToNativeValue(ctx, argv[1])};
return window->callNativeMethods("scrollBy", 2, arguments);
}
JSValue Window::postMessage(QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
JSValue messageValue = argv[0];
JSValue originValue = argv[1];
JSValue globalObjectValue = JS_GetGlobalObject(ctx);
auto* window = static_cast<WindowInstance*>(JS_GetOpaque(globalObjectValue, Window::classId()));
JSValue messageEventInitValue = JS_NewObject(ctx);
JS_SetPropertyStr(ctx, messageEventInitValue, "data", JS_DupValue(ctx, messageValue));
JS_SetPropertyStr(ctx, originValue, "origin", JS_DupValue(ctx, originValue));
JSValue messageEventValue = JS_CallConstructor(ctx, MessageEvent::instance(window->m_context)->classObject, 1, &messageEventInitValue);
auto* event = static_cast<MessageEventInstance*>(JS_GetOpaque(messageEventValue, Event::kEventClassID));
window->dispatchEvent(event);
JS_FreeValue(ctx, messageEventValue);
JS_FreeValue(ctx, messageEventInitValue);
JS_FreeValue(ctx, globalObjectValue);
return JS_NULL;
}
PROP_GETTER(Window, devicePixelRatio)(QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
if (getDartMethod()->devicePixelRatio == nullptr) {
return JS_ThrowTypeError(ctx, "Failed to read devicePixelRatio: dart method (devicePixelRatio) is not register.");
}
auto* context = static_cast<JSContext*>(JS_GetContextOpaque(ctx));
double devicePixelRatio = getDartMethod()->devicePixelRatio(context->getContextId());
return JS_NewFloat64(ctx, devicePixelRatio);
}
PROP_SETTER(Window, devicePixelRatio)(QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
return JS_NULL;
}
PROP_GETTER(Window, colorScheme)(QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
if (getDartMethod()->platformBrightness == nullptr) {
return JS_ThrowTypeError(ctx, "Failed to read colorScheme: dart method (platformBrightness) not register.");
}
auto* context = static_cast<JSContext*>(JS_GetContextOpaque(ctx));
const NativeString* code = getDartMethod()->platformBrightness(context->getContextId());
return JS_NewUnicodeString(context->runtime(), ctx, code->string, code->length);
}
PROP_SETTER(Window, colorScheme)(QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
return JS_NULL;
}
PROP_GETTER(Window, __location__)(QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
auto* window = static_cast<WindowInstance*>(JS_GetOpaque(this_val, 1));
if (window == nullptr)
return JS_UNDEFINED;
return JS_DupValue(ctx, window->m_location.value());
}
PROP_SETTER(Window, __location__)(QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
return JS_NULL;
}
PROP_GETTER(Window, location)(QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
auto* window = static_cast<WindowInstance*>(JS_GetOpaque(this_val, 1));
return JS_GetPropertyStr(ctx, window->m_context->global(), "location");
}
PROP_SETTER(Window, location)(QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
return JS_NULL;
}
PROP_GETTER(Window, window)(QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
return JS_GetGlobalObject(ctx);
}
PROP_SETTER(Window, window)(QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
return JS_NULL;
}
PROP_GETTER(Window, parent)(QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
return JS_GetGlobalObject(ctx);
}
PROP_SETTER(Window, parent)(QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
return JS_NULL;
}
PROP_GETTER(Window, scrollX)(QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
auto* window = static_cast<WindowInstance*>(JS_GetOpaque(this_val, 1));
return window->callNativeMethods("scrollX", 0, nullptr);
}
PROP_SETTER(Window, scrollX)(QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
return JS_NULL;
}
PROP_GETTER(Window, scrollY)(QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
auto* window = static_cast<WindowInstance*>(JS_GetOpaque(this_val, 1));
return window->callNativeMethods("scrollY", 0, nullptr);
}
PROP_SETTER(Window, scrollY)(QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
return JS_NULL;
}
PROP_GETTER(Window, onerror)(QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
auto* window = static_cast<WindowInstance*>(JS_GetOpaque(this_val, 1));
return JS_DupValue(ctx, window->onerror);
}
PROP_SETTER(Window, onerror)(QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
auto* window = static_cast<WindowInstance*>(JS_GetOpaque(this_val, 1));
JSValue eventString = JS_NewString(ctx, "onerror");
JSString* p = JS_VALUE_GET_STRING(eventString);
window->setPropertyHandler(p, argv[0]);
if (!JS_IsNull(window->onerror)) {
JS_FreeValue(ctx, window->onerror);
}
window->onerror = JS_DupValue(ctx, argv[0]);
JS_FreeValue(ctx, eventString);
return JS_NULL;
}
PROP_SETTER(Window, self)(QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
return JS_NULL;
}
PROP_GETTER(Window, self)(QjsContext* ctx, JSValue this_val, int argc, JSValue* argv) {
return JS_GetGlobalObject(ctx);
}
WindowInstance::WindowInstance(Window* window) : EventTargetInstance(window, Window::kWindowClassId, "window", WINDOW_TARGET_ID) {
if (getDartMethod()->initWindow != nullptr) {
getDartMethod()->initWindow(context()->getContextId(), nativeEventTarget);
}
m_context->m_window = this;
}
void WindowInstance::gcMark(JSRuntime* rt, JSValue val, JS_MarkFunc* mark_func) {
EventTargetInstance::gcMark(rt, val, mark_func);
// Should check object is already inited before gc mark.
if (JS_IsObject(onerror))
JS_MarkValue(rt, onerror, mark_func);
}
} // namespace kraken::binding::qjs