microsoft / ClearScript

A library for adding scripting to .NET applications. Supports V8 (Windows, Linux, macOS) and JScript/VBScript (Windows).
https://microsoft.github.io/ClearScript/
MIT License
1.74k stars 148 forks source link

out parameters with .NET arrays #496

Closed OuterHeaven07 closed 1 year ago

OuterHeaven07 commented 1 year ago

Hi @ClearScriptLib

I have a problem with the keyword out and .NET arrays. The array object itself does not seem to have an .out method implemented

here is a simple example

//Script
var stringArray= host.newArr(System.String, 3)

method1(stringArray.out) // --> error: method1 got invalid arguments
// c#
    public void method1(out string[] strArray) 
    { 
             // Initialize the array:
            strArray = new string[3] { "do", "some", "thing"};
    } 

Basically the out parameter works with single variables, but unfortunately I can't get it to work with arrays.

Thanks for the support

ClearScriptLib commented 1 year ago

Hi @OuterHeaven07,

out is a property of host variables. To call method1, you must first create a host variable of type string[]. You can use an initial dummy value to avoid having to provide an explicit type argument when invoking newVar:

const initialDummyValue = host.newArr(System.String, 0);
const arrayVar = host.newVar(initialDummyValue);
method1(arrayVar.out);
System.Console.WriteLine(System.String.Join(', ', arrayVar /* or arrayVar.value */));

Good luck!

OuterHeaven07 commented 1 year ago

Thank you very much. That was exactly what I needed