All Downloads are FREE. Search and download functionalities are using the official Maven repository.

nativeMain.com.dokar.quickjs.bridge.invokeJsFunction.kt Maven / Gradle / Ivy

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..




© 2015 - 2024 Weber Informatics LLC | Privacy Policy