public override PredefinedImporter GenerateImporter() { var importer = new PredefinedImporter(); importer.DefineFunction("element_spawn_object", new DelegateFunctionDefinition( ValueType.Int, ValueType.Int, arg => Invoke(arg, SpawnObject) )); importer.DefineFunction("element_get_element_index_by_id", new DelegateFunctionDefinition( ValueType.String, ValueType.Int, arg => Invoke(arg, GetElementIndexById) )); importer.DefineFunction("element_get_resource_index_by_id", new DelegateFunctionDefinition( ValueType.String, ValueType.Int, arg => Invoke(arg, GetResourceIndexById) )); return(importer); }
public override PredefinedImporter GenerateImporter() { var importer = new PredefinedImporter(); importer.DefineFunction("args_get", new DelegateFunctionDefinition( ValueType.PointerAndPointer, ValueType.Short, ArgsGet )); importer.DefineFunction("args_sizes_get", new DelegateFunctionDefinition( ValueType.PointerAndPointer, ValueType.Short, ArgsSizesGet )); importer.DefineFunction("environ_get", new DelegateFunctionDefinition( ValueType.PointerAndPointer, ValueType.Short, EnvironGet )); importer.DefineFunction("environ_sizes_get", new DelegateFunctionDefinition( ValueType.PointerAndPointer, ValueType.Short, EnvironSizesGet )); return(importer); }
// 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]); } }
/// <summary> /// Creates a new terminal I/O runtime and adds all of its definitions to the given /// importer. /// </summary> /// <param name="inputStream">The runtime's standard input stream.</param> /// <param name="outputStream">The runtime's standard output stream.</param> /// <param name="errorStream">The runtime's standard error stream.</param> /// <param name="importer">The importer.</param> public static void IncludeDefinitionsIn( Stream inputStream, Stream outputStream, Stream errorStream, PredefinedImporter importer) { new TerminalRuntime(inputStream, outputStream, errorStream).IncludeDefinitionsIn(importer); }
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); }
public override PredefinedImporter GenerateImporter() { var importer = new PredefinedImporter(); importer.DefineFunction("debug_log_info", new DelegateFunctionDefinition( ValueType.String, ValueType.Unit, LogInfo )); return(importer); }
public override PredefinedImporter GenerateImporter() { var importer = new PredefinedImporter(); importer.DefineFunction("object_spawn_object", new DelegateFunctionDefinition( ValueType.Int, ValueType.Int, SpawnObject )); return(importer); }
public override PredefinedImporter GenerateImporter() { var importer = new PredefinedImporter(); importer.DefineFunction("physics_set_world_velocity", new DelegateFunctionDefinition( ValueType.IdAndVector3, ValueType.Unit, arg => Invoke(arg, SetWorldVelocity) )); return(importer); }
public override PredefinedImporter GenerateImporter() { var importer = new PredefinedImporter(); importer.DefineFunction("text_set_text", new DelegateFunctionDefinition( ValueType.IdAndString, ValueType.Unit, SetText )); return(importer); }
public override PredefinedImporter GenerateImporter() { var importer = new PredefinedImporter(); importer.DefineFunction("fd_close", new DelegateFunctionDefinition( new WasmValueType[] { WasmValueType.Int32, }, new WasmValueType[] { WasmValueType.Int32, }, Close )); importer.DefineFunction("fd_read", new DelegateFunctionDefinition( new WasmValueType[] { WasmValueType.Int32, WasmValueType.Int32, WasmValueType.Int32, WasmValueType.Int32 }, new WasmValueType[] { WasmValueType.Int32 }, Read )); importer.DefineFunction("fd_write", new DelegateFunctionDefinition( new WasmValueType[] { WasmValueType.Int32, WasmValueType.Int32, WasmValueType.Int32, WasmValueType.Int32 }, new WasmValueType[] { WasmValueType.Int32 }, Write )); importer.DefineFunction("sock_connect", new DelegateFunctionDefinition( new WasmValueType[] { WasmValueType.Int32, WasmValueType.Int32, WasmValueType.Int32 }, new WasmValueType[] { WasmValueType.Int32, }, Connect )); importer.DefineFunction("sock_send", new DelegateFunctionDefinition( new WasmValueType[] { WasmValueType.Int32, WasmValueType.Int32, WasmValueType.Int32, WasmValueType.Int32, WasmValueType.Int32 }, new WasmValueType[] { WasmValueType.Int32, }, Send )); importer.DefineFunction("sock_recv", new DelegateFunctionDefinition( new WasmValueType[] { WasmValueType.Int32, WasmValueType.Int32, WasmValueType.Int32, WasmValueType.Int32, WasmValueType.Int32, WasmValueType.Int32 }, new WasmValueType[] { WasmValueType.Int32, }, Receive )); return(importer); }
public override PredefinedImporter GenerateImporter() { var importer = new PredefinedImporter(); importer.DefineFunction("time_get_time", new DelegateFunctionDefinition( ValueType.Unit, ValueType.Float, GetTime )); importer.DefineFunction("time_get_delta_time", new DelegateFunctionDefinition( ValueType.Unit, ValueType.Float, GetDeltaTime )); return(importer); }
public override PredefinedImporter GenerateImporter() { var importer = new PredefinedImporter(); /* * importer.DefineFunction("physics_set_local_velocity", * new DelegateFunctionDefinition( * ValueType.IdAndVector3, * ValueType.Unit, * SetLocalVelocity * )); */ importer.DefineFunction("physics_set_world_velocity", new DelegateFunctionDefinition( ValueType.IdAndVector3, ValueType.Unit, SetWorldVelocity )); return(importer); }
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); }
public override PredefinedImporter GenerateImporter() { var importer = new PredefinedImporter(); // Local position foreach (Vector3ElementType axis in Enum.GetValues(typeof(Vector3ElementType))) { importer.DefineFunction($"transform_get_local_position_{axis}", new DelegateFunctionDefinition( ValueType.ObjectId, ValueType.Float, arg => GetTransformValue(arg, t => t.localPosition.GetSpecificValue(axis)) )); } importer.DefineFunction("transform_set_local_position", new DelegateFunctionDefinition( ValueType.IdAndVector3, ValueType.Unit, SetLocalPosition )); // World position foreach (Vector3ElementType axis in Enum.GetValues(typeof(Vector3ElementType))) { importer.DefineFunction($"transform_get_world_position_{axis}", new DelegateFunctionDefinition( ValueType.ObjectId, ValueType.Float, arg => GetTransformValue(arg, t => t.position.GetSpecificValue(axis)) )); } importer.DefineFunction("transform_set_world_position", new DelegateFunctionDefinition( ValueType.IdAndVector3, ValueType.Unit, SetWorldPosition )); // Local scale foreach (Vector3ElementType axis in Enum.GetValues(typeof(Vector3ElementType))) { importer.DefineFunction($"transform_get_local_scale_{axis}", new DelegateFunctionDefinition( ValueType.ObjectId, ValueType.Float, arg => GetTransformValue(arg, t => t.localScale.GetSpecificValue(axis)) )); } importer.DefineFunction("transform_set_local_scale", new DelegateFunctionDefinition( ValueType.IdAndVector3, ValueType.Unit, SetLocalScale )); // World scale foreach (Vector3ElementType axis in Enum.GetValues(typeof(Vector3ElementType))) { importer.DefineFunction($"transform_get_world_scale_{axis}", new DelegateFunctionDefinition( ValueType.ObjectId, ValueType.Float, arg => GetTransformValue(arg, t => t.lossyScale.GetSpecificValue(axis)) )); } importer.DefineFunction("transform_set_world_scale", new DelegateFunctionDefinition( ValueType.IdAndVector3, ValueType.Unit, SetWorldScale )); /* * importer.DefineFunction("transform_get_local_position_y", * new DelegateFunctionDefinition( * ValueType.ObjectId, * ValueType.Float, * _ => new object[] { transform.localPosition.y } * )); * importer.DefineFunction("transform_get_local_position_z", * new DelegateFunctionDefinition( * ValueType.ObjectId, * ValueType.Float, * _ => new object[] { transform.localPosition.z } * )); * // local rotation * importer.DefineFunction("transform_set_local_rotation", * new DelegateFunctionDefinition( * ValueType.Quaternion, * ValueType.Unit, * SetLocalRotation * )); * importer.DefineFunction("transform_get_local_rotation", * new DelegateFunctionDefinition( * ValueType.Unit, * ValueType.Quaternion, * GetLocalRotation * )); * * // world rotation * importer.DefineFunction("transform_set_world_rotation", * new DelegateFunctionDefinition( * ValueType.IdAndQuaternion, * ValueType.Unit, * SetWorldRotation * )); * importer.DefineFunction("transform_get_world_rotation_x", * new DelegateFunctionDefinition( * ValueType.ObjectId, * ValueType.Float, * _ => new object[] { transform.rotation.x } * )); * importer.DefineFunction("transform_get_world_rotation_y", * new DelegateFunctionDefinition( * ValueType.ObjectId, * ValueType.Float, * _ => new object[] { transform.rotation.y } * )); * importer.DefineFunction("transform_get_world_rotation_z", * new DelegateFunctionDefinition( * ValueType.ObjectId, * ValueType.Float, * _ => new object[] { transform.rotation.z } * )); * importer.DefineFunction("transform_get_world_rotation_w", * new DelegateFunctionDefinition( * ValueType.ObjectId, * ValueType.Float, * _ => new object[] { transform.rotation.w } * )); * * importer.DefineFunction("transform_set_local_scale", * new DelegateFunctionDefinition( * ValueType.Vector3, * ValueType.Unit, * SetLocalScale * )); */ /* * importer.DefineFunction("transform_get_local_scale_x", * new DelegateFunctionDefinition( * ValueType.ObjectId, * ValueType.Float, * _ => new object[] { transform.localScale.x } * )); * importer.DefineFunction("transform_get_local_scale_y", * new DelegateFunctionDefinition( * ValueType.ObjectId, * ValueType.Float, * _ => new object[] { transform.localScale.y } * )); * importer.DefineFunction("transform_get_local_scale_z", * new DelegateFunctionDefinition( * ValueType.ObjectId, * ValueType.Float, * _ => new object[] { transform.localScale.z } * )); */ return(importer); }
public BindingBase(Element element, ContentsStore store) { Importer = GenerateImporter(); this.thisElement = element; this.store = store; }
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); } }
public override PredefinedImporter GenerateImporter() { var importer = new PredefinedImporter(); importer.DefineFunction("transform_set_local_position", new DelegateFunctionDefinition( ValueType.IdAndVector3, ValueType.Unit, SetLocalPosition )); importer.DefineFunction("transform_set_world_position", new DelegateFunctionDefinition( ValueType.IdAndVector3, ValueType.Unit, SetWorldPosition )); /* * importer.DefineFunction("transform_get_local_position", * new DelegateFunctionDefinition( * ValueType.Unit, * ValueType.Vector3, * GetLocalPosition * )); */ // get local position importer.DefineFunction("transform_get_local_position_x", new DelegateFunctionDefinition( ValueType.ObjectId, ValueType.Float, _ => new object[] { transform.localPosition.x } )); importer.DefineFunction("transform_get_local_position_y", new DelegateFunctionDefinition( ValueType.ObjectId, ValueType.Float, _ => new object[] { transform.localPosition.y } )); importer.DefineFunction("transform_get_local_position_z", new DelegateFunctionDefinition( ValueType.ObjectId, ValueType.Float, _ => new object[] { transform.localPosition.z } )); importer.DefineFunction("transform_get_world_position_x", new DelegateFunctionDefinition( ValueType.ObjectId, ValueType.Float, _ => new object[] { transform.position.x } )); importer.DefineFunction("transform_get_world_position_y", new DelegateFunctionDefinition( ValueType.ObjectId, ValueType.Float, _ => new object[] { transform.position.y } )); importer.DefineFunction("transform_get_world_position_z", new DelegateFunctionDefinition( ValueType.ObjectId, ValueType.Float, _ => new object[] { transform.position.z } )); // temporary function importer.DefineFunction("transform_get_world_forward_x", new DelegateFunctionDefinition( ValueType.ObjectId, ValueType.Float, _ => new object[] { transform.forward.x } )); importer.DefineFunction("transform_get_world_forward_y", new DelegateFunctionDefinition( ValueType.ObjectId, ValueType.Float, _ => new object[] { transform.forward.y } )); importer.DefineFunction("transform_get_world_forward_z", new DelegateFunctionDefinition( ValueType.ObjectId, ValueType.Float, _ => new object[] { transform.forward.z } )); // temporary function end importer.DefineFunction("transform_get_local_position_y", new DelegateFunctionDefinition( ValueType.ObjectId, ValueType.Float, _ => new object[] { transform.localPosition.y } )); importer.DefineFunction("transform_get_local_position_z", new DelegateFunctionDefinition( ValueType.ObjectId, ValueType.Float, _ => new object[] { transform.localPosition.z } )); // set local position importer.DefineFunction("transform_set_local_rotation", new DelegateFunctionDefinition( ValueType.Quaternion, ValueType.Unit, SetLocalRotation )); importer.DefineFunction("transform_get_local_rotation", new DelegateFunctionDefinition( ValueType.Unit, ValueType.Quaternion, GetLocalRotation )); importer.DefineFunction("transform_set_local_scale", new DelegateFunctionDefinition( ValueType.Vector3, ValueType.Unit, SetLocalScale )); /* * importer.DefineFunction("transform_get_local_scale", * new DelegateFunctionDefinition( * ValueType.Unit, * ValueType.Vector3, * GetLocalScale * )); */ importer.DefineFunction("transform_get_local_scale_x", new DelegateFunctionDefinition( ValueType.ObjectId, ValueType.Float, _ => new object[] { transform.localScale.x } )); importer.DefineFunction("transform_get_local_scale_y", new DelegateFunctionDefinition( ValueType.ObjectId, ValueType.Float, _ => new object[] { transform.localScale.y } )); importer.DefineFunction("transform_get_local_scale_z", new DelegateFunctionDefinition( ValueType.ObjectId, ValueType.Float, _ => new object[] { transform.localScale.z } )); return(importer); }
/// <summary> /// Adds all definitions from this runtime to the given importer. /// </summary> /// <param name="Importer">The importer.</param> private void IncludeDefinitionsIn(PredefinedImporter Importer) { Importer.IncludeDefinitions(importerVal); }
public BindingBase() { Importer = GenerateImporter(); }
public override PredefinedImporter GenerateImporter() { var importer = new PredefinedImporter(); var coordinateValues = Enum.GetValues(typeof(CoordinateType)); var vector3ElementValues = Enum.GetValues(typeof(Vector3ElementType)); var quaternionElementType = Enum.GetValues(typeof(QuaternionElementType)); // Position foreach (CoordinateType coordinate in coordinateValues) { foreach (Vector3ElementType axis in vector3ElementValues) { var functionName = $"transform_get_{coordinate}_position_{axis}"; importer.DefineFunction(functionName, new DelegateFunctionDefinition( ValueType.ObjectId, ValueType.Float, arg => Invoke(arg, parser => GetPosition(parser, axis, coordinate)) )); } importer.DefineFunction($"transform_set_{coordinate}_position", new DelegateFunctionDefinition( ValueType.IdAndVector3, ValueType.Unit, arg => Invoke(arg, parser => SetPosition(parser, coordinate)) )); } // Scale foreach (CoordinateType coordinate in coordinateValues) { foreach (Vector3ElementType axis in vector3ElementValues) { var functionName = $"transform_get_{coordinate}_scale_{axis}"; importer.DefineFunction(functionName, new DelegateFunctionDefinition( ValueType.ObjectId, ValueType.Float, arg => Invoke(arg, parser => GetScale(parser, axis, coordinate)) )); } importer.DefineFunction($"transform_set_{coordinate}_scale", new DelegateFunctionDefinition( ValueType.IdAndVector3, ValueType.Unit, arg => Invoke(arg, parser => SetScale(parser, coordinate)) )); } // Rotation foreach (CoordinateType coordinate in coordinateValues) { foreach (QuaternionElementType axis in quaternionElementType) { var functionName = $"transform_get_{coordinate}_rotation_{axis}"; importer.DefineFunction(functionName, new DelegateFunctionDefinition( ValueType.ObjectId, ValueType.Float, arg => Invoke(arg, parser => GetRotation(parser, axis, coordinate)) )); } importer.DefineFunction($"transform_set_{coordinate}_rotation", new DelegateFunctionDefinition( ValueType.IdAndQuaternion, ValueType.Unit, arg => Invoke(arg, parser => SetRotation(parser, coordinate)) )); } return(importer); }
/// <summary> /// Creates a new terminal I/O runtime and adds all of its definitions to the given /// importer. /// </summary> /// <param name="InputStream">The runtime's standard input stream.</param> /// <param name="OutputStream">The runtime's standard output stream.</param> /// <param name="ErrorStream">The runtime's standard error stream.</param> /// <param name="Importer">The importer.</param> public static void IncludeDefinitionsIn( Stream InputStream, Stream OutputStream, Stream ErrorStream, PredefinedImporter Importer) { new TerminalRuntime(InputStream, OutputStream, ErrorStream).IncludeDefinitionsIn(Importer); }