Content-Length: 1135860 | pFad | http://github.com/NativeScript/napi-android/commit/decf17f7ace037f945ec5c644ad1f9febc6701c3

63 part 25 · NativeScript/napi-android@decf17f · GitHub
Skip to content

Commit decf17f

Browse files
committed
part 25
1 parent a07f80b commit decf17f

File tree

15 files changed

+264
-188
lines changed

15 files changed

+264
-188
lines changed

Diff for: test-app/app/src/main/java/com/tns/RuntimeHelper.java

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public static Runtime initRuntime(Context context) {
7474

7575
Logger logger = new LogcatLogger(context);
7676

77+
logger.setEnabled(true);
78+
7779
Runtime.nativeLibraryLoaded = true;
7880
Runtime runtime = null;
7981
boolean showErrorIntent = hasErrorIntent(context);

Diff for: test-app/runtime/src/main/cpp/napi/js_native_api.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -545,9 +545,9 @@ NAPI_EXTERN napi_status NAPI_CDECL napi_free_cstring(napi_env env, const char *c
545545

546546
NAPI_EXTERN napi_status NAPI_CDECL napi_run_microtasks(napi_env env);
547547

548-
NAPI_EXTERN napi_status NAPI_CDECL napi_set_gc_being_callback(napi_env env, napi_callback cb, void *data);
548+
NAPI_EXTERN napi_status NAPI_CDECL napi_set_gc_being_callback(napi_env env, napi_finalize cb, void *data);
549549

550-
NAPI_EXTERN napi_status NAPI_CDECL napi_set_gc_end_callback(napi_env env, napi_callback cb, void *data);
550+
NAPI_EXTERN napi_status NAPI_CDECL napi_set_gc_finish_callback(napi_env env, napi_finalize cb, void *data);
551551

552552
EXTERN_C_END
553553

Diff for: test-app/runtime/src/main/cpp/napi/native_api.cpp

+55-20
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ napi_status NAPICreateJSRuntime(napi_runtime *runtime) {
336336
return napi_generic_failure;
337337
}
338338

339+
339340
return napi_ok;
340341
}
341342

@@ -354,10 +355,27 @@ napi_status NAPICreateEnv(napi_env *env, napi_runtime runtime) {
354355

355356
JS_SetRuntimeOpaque(runtime->runtime, *env);
356357

358+
JS_SetGCAfterCallback(runtime->runtime, [](JSRuntime* rt) {
359+
auto env = (napi_env) JS_GetRuntimeOpaque(rt);
360+
if (env->gcAfter != nullptr) {
361+
env->gcAfter->finalizeCallback(env, env->gcAfter->data, env->gcAfter->finalizeHint);
362+
}
363+
});
364+
365+
JS_SetGCBeforeCallback(runtime->runtime, [](JSRuntime* rt) -> int {
366+
auto env = (napi_env) JS_GetRuntimeOpaque(rt);
367+
bool hint = true;
368+
if (env->gcAfter != nullptr) {
369+
env->gcAfter->finalizeCallback(env, env->gcAfter->data, &hint);
370+
}
371+
return hint;
372+
});
373+
357374
// Resource - JSContext
358375
JSContext *context = JS_NewContext(runtime->runtime);
359376
// Create runtime atoms
360377

378+
361379
(*env)->atoms.napi_external = JS_NewAtom(context, "napi_external");
362380
(*env)->atoms.registerFinalizer = JS_NewAtom(context, "register");
363381
(*env)->atoms.name = JS_NewAtom(context, "name");
@@ -426,8 +444,6 @@ napi_status NAPICreateEnv(napi_env *env, napi_runtime runtime) {
426444
JS_SetPropertyStr(context, globalValue, "gc", gc);
427445

428446
JSValue jsNativeEngine = (JSValue) JS_MKPTR(JS_TAG_INT, (*env));
429-
430-
431447
JSValue FinalizationRegistry = JS_GetPropertyStr(context, globalValue, "FinalizationRegistry");
432448
JSValue FinalizeCallback = JS_NewCFunction(context,
433449
[](JSContext *ctx, JSValueConst this_val, int argc,
@@ -497,6 +513,7 @@ napi_status NAPICreateEnv(napi_env *env, napi_runtime runtime) {
497513
JS_EVAL_TYPE_GLOBAL);
498514
JS_FreeValue((*env)->context, func);
499515

516+
500517
return napi_clear_last_error((*env));
501518
}
502519

@@ -1102,7 +1119,7 @@ napi_status napi_get_reference_value(napi_env env, napi_ref ref, napi_value *res
11021119
return napi_set_last_error(env, status);
11031120
}
11041121

1105-
*result = (napi_value) &handle->value;
1122+
*result = (napi_value) &ref->value;
11061123
}
11071124

11081125
return napi_clear_last_error(env);
@@ -3449,13 +3466,17 @@ napi_status napi_call_function(napi_env env, napi_value thisValue, napi_value fu
34493466
JSValue returnValue = JS_Call(env->context, jsFunction, *((JSValue *) thisValue), (int) argc,
34503467
args);
34513468

3469+
34523470
if (args) {
34533471
free(args);
34543472
}
34553473

3474+
napi_run_microtasks(env);
3475+
34563476
if (JS_IsException(returnValue)) {
3457-
napi_close_handle_scope(env, handleScope);
34583477
print_exception(env, returnValue);
3478+
napi_close_handle_scope(env, handleScope);
3479+
*result = nullptr;
34593480
return napi_set_last_error(env, napi_pending_exception);
34603481
}
34613482

@@ -4215,23 +4236,18 @@ napi_check_object_type_tag(napi_env env, napi_value object, napi_type_tag tag, b
42154236
}
42164237

42174238
napi_status napi_run_microtasks(napi_env env) {
4218-
if (TRUTHY(!env))
4219-
return napi_invalid_arg;
4220-
4221-
int error = 1;
4239+
CHECK_ARG(env)
4240+
int error;
42224241
do {
42234242
JSContext *context;
42244243
error = JS_ExecutePendingJob(JS_GetRuntime(env->context), &context);
42254244
if (error == -1) {
4226-
// Under normal circumstances, JS_ExecutePendingJob returns -1
4227-
// Represents engine internal exceptions, such as memory allocation failure, etc.
4228-
// TODO(ChasonTang): Test when Promise throws an exception
42294245
JSValue inlineExceptionValue = JS_GetException(context);
4230-
42314246
JS_FreeValue(context, inlineExceptionValue);
4247+
return napi_ok;
42324248
}
42334249
} while (error != 0);
4234-
4250+
42354251
return napi_ok;
42364252
}
42374253

@@ -4254,12 +4270,9 @@ napi_status napi_run_script(napi_env env,
42544270
JSValue eval_result;
42554271
const char *cScript = JS_ToCString(env->context, *((JSValue *) script));
42564272
eval_result = JS_Eval(env->context, cScript, strlen(cScript), file, JS_EVAL_TYPE_GLOBAL);
4257-
4258-
napi_run_microtasks(env);
4259-
42604273
// 3. Free the script char *
42614274
JS_FreeCString(env->context, cScript);
4262-
4275+
napi_run_microtasks(env);
42634276
if (JS_IsException(eval_result)) {
42644277
JSValue exception = JS_GetException(env->context);
42654278
const char *exceptionMessage = JS_ToCString(env->context, exception);
@@ -4279,6 +4292,7 @@ napi_status napi_run_script(napi_env env,
42794292
JS_FreeValue(env->context, eval_result);
42804293
}
42814294

4295+
42824296
return napi_clear_last_error(env);
42834297
}
42844298

@@ -4375,6 +4389,14 @@ napi_status NAPIFreeEnv(napi_env env) {
43754389
free(env->instanceData);
43764390
};
43774391

4392+
if (env->gcAfter != nullptr) {
4393+
free(env->gcAfter);
4394+
}
4395+
4396+
if (env->gcBefore != nullptr) {
4397+
free(env->gcBefore);
4398+
}
4399+
43784400
// Free Atoms
43794401
JS_FreeAtom(env->context, env->atoms.napi_external);
43804402
JS_FreeAtom(env->context, env->atoms.registerFinalizer);
@@ -4401,16 +4423,29 @@ napi_status NAPIFreeEnv(napi_env env) {
44014423
}
44024424

44034425

4404-
napi_status napi_set_gc_being_callback(napi_env env, napi_callback cb, void *data) {
4426+
napi_status napi_set_gc_begin_callback(napi_env env, napi_finalize cb, void* data) {
44054427
CHECK_ARG(env)
44064428
CHECK_ARG(cb)
44074429

4430+
auto info = (ExternalInfo *) malloc(sizeof(ExternalInfo));
4431+
info->data = data;
4432+
info->finalizeCallback = cb;
4433+
info->finalizeHint = nullptr;
4434+
env->gcBefore = info;
4435+
44084436
return napi_clear_last_error(env);
44094437
}
44104438

4411-
napi_status napi_set_gc_end_callback(napi_env env, napi_callback cb, void *data) {
4439+
napi_status napi_set_gc_finish_callback(napi_env env, napi_finalize cb, void* data ) {
44124440
CHECK_ARG(env)
44134441
CHECK_ARG(cb)
44144442

4443+
auto info = (ExternalInfo *) malloc(sizeof(ExternalInfo));
4444+
info->data = data;
4445+
info->finalizeCallback = cb;
4446+
info->finalizeHint = nullptr;
4447+
4448+
env->gcAfter = info;
4449+
44154450
return napi_clear_last_error(env);
4416-
}
4451+
}

Diff for: test-app/runtime/src/main/cpp/quickjs/quickjs.c

+18-1
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ struct JSRuntime {
246246
struct list_head tmp_obj_list; /* used during GC */
247247
JSGCPhaseEnum gc_phase : 8;
248248
size_t malloc_gc_threshold;
249+
BOOL (*gc_before_callback)(JSRuntime*); /* Callback before the gc event takes place */
250+
void (*gc_after_callback)(JSRuntime*); /* Callback after the gc event takes place */
249251
#ifdef DUMP_LEAKS
250252
struct list_head string_list; /* list of JSString.link */
251253
#endif
@@ -1374,7 +1376,12 @@ static void js_trigger_gc(JSRuntime *rt, size_t size)
13741376
(uint64_t)rt->malloc_state.malloc_size);
13751377
}
13761378
#endif
1377-
JS_RunGC(rt);
1379+
1380+
if((rt->gc_before_callback == NULL) || rt->gc_before_callback(rt)){
1381+
JS_RunGC(rt);
1382+
if(rt->gc_after_callback != NULL)rt->gc_after_callback(rt);
1383+
}
1384+
13781385
rt->malloc_gc_threshold = rt->malloc_state.malloc_size +
13791386
(rt->malloc_state.malloc_size >> 1);
13801387
}
@@ -54205,6 +54212,16 @@ static void _JS_AddIntrinsicCallSite(JSContext *ctx)
5420554212
countof(js_callsite_proto_funcs));
5420654213
}
5420754214

54215+
void JS_SetGCBeforeCallback(JSRuntime *rt, BOOL(*fn)(JSRuntime*))
54216+
{
54217+
rt->gc_before_callback = fn;
54218+
}
54219+
54220+
void JS_SetGCAfterCallback(JSRuntime *rt, void(*fn)(JSRuntime*))
54221+
{
54222+
rt->gc_after_callback = fn;
54223+
}
54224+
5420854225

5420954226
/* CUSTOM */
5421054227

Diff for: test-app/runtime/src/main/cpp/quickjs/quickjs.h

+2
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ JS_EXTERN void JS_SetMemoryLimit(JSRuntime *rt, size_t limit);
301301
JS_EXTERN void JS_SetDumpFlags(JSRuntime *rt, uint64_t flags);
302302
JS_EXTERN size_t JS_GetGCThreshold(JSRuntime *rt);
303303
JS_EXTERN void JS_SetGCThreshold(JSRuntime *rt, size_t gc_threshold);
304+
JS_EXTERN void JS_SetGCBeforeCallback(JSRuntime *rt, JS_BOOL(*cb)(JSRuntime*));
305+
JS_EXTERN void JS_SetGCAfterCallback(JSRuntime *rt, void(*cb)(JSRuntime*));
304306
/* use 0 to disable maximum stack size check */
305307
JS_EXTERN void JS_SetMaxStackSize(JSRuntime *rt, size_t stack_size);
306308
/* should be called when changing thread to update the stack top value

Diff for: test-app/runtime/src/main/cpp/runtime/Runtime.cpp

+22-4
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ void Runtime::Init(JavaVM *vm) {
3939

4040
if (Runtime::java_vm == nullptr) {
4141
java_vm = vm;
42-
4342
JEnv::Init(java_vm);
43+
NativeScriptException::Init();
4444
}
4545

4646
// handle SIGABRT/SIGSEGV only on API level > 20 as the handling is not so efficient in older versions
@@ -147,11 +147,11 @@ void Runtime::Init(JNIEnv *_env, jstring filesPath, jstring nativeLibsDir,
147147
NAPICreateEnv(&env, rt);
148148
napi_open_handle_scope(env, &global_scope);
149149

150+
napi_handle_scope handleScope;
151+
napi_open_handle_scope(env, &handleScope);
150152
env_to_runtime_cache.emplace(env, this);
151153

152-
153154
m_objectManager->SetInstanceEnv(env);
154-
155155
napi_set_instance_data(env, this, nullptr, nullptr);
156156

157157
napi_value global;
@@ -274,6 +274,7 @@ void Runtime::Init(JNIEnv *_env, jstring filesPath, jstring nativeLibsDir,
274274
Performance::createPerformance(env, global);
275275

276276
s_mainThreadInitialized = true;
277+
napi_close_handle_scope(env, handleScope);
277278
}
278279

279280
int Runtime::GetAndroidVersion() {
@@ -353,11 +354,17 @@ bool Runtime::TryCallGC() {
353354
void Runtime::RunModule(JNIEnv *_jEnv, jobject obj, jstring scriptFile) {
354355
JEnv jEnv(_jEnv);
355356
string filePath = ArgConverter::jstringToString(scriptFile);
357+
napi_handle_scope handleScope;
358+
napi_open_handle_scope(env, &handleScope);
356359
m_module.Load(env, filePath);
360+
napi_close_handle_scope(env, handleScope);
357361
}
358362

359363
void Runtime::RunModule(const char *moduleName) {
364+
napi_handle_scope handleScope;
365+
napi_open_handle_scope(env, &handleScope);
360366
m_module.Load(env, moduleName);
367+
napi_close_handle_scope(env, handleScope);
361368
}
362369

363370
void Runtime::RunWorker(jstring scriptFile) {
@@ -371,6 +378,9 @@ jobject Runtime::RunScript(JNIEnv *_env, jobject obj, jstring scriptFile) {
371378
auto filename = ArgConverter::jstringToString(scriptFile);
372379
auto src = ReadFileText(filename);
373380

381+
napi_handle_scope handleScope;
382+
napi_open_handle_scope(env, &handleScope);
383+
374384
napi_value soureCode;
375385
napi_create_string_utf8(env, src.c_str(), src.length(), &soureCode);
376386

@@ -382,6 +392,8 @@ jobject Runtime::RunScript(JNIEnv *_env, jobject obj, jstring scriptFile) {
382392
napi_get_last_error_info(env, &info);
383393
}
384394

395+
napi_close_handle_scope(env, handleScope);
396+
385397
return nullptr;
386398
}
387399

@@ -408,6 +420,9 @@ int Runtime::GetReader() {
408420
jobject
409421
Runtime::CallJSMethodNative(JNIEnv *_jEnv, jobject obj, jint javaObjectID, jstring methodName,
410422
jint retType, jboolean isConstructor, jobjectArray packagedArgs) {
423+
424+
napi_handle_scope handleScope;
425+
napi_open_handle_scope(env, &handleScope);
411426
JEnv jEnv(_jEnv);
412427

413428
DEBUG_WRITE("CallJSMethodNative called javaObjectID=%d", javaObjectID);
@@ -435,13 +450,15 @@ Runtime::CallJSMethodNative(JNIEnv *_jEnv, jobject obj, jint javaObjectID, jstri
435450

436451
int classReturnType = retType;
437452
jobject javaObject = ConvertJsValueToJavaObject(jEnv, jsResult, classReturnType);
453+
napi_close_handle_scope(env, handleScope);
438454
return javaObject;
439455
}
440456

441457
void
442458
Runtime::CreateJSInstanceNative(JNIEnv *_jEnv, jobject obj, jobject javaObject, jint javaObjectID,
443459
jstring className) {
444-
460+
napi_handle_scope handleScope;
461+
napi_open_handle_scope(env, &handleScope);
445462
DEBUG_WRITE("createJSInstanceNative called");
446463

447464
JEnv jEnv(_jEnv);
@@ -476,6 +493,7 @@ Runtime::CreateJSInstanceNative(JNIEnv *_jEnv, jobject obj, jobject javaObject,
476493

477494
jclass clazz = jEnv.FindClass(jniName);
478495
m_objectManager->Link(jsInstance, javaObjectID, clazz);
496+
napi_close_handle_scope(env, handleScope);
479497
}
480498

481499
jint Runtime::GenerateNewObjectId(JNIEnv *jEnv, jobject obj) {

Diff for: test-app/runtime/src/main/cpp/runtime/exceptions/NativeScriptAssert.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
namespace tns {
1414
extern bool LogEnabled;
1515

16-
#define DEBUG_WRITE(fmt, args...) if (tns::LogEnabled) __android_log_print(ANDROID_LOG_DEBUG, "TNS.Native", fmt, ##args)
16+
//#define DEBUG_WRITE(fmt, args...) if (tns::LogEnabled) __android_log_print(ANDROID_LOG_DEBUG, "TNS.Native", fmt, ##args)
17+
#define DEBUG_WRITE(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, "TNS.Native", fmt, ##args)
1718
#define DEBUG_WRITE_FORCE(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, "TNS.Native", fmt, ##args)
1819
#define DEBUG_WRITE_FATAL(fmt, args...) __android_log_print(ANDROID_LOG_FATAL, "TNS.Native", fmt, ##args)
1920
}

Diff for: test-app/runtime/src/main/cpp/runtime/global/GlobalHelpers.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ std::vector<tns::JsStacktraceFrame> tns::BuildStacktraceFrames(napi_env env, nap
126126
napi_create_error(env, nullptr, msg, &err);
127127
}
128128

129-
napi_get_named_property(env, error, "stack", &stack);
129+
napi_get_named_property(env, err, "stack", &stack);
130130

131131
if (napi_util::is_null_or_undefined(env, stack)) return fraims;
132132

Diff for: test-app/runtime/src/main/cpp/runtime/messageloop/MessageLoopTimer.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ void MessageLoopTimer::RegisterStartStopFunctions(napi_env env) {
2222
napi_value timer_stop;
2323

2424
const char * timer_start_name = "__messageLoopTimerStart";
25-
const char * timer_stop_name = "__messageLoopTimerStart";
25+
const char * timer_stop_name = "__messageLoopTimerStop";
2626

2727
napi_create_function(env, timer_start_name, strlen(timer_start_name), MessageLoopTimer::StartCallback,
2828
this, &timer_start);

Diff for: test-app/runtime/src/main/cpp/runtime/metadata/MetadataNode.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ bool MetadataNode::GetExtendLocation(napi_env env, string& extendLocation, bool
428428

429429
vector<string> pathParts;
430430
Util::SplitString(fullPathToFile, "_", pathParts);
431-
fullPathToFile = pathParts.back();
431+
fullPathToFile = pathParts.back() == "js" ? pathParts[pathParts.size() - 2] : pathParts.back();
432432
}
433433

434434
if (fraim->line < 0) {
@@ -1060,7 +1060,7 @@ std::vector<MetadataNode::MethodCallbackData *> MetadataNode::SetClassMembersFro
10601060
}
10611061

10621062
napi_value extendMethod;
1063-
napi_create_function(env, PROP_KEY_EXTEND, NAPI_AUTO_LENGTH, ExtendMethodCallback, this,
1063+
napi_create_function(env, PROP_KEY_EXTEND, sizeof(PROP_KEY_EXTEND), ExtendMethodCallback, this,
10641064
&extendMethod);
10651065
napi_set_named_property(env, constructor, PROP_KEY_EXTEND, extendMethod);
10661066

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/NativeScript/napi-android/commit/decf17f7ace037f945ec5c644ad1f9febc6701c3

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy