mirror of
https://github.com/wgh136/flutter_qjs.git
synced 2025-09-27 13:27:24 +00:00
fix js memory leak.
This commit is contained in:
@@ -23,7 +23,7 @@ add_library( # Sets the name of the library.
|
||||
# quickjs
|
||||
set(QUICK_JS_LIB_DIR ../../../../cxx/quickjs)
|
||||
file (STRINGS "${QUICK_JS_LIB_DIR}/VERSION" QUICKJS_VERSION)
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DCONFIG_VERSION=\\\"${QUICKJS_VERSION}\\\"")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DDUMP_LEAKS -DCONFIG_VERSION=\\\"${QUICKJS_VERSION}\\\"")
|
||||
target_sources(${JNI_LIB_NAME} PUBLIC
|
||||
${QUICK_JS_LIB_DIR}/cutils.c
|
||||
${QUICK_JS_LIB_DIR}/libregexp.c
|
||||
|
@@ -3,7 +3,7 @@
|
||||
* @Author: ekibun
|
||||
* @Date: 2020-08-16 11:08:23
|
||||
* @LastEditors: ekibun
|
||||
* @LastEditTime: 2020-08-20 13:09:08
|
||||
* @LastEditTime: 2020-08-25 18:06:08
|
||||
*/
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
@@ -45,10 +45,15 @@ namespace qjs
|
||||
JSValue array = JS_NewArray(ctx);
|
||||
auto buf = env->GetDoubleArrayElements((jdoubleArray)val, 0);
|
||||
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,
|
||||
JS_NewFloat64(ctx, buf[i]),
|
||||
JS_PROP_C_W_E);
|
||||
JS_FreeAtom(ctx, atom);
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
else if (className.compare("java.lang.Boolean") == 0)
|
||||
@@ -82,10 +87,15 @@ namespace qjs
|
||||
JSValue array = JS_NewArray(ctx);
|
||||
cache[val] = array;
|
||||
for (uint32_t i = 0; i < size; i++)
|
||||
{
|
||||
auto atom = JS_NewAtomUInt32(ctx, i);
|
||||
JS_DefinePropertyValue(
|
||||
ctx, array, JS_NewAtomUInt32(ctx, i),
|
||||
javaToJs(ctx, env, env->GetObjectArrayElement(list, i), cache),
|
||||
JS_PROP_C_W_E);
|
||||
ctx, array, atom,
|
||||
javaToJs(ctx, env, env->GetObjectArrayElement(list, i), cache),
|
||||
JS_PROP_C_W_E);
|
||||
JS_FreeAtom(ctx, atom);
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
else if (className.compare("java.util.HashMap") == 0)
|
||||
@@ -119,10 +129,14 @@ namespace qjs
|
||||
{
|
||||
// 读取一条数据
|
||||
jobject entryObj = env->CallObjectMethod(iteratorObj, nextMID);
|
||||
auto atomvalue = javaToJs(ctx, env, env->CallObjectMethod(entryObj, getKeyMID), cache);
|
||||
auto atom = JS_ValueToAtom(ctx, atomvalue);
|
||||
JS_DefinePropertyValue(
|
||||
ctx, obj, JS_ValueToAtom(ctx, javaToJs(ctx, env, env->CallObjectMethod(entryObj, getKeyMID), cache)),
|
||||
ctx, obj, atom,
|
||||
javaToJs(ctx, env, env->CallObjectMethod(entryObj, getValueMID), cache),
|
||||
JS_PROP_C_W_E);
|
||||
JS_FreeAtom(ctx, atom);
|
||||
JS_FreeValue(ctx, atomvalue);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
@@ -158,7 +172,7 @@ namespace qjs
|
||||
if (JS_IsFunction(val.ctx, val.v))
|
||||
{
|
||||
std::map<jobject, jobject> retMap;
|
||||
retMap[env->NewStringUTF("__js_function__")] = jniWrapPrimity<jlong>(env, (int64_t) new JSValue{JS_DupValue(val.ctx, val.v)});
|
||||
retMap[env->NewStringUTF("__js_function__")] = jniWrapPrimity<jlong>(env, (int64_t) new JSValue{js_add_ref(val)});
|
||||
return jniWrapMap(env, retMap);
|
||||
}
|
||||
else if (JS_IsArray(val.ctx, val.v) > 0)
|
||||
|
@@ -3,9 +3,10 @@
|
||||
* @Author: ekibun
|
||||
* @Date: 2020-08-09 18:16:11
|
||||
* @LastEditors: ekibun
|
||||
* @LastEditTime: 2020-08-16 19:00:06
|
||||
* @LastEditTime: 2020-08-25 16:00:46
|
||||
*/
|
||||
#include "java_js_wrapper.hpp"
|
||||
#include "android/log.h"
|
||||
|
||||
JNIEnv *getEnv(JavaVM *gJvm)
|
||||
{
|
||||
@@ -109,7 +110,7 @@ Java_soko_ekibun_flutter_1qjs_JniBridge_close(
|
||||
jobject thiz,
|
||||
jlong engine)
|
||||
{
|
||||
delete (qjs::Engine *)engine;
|
||||
delete ((qjs::Engine *)engine);
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT void JNICALL
|
||||
@@ -136,8 +137,8 @@ Java_soko_ekibun_flutter_1qjs_JniBridge_call(
|
||||
callargs[i] = qjs::javaToJs(ctx.ctx, env, env->GetObjectArrayElement(array, i));
|
||||
}
|
||||
jvm->DetachCurrentThread();
|
||||
qjs::JSValue ret = JS_Call(ctx.ctx, *function, ctx.global(), (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};
|
||||
|
@@ -21,7 +21,7 @@ class FlutterQjsPlugin: FlutterPlugin, MethodCallHandler {
|
||||
|
||||
override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
|
||||
applicationContext = flutterPluginBinding.applicationContext
|
||||
val channel = MethodChannel(flutterPluginBinding.binaryMessenger, "soko.ekibun.flutter_qjs")
|
||||
channel = MethodChannel(flutterPluginBinding.binaryMessenger, "soko.ekibun.flutter_qjs")
|
||||
channel.setMethodCallHandler(this)
|
||||
channelwrapper = MethodChannelWrapper(handler, channel)
|
||||
}
|
||||
@@ -37,13 +37,13 @@ class FlutterQjsPlugin: FlutterPlugin, MethodCallHandler {
|
||||
val name: String = call.argument<String>("name")!!
|
||||
JniBridge.instance.evaluate(engine, script, name, ResultWrapper(handler, result))
|
||||
} else if (call.method == "call") {
|
||||
println(call.arguments<Map<*, *>>());
|
||||
val engine: Long = call.argument<Long>("engine")!!
|
||||
val function: Long = call.argument<Long>("function")!!
|
||||
val args: List<Any> = call.argument<List<Any>>("arguments")!!
|
||||
JniBridge.instance.call(engine, function, args, ResultWrapper(handler, result))
|
||||
} else if (call.method == "close") {
|
||||
val engine: Long = call.argument<Long>("engine")!!
|
||||
val engine: Long = call.arguments<Long>()
|
||||
println(engine)
|
||||
JniBridge.instance.close(engine)
|
||||
result.success(null)
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user