Skip to content

Commit a5bf7b4

Browse files
committed
refactor: refactor invokeModule and refactor test framework.
1 parent 63f27a7 commit a5bf7b4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+904
-484
lines changed

bridge/CMakeLists.txt

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,6 @@ if ($ENV{KRAKEN_JS_ENGINE} MATCHES "quickjs")
184184
list(APPEND BRIDGE_LINK_LIBS quickjs)
185185

186186
list(APPEND BRIDGE_SOURCE
187-
page.cc
188-
page.h
189-
190187
# Binding files
191188
bindings/qjs/binding_initializer.cc
192189
bindings/qjs/binding_initializer.h
@@ -197,24 +194,30 @@ if ($ENV{KRAKEN_JS_ENGINE} MATCHES "quickjs")
197194
bindings/qjs/heap_hashmap.h
198195
bindings/qjs/native_string_utils.cc
199196
bindings/qjs/native_string_utils.h
200-
bindings/qjs/qjs_patch.cc
201-
bindings/qjs/qjs_patch.h
197+
bindings/qjs/qjs_engine_patch.cc
198+
bindings/qjs/qjs_engine_patch.h
202199
bindings/qjs/qjs_function.cc
203200
bindings/qjs/qjs_function.h
204201
bindings/qjs/script_value.cc
205202
bindings/qjs/script_value.h
206203
bindings/qjs/exception_state.cc
207204
bindings/qjs/exception_state.h
208-
bindings/qjs/visitor.cc
209-
bindings/qjs/visitor.h
205+
bindings/qjs/gc_visitor.cc
206+
bindings/qjs/gc_visitor.h
210207
bindings/qjs/rejected_promises.h
211208
bindings/qjs/rejected_promises.cc
212209
bindings/qjs/qjs_window.cc
213210
bindings/qjs/qjs_window.h
211+
bindings/qjs/qjs_page.cc
212+
bindings/qjs/qjs_page.h
213+
bindings/qjs/qjs_module_manager.cc
214+
bindings/qjs/qjs_module_manager.h
214215

215216
# Core sources
216217
core/executing_context.cc
217218
core/executing_context.h
219+
core/page.h
220+
core/page.cc
218221
core/executing_context_data.cc
219222
core/executing_context_data.h
220223
core/dart_methods.h
@@ -224,6 +227,18 @@ if ($ENV{KRAKEN_JS_ENGINE} MATCHES "quickjs")
224227
core/frame/dom_timer_coordinator.h
225228
core/frame/window_or_worker_global_scope.cc
226229
core/frame/window_or_worker_global_scope.h
230+
core/frame/module_listener.cc
231+
core/frame/module_listener.h
232+
core/frame/module_listener_container.cc
233+
core/frame/module_listener_container.h
234+
core/frame/module_manager.cc
235+
core/frame/module_manager.h
236+
core/frame/module_callback.cc
237+
core/frame/module_callback.h
238+
core/frame/module_callback_coordinator.cc
239+
core/frame/module_callback_coordinator.h
240+
core/dom/frame_request_callback_collection.cc
241+
core/dom/frame_request_callback_collection.h
227242
# core/dom/character_data.cc
228243
# core/dom/character_data.h
229244
# core/dom/comment.cc

bridge/bindings/qjs/exception_state.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ void ExceptionState::throwException(JSContext* ctx, ErrorType type, const char*
2727
}
2828
}
2929

30+
void ExceptionState::throwException(JSContext* ctx, JSValue exception) {
31+
m_exception = JS_DupValue(ctx, exception);
32+
}
33+
3034
bool ExceptionState::hasException() {
3135
return !JS_IsNull(m_exception);
3236
}

bridge/bindings/qjs/exception_state.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#define KRAKENBRIDGE_EXCEPTION_STATE_H
88

99
#include <quickjs/quickjs.h>
10+
#include "foundation/macros.h"
1011

1112
namespace kraken {
1213

@@ -20,12 +21,16 @@ enum ErrorType {
2021

2122
// ExceptionState is a scope-like class and provides a way to store an exception.
2223
class ExceptionState {
24+
// ExceptionState should only allocate at stack.
25+
KRAKEN_DISALLOW_NEW();
2326
public:
2427
void throwException(JSContext* ctx, ErrorType type, const char* message);
28+
void throwException(JSContext* ctx, JSValue exception);
2529
bool hasException();
2630
JSValue toQuickJS();
2731
private:
2832
JSValue m_exception{JS_NULL};
33+
JSContext* m_ctx;
2934
};
3035

3136
} // namespace kraken

bridge/bindings/qjs/garbage_collected.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
#include <memory>
1111

1212
#include "foundation/macros.h"
13-
#include "qjs_patch.h"
14-
#include "visitor.h"
13+
#include "gc_visitor.h"
14+
#include "qjs_engine_patch.h"
1515

1616
namespace kraken {
1717

@@ -56,7 +56,7 @@ class GarbageCollected {
5656
* This Trace method must be override by objects inheriting from
5757
* GarbageCollected.
5858
*/
59-
virtual void trace(Visitor* visitor) const = 0;
59+
virtual void trace(GCVisitor* visitor) const = 0;
6060

6161
/**
6262
* Called before underline JavaScript object been collected by GC.
@@ -118,7 +118,7 @@ P* GarbageCollected<T>::initialize(JSContext* ctx, JSClassID* classId, JSClassEx
118118
/// which member of their class should be collected by GC.
119119
def.gc_mark = [](JSRuntime* rt, JSValueConst val, JS_MarkFunc* mark_func) {
120120
auto* object = static_cast<P*>(JS_GetOpaque(val, JSValueGetClassId(val)));
121-
Visitor visitor{rt, mark_func};
121+
GCVisitor visitor{rt, mark_func};
122122
object->trace(&visitor);
123123
};
124124

bridge/bindings/qjs/visitor.cc renamed to bridge/bindings/qjs/gc_visitor.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
* Author: Kraken Team.
44
*/
55

6-
#include "visitor.h"
6+
#include "gc_visitor.h"
77

88
namespace kraken {
99

10-
void Visitor::trace(JSValue value) {
10+
void GCVisitor::trace(JSValue value) {
1111
JS_MarkValue(m_runtime, value, m_markFunc);
1212
}
1313

bridge/bindings/qjs/gc_visitor.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (C) 2021 Alibaba Inc. All rights reserved.
3+
* Author: Kraken Team.
4+
*/
5+
6+
#ifndef KRAKENBRIDGE_GC_VISITOR_H
7+
#define KRAKENBRIDGE_GC_VISITOR_H
8+
9+
#include <quickjs/quickjs.h>
10+
11+
namespace kraken {
12+
13+
// Use GCVisitor to keep track gc managed members in C++ class.
14+
class GCVisitor final {
15+
public:
16+
explicit GCVisitor(JSRuntime* rt, JS_MarkFunc* markFunc): m_runtime(rt), m_markFunc(markFunc) {};
17+
18+
void trace(JSValue value);
19+
20+
private:
21+
JSRuntime* m_runtime{nullptr};
22+
JS_MarkFunc* m_markFunc{nullptr};
23+
};
24+
25+
}
26+
27+
#endif // KRAKENBRIDGE_GC_VISITOR_H

bridge/bindings/qjs/native_string_utils.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
*/
55

66
#include "native_string_utils.h"
7-
#include "bindings/qjs/qjs_patch.h"
7+
#include "bindings/qjs/qjs_engine_patch.h"
88

99
namespace kraken {
1010

bridge/bindings/qjs/qjs_patch.cc renamed to bridge/bindings/qjs/qjs_engine_patch.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Author: Kraken Team.
44
*/
55

6-
#include "qjs_patch.h"
6+
#include "qjs_engine_patch.h"
77
#include <quickjs/cutils.h>
88
#include <quickjs/list.h>
99
#include <cstring>

bridge/bindings/qjs/qjs_patch.h renamed to bridge/bindings/qjs/qjs_engine_patch.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
* Author: Kraken Team.
44
*/
55

6-
#ifndef KRAKENBRIDGE_QJS_PATCH_H
7-
#define KRAKENBRIDGE_QJS_PATCH_H
6+
#ifndef KRAKENBRIDGE_QJS_ENGINE_PATCH_H
7+
#define KRAKENBRIDGE_QJS_ENGINE_PATCH_H
88

99
#include <quickjs/list.h>
1010
#include <quickjs/quickjs.h>
@@ -110,4 +110,4 @@ JSValue JS_GetProxyTarget(JSValue value);
110110
}
111111
#endif
112112

113-
#endif // KRAKENBRIDGE_QJS_PATCH_H
113+
#endif // KRAKENBRIDGE_QJS_ENGINE_PATCH_H

bridge/bindings/qjs/qjs_function.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const char* QJSFunction::getHumanReadableName() const {
3434
return "QJSFunction";
3535
}
3636

37-
void QJSFunction::trace(Visitor* visitor) const {
37+
void QJSFunction::trace(GCVisitor* visitor) const {
3838
visitor->trace(m_function);
3939
}
4040

bridge/bindings/qjs/qjs_function.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class QJSFunction : public GarbageCollected<QJSFunction> {
2424
ScriptValue invoke(JSContext* ctx, int32_t argc, ScriptValue* arguments);
2525

2626
const char* getHumanReadableName() const override;
27-
void trace(Visitor* visitor) const override;
27+
void trace(GCVisitor* visitor) const override;
2828
void dispose() const override;
2929

3030
private:
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright (C) 2021 Alibaba Inc. All rights reserved.
3+
* Author: Kraken Team.
4+
*/
5+
6+
#include "qjs_module_manager.h"
7+
#include "member_installer.h"
8+
#include "qjs_function.h"
9+
#include "core/frame/module_manager.h"
10+
11+
namespace kraken {
12+
13+
14+
JSValue krakenModuleListener(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
15+
if (argc < 1) {
16+
return JS_ThrowTypeError(ctx, "Failed to execute '__kraken_module_listener__': 1 parameter required, but only 0 present.");
17+
}
18+
19+
JSValue callbackValue = argv[0];
20+
if (!JS_IsObject(callbackValue)) {
21+
return JS_ThrowTypeError(ctx, "Failed to execute '__kraken_module_listener__': parameter 1 (callback) must be a function.");
22+
}
23+
24+
if (!JS_IsFunction(ctx, callbackValue)) {
25+
return JS_ThrowTypeError(ctx, "Failed to execute '__kraken_module_listener__': parameter 1 (callback) must be a function.");
26+
}
27+
28+
auto context = static_cast<ExecutionContext*>(JS_GetContextOpaque(ctx));
29+
30+
QJSFunction* handler = QJSFunction::create(ctx, callbackValue);
31+
ExceptionState exception;
32+
33+
ModuleManager::addModuleListener(context, handler, &exception);
34+
if (exception.hasException()) {
35+
return exception.toQuickJS();
36+
}
37+
38+
// auto* link = new ModuleContext{JS_DupValue(ctx, callbackValue), context};
39+
// list_add_tail(&link->link, &context->module_job_list);
40+
41+
return JS_NULL;
42+
}
43+
44+
JSValue krakenInvokeModule(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
45+
if (argc < 2) {
46+
return JS_ThrowTypeError(ctx, "Failed to execute 'kraken.invokeModule()': 2 arguments required.");
47+
}
48+
49+
ScriptValue moduleName = ScriptValue(ctx, argv[0]);
50+
ScriptValue methodValue = ScriptValue(ctx, argv[1]);
51+
ScriptValue paramsValue = ScriptValue(ctx, JS_NULL);
52+
53+
QJSFunction* callback = nullptr;
54+
55+
auto* context = static_cast<ExecutionContext*>(JS_GetContextOpaque(ctx));
56+
57+
if (argc > 2 && !JS_IsNull(argv[2])) {
58+
paramsValue = ScriptValue(ctx, argv[2]);
59+
}
60+
61+
if (argc > 3 && JS_IsFunction(ctx, argv[3])) {
62+
callback = QJSFunction::create(ctx, argv[3]);
63+
}
64+
65+
ExceptionState exception;
66+
ScriptValue result = ModuleManager::invokeModule(context, moduleName, methodValue, paramsValue, callback, &exception);
67+
68+
if (exception.hasException()) {
69+
return exception.toQuickJS();
70+
}
71+
72+
return result.toQuickJS();
73+
}
74+
75+
JSValue flushUICommand(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
76+
auto* context = static_cast<ExecutionContext*>(JS_GetContextOpaque(ctx));
77+
78+
if (context->dartMethodPtr()->flushUICommand == nullptr) {
79+
return JS_ThrowTypeError(ctx, "Failed to execute '__kraken_flush_ui_command__': dart method (flushUICommand) is not registered.");
80+
}
81+
context->dartMethodPtr()->flushUICommand();
82+
return JS_NULL;
83+
}
84+
85+
void QJSModuleManager::installGlobalFunctions(JSContext* ctx) {
86+
std::initializer_list<MemberInstaller::FunctionConfig> functionConfig {
87+
{"__kraken_module_listener__", krakenModuleListener, 1, combinePropFlags(JSPropFlag::enumerable, JSPropFlag::writable, JSPropFlag::configurable)},
88+
{"__kraken_invoke_module__", krakenInvokeModule, 3, combinePropFlags(JSPropFlag::enumerable, JSPropFlag::writable, JSPropFlag::configurable)},
89+
{"__kraken_flush_ui_command__", flushUICommand, 0, combinePropFlags(JSPropFlag::enumerable, JSPropFlag::writable, JSPropFlag::configurable)},
90+
};
91+
92+
JSValue globalObject = JS_GetGlobalObject(ctx);
93+
MemberInstaller::installFunctions(ctx, globalObject, functionConfig);
94+
JS_FreeValue(ctx, globalObject);
95+
}
96+
97+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (C) 2021 Alibaba Inc. All rights reserved.
3+
* Author: Kraken Team.
4+
*/
5+
6+
#ifndef KRAKENBRIDGE_QJS_MODULE_MANAGER_H
7+
#define KRAKENBRIDGE_QJS_MODULE_MANAGER_H
8+
9+
#include <quickjs/quickjs.h>
10+
11+
namespace kraken {
12+
13+
class QJSModuleManager final {
14+
public:
15+
static void installGlobalFunctions(JSContext* ctx);
16+
};
17+
18+
}
19+
20+
//
21+
//void bindModuleManager(ExecutionContext* context);
22+
//void handleInvokeModuleUnexpectedCallback(void* callbackContext, int32_t contextId, NativeString* errmsg, NativeString* json);
23+
24+
#endif // KRAKENBRIDGE_QJS_MODULE_MANAGER_H

bridge/bindings/qjs/qjs_page.cc

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

bridge/bindings/qjs/qjs_page.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (C) 2021 Alibaba Inc. All rights reserved.
3+
* Author: Kraken Team.
4+
*/
5+
6+
#ifndef KRAKENBRIDGE_QJS_PAGE_H
7+
#define KRAKENBRIDGE_QJS_PAGE_H
8+
9+
#include <quickjs/quickjs.h>
10+
11+
namespace kraken {
12+
13+
class QJSPage final {
14+
public:
15+
static void installGlobalFunctions(JSContext* ctx);
16+
};
17+
18+
}
19+
20+
#endif // KRAKENBRIDGE_QJS_PAGE_H

bridge/bindings/qjs/qjs_patch_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
* Author: Kraken Team.
44
*/
55

6-
#include "qjs_patch.h"
76
#include <codecvt>
87
#include "gtest/gtest.h"
8+
#include "qjs_engine_patch.h"
99

1010
TEST(JS_ToUnicode, asciiWords) {
1111
JSRuntime* runtime = JS_NewRuntime();

0 commit comments

Comments
 (0)