public ChakraHost() { if (Native.JsCreateRuntime(JavaScriptRuntimeAttributes.None, null, out runtime) != JavaScriptErrorCode.NoError) { throw new Exception("failed to create runtime."); } if (Native.JsCreateContext(runtime, out context) != JavaScriptErrorCode.NoError) throw new Exception("failed to create execution context."); ResetContext(); // ES6 Promise callback JavaScriptPromiseContinuationCallback promiseContinuationCallback = delegate (JavaScriptValue task, IntPtr callbackState) { promiseCallback = task; }; if (Native.JsSetPromiseContinuationCallback(promiseContinuationCallback, IntPtr.Zero) != JavaScriptErrorCode.NoError) throw new Exception("failed to setup callback for ES6 Promise"); // Projections if (Native.JsProjectWinRTNamespace("ChakraBridge") != JavaScriptErrorCode.NoError) throw new Exception("failed to project ChakraBridge namespace."); //#if DEBUG // // Debug // if (Native.JsStartDebugging() != JavaScriptErrorCode.NoError) // throw new Exception("failed to start debugging."); //#endif }
static async void ExecuteAsync(int delay, JavaScriptValue callbackValue, JavaScriptValue callee) { await Task.Delay(delay); callbackValue.CallFunction(callee); uint refCount; Native.JsRelease(callbackValue, out refCount); Native.JsRelease(callee, out refCount); }
public ChakraHost() { if (Native.JsCreateRuntime(JavaScriptRuntimeAttributes.None, null, out runtime) != JavaScriptErrorCode.NoError) { throw new Exception("failed to create runtime."); } if (Native.JsCreateContext(runtime, out context) != JavaScriptErrorCode.NoError) throw new Exception("failed to create execution context."); if (Native.JsSetCurrentContext(context) != JavaScriptErrorCode.NoError) throw new Exception("failed to set current context."); // ES6 Promise callback JavaScriptPromiseContinuationCallback promiseContinuationCallback = delegate (JavaScriptValue task, IntPtr callbackState) { promiseCallback = task; }; if (Native.JsSetPromiseContinuationCallback(promiseContinuationCallback, IntPtr.Zero) != JavaScriptErrorCode.NoError) throw new Exception("failed to setup callback for ES6 Promise"); // Bind to global object // setTimeout SetTimeoutJavaScriptNativeFunction = SetTimeout.SetTimeoutJavaScriptNativeFunction; DefineHostCallback("setTimeout", SetTimeoutJavaScriptNativeFunction); SendToHostJavaScriptNativeFunction = CommunicationManager.SendToHostJavaScriptNativeFunction; DefineHostCallback("sendToHost", SendToHostJavaScriptNativeFunction); // Projections if (Native.JsProjectWinRTNamespace("ChakraBridge") != JavaScriptErrorCode.NoError) throw new Exception("failed to project ChakraBridge namespace."); this.window = new Window(); ProjectObjectToGlobal(new Console(), "console"); ProjectObjectToGlobal(this.window, "window"); ProjectObjectToGlobal(this.window.navigator, "navigator"); ProjectObjectToGlobal(this.window.document, "document"); // Add references RunScript(@"XMLHttpRequest = ChakraBridge.XMLHttpRequest; HTMLCanvasElement = ChakraBridge.HTMLCanvasElementWrapper; atob = window.atob; btoa = window.btoa;"); #if DEBUG // Debug if (Native.JsStartDebugging() != JavaScriptErrorCode.NoError) throw new Exception("failed to start debugging."); #endif }
public static JavaScriptValue SetTimeoutJavaScriptNativeFunction(JavaScriptValue callee, bool isConstructCall, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)] JavaScriptValue[] arguments, ushort argumentCount, IntPtr callbackData) { // setTimeout signature is (callback, after) JavaScriptValue callbackValue = arguments[1]; JavaScriptValue afterValue = arguments[2].ConvertToNumber(); var after = Math.Max(afterValue.ToDouble(), 1); uint refCount; Native.JsAddRef(callbackValue, out refCount); Native.JsAddRef(callee, out refCount); ExecuteAsync((int)after, callbackValue, callee); return JavaScriptValue.True; }
public static JavaScriptValue PrintJavaScriptNativeFunction(JavaScriptValue callee, bool isConstructCall, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)] JavaScriptValue[] arguments, ushort argumentCount, IntPtr callbackData) { // setTimeout signature is (callback, after) //JavaScriptValue callbackValue = arguments[1]; //JavaScriptValue afterValue = arguments[2].ConvertToNumber(); //var after = Math.Max(afterValue.ToDouble(), 1); //uint refCount; //Native.JsAddRef(callbackValue, out refCount); //Native.JsAddRef(callee, out refCount); //ExecuteAsync((int)after, callbackValue, callee); if (arguments.Length > 0) { String str = arguments[0].ConvertToString().ToString(); Debug.WriteLine("JsEngine.print", str); } return JavaScriptValue.True; }
internal static JavaScriptValue SendToHostJavaScriptNativeFunction(JavaScriptValue callee, bool isConstructCall, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)] JavaScriptValue[] arguments, ushort argumentCount, IntPtr callbackData) { // sendToHost signature is (json, type) JavaScriptValue jsonValue = arguments[1]; string json = jsonValue.ConvertToString().ToString(); JavaScriptValue typeValue = arguments[2]; string typename = typeValue.ConvertToString().ToString(); if (!RegisteredTypes.ContainsKey(typename)) { throw new Exception("Not registered type found: " + typename); } var type = RegisteredTypes[typename]; var poco = JsonConvert.DeserializeObject(json, type); OnObjectReceived?.Invoke(poco); return JavaScriptValue.True; }
public static extern JavaScriptErrorCode JsCreateNamedFunction(JavaScriptValue name, JavaScriptNativeFunction nativeFunction, IntPtr callbackState, out JavaScriptValue function);
public static extern JavaScriptErrorCode JsGetAndClearException(out JavaScriptValue exception);
public static extern JavaScriptErrorCode JsCreateFunction(JavaScriptNativeFunction nativeFunction, IntPtr externalData, out JavaScriptValue function);
internal static extern JavaScriptErrorCode JsGetNullValue(out JavaScriptValue nullValue);
public static extern JavaScriptErrorCode JsConstructObject(JavaScriptValue function, JavaScriptValue[] arguments, ushort argumentCount, out JavaScriptValue result);
internal static extern JavaScriptErrorCode JsNumberToInt(JavaScriptValue value, out int intValue);
internal static extern JavaScriptErrorCode JsRelease(JavaScriptValue reference, out uint count);
public static extern JavaScriptErrorCode JsGetTypedArrayStorage(JavaScriptValue typedArray, out byte[] buffer, out uint bufferLength, out JavaScriptTypedArrayType arrayType, out int elementSize);
public static extern JavaScriptErrorCode JsGetDataViewStorage(JavaScriptValue dataView, out byte[] buffer, out uint bufferLength);
public static extern JavaScriptErrorCode JsCreateDataView(JavaScriptValue arrayBuffer, uint byteOffset, uint byteOffsetLength, out JavaScriptValue result);
public static extern JavaScriptErrorCode JsGetArrayBufferStorage(JavaScriptValue arrayBuffer, out byte[] buffer, out uint bufferLength);
public static extern JavaScriptErrorCode JsCreateTypedArray(JavaScriptTypedArrayType arrayType, JavaScriptValue arrayBuffer, uint byteOffset, uint elementLength, out JavaScriptValue result);
public static extern JavaScriptErrorCode JsCreateArrayBuffer(uint byteLength, out JavaScriptValue result);
public static extern JavaScriptErrorCode JsInspectableToObject([MarshalAs(UnmanagedType.IInspectable)] System.Object inspectable, out JavaScriptValue value);
internal static extern JavaScriptErrorCode JsCreateSymbol(JavaScriptValue description, out JavaScriptValue symbol);
public static extern JavaScriptErrorCode JsCreateSymbol(JavaScriptValue description, out JavaScriptValue symbol);
internal static extern JavaScriptErrorCode JsGetPropertyIdFromSymbol(JavaScriptValue symbol, out JavaScriptPropertyId propertyId);
public static extern JavaScriptErrorCode JsGetSymbolFromPropertyId(JavaScriptPropertyId propertyId, out JavaScriptValue symbol);
internal static extern JavaScriptErrorCode JsHasIndexedPropertiesExternalData(JavaScriptValue obj, out bool value);
public static extern JavaScriptErrorCode JsGetPropertyIdFromSymbol(JavaScriptValue symbol, out JavaScriptPropertyId propertyId);
internal static extern JavaScriptErrorCode JsRunSerializedScript(string script, byte[] buffer, JavaScriptSourceContext sourceContext, string sourceUrl, out JavaScriptValue result);
internal static extern JavaScriptErrorCode JsCreateNamedFunction(JavaScriptValue name, JavaScriptNativeFunction nativeFunction, IntPtr callbackState, out JavaScriptValue function);
internal static extern JavaScriptErrorCode JsGetFalseValue(out JavaScriptValue falseValue);
internal static extern JavaScriptErrorCode JsCreateArrayBuffer(uint byteLength, out JavaScriptValue result);
public static extern JavaScriptErrorCode JsSetException(JavaScriptValue exception);
internal static extern JavaScriptErrorCode JsCreateDataView(JavaScriptValue arrayBuffer, uint byteOffset, uint byteOffsetLength, out JavaScriptValue result);
public static extern JavaScriptErrorCode JsCreateURIError(JavaScriptValue message, out JavaScriptValue error);
internal static extern JavaScriptErrorCode JsGetTypedArrayStorage(JavaScriptValue typedArray, out byte[] buffer, out uint bufferLength, out JavaScriptTypedArrayType arrayType, out int elementSize);
public static extern JavaScriptErrorCode JsGetOwnPropertySymbols(JavaScriptValue obj, out JavaScriptValue propertySymbols);
public static extern JavaScriptErrorCode JsSetObjectBeforeCollectCallback(JavaScriptValue reference, IntPtr callbackState, JavaScriptObjectBeforeCollectCallback beforeCollectCallback);
internal static extern JavaScriptErrorCode JsInspectableToObject([MarshalAs(UnmanagedType.IInspectable)] System.Object inspectable, out JavaScriptValue value);
public static extern JavaScriptErrorCode JsNumberToInt(JavaScriptValue value, out int intValue);
internal static extern JavaScriptErrorCode JsCreateTypedArray(JavaScriptTypedArrayType arrayType, JavaScriptValue arrayBuffer, uint byteOffset, uint elementLength, out JavaScriptValue result);
public static extern JavaScriptErrorCode JsGetIndexedPropertiesExternalData(JavaScriptValue obj, IntPtr data, out JavaScriptTypedArrayType arrayType, out uint elementLength);
internal static extern JavaScriptErrorCode JsGetArrayBufferStorage(JavaScriptValue arrayBuffer, out byte[] buffer, out uint bufferLength);
public static extern JavaScriptErrorCode JsHasIndexedPropertiesExternalData(JavaScriptValue obj, out bool value);
internal static extern JavaScriptErrorCode JsGetDataViewStorage(JavaScriptValue dataView, out byte[] buffer, out uint bufferLength);
public static extern JavaScriptErrorCode JsAddRef(JavaScriptValue reference, out uint count);
internal static extern JavaScriptErrorCode JsGetSymbolFromPropertyId(JavaScriptPropertyId propertyId, out JavaScriptValue symbol);
public static extern JavaScriptErrorCode JsRelease(JavaScriptValue reference, out uint count);
internal static extern JavaScriptErrorCode JsGetOwnPropertySymbols(JavaScriptValue obj, out JavaScriptValue propertySymbols);
public static extern JavaScriptErrorCode JsRunScript(string script, JavaScriptSourceContext sourceContext, string sourceUrl, out JavaScriptValue result);
internal static extern JavaScriptErrorCode JsGetIndexedPropertiesExternalData(JavaScriptValue obj, IntPtr data, out JavaScriptTypedArrayType arrayType, out uint elementLength);
public static extern JavaScriptErrorCode JsRunSerializedScript(string script, byte[] buffer, JavaScriptSourceContext sourceContext, string sourceUrl, out JavaScriptValue result);
internal static extern JavaScriptErrorCode JsAddRef(JavaScriptValue reference, out uint count);
public static extern JavaScriptErrorCode JsGetUndefinedValue(out JavaScriptValue undefinedValue);
internal static extern JavaScriptErrorCode JsRunScript(string script, JavaScriptSourceContext sourceContext, string sourceUrl, out JavaScriptValue result);
public static extern JavaScriptErrorCode JsGetNullValue(out JavaScriptValue nullValue);
internal static extern JavaScriptErrorCode JsGetUndefinedValue(out JavaScriptValue undefinedValue);
public static extern JavaScriptErrorCode JsGetTrueValue(out JavaScriptValue trueValue);
internal static extern JavaScriptErrorCode JsGetTrueValue(out JavaScriptValue trueValue);
public static extern JavaScriptErrorCode JsGetFalseValue(out JavaScriptValue falseValue);
internal static extern JavaScriptErrorCode JsBoolToBoolean(bool value, out JavaScriptValue booleanValue);
public static extern JavaScriptErrorCode JsBoolToBoolean(bool value, out JavaScriptValue booleanValue);