nativeMain.com.dokar.quickjs.bridge.invokeJsFunction.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quickjs-kt Show documentation
Show all versions of quickjs-kt Show documentation
A QuickJS binding for idiomatic Kotlin, with Async/DSL/ES Modules support.
The newest version!
package com.dokar.quickjs.bridge
import com.dokar.quickjs.util.allocArrayOf
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.CValue
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.memScoped
import quickjs.JSContext
import quickjs.JSValue
import quickjs.JS_Call
import quickjs.JS_FreeValue
import quickjs.JS_GetRuntime
import quickjs.JS_UpdateStackTop
import quickjs.JsNull
@OptIn(ExperimentalForeignApi::class)
internal fun CPointer.invokeJsFunction(
func: CValue,
args: Array,
): Unit = memScoped {
val context = this@invokeJsFunction
JS_UpdateStackTop(JS_GetRuntime(context))
val jsArgs = Array(args.size) { JsNull() }
for (i in jsArgs.indices) {
try {
jsArgs[i] = args[i].toJsValue(context)
} catch (e: Throwable) {
for (j in 0..