public static JavaScriptValue ParseScript( string script, JavaScriptSourceContext sourceContext, string sourceName, JavaScriptParseScriptAttributes parseAttributes, bool ignoreScriptError = false ) { return(ParseScript( JavaScriptValue.FromString(script), sourceContext, JavaScriptValue.FromString(sourceName), parseAttributes, ignoreScriptError )); }
/// <summary> /// Parses a script and returns a function representing the script. /// </summary> /// <remarks> /// <para> /// Requires an active script context. /// </para> /// <para> /// Script source can be either JavascriptString or JavascriptExternalArrayBuffer. /// In case it is an ExternalArrayBuffer, and the encoding of the buffer is Utf16, /// JsParseScriptAttributeArrayBufferIsUtf16Encoded is expected on parseAttributes. /// </para> /// <para> /// Use JavascriptExternalArrayBuffer with Utf8/ASCII script source /// for better performance and smaller memory footprint. /// </para> /// </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 compiled script. /// </returns> public static JavaScriptValue ParseScript( JavaScriptValue script, JavaScriptSourceContext sourceContext, JavaScriptValue sourceUrl, JavaScriptParseScriptAttributes parseAttributes, bool ignoreScriptError = false ) { JavaScriptValue result; Native.ThrowIfError( Native.JsParse( script, sourceContext, sourceUrl, parseAttributes, out result ), ignoreScriptError ); return(result); }
/// <summary> /// Executes a script, automatically storing the script value within an ExternalArrayBuffer. /// </summary> /// <remarks> /// This extension method exists for simplicity. /// </remarks> /// <param name="jsrt"></param> /// <param name="script"></param> /// <param name="sourceContext"></param> /// <param name="sourceUrl"></param> /// <param name="parseAttributes"></param> /// <param name="result"></param> /// <returns>A JavaScriptValueSafeHandle containing the result.</returns> public static JavaScriptValueSafeHandle JsRunScript(this IJavaScriptEngine jsrt, string script, JavaScriptSourceContext sourceContext = default(JavaScriptSourceContext), string sourceUrl = null, JavaScriptParseScriptAttributes parseAttributes = JavaScriptParseScriptAttributes.None) { var ptrScript = Marshal.StringToHGlobalAnsi(script); JavaScriptObjectFinalizeCallback callback = (IntPtr ptr) => { //Release pinned string. Marshal.FreeHGlobal(ptrScript); }; var scriptHandle = jsrt.JsCreateExternalArrayBuffer(ptrScript, (uint)script.Length, callback, IntPtr.Zero); if (sourceContext == default(JavaScriptSourceContext)) { sourceContext = JavaScriptSourceContext.None; } JavaScriptValueSafeHandle sourceUrlHandle; if (string.IsNullOrWhiteSpace(sourceUrl)) { sourceUrl = "[eval code]"; } sourceUrlHandle = jsrt.JsCreateString(EvalSourceUrl, (ulong)EvalSourceUrl.Length); if (parseAttributes == default(JavaScriptParseScriptAttributes)) { parseAttributes = JavaScriptParseScriptAttributes.None; } try { return(jsrt.JsRun(scriptHandle, sourceContext, sourceUrlHandle, parseAttributes)); } finally { //Release variables created during this operation. sourceUrlHandle.Dispose(); scriptHandle.Dispose(); } }
public static extern JavaScriptErrorCode JsRun(JavaScriptValue script, JavaScriptSourceContext sourceContext, JavaScriptValue sourceUrl, JavaScriptParseScriptAttributes parseAttributes, out JavaScriptValue result);
public static extern JavaScriptErrorCode JsDiagEvaluate(JavaScriptValue expression, uint stackFrameIndex, JavaScriptParseScriptAttributes parseAttributes, bool forceSetValueProp, out JavaScriptValue evalResult);
public JavaScriptValueSafeHandle JsParseScriptWithAttributes(string script, JavaScriptSourceContext sourceContext, string sourceUrl, JavaScriptParseScriptAttributes parseAttributes) { Errors.ThrowIfError(LibChakraCore.JsParseScriptWithAttributes(script, sourceContext, sourceUrl, parseAttributes, out JavaScriptValueSafeHandle result)); result.NativeFunctionSource = nameof(LibChakraCore.JsParseScriptWithAttributes); if (result != JavaScriptValueSafeHandle.Invalid) { Errors.ThrowIfError(LibChakraCore.JsAddRef(result, out uint valueRefCount)); } return(result); }