示例#1
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);
            }
        }
示例#2
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");
            }
        }
示例#3
0
 public static int duk_assert(IntPtr ctx)
 {
     if (!DuktapeDLL.duk_get_boolean(ctx, 0))
     {
         var msg = string.Empty;
         if (DuktapeDLL.duk_get_top(ctx) > 1)
         {
             msg = duk_print_string(ctx, 1, false);
         }
         return(DuktapeDLL.duk_generic_error(ctx, "assertion failed: " + msg));
     }
     return(0);
 }
示例#4
0
 // 记录栈状态
 public void BeginInvoke(IntPtr ctx)
 {
     // Debug.Log($"BeginInvoke: {_savedState}");
     if (_jsInvoker == null)
     {
         this.Push(ctx); // push this
         if (!DuktapeDLL.duk_is_function(ctx, -1))
         {
             // Debug.Log("DuktapeDelegate based on Dispatcher");
             DuktapeDLL.duk_get_prop_string(ctx, -1, "dispatch");
             DuktapeDLL.duk_remove(ctx, -2); // remove this
         }
         _jsInvoker = new DuktapeFunction(ctx, DuktapeDLL.duk_unity_ref(ctx));
     }
     _jsInvoker.Push(ctx); // push function
     this.Push(ctx);       // push this
     _savedState = DuktapeDLL.duk_get_top(ctx);
 }
示例#5
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);
        }
示例#6
0
        private static string duk_print_string(IntPtr ctx, int startIndex, bool withStacktrace)
        {
            var narg = DuktapeDLL.duk_get_top(ctx);
            var str  = string.Empty;

            for (int i = startIndex; i < narg; i++)
            {
                object o;
                if (DuktapeBinding.duk_get_object(ctx, i, out o))
                {
                    str += (o == null ? "(null)" : o.ToString()) + " ";
                }
                else
                {
                    str += DuktapeDLL.duk_safe_to_string(ctx, i) + " ";
                }
            }
            if (withStacktrace)
            {
                str += $"\n{duk_get_stacktrace(ctx)}";
            }
            return(str);
        }