/// <summary>
        /// Gets a object's property
        /// </summary>
        /// <remarks>
        /// Requires an active script context.
        /// </remarks>
        /// <param name="id">The ID of the property</param>
        /// <returns>The value of the property</returns>
        public EdgeJsValue GetProperty(EdgeJsPropertyId id)
        {
            EdgeJsValue propertyReference;

            EdgeJsErrorHelpers.ThrowIfError(EdgeNativeMethods.JsGetProperty(this, id, out propertyReference));

            return(propertyReference);
        }
        /// <summary>
        /// Deletes a object's property
        /// </summary>
        /// <remarks>
        /// Requires an active script context.
        /// </remarks>
        /// <param name="propertyId">The ID of the property</param>
        /// <param name="useStrictRules">The property set should follow strict mode rules</param>
        /// <returns>Whether the property was deleted</returns>
        public EdgeJsValue DeleteProperty(EdgeJsPropertyId propertyId, bool useStrictRules)
        {
            EdgeJsValue returnReference;

            EdgeJsErrorHelpers.ThrowIfError(EdgeNativeMethods.JsDeleteProperty(this, propertyId, useStrictRules, out returnReference));

            return(returnReference);
        }
        /// <summary>
        /// Defines a new object's own property from a property descriptor
        /// </summary>
        /// <remarks>
        /// Requires an active script context.
        /// </remarks>
        /// <param name="propertyId">The ID of the property</param>
        /// <param name="propertyDescriptor">The property descriptor</param>
        /// <returns>Whether the property was defined</returns>
        public bool DefineProperty(EdgeJsPropertyId propertyId, EdgeJsValue propertyDescriptor)
        {
            bool result;

            EdgeJsErrorHelpers.ThrowIfError(EdgeNativeMethods.JsDefineProperty(this, propertyId, propertyDescriptor, out result));

            return(result);
        }
        /// <summary>
        /// Gets a property descriptor for an object's own property
        /// </summary>
        /// <remarks>
        /// Requires an active script context.
        /// </remarks>
        /// <param name="propertyId">The ID of the property</param>
        /// <returns>The property descriptor</returns>
        public EdgeJsValue GetOwnPropertyDescriptor(EdgeJsPropertyId propertyId)
        {
            EdgeJsValue descriptorReference;

            EdgeJsErrorHelpers.ThrowIfError(EdgeNativeMethods.JsGetOwnPropertyDescriptor(this, propertyId, out descriptorReference));

            return(descriptorReference);
        }
        /// <summary>
        /// Determines whether an object has a property
        /// </summary>
        /// <remarks>
        /// Requires an active script context.
        /// </remarks>
        /// <param name="propertyId">The ID of the property</param>
        /// <returns>Whether the object (or a prototype) has the property</returns>
        public bool HasProperty(EdgeJsPropertyId propertyId)
        {
            bool hasProperty;

            EdgeJsErrorHelpers.ThrowIfError(EdgeNativeMethods.JsHasProperty(this, propertyId, out hasProperty));

            return(hasProperty);
        }
        public override void RemoveVariable(string variableName)
        {
            InvokeScript(() =>
            {
                EdgeJsValue globalObj       = EdgeJsValue.GlobalObject;
                EdgeJsPropertyId variableId = EdgeJsPropertyId.FromString(variableName);

                if (globalObj.HasProperty(variableId))
                {
                    globalObj.SetProperty(variableId, EdgeJsValue.Undefined, true);
                }
            });
        }
        public override object CallFunction(string functionName, params object[] args)
        {
            object result = InvokeScript(() =>
            {
                EdgeJsValue globalObj       = EdgeJsValue.GlobalObject;
                EdgeJsPropertyId functionId = EdgeJsPropertyId.FromString(functionName);

                bool functionExist = globalObj.HasProperty(functionId);
                if (!functionExist)
                {
                    throw new JsRuntimeException(
                        string.Format(CommonStrings.Runtime_FunctionNotExist, functionName));
                }

                EdgeJsValue resultValue;
                EdgeJsValue functionValue = globalObj.GetProperty(functionId);

                if (args.Length > 0)
                {
                    EdgeJsValue[] processedArgs = MapToScriptType(args);

                    foreach (EdgeJsValue processedArg in processedArgs)
                    {
                        AddReferenceToValue(processedArg);
                    }

                    EdgeJsValue[] allProcessedArgs = new[] { globalObj }.Concat(processedArgs).ToArray();
                    resultValue = functionValue.CallFunction(allProcessedArgs);

                    foreach (EdgeJsValue processedArg in processedArgs)
                    {
                        RemoveReferenceToValue(processedArg);
                    }
                }
                else
                {
                    resultValue = functionValue.CallFunction(globalObj);
                }

                return(MapToHostType(resultValue));
            });

            return(result);
        }
        public override bool HasVariable(string variableName)
        {
            bool result = InvokeScript(() =>
            {
                EdgeJsValue globalObj       = EdgeJsValue.GlobalObject;
                EdgeJsPropertyId variableId = EdgeJsPropertyId.FromString(variableName);
                bool variableExist          = globalObj.HasProperty(variableId);

                if (variableExist)
                {
                    EdgeJsValue variableValue = globalObj.GetProperty(variableId);
                    variableExist             = variableValue.ValueType != JsValueType.Undefined;
                }

                return(variableExist);
            });

            return(result);
        }
 internal static extern JsErrorCode JsGetProperty(EdgeJsValue obj, EdgeJsPropertyId propertyId,
                                                  out EdgeJsValue value);
        private JsRuntimeException ConvertJsExceptionToJsRuntimeException(
            JsException jsException)
        {
            string message        = jsException.Message;
            string category       = string.Empty;
            int    lineNumber     = 0;
            int    columnNumber   = 0;
            string sourceFragment = string.Empty;

            var jsScriptException = jsException as EdgeJsScriptException;

            if (jsScriptException != null)
            {
                category = "Script error";
                EdgeJsValue errorValue = jsScriptException.Error;

                EdgeJsPropertyId stackPropertyId = EdgeJsPropertyId.FromString("stack");
                if (errorValue.HasProperty(stackPropertyId))
                {
                    EdgeJsValue stackPropertyValue = errorValue.GetProperty(stackPropertyId);
                    message = stackPropertyValue.ConvertToString().ToString();
                }
                else
                {
                    EdgeJsValue messagePropertyValue = errorValue.GetProperty("message");
                    string      scriptMessage        = messagePropertyValue.ConvertToString().ToString();
                    if (!string.IsNullOrWhiteSpace(scriptMessage))
                    {
                        message = string.Format("{0}: {1}", message.TrimEnd('.'), scriptMessage);
                    }
                }

                EdgeJsPropertyId linePropertyId = EdgeJsPropertyId.FromString("line");
                if (errorValue.HasProperty(linePropertyId))
                {
                    EdgeJsValue linePropertyValue = errorValue.GetProperty(linePropertyId);
                    lineNumber = linePropertyValue.ConvertToNumber().ToInt32() + 1;
                }

                EdgeJsPropertyId columnPropertyId = EdgeJsPropertyId.FromString("column");
                if (errorValue.HasProperty(columnPropertyId))
                {
                    EdgeJsValue columnPropertyValue = errorValue.GetProperty(columnPropertyId);
                    columnNumber = columnPropertyValue.ConvertToNumber().ToInt32() + 1;
                }

                if (lineNumber <= 0 && columnNumber <= 0)
                {
                    GetErrorCoordinatesFromMessage(message, out lineNumber, out columnNumber);
                }

                EdgeJsPropertyId sourcePropertyId = EdgeJsPropertyId.FromString("source");
                if (errorValue.HasProperty(sourcePropertyId))
                {
                    EdgeJsValue sourcePropertyValue = errorValue.GetProperty(sourcePropertyId);
                    sourceFragment = sourcePropertyValue.ConvertToString().ToString();
                }
            }
            else if (jsException is JsUsageException)
            {
                category = "Usage error";
            }
            else if (jsException is JsEngineException)
            {
                category = "Engine error";
            }
            else if (jsException is JsFatalException)
            {
                category = "Fatal error";
            }

            var jsEngineException = new JsRuntimeException(message, _engineModeName)
            {
                ErrorCode      = ((uint)jsException.ErrorCode).ToString(CultureInfo.InvariantCulture),
                Category       = category,
                LineNumber     = lineNumber,
                ColumnNumber   = columnNumber,
                SourceFragment = sourceFragment
            };

            return(jsEngineException);
        }
 internal static extern JsErrorCode JsSetProperty(EdgeJsValue obj, EdgeJsPropertyId propertyId,
                                                  EdgeJsValue value, bool useStrictRules);
 internal static extern JsErrorCode JsDeleteProperty(EdgeJsValue obj, EdgeJsPropertyId propertyId,
                                                     bool useStrictRules, out EdgeJsValue result);
 internal static extern JsErrorCode JsHasProperty(EdgeJsValue obj, EdgeJsPropertyId propertyId,
                                                  out bool hasProperty);
 /// <summary>
 /// Sets a 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(EdgeJsPropertyId id, EdgeJsValue value, bool useStrictRules)
 {
     EdgeJsErrorHelpers.ThrowIfError(EdgeNativeMethods.JsSetProperty(this, id, value, useStrictRules));
 }
 internal static extern JsErrorCode JsDefineProperty(EdgeJsValue obj, EdgeJsPropertyId propertyId,
                                                     EdgeJsValue propertyDescriptor, out bool result);
 internal static extern JsErrorCode JsGetOwnPropertyDescriptor(EdgeJsValue obj, EdgeJsPropertyId propertyId,
                                                               out EdgeJsValue propertyDescriptor);
 internal static extern JsErrorCode JsGetPropertyIdFromName(string name, out EdgeJsPropertyId propertyId);
 internal static extern JsErrorCode JsGetPropertyNameFromId(EdgeJsPropertyId propertyId, out IntPtr buffer);