mirror of
https://github.com/wgh136/flutter_qjs.git
synced 2025-09-27 05:27:23 +00:00
fix js memory leak.
This commit is contained in:
@@ -10,6 +10,7 @@ add_library(libquickjs SHARED
|
||||
)
|
||||
project(libquickjs LANGUAGES C)
|
||||
target_compile_options(libquickjs PRIVATE "-DCONFIG_VERSION=\"${QUICKJS_VERSION}\"")
|
||||
target_compile_options(libquickjs PRIVATE "-DDUMP_LEAKS")
|
||||
|
||||
set(PROJECT_NAME "flutter_qjs")
|
||||
project(${PROJECT_NAME} LANGUAGES CXX)
|
||||
|
@@ -3,7 +3,7 @@
|
||||
* @Author: ekibun
|
||||
* @Date: 2020-08-14 21:45:02
|
||||
* @LastEditors: ekibun
|
||||
* @LastEditTime: 2020-08-20 13:09:21
|
||||
* @LastEditTime: 2020-08-25 18:11:19
|
||||
*/
|
||||
#include "../cxx/js_engine.hpp"
|
||||
#include <flutter_linux/flutter_linux.h>
|
||||
@@ -37,9 +37,14 @@ namespace qjs
|
||||
auto size = (uint32_t)fl_value_get_length(val);
|
||||
JSValue array = JS_NewArray(ctx);
|
||||
for (uint32_t i = 0; i < size; ++i)
|
||||
{
|
||||
auto atom = JS_NewAtomUInt32(ctx, i);
|
||||
JS_DefinePropertyValue(
|
||||
ctx, array, JS_NewAtomUInt32(ctx, i), JS_NewFloat64(ctx, buf[i]),
|
||||
ctx, array, atom, JS_NewFloat64(ctx, buf[i]),
|
||||
JS_PROP_C_W_E);
|
||||
JS_FreeAtom(ctx, atom);
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
case FL_VALUE_TYPE_LIST:
|
||||
@@ -47,10 +52,14 @@ namespace qjs
|
||||
auto size = (uint32_t)fl_value_get_length(val);
|
||||
JSValue array = JS_NewArray(ctx);
|
||||
for (uint32_t i = 0; i < size; ++i)
|
||||
{
|
||||
auto atom = JS_NewAtomUInt32(ctx, i);
|
||||
JS_DefinePropertyValue(
|
||||
ctx, array, JS_NewAtomUInt32(ctx, i),
|
||||
ctx, array, atom,
|
||||
dartToJs(ctx, fl_value_get_list_value(val, i)),
|
||||
JS_PROP_C_W_E);
|
||||
JS_FreeAtom(ctx, atom);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
case FL_VALUE_TYPE_MAP:
|
||||
@@ -58,11 +67,16 @@ namespace qjs
|
||||
auto size = (uint32_t)fl_value_get_length(val);
|
||||
JSValue obj = JS_NewObject(ctx);
|
||||
for (uint32_t i = 0; i < size; ++i)
|
||||
{
|
||||
auto atomvalue = dartToJs(ctx, fl_value_get_map_key(val, i));
|
||||
auto atom = JS_ValueToAtom(ctx, atomvalue);
|
||||
JS_DefinePropertyValue(
|
||||
ctx, obj,
|
||||
JS_ValueToAtom(ctx, dartToJs(ctx, fl_value_get_map_key(val, i))),
|
||||
ctx, obj, atom,
|
||||
dartToJs(ctx, fl_value_get_map_value(val, i)),
|
||||
JS_PROP_C_W_E);
|
||||
JS_FreeAtom(ctx, atom);
|
||||
JS_FreeValue(ctx, atomvalue);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
default:
|
||||
@@ -93,7 +107,7 @@ namespace qjs
|
||||
if (JS_IsFunction(val.ctx, val.v))
|
||||
{
|
||||
FlValue *retMap = fl_value_new_map();
|
||||
fl_value_set_string_take(retMap, "__js_function__", fl_value_new_int((int64_t) new JSValue{JS_DupValue(val.ctx, val.v)}));
|
||||
fl_value_set_string_take(retMap, "__js_function__", fl_value_new_int((int64_t) new JSValue{js_add_ref(val)}));
|
||||
return retMap;
|
||||
}
|
||||
else if (JS_IsArray(val.ctx, val.v) > 0)
|
||||
|
@@ -3,7 +3,7 @@
|
||||
* @Author: ekibun
|
||||
* @Date: 2020-08-17 21:37:11
|
||||
* @LastEditors: ekibun
|
||||
* @LastEditTime: 2020-08-18 23:22:08
|
||||
* @LastEditTime: 2020-08-25 16:07:02
|
||||
*/
|
||||
#include "include/flutter_qjs/flutter_qjs_plugin.h"
|
||||
|
||||
@@ -112,8 +112,8 @@ static void flutter_qjs_plugin_handle_method_call(
|
||||
{
|
||||
callargs[i] = qjs::dartToJs(ctx.ctx, fl_value_get_list_value(arguments, i));
|
||||
}
|
||||
qjs::JSValue ret = JS_Call(ctx.ctx, *function, qjs::JSValue{qjs::JSValueUnion{0}, qjs::JS_TAG_UNDEFINED}, (int)argscount, callargs);
|
||||
qjs::JS_FreeValue(ctx.ctx, *function);
|
||||
qjs::JSValue ret = qjs::call_handler(ctx.ctx, *function, (int)argscount, callargs);
|
||||
delete[] callargs;
|
||||
if (qjs::JS_IsException(ret))
|
||||
throw qjs::exception{};
|
||||
return qjs::Value{ctx.ctx, ret};
|
||||
@@ -128,6 +128,13 @@ static void flutter_qjs_plugin_handle_method_call(
|
||||
g_object_unref(pmethod_call);
|
||||
}});
|
||||
}
|
||||
else if (strcmp(method, "close") == 0)
|
||||
{
|
||||
qjs::Engine *engine = (qjs::Engine *)fl_value_get_int(fl_method_call_get_args(method_call));
|
||||
delete engine;
|
||||
g_autoptr(FlMethodResponse) response = FL_METHOD_RESPONSE(fl_method_success_response_new(fl_value_new_null()));
|
||||
fl_method_call_respond(method_call, response, nullptr);
|
||||
}
|
||||
else
|
||||
{
|
||||
g_autoptr(FlMethodResponse) response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
|
||||
|
Reference in New Issue
Block a user