Skip to content

Commit 6dab9e7

Browse files
committed
fix: fix module api binding.
1 parent a5bf7b4 commit 6dab9e7

File tree

6 files changed

+19
-25
lines changed

6 files changed

+19
-25
lines changed

bridge/bindings/qjs/binding_initializer.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "binding_initializer.h"
77

88
#include "qjs_window.h"
9+
#include "qjs_module_manager.h"
910

1011
//#include "bindings/qjs/bom/blob.h"
1112
//#include "bindings/qjs/bom/console.h"
@@ -45,6 +46,8 @@ namespace kraken {
4546

4647
void installBindings(JSContext* ctx) {
4748
QJSWindow::installGlobalFunctions(ctx);
49+
QJSModuleManager::installGlobalFunctions(ctx);
50+
4851
}
4952

5053
} // namespace kraken

bridge/bindings/qjs/qjs_module_manager.cc

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
namespace kraken {
1212

13-
1413
JSValue krakenModuleListener(JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
1514
if (argc < 1) {
1615
return JS_ThrowTypeError(ctx, "Failed to execute '__kraken_module_listener__': 1 parameter required, but only 0 present.");
@@ -35,9 +34,6 @@ JSValue krakenModuleListener(JSContext* ctx, JSValueConst this_val, int argc, JS
3534
return exception.toQuickJS();
3635
}
3736

38-
// auto* link = new ModuleContext{JS_DupValue(ctx, callbackValue), context};
39-
// list_add_tail(&link->link, &context->module_job_list);
40-
4137
return JS_NULL;
4238
}
4339

@@ -72,21 +68,10 @@ JSValue krakenInvokeModule(JSContext* ctx, JSValueConst this_val, int argc, JSVa
7268
return result.toQuickJS();
7369
}
7470

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-
8571
void QJSModuleManager::installGlobalFunctions(JSContext* ctx) {
8672
std::initializer_list<MemberInstaller::FunctionConfig> functionConfig {
8773
{"__kraken_module_listener__", krakenModuleListener, 1, combinePropFlags(JSPropFlag::enumerable, JSPropFlag::writable, JSPropFlag::configurable)},
8874
{"__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)},
9075
};
9176

9277
JSValue globalObject = JS_GetGlobalObject(ctx);

bridge/bindings/qjs/qjs_module_manager.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,4 @@ class QJSModuleManager final {
1717

1818
}
1919

20-
//
21-
//void bindModuleManager(ExecutionContext* context);
22-
//void handleInvokeModuleUnexpectedCallback(void* callbackContext, int32_t contextId, NativeString* errmsg, NativeString* json);
23-
2420
#endif // KRAKENBRIDGE_QJS_MODULE_MANAGER_H

bridge/core/frame/module_callback.h

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@
1212

1313
namespace kraken {
1414

15+
class ModuleCallback;
16+
17+
// In C++ code, We can not use offsetof to access members of structures or classes that are not Plain Old Data Structures.
18+
// So we use struct which support offsetof.
19+
struct ModuleCallbackLinker {
20+
ModuleCallback* ptr;
21+
list_head link;
22+
};
23+
1524
// ModuleCallback is an asynchronous callback function, usually from the 4th parameter of `kraken.invokeModule` function.
1625
// When the asynchronous operation on the Dart side ends, the callback is will called and to return to the JS executing environment.
1726
class ModuleCallback : public GarbageCollected<ModuleCallback> {
@@ -23,14 +32,13 @@ class ModuleCallback : public GarbageCollected<ModuleCallback> {
2332
void trace(GCVisitor*visitor) const override;
2433
void dispose() const override;
2534

26-
list_head link;
35+
ModuleCallbackLinker linker{this};
2736

2837
private:
2938
QJSFunction* m_function{nullptr};
3039
};
3140

3241

33-
3442
}
3543

3644
#endif // KRAKENBRIDGE_MODULE_CALLBACK_H

bridge/core/frame/module_callback_coordinator.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
namespace kraken {
99

1010
void ModuleCallbackCoordinator::addModuleCallbacks(ModuleCallback* callback) {
11-
list_add_tail(&m_listeners, &callback->link);
11+
list_add_tail(&m_listeners, &callback->linker.link);
1212
}
1313

1414
void ModuleCallbackCoordinator::removeModuleCallbacks(ModuleCallback* callback) {
15-
list_del(&callback->link);
15+
list_del(&callback->linker.link);
1616
}
1717

1818
ModuleCallbackCoordinator::ModuleCallbackCoordinator() {
@@ -23,8 +23,8 @@ void ModuleCallbackCoordinator::trace(GCVisitor* visitor) {
2323
{
2424
struct list_head *el, *el1;
2525
list_for_each_safe(el, el1, &m_listeners) {
26-
auto* callback = list_entry(el, ModuleCallback, link);
27-
visitor->trace(callback->toQuickJS());
26+
auto* linker = list_entry(el, ModuleCallbackLinker, link);
27+
visitor->trace(linker->ptr->toQuickJS());
2828
}
2929
}
3030
}

bridge/foundation/native_value.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ static JSValue anonymousFunction(JSContext* ctx, JSValueConst this_val, int argc
156156
// JSValue returnValue = eventTarget->callNativeMethods(call_params.c_str(), argc, arguments);
157157
// delete[] arguments;
158158
// return returnValue;
159+
return JS_NULL;
159160
}
160161

161162
void anonymousAsyncCallback(void* callbackContext, NativeValue* nativeValue, int32_t contextId, const char* errmsg) {
@@ -215,6 +216,7 @@ static JSValue anonymousAsyncFunction(JSContext* ctx, JSValueConst this_val, int
215216
// delete[] arguments;
216217
//
217218
// return promise;
219+
return JS_NULL;
218220
}
219221

220222
JSValue nativeValueToJSValue(ExecutionContext* context, NativeValue& value) {

0 commit comments

Comments
 (0)