Beispiel #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);
                }
            }
        }
Beispiel #2
0
        private bool 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;

                    return(Interop.wasmtime_linker_define(Linker, ref moduleNameVec, ref nameVec, ext));
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Gets an exported memory of the caller by the given name.
        /// </summary>
        /// <param name="name">The name of the exported memory.</param>
        /// <returns>Returns the exported memory if found or null if a memory of the requested name is not exported.</returns>
        public CallerMemory GetMemory(string name)
        {
            if (Handle == IntPtr.Zero)
            {
                throw new InvalidOperationException();
            }

            unsafe
            {
                var bytes = Encoding.UTF8.GetBytes(name);

                fixed(byte *ptr = bytes)
                {
                    Interop.wasm_byte_vec_t nameVec = new Interop.wasm_byte_vec_t();
                    nameVec.size = (UIntPtr)bytes.Length;
                    nameVec.data = ptr;

                    var export = Interop.wasmtime_caller_export_get(Handle, ref nameVec);

                    if (export.IsInvalid)
                    {
                        return(null);
                    }

                    var memory = Interop.wasm_extern_as_memory(export.DangerousGetHandle());

                    if (memory == IntPtr.Zero)
                    {
                        export.Dispose();
                        return(null);
                    }

                    return(new CallerMemory(export, memory));
                }
            }
        }