Пример #1
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]);
        }
    }
Пример #2
0
        public static int Main(string[] args)
        {
            CatArgs parsedArgs;

            if (!CatArgs.TryParse(args, out parsedArgs))
            {
                Console.Error.WriteLine("usage: wasm-cat file.wasm... [-o output.wasm]");
                return(1);
            }

            var file = new WasmFile();

            foreach (var path in parsedArgs.Inputs)
            {
                // Read the file and append its sections to the resulting file.
                var inputFile = WasmFile.ReadBinary(path);
                file.Sections.AddRange(inputFile.Sections);

                // Also, set the WebAssembly version number to the max of the
                // input files.
                if (inputFile.Header.Version > file.Header.Version)
                {
                    file.Header = inputFile.Header;
                }
            }

            // Now write the file to standard output.
            using (var outputStream = string.IsNullOrEmpty(parsedArgs.Output)
                ? Console.OpenStandardOutput()
                : File.OpenWrite(parsedArgs.Output))
            {
                file.WriteBinaryTo(outputStream);
            }
            return(0);
        }
    private void LoadFromFile()
    {
        string   path = Application.streamingAssetsPath + "/test.wasm";
        WasmFile file = WasmFile.ReadBinary(path);

        Perform(file);
    }
Пример #4
0
        public static int Main(string[] args)
        {
            if (args.Length > 1)
            {
                Console.Error.WriteLine("usage: wasm-dump [file.wasm]");
                return(1);
            }

            WasmFile file;

            if (args.Length == 0)
            {
                using (var input = ReadStdinToEnd())
                {
                    file = WasmFile.ReadBinary(input);
                }
            }
            else
            {
                file = WasmFile.ReadBinary(args[0]);
            }
            file.Dump(Console.Out);
            Console.WriteLine();
            return(0);
        }
Пример #5
0
        public static void Main(string[] args)
        {
            // Create an empty WebAssembly file.
            var file = new WasmFile();

            // Define a type section.
            var typeSection = new TypeSection();

            file.Sections.Add(typeSection);

            // Write the file to a (memory) stream.
            var stream = new MemoryStream();

            file.WriteBinaryTo(stream);
            stream.Seek(0, SeekOrigin.Begin);

            // Read the file from a (memory) stream.
            file = WasmFile.ReadBinary(stream);
            stream.Seek(0, SeekOrigin.Begin);

            // Define a memory section if it doesn't exist already.
            var memSection = file.GetFirstSectionOrNull <MemorySection>();

            if (memSection == null)
            {
                // The file doesn't specify a memory section, so we'll
                // have to create one and add it to the file.
                memSection = new MemorySection();
                file.Sections.Add(memSection);
            }

            memSection.Memories.Clear();
            // Memory sizes are specified in WebAssembly pages,
            // which are regions of storage with size 64KiB.
            // `new ResizableLimits(1, 1)` creates a memory description
            // that is initially one page (first argument) in size and
            // is capped at one page of memory (second argument), so
            // there will always be exactly one page of linear memory.
            memSection.Memories.Add(
                new MemoryType(new ResizableLimits(1, 1)));

            // Print the memory size.
            List <MemoryType> memSections =
                file.GetFirstSectionOrNull <MemorySection>()
                .Memories;

            Console.WriteLine(
                "Memory size: {0}",
                memSections
                .Single <MemoryType>()
                .Limits);

            // Save the file again.
            file.WriteBinaryTo(stream);
            stream.Seek(0, SeekOrigin.Begin);
        }
    private void LoadFromServer()
    {
        UnityWebRequest req = UnityWebRequest.Get(_url);

        req.SendWebRequest().completed += operation =>
        {
            MemoryStream stream = new MemoryStream();

            stream.Write(req.downloadHandler.data, 0, req.downloadHandler.data.Length);
            stream.Seek(0, SeekOrigin.Begin);

            WasmFile file = WasmFile.ReadBinary(stream);

            Perform(file);
        };
    }
Пример #7
0
        public static int Main(string[] args)
        {
            OptArgs parsedArgs;

            if (!OptArgs.TryParse(args, out parsedArgs))
            {
                Console.Error.WriteLine("usage: wasm-opt file.wasm [-o output.wasm]");
                return(1);
            }

            // Read the file.
            var file = WasmFile.ReadBinary(parsedArgs.Input);

            file.Optimize();

            // Now write the file to standard output.
            using (var outputStream = string.IsNullOrEmpty(parsedArgs.Output)
                ? Console.OpenStandardOutput()
                : File.OpenWrite(parsedArgs.Output))
            {
                file.WriteBinaryTo(outputStream);
            }
            return(0);
        }
Пример #8
0
        public static int Main(string[] args)
        {
            // Read command-line arguments.
            InterpreterArguments parsedArgs;

            if (!InterpreterArguments.TryRead(args, out parsedArgs))
            {
                return(PrintUsage());
            }

            IImporter importer;

            if (!parsedArgs.TryGetImporter(out importer))
            {
                Console.Error.WriteLine("error: there is no importer named '" + parsedArgs.ImporterName + "'");
                return(1);
            }

            // Read and instantiate the module.
            var wasmFile = WasmFile.ReadBinary(parsedArgs.WasmFilePath);
            InstructionInterpreter interp = DefaultInstructionInterpreter.Default;

            if (parsedArgs.TraceExecution)
            {
                interp = new TracingInstructionInterpreter(interp, Console.Error);
            }
            var module = ModuleInstance.Instantiate(wasmFile, importer, interp);

            // Figure out which function to run.
            FunctionDefinition funcToRun = null;

            if (parsedArgs.FunctionToRun != null)
            {
                if (!module.ExportedFunctions.TryGetValue(parsedArgs.FunctionToRun, out funcToRun))
                {
                    Console.Error.WriteLine(
                        "error: module does not export a function named '" +
                        parsedArgs.FunctionToRun + "'");
                    return(1);
                }
            }
            else
            {
                var startSec = wasmFile.GetFirstSectionOrNull <StartSection>();
                if (startSec == null)
                {
                    Console.Error.WriteLine(
                        "error: module does not define a 'start' section " +
                        " and '--run exported_func_name' was not specified.");
                    return(1);
                }
                else
                {
                    IReadOnlyList <FunctionDefinition> funcs = module.Functions;
                    funcToRun = funcs[(int)startSec.StartFunctionIndex];
                }
            }

            // Run that function.
            int exitCode = 0;

            try
            {
                IReadOnlyList <object> output = funcToRun.Invoke(parsedArgs.FunctionArgs);
                if (output.Count > 0)
                {
                    for (int i = 0; i < output.Count; i++)
                    {
                        if (i > 0)
                        {
                            Console.Write(" ");
                        }
                        Console.Write(output[i]);
                    }
                    Console.WriteLine();
                }
            }
            catch (WasmException ex)
            {
                Console.Error.WriteLine("error: " + ex.Message);
                exitCode = 1;
            }
            return(exitCode);
        }
Пример #9
0
        public void LoadWasm(Stream stream, ContentsStore store = null, List <string> args = null)
        {
            var file = WasmFile.ReadBinary(stream);

            LoadWasm(file, store, args);
        }
Пример #10
0
        public void LoadWasm(Stream stream, ContentsStore store = null)
        {
            var file = WasmFile.ReadBinary(stream);

            LoadWasm(file, store);
        }
Пример #11
0
        public void LoadWasm(string path, ContentsStore store = null)
        {
            var file = WasmFile.ReadBinary(path);

            LoadWasm(file, store);
        }