Ejemplo n.º 1
0
        public void EvalSource(string source, string filename)
        {
            if (filename == null)
            {
                filename = "eval";
            }
            if (string.IsNullOrEmpty(source))
            {
                return;
            }
            var ctx = _ctx.rawValue;

            DuktapeDLL.duk_push_string(ctx, source);
            DuktapeDLL.duk_push_string(ctx, filename);
            if (DuktapeDLL.duk_pcompile(ctx, 0) != 0)
            {
                DuktapeAux.PrintError(ctx, -1, filename);
                DuktapeDLL.duk_pop(ctx);
            }
            else
            {
                // Debug.LogFormat("check top {0}", DuktapeDLL.duk_get_top(ctx));
                if (DuktapeDLL.duk_pcall(ctx, 0) != DuktapeDLL.DUK_EXEC_SUCCESS)
                {
                    DuktapeAux.PrintError(ctx, -1, filename);
                }
                DuktapeDLL.duk_pop(ctx);
                // Debug.LogFormat("check top {0}", DuktapeDLL.duk_get_top(ctx));
            }
        }
Ejemplo n.º 2
0
        public void EvalMain(string filename, byte[] source)
        {
            if (source == null || source.Length == 0)
            {
                return;
            }
            if (source[0] == 0xbf)
            {
                //NOTE: module is not supported in bytecode mode
                EvalSource(filename, source);
            }
            else
            {
                if (filename == null)
                {
                    filename = "eval";
                }
                var ctx = _ctx.rawValue;
                var top = DuktapeDLL.duk_get_top(ctx);

                DuktapeDLL.duk_unity_push_lstring(ctx, source, (uint)source.Length);
                var err = DuktapeDLL.duk_module_node_peval_main(ctx, filename);
                // var err = DuktapeDLL.duk_peval(ctx);
                // var err = DuktapeDLL.duk_peval_string_noresult(ctx, source);
                // Debug.Log($"load main module: {filename} ({resolvedPath})");
                if (err != 0)
                {
                    DuktapeAux.PrintError(ctx, -1, filename);
                    // Debug.LogErrorFormat("eval main error: {0}\n{1}", DuktapeDLL.duk_safe_to_string(ctx, -1), filename);
                }
                DuktapeDLL.duk_set_top(ctx, top);
            }
        }
Ejemplo n.º 3
0
        public bool InvokeMemberWithBooleanReturn(string name)
        {
            var member = GetMember(name);

            if (member != null)
            {
                var ctx = member.context.rawValue;
                if (ctx != IntPtr.Zero)
                {
                    member.Push(ctx);
                    this.Push(ctx);
                    var ret = DuktapeDLL.duk_pcall_method(ctx, 0);
                    if (ret != DuktapeDLL.DUK_EXEC_SUCCESS)
                    {
                        DuktapeAux.PrintError(ctx, -1);
                        DuktapeDLL.duk_pop(ctx);
                        throw new Exception("InvokeMemberWithBooleanReturn failed");
                    }

                    var o = DuktapeDLL.duk_get_boolean_default(ctx, -1, false);
                    DuktapeDLL.duk_pop(ctx);
                    return(o);
                }
            }

            return(false);
        }
Ejemplo n.º 4
0
        // 根据当前栈参数数量调用函数
        // 调用失败时抛异常, 成功时栈上保留返回值
        public void EndInvoke(IntPtr ctx)
        {
            var nargs = DuktapeDLL.duk_get_top(ctx) - _savedState;
            var ret   = DuktapeDLL.duk_pcall_method(ctx, nargs);

            if (ret != DuktapeDLL.DUK_EXEC_SUCCESS)
            {
                DuktapeAux.PrintError(ctx, -1);
                DuktapeDLL.duk_pop(ctx);
                throw new Exception("duktape delegate exception");
            }
        }
Ejemplo n.º 5
0
 // 获取全局函数并调用 (do not cache it)
 public void Invoke(string funcName)
 {
     DuktapeDLL.duk_push_global_object(_ctx);
     DuktapeDLL.duk_get_prop_string(_ctx, -1, funcName);
     if (DuktapeDLL.duk_is_function(_ctx, -1))
     {
         if (DuktapeDLL.duk_pcall(_ctx, 0) != DuktapeDLL.DUK_EXEC_SUCCESS)
         {
             DuktapeAux.PrintError(_ctx, -1);
         }
     }
     DuktapeDLL.duk_pop_2(_ctx);
 }
Ejemplo n.º 6
0
        public object EvalSource(string filename, byte[] source)
        {
            object retValue = null;

            if (source == null || source.Length == 0)
            {
                return(retValue);
            }
            if (filename == null)
            {
                filename = "eval";
            }

            var ctx = _ctx.rawValue;

            if (source[0] == 0xbf)
            {
                // load bytecode...
                var buffer_ptr = DuktapeDLL.duk_push_fixed_buffer(ctx, (uint)source.Length);
                Marshal.Copy(source, 0, buffer_ptr, source.Length);
                DuktapeDLL.duk_load_function(ctx);
            }
            else
            {
                DuktapeDLL.duk_unity_push_lstring(ctx, source, (uint)source.Length);
                DuktapeDLL.duk_push_string(ctx, filename);
                if (DuktapeDLL.duk_pcompile(ctx, 0) != 0)
                {
                    DuktapeAux.PrintError(ctx, -1, filename);
                    DuktapeDLL.duk_pop(ctx);
                    throw new Exception("[duktape] source compile failed");
                }
            }

            // Debug.LogFormat("check top {0}", DuktapeDLL.duk_get_top(ctx));
            if (DuktapeDLL.duk_pcall(ctx, 0) != DuktapeDLL.DUK_EXEC_SUCCESS)
            {
                DuktapeAux.PrintError(ctx, -1, filename);
                DuktapeDLL.duk_pop(ctx);
                throw new Exception("[duktape] source eval failed");
            }
            DuktapeBinding.duk_get_var(ctx, -1, out retValue);
            DuktapeDLL.duk_pop(ctx);
            return(retValue);
            // Debug.LogFormat("check top {0}", DuktapeDLL.duk_get_top(ctx));
        }
Ejemplo n.º 7
0
        // 函数对象以及nargs数的参数必须由调用者保证已经入栈, 然后才能调用 _InternalCall
        // 调用完成后栈顶为函数返回值, 或异常对象
        public void _InternalPCall(IntPtr ctx, int nargs)
        {
            if (_argv != null)
            {
                var length = _argv.Length;
                nargs += length;
                for (var i = 0; i < length; i++)
                {
                    _argv[i].Push(ctx);
                }
            }
            var ret = DuktapeDLL.duk_pcall(ctx, nargs);

            if (ret != DuktapeDLL.DUK_EXEC_SUCCESS)
            {
                DuktapeAux.PrintError(ctx, -1);
                // throw new Exception(err);
            }
        }
Ejemplo n.º 8
0
        public void EvalSource(string filename, byte[] source)
        {
            if (source == null || source.Length == 0)
            {
                return;
            }
            if (filename == null)
            {
                filename = "eval";
            }
            var ctx = _ctx.rawValue;

            do
            {
                if (source[0] == 0xbf)
                {
                    // load bytecode...
                    var buffer_ptr = DuktapeDLL.duk_push_fixed_buffer(ctx, (uint)source.Length);
                    Marshal.Copy(source, 0, buffer_ptr, source.Length);
                    DuktapeDLL.duk_load_function(ctx);
                }
                else
                {
                    DuktapeDLL.duk_unity_push_lstring(ctx, source, (uint)source.Length);
                    DuktapeDLL.duk_push_string(ctx, filename);
                    if (DuktapeDLL.duk_pcompile(ctx, 0) != 0)
                    {
                        DuktapeAux.PrintError(ctx, -1, filename);
                        DuktapeDLL.duk_pop(ctx);
                        break;
                    }
                }
                // Debug.LogFormat("check top {0}", DuktapeDLL.duk_get_top(ctx));
                if (DuktapeDLL.duk_pcall(ctx, 0) != DuktapeDLL.DUK_EXEC_SUCCESS)
                {
                    DuktapeAux.PrintError(ctx, -1, filename);
                }
                DuktapeDLL.duk_pop(ctx);
                // Debug.LogFormat("check top {0}", DuktapeDLL.duk_get_top(ctx));
            } while (false);
        }
Ejemplo n.º 9
0
        public void EvalMain(string filename)
        {
            filename = EnsureExtension(filename);
            var ctx          = _ctx.rawValue;
            var top          = DuktapeDLL.duk_get_top(ctx);
            var resolvedPath = ResolvePath(filename);
            var source       = _fileManager.ReadAllText(resolvedPath);

            DuktapeDLL.duk_push_string(ctx, source);
            var err = DuktapeDLL.duk_module_node_peval_main(ctx, filename);

            // var err = DuktapeDLL.duk_peval(ctx);
            // var err = DuktapeDLL.duk_peval_string_noresult(ctx, source);
            // Debug.Log($"load main module: {filename} ({resolvedPath})");
            if (err != 0)
            {
                DuktapeAux.PrintError(ctx, -1, filename);
                // Debug.LogErrorFormat("eval main error: {0}\n{1}", DuktapeDLL.duk_safe_to_string(ctx, -1), filename);
            }
            DuktapeDLL.duk_set_top(ctx, top);
        }
Ejemplo n.º 10
0
        public void InvokeMember(string name)
        {
            var member = GetMember(name);

            if (member != null)
            {
                var ctx = member.context.rawValue;
                if (ctx != IntPtr.Zero)
                {
                    member.Push(ctx);
                    this.Push(ctx);
                    var ret = DuktapeDLL.duk_pcall_method(ctx, 0);
                    if (ret != DuktapeDLL.DUK_EXEC_SUCCESS)
                    {
                        DuktapeAux.PrintError(ctx, -1);
                        // throw new Exception(err);
                    }
                    DuktapeDLL.duk_pop(ctx);
                }
            }
        }
Ejemplo n.º 11
0
 public static byte[] DumpBytecode(IntPtr ctx, string filename, byte[] source)
 {
     if (ctx == IntPtr.Zero)
     {
         throw new InvalidOperationException();
     }
     if (source == null || source.Length == 0)
     {
         return(null);
     }
     if (source[0] == 0xbf)
     {
         return(source);
     }
     if (filename == null)
     {
         filename = "eval";
     }
     DuktapeDLL.duk_unity_push_lstring(ctx, source, (uint)source.Length);
     DuktapeDLL.duk_push_string(ctx, filename);
     if (DuktapeDLL.duk_pcompile(ctx, 0) != 0)
     {
         DuktapeAux.PrintError(ctx, -1, filename);
         DuktapeDLL.duk_pop(ctx);
         return(null);
     }
     else
     {
         DuktapeDLL.duk_dump_function(ctx);
         uint buffer_size;
         var  buffer_ptr = DuktapeDLL.duk_unity_require_buffer_data(ctx, -1, out buffer_size);
         var  bytes      = new byte[buffer_size];
         Marshal.Copy(buffer_ptr, bytes, 0, (int)buffer_size);
         DuktapeDLL.duk_pop(ctx);
         return(bytes);
     }
 }