private static void Run(JavaScriptRuntime runtime, ViewModel vm) { try { vm.Result.Value = JavaScriptContext.RunScript(vm.Script.Value) .ConvertToString() .ToString(); } catch (JavaScriptScriptException e) { vm.Result.Value = e.Error .ConvertToString() .ToString(); } }
internal static extern JavaScriptErrorCode JsCreateContext(JavaScriptRuntime runtime, out JavaScriptContext newContext);
internal static extern JavaScriptErrorCode JsEnableRuntimeExecution(JavaScriptRuntime runtime);
internal static extern JavaScriptErrorCode JsGetRuntimeMemoryUsage(JavaScriptRuntime runtime, out UIntPtr memoryUsage);
internal static extern JavaScriptErrorCode JsSetRuntimeMemoryAllocationCallback(JavaScriptRuntime runtime, IntPtr callbackState, JavaScriptMemoryAllocationCallback allocationCallback);
internal static extern JavaScriptErrorCode JsGetRuntime(JavaScriptContext context, out JavaScriptRuntime runtime);
internal static extern JavaScriptErrorCode JsCollectGarbage(JavaScriptRuntime handle);
internal static extern JavaScriptErrorCode JsDiagStartDebugging( JavaScriptRuntime handle, JavaScriptDiagDebugEventCallback debugEventCallback, IntPtr callbackState);
public static extern JavaScriptErrorCode JsGetRuntimeMemoryLimit(JavaScriptRuntime runtime, out UIntPtr memoryLimit);
public static extern JavaScriptErrorCode JsDisableRuntimeExecution(JavaScriptRuntime runtime);
internal static extern JavaScriptErrorCode JsCreateContext(JavaScriptRuntime runtime, IDebugApplication32 debugSite, out JavaScriptContext newContext);
private static JavaScriptContext CreateHostContext(JavaScriptRuntime runtime, string[] arguments, int argumentsStart) { // // Create the context. Note that if we had wanted to start debugging from the very // beginning, we would have passed in an IDebugApplication pointer here. // JavaScriptContext context = runtime.CreateContext(); // // Now set the execution context as being the current one on this thread. // using (new JavaScriptContext.Scope(context)) { // // Create the host object the script will use. // JavaScriptValue hostObject = JavaScriptValue.CreateObject(); // // Get the global object // JavaScriptValue globalObject = JavaScriptValue.GlobalObject; // // Get the name of the property ("host") that we're going to set on the global object. // JavaScriptPropertyId hostPropertyId = JavaScriptPropertyId.FromString("host"); // // Set the property. // globalObject.SetProperty(hostPropertyId, hostObject, true); // // Now create the host callbacks that we're going to expose to the script. // DefineHostCallback(hostObject, "echo", echoDelegate, IntPtr.Zero); DefineHostCallback(hostObject, "runScript", runScriptDelegate, IntPtr.Zero); // // Create an array for arguments. // JavaScriptValue hostArguments = JavaScriptValue.CreateArray((uint)(arguments.Length - argumentsStart)); for (int index = argumentsStart; index < arguments.Length; index++) { // // Create the argument value. // JavaScriptValue argument = JavaScriptValue.FromString(arguments[index]); // // Create the index. // JavaScriptValue indexValue = JavaScriptValue.FromInt32(index - argumentsStart); // // Set the value. // hostArguments.SetIndexedProperty(indexValue, argument); } // // Get the name of the property that we're going to set on the host object. // JavaScriptPropertyId argumentsPropertyId = JavaScriptPropertyId.FromString("arguments"); // // Set the arguments property. // hostObject.SetProperty(argumentsPropertyId, hostArguments, true); } return context; }
internal static extern JavaScriptErrorCode JsSetRuntimeBeforeCollectCallback(JavaScriptRuntime runtime, IntPtr callbackState, JavaScriptBeforeCollectCallback beforeCollectCallback);
internal static extern JavaScriptErrorCode JsIsRuntimeExecutionDisabled(JavaScriptRuntime runtime, out bool isDisabled);
internal static extern JavaScriptErrorCode JsCreateRuntime(JavaScriptRuntimeAttributes attributes, JavaScriptThreadServiceCallback threadService, out JavaScriptRuntime runtime);
internal static extern JavaScriptErrorCode JsDisposeRuntime(JavaScriptRuntime handle);
internal static extern JavaScriptErrorCode JsSetRuntimeMemoryLimit(JavaScriptRuntime runtime, UIntPtr memoryLimit);
private void InitChakraRuntime() { runtime = JavaScriptRuntime.Create(); JavaScriptContext.Current = runtime.CreateContext(); }