Taritsyn/MsieJavaScriptEngine

Pass in JavaScript object reference to CallFunction

ctaggart opened this issue · 2 comments

I want to pass a JavaScript object reference in as an argument in CallFunction. Do you think this might be possible to do? I currently get this exception when using this code (from ctaggart/TsAst#5):

image

module Taser.Chakra

open MsieJavaScriptEngine

let main() =
    use jse = 
        new MsieJsEngine(
            JsEngineSettings(
                EngineMode = JsEngineMode.ChakraEdgeJsRt,
                UseEcmaScript5Polyfill = false,
                UseJson2Library = false))

    jse.Execute
        """
        function add(num1, num2) {
            return (num1 + num2);
        }
        """

    let a = jse.Evaluate "add(7, 9);" 
    printfn "a: %A" a // a: 16.0

    let b = jse.Evaluate<int> "add(7, 9);"
    printfn "b: %A" b // b: 16

    let i = jse.CallFunction<int>("add", 7, 9)
    printfn "i: %A" i // i: 16

    jse.ExecuteFile @"C:\ts\TsAst\node_modules\typescript\lib\typescriptServices.js"
    let node = jse.Evaluate @"ts.createNode(1);"
    printfn "node: %A" node // node: System.__ComObject

    jse.Execute "function getKind(node){ return node.kind; }"
    let kind = jse.CallFunction("getKind", node);
    // Exception: One of the function parameters 'getKind' has a type `System.__ComObject`, which is not supported.
    printfn "kind: %A" kind

This definitely looks possible using the lower level Edge API. MapToHostType is converting the EdgeJsValue, but I want it to hold onto it so that it can be passed into JsCallFunction somehow. I'll see what I can hack together.

Hello, Cameron!

I want to pass a JavaScript object reference in as an argument in CallFunction . Do you think this might be possible to do?

Answer to your question is contained in the text of error message. In addition, the documentation is clearly written:

The supported .NET types are as follows:

  • MsieJavaScriptEngine.Undefined
  • System.Boolean
  • System.Int32
  • System.Double
  • System.String

This definitely looks possible using the lower level Edge API. MapToHostType is converting the EdgeJsValue , but I want it to hold onto it so that it can be passed into JsCallFunction somehow. I'll see what I can hack together.

You can directly use the JsRT version of Chakra. See the following repositories: Chakra Sample Hosts and Chakra-Samples. Or wait for the release of ChakraCore.