コード例 #1
0
        private static int _GetTimerFunction(IntPtr ctx, out DuktapeFunction fn)
        {
            if (!DuktapeDLL.duk_is_function(ctx, 0))
            {
                fn = null;
                return(0);
            }
            if (!DuktapeDLL.duk_is_number(ctx, 1))
            {
                fn = null;
                return(1);
            }
            var top_index = DuktapeDLL.duk_get_top_index(ctx);

            // Debug.Log($"_GetTimerFunction {top} ?? {DuktapeDLL.duk_get_top(ctx)}");
            DuktapeValue[] argv = null;
            if (top_index > 1)
            {
                argv = new DuktapeValue[top_index - 1];
                for (var i = 2; i <= top_index; i++)
                {
                    DuktapeDLL.duk_dup(ctx, i);
                    var argPtr = DuktapeDLL.duk_get_heapptr(ctx, -1);
                    argv[i - 2] = new DuktapeValue(ctx, DuktapeDLL.duk_unity_ref(ctx), argPtr);
                }
            }
            DuktapeDLL.duk_dup(ctx, 0);
            var fnPtr = DuktapeDLL.duk_get_heapptr(ctx, -1);

            fn = new DuktapeFunction(ctx, DuktapeDLL.duk_unity_ref(ctx), fnPtr, argv);
            return(-1);
        }
コード例 #2
0
        public static bool duk_get_delegate <T>(IntPtr ctx, int idx, out T o)
            where T : class
        {
            //TODO: 20200320 !!! 如果 o 不是 jsobject, 且是 Delegate 但不是 DuktapeDelegate, 则 ... 处理

            if (DuktapeDLL.duk_is_object(ctx, idx) || DuktapeDLL.duk_is_function(ctx, idx))
            {
                var             heapptr = DuktapeDLL.duk_get_heapptr(ctx, idx);
                var             cache   = DuktapeVM.GetObjectCache(ctx);
                DuktapeDelegate fn;
                if (cache.TryGetDelegate(heapptr, out fn))
                {
                    // Debug.LogWarningFormat("cache hit {0}", heapptr);
                    o = fn.target as T;
                    return(true);
                }
                // 默认赋值操作
                DuktapeDLL.duk_dup(ctx, idx);
                fn = new DuktapeDelegate(ctx, DuktapeDLL.duk_unity_ref(ctx), heapptr);
                var vm = DuktapeVM.GetVM(ctx);
                o = vm.CreateDelegate(typeof(T), fn) as T;

                // DuktapeDelegate 拥有 js 对象的强引用, 此 js 对象无法释放 cache 中的 object, 所以这里用弱引用注册
                // 会出现的问题是, 如果 c# 没有对 DuktapeDelegate 的强引用, 那么反复 get_delegate 会重复创建 DuktapeDelegate
                // Debug.LogWarningFormat("cache create : {0}", heapptr);
                cache.AddDelegate(heapptr, fn);
                return(true);
            }
            // else if (DuktapeDLL.duk_is_object(ctx, idx))
            // {
            //     return duk_get_classvalue<T>(ctx, idx, out o);
            // }
            o = null;
            return(false);
        }
コード例 #3
0
        public static bool duk_get_delegate <T>(IntPtr ctx, int idx, out T o)
            where T : class
        {
            if (DuktapeDLL.duk_is_object(ctx, idx) ||/* && check if js delegate type (hidden property) */
                DuktapeDLL.duk_is_function(ctx, idx))
            {
                var             heapptr = DuktapeDLL.duk_get_heapptr(ctx, idx);
                var             cache   = DuktapeVM.GetObjectCache(ctx);
                DuktapeDelegate fn;
                if (cache.TryGetDelegate(heapptr, out fn))
                {
                    // Debug.LogWarningFormat("cache hit {0}", heapptr);
                    o = fn.target as T;
                    return(true);
                }
                // 默认赋值操作
                DuktapeDLL.duk_dup(ctx, idx);
                fn = new DuktapeDelegate(ctx, DuktapeDLL.duk_unity_ref(ctx));
                var vm = DuktapeVM.GetVM(ctx);
                o = vm.CreateDelegate(typeof(T), fn) as T;

                // DuktapeDelegate 拥有 js 对象的强引用, 此 js 对象无法释放 cache 中的 object, 所以这里用弱引用注册
                // 会出现的问题是, 如果 c# 没有对 DuktapeDelegate 的强引用, 那么反复 get_delegate 会重复创建 DuktapeDelegate
                // Debug.LogWarningFormat("cache create : {0}", heapptr);
                cache.AddDelegate(heapptr, fn);
                return(true);
            }
            o = null;
            return(false);
        }
コード例 #4
0
        public DuktapeFunction GetMember(string name)
        {
            DuktapeFunction method = null;

            if (!_methodCache.TryGetValue(name, out method))
            {
                var ctx = _context.rawValue;
                if (ctx != IntPtr.Zero)
                {
                    this.PushProperty(ctx, name);
                    if (DuktapeDLL.duk_is_function(ctx, -1))
                    {
                        var ptr   = DuktapeDLL.duk_get_heapptr(ctx, -1);
                        var refid = DuktapeDLL.duk_unity_ref(ctx);
                        method = new DuktapeFunction(ctx, refid, ptr);
                    }
                    else
                    {
                        DuktapeDLL.duk_pop(ctx);
                    }
                }
                _methodCache[name] = method;
            }
            return(method);
        }
コード例 #5
0
        public static bool duk_get_delegate <T>(IntPtr ctx, int idx, out T o)
            where T : class
        {
            if (DuktapeDLL.duk_is_object(ctx, idx) ||/* && check if js delegate type (hidden property) */
                DuktapeDLL.duk_is_function(ctx, idx))
            {
                DuktapeDLL.duk_get_prop_string(ctx, idx, DuktapeVM.OBJ_PROP_NATIVE_WEAK);
                var refid = DuktapeDLL.duk_get_int(ctx, -1);
                DuktapeDLL.duk_pop(ctx);

                var             cache = DuktapeVM.GetObjectCache(ctx);
                DuktapeDelegate fn;
                if (cache.TryGetTypedWeakObject(refid, out fn) && fn != null)
                {
                    o = fn.target as T;
                    return(true);
                }
                // 默认赋值操作
                DuktapeDLL.duk_dup(ctx, idx);
                var heapptr = DuktapeDLL.duk_get_heapptr(ctx, idx);
                fn = new DuktapeDelegate(ctx, DuktapeDLL.duk_unity_ref(ctx));
                var vm = DuktapeVM.GetVM(ctx);
                o = vm.CreateDelegate(typeof(T), fn) as T;

                // DuktapeDelegate 拥有 js 对象的强引用, 此 js 对象无法释放 cache 中的 object, 所以这里用弱引用注册
                // 会出现的问题是, 如果 c# 没有对 DuktapeDelegate 的强引用, 那么反复 get_delegate 会重复创建 DuktapeDelegate
                refid = cache.AddWeakObject(fn);
                DuktapeDLL.duk_unity_set_prop_i(ctx, idx, DuktapeVM.OBJ_PROP_NATIVE_WEAK, refid);
                cache.AddJSValue(o, heapptr);
                return(true);
            }
            o = null;
            return(false);
        }
コード例 #6
0
        public static void duk_bind_native(IntPtr ctx, int idx, object o)
        {
            var type  = o.GetType();
            var vm    = DuktapeVM.GetVM(ctx);
            var cache = vm.GetObjectCache();
            var id    = cache.AddObject(o);

            if (id >= 0)
            {
                DuktapeDLL.duk_unity_set_refid(ctx, idx, id);
            }

            if (DuktapeVM.GetVM(ctx).PushChainedPrototypeOf(ctx, type))
            {
                DuktapeDLL.duk_set_prototype(ctx, -2);
            }
            else
            {
                Debug.LogWarning($"no prototype found for {type}");
            }
            if (!type.IsValueType)
            {
                var heapptr = DuktapeDLL.duk_get_heapptr(ctx, idx);
                cache.AddJSValue(o, heapptr);
            }
        }
コード例 #7
0
        protected static void duk_begin_special(IntPtr ctx, string name)
        {
            DuktapeDLL.duk_push_c_function(ctx, object_private_ctor, 0); // ctor
            DuktapeDLL.duk_dup(ctx, -1);
            DuktapeDLL.duk_dup(ctx, -1);
            var ptr       = DuktapeDLL.duk_get_heapptr(ctx, -1);
            var typeValue = new DuktapeFunction(ctx, DuktapeDLL.duk_unity_ref(ctx), ptr); // ctor, ctor

            DuktapeVM.GetVM(ctx).AddSpecial(name, typeValue);
            DuktapeDLL.duk_put_prop_string(ctx, -3, name);        // ctor
            DuktapeDLL.duk_push_object(ctx);                      // ctor, prototype
            DuktapeDLL.duk_dup_top(ctx);                          // ctor, prototype, prototype
            DuktapeDLL.duk_put_prop_string(ctx, -3, "prototype"); // ctor, prototype
        }
コード例 #8
0
ファイル: DuktapeThread.cs プロジェクト: shunfy/duktape-unity
        public DuktapeThread(DuktapeFunction fn)
        {
            var ctx = fn.ctx;
            var vm  = DuktapeVM.GetContext(ctx).vm;
            var idx = DuktapeDLL.duk_push_thread(ctx);

            DuktapeDLL.duk_dup(ctx, -1);
            var ptr = DuktapeDLL.duk_get_heapptr(ctx, -1);

            _thread        = new DuktapeValue(ctx, DuktapeDLL.duk_unity_ref(ctx), ptr);
            _threadContext = new DuktapeContext(vm, DuktapeDLL.duk_get_context(ctx, idx));
            if (fn.Push(_threadContext.rawValue))
            {
            }

            DuktapeDLL.duk_pop(ctx);
        }
コード例 #9
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
         }
         var ptr = DuktapeDLL.duk_get_heapptr(ctx, -1);
         _jsInvoker = new DuktapeFunction(ctx, DuktapeDLL.duk_unity_ref(ctx), ptr);
     }
     _jsInvoker.Push(ctx); // push function
     this.Push(ctx);       // push this
     _savedState = DuktapeDLL.duk_get_top(ctx);
 }
コード例 #10
0
        protected static void duk_begin_class(IntPtr ctx, string typename, Type type, DuktapeDLL.duk_c_function ctor)
        {
            // Debug.LogFormat("begin class {0}", DuktapeDLL.duk_get_top(ctx));
            DuktapeDLL.duk_push_c_function(ctx, ctor, DuktapeDLL.DUK_VARARGS); // [ctor]
            DuktapeDLL.duk_dup(ctx, -1);                                       // [ctor ctor]
            // Debug.LogFormat("begin check {0}", DuktapeDLL.duk_get_top(ctx));
            DuktapeDLL.duk_dup(ctx, -1);                                       // [ctor ctor ctor]
            var ptr    = DuktapeDLL.duk_get_heapptr(ctx, -1);
            var typeid = DuktapeVM.GetVM(ctx).AddExportedType(type, new DuktapeFunction(ctx, DuktapeDLL.duk_unity_ref(ctx), ptr));

            DuktapeDLL.duk_unity_set_type_refid(ctx, -1, typeid); // constructor_function.!type == typeid
            // Debug.LogFormat("end check {0}", DuktapeDLL.duk_get_top(ctx));
            DuktapeDLL.duk_put_prop_string(ctx, -3, typename);
            DuktapeDLL.duk_push_object(ctx);                      // [ctor, prototype]
            DuktapeDLL.duk_dup_top(ctx);                          // [ctor, prototype, prototype]
            DuktapeDLL.duk_unity_set_type_refid(ctx, -1, typeid); // prototype.!type == typeid
            DuktapeDLL.duk_push_c_function(ctx, object_dtor, 1);
            DuktapeDLL.duk_set_finalizer(ctx, -3);                // set prototype finalizer
            DuktapeDLL.duk_put_prop_string(ctx, -3, "prototype"); // [ctor, prototype]
        }
コード例 #11
0
        public static void duk_bind_native(IntPtr ctx, int idx, object o)
        {
            var cache = DuktapeVM.GetObjectCache(ctx);
            var id    = cache.AddObject(o);

            DuktapeDLL.duk_unity_set_prop_i(ctx, idx, DuktapeVM.OBJ_PROP_NATIVE, id);
            if (DuktapeVM.GetVM(ctx).PushChainedPrototypeOf(ctx, o.GetType()))
            {
                DuktapeDLL.duk_set_prototype(ctx, -2);
            }
            else
            {
                Debug.LogWarning($"no prototype found for {o.GetType()}");
            }
            if (!o.GetType().IsValueType)
            {
                var heapptr = DuktapeDLL.duk_get_heapptr(ctx, idx);
                cache.AddJSValue(o, heapptr);
            }
        }
コード例 #12
0
        public static bool duk_get_classvalue(IntPtr ctx, int idx, out DuktapeArray o)
        {
            object obj;

            if (duk_get_cached_object(ctx, idx, out obj))
            {
                if (obj is DuktapeArray)
                {
                    o = (DuktapeArray)obj;
                    return(true);
                }
            }
            if (DuktapeDLL.duk_is_array(ctx, idx))
            {
                DuktapeDLL.duk_dup(ctx, idx);
                var ptr   = DuktapeDLL.duk_get_heapptr(ctx, -1);
                var refid = DuktapeDLL.duk_unity_ref(ctx);
                o = new DuktapeArray(ctx, refid, ptr);
                return(true);
            }
            o = null;
            return(false);
        }