Beispiel #1
0
        public void SendPostRequest(string url, string body, Action <bool, string> oncomplete)
        {
            var form = new WWWForm();
            var ps   = body.Split('&');

            foreach (var p in ps)
            {
                var pp = p.Split('=');
                form.AddField(pp[0], pp[1] != null ? pp[1] : "");
            }
            _req = UnityWebRequest.Post(url, form);
            if (!_requesting)
            {
                var runner = DuktapeRunner.GetRunner();
                if (runner != null)
                {
                    _requesting = true;
                    this.ApplyHeaders();
                    runner.StartCoroutine(Run(oncomplete));
                }
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
Beispiel #2
0
        public void Destroy()
        {
            try
            {
                _instance = null;
                if (_ctx != null)
                {
                    var ctx = _ctx.rawValue;
                    _ctx.Destroy();
                    _ctx            = null;
                    _lastContextPtr = IntPtr.Zero;
                    _lastContext    = null;
                    _contexts.Clear();
                    _objectCache.Clear();
                    DuktapeDLL.duk_unity_destroy_heap(ctx);
                    // Debug.LogWarning("duk_destroy_heap");
                }

                if (_updateTimer != 0)
                {
                    DuktapeRunner.Clear(_updateTimer);
                    _updateTimer = 0;
                }
            }
            finally
            {
                if (_memAllocPool != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(_memAllocPool);
                    _memAllocPool = IntPtr.Zero;
                }
            }
        }
Beispiel #3
0
 private void Start(int port)
 {
     Stop();
     _server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     _server.Bind(new IPEndPoint(IPAddress.Any, port));
     _server.Listen(1);
     _server.BeginAccept(_Accept, _server);
     _loop = DuktapeRunner.SetLoop(OnUpdate);
 }
Beispiel #4
0
 public static int ClearTimer(IntPtr ctx)
 {
     if (DuktapeDLL.duk_is_number(ctx, 0))
     {
         var id = DuktapeDLL.duk_get_uint(ctx, 0);
         DuktapeDLL.duk_push_boolean(ctx, DuktapeRunner.Clear(id));
         return(1);
     }
     return(0);
 }
Beispiel #5
0
        public static int SetTimeout(IntPtr ctx)
        {
            DuktapeFunction fn;
            var             idx = _GetTimerFunction(ctx, out fn);

            if (idx < 0)
            {
                var id = DuktapeRunner.SetTimeout(fn, DuktapeDLL.duk_get_int(ctx, 1));
                DuktapeDLL.duk_push_uint(ctx, id);
                return(1);
            }
            return(DuktapeDLL.duk_generic_error(ctx, "invalid arg " + idx));
        }
Beispiel #6
0
        public static DuktapeRunner GetRunner()
        {
            if (_runner == null)
            {
                var go = new GameObject {
                    hideFlags = HideFlags.HideAndDontSave
                };
                GameObject.DontDestroyOnLoad(go);
                _runner = go.AddComponent <DuktapeRunner>();
            }

            return(_runner);
        }
        void OnGUI()
        {
            var vm = DuktapeVM.GetInstance();

            if (vm == null)
            {
                EditorGUILayout.HelpBox("No Running VM", MessageType.Info);
                return;
            }

            uint objectCount;
            uint allocBytes;
            uint poolBytes;

            vm.GetMemoryState(out objectCount, out allocBytes, out poolBytes);
            EditorGUILayout.IntField("Objects", (int)objectCount);
            if (allocBytes > 1024 * 1024 * 2)
            {
                EditorGUILayout.FloatField("Allocated Memory (MB)", (float)allocBytes / 1024f / 1024f);
            }
            else if (allocBytes > 1024 * 2)
            {
                EditorGUILayout.FloatField("Allocated Memory (KB)", (float)allocBytes / 1024f);
            }
            else
            {
                EditorGUILayout.IntField("Allocated Memory", (int)allocBytes);
            }

            if (poolBytes != 0)
            {
                EditorGUILayout.IntField("Pool Size", (int)poolBytes);
                EditorGUILayout.FloatField("Used (%)", (float)allocBytes * 100f / poolBytes);
            }

            EditorGUILayout.IntField("Exported Types", vm.GetExportedTypeCount());

            var objectCache = vm.GetObjectCache();

            EditorGUILayout.IntField("ManagedObject Count", objectCache.GetManagedObjectCount());
            EditorGUILayout.IntField("JSObject Count", objectCache.GetJSObjectCount());
            EditorGUILayout.IntField("Delegate Count", objectCache.GetDelegateCount());

            var scheduler = DuktapeRunner.GetScheduler();

            if (scheduler != null)
            {
                EditorGUILayout.IntField("Active Timer", scheduler.GetActiveTimeHandleCount());
            }
        }
 public void SendPostRequest(string url, string body, Action <bool, string> oncomplete)
 {
     if (!_requesting)
     {
         var runner = DuktapeRunner.GetRunner();
         if (runner != null)
         {
             _req.method = "GET";
             _req.url    = url;
             _requesting = true;
             runner.StartCoroutine(Run(oncomplete));
         }
     }
     else
     {
         throw new InvalidOperationException();
     }
 }
Beispiel #9
0
        public void Initialize(IO.ByteBufferAllocator byteBufferAllocator, IFileResolver fileResolver, IDuktapeListener listener, int step = 30)
        {
            _byteBufferAllocator = byteBufferAllocator;
            _fileResolver        = fileResolver;
            var runner = DuktapeRunner.GetRunner();

            if (runner != null)
            {
                runner.StartCoroutine(_InitializeStep(listener, step));
            }
            else
            {
                var e = _InitializeStep(listener, step);
                while (e.MoveNext())
                {
                    ;
                }
            }
        }
Beispiel #10
0
 public void SendGetRequest(string url, Action <bool, string> oncomplete)
 {
     _req = new UnityWebRequest();
     if (!_requesting)
     {
         var runner = DuktapeRunner.GetRunner();
         if (runner != null)
         {
             _req.method = "GET";
             _req.url    = url;
             _requesting = true;
             this.ApplyHeaders();
             runner.StartCoroutine(Run(oncomplete));
         }
     }
     else
     {
         throw new InvalidOperationException();
     }
 }
Beispiel #11
0
 private void Stop()
 {
     lock (_pending)
     {
         _pending.Clear();
     }
     DetachCurrent();
     if (_client != null)
     {
         _client.Close();
         _client = null;
     }
     if (_server != null)
     {
         _server.Close();
         _server = null;
     }
     DuktapeRunner.Clear(_loop);
     _loop = 0;
 }
Beispiel #12
0
        public void Destroy()
        {
            _instance = null;
            if (_ctx != null)
            {
                var ctx = _ctx.rawValue;
                _ctx.onDestroy();
                _ctx            = null;
                _lastContextPtr = IntPtr.Zero;
                _lastContext    = null;
                _contexts.Clear();
                _objectCache.Clear();
                DuktapeDLL.duk_destroy_heap_default(ctx);
                // Debug.LogWarning("duk_destroy_heap");
            }

            if (_updateTimer != 0)
            {
                DuktapeRunner.Clear(_updateTimer);
                _updateTimer = 0;
            }
        }
Beispiel #13
0
        private IEnumerator _InitializeStep(IDuktapeListener listener, int step)
        {
            DuktapeDLL.duk_push_global_object(ctx);
            DuktapeJSBuiltins.reg(ctx);
            listener?.OnTypesBinding(this);
            var ctxAsArgs    = new object[] { ctx };
            var bindingTypes = new List <Type>();
            var assemblies   = AppDomain.CurrentDomain.GetAssemblies();

            for (int assemblyIndex = 0, assemblyCount = assemblies.Length; assemblyIndex < assemblyCount; assemblyIndex++)
            {
                var assembly = assemblies[assemblyIndex];
                try
                {
                    if (assembly.IsDynamic)
                    {
                        continue;
                    }
                    var exportedTypes = assembly.GetExportedTypes();
                    for (int i = 0, size = exportedTypes.Length; i < size; i++)
                    {
                        var type = exportedTypes[i];
#if UNITY_EDITOR
                        if (type.IsDefined(typeof(JSAutoRunAttribute), false))
                        {
                            try
                            {
                                var run = type.GetMethod("Run", BindingFlags.Static | BindingFlags.Public);
                                if (run != null)
                                {
                                    run.Invoke(null, null);
                                }
                            }
                            catch (Exception exception)
                            {
                                Debug.LogWarning($"JSAutoRun failed: {exception}");
                            }
                            continue;
                        }
#endif
                        var attributes = type.GetCustomAttributes(typeof(JSBindingAttribute), false);
                        if (attributes.Length == 1)
                        {
                            var jsBinding = attributes[0] as JSBindingAttribute;
                            if (jsBinding.Version == 0 || jsBinding.Version == VERSION)
                            {
                                bindingTypes.Add(type);
                            }
                            else
                            {
                                if (listener != null)
                                {
                                    listener.OnBindingError(this, type);
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Debug.LogErrorFormat("assembly: {0}, {1}", assembly, e);
                }
            }
            var numRegInvoked = bindingTypes.Count;
            for (var i = 0; i < numRegInvoked; ++i)
            {
                var type = bindingTypes[i];
                var reg  = type.GetMethod("reg");
                if (reg != null)
                {
                    reg.Invoke(null, ctxAsArgs);
                    if (listener != null)
                    {
                        listener.OnProgress(this, i, numRegInvoked);
                    }

                    if (i % step == 0)
                    {
                        yield return(null);
                    }
                }
            }
            if (listener != null)
            {
                listener.OnBinded(this, numRegInvoked);
            }
            // Debug.LogFormat("exported {0} classes", _exported.Count);

            // 设置导出类的继承链
            foreach (var kv in _exported)
            {
                var type     = kv.Key;
                var baseType = type.BaseType;
                if (baseType == null)
                {
                    // Debug.Log($"baseType is null, for {type}");
                    continue;
                }
                var fn = kv.Value;
                fn.PushPrototype(ctx);
                if (PushChainedPrototypeOf(ctx, baseType))
                {
                    // Debug.LogFormat($"set {type} super {baseType}");
                    DuktapeDLL.duk_set_prototype(ctx, -2);
                }
                else
                {
                    Debug.LogWarning($"fail to push prototype, for {type}: {baseType}");
                }
                DuktapeDLL.duk_pop(ctx);
            }

            DuktapeJSBuiltins.postreg(ctx);
            DuktapeDLL.duk_pop(ctx); // pop global

            _updateTimer = DuktapeRunner.SetInterval(this.OnUpdate, 100);

            if (listener != null)
            {
                listener.OnLoaded(this);
            }
        }
Beispiel #14
0
 public void Initialize(IFileSystem fs, IDuktapeListener listener, int step = 30)
 {
     _fileManager = fs;
     DuktapeRunner.GetRunner().StartCoroutine(_InitializeStep(listener, step));
 }