Пример #1
0
 public JsValueHolder(IContextSwitchService contextSwitch, JavaScriptValue value)
 {
     this.contextSwitch = contextSwitch;
     contextSwitch.With(() =>
     {
         value.AddRef();
     });
 }
Пример #2
0
 internal void SetJSSource(JavaScriptValue value, IContextSwitchService context)
 {
     if (JSSource.IsValid)
     {
         throw new InvalidOperationException("cannot set jsvalue twice");
     }
     if (!value.IsValid)
     {
         throw new ArgumentException("not a valid javascriptvalue", nameof(value));
     }
     value.AddRef();
     JSSource       = value;
     releaseJSValue = new Action(() => { context.With(() => value.Release()); });
 }
Пример #3
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");
            }
        }
Пример #4
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");
            }
        }
Пример #5
0
            private bool disposedValue = false; // To detect redundant calls

            protected virtual void Dispose(bool disposing)
            {
                if (!disposedValue)
                {
                    if (disposing)
                    {
                        // TODO: dispose managed state (managed objects).
                        contextSwitch.With(() =>
                        {
                            value.Release();
                        });
                    }

                    // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
                    // TODO: set large fields to null.

                    disposedValue = true;
                }
            }