/// <summary> /// Invokes a function. /// </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, if any.</returns> public JavaScriptValue CallFunction(params JavaScriptValue[] arguments) { if (arguments.Length > UInt16.MaxValue) { throw new ArgumentOutOfRangeException("arguments"); } Native.ThrowIfError(Native.JsCallFunction(this, arguments, (UInt16)arguments.Length, out JavaScriptValue returnReference)); return(returnReference); }
public string RunScript(string script) { IntPtr returnValue; try { JavaScriptValue result; if (Native.JsRunScript(script, currentSourceContext++, "", out result) != JavaScriptErrorCode.NoError) { throw new Exception(GetErrorMessage()); } // Execute promise tasks stored in taskQueue while (taskQueue.Count != 0) { JavaScriptValue task = taskQueue.Dequeue(); JavaScriptValue promiseResult; JavaScriptValue global; Native.JsGetGlobalObject(out global); JavaScriptValue[] args = new JavaScriptValue[1] { global }; Native.JsCallFunction(task, args, 1, out promiseResult); } // Convert the return value. JavaScriptValue stringResult; UIntPtr stringLength; if (Native.JsConvertValueToString(result, out stringResult) != JavaScriptErrorCode.NoError) { return("failed to convert value to string."); } if (Native.JsStringToPointer(stringResult, out returnValue, out stringLength) != JavaScriptErrorCode.NoError) { return("failed to convert return value."); } } catch (Exception e) { return("chakrahost: fatal error: internal error: " + e.Message); } return(Marshal.PtrToStringUni(returnValue)); }
public JavaScriptValue RunScriptForJavaScriptValue(string script) { JavaScriptValue result; if (Native.JsRunScript(script, currentSourceContext++, string.Empty, out result) != JavaScriptErrorCode.NoError) { throw new Exception(GetErrorMessage()); } // Execute promise tasks stored in taskQueue while (taskQueue.Count != 0) { JavaScriptValue task = taskQueue.Dequeue(); JavaScriptValue promiseResult; JavaScriptValue global; Native.JsGetGlobalObject(out global); JavaScriptValue[] args = new JavaScriptValue[1] { global }; Native.JsCallFunction(task, args, 1, out promiseResult); } return(result); }