Exemplo n.º 1
0
        public static JavaScriptValue CreateTypedArray(this IJSValueService valueService, JSTypedArray source, JavaScriptValue?arrayBufferSource)
        {
            IContextSwitchService switchService = valueService.CurrentNode.GetService <IRuntimeService>().InternalContextSwitchService;

            if (source.JSSource.IsValid)
            {
                return(source.JSSource);
            }
            switch (source.BufferSource)
            {
            case SharedBufferSourceEnum.CreateByDotnet:
                return(switchService.With <JavaScriptValue>(
                           () =>
                {
                    if (arrayBufferSource == null)
                    {
                        throw new ArgumentNullException(nameof(arrayBufferSource));
                    }
                    var result = JavaScriptValue.CreateTypedArray(source.ArrayType, arrayBufferSource.Value, source.Position, source.UnitCount);
                    source.SetJSSource(result, switchService);
                    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.CreateTypedArray(source.ArrayType, JavaScriptValue.Invalid, source.Position, source.UnitCount);
                    source.SetJSSource(result, switchService);        //hold the objec
                    //get the internal storage
                    JavaScriptValue.GetTypedArrayStorage(result, out IntPtr data, out uint bufferLength, out JavaScriptTypedArrayType type, out int elementSize);
                    source.InitWindow(data, false);
                    source.InitBeforeConvert(source.Buffer);
                    return result;
                }
                           ));

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

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