예제 #1
0
        /// <summary>
        /// Instantiates the given WebAssembly file. An importer is used to
        /// resolve module imports and an interpreter is used to interpret
        /// instructions.
        /// </summary>
        /// <param name="File">The file to instantiate.</param>
        /// <param name="Importer">Resolves module imports.</param>
        /// <param name="Interpreter">Interprets instructions.</param>
        /// <returns>A module instance.</returns>
        public static ModuleInstance Instantiate(
            WasmFile File,
            IImporter Importer,
            InstructionInterpreter Interpreter)
        {
            var instance = new ModuleInstance(Interpreter);

            // Extract the function types.
            var allFuncTypes = GetFunctionTypes(File);

            // Resolve all imports.
            instance.ResolveImports(File, Importer, allFuncTypes);

            // Instantiate global variables.
            instance.InstantiateGlobals(File);

            // Instantiate memories.
            instance.InstantiateMemories(File);

            // Instantiate function definitions.
            instance.InstantiateFunctionDefs(File, allFuncTypes);

            // Instantiate function tables.
            instance.InstantiateTables(File);

            // Export values.
            instance.RegisterExports(File);

            return(instance);
        }
예제 #2
0
        /// <summary>
        /// Instantiates the given WebAssembly file.
        /// </summary>
        /// <param name="file">The file to instantiate.</param>
        /// <param name="importer">The importer to use to resolve module imports.</param>
        /// <param name="interpreter">
        /// Interprets instructions. A <c>null</c> interpreter indicates that the default interpreter should be used.
        /// </param>
        /// <param name="policy">
        /// The execution policy to adhere to for this module.
        /// A <c>null</c> execution policy indicates that the default policy should be used.
        /// </param>
        /// <param name="compiler">
        /// Creates a new instance of a module compiler to use.
        /// </param>
        /// <returns>A module instance.</returns>
        public static ModuleInstance Instantiate(
            WasmFile file,
            IImporter importer,
            InstructionInterpreter interpreter = null,
            ExecutionPolicy policy             = null,
            Func <ModuleCompiler> compiler     = null)
        {
            if (interpreter == null)
            {
                interpreter = DefaultInstructionInterpreter.Default;
            }
            if (policy == null)
            {
                policy = ExecutionPolicy.Create();
            }
            if (compiler == null)
            {
                compiler = () => new InterpreterCompiler();
            }

            var instance = new ModuleInstance(interpreter, policy);

            // Extract the function types.
            var allFuncTypes = GetFunctionTypes(file);

            instance.definedTypes.AddRange(allFuncTypes);

            // Resolve all imports.
            instance.ResolveImports(file, importer, allFuncTypes);

            // Instantiate global variables.
            instance.InstantiateGlobals(file);

            // Instantiate memories.
            instance.InstantiateMemories(file, policy.MaxMemorySize);

            // Instantiate function definitions.
            instance.InstantiateFunctionDefs(file, compiler(), allFuncTypes);

            // Instantiate function tables.
            instance.InstantiateTables(file);

            // Export values.
            instance.RegisterExports(file);

            return(instance);
        }