Exemplo n.º 1
0
        private static JavaScriptValue RunScript(JavaScriptValue callee, bool isConstructCall, JavaScriptValue[] arguments, ushort argumentCount, IntPtr callbackData)
        {
            if (argumentCount < 2)
            {
                ThrowException("not enough arguments");
                return JavaScriptValue.Invalid;
            }

            //
            // Convert filename.
            //

            string filename = arguments[1].ToString();

            //
            // Load the script from the disk.
            //

            string script = File.ReadAllText(filename);
            if (string.IsNullOrEmpty(script))
            {
                ThrowException("invalid script");
                return JavaScriptValue.Invalid;
            }

            //
            // Run the script.
            //

            return JavaScriptContext.RunScript(script, currentSourceContext++, filename);
        }
Exemplo n.º 2
0
        public string init()
        {
            JavaScriptContext context;

            if (Native.JsCreateRuntime(JavaScriptRuntimeAttributes.None, null, out runtime) != JavaScriptErrorCode.NoError)
                return "failed to create runtime.";

            if (Native.JsCreateContext(runtime, out context) != JavaScriptErrorCode.NoError)
                return "failed to create execution context.";

            if (Native.JsSetCurrentContext(context) != JavaScriptErrorCode.NoError)
                return "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)
                return "failed to setup callback for ES6 Promise";

            if (Native.JsProjectWinRTNamespace("Windows") != JavaScriptErrorCode.NoError)
                return "failed to project windows namespace.";

            if (Native.JsStartDebugging() != JavaScriptErrorCode.NoError)
                return "failed to start debugging.";

            return "NoError";
        }
Exemplo n.º 3
0
        private static JavaScriptValue Echo(JavaScriptValue callee, bool isConstructCall, JavaScriptValue[] arguments, ushort argumentCount, IntPtr callbackData)
        {
            for (uint index = 1; index < argumentCount; index++)
            {
                if (index > 1)
                {
                    Console.Write(" ");
                }

                Console.Write(arguments[index].ConvertToString().ToString());
            }

            Console.WriteLine();

            return JavaScriptValue.Invalid;
        }
Exemplo n.º 4
0
        private static void DefineHostCallback(JavaScriptValue globalObject, string callbackName, JavaScriptNativeFunction callback, IntPtr callbackData)
        {
            //
            // Get property ID.
            //

            JavaScriptPropertyId propertyId = JavaScriptPropertyId.FromString(callbackName);

            //
            // Create a function
            //

            JavaScriptValue function = JavaScriptValue.CreateFunction(callback, callbackData);

            //
            // Set the property
            //

            globalObject.SetProperty(propertyId, function, true);
        }
Exemplo n.º 5
0
 internal static extern JavaScriptErrorCode JsGetIndexedPropertiesExternalData(JavaScriptValue obj, IntPtr data, out JavaScriptTypedArrayType arrayType, out uint elementLength);
Exemplo n.º 6
0
        /// <summary>
        ///     Executes a script.
        /// </summary>
        /// <remarks>
        ///     Requires an active script context.
        /// </remarks>
        /// <param name="script">The script to run.</param>
        /// <param name="sourceContext">
        ///     A cookie identifying the script that can be used by script contexts that have debugging enabled.
        /// </param>
        /// <param name="sourceName">The location the script came from.</param>
        /// <returns>The result of the script, if any.</returns>
        public static JavaScriptValue RunScript(string script, JavaScriptSourceContext sourceContext, string sourceName)
        {
            JavaScriptValue result;

            Native.ThrowIfError(Native.JsRun(JavaScriptValue.FromString(script), sourceContext, JavaScriptValue.FromString(sourceName), JavaScriptParseScriptAttributes.JsParseScriptAttributeNone, out result));
            return(result);
        }
Exemplo n.º 7
0
 internal static extern JavaScriptErrorCode JsGetArrayBufferStorage(JavaScriptValue arrayBuffer, out byte[] buffer, out uint bufferLength);
Exemplo n.º 8
0
 internal static extern JavaScriptErrorCode JsCreateTypedArray(JavaScriptTypedArrayType arrayType, JavaScriptValue arrayBuffer, uint byteOffset,
     uint elementLength, out JavaScriptValue result);
Exemplo n.º 9
0
 internal static extern JavaScriptErrorCode JsInspectableToObject([MarshalAs(UnmanagedType.IInspectable)] System.Object inspectable, out JavaScriptValue value);
Exemplo n.º 10
0
 internal static extern JavaScriptErrorCode JsSetObjectBeforeCollectCallback(JavaScriptValue reference, IntPtr callbackState, JavaScriptObjectBeforeCollectCallback beforeCollectCallback);
Exemplo n.º 11
0
 /// <summary>
 ///     Set the value at the specified index of an object.
 /// </summary>
 /// <remarks>
 ///     Requires an active script context.
 /// </remarks>
 /// <param name="index">The index to set.</param>
 /// <param name="value">The value to set.</param>
 public void SetIndexedProperty(JavaScriptValue index, JavaScriptValue value)
 {
     Native.ThrowIfError(Native.JsSetIndexedProperty(this, index, value));
 }
Exemplo n.º 12
0
 internal static extern JavaScriptErrorCode JsGetUndefinedValue(out JavaScriptValue undefinedValue);
Exemplo n.º 13
0
 internal static extern JavaScriptErrorCode JsRunSerializedScript(string script, byte[] buffer, JavaScriptSourceContext sourceContext, string sourceUrl, out JavaScriptValue result);
Exemplo n.º 14
0
 internal static extern JavaScriptErrorCode JsRunScript(string script, JavaScriptSourceContext sourceContext, string sourceUrl, out JavaScriptValue result);
Exemplo n.º 15
0
 internal static extern JavaScriptErrorCode JsRelease(JavaScriptValue reference, out uint count);
Exemplo n.º 16
0
 internal static extern JavaScriptErrorCode JsAddRef(JavaScriptValue reference, out uint count);
Exemplo n.º 17
0
 /// <summary>
 ///     Sets the runtime of the current context to an exception state.
 /// </summary>
 /// <remarks>
 ///     <para>
 ///     If the runtime of the current context is already in an exception state, this API will
 ///     throw <c>JsErrorInExceptionState</c>.
 ///     </para>
 ///     <para>
 ///     Requires an active script context.
 ///     </para>
 /// </remarks>
 /// <param name="exception">
 ///     The JavaScript exception to set for the runtime of the current context.
 /// </param>
 public static void SetException(JavaScriptValue exception)
 {
     Native.ThrowIfError(Native.JsSetException(exception));
 }
Exemplo n.º 18
0
 internal static extern JavaScriptErrorCode JsInstanceOf(JavaScriptValue obj, JavaScriptValue constructor, out bool result);
Exemplo n.º 19
0
 internal static extern JavaScriptErrorCode JsGetNullValue(out JavaScriptValue nullValue);
Exemplo n.º 20
0
 internal static extern JavaScriptErrorCode JsGetContextOfObject(JavaScriptValue obj, out JavaScriptContext context);
Exemplo n.º 21
0
 internal static extern JavaScriptErrorCode JsGetTrueValue(out JavaScriptValue trueValue);
Exemplo n.º 22
0
 internal static extern JavaScriptErrorCode JsSetException(JavaScriptValue exception);
Exemplo n.º 23
0
 /// <summary>
 ///     Delete the value at the specified index of an object.
 /// </summary>
 /// <remarks>
 ///     Requires an active script context.
 /// </remarks>
 /// <param name="index">The index to delete.</param>
 public void DeleteIndexedProperty(JavaScriptValue index)
 {
     Native.ThrowIfError(Native.JsDeleteIndexedProperty(this, index));
 }
Exemplo n.º 24
0
 internal static extern JavaScriptErrorCode JsCreateNamedFunction(JavaScriptValue name, JavaScriptNativeFunction nativeFunction, IntPtr callbackState, out JavaScriptValue function);
Exemplo n.º 25
0
 internal static extern JavaScriptErrorCode JsGetDataViewStorage(JavaScriptValue dataView, out byte[] buffer, out uint bufferLength);
Exemplo n.º 26
0
 internal static extern JavaScriptErrorCode JsCreateArrayBuffer(uint byteLength, out JavaScriptValue result);
Exemplo n.º 27
0
 internal static extern JavaScriptErrorCode JsGetSymbolFromPropertyId(JavaScriptPropertyId propertyId, out JavaScriptValue symbol);
Exemplo n.º 28
0
 internal static extern JavaScriptErrorCode JsCreateDataView(JavaScriptValue arrayBuffer, uint byteOffset, uint byteOffsetLength, out JavaScriptValue result);
Exemplo n.º 29
0
 internal static extern JavaScriptErrorCode JsGetOwnPropertySymbols(JavaScriptValue obj, out JavaScriptValue propertySymbols);
Exemplo n.º 30
0
 internal static extern JavaScriptErrorCode JsGetTypedArrayStorage(JavaScriptValue typedArray, out byte[] buffer, out uint bufferLength, out JavaScriptTypedArrayType arrayType, out int elementSize);
        public static void SetHostUrl(JavaScriptModuleRecord module, string url)
        {
            var value = JavaScriptValue.FromString(url);

            Native.ThrowIfError(Native.JsSetModuleHostInfo(module, JavascriptModuleHostInfoKind.JsModuleHostInfo_Url, value));
        }
Exemplo n.º 32
0
 internal static extern JavaScriptErrorCode JsCreateSymbol(JavaScriptValue description, out JavaScriptValue symbol);
Exemplo n.º 33
0
 internal static extern JavaScriptErrorCode JsCallFunction(JavaScriptValue function, JavaScriptValue[] arguments, ushort argumentCount, out JavaScriptValue result);
Exemplo n.º 34
0
 internal static extern JavaScriptErrorCode JsGetPropertyIdFromSymbol(JavaScriptValue symbol, out JavaScriptPropertyId propertyId);
Exemplo n.º 35
0
 internal static extern JavaScriptErrorCode JsConstructObject(JavaScriptValue function, JavaScriptValue[] arguments, ushort argumentCount, out JavaScriptValue result);
Exemplo n.º 36
0
 internal static extern JavaScriptErrorCode JsNumberToInt(JavaScriptValue value, out int intValue);
Exemplo n.º 37
0
 internal static extern JavaScriptErrorCode JsCreateFunction(JavaScriptNativeFunction nativeFunction, IntPtr externalData, out JavaScriptValue function);
Exemplo n.º 38
0
 internal static extern JavaScriptErrorCode JsHasIndexedPropertiesExternalData(JavaScriptValue obj, out bool value);
Exemplo n.º 39
0
 internal static extern JavaScriptErrorCode JsCreateURIError(JavaScriptValue message, out JavaScriptValue error);
Exemplo n.º 40
0
 internal static extern JavaScriptErrorCode JsGetTypedArrayInfo(JavaScriptValue typedArray, out JavaScriptTypedArrayType arrayType, out JavaScriptValue arrayBuffer, out uint byteOffset, out uint byteLength);
Exemplo n.º 41
0
 internal static extern JavaScriptErrorCode JsGetAndClearException(out JavaScriptValue exception);
Exemplo n.º 42
0
 internal static extern JavaScriptErrorCode JsRunSerializedScriptWithCallback(JavaScriptSerializedScriptLoadSourceCallback scriptLoadCallback,
     JavaScriptSerializedScriptUnloadCallback scriptUnloadCallback, byte[] buffer, JavaScriptSourceContext sourceContext, string sourceUrl, out JavaScriptValue result);
Exemplo n.º 43
0
 /// <summary>
 ///     Sets an object's property.
 /// </summary>
 /// <remarks>
 ///     Requires an active script context.
 /// </remarks>
 /// <param name="id">The ID of the property.</param>
 /// <param name="value">The new value of the property.</param>
 /// <param name="useStrictRules">The property set should follow strict mode rules.</param>
 public void SetProperty(JavaScriptPropertyId id, JavaScriptValue value, bool useStrictRules)
 {
     Native.ThrowIfError(Native.JsSetProperty(this, id, value, useStrictRules));
 }