Ejemplo n.º 1
0
 private static bool TryTranslateException(Exception original, out Exception translated)
 {
     if (original.GetType() == typeof(DivideByZeroException))
     {
         translated = new TrapException(original.Message, TrapException.SpecMessages.IntegerDivideByZero);
         return(true);
     }
     else if (original.GetType() == typeof(OverflowException))
     {
         translated = new TrapException(original.Message, TrapException.SpecMessages.IntegerOverflow);
         return(true);
     }
     else
     {
         translated = null;
         return(false);
     }
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Invokes the WebAssembly function.
        /// </summary>
        /// <param name="arguments">The array of arguments to pass to the function.</param>
        /// <returns>
        ///   Returns null if the function has no return value.
        ///   Returns the value if the function returns a single value.
        ///   Returns an array of values if the function returns more than one value.
        /// </returns>
        public object Invoke(params object[] arguments)
        {
            if (arguments.Length != Parameters.Count)
            {
                throw new WasmtimeException($"Argument mismatch when invoking function '{Name}': requires {Parameters.Count} but was given {arguments.Length}.");
            }

            unsafe
            {
                Interop.wasm_val_t *args    = stackalloc Interop.wasm_val_t[Parameters.Count];
                Interop.wasm_val_t *results = stackalloc Interop.wasm_val_t[Results.Count];

                for (int i = 0; i < arguments.Length; ++i)
                {
                    args[i] = Interop.ToValue(arguments[i], Parameters[i]);
                }

                var trap = Interop.wasm_func_call(_func, args, results);
                if (trap != IntPtr.Zero)
                {
                    throw TrapException.FromOwnedTrap(trap);
                }

                if (Results.Count == 0)
                {
                    return(null);
                }

                if (Results.Count == 1)
                {
                    return(Interop.ToObject(&results[0]));
                }

                var ret = new object[Results.Count];
                for (int i = 0; i < Results.Count; ++i)
                {
                    ret[i] = Interop.ToObject(&results[i]);
                }
                return(ret);
            }
        }