Example #1
0
        private void Run()
        {
            var       tick         = Environment.TickCount;
            var       list         = new List <IO.ByteBuffer>();
            var       context      = _runtime.GetMainContext();
            var       globalObject = context.GetGlobalObject();
            var       onmessage    = JSApi.JS_GetPropertyStr(context, globalObject, "onmessage");
            JSContext ctx          = context;
            var       logger       = _runtime.GetLogger();

            while (_runtime.isRunning)
            {
                lock (_inbox)
                {
                    list.AddRange(_inbox);
                    _inbox.Clear();
                }

                if (list.Count == 0)
                {
                    Thread.Yield();
                }
                else
                {
                    for (int i = 0, count = list.Count; i < count; i++)
                    {
                        var byteBuffer = list[i];

                        unsafe
                        {
                            JSValue data;
                            fixed(byte *buf = byteBuffer.data)
                            {
                                data = JSApi.JS_ReadObject(ctx, buf, byteBuffer.readableBytes, 0);
                            }

                            if (data.IsException())
                            {
                                var exceptionString = ctx.GetExceptionString();
                                if (logger != null)
                                {
                                    logger.Write(LogLevel.Error, exceptionString);
                                }
                            }
                            else
                            {
                                var argv = stackalloc JSValue[1] {
                                    data
                                };
                                var rval = JSApi.JS_Call(ctx, onmessage, globalObject, 1, argv);
                                JSApi.JS_FreeValue(ctx, rval);
                            }
                        }
                        byteBuffer.Release();
                    }
                    list.Clear();
                }

                var now = Environment.TickCount;
                var dt  = now - tick;
                tick = now;

                _runtime.Update(dt);
            }

            JSApi.JS_FreeValue(ctx, onmessage);
            JSApi.JS_FreeValue(ctx, globalObject);
            _runtime.Destroy();
        }
Example #2
0
        private void Run()
        {
            var tick    = Environment.TickCount;
            var list    = new List <IO.ByteBuffer>();
            var context = _runtime.GetMainContext();
            var logger  = _runtime.GetLogger();

            while (_runtime.isRunning)
            {
                lock (_inbox)
                {
                    list.AddRange(_inbox);
                    _inbox.Clear();
                }

                if (list.Count == 0)
                {
                    Thread.Yield();
                }
                else
                {
                    JSContext ctx          = context;
                    var       globalObject = context.GetGlobalObject();
                    var       onmessage    = JSApi.JS_GetPropertyStr(context, globalObject, "onmessage");
                    var       callable     = JSApi.JS_IsFunction(ctx, onmessage) == 1;

                    for (int i = 0, count = list.Count; i < count; i++)
                    {
                        var byteBuffer = list[i];

                        if (callable)
                        {
                            unsafe
                            {
                                JSValue data;
                                fixed(byte *buf = byteBuffer.data)
                                {
                                    data = JSApi.JS_ReadObject(ctx, buf, byteBuffer.readableBytes, JSApi.JS_READ_OBJ_REFERENCE);
                                }

                                do
                                {
                                    if (!data.IsException())
                                    {
                                        var evt = JSApi.JS_NewObject(ctx);
                                        if (!evt.IsException())
                                        {
                                            JSApi.JS_SetProperty(ctx, evt, context.GetAtom("data"), data);
                                            var argv = stackalloc JSValue[1] {
                                                evt
                                            };
                                            var rval = JSApi.JS_Call(ctx, onmessage, globalObject, 1, argv);
                                            JSApi.JS_FreeValue(ctx, rval);
                                            JSApi.JS_FreeValue(ctx, evt);
                                            break;
                                        }
                                        else
                                        {
                                            JSApi.JS_FreeValue(ctx, data);
                                        }
                                    }

                                    var exceptionString = ctx.GetExceptionString();
                                    if (logger != null)
                                    {
                                        logger.Write(LogLevel.Error, exceptionString);
                                    }
                                } while (false);
                            }
                        }
                        byteBuffer.Release();
                    }
                    JSApi.JS_FreeValue(ctx, onmessage);
                    JSApi.JS_FreeValue(ctx, globalObject);
                    list.Clear();
                }

                var now = Environment.TickCount;
                if (now < tick)
                {
                    _runtime.Update((now - int.MinValue) + (int.MaxValue - tick));
                }
                else
                {
                    _runtime.Update(now - tick);
                }
                tick = now;
            }

            _runtime.Destroy();
        }