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.77k stars 148 forks source link

Question: try pattern from script #570

Closed dementcore closed 6 months ago

dementcore commented 7 months ago

Hi!

It is possible to invoke a host function with out parameters from script and get the value in script after invoke?

Example host code:

public int tryread(int count, out string result) 
{
    result = "Test";
    return 10;
}

Example script code:

let variable= '';
var result = myhostobj.tryread(5,variable);
variable == 'Test'; //the host method invocation fills this script variable from the out parameter of tryread host method???

Thanks in advance!

ClearScriptLib commented 7 months ago

Hi @dementcore,

You can use a host variable for this purpose.

First, you'll need a HostFunctions object:

engine.AddHostObject("host", new HostFunctions());

Once you have that, you can do the following in JavaScript:

let variable= host.newVar('');
var result = myhostobj.tryread(5,variable.out);
variable.value == 'Test'; // true

Good luck!

ClearScriptLib commented 6 months ago

Please reopen this issue if you have additional thoughts or questions about this topic. Thank you!