mirror of
https://github.com/wgh136/flutter_qjs.git
synced 2025-09-27 05:27:23 +00:00
remove dart object when jsfree.
This commit is contained in:
46
cxx/ffi.cpp
46
cxx/ffi.cpp
@@ -39,7 +39,7 @@ extern "C"
|
||||
{
|
||||
JSRuntime *rt = JS_GetRuntime(ctx);
|
||||
JSChannel *channel = (JSChannel *)JS_GetRuntimeOpaque(rt);
|
||||
const char *str = (char *)channel(ctx, (char *)0, (void *)module_name);
|
||||
const char *str = (char *)channel(ctx, module_name, nullptr);
|
||||
if (str == 0)
|
||||
return NULL;
|
||||
JSValue func_val = JS_Eval(ctx, str, strlen(str), module_name, JS_EVAL_TYPE_MODULE | JS_EVAL_FLAG_COMPILE_ONLY);
|
||||
@@ -71,6 +71,50 @@ extern "C"
|
||||
return rt;
|
||||
}
|
||||
|
||||
DLLEXPORT uint32_t jsNewClass(JSContext *ctx, const char *name)
|
||||
{
|
||||
JSClassID QJSClassId = 0;
|
||||
JS_NewClassID(&QJSClassId);
|
||||
JSRuntime *rt = JS_GetRuntime(ctx);
|
||||
if (!JS_IsRegisteredClass(rt, QJSClassId))
|
||||
{
|
||||
JSClassDef def{
|
||||
name,
|
||||
// destructor
|
||||
[](JSRuntime *rt, JSValue obj) noexcept {
|
||||
JSClassID classid = JS_GetClassID(obj);
|
||||
ObjectOpaque *opaque = (ObjectOpaque *)JS_GetOpaque(obj, classid);
|
||||
JSChannel *channel = (JSChannel *)JS_GetRuntimeOpaque(rt);
|
||||
channel((JSContext *)rt, nullptr, (void *)opaque->opaque);
|
||||
delete opaque;
|
||||
}};
|
||||
int e = JS_NewClass(rt, QJSClassId, &def);
|
||||
if (e < 0)
|
||||
{
|
||||
JS_ThrowInternalError(ctx, "Cant register class %s", name);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return QJSClassId;
|
||||
}
|
||||
|
||||
DLLEXPORT void *jsGetObjectOpaque(JSValue *obj, uint32_t classid)
|
||||
{
|
||||
ObjectOpaque *opaque = (ObjectOpaque *)JS_GetOpaque(*obj, classid);
|
||||
if(opaque == nullptr) return nullptr;
|
||||
return opaque->opaque;
|
||||
}
|
||||
|
||||
DLLEXPORT JSValue *jsNewObjectClass(JSContext *ctx, uint32_t QJSClassId, void *opaque)
|
||||
{
|
||||
auto jsobj = new JSValue(JS_NewObjectClass(ctx, QJSClassId));
|
||||
if (JS_IsException(*jsobj))
|
||||
return jsobj;
|
||||
ObjectOpaque *objOpaque = new ObjectOpaque{ctx, opaque};
|
||||
JS_SetOpaque(*jsobj, objOpaque);
|
||||
return jsobj;
|
||||
}
|
||||
|
||||
DLLEXPORT void jsSetMaxStackSize(JSRuntime *rt, size_t stack_size)
|
||||
{
|
||||
JS_SetMaxStackSize(rt, stack_size);
|
||||
|
11
cxx/ffi.h
11
cxx/ffi.h
@@ -8,6 +8,11 @@
|
||||
|
||||
extern "C"
|
||||
{
|
||||
struct ObjectOpaque
|
||||
{
|
||||
JSContext *ctx;
|
||||
void *opaque;
|
||||
};
|
||||
|
||||
typedef void *JSChannel(JSContext *ctx, const char *method, void *argv);
|
||||
|
||||
@@ -21,6 +26,12 @@ extern "C"
|
||||
|
||||
DLLEXPORT JSRuntime *jsNewRuntime(JSChannel channel);
|
||||
|
||||
DLLEXPORT uint32_t jsNewClass(JSContext *ctx, const char *name);
|
||||
|
||||
DLLEXPORT void *jsGetObjectOpaque(JSValue *obj, uint32_t classid);
|
||||
|
||||
DLLEXPORT JSValue *jsNewObjectClass(JSContext *ctx, uint32_t QJSClassId, void *opaque);
|
||||
|
||||
DLLEXPORT void jsSetMaxStackSize(JSRuntime *rt, size_t stack_size);
|
||||
|
||||
DLLEXPORT void jsFreeRuntime(JSRuntime *rt);
|
||||
|
Submodule cxx/quickjs updated: 9ac874168b...7daa190ca7
Reference in New Issue
Block a user