Example #1
0
        private WasmtimeException Define(string moduleName, string name, IntPtr ext)
        {
            var moduleNameBytes = Encoding.UTF8.GetBytes(moduleName);
            var nameBytes       = Encoding.UTF8.GetBytes(name);

            unsafe
            {
                fixed(byte *moduleNamePtr = moduleNameBytes)
                fixed(byte *namePtr = nameBytes)
                {
                    Interop.wasm_byte_vec_t moduleNameVec = new Interop.wasm_byte_vec_t();
                    moduleNameVec.size = (UIntPtr)moduleNameBytes.Length;
                    moduleNameVec.data = moduleNamePtr;

                    Interop.wasm_byte_vec_t nameVec = new Interop.wasm_byte_vec_t();
                    nameVec.size = (UIntPtr)nameBytes.Length;
                    nameVec.data = namePtr;

                    var error = Interop.wasmtime_linker_define(Linker, ref moduleNameVec, ref nameVec, ext);

                    if (error != IntPtr.Zero)
                    {
                        return(WasmtimeException.FromOwnedError(error));
                    }

                    return(null);
                }
            }
        }
Example #2
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);
        }
Example #3
0
        /// <summary>
        /// Loads a <see cref="Module"/> based on a WebAssembly text format representation.
        /// </summary>
        /// <param name="name">The name of the module.</param>
        /// <param name="text">The WebAssembly text format representation of the module.</param>
        /// <returns>Returns a new <see cref="Module"/>.</returns>
        public Module LoadModuleText(string name, string text)
        {
            CheckDisposed();

            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (text is null)
            {
                throw new ArgumentNullException(nameof(text));
            }

            var textBytes = Encoding.UTF8.GetBytes(text);

            unsafe
            {
                fixed(byte *ptr = textBytes)
                {
                    Interop.wasm_byte_vec_t textVec;
                    textVec.size = (UIntPtr)textBytes.Length;
                    textVec.data = ptr;

                    var error = Interop.wasmtime_wat2wasm(ref textVec, out var bytes);

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

                    var byteSpan    = new ReadOnlySpan <byte>(bytes.data, checked ((int)bytes.size));
                    var moduleBytes = byteSpan.ToArray();

                    Interop.wasm_byte_vec_delete(ref bytes);
                    return(LoadModule(name, moduleBytes));
                }
            }
        }
Example #4
0
        /// <summary>
        /// Defines a WASI implementation in the host.
        /// </summary>
        /// <param name="name">The name of the WASI module to define.</param>
        /// <param name="config">The <see cref="WasiConfiguration"/> to configure the WASI implementation with.</param>
        public void DefineWasi(string name, WasiConfiguration config = null)
        {
            CheckDisposed();

            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("Name cannot be null or empty.", nameof(name));
            }

            if (config is null)
            {
                config = new WasiConfiguration();
            }

            using var wasi = config.CreateWasi(Store, name);

            var error = Interop.wasmtime_linker_define_wasi(Linker, wasi);

            if (error != IntPtr.Zero)
            {
                throw WasmtimeException.FromOwnedError(error);
            }
        }
Example #5
0
        internal Module(Interop.StoreHandle store, string name, byte[] bytes)
        {
            unsafe
            {
                fixed(byte *ptr = bytes)
                {
                    Interop.wasm_byte_vec_t vec;
                    vec.size = (UIntPtr)bytes.Length;
                    vec.data = ptr;

                    var error = Interop.wasmtime_module_new(store, ref vec, out var handle);

                    if (error != IntPtr.Zero)
                    {
                        throw new WasmtimeException($"WebAssembly module '{name}' is not valid: {WasmtimeException.FromOwnedError(error).Message}");
                    }

                    Handle = handle;
                }
            }

            Name    = name;
            Imports = new Wasmtime.Imports.Imports(this);
            Exports = new Wasmtime.Exports.Exports(this);
        }