Ejemplo n.º 1
0
        public static bool js_get_classvalue(JSContext ctx, JSValue val, out QuickJS.IO.ByteBuffer o)
        {
            object obj;

            if (js_get_cached_object(ctx, val, out obj))
            {
                if (obj is QuickJS.IO.ByteBuffer)
                {
                    o = (QuickJS.IO.ByteBuffer)obj;
                    return(true);
                }
            }
            size_t psize;
            var    pointer = JSApi.JS_GetArrayBuffer(ctx, out psize, val);

            if (pointer != IntPtr.Zero)
            {
                var runtime   = ScriptEngine.GetRuntime(ctx);
                var allocator = runtime.GetByteBufferAllocator();
                if (allocator != null)
                {
                    var length = (int)psize;

                    o = allocator.Alloc(length);
                    runtime.AutoRelease(o);
                    o.WriteBytes(pointer, length);
                    return(true);
                }
            }

            var asBuffer = JSApi.JS_GetProperty(ctx, val, 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 runtime   = ScriptEngine.GetRuntime(ctx);
                    var allocator = runtime.GetByteBufferAllocator();
                    if (allocator != null)
                    {
                        var length = (int)psize;

                        o = allocator.Alloc(length);
                        runtime.AutoRelease(o);
                        o.WriteBytes(pointer, length);
                        return(true);
                    }
                }
            }
            else
            {
                JSApi.JS_FreeValue(ctx, asBuffer);
            }

            o = null;
            return(false);
        }
Ejemplo n.º 2
0
        public static bool js_get_classvalue(JSContext ctx, JSValue val, out QuickJS.IO.ByteBuffer o)
        {
            object obj;

            if (js_get_cached_object(ctx, val, out obj))
            {
                if (obj is QuickJS.IO.ByteBuffer)
                {
                    o = (QuickJS.IO.ByteBuffer)obj;
                    return(true);
                }
            }
            size_t psize;
            var    pointer = JSApi.JS_GetArrayBuffer(ctx, out psize, val);

            if (pointer != IntPtr.Zero)
            {
                var allocator = ScriptEngine.GetRuntime(ctx).GetByteBufferAllocator();
                if (allocator != null)
                {
                    var length = (int)psize;
                    if (length > 0)
                    {
                        o = allocator.Alloc(length);
                        allocator.AutoRelease(o);
                        o.WriteBytes(pointer, length);
                        return(true);
                    }
                }
            }
            o = null;
            return(false);
        }