Esempio n. 1
0
        public void Invoke(object arg0, object arg1, object arg2)
        {
            var ctx = _context.rawValue;

            this.Push(ctx);
            DuktapeBinding.duk_push_var(ctx, arg0);
            DuktapeBinding.duk_push_var(ctx, arg1);
            DuktapeBinding.duk_push_var(ctx, arg2);
            _InternalPCall(ctx, 3);
        }
Esempio n. 2
0
        public T ToDelegate <T>()
            where T : class
        {
            T   o;
            var ctx = _context.rawValue;

            this.Push(ctx);
            DuktapeBinding.duk_get_delegate <T>(_context.rawValue, -1, out o);
            DuktapeDLL.duk_pop(ctx);
            return(o);
        }
Esempio n. 3
0
        public object GetProperty(string name)
        {
            var ctx = _context.rawValue;

            if (ctx != IntPtr.Zero)
            {
                PushProperty(ctx, name);
                object o;
                DuktapeBinding.duk_get_var(ctx, -1, out o);
                DuktapeDLL.duk_pop(ctx);
                return(o);
            }

            return(null);
        }
Esempio n. 4
0
        public void Invoke(object arg0, object arg1, object arg2, params object[] args)
        {
            var ctx = _context.rawValue;

            this.Push(ctx);
            DuktapeBinding.duk_push_var(ctx, arg0);
            DuktapeBinding.duk_push_var(ctx, arg1);
            DuktapeBinding.duk_push_var(ctx, arg2);
            var size = args.Length;

            for (var i = 0; i < size; i++)
            {
                DuktapeBinding.duk_push_var(ctx, args[i]);
            }
            _InternalPCall(ctx, size + 3);
        }
Esempio n. 5
0
 public void SetProperty(IntPtr ctx, string name, Object value)
 {
     if (ctx != IntPtr.Zero)
     {
         if (_refPtr != IntPtr.Zero)
         {
             DuktapeDLL.duk_push_heapptr(ctx, _refPtr);
         }
         else
         {
             DuktapeDLL.duk_unity_getref(ctx, this._refid);
         }
         DuktapeBinding.duk_push_classvalue(ctx, value);
         DuktapeDLL.duk_put_prop_string(ctx, -2, name);
         DuktapeDLL.duk_pop(ctx);
     }
 }
Esempio 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));
        }
Esempio n. 7
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);
        }
Esempio n. 8
0
        public object EvalMain(string filename, byte[] source)
        {
            if (source == null || source.Length == 0)
            {
                return(null);
            }
            if (source[0] == 0xbf)
            {
                //NOTE: module is not supported in bytecode mode
                return(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);
                }
                object retValue = null;
                DuktapeBinding.duk_get_var(ctx, -1, out retValue);
                DuktapeDLL.duk_set_top(ctx, top);
                return(retValue);
            }
        }