Exemple #1
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);
        }
        internal MutableGlobal(Interop.StoreHandle store, T initialValue)
        {
            if (!Interop.TryGetValueKind(typeof(T), out var kind))
            {
                throw new WasmtimeException($"Mutable global variables cannot be of type '{typeof(T).ToString()}'.");
            }

            Kind = kind;

            var value = Interop.ToValue((object)initialValue, Kind);

            var valueType       = Interop.wasm_valtype_new(value.kind);
            var valueTypeHandle = valueType.DangerousGetHandle();

            valueType.SetHandleAsInvalid();

            using var globalType = Interop.wasm_globaltype_new(
                      valueTypeHandle,
                      Interop.wasm_mutability_t.WASM_VAR
                      );

            unsafe
            {
                Handle = Interop.wasm_global_new(store, globalType, &value);

                if (Handle.IsInvalid)
                {
                    throw new WasmtimeException("Failed to create mutable Wasmtime global.");
                }
            }
        }
Exemple #3
0
        internal Memory(Interop.StoreHandle store, uint minimum = 1, uint maximum = uint.MaxValue)
        {
            if (minimum == 0)
            {
                throw new ArgumentException("The minimum cannot be zero.", nameof(minimum));
            }

            if (maximum < minimum)
            {
                throw new ArgumentException("The maximum cannot be less than the minimum.", nameof(maximum));
            }

            Minimum = minimum;
            Maximum = maximum;

            unsafe
            {
                Interop.wasm_limits_t limits = new Interop.wasm_limits_t();
                limits.min = minimum;
                limits.max = maximum;

                using var memoryType = Interop.wasm_memorytype_new(&limits);
                Handle = Interop.wasm_memory_new(store, memoryType);

                if (Handle.IsInvalid)
                {
                    throw new WasmtimeException("Failed to create Wasmtime memory.");
                }
            }
        }
Exemple #4
0
        internal Function(Interop.StoreHandle store, Delegate func, bool hasReturn)
        {
            if (func is null)
            {
                throw new ArgumentNullException(nameof(func));
            }

            var         type           = func.GetType();
            Span <Type> parameterTypes = null;
            Type        returnType     = null;

            if (hasReturn)
            {
                parameterTypes = type.GenericTypeArguments[0..^ 1];
Exemple #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;

                    Handle = Interop.wasm_module_new(store, ref vec);
                }

                if (Handle.IsInvalid)
                {
                    throw new WasmtimeException($"WebAssembly module '{name}' is not valid.");
                }
            }

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