예제 #1
0
        public bool Resume(out object value)
        {
            var ctx = _thread.ctx;

            DuktapeDLL.duk_push_c_function(ctx, DuktapeDLL.duk_unity_thread_resume, DuktapeDLL.DUK_VARARGS);
            if (_thread.Push(ctx))
            {
                DuktapeDLL.duk_push_null(ctx); // 预留
                DuktapeDLL.duk_call(ctx, 2);
                DuktapeBinding.duk_get_var(ctx, -1, out value);
                DuktapeDLL.duk_pop(ctx);
                if (_thread.Push(ctx))
                {
                    var state = DuktapeDLL.duk_unity_thread_state(ctx);
                    DuktapeDLL.duk_pop(ctx);
                    return(state != 5);
                }
            }
            else
            {
                DuktapeDLL.duk_pop(ctx);
            }

            value = null;
            return(false);
        }
예제 #2
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);
        }
예제 #3
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));
        }
예제 #4
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);
            }
        }