Beispiel #1
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");
                }
            }
                                                        ));
        }