Esempio n. 1
0
        public void OnBeforeSerialize()
        {
            if (_onBeforeSerializeValid)
            {
                if (_properties == null)
                {
                    _properties = new JSScriptProperties();
                }
                else
                {
                    _properties.Clear();
                }

                var buffer = ScriptEngine.AllocByteBuffer(_ctx, 512);

                unsafe
                {
                    var argv = stackalloc[] { Binding.Values.js_push_classvalue(_ctx, _properties), Binding.Values.js_push_classvalue(_ctx, buffer) };
                    var rval = JSApi.JS_Call(_ctx, _onBeforeSerializeFunc, _this_obj, 2, argv);
                    JSApi.JS_FreeValue(_ctx, argv[0]);
                    JSApi.JS_FreeValue(_ctx, argv[1]);
                    if (rval.IsException())
                    {
                        _ctx.print_exception();
                    }
                    else
                    {
                        JSApi.JS_FreeValue(_ctx, rval);
                    }
                }
                _properties.SetGenericValue(buffer);
            }
        }
Esempio n. 2
0
        private static JSValue _js_constructor(JSContext ctx, JSValue new_target, int argc, JSValue[] argv, int magic)
        {
            try
            {
                if (argc < 1 || !argv[0].IsString())
                {
                    throw new ParameterException("url", typeof(string), 0);
                }
                var protocols = new List <string>();
                if (argc > 1)
                {
                    if (argv[1].IsString())
                    {
                        protocols.Add(JSApi.GetString(ctx, argv[1]));
                    }
                    else if (JSApi.JS_IsArray(ctx, argv[1]) == 1)
                    {
                        var length_prop = JSApi.JS_GetProperty(ctx, argv[1], JSApi.JS_ATOM_length);
                        int length;
                        if (JSApi.JS_ToInt32(ctx, out length, length_prop) >= 0)
                        {
                            for (uint i = 0; i < length; i++)
                            {
                                var element          = JSApi.JS_GetPropertyUint32(ctx, argv[1], i);
                                var protocol_element = JSApi.GetString(ctx, element);
                                if (protocol_element != null)
                                {
                                    protocols.Add(protocol_element);
                                }
                                JSApi.JS_FreeValue(ctx, element);
                            }
                        }
                        JSApi.JS_FreeValue(ctx, length_prop);
                    }
                    else
                    {
                        throw new ParameterException("protocol", typeof(string), 1);
                    }
                }
                var url    = JSApi.GetString(ctx, argv[0]);
                var buffer = ScriptEngine.AllocByteBuffer(ctx);

                var o   = new WebSocket(buffer, url, protocols);
                var val = NewBridgeClassObject(ctx, new_target, o, magic);
                o._Transfer(ctx, val);
                return(val);
            }
            catch (Exception exception)
            {
                return(JSApi.ThrowException(ctx, exception));
            }
        }
Esempio n. 3
0
        private static JSValue _js_send(JSContext ctx, JSValue this_obj, int argc, JSValue[] argv)
        {
            try
            {
                WebSocket self;
                if (!js_get_classvalue(ctx, this_obj, out self))
                {
                    throw new ThisBoundException();
                }
                if (argc == 0)
                {
                    throw new ParameterException("data", typeof(string), 0);
                }
                if (!self._wsi.IsValid() || !self._context.IsValid())
                {
                    return(JSApi.JS_ThrowInternalError(ctx, "websocket closed"));
                }

                if (argv[0].IsString())
                {
                    // send text data
                    size_t psize;
                    var    pointer = JSApi.JS_ToCStringLen(ctx, out psize, argv[0]);
                    if (pointer != IntPtr.Zero && psize > 0)
                    {
                        var buffer = ScriptEngine.AllocByteBuffer(ctx, psize + WSApi.LWS_PRE);
                        if (buffer != null)
                        {
                            buffer.WriteBytes(WSApi.LWS_PRE);
                            buffer.WriteBytes(pointer, psize);
                            self._pending.Enqueue(new Packet(false, buffer));
                            self._bufferedAmount += psize;
                            WSApi.lws_callback_on_writable(self._wsi);
                        }
                        else
                        {
                            JSApi.JS_FreeCString(ctx, pointer);
                            return(JSApi.JS_ThrowInternalError(ctx, "buf alloc failed"));
                        }
                    }
                    JSApi.JS_FreeCString(ctx, pointer);
                }
                else
                {
                    size_t psize;
                    var    pointer = JSApi.JS_GetArrayBuffer(ctx, out psize, argv[0]);
                    if (pointer != IntPtr.Zero)
                    {
                        var buffer = ScriptEngine.AllocByteBuffer(ctx, psize + WSApi.LWS_PRE);
                        if (buffer != null)
                        {
                            buffer.WriteBytes(WSApi.LWS_PRE);
                            buffer.WriteBytes(pointer, psize);
                            self._pending.Enqueue(new Packet(false, buffer));
                            self._bufferedAmount += psize;
                            WSApi.lws_callback_on_writable(self._wsi);
                        }
                        else
                        {
                            return(JSApi.JS_ThrowInternalError(ctx, "buf alloc failed"));
                        }
                    }
                    else
                    {
                        var asBuffer = JSApi.JS_GetProperty(ctx, argv[0], ScriptEngine.GetContext(ctx).GetAtom("buffer"));
                        if (asBuffer.IsObject())
                        {
                            pointer = JSApi.JS_GetArrayBuffer(ctx, out psize, asBuffer);
                            JSApi.JS_FreeValue(ctx, asBuffer);
                            if (pointer != IntPtr.Zero)
                            {
                                var buffer = ScriptEngine.AllocByteBuffer(ctx, psize + WSApi.LWS_PRE);
                                if (buffer != null)
                                {
                                    buffer.WriteBytes(WSApi.LWS_PRE);
                                    buffer.WriteBytes(pointer, psize);
                                    self._pending.Enqueue(new Packet(false, buffer));
                                    self._bufferedAmount += psize;
                                    WSApi.lws_callback_on_writable(self._wsi);
                                    return(JSApi.JS_UNDEFINED);
                                }
                                else
                                {
                                    return(JSApi.JS_ThrowInternalError(ctx, "buf alloc failed"));
                                }
                            }
                        }
                        else
                        {
                            JSApi.JS_FreeValue(ctx, asBuffer);
                        }
                        return(JSApi.JS_ThrowInternalError(ctx, "unknown buf type"));
                    }
                }

                return(JSApi.JS_UNDEFINED);
            }
            catch (Exception exception)
            {
                return(JSApi.ThrowException(ctx, exception));
            }
        }