Taritsyn / MsieJavaScriptEngine

.NET wrapper for working with the JavaScript engines of Internet Explorer and Edge Legacy.
Apache License 2.0
106 stars 22 forks source link

Pass in JavaScript object reference to CallFunction #5

Closed ctaggart closed 8 years ago

ctaggart commented 8 years ago

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 https://github.com/ctaggart/TsAst/issues/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
ctaggart commented 8 years ago

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.

Taritsyn commented 8 years ago

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:

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.