public static unsafe JsValue RunScriptUtf16Buffer( ReadOnlySpan <char> scriptBuffer, JsSourceContext sourceContext, ReadOnlySpan <char> sourceUrl) { //not work, because fixed.. https://github.com/dotnet/corefx/issues/31651 PInvoke marshaling will pin the pointer for ref byte hash arguments that will pin the whole block as side-effect fixed(char *scriptBufferPtr = &MemoryMarshal.GetReference(scriptBuffer)) fixed(char *sourceUrlPtr = &MemoryMarshal.GetReference(sourceUrl)) { JsErrorHelpers.ThrowIfError(NativeMethods.JsCreateStringUtf16((IntPtr)scriptBufferPtr, (uint)scriptBuffer.Length, out var scriptValue)); scriptValue.AddRef(); JsErrorHelpers.ThrowIfError(NativeMethods.JsCreateStringUtf16((IntPtr)sourceUrlPtr, (uint)sourceUrl.Length, out var sourceUrlValue)); sourceUrlValue.AddRef(); JsValue result; try { JsErrorHelpers.ThrowIfError(NativeMethods.JsRun(scriptValue, sourceContext, sourceUrlValue, JsParseScriptAttributes.None, out result)); } finally { scriptValue.Release(); sourceUrlValue.Release(); } return(result); } }
/// <summary> /// Serializes a parsed script to a buffer than can be reused /// </summary> /// <remarks> /// <para> /// SerializeScript parses a script and then stores the parsed form of the script in a /// runtime-independent format. The serialized script then can be deserialized in any /// runtime without requiring the script to be re-parsed. /// </para> /// <para> /// Requires an active script context. /// </para> /// </remarks> /// <param name="script">The script to serialize</param> /// <param name="buffer">The buffer to put the serialized script into. Can be null</param> /// <returns>The size of the buffer, in bytes, required to hold the serialized script</returns> public static ulong SerializeScript(string script, byte[] buffer) { var bufferSize = (ulong)buffer.Length; JsErrorCode errorCode; if (Utils.IsWindows()) { errorCode = NativeMethods.JsSerializeScript(script, buffer, ref bufferSize); JsErrorHelpers.ThrowIfError(errorCode); } else { JsValue scriptValue = JsValue.FromString(script); scriptValue.AddRef(); JsValue bufferValue; try { errorCode = NativeMethods.JsSerialize(scriptValue, out bufferValue, JsParseScriptAttributes.None); JsErrorHelpers.ThrowIfError(errorCode); } finally { scriptValue.Release(); } JsValue lengthValue = bufferValue.GetProperty("length"); bufferSize = Convert.ToUInt64(lengthValue.ConvertToNumber().ToDouble()); } return(bufferSize); }
/// <summary> /// Executes a script /// </summary> /// <remarks> /// Requires an active script context. /// </remarks> /// <param name="script">The script to run</param> /// <param name="sourceContext">A cookie identifying the script that can be used /// by debuggable script contexts</param> /// <param name="sourceUrl">The location the script came from</param> /// <param name="parseAttributes">Attribute mask for parsing the script</param> /// <returns>The result of the script, if any</returns> public static JsValue RunScript(string script, JsSourceContext sourceContext, string sourceUrl, ref JsParseScriptAttributes parseAttributes) { JsValue scriptValue = JsValue.FromString(script); scriptValue.AddRef(); JsValue sourceUrlValue = JsValue.FromString(sourceUrl); sourceUrlValue.AddRef(); JsValue result; try { JsErrorCode errorCode = NativeMethods.JsRun(scriptValue, sourceContext, sourceUrlValue, parseAttributes, out result); JsErrorHelpers.ThrowIfError(errorCode); } finally { scriptValue.Release(); sourceUrlValue.Release(); } return(result); }
/// <summary> /// Retrieves a string pointer of a <c>String</c> value /// </summary> /// <remarks> /// <para> /// This function retrieves the string pointer of a <c>String</c> value. It will fail with /// <c>InvalidArgument</c> if the type of the value is not <c>String</c>. /// </para> /// <para> /// Requires an active script context. /// </para> /// </remarks> /// <returns>The string</returns> public new string ToString() { string result; JsErrorCode errorCode; if (Utils.IsWindows()) { IntPtr ptr; UIntPtr stringLength; errorCode = NativeMethods.JsStringToPointer(this, out ptr, out stringLength); JsErrorHelpers.ThrowIfError(errorCode); result = Marshal.PtrToStringUni(ptr, (int)stringLength); } else { byte[] buffer = null; UIntPtr bufferSize = UIntPtr.Zero; UIntPtr written; errorCode = NativeMethods.JsCopyString(this, buffer, bufferSize, out written); JsErrorHelpers.ThrowIfError(errorCode); buffer = new byte[(int)written]; bufferSize = new UIntPtr((uint)written); errorCode = NativeMethods.JsCopyString(this, buffer, bufferSize, out written); JsErrorHelpers.ThrowIfError(errorCode); result = Encoding.GetEncoding(0).GetString(buffer); } return(result); }
/// <summary> /// Runs a serialized script /// </summary> /// <remarks> /// <para>Requires an active script context.</para> /// <para>The runtime will detach the data from the buffer and hold on to it until all /// instances of any functions created from the buffer are garbage collected.</para> /// </remarks> /// <param name="script">The source code of the serialized script</param> /// <param name="buffer">The serialized script</param> /// <param name="scriptLoadCallback">Callback to load the source code of the serialized script</param> /// <param name="sourceContext">A cookie identifying the script that can be used /// by debuggable script contexts</param> /// <param name="sourceUrl">The location the script came from</param> /// <returns>The result of running the script, if any</returns> public static JsValue RunSerializedScript(string script, byte[] buffer, JsSerializedLoadScriptCallback scriptLoadCallback, JsSourceContext sourceContext, string sourceUrl) { JsValue bufferValue = JsValue.CreateExternalArrayBuffer(buffer); bufferValue.AddRef(); JsValue sourceUrlValue = JsValue.FromString(sourceUrl); sourceUrlValue.AddRef(); JsValue result; try { JsErrorCode errorCode = NativeMethods.JsRunSerialized(bufferValue, scriptLoadCallback, sourceContext, sourceUrlValue, out result); JsErrorHelpers.ThrowIfError(errorCode); } finally { bufferValue.Release(); sourceUrlValue.Release(); } return(result); }
/// <summary> /// Executes a script /// </summary> /// <remarks> /// Requires an active script context. /// </remarks> /// <param name="script">The script to run</param> /// <param name="sourceContext">The cookie identifying the script that can be used /// by script contexts that have debugging enabled</param> /// <param name="sourceName">The location the script came from</param> /// <returns>The result of the script, if any</returns> public static JsValue RunScript(string script, JsSourceContext sourceContext, string sourceName) { JsValue result; JsErrorCode errorCode; if (Utils.IsWindows()) { errorCode = NativeMethods.JsRunScript(script, sourceContext, sourceName, out result); JsErrorHelpers.ThrowIfError(errorCode); } else { JsValue scriptValue = JsValue.FromString(script); scriptValue.AddRef(); JsValue sourceUrlValue = JsValue.FromString(sourceName); sourceUrlValue.AddRef(); try { errorCode = NativeMethods.JsRun(scriptValue, sourceContext, sourceUrlValue, JsParseScriptAttributes.None, out result); JsErrorHelpers.ThrowIfError(errorCode); } finally { scriptValue.Release(); sourceUrlValue.Release(); } } return(result); }
/// <summary> /// Defines a new object's own property from a property descriptor /// </summary> /// <remarks> /// Requires an active script context. /// </remarks> /// <param name="propertyId">The ID of the property</param> /// <param name="propertyDescriptor">The property descriptor</param> /// <returns>Whether the property was defined</returns> public bool DefineProperty(JsPropertyId propertyId, JsValue propertyDescriptor) { bool result; JsErrorHelpers.ThrowIfError(NativeMethods.JsDefineProperty(this, propertyId, propertyDescriptor, out result)); return(result); }
/// <summary> /// Gets an object's property /// </summary> /// <remarks> /// Requires an active script context. /// </remarks> /// <param name="id">The ID of the property</param> /// <returns>The value of the property</returns> public JsValue GetProperty(JsPropertyId id) { JsValue propertyReference; JsErrorHelpers.ThrowIfError(NativeMethods.JsGetProperty(this, id, out propertyReference)); return(propertyReference); }
/// <summary> /// Deletes an object's property /// </summary> /// <remarks> /// Requires an active script context. /// </remarks> /// <param name="propertyId">The ID of the property</param> /// <param name="useStrictRules">The property set should follow strict mode rules</param> /// <returns>Whether the property was deleted</returns> public JsValue DeleteProperty(JsPropertyId propertyId, bool useStrictRules) { JsValue returnReference; JsErrorHelpers.ThrowIfError(NativeMethods.JsDeleteProperty(this, propertyId, useStrictRules, out returnReference)); return(returnReference); }
/// <summary> /// Gets a list of all properties on the object /// </summary> /// <remarks> /// Requires an active script context. /// </remarks> /// <returns>An array of property names</returns> public JsValue GetOwnPropertyNames() { JsValue propertyNamesReference; JsErrorHelpers.ThrowIfError(NativeMethods.JsGetOwnPropertyNames(this, out propertyNamesReference)); return(propertyNamesReference); }
/// <summary> /// Determines whether an object has a non-inherited property /// </summary> /// <remarks> /// Requires an active script context. /// </remarks> /// <param name="propertyId">The ID of the property</param> /// <returns>Whether the object has the non-inherited property</returns> public bool HasOwnProperty(JsPropertyId propertyId) { bool hasOwnProperty; JsErrorHelpers.ThrowIfError(NativeMethods.JsHasOwnProperty(this, propertyId, out hasOwnProperty)); return(hasOwnProperty); }
/// <summary> /// Creates a new JavaScript function /// </summary> /// <remarks> /// Requires an active script context. /// </remarks> /// <param name="function">The method to call when the function is invoked</param> /// <param name="callbackData">Data to be provided to all function callbacks</param> /// <returns>The new function object</returns> public static JsValue CreateFunction(JsNativeFunction function, IntPtr callbackData) { JsValue reference; JsErrorHelpers.ThrowIfError(NativeMethods.JsCreateFunction(function, callbackData, out reference)); return(reference); }
/// <summary> /// Gets a property descriptor for an object's own property /// </summary> /// <remarks> /// Requires an active script context. /// </remarks> /// <param name="propertyId">The ID of the property</param> /// <returns>The property descriptor</returns> public JsValue GetOwnPropertyDescriptor(JsPropertyId propertyId) { JsValue descriptorReference; JsErrorHelpers.ThrowIfError(NativeMethods.JsGetOwnPropertyDescriptor(this, propertyId, out descriptorReference)); return(descriptorReference); }
/// <summary> /// Test if an object has a value at the specified index /// </summary> /// <remarks> /// Requires an active script context. /// </remarks> /// <param name="index">The index to test</param> /// <returns>Whether the object has an value at the specified index</returns> public bool HasIndexedProperty(JsValue index) { bool hasProperty; JsErrorHelpers.ThrowIfError(NativeMethods.JsHasIndexedProperty(this, index, out hasProperty)); return(hasProperty); }
/// <summary> /// Tells the runtime to do any idle processing it need to do /// </summary> /// <remarks> /// <para> /// If idle processing has been enabled for the current runtime, calling <c>Idle</c> will /// inform the current runtime that the host is idle and that the runtime can perform /// memory cleanup tasks. /// </para> /// <para> /// <c>Idle</c> will also return the number of system ticks until there will be more idle work /// for the runtime to do. Calling <c>Idle</c> before this number of ticks has passed will do /// no work. /// </para> /// <para> /// Requires an active script context. /// </para> /// </remarks> /// <returns> /// The next system tick when there will be more idle work to do. Returns the /// maximum number of ticks if there no upcoming idle work to do. /// </returns> public static uint Idle() { uint ticks; JsErrorHelpers.ThrowIfError(NativeMethods.JsIdle(out ticks)); return(ticks); }
/// <summary> /// Retrieves a <c>int</c> value of a <c>Number</c> value /// </summary> /// <remarks> /// <para> /// This function retrieves the value of a Number value. It will fail with /// <c>InvalidArgument</c> if the type of the value is not <c>Number</c>. /// </para> /// <para> /// Requires an active script context. /// </para> /// </remarks> /// <returns>The <c>int</c> value</returns> public int ToInt32() { int value; JsErrorHelpers.ThrowIfError(NativeMethods.JsNumberToInt(this, out value)); return(value); }
/// <summary> /// Converts a value to <c>Boolean</c> using regular JavaScript semantics /// </summary> /// <remarks> /// Requires an active script context. /// </remarks> /// <returns>The converted value</returns> public JsValue ConvertToBoolean() { JsValue booleanReference; JsErrorHelpers.ThrowIfError(NativeMethods.JsConvertValueToBoolean(this, out booleanReference)); return(booleanReference); }
/// <summary> /// Retrieves a <c>double</c> value of a <c>Number</c> value /// </summary> /// <remarks> /// <para> /// This function retrieves the value of a Number value. It will fail with /// <c>InvalidArgument</c> if the type of the value is not <c>Number</c>. /// </para> /// <para> /// Requires an active script context. /// </para> /// </remarks> /// <returns>The <c>double</c> value</returns> public double ToDouble() { double value; JsErrorHelpers.ThrowIfError(NativeMethods.JsNumberToDouble(this, out value)); return(value); }
/// <summary> /// Retrieves a <c>bool</c> value of a <c>Boolean</c> value /// </summary> /// <remarks> /// Requires an active script context. /// </remarks> /// <returns>The converted value</returns> public bool ToBoolean() { bool value; JsErrorHelpers.ThrowIfError(NativeMethods.JsBooleanToBool(this, out value)); return(value); }
/// <summary> /// Adds a reference to the object /// </summary> /// <remarks> /// This only needs to be called on objects that are not going to be stored somewhere on /// the stack. Calling AddRef ensures that the JavaScript object the value refers to will not be freed /// until Release is called /// </remarks> /// <returns>The object's new reference count</returns> public uint AddRef() { uint count; JsErrorHelpers.ThrowIfError(NativeMethods.JsAddRef(this, out count)); return(count); }
/// <summary> /// Creates a new JavaScript URIError error object /// </summary> /// <remarks> /// Requires an active script context. /// </remarks> /// <param name="message">Message for the error object</param> /// <returns>The new error object</returns> public static JsValue CreateUriError(JsValue message) { JsValue reference; JsErrorHelpers.ThrowIfError(NativeMethods.JsCreateURIError(message, out reference)); return(reference); }
/// <summary> /// Creates a JavaScript array object /// </summary> /// <remarks> /// Requires an active script context. /// </remarks> /// <param name="length">The initial length of the array</param> /// <returns>The new array object</returns> public static JsValue CreateArray(uint length) { JsValue reference; JsErrorHelpers.ThrowIfError(NativeMethods.JsCreateArray(length, out reference)); return(reference); }
/// <summary> /// Retrieves a value at the specified index of an object /// </summary> /// <remarks> /// Requires an active script context. /// </remarks> /// <param name="index">The index to retrieve</param> /// <returns>The retrieved value</returns> public JsValue GetIndexedProperty(JsValue index) { JsValue propertyReference; JsErrorHelpers.ThrowIfError(NativeMethods.JsGetIndexedProperty(this, index, out propertyReference)); return(propertyReference); }
/// <summary> /// Returns a metadata relating to the exception that caused the runtime of the current context /// to be in the exception state and resets the exception state for that runtime. The metadata /// includes a reference to the exception itself. /// </summary> /// <remarks> /// <para> /// If the runtime of the current context is not in an exception state, this API will throw /// <see cref="JsErrorCode.InvalidArgument"/>. If the runtime is disabled, this will return /// an exception indicating that the script was terminated, but it will not clear the exception /// (the exception will be cleared if the runtime is re-enabled using /// <c>JsEnableRuntimeExecution</c>). /// </para> /// <para> /// The metadata value is a javascript object with the following properties: <c>exception</c>, the /// thrown exception object; <c>line</c>, the 0 indexed line number where the exception was thrown; /// <c>column</c>, the 0 indexed column number where the exception was thrown; <c>length</c>, the /// source-length of the cause of the exception; <c>source</c>, a string containing the line of /// source code where the exception was thrown; and <c>url</c>, a string containing the name of /// the script file containing the code that threw the exception. /// </para> /// <para> /// Requires an active script context. /// </para> /// </remarks> /// <returns>The exception metadata for the runtime of the current context</returns> public static JsValue GetAndClearExceptionWithMetadata() { JsValue metadata; JsErrorHelpers.ThrowIfError(NativeMethods.JsGetAndClearExceptionWithMetadata(out metadata)); return(metadata); }
/// <summary> /// Converts a value to <c>Number</c> using regular JavaScript semantics /// </summary> /// <remarks> /// Requires an active script context. /// </remarks> /// <returns>The converted value</returns> public JsValue ConvertToNumber() { JsValue numberReference; JsErrorHelpers.ThrowIfError(NativeMethods.JsConvertValueToNumber(this, out numberReference)); return(numberReference); }
/// <summary> /// Creates a new <c>Object</c> that stores some external data /// </summary> /// <remarks> /// Requires an active script context. /// </remarks> /// <param name="data">External data that the object will represent. May be null.</param> /// <param name="finalizer">The callback for when the object is finalized. May be null.</param> /// <returns>The new <c>Object</c></returns> public static JsValue CreateExternalObject(IntPtr data, JsFinalizeCallback finalizer) { JsValue reference; JsErrorHelpers.ThrowIfError(NativeMethods.JsCreateExternalObject(data, finalizer, out reference)); return(reference); }
/// <summary> /// Returns a exception that caused the runtime of the current context to be in the /// exception state and resets the exception state for that runtime /// </summary> /// <remarks> /// <para> /// If the runtime of the current context is not in an exception state, this API will throw /// <see cref="JsErrorCode.InvalidArgument"/>. If the runtime is disabled, this will return /// an exception indicating that the script was terminated, but it will not clear the exception /// (the exception will be cleared if the runtime is re-enabled using /// <c>JsEnableRuntimeExecution</c>). /// </para> /// <para> /// Requires an active script context. /// </para> /// </remarks> /// <returns>The exception for the runtime of the current context</returns> public static JsValue GetAndClearException() { JsValue exception; JsErrorHelpers.ThrowIfError(NativeMethods.JsGetAndClearException(out exception)); return(exception); }
/// <summary> /// Converts a value to <c>String</c> using regular JavaScript semantics /// </summary> /// <remarks> /// Requires an active script context. /// </remarks> /// <returns>The converted value</returns> public JsValue ConvertToString() { JsValue stringReference; JsErrorHelpers.ThrowIfError(NativeMethods.JsConvertValueToString(this, out stringReference)); return(stringReference); }
/// <summary> /// Releases a reference to a script context /// </summary> /// <remarks> /// Removes a reference to a context that was created by AddRef. /// </remarks> /// <returns>The object's new reference count</returns> public uint Release() { uint count; JsErrorHelpers.ThrowIfError(NativeMethods.JsContextRelease(this, out count)); return(count); }
/// <summary> /// Converts a value to <c>Object</c> using regular JavaScript semantics /// </summary> /// <remarks> /// Requires an active script context. /// </remarks> /// <returns>The converted value</returns> public JsValue ConvertToObject() { JsValue objectReference; JsErrorHelpers.ThrowIfError(NativeMethods.JsConvertValueToObject(this, out objectReference)); return(objectReference); }