Esempio n. 1
0
        /// <summary>
        /// Create a ArrayBuffer from a pointer to an pre-allocated memory cache, the memory will not be released when the JSArrayBuffer is disposed
        /// </summary>
        /// <param name="handle">pointer to memory cache</param>
        /// <param name="size">arraybuffer size in bytes</param>
        /// <returns></returns>
        public static JSArrayBuffer CreateFromExternal(IntPtr handle, ulong size)
        {
            var result = new JSArrayBuffer(SharedBufferSourceEnum.CreateByExternal, size);

            result.InitWindow(handle, false);
            return(result);
        }
Esempio n. 2
0
        internal static JSArrayBuffer CreateFromJS(IntPtr handle, uint size, JavaScriptValue value, IContextSwitchService context)
        {
            var result = new JSArrayBuffer(SharedBufferSourceEnum.CreateByJavascript, (ulong)size);

            result.SetJSSource(value, context);
            result.InitWindow(handle, false); //memory owned by js, do not release when object is disposed
            return(result);
        }
Esempio n. 3
0
        public static JavaScriptValue CreateArrayBuffer(this IJSValueService valueService, JSArrayBuffer source)
        {
            IContextSwitchService switchService = valueService.CurrentNode.GetService <IRuntimeService>().InternalContextSwitchService;

            return(switchService.With <JavaScriptValue>(() =>
            {
                if (source.JSSource.IsValid)
                {
                    return source.JSSource;
                }
                switch (source.BufferSource)
                {
                case SharedBufferSourceEnum.CreateByJavascript:
                    throw new InvalidOperationException("invalid source array buffer");    //create by javascript should already have JavaScriptValue assigned

                case SharedBufferSourceEnum.CreateInJavascript:
                    {
                        JavaScriptValue result = JavaScriptValue.CreateArrayBuffer((uint)source.Size);
                        source.SetJSSource(result, switchService);    //hold the varient
                        var data = JavaScriptValue.GetArrayBufferStorage(result, out uint bufferSize);
                        source.InitWindow(data, false);
                        source.InitBeforeConvert(source.Buffer);
                        return result;
                    }

                case SharedBufferSourceEnum.CreateByDotnet:
                case SharedBufferSourceEnum.CreateByExternal:
                    {
                        var result = JavaScriptValue.CreateExternalArrayBuffer(source.Buffer.Handle, (uint)source.Buffer.ByteLength, null, IntPtr.Zero); //do not handle GC callback, user should control the varient life cycle
                        source.SetJSSource(result, switchService);                                                                                       //hold the varient
                        return result;
                    }

                default:
                    throw new ArgumentOutOfRangeException("Invalid BufferSource property in JSArryBuffer object");
                }
            }
                                                        ));
        }