Пример #1
0
        private bool TryGetErrorProperty <T>(JavaScriptValueSafeHandle error, string propertyName, out T value)
        {
            JsErrorCode innerError;

            innerError = LibChakraCore.JsCreatePropertyId(propertyName, (ulong)propertyName.Length, out JavaScriptPropertyIdSafeHandle propertyHandle);
            innerError = LibChakraCore.JsHasProperty(error, propertyHandle, out bool hasProperty);

            if (hasProperty == true)
            {
                innerError = LibChakraCore.JsGetProperty(error, propertyHandle, out JavaScriptValueSafeHandle propertyValue);
                innerError = LibChakraCore.JsGetValueType(propertyValue, out JsValueType propertyType);
                switch (propertyType)
                {
                case JsValueType.Number:
                    innerError = LibChakraCore.JsNumberToDouble(propertyValue, out double doubleValue);
                    value      = (T)Convert.ChangeType(doubleValue, typeof(T));
                    return(true);

                case JsValueType.String:
                    var strValue = Helpers.GetStringUtf8(propertyValue, releaseHandle: true);
                    value = (T)Convert.ChangeType(strValue, typeof(T));
                    return(true);

                default:
                    value = default(T);
                    return(false);
                }
            }

            value = default(T);
            return(false);
        }
Пример #2
0
        public static string GetStringUtf8(JavaScriptValueSafeHandle stringHandle, bool releaseHandle = false)
        {
            bool stringHandleWasCreated = false;

            if (stringHandle == null || stringHandle == JavaScriptValueSafeHandle.Invalid)
            {
                throw new ArgumentNullException(nameof(stringHandle));
            }

            //Don't use our helper error class in order to prevent recursive errors.
            JsErrorCode innerError;

            //Get the size
            innerError = LibChakraCore.JsCopyString(stringHandle, null, 0, out ulong size);

            if ((int)size > int.MaxValue)
            {
                throw new OutOfMemoryException("Exceeded maximum string length.");
            }

            byte[] result = new byte[(int)size];
            innerError = LibChakraCore.JsCopyString(stringHandle, result, (ulong)result.Length, out ulong written);

            var strResult = Encoding.UTF8.GetString(result, 0, result.Length);

            if (stringHandleWasCreated || releaseHandle)
            {
                stringHandle.Dispose();
            }

            return(strResult);
        }
Пример #3
0
 public JavaScriptValueSafeHandle JsRunSerializedScript(string script, byte[] buffer, JavaScriptSourceContext sourceContext, string sourceUrl)
 {
     Errors.ThrowIfError(LibChakraCore.JsRunSerializedScript(script, buffer, sourceContext, sourceUrl, out JavaScriptValueSafeHandle result));
     result.NativeFunctionSource = nameof(LibChakraCore.JsRunSerializedScript);
     if (result != JavaScriptValueSafeHandle.Invalid)
     {
         Errors.ThrowIfError(LibChakraCore.JsAddRef(result, out uint valueRefCount));
     }
     return(result);
 }
Пример #4
0
 public JavaScriptValueSafeHandle JsParseSerializedScriptWithCallback(JavaScriptSerializedScriptLoadSourceCallback scriptLoadCallback, JavaScriptSerializedScriptUnloadCallback scriptUnloadCallback, byte[] buffer, JavaScriptSourceContext sourceContext, string sourceUrl)
 {
     Errors.ThrowIfError(LibChakraCore.JsParseSerializedScriptWithCallback(scriptLoadCallback, scriptUnloadCallback, buffer, sourceContext, sourceUrl, out JavaScriptValueSafeHandle result));
     result.NativeFunctionSource = nameof(LibChakraCore.JsParseSerializedScriptWithCallback);
     if (result != JavaScriptValueSafeHandle.Invalid)
     {
         Errors.ThrowIfError(LibChakraCore.JsAddRef(result, out uint valueRefCount));
     }
     return(result);
 }
Пример #5
0
 public JavaScriptValueSafeHandle JsExperimentalApiRunModule(string script, JavaScriptSourceContext sourceContext, string sourceUrl)
 {
     Errors.ThrowIfError(LibChakraCore.JsExperimentalApiRunModule(script, sourceContext, sourceUrl, out JavaScriptValueSafeHandle result));
     result.NativeFunctionSource = nameof(LibChakraCore.JsExperimentalApiRunModule);
     if (result != JavaScriptValueSafeHandle.Invalid)
     {
         Errors.ThrowIfError(LibChakraCore.JsAddRef(result, out uint valueRefCount));
     }
     return(result);
 }
Пример #6
0
 public JavaScriptValueSafeHandle JsParseScriptWithAttributes(string script, JavaScriptSourceContext sourceContext, string sourceUrl, JavaScriptParseScriptAttributes parseAttributes)
 {
     Errors.ThrowIfError(LibChakraCore.JsParseScriptWithAttributes(script, sourceContext, sourceUrl, parseAttributes, out JavaScriptValueSafeHandle result));
     result.NativeFunctionSource = nameof(LibChakraCore.JsParseScriptWithAttributes);
     if (result != JavaScriptValueSafeHandle.Invalid)
     {
         Errors.ThrowIfError(LibChakraCore.JsAddRef(result, out uint valueRefCount));
     }
     return(result);
 }
Пример #7
0
 public JavaScriptValueSafeHandle JsPointerToString(string stringValue, ulong stringLength)
 {
     Errors.ThrowIfError(LibChakraCore.JsPointerToString(stringValue, stringLength, out JavaScriptValueSafeHandle value));
     value.NativeFunctionSource = nameof(LibChakraCore.JsPointerToString);
     if (value != JavaScriptValueSafeHandle.Invalid)
     {
         Errors.ThrowIfError(LibChakraCore.JsAddRef(value, out uint valueRefCount));
     }
     return(value);
 }
Пример #8
0
 public JavaScriptPropertyIdSafeHandle JsGetPropertyIdFromName(string name)
 {
     Errors.ThrowIfError(LibChakraCore.JsGetPropertyIdFromName(name, out JavaScriptPropertyIdSafeHandle propertyId));
     propertyId.NativeFunctionSource = nameof(LibChakraCore.JsGetPropertyIdFromName);
     if (propertyId != JavaScriptPropertyIdSafeHandle.Invalid)
     {
         Errors.ThrowIfError(LibChakraCore.JsAddRef(propertyId, out uint valueRefCount));
     }
     return(propertyId);
 }
Пример #9
0
        protected override void Dispose(bool disposing)
        {
            if (disposing && !IsClosed)
            {
                var error = LibChakraCore.JsRelease(this, out uint count);

                //This has no effect.
                SetHandleAsInvalid();
            }

            base.Dispose(disposing);
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing && !IsClosed)
            {
                //Ensure that a context is not active, otherwise the runtime will throw a "Runtime In Use" exception.
                LibChakraCore.JsSetCurrentContext(JavaScriptContextSafeHandle.Invalid);
                LibChakraCore.JsDisposeRuntime(handle);
            }

            //Do not call the base implementation as we have no references to free.
            //base.Dispose(disposing);
        }
Пример #11
0
        public static void ThrowIfError(JsErrorCode error)
        {
            if (error != JsErrorCode.NoError)
            {
                switch (error)
                {
                case JsErrorCode.InvalidArgument:
                    throw new JsUsageException(error, "Invalid argument.");

                case JsErrorCode.NullArgument:
                    throw new JsUsageException(error, "Null argument.");

                case JsErrorCode.NoCurrentContext:
                    throw new JsUsageException(error, "No current context.");

                case JsErrorCode.InExceptionState:
                    throw new JsUsageException(error, "Runtime is in exception state.");

                case JsErrorCode.NotImplemented:
                    throw new JsUsageException(error, "Method is not implemented.");

                case JsErrorCode.WrongThread:
                    throw new JsUsageException(error, "Runtime is active on another thread.");

                case JsErrorCode.RuntimeInUse:
                    throw new JsUsageException(error, "Runtime is in use.");

                case JsErrorCode.BadSerializedScript:
                    throw new JsUsageException(error, "Bad serialized script.");

                case JsErrorCode.InDisabledState:
                    throw new JsUsageException(error, "Runtime is disabled.");

                case JsErrorCode.CannotDisableExecution:
                    throw new JsUsageException(error, "Cannot disable execution.");

                case JsErrorCode.AlreadyDebuggingContext:
                    throw new JsUsageException(error, "Context is already in debug mode.");

                case JsErrorCode.HeapEnumInProgress:
                    throw new JsUsageException(error, "Heap enumeration is in progress.");

                case JsErrorCode.ArgumentNotObject:
                    throw new JsUsageException(error, "Argument is not an object.");

                case JsErrorCode.InProfileCallback:
                    throw new JsUsageException(error, "In a profile callback.");

                case JsErrorCode.InThreadServiceCallback:
                    throw new JsUsageException(error, "In a thread service callback.");

                case JsErrorCode.CannotSerializeDebugScript:
                    throw new JsUsageException(error, "Cannot serialize a debug script.");

                case JsErrorCode.AlreadyProfilingContext:
                    throw new JsUsageException(error, "Already profiling this context.");

                case JsErrorCode.IdleNotEnabled:
                    throw new JsUsageException(error, "Idle is not enabled.");

                case JsErrorCode.OutOfMemory:
                    throw new JsEngineException(error, "Out of memory.");

                case JsErrorCode.ScriptException:
                {
                    JsErrorCode innerError = LibChakraCore.JsGetAndClearException(out JavaScriptValueSafeHandle errorObject);

                    //The following throws a really bad error, but if you set and get the error again, it works, but with no actual metadata :-/
                    //JsErrorCode innerError = LibChakraCore.JsGetAndClearExceptionWithMetadata(out JavaScriptValueSafeHandle metadataObject);
                    //innerError = LibChakraCore.JsGetPropertyIdFromName("exception", out JavaScriptPropertyIdSafeHandle propertyId);
                    //innerError = LibChakraCore.JsGetProperty(metadataObject, propertyId, out JavaScriptValueSafeHandle errorObject);

                    if (innerError != JsErrorCode.NoError)
                    {
                        throw new JsFatalException(innerError);
                    }

                    innerError = LibChakraCore.JsSetException(errorObject);
                    if (innerError != JsErrorCode.NoError)
                    {
                        throw new JsFatalException(innerError);
                    }

                    throw new JsScriptException(error, errorObject, "Script threw an exception.");
                }

                case JsErrorCode.ScriptCompile:
                {
                    JsErrorCode innerError;
                    innerError = LibChakraCore.JsHasException(out bool hasException);
                    //We attempted to clear the exception, but the result of that action was an exception.
                    if (innerError != JsErrorCode.NoError)
                    {
                        throw new JsFatalException(innerError);
                    }

                    // Only throw an exception if the runtime is currently in an exception state
                    // Parse errors are inheritly continuable.
                    if (hasException)
                    {
                        innerError = LibChakraCore.JsGetAndClearException(out JavaScriptValueSafeHandle errorObject);

                        //We attempted to clear the exception, but the result of that action was an exception.
                        if (innerError != JsErrorCode.NoError)
                        {
                            throw new JsFatalException(innerError);
                        }

                        innerError = LibChakraCore.JsSetException(errorObject);
                        if (innerError != JsErrorCode.NoError)
                        {
                            throw new JsFatalException(innerError);
                        }

                        throw new JsScriptException(error, errorObject, "Compile error.");
                    }
                }
                break;

                case JsErrorCode.ScriptTerminated:
                    throw new JsScriptException(error, JavaScriptValueSafeHandle.Invalid, "Script was terminated.");

                case JsErrorCode.ScriptEvalDisabled:
                    throw new JsScriptException(error, JavaScriptValueSafeHandle.Invalid, "Eval of strings is disabled in this runtime.");

                case JsErrorCode.Fatal:
                    throw new JsFatalException(error);

                default:
                    throw new JsFatalException(error);
                }
            }
        }
Пример #12
0
        /// <summary>
        ///     Initializes a new instance of the <see cref="JsScriptException"/> class.
        /// </summary>
        /// <param name="code">The error code returned.</param>
        /// <param name="error">The JavaScript error object.</param>
        /// <param name="message">The error message.</param>
        public JsScriptException(JsErrorCode code, JavaScriptValueSafeHandle error, string message) :
            base(code, message)
        {
            m_error   = error;
            m_message = message;

            //Don't use our helper errors class in order to prevent recursive errors.
            JsErrorCode innerError;

            //Get the error object type
            innerError = LibChakraCore.JsGetValueType(error, out JsValueType errorType);

            switch (errorType)
            {
            case JsValueType.Object:
                if (TryGetErrorProperty(error, ColumnNumberPropertyName, out int metadataColumnValue))
                {
                    ColumnNumber = metadataColumnValue;
                }

                if (TryGetErrorProperty(error, LineNumberPropertyName, out int metadataLineValue))
                {
                    LineNumber = metadataLineValue;
                }
                if (TryGetErrorProperty(error, "source", out string metadataScriptSourceValue))
                {
                    ScriptSource = metadataScriptSourceValue;
                }
                if (TryGetErrorProperty(error, "url", out string metadataUrlValue))
                {
                    Name = metadataUrlValue;
                }
                break;

            case JsValueType.Error:
                //Get the message of the Script Error.
                if (TryGetErrorProperty(error, MessagePropertyName, out string errrorMessageValue))
                {
                    m_message = errrorMessageValue;
                }

                if (TryGetErrorProperty(error, ColumnNumberPropertyName, out int errorColumnValue))
                {
                    ColumnNumber = errorColumnValue;
                }

                if (TryGetErrorProperty(error, LineNumberPropertyName, out int errorLineValue))
                {
                    LineNumber = errorLineValue;
                }

                if (TryGetErrorProperty(error, NamePropertyName, out string errorNameValue))
                {
                    Name = errorNameValue;
                }

                break;

            case JsValueType.String:
                m_message = Helpers.GetStringUtf8(error);
                break;
            }
        }
Пример #13
0
 public void JsSerializeScript(string script, byte[] buffer, ref uint bufferSize)
 {
     Errors.ThrowIfError(LibChakraCore.JsSerializeScript(script, buffer, ref bufferSize));
 }
Пример #14
0
 public IntPtr JsStringToPointer(JavaScriptValueSafeHandle value, out ulong stringLength)
 {
     Errors.ThrowIfError(LibChakraCore.JsStringToPointer(value, out IntPtr stringValue, out stringLength));
     return(stringValue);
 }
Пример #15
0
 public IntPtr JsGetPropertyNameFromId(JavaScriptPropertyIdSafeHandle propertyId)
 {
     Errors.ThrowIfError(LibChakraCore.JsGetPropertyNameFromId(propertyId, out IntPtr name));
     return(name);
 }