Skip to content

Commit 03bd68b

Browse files
committed
quickjs: support napi_adjust_external_memory
1 parent 3c6c8e2 commit 03bd68b

3 files changed

Lines changed: 21 additions & 3 deletions

File tree

test-app/runtime/src/main/cpp/napi/quickjs/jsr.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ napi_status js_execute_pending_jobs(napi_env env) {
5454

5555
napi_status
5656
js_adjust_external_memory(napi_env env, int64_t changeInBytes, int64_t *externalMemory) {
57+
napi_adjust_external_memory(env, changeInBytes, externalMemory);
5758
return napi_ok;
5859
}
5960

test-app/runtime/src/main/cpp/napi/quickjs/quickjs-api.c

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ typedef struct napi_env__ {
183183
ExternalInfo *gcBefore;
184184
ExternalInfo *gcAfter;
185185
int js_enter_state;
186+
int64_t usedMemory;
186187
} napi_env__;
187188

188189
typedef struct napi_runtime__ {
@@ -3651,6 +3652,19 @@ napi_status napi_is_promise(napi_env env,
36513652
return napi_clear_last_error(env);
36523653
}
36533654

3655+
NAPI_EXTERN napi_status NAPI_CDECL napi_adjust_external_memory(
3656+
napi_env env, int64_t change_in_bytes, int64_t *adjusted_value) {
3657+
size_t cur = JS_GetGCThreshold(env->runtime->runtime);
3658+
if (cur != env->usedMemory && change_in_bytes < 0)
3659+
return napi_ok; // don't update, changed after GC
3660+
int64_t new = cur - change_in_bytes;
3661+
if (new < 0) new = 0;
3662+
JS_SetGCThreshold(env->runtime->runtime, new);
3663+
env->usedMemory = new;
3664+
*adjusted_value = new;
3665+
return napi_ok;
3666+
}
3667+
36543668

36553669
/**
36563670
* --------------------------------------
@@ -3918,7 +3932,7 @@ napi_status napi_get_host_object_data(napi_env env, napi_value object, void **da
39183932
return napi_clear_last_error(env);
39193933
}
39203934

3921-
napi_status napi_is_host_object(napi_env env, napi_value object, bool* result) {
3935+
napi_status napi_is_host_object(napi_env env, napi_value object, bool *result) {
39223936
CHECK_ARG(env);
39233937
CHECK_ARG(object);
39243938

@@ -3928,8 +3942,8 @@ napi_status napi_is_host_object(napi_env env, napi_value object, bool* result) {
39283942
return napi_set_last_error(env, napi_object_expected, NULL, 0, NULL);
39293943
}
39303944

3931-
void* data = JS_GetOpaque(jsValue,
3932-
env->runtime->napiHostObjectClassId);
3945+
void *data = JS_GetOpaque(jsValue,
3946+
env->runtime->napiHostObjectClassId);
39333947
if (data != NULL) {
39343948
*result = true;
39353949
} else {

test-app/runtime/src/main/cpp/napi/quickjs/source/quickjs.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2479,6 +2479,9 @@ static void JS_MarkContext(JSRuntime *rt, JSContext *ctx,
24792479
JS_MarkValue(rt, ctx->regexp_ctor, mark_func);
24802480
JS_MarkValue(rt, ctx->function_ctor, mark_func);
24812481
JS_MarkValue(rt, ctx->function_proto, mark_func);
2482+
JS_MarkValue(rt, ctx->regexp_last_match_str, mark_func);
2483+
JS_MarkValue(rt, ctx->regexp_right_ctx, mark_func);
2484+
JS_MarkValue(rt, ctx->regexp_left_ctx, mark_func);
24822485

24832486
if (ctx->array_shape)
24842487
mark_func(rt, &ctx->array_shape->header);

0 commit comments

Comments
 (0)