/// <summary>
        ///     Converts the value to <c>String</c> using regular JavaScript semantics.
        /// </summary>
        /// <remarks>
        ///     Requires an active script context.
        /// </remarks>
        /// <returns>The converted value.</returns>
        public JavaScriptValue ConvertToString()
        {
            JavaScriptValue stringReference;

            Native.ThrowIfError(Native.JsConvertValueToString(this, out stringReference));
            return(stringReference);
        }
Exemple #2
0
        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));
        }
Exemple #3
0
        public void PrintJavaScriptError()
        {
            Native.JsGetProperty(this, JavaScriptPropertyId.FromString("stack"), out var stackTrace);

            if (stackTrace.IsValid)
            {
                Native.JsConvertValueToString(stackTrace, out var stackStringValue);
                Native.JsStringToPointer(stackStringValue, out var stackResultPtr, out _);
                var resultStackTrace = Marshal.PtrToStringUni(stackResultPtr);

                Debug.LogError(resultStackTrace);
            }
            else
            {
                Native.JsConvertValueToString(this, out var stringValue);
                Native.JsStringToPointer(stringValue, out var resultPtr, out _);
                var resultString = Marshal.PtrToStringUni(resultPtr);

                Debug.LogError(resultString);
            }
        }