/// <summary> /// Disposes a runtime /// </summary> /// <remarks> /// Once a runtime has been disposed, all resources owned by it are invalid and cannot be used. /// If the runtime is active (i.e. it is set to be current on a particular thread), it cannot /// be disposed. /// </remarks> public void Dispose() { if (IsValid) { JsErrorHelpers.ThrowIfError(NativeMethods.JsDisposeRuntime(this)); } _handle = IntPtr.Zero; }
/// <summary> /// Invokes a function as a constructor /// </summary> /// <remarks> /// Requires an active script context. /// </remarks> /// <param name="arguments">The arguments to the call</param> /// <returns>The <c>Value</c> returned from the function invocation</returns> public JsValue ConstructObject(params JsValue[] arguments) { JsValue returnReference; if (arguments.Length > ushort.MaxValue) { throw new ArgumentOutOfRangeException("arguments"); } JsErrorHelpers.ThrowIfError(NativeMethods.JsConstructObject(this, arguments, (ushort)arguments.Length, out returnReference)); return(returnReference); }
/// <summary> /// Gets a property ID associated with the name /// </summary> /// <remarks> /// <para> /// Property IDs are specific to a context and cannot be used across contexts. /// </para> /// <para> /// Requires an active script context. /// </para> /// </remarks> /// <param name="name">The name of the property ID to get or create. /// The name may consist of only digits.</param> /// <returns>The property ID in this runtime for the given name</returns> public static JsPropertyId FromString(string name) { JsPropertyId id; JsErrorCode errorCode; if (Utils.IsWindows()) { errorCode = NativeMethods.JsGetPropertyIdFromName(name, out id); } else { var byteCount = new UIntPtr((uint)Encoding.GetEncoding(0).GetByteCount(name)); errorCode = NativeMethods.JsCreatePropertyIdUtf8(name, byteCount, out id); } JsErrorHelpers.ThrowIfError(errorCode); return(id); }
/// <summary> /// Creates a <c>String</c> value from a string pointer /// </summary> /// <remarks> /// Requires an active script context. /// </remarks> /// <param name="value">The string to convert to a <c>String</c> value</param> /// <returns>The new <c>String</c> value</returns> public static JsValue FromString(string value) { JsValue reference; JsErrorCode errorCode; if (Utils.IsWindows()) { var stringLength = new UIntPtr((uint)value.Length); errorCode = NativeMethods.JsPointerToString(value, stringLength, out reference); } else { var byteCount = new UIntPtr((uint)Encoding.GetEncoding(0).GetByteCount(value)); errorCode = NativeMethods.JsCreateStringUtf8(value, byteCount, out reference); } JsErrorHelpers.ThrowIfError(errorCode); return(reference); }
/// <summary> /// Sets a callback function that is called by the runtime before garbage collection /// </summary> /// <remarks> /// <para> /// The callback is invoked on the current runtime execution thread, therefore execution is /// blocked until the callback completes. /// </para> /// <para> /// The callback can be used by hosts to prepare for garbage collection. For example, by /// releasing unnecessary references on Chakra objects. /// </para> /// </remarks> /// <param name="callbackState">User provided state that will be passed back to the callback</param> /// <param name="beforeCollectCallback">The callback function being set</param> public void SetBeforeCollectCallback(IntPtr callbackState, JsBeforeCollectCallback beforeCollectCallback) { JsErrorHelpers.ThrowIfError(NativeMethods.JsSetRuntimeBeforeCollectCallback(this, callbackState, beforeCollectCallback)); }
/// <summary> /// Sets a memory allocation callback for specified runtime /// </summary> /// <remarks> /// <para> /// Registering a memory allocation callback will cause the runtime to call back to the host /// whenever it acquires memory from, or releases memory to, the OS. The callback routine is /// called before the runtime memory manager allocates a block of memory. The allocation will /// be rejected if the callback returns false. The runtime memory manager will also invoke the /// callback routine after freeing a block of memory, as well as after allocation failures. /// </para> /// <para> /// The callback is invoked on the current runtime execution thread, therefore execution is /// blocked until the callback completes. /// </para> /// <para> /// The return value of the callback is not stored; previously rejected allocations will not /// prevent the runtime from invoking the callback again later for new memory allocations. /// </para> /// </remarks> /// <param name="callbackState"> /// User provided state that will be passed back to the callback. /// </param> /// <param name="allocationCallback"> /// Memory allocation callback to be called for memory allocation events. /// </param> public void SetMemoryAllocationCallback(IntPtr callbackState, JsMemoryAllocationCallback allocationCallback) { JsErrorHelpers.ThrowIfError(NativeMethods.JsSetRuntimeMemoryAllocationCallback(this, callbackState, allocationCallback)); }
/// <summary> /// Performs a full garbage collection /// </summary> public void CollectGarbage() { JsErrorHelpers.ThrowIfError(NativeMethods.JsCollectGarbage(this)); }
/// <summary> /// Deletes d value at the specified index of an object /// </summary> /// <remarks> /// Requires an active script context. /// </remarks> /// <param name="index">The index to delete</param> public void DeleteIndexedProperty(JsValue index) { JsErrorHelpers.ThrowIfError(NativeMethods.JsDeleteIndexedProperty(this, index)); }
/// <summary> /// Sets a value at the specified index of an object /// </summary> /// <remarks> /// Requires an active script context. /// </remarks> /// <param name="index">The index to set</param> /// <param name="value">The value to set</param> public void SetIndexedProperty(JsValue index, JsValue value) { JsErrorHelpers.ThrowIfError(NativeMethods.JsSetIndexedProperty(this, index, value)); }
/// <summary> /// Sets an object's property /// </summary> /// <remarks> /// Requires an active script context. /// </remarks> /// <param name="id">The ID of the property</param> /// <param name="value">The new value of the property</param> /// <param name="useStrictRules">The property set should follow strict mode rules</param> public void SetProperty(JsPropertyId id, JsValue value, bool useStrictRules) { JsErrorHelpers.ThrowIfError(NativeMethods.JsSetProperty(this, id, value, useStrictRules)); }
/// <summary> /// Sets an object to not be extensible /// </summary> /// <remarks> /// Requires an active script context /// </remarks> public void PreventExtension() { JsErrorHelpers.ThrowIfError(NativeMethods.JsPreventExtension(this)); }
/// <summary> /// Sets a runtime of the current context to an exception state /// </summary> /// <remarks> /// <para> /// If the runtime of the current context is already in an exception state, this API will /// throw <c>JsErrorInExceptionState</c>. /// </para> /// <para> /// Requires an active script context. /// </para> /// </remarks> /// <param name="exception">The JavaScript exception to set for the runtime of the current context</param> public static void SetException(JsValue exception) { JsErrorHelpers.ThrowIfError(NativeMethods.JsSetException(exception)); }