コード例 #1
0
        /// <summary>
        /// Create DataView in javascript
        /// this method requires a sourceArrayBuffer which make sure the arraybuffer is initialized before transfer to javascript
        /// </summary>
        /// <param name="sourceArrayBuffer">ArrayBuffer object</param>
        /// <param name="source"></param>
        /// <returns></returns>
        public static JavaScriptValue CreateDataView(this IJSValueService valueService, JavaScriptValue sourceArrayBuffer, JSDataView source)
        {
            IContextSwitchService switchService = valueService.CurrentNode.GetService <IRuntimeService>().InternalContextSwitchService;

            if (source.JSSource.IsValid)
            {
                return(source.JSSource);
            }
            switch (source.BufferSource)
            {
            case SharedBufferSourceEnum.CreateByDotnet:
                return(switchService.With <JavaScriptValue>(
                           () =>
                {
                    var result = JavaScriptValue.CreateDataView(sourceArrayBuffer, source.Position, (uint)source.Size);
                    source.SetJSSource(result, switchService);
                    source.InitBeforeConvert(source.Buffer);
                    return result;
                }
                           ));

            case SharedBufferSourceEnum.CreateByJavascript:
                throw new InvalidOperationException("invalid source typed array");    //create by javascript should already have JavaScriptValue assigned

            case SharedBufferSourceEnum.CreateInJavascript:
                return(switchService.With <JavaScriptValue>(
                           () =>
                {
                    var result = JavaScriptValue.CreateDataView(sourceArrayBuffer, source.Position, (uint)source.Size);
                    source.SetJSSource(result, switchService);
                    JavaScriptValue.GetDataViewStorage(result, out IntPtr data, out uint bufferLength);
                    source.InitWindow(data, false);
                    source.InitBeforeConvert(source.Buffer);
                    return result;
                }
                           ));

            case SharedBufferSourceEnum.CreateByExternal:
                throw new ArgumentException("DataView does not support create from external");

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