Skip to content

Commit

Permalink
Expose GC to guest code (#1410)
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeWharton authored Aug 28, 2024
1 parent b7c222a commit 780a79b
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions zipline/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ kotlin {
header(file("native/quickjs/quickjs.h"))
header(file("native/common/context-no-eval.h"))
header(file("native/common/finalization-registry.h"))
header(file("native/common/global-gc.h"))
packageName("app.cash.zipline.quickjs")
}
}
Expand Down
3 changes: 3 additions & 0 deletions zipline/native/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "ExceptionThrowers.h"
#include "common/context-no-eval.h"
#include "common/finalization-registry.h"
#include "common/global-gc.h"
#include "quickjs/quickjs.h"

/**
Expand Down Expand Up @@ -95,6 +96,8 @@ Context::Context(JNIEnv* env)
JS_SetRuntimeOpaque(jsRuntime, this);
JS_SetInterruptHandler(jsRuntime, &jsInterruptHandlerPoll, this);

JS_AddGlobalThisGc(jsContext);

if (installFinalizationRegistry(jsContext, jsContextForCompiling) < 0) {
throwJavaException(env, "java/lang/IllegalStateException",
"Failed to install FinalizationRegistry");
Expand Down
29 changes: 29 additions & 0 deletions zipline/native/common/global-gc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (C) 2024 Block, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "../quickjs/quickjs.h"
#include "global-gc.h"

static JSValue js_global_gc(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {
JS_RunGC(JS_GetRuntime(ctx));
return JS_UNDEFINED;
}

void JS_AddGlobalThisGc(JSContext *jsContext) {
JSValue gc = JS_NewCFunction(jsContext, js_global_gc, "gc", 0);
JSValue globalThis = JS_GetGlobalObject(jsContext);
JS_SetPropertyStr(jsContext, globalThis, "gc", gc);
JS_FreeValue(jsContext, globalThis);
}
31 changes: 31 additions & 0 deletions zipline/native/common/global-gc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright (C) 2024 Block, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef QUICKJS_ANDROID_GLOBALGC_H
#define QUICKJS_ANDROID_GLOBALGC_H

#include "../quickjs/quickjs.h"

#ifdef __cplusplus
extern "C" {
#endif

void JS_AddGlobalThisGc(JSContext *jsContext);

#ifdef __cplusplus
} /* extern "C" { */
#endif

#endif //QUICKJS_ANDROID_GLOBALGC_H
4 changes: 4 additions & 0 deletions zipline/src/hostTest/kotlin/app/cash/zipline/QuickJsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,8 @@ class QuickJsTest {
quickJs.evaluate("""["test", true, false, 1, 1.123, undefined, null];""") as Array<Any?>,
)
}

@Test fun gc() {
assertNull(quickJs.evaluate("""globalThis.gc();"""))
}
}
3 changes: 3 additions & 0 deletions zipline/src/nativeMain/kotlin/app/cash/zipline/QuickJs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import app.cash.zipline.quickjs.JSContext
import app.cash.zipline.quickjs.JSMemoryUsage
import app.cash.zipline.quickjs.JSRuntime
import app.cash.zipline.quickjs.JSValue
import app.cash.zipline.quickjs.JS_AddGlobalThisGc
import app.cash.zipline.quickjs.JS_ComputeMemoryUsage
import app.cash.zipline.quickjs.JS_EVAL_FLAG_COMPILE_ONLY
import app.cash.zipline.quickjs.JS_EVAL_FLAG_STRICT
Expand Down Expand Up @@ -152,6 +153,8 @@ actual class QuickJs private constructor(
init {
JS_SetRuntimeOpaque(runtime, thisPtr.asCPointer())
JS_SetInterruptHandler(runtime, jsInterruptHandlerCFunction, thisPtr.asCPointer())

JS_AddGlobalThisGc(context)
}

private var closed = false
Expand Down

0 comments on commit 780a79b

Please sign in to comment.