Skip to content

Trouble calling script function from app #727

Description

@Cyprias

Hi again,

I'm trying to call a script function before disposing a engine so the script knows its being stopped. To do so I'm first trying the example to call a script function.

Creating and executing the engine.

Script runScript(ScriptPackage package)
{
	var flags = V8ScriptEngineFlags.EnableDynamicModuleImports;
	V8ScriptEngine engine = new V8ScriptEngine(flags);
	engine.DocumentSettings.AccessFlags |= DocumentAccessFlags.EnableFileLoading;
	string contents = System.IO.File.ReadAllText(package.main);
	DocumentLoader.Default.DiscardCachedDocuments();
	
	var script = new Script(package.name, package.directory);
	script.engine = engine;
	
	engine.Execute(new DocumentInfo { Category = ModuleCategory.Standard }, contents);
	return script;
}

Then in the script I have the a function.

function myFunc(x, y, z) {
    return { xValue: x, yValue: y, zValue: z };
}

Elsewhere in my app when I call my script.Dispose() function.

public class Script
{
	public V8ScriptEngine engine;
	public void Dispose()
	{
		// Where I would call a script function to notify of stopping.
		var yValue = engine.Script.myFunc(1, 2.0, "three").yValue;
		print("yValue " + yValue);
		engine.Dispose();
	}
}

I get in my log

7/26/2026 1:37:42 AM
Error: TypeError: Method or property not found
Source: ClearScript.Core
Stack:    at Microsoft.ClearScript.ScriptItem.ThrowLastScriptError()
   at CallSite.Target(Closure , CallSite , Object , Int32 , Double , String )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute4[T0,T1,T2,T3,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3)
   at App.PluginCore.Script.Dispose() in C:\Games\App\PluginCore.cs:line 1538
   at App.PluginCore.lstScripts_Selected(Object sender, Int32 row, Int32 col) in C:\Games\App\PluginCore.cs:line 1806
ErrorDetails: TypeError: Method or property not found

Then the script stops as expected.

I tried changing the script function to

myFunc = function(x, y, z) {
    return { xValue: x, yValue: y, zValue: z };
};

But then I get a error on script load.

7/26/2026 2:10:22 AM
Error: ReferenceError: myFunc is not defined
Source: ClearScript.V8
Stack:    at Microsoft.ClearScript.V8.SplitProxy.V8SplitProxyNative.Invoke[TArg](Action`2 action, TArg& arg)
   at Microsoft.ClearScript.V8.SplitProxy.V8ContextProxyImpl.InvokeWithLock[TArg](Action`1 action, TArg& arg)
   at Microsoft.ClearScript.V8.V8ScriptEngine.ScriptInvoke[TArg,TResult](Func`2 func, TArg& arg)
   at Microsoft.ClearScript.V8.V8ScriptEngine.Execute(UniqueDocumentInfo documentInfo, String code, Boolean evaluate)
   at Microsoft.ClearScript.ScriptEngine.Execute(DocumentInfo documentInfo, String code)
   at App.PluginCore.runScript(ScriptPackage package) in C:\Games\App\PluginCore.cs:line 1226
   at App.PluginCore.lstScripts_Selected(Object sender, Int32 row, Int32 col) in C:\Games\App\PluginCore.cs:line 1805
Inner: ReferenceError: myFunc is not defined
Inner Stack:    at Microsoft.ClearScript.V8.SplitProxy.V8SplitProxyNative.Invoke[TResult,TArg](Func`3 func, TArg& arg)
   at Microsoft.ClearScript.V8.SplitProxy.V8ContextProxyImpl.Execute(UniqueDocumentInfo documentInfo, String code, Boolean evaluate)
   at Microsoft.ClearScript.V8.V8ScriptEngine.ExecuteRaw(UniqueDocumentInfo documentInfo, String code, Boolean evaluate)
   at Microsoft.ClearScript.V8.V8ScriptEngine.ExecuteInternal(UniqueDocumentInfo documentInfo, String code, Boolean evaluate)
   at Microsoft.ClearScript.V8.V8ScriptEngine.<>c.<Execute>b__139_0(ValueTuple`4 ctx)
   at Microsoft.ClearScript.ScriptEngine.ScriptInvokeInternal[TArg,TResult](Func`2 func, TArg& arg)
   at Microsoft.ClearScript.V8.V8ScriptEngine.<>c__148`2.<ScriptInvoke>b__148_0(StructPtr`1 pCtx)
   at Microsoft.ClearScript.V8.SplitProxy.V8ContextProxyImpl.<>c__14`1.<InvokeWithLock>b__14_0(IntPtr pCtx)
   at Microsoft.ClearScript.V8.SplitProxy.V8SplitProxyManaged.InvokeHostActionWithArg(IntPtr pAction, IntPtr pArg)
ErrorDetails: ReferenceError: myFunc is not defined
    at Module:6:8

From reading some other issue threads I'm guessing the module loading is messing up the calling of functions? Is there a better way to accomplish my goal?

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions