Exemplo n.º 1
0
        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
        }
Exemplo n.º 2
0
 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);
 }
Exemplo n.º 3
0
        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
        }
Exemplo n.º 4
0
        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;
        }
Exemplo n.º 5
0
        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;
        }
Exemplo n.º 6
0
        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;
        }
Exemplo n.º 7
0
 public static extern JavaScriptErrorCode JsCreateNamedFunction(JavaScriptValue name, JavaScriptNativeFunction nativeFunction, IntPtr callbackState, out JavaScriptValue function);
Exemplo n.º 8
0
 public static extern JavaScriptErrorCode JsGetAndClearException(out JavaScriptValue exception);
Exemplo n.º 9
0
 public static extern JavaScriptErrorCode JsCreateFunction(JavaScriptNativeFunction nativeFunction, IntPtr externalData, out JavaScriptValue function);
Exemplo n.º 10
0
 internal static extern JavaScriptErrorCode JsGetNullValue(out JavaScriptValue nullValue);
Exemplo n.º 11
0
 public static extern JavaScriptErrorCode JsConstructObject(JavaScriptValue function, JavaScriptValue[] arguments, ushort argumentCount, out JavaScriptValue result);
Exemplo n.º 12
0
 internal static extern JavaScriptErrorCode JsNumberToInt(JavaScriptValue value, out int intValue);
Exemplo n.º 13
0
 internal static extern JavaScriptErrorCode JsRelease(JavaScriptValue reference, out uint count);
Exemplo n.º 14
0
 public static extern JavaScriptErrorCode JsGetTypedArrayStorage(JavaScriptValue typedArray, out byte[] buffer, out uint bufferLength, out JavaScriptTypedArrayType arrayType, out int elementSize);
Exemplo n.º 15
0
 public static extern JavaScriptErrorCode JsGetDataViewStorage(JavaScriptValue dataView, out byte[] buffer, out uint bufferLength);
Exemplo n.º 16
0
 public static extern JavaScriptErrorCode JsCreateDataView(JavaScriptValue arrayBuffer, uint byteOffset, uint byteOffsetLength, out JavaScriptValue result);
Exemplo n.º 17
0
 public static extern JavaScriptErrorCode JsGetArrayBufferStorage(JavaScriptValue arrayBuffer, out byte[] buffer, out uint bufferLength);
Exemplo n.º 18
0
 public static extern JavaScriptErrorCode JsCreateTypedArray(JavaScriptTypedArrayType arrayType, JavaScriptValue arrayBuffer, uint byteOffset,
                                                             uint elementLength, out JavaScriptValue result);
Exemplo n.º 19
0
 public static extern JavaScriptErrorCode JsCreateArrayBuffer(uint byteLength, out JavaScriptValue result);
Exemplo n.º 20
0
 public static extern JavaScriptErrorCode JsInspectableToObject([MarshalAs(UnmanagedType.IInspectable)] System.Object inspectable, out JavaScriptValue value);
Exemplo n.º 21
0
 internal static extern JavaScriptErrorCode JsCreateSymbol(JavaScriptValue description, out JavaScriptValue symbol);
Exemplo n.º 22
0
 public static extern JavaScriptErrorCode JsCreateSymbol(JavaScriptValue description, out JavaScriptValue symbol);
Exemplo n.º 23
0
 internal static extern JavaScriptErrorCode JsGetPropertyIdFromSymbol(JavaScriptValue symbol, out JavaScriptPropertyId propertyId);
Exemplo n.º 24
0
 public static extern JavaScriptErrorCode JsGetSymbolFromPropertyId(JavaScriptPropertyId propertyId, out JavaScriptValue symbol);
Exemplo n.º 25
0
 internal static extern JavaScriptErrorCode JsHasIndexedPropertiesExternalData(JavaScriptValue obj, out bool value);
Exemplo n.º 26
0
 public static extern JavaScriptErrorCode JsGetPropertyIdFromSymbol(JavaScriptValue symbol, out JavaScriptPropertyId propertyId);
Exemplo n.º 27
0
 internal static extern JavaScriptErrorCode JsRunSerializedScript(string script, byte[] buffer, JavaScriptSourceContext sourceContext, string sourceUrl, out JavaScriptValue result);
Exemplo n.º 28
0
 internal static extern JavaScriptErrorCode JsCreateNamedFunction(JavaScriptValue name, JavaScriptNativeFunction nativeFunction, IntPtr callbackState, out JavaScriptValue function);
Exemplo n.º 29
0
 internal static extern JavaScriptErrorCode JsGetFalseValue(out JavaScriptValue falseValue);
Exemplo n.º 30
0
 internal static extern JavaScriptErrorCode JsCreateArrayBuffer(uint byteLength, out JavaScriptValue result);
Exemplo n.º 31
0
 public static extern JavaScriptErrorCode JsSetException(JavaScriptValue exception);
Exemplo n.º 32
0
 internal static extern JavaScriptErrorCode JsCreateDataView(JavaScriptValue arrayBuffer, uint byteOffset, uint byteOffsetLength, out JavaScriptValue result);
Exemplo n.º 33
0
 public static extern JavaScriptErrorCode JsCreateURIError(JavaScriptValue message, out JavaScriptValue error);
Exemplo n.º 34
0
 internal static extern JavaScriptErrorCode JsGetTypedArrayStorage(JavaScriptValue typedArray, out byte[] buffer, out uint bufferLength, out JavaScriptTypedArrayType arrayType, out int elementSize);
Exemplo n.º 35
0
 public static extern JavaScriptErrorCode JsGetOwnPropertySymbols(JavaScriptValue obj, out JavaScriptValue propertySymbols);
Exemplo n.º 36
0
 public static extern JavaScriptErrorCode JsSetObjectBeforeCollectCallback(JavaScriptValue reference, IntPtr callbackState, JavaScriptObjectBeforeCollectCallback beforeCollectCallback);
Exemplo n.º 37
0
 internal static extern JavaScriptErrorCode JsInspectableToObject([MarshalAs(UnmanagedType.IInspectable)] System.Object inspectable, out JavaScriptValue value);
Exemplo n.º 38
0
 public static extern JavaScriptErrorCode JsNumberToInt(JavaScriptValue value, out int intValue);
Exemplo n.º 39
0
 internal static extern JavaScriptErrorCode JsCreateTypedArray(JavaScriptTypedArrayType arrayType, JavaScriptValue arrayBuffer, uint byteOffset,
     uint elementLength, out JavaScriptValue result);
Exemplo n.º 40
0
 public static extern JavaScriptErrorCode JsGetIndexedPropertiesExternalData(JavaScriptValue obj, IntPtr data, out JavaScriptTypedArrayType arrayType, out uint elementLength);
Exemplo n.º 41
0
 internal static extern JavaScriptErrorCode JsGetArrayBufferStorage(JavaScriptValue arrayBuffer, out byte[] buffer, out uint bufferLength);
Exemplo n.º 42
0
 public static extern JavaScriptErrorCode JsHasIndexedPropertiesExternalData(JavaScriptValue obj, out bool value);
Exemplo n.º 43
0
 internal static extern JavaScriptErrorCode JsGetDataViewStorage(JavaScriptValue dataView, out byte[] buffer, out uint bufferLength);
Exemplo n.º 44
0
 public static extern JavaScriptErrorCode JsAddRef(JavaScriptValue reference, out uint count);
Exemplo n.º 45
0
 internal static extern JavaScriptErrorCode JsGetSymbolFromPropertyId(JavaScriptPropertyId propertyId, out JavaScriptValue symbol);
Exemplo n.º 46
0
 public static extern JavaScriptErrorCode JsRelease(JavaScriptValue reference, out uint count);
Exemplo n.º 47
0
 internal static extern JavaScriptErrorCode JsGetOwnPropertySymbols(JavaScriptValue obj, out JavaScriptValue propertySymbols);
Exemplo n.º 48
0
 public static extern JavaScriptErrorCode JsRunScript(string script, JavaScriptSourceContext sourceContext, string sourceUrl, out JavaScriptValue result);
Exemplo n.º 49
0
 internal static extern JavaScriptErrorCode JsGetIndexedPropertiesExternalData(JavaScriptValue obj, IntPtr data, out JavaScriptTypedArrayType arrayType, out uint elementLength);
Exemplo n.º 50
0
 public static extern JavaScriptErrorCode JsRunSerializedScript(string script, byte[] buffer, JavaScriptSourceContext sourceContext, string sourceUrl, out JavaScriptValue result);
Exemplo n.º 51
0
 internal static extern JavaScriptErrorCode JsAddRef(JavaScriptValue reference, out uint count);
Exemplo n.º 52
0
 public static extern JavaScriptErrorCode JsGetUndefinedValue(out JavaScriptValue undefinedValue);
Exemplo n.º 53
0
 internal static extern JavaScriptErrorCode JsRunScript(string script, JavaScriptSourceContext sourceContext, string sourceUrl, out JavaScriptValue result);
Exemplo n.º 54
0
 public static extern JavaScriptErrorCode JsGetNullValue(out JavaScriptValue nullValue);
Exemplo n.º 55
0
 internal static extern JavaScriptErrorCode JsGetUndefinedValue(out JavaScriptValue undefinedValue);
Exemplo n.º 56
0
 public static extern JavaScriptErrorCode JsGetTrueValue(out JavaScriptValue trueValue);
Exemplo n.º 57
0
 internal static extern JavaScriptErrorCode JsGetTrueValue(out JavaScriptValue trueValue);
Exemplo n.º 58
0
 public static extern JavaScriptErrorCode JsGetFalseValue(out JavaScriptValue falseValue);
Exemplo n.º 59
0
 internal static extern JavaScriptErrorCode JsBoolToBoolean(bool value, out JavaScriptValue booleanValue);
Exemplo n.º 60
0
 public static extern JavaScriptErrorCode JsBoolToBoolean(bool value, out JavaScriptValue booleanValue);