Ejemplo 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);
        }
Ejemplo n.º 2
0
        //internal void InitBuffer(IntPtr handle,bool ownsHandle)
        //{
        //    InitWindow(handle, ownsHandle);
        //}

        //internal void InitBuffer()
        //{
        //    InitNew();
        //}


        /// <summary>
        /// Create a ArrayBuffer and assign memory inside dotnet caller, the memory will be released when the JSArrayBuffer is disposed
        /// </summary>
        /// <param name="size">arraybuffer size in bytes</param>
        /// <returns>JSArrayBuffer</returns>
        public static JSArrayBuffer Create(long size)
        {
            var result = new JSArrayBuffer(SharedBufferSourceEnum.CreateByDotnet, (ulong)size);

            result.InitNew();
            return(result);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
 private JSTypedArray(JavaScriptTypedArrayType type, JSArrayBuffer source, uint position, uint unitCount) : base(SharedBufferSourceEnum.CreateByDotnet, unitCount * GetUnitByteSizeByArrayType(type))
 {
     ArrayType   = type;
     Position    = position;
     UnitSize    = GetUnitByteSizeByArrayType(type);
     UnitCount   = unitCount;
     ArrayBuffer = source;
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Create a JSArrayBuffer and let javascript allocate memory for it. Read/Write to the JSArrayBuffer is not available until the object is passed to a javascript context
        /// </summary>
        /// <param name="size">arraybuffer size in bytes</param>
        /// <param name="init">Action to setup initial value when the JSArrayBuffer is initialized in the javascript context</param>
        /// <returns>JSArrayBuffer</returns>
        public static JSArrayBuffer CreateInJavascript(uint size, Action <SharedMemoryBuffer> init)
        {
            var result = new JSArrayBuffer(SharedBufferSourceEnum.CreateInJavascript, (ulong)size);

            if (init != null)
            {
                result.SetupInitValueAction(init);
            }
            return(result);
        }
Ejemplo n.º 6
0
        public static JSDataView CreateFromArrayBuffer(JSArrayBuffer source, uint position, uint size, Action <SharedMemoryBuffer> init)
        {
            JSDataView result;

            if (source.Buffer == null)
            {
                result = new JSDataView(SharedBufferSourceEnum.CreateInJavascript, source, position, size);
            }
            else
            {
                result = new JSDataView(SharedBufferSourceEnum.CreateByDotnet, source, position, size);
                result.InitWindow(source.Buffer, position);
            }
            if (init != null)
            {
                result.SetupInitValueAction(init);
            }
            return(result);
        }
Ejemplo n.º 7
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");
                }
            }
                                                        ));
        }
Ejemplo n.º 8
0
 public JSDataView(SharedBufferSourceEnum source, JSArrayBuffer arrayBuffer, uint position, uint size) : base(source, size)
 {
     Position    = position;
     ArrayBuffer = arrayBuffer;
 }
Ejemplo n.º 9
0
        public static JSTypedArray CreateFromArrayBuffer(JavaScriptTypedArrayType type, JSArrayBuffer source, uint position, uint unitCount)
        {
            JSTypedArray result = new JSTypedArray(type, source, position, unitCount);

            result.InitWindow(source.Buffer, position);
            return(result);
        }