private static object EvaluateConstExpr(WasmType resultType, string expr) { var asm = AssembleModule($"(module (memory 1) (global $five (mut i32) (i32.const 5)) (func $f (export \"f\") (result {DumpHelpers.WasmTypeToString(resultType)}) {expr}) (func $constant_five (result i32) i32.const 5))"); var instance = ModuleInstance.Instantiate(asm, new PredefinedImporter()); return(instance.ExportedFunctions["f"].Invoke(Array.Empty <object>())[0]); }
// Start is called before the first frame update void Start() { // wasmファイルを読み込む string path = Application.streamingAssetsPath + "/something.wasm"; WasmFile file = WasmFile.ReadBinary(path); // importerを生成 var importer = new PredefinedImporter(); // 関数定義情報の注入 importer.DefineFunction( "GetParam", // 関数名 new DelegateFunctionDefinition( // 関数の定義 new WasmValueType[] { }, new[] { WasmValueType.Int32, }, GetParam ) ); // wasmをインスタンス化 ModuleInstance module = ModuleInstance.Instantiate(file, importer); // インスタンスから、定義済み関数の取得を試みる if (module.ExportedFunctions.TryGetValue("Something", out FunctionDefinition funcDef)) { // 関数が見つかったらそれを実行 IReadOnlyList <object> results = funcDef.Invoke(new object[] { 1, }); Debug.Log("定義があったよ==〜" + results[0]); } }
private object EvaluateConstExpr(SExpression expression, WasmValueType resultType) { var anonModule = new WasmFile(); var instructions = Assembler.AssembleInstructionExpression(expression, anonModule); var inst = ModuleInstance.Instantiate(anonModule, new SpecTestImporter()); return(inst.Evaluate(new InitializerExpression(instructions), resultType)); }
protected void LoadWasm(WasmFile file, ContentsStore store = null) { if (store == null) { store = new ContentsStore(); } var importer = new PredefinedImporter(); var wasiFunctions = new List <string>() { "proc_exit", "fd_write", "fd_prestat_get", "fd_prestat_dir_name", "environ_sizes_get", "environ_get", //"env.abort", "abort", }; foreach (var wasiFunction in wasiFunctions) { importer.DefineFunction(wasiFunction, new DelegateFunctionDefinition( new WasmValueType[] { }, new WasmValueType[] { }, x => x )); } var gameObjectBinding = new GameObjectBinding(transform, store); importer.IncludeDefinitions(gameObjectBinding.Importer); var transformBinding = new TransformBinding(transform, store); importer.IncludeDefinitions(transformBinding.Importer); var physicsBinding = new PhysicsBinding(transform, store); importer.IncludeDefinitions(physicsBinding.Importer); var timeBinding = new TimeBinding(); importer.IncludeDefinitions(timeBinding.Importer); module = ModuleInstance.Instantiate(file, importer); var exportedFunctions = module.ExportedFunctions; exportedFunctions.TryGetValue("update", out updateFunction); exportedFunctions.TryGetValue("on_touch_start", out onTouchStartFunction); }
private void Perform(WasmFile file) { var importer = new PredefinedImporter(); importer.DefineFunction( "GetParam", new DelegateFunctionDefinition( new WasmValueType[] { }, new[] { WasmValueType.Int32, }, GetParam)); ModuleInstance module = ModuleInstance.Instantiate(file, importer); if (module.ExportedFunctions.TryGetValue("Test", out FunctionDefinition funcDef2)) { IReadOnlyList <object> results = funcDef2.Invoke(new object[] { 1, }); Debug.Log(results[0]); _text.text = results[0].ToString(); } }
protected void LoadWasm(WasmFile file, ContentsStore store = null, List <string> args = null) { if (store == null) { store = new ContentsStore(); } var importer = new PredefinedImporter(); var wasiFunctions = new List <string>() { "proc_exit", "fd_read", "fd_write", "fd_prestat_get", "fd_prestat_dir_name", "environ_sizes_get", "environ_get", // "random_get", //"env.abort", "abort", }; foreach (var wasiFunction in wasiFunctions) { importer.DefineFunction(wasiFunction, new DelegateFunctionDefinition( new WasmValueType[] { }, new WasmValueType[] { }, x => x )); } importer.DefineFunction("random_get", new DelegateFunctionDefinition( ValueType.PointerAndPointer, ValueType.Int, x => ReturnValue.FromObject(0) )); var element = new Element() { GameObject = gameObject }; if (args == null) { args = new List <string>(); } var scriptName = ""; args.Insert(0, scriptName); var argsBinding = new ArgsBinding(element, store, args); importer.IncludeDefinitions(argsBinding.Importer); var debugBinding = new DebugBinding(element, store); importer.IncludeDefinitions(debugBinding.Importer); var gameObjectBinding = new GameObjectBinding(element, store); importer.IncludeDefinitions(gameObjectBinding.Importer); var transformBinding = new TransformBinding(element, store); importer.IncludeDefinitions(transformBinding.Importer); var physicsBinding = new PhysicsBinding(element, store); importer.IncludeDefinitions(physicsBinding.Importer); var timeBinding = new TimeBinding(element, store); importer.IncludeDefinitions(timeBinding.Importer); module = ModuleInstance.Instantiate(file, importer); argsBinding.ModuleInstance = module; gameObjectBinding.ModuleInstance = module; debugBinding.ModuleInstance = module; var exportedFunctions = module.ExportedFunctions; exportedFunctions.TryGetValue("start", out startFunction); exportedFunctions.TryGetValue("update", out updateFunction); exportedFunctions.TryGetValue("on_touch_start", out onTouchStartFunction); exportedFunctions.TryGetValue("on_use", out onUseFunction); }
protected void LoadWasm(WasmFile file, ContentsStore store = null, List <string> args = null) { if (store == null) { store = new ContentsStore(); } var importer = new PredefinedImporter(); var wasiFunctions = new List <string>() { //"proc_exit", "fd_prestat_get", "fd_prestat_dir_name", // "random_get", "abort", }; importer.DefineFunction("proc_exit", new DelegateFunctionDefinition( new WasmValueType[] { WasmValueType.Int32 }, new WasmValueType[] { }, x => new object[0] )); importer.DefineFunction("clock_time_get", new DelegateFunctionDefinition( new WasmValueType[] { WasmValueType.Int32, WasmValueType.Int64, WasmValueType.Int32 }, new WasmValueType[] { WasmValueType.Int32 }, GetTime )); foreach (var wasiFunction in wasiFunctions) { importer.DefineFunction(wasiFunction, new DelegateFunctionDefinition( new WasmValueType[] { }, new WasmValueType[] { }, x => ReturnValue.FromObject(0) )); } importer.DefineFunction("random_get", new DelegateFunctionDefinition( ValueType.PointerAndPointer, ValueType.Int, x => ReturnValue.FromObject(0) )); var element = new Element() { GameObject = gameObject }; // WASI functions if (args == null) { args = new List <string>(); } var scriptName = ""; args.Insert(0, scriptName); var envs = new List <string>(); var argsBinding = new ArgsBinding(element, store, args, envs); importer.IncludeDefinitions(argsBinding.Importer); var fileDescriptorBinding = new FileDescriptorBinding(element, store); importer.IncludeDefinitions(fileDescriptorBinding.Importer); // Spirare functions var debugBinding = new DebugBinding(element, store); importer.IncludeDefinitions(debugBinding.Importer); var gameObjectBinding = new GameObjectBinding(element, store, context, mainThread); importer.IncludeDefinitions(gameObjectBinding.Importer); var transformBinding = new TransformBinding(element, store, context, mainThread); importer.IncludeDefinitions(transformBinding.Importer); var physicsBinding = new PhysicsBinding(element, store, context, mainThread); importer.IncludeDefinitions(physicsBinding.Importer); var textBinding = new TextBinding(element, store, context, mainThread); importer.IncludeDefinitions(textBinding.Importer); var timeBinding = new TimeBinding(element, store); importer.IncludeDefinitions(timeBinding.Importer); try { module = ModuleInstance.Instantiate(file, importer); argsBinding.ModuleInstance = module; fileDescriptorBinding.ModuleInstance = module; gameObjectBinding.ModuleInstance = module; debugBinding.ModuleInstance = module; textBinding.ModuleInstance = module; var exportedFunctions = module.ExportedFunctions; exportedFunctions.TryGetValue("start", out startFunction); exportedFunctions.TryGetValue("update", out updateFunction); exportedFunctions.TryGetValue("on_use", out onUseFunction); exportedFunctions.TryGetValue("on_select", out onSelectFunction); exportedFunctions.TryGetValue("on_equip", out onEquipFunction); exportedFunctions.TryGetValue("on_unequip", out onEquipFunction); } catch (Exception e) { Debug.LogError(e); } }