mirror of
https://github.com/wgh136/flutter_qjs.git
synced 2025-09-27 05:27:23 +00:00
add js module
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
* @Author: ekibun
|
||||
* @Date: 2020-08-07 13:55:52
|
||||
* @LastEditors: ekibun
|
||||
* @LastEditTime: 2020-08-25 16:07:29
|
||||
* @LastEditTime: 2020-08-27 20:31:25
|
||||
*/
|
||||
#pragma once
|
||||
#include "quickjs/quickjspp.hpp"
|
||||
@@ -59,6 +59,34 @@ namespace qjs
|
||||
DartChannel channel;
|
||||
} JSThreadState;
|
||||
|
||||
JSModuleDef *js_module_loader(
|
||||
JSContext *ctx,
|
||||
const char *module_name, void *opaque)
|
||||
{
|
||||
JSRuntime *rt = JS_GetRuntime(ctx);
|
||||
JSThreadState *ts = (JSThreadState *)JS_GetRuntimeOpaque(rt);
|
||||
auto promise = ts->channel("__dart_load_module__", Value{ctx, JS_NewString(ctx, module_name)});
|
||||
JSOSFutureArgv argv = promise->get_future().get()(ctx);
|
||||
if (argv.count > 0)
|
||||
{
|
||||
const char *str = JS_ToCString(ctx, argv.argv[0]);
|
||||
JSValue func_val = JS_Eval(ctx, str, strlen(str), module_name, JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY);
|
||||
JS_FreeCString(ctx, str);
|
||||
JS_FreeValue(ctx, argv.argv[0]);
|
||||
if (JS_IsException(func_val))
|
||||
return NULL;
|
||||
/* the module is already referenced, so we must free it */
|
||||
JSModuleDef *m = (JSModuleDef *)JS_VALUE_GET_PTR(func_val);
|
||||
JS_FreeValue(ctx, func_val);
|
||||
return m;
|
||||
}
|
||||
else
|
||||
{
|
||||
JS_Throw(ctx, argv.argv[0]);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
JSValue js_add_ref(Value val)
|
||||
{
|
||||
JSRuntime *rt = JS_GetRuntime(val.ctx);
|
||||
|
@@ -3,7 +3,7 @@
|
||||
* @Author: ekibun
|
||||
* @Date: 2020-08-08 10:30:59
|
||||
* @LastEditors: ekibun
|
||||
* @LastEditTime: 2020-08-26 23:35:20
|
||||
* @LastEditTime: 2020-08-27 18:55:57
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
@@ -76,6 +76,7 @@ namespace qjs
|
||||
__DartImpl.__invoke(res, rej, method, args));
|
||||
)xxx",
|
||||
"<dart>", JS_EVAL_TYPE_MODULE);
|
||||
JS_SetModuleLoaderFunc(rt.rt, nullptr, js_module_loader, nullptr);
|
||||
std::vector<EngineTaskResolver> unresolvedTask;
|
||||
Value promiseWrapper = ctx.eval(
|
||||
R"xxx(
|
||||
|
Reference in New Issue
Block a user