Exemple #1
0
        internal Instance(Interop.LinkerHandle linker, Module module)
        {
            Module = module;

            unsafe
            {
                var error = Interop.wasmtime_linker_instantiate(linker, module.Handle, out var handle, out var trap);
                Handle = handle;

                if (error != IntPtr.Zero)
                {
                    throw WasmtimeException.FromOwnedError(error);
                }
                if (trap != IntPtr.Zero)
                {
                    throw TrapException.FromOwnedTrap(trap);
                }
            }

            if (Handle.IsInvalid)
            {
                throw new WasmtimeException("Failed to create Wasmtime instance.");
            }

            Interop.wasm_instance_exports(Handle, out _externs);

            Externs = new Wasmtime.Externs.Externs(Module.Exports, _externs);

            _functions = Externs.Functions.ToDictionary(f => f.Name);
            _globals   = Externs.Globals.ToDictionary(g => g.Name);
        }
        internal Instance(Module module, IHost host)
        {
            Host   = host;
            Module = module;

            //Save the bindings to root the objects.
            //Otherwise the GC may collect the delegates from ExternFunction for example.
            _bindings = host.GetImportBindings(module);
            var handles = _bindings.Select(b => b.Bind(module.Store, host)).ToList();

            unsafe
            {
                Handle = Interop.wasm_instance_new(
                    Module.Store.Handle,
                    Module.Handle,
                    handles.Select(h => ToExtern(h)).ToArray(),
                    out var trap);

                if (trap != IntPtr.Zero)
                {
                    throw TrapException.FromOwnedTrap(trap);
                }
            }

            if (Handle.IsInvalid)
            {
                throw new WasmtimeException($"Failed to instantiate module '{module.Name}'.");
            }

            // Dispose of all function handles (not needed at runtime)
            foreach (var h in handles.Where(h => h is Interop.FunctionHandle))
            {
                h.Dispose();
            }

            Interop.wasm_instance_exports(Handle, out _externs);

            Externs = new Wasmtime.Externs.Externs(Module.Exports, _externs);

            _functions = Externs.Functions.ToDictionary(f => f.Name);
            _globals   = Externs.Globals.ToDictionary(g => g.Name);
        }
Exemple #3
0
        internal Instance(Module module, Wasi wasi = null, IHost host = null)
        {
            Host   = host;
            Module = module;

            // Save the bindings to root the objects.
            // Otherwise the GC may collect the callback delegates from FunctionHandles for example.
            _bindings = Binding.GetImportBindings(module, wasi, host)
                        .Select(b => b.Bind(module.Store, host))
                        .ToArray();

            unsafe
            {
                Handle = Interop.wasm_instance_new(
                    Module.Store.Handle,
                    Module.Handle,
                    _bindings.Select(h => ToExtern(h)).ToArray(),
                    out var trap);

                if (trap != IntPtr.Zero)
                {
                    throw TrapException.FromOwnedTrap(trap);
                }
            }

            if (Handle.IsInvalid)
            {
                throw new WasmtimeException($"Failed to instantiate module '{module.Name}'.");
            }

            Interop.wasm_instance_exports(Handle, out _externs);

            Externs = new Wasmtime.Externs.Externs(Module.Exports, _externs);

            _functions = Externs.Functions.ToDictionary(f => f.Name);
            _globals   = Externs.Globals.ToDictionary(g => g.Name);
        }