Skip to content

Commit 2570240

Browse files
committed
chore: tmp
1 parent d55a199 commit 2570240

24 files changed

+622
-433
lines changed

bridge/CMakeLists.txt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,15 +197,28 @@ if ($ENV{KRAKEN_JS_ENGINE} MATCHES "quickjs")
197197
bindings/qjs/native_string_utils.h
198198
bindings/qjs/qjs_patch.cc
199199
bindings/qjs/qjs_patch.h
200-
core/dart_methods.h
200+
bindings/qjs/qjs_function.cc
201+
bindings/qjs/qjs_function.h
202+
bindings/qjs/script_value.cc
203+
bindings/qjs/script_value.h
204+
bindings/qjs/exception_state.cc
205+
bindings/qjs/exception_state.h
206+
207+
bindings/qjs/timer.cc
208+
bindings/qjs/timer.h
201209

202210
# Core sources
203211
core/executing_context.cc
204212
core/executing_context.h
205213
core/executing_context_data.cc
206214
core/executing_context_data.h
215+
core/dart_methods.h
216+
core/frame/dom_timer.cc
217+
core/frame/dom_timer.h
207218
core/frame/dom_timer_coordinator.cc
208219
core/frame/dom_timer_coordinator.h
220+
core/frame/window_or_worker_global_scope.cc
221+
core/frame/window_or_worker_global_scope.h
209222
# core/dom/character_data.cc
210223
# core/dom/character_data.h
211224
# core/dom/comment.cc

bridge/bindings/qjs/binding_initializer.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ namespace kraken {
1010

1111
void initBinding();
1212

13-
1413
// bindConsole(m_context);
1514
// bindTimer(m_context);
1615
// bindScreen(m_context);

bridge/bindings/qjs/dom/elements/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
* Copyright (C) 2021 Alibaba Inc. All rights reserved.
3+
* Author: Kraken Team.
4+
*/
5+
6+
#include "exception_state.h"

bridge/bindings/qjs/exception_state.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (C) 2021 Alibaba Inc. All rights reserved.
3+
* Author: Kraken Team.
4+
*/
5+
6+
#ifndef KRAKENBRIDGE_EXCEPTION_STATE_H
7+
#define KRAKENBRIDGE_EXCEPTION_STATE_H
8+
9+
namespace kraken {
10+
11+
// ExceptionState is a scope-like class and provides a way to throw an exception.
12+
class ExceptionState {
13+
public:
14+
15+
private:
16+
17+
};
18+
19+
}
20+
21+
#endif // KRAKENBRIDGE_EXCEPTION_STATE_H

bridge/bindings/qjs/garbage_collected.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define KRAKENBRIDGE_GARBAGE_COLLECTED_H
88

99
#include <quickjs/quickjs.h>
10+
#include <memory>
1011

1112
#include "foundation/macros.h"
1213
#include "qjs_patch.h"

bridge/bindings/qjs/qjs_function.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/*
2+
* Copyright (C) 2021 Alibaba Inc. All rights reserved.
3+
* Author: Kraken Team.
4+
*/
5+
6+
#include "qjs_function.h"

bridge/bindings/qjs/qjs_function.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright (C) 2021 Alibaba Inc. All rights reserved.
3+
* Author: Kraken Team.
4+
*/
5+
6+
#ifndef KRAKENBRIDGE_QJS_FUNCTION_H
7+
#define KRAKENBRIDGE_QJS_FUNCTION_H
8+
9+
#include "garbage_collected.h"
10+
11+
namespace kraken {
12+
13+
// https://webidl.spec.whatwg.org/#dfn-callback-interface
14+
class QJSFunction : public GarbageCollected<QJSFunction> {
15+
public:
16+
static QJSFunction* create(JSContext* ctx, JSValue function) { return makeGarbageCollected<QJSFunction>(ctx, function); }
17+
18+
explicit QJSFunction(JSContext* ctx, JSValue function) : m_function(JS_DupValue(ctx, function)){};
19+
20+
const char* getHumanReadableName() const override;
21+
22+
[[nodiscard]]
23+
24+
void trace(JSRuntime* rt, JSValue val, JS_MarkFunc* mark_func) const override;
25+
void dispose() const override;
26+
27+
private:
28+
JSValue m_function{JS_NULL};
29+
};
30+
31+
} // namespace kraken
32+
33+
#endif // KRAKENBRIDGE_QJS_FUNCTION_H

bridge/bindings/qjs/script_value.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Copyright (C) 2021 Alibaba Inc. All rights reserved.
3+
* Author: Kraken Team.
4+
*/
5+
6+
7+
#include "script_value.h"
8+
9+
namespace kraken {
10+
11+
}

bridge/bindings/qjs/script_value.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (C) 2021 Alibaba Inc. All rights reserved.
3+
* Author: Kraken Team.
4+
*/
5+
6+
#ifndef KRAKENBRIDGE_SCRIPT_VALUE_H
7+
#define KRAKENBRIDGE_SCRIPT_VALUE_H
8+
9+
#include <quickjs/quickjs.h>
10+
#include "foundation/macros.h"
11+
12+
namespace kraken {
13+
14+
// ScriptValue is a QuickJS JSValue wrapper which hold all information to hide out QuickJS running details.
15+
class ScriptValue final {
16+
KRAKEN_DISALLOW_NEW();
17+
18+
public:
19+
explicit ScriptValue(JSContext* ctx, JSValue value): m_ctx(ctx), m_value(value) {};
20+
21+
private:
22+
JSContext* m_ctx{nullptr};
23+
JSValue m_value{JS_NULL};
24+
};
25+
26+
}
27+
28+
#endif // KRAKENBRIDGE_SCRIPT_VALUE_H
Lines changed: 16 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,12 @@
11
/*
2-
* Copyright (C) 2021 Alibaba Inc. All rights reserved.
3-
* Author: Kraken Team.
4-
*/
2+
* Copyright (C) 2021 Alibaba Inc. All rights reserved.
3+
* Author: Kraken Team.
4+
*/
55

66
#include "timer.h"
7-
#include "bindings/qjs/garbage_collected.h"
8-
#include "bindings/qjs/qjs_patch.h"
9-
#include "dart_methods.h"
10-
11-
#if UNIT_TEST
12-
#include "kraken_test_env.h"
13-
#endif
147

158
namespace kraken {
169

17-
DOMTimer::DOMTimer(JSValue callback) : m_callback(callback) {}
18-
19-
JSClassID DOMTimer::classId{0};
20-
21-
void DOMTimer::fire() {
22-
// 'callback' might be destroyed when calling itself (if it frees the handler), so must take extra care.
23-
auto* context = static_cast<ExecutionContext*>(JS_GetContextOpaque(m_ctx));
24-
if (!JS_IsFunction(m_ctx, m_callback))
25-
return;
26-
27-
JS_DupValue(m_ctx, m_callback);
28-
JSValue returnValue = JS_Call(m_ctx, m_callback, JS_UNDEFINED, 0, nullptr);
29-
JS_FreeValue(m_ctx, m_callback);
30-
31-
if (JS_IsException(returnValue)) {
32-
context->handleException(&returnValue);
33-
}
34-
35-
JS_FreeValue(m_ctx, returnValue);
36-
}
37-
38-
void DOMTimer::trace(JSRuntime* rt, JSValue val, JS_MarkFunc* mark_func) const {
39-
JS_MarkValue(rt, m_callback, mark_func);
40-
}
41-
42-
void DOMTimer::dispose() const {
43-
JS_FreeValueRT(m_runtime, m_callback);
44-
}
45-
46-
int32_t DOMTimer::timerId() {
47-
return m_timerId;
48-
}
49-
50-
void DOMTimer::setTimerId(int32_t timerId) {
51-
m_timerId = timerId;
52-
}
53-
54-
static void handleTimerCallback(DOMTimer* timer, const char* errmsg) {
55-
auto* context = static_cast<ExecutionContext*>(JS_GetContextOpaque(timer->ctx()));
56-
57-
if (errmsg != nullptr) {
58-
JSValue exception = JS_ThrowTypeError(timer->ctx(), "%s", errmsg);
59-
context->handleException(&exception);
60-
return;
61-
}
62-
63-
if (context->timers()->getTimerById(timer->timerId()) == nullptr)
64-
return;
65-
66-
// Trigger timer callbacks.
67-
timer->fire();
68-
69-
// Executing pending async jobs.
70-
context->drainPendingPromiseJobs();
71-
}
72-
73-
static void handleTransientCallback(void* ptr, int32_t contextId, const char* errmsg) {
74-
auto* timer = static_cast<DOMTimer*>(ptr);
75-
auto* context = static_cast<ExecutionContext*>(JS_GetContextOpaque(timer->ctx()));
76-
77-
if (!checkPage(contextId, context))
78-
return;
79-
if (!context->isValid())
80-
return;
81-
82-
handleTimerCallback(timer, errmsg);
83-
84-
context->timers()->removeTimeoutById(timer->timerId());
85-
}
86-
87-
static void handlePersistentCallback(void* ptr, int32_t contextId, const char* errmsg) {
88-
auto* timer = static_cast<DOMTimer*>(ptr);
89-
auto* context = static_cast<ExecutionContext*>(JS_GetContextOpaque(timer->ctx()));
90-
91-
if (!checkPage(contextId, context))
92-
return;
93-
if (!context->isValid())
94-
return;
95-
96-
handleTimerCallback(timer, errmsg);
97-
}
98-
9910
static JSValue setTimeout(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
10011
if (argc < 1) {
10112
return JS_ThrowTypeError(ctx, "Failed to execute 'setTimeout': 1 argument required, but only 0 present.");
@@ -130,9 +41,9 @@ static JSValue setTimeout(JSContext* ctx, JSValueConst this_val, int argc, JSVal
13041
#endif
13142

13243
// Create a timer object to keep track timer callback.
133-
auto* timer = makeGarbageCollected<DOMTimer>(JS_DupValue(ctx, callbackValue))->initialize(context->ctx(), &DOMTimer::classId);
44+
auto* timer = makeGarbageCollected<DOMTimer>(JS_DupValue(ctx, callbackValue))->initialize<DOMTimer>(context->ctx(), &DOMTimer::classId);
13445

135-
auto timerId = getDartMethod()->setTimeout(timer, context->getContextId(), handleTransientCallback, timeout);
46+
auto timerId = context->dartMethodPtr()->setTimeout(timer, context->getContextId(), handleTransientCallback, timeout);
13647

13748
// Register timerId.
13849
timer->setTimerId(timerId);
@@ -174,14 +85,14 @@ static JSValue setInterval(JSContext* ctx, JSValueConst this_val, int argc, JSVa
17485
return JS_ThrowTypeError(ctx, "Failed to execute 'setTimeout': parameter 2 (timeout) only can be a number or undefined.");
17586
}
17687

177-
if (getDartMethod()->setInterval == nullptr) {
88+
if (context->dartMethodPtr()->setInterval == nullptr) {
17889
return JS_ThrowTypeError(ctx, "Failed to execute 'setInterval': dart method (setInterval) is not registered.");
17990
}
18091

18192
// Create a timer object to keep track timer callback.
182-
auto* timer = makeGarbageCollected<DOMTimer>(JS_DupValue(ctx, callbackValue))->initialize(context->ctx(), &DOMTimer::classId);
93+
auto* timer = makeGarbageCollected<DOMTimer>(JS_DupValue(ctx, callbackValue))->initialize<DOMTimer>(context->ctx(), &DOMTimer::classId);
18394

184-
uint32_t timerId = getDartMethod()->setInterval(timer, context->getContextId(), handlePersistentCallback, timeout);
95+
uint32_t timerId = context->dartMethodPtr()->setInterval(timer, context->getContextId(), handlePersistentCallback, timeout);
18596

18697
// Register timerId.
18798
timer->setTimerId(timerId);
@@ -209,20 +120,21 @@ static JSValue clearTimeout(JSContext* ctx, JSValueConst this_val, int argc, JSV
209120
int32_t id;
210121
JS_ToInt32(ctx, &id, timeIdValue);
211122

212-
if (getDartMethod()->clearTimeout == nullptr) {
123+
if (context->dartMethodPtr()->clearTimeout == nullptr) {
213124
return JS_ThrowTypeError(ctx, "Failed to execute 'clearTimeout': dart method (clearTimeout) is not registered.");
214125
}
215126

216-
getDartMethod()->clearTimeout(context->getContextId(), id);
127+
context->dartMethodPtr()->clearTimeout(context->getContextId(), id);
217128

218129
context->timers()->removeTimeoutById(id);
219130
return JS_NULL;
220131
}
221132

222133
void bindTimer(ExecutionContext* context) {
223-
QJS_GLOBAL_BINDING_FUNCTION(context, setTimeout, "setTimeout", 2);
224-
QJS_GLOBAL_BINDING_FUNCTION(context, setInterval, "setInterval", 2);
225-
QJS_GLOBAL_BINDING_FUNCTION(context, clearTimeout, "clearTimeout", 1);
226-
QJS_GLOBAL_BINDING_FUNCTION(context, clearTimeout, "clearInterval", 1);
134+
// QJS_GLOBAL_BINDING_FUNCTION(context, setTimeout, "setTimeout", 2);
135+
// QJS_GLOBAL_BINDING_FUNCTION(context, setInterval, "setInterval", 2);
136+
// QJS_GLOBAL_BINDING_FUNCTION(context, clearTimeout, "clearTimeout", 1);
137+
// QJS_GLOBAL_BINDING_FUNCTION(context, clearTimeout, "clearInterval", 1);
138+
}
139+
227140
}
228-
} // namespace kraken

bridge/bindings/qjs/timer.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright (C) 2021 Alibaba Inc. All rights reserved.
3+
* Author: Kraken Team.
4+
*/
5+
6+
#ifndef KRAKENBRIDGE_DOM_TIMER_H
7+
#define KRAKENBRIDGE_TIMER_H
8+
9+
#include "core/executing_context.h"
10+
11+
namespace kraken {
12+
13+
void bindTimer(ExecutionContext* context);
14+
15+
}
16+
17+
#endif // KRAKENBRIDGE_DOM_TIMER_H

bridge/core/dom/frame_request_callback_collection.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#ifndef KRAKENBRIDGE_BINDINGS_QJS_BOM_FRAME_REQUEST_CALLBACK_COLLECTION_H_
77
#define KRAKENBRIDGE_BINDINGS_QJS_BOM_FRAME_REQUEST_CALLBACK_COLLECTION_H_
88

9-
#include "bindings/qjs/executing_context.h"
9+
#include "core/executing_context.h"
1010

1111
namespace kraken {
1212

bridge/core/executing_context_test.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ TEST(Context, accessGetUICommandItemsAfterDisposed) {
9090

9191
TEST(Context, disposeContext) {
9292
initJSPagePool(1024 * 1024);
93-
TEST_mockDartMethods(nullptr);
93+
TEST_mockDartMethods(0, nullptr);
9494
uint32_t contextId = 0;
9595
auto bridge = static_cast<kraken::KrakenPage*>(getPage(contextId));
9696
static bool disposed = false;
@@ -159,7 +159,7 @@ TEST(Context, evaluateByteCode) {
159159
TEST(jsValueToNativeString, utf8String) {
160160
auto bridge = TEST_init([](int32_t contextId, const char* errmsg) {});
161161
JSValue str = JS_NewString(bridge->getContext()->ctx(), "helloworld");
162-
std::unique_ptr<NativeString> nativeString = kraken::binding::qjs::jsValueToNativeString(bridge->getContext()->ctx(), str);
162+
std::unique_ptr<kraken::NativeString> nativeString = kraken::jsValueToNativeString(bridge->getContext()->ctx(), str);
163163
EXPECT_EQ(nativeString->length, 10);
164164
uint8_t expectedString[10] = {104, 101, 108, 108, 111, 119, 111, 114, 108, 100};
165165
for (int i = 0; i < 10; i++) {
@@ -171,7 +171,7 @@ TEST(jsValueToNativeString, utf8String) {
171171
TEST(jsValueToNativeString, unicodeChinese) {
172172
auto bridge = TEST_init([](int32_t contextId, const char* errmsg) {});
173173
JSValue str = JS_NewString(bridge->getContext()->ctx(), "这是你的优乐美");
174-
std::unique_ptr<NativeString> nativeString = kraken::binding::qjs::jsValueToNativeString(bridge->getContext()->ctx(), str);
174+
std::unique_ptr<kraken::NativeString> nativeString = kraken::jsValueToNativeString(bridge->getContext()->ctx(), str);
175175
std::u16string expectedString = u"这是你的优乐美";
176176
EXPECT_EQ(nativeString->length, expectedString.size());
177177
for (int i = 0; i < nativeString->length; i++) {
@@ -183,7 +183,7 @@ TEST(jsValueToNativeString, unicodeChinese) {
183183
TEST(jsValueToNativeString, emoji) {
184184
auto bridge = TEST_init([](int32_t contextId, const char* errmsg) {});
185185
JSValue str = JS_NewString(bridge->getContext()->ctx(), "……🤪");
186-
std::unique_ptr<NativeString> nativeString = kraken::binding::qjs::jsValueToNativeString(bridge->getContext()->ctx(), str);
186+
std::unique_ptr<kraken::NativeString> nativeString = kraken::jsValueToNativeString(bridge->getContext()->ctx(), str);
187187
std::u16string expectedString = u"……🤪";
188188
EXPECT_EQ(nativeString->length, expectedString.length());
189189
for (int i = 0; i < nativeString->length; i++) {

0 commit comments

Comments
 (0)