“Edge” JavaScript value
The JavaScript value is one of the following types of values: Undefined, Null, Boolean, String, Number, or Object.
 /// <summary>
 /// Removes a reference to the value
 /// </summary>
 /// <param name="value">The value</param>
 private static void RemoveReferenceToValue(EdgeJsValue value)
 {
     if (CanHaveReferences(value))
     {
         value.Release();
     }
 }
 /// <summary>
 /// Adds a reference to the value
 /// </summary>
 /// <param name="value">The value</param>
 private static void AddReferenceToValue(EdgeJsValue value)
 {
     if (CanHaveReferences(value))
     {
         value.AddRef();
     }
 }
        private EdgeJsValue FromObject(object value)
        {
            var         del      = value as Delegate;
            EdgeJsValue objValue = del != null?CreateFunctionFromDelegate(del) : CreateExternalObjectFromObject(value);

            return(objValue);
        }
        private object ToObject(EdgeJsValue value)
        {
            object result = value.HasExternalData ?
                            GCHandle.FromIntPtr(value.ExternalData).Target : value.ConvertToObject();

            return(result);
        }
        /// <summary>
        /// Makes a mapping of value from the script type to a host type
        /// </summary>
        /// <param name="value">The source value</param>
        /// <returns>The mapped value</returns>
        private object MapToHostType(EdgeJsValue value)
        {
            JsValueType valueType = value.ValueType;
            EdgeJsValue processedValue;
            object      result;

            switch (valueType)
            {
            case JsValueType.Null:
                result = null;
                break;

            case JsValueType.Undefined:
                result = Undefined.Value;
                break;

            case JsValueType.Boolean:
                processedValue = value.ConvertToBoolean();
                result         = processedValue.ToBoolean();
                break;

            case JsValueType.Number:
                processedValue = value.ConvertToNumber();
                result         = NumericHelpers.CastDoubleValueToCorrectType(processedValue.ToDouble());
                break;

            case JsValueType.String:
                processedValue = value.ConvertToString();
                result         = processedValue.ToString();
                break;

            case JsValueType.Object:
            case JsValueType.Function:
            case JsValueType.Error:
            case JsValueType.Array:
#if NETSTANDARD1_3
                result = ToObject(value);
#else
                processedValue = value.ConvertToObject();
                object obj = processedValue.ToObject();

                if (!TypeConverter.IsPrimitiveType(obj.GetType()))
                {
                    var hostObj = obj as HostObject;
                    result = hostObj != null ? hostObj.Target : obj;
                }
                else
                {
                    result = obj;
                }
#endif
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            return(result);
        }
 public override void SetVariableValue(string variableName, object value)
 {
     InvokeScript(() =>
     {
         EdgeJsValue inputValue = MapToScriptType(value);
         EdgeJsValue.GlobalObject.SetProperty(variableName, inputValue, true);
     });
 }
        /// <summary>
        /// Creates a new JavaScript URIError error object
        /// </summary>
        /// <remarks>
        /// Requires an active script context.
        /// </remarks>
        /// <param name="message">Message for the error object</param>
        /// <returns>The new error object</returns>
        public static EdgeJsValue CreateUriError(EdgeJsValue message)
        {
            EdgeJsValue reference;

            EdgeJsErrorHelpers.ThrowIfError(EdgeNativeMethods.JsCreateURIError(message, out reference));

            return(reference);
        }
        /// <summary>
        /// Compare two JavaScript values for strict equality
        /// </summary>
        /// <remarks>
        /// <para>
        /// This function is equivalent to the "===" operator in JavaScript.
        /// </para>
        /// <para>
        /// Requires an active script context.
        /// </para>
        /// </remarks>
        /// <param name="other">The object to compare</param>
        /// <returns>Whether the values are strictly equal</returns>
        public bool StrictEquals(EdgeJsValue other)
        {
            bool equals;

            EdgeJsErrorHelpers.ThrowIfError(EdgeNativeMethods.JsStrictEquals(this, other, out equals));

            return(equals);
        }
        /// <summary>
        /// Retrieve a value at the specified index of an object
        /// </summary>
        /// <remarks>
        /// Requires an active script context.
        /// </remarks>
        /// <param name="index">The index to retrieve</param>
        /// <returns>The retrieved value</returns>
        public EdgeJsValue GetIndexedProperty(EdgeJsValue index)
        {
            EdgeJsValue propertyReference;

            EdgeJsErrorHelpers.ThrowIfError(EdgeNativeMethods.JsGetIndexedProperty(this, index, out propertyReference));

            return(propertyReference);
        }
        /// <summary>
        /// Test if an object has a value at the specified index
        /// </summary>
        /// <remarks>
        /// Requires an active script context.
        /// </remarks>
        /// <param name="index">The index to test</param>
        /// <returns>Whether the object has an value at the specified index</returns>
        public bool HasIndexedProperty(EdgeJsValue index)
        {
            bool hasProperty;

            EdgeJsErrorHelpers.ThrowIfError(EdgeNativeMethods.JsHasIndexedProperty(this, index, out hasProperty));

            return(hasProperty);
        }
        /// <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);
        }
 public override void EmbedHostObject(string itemName, object value)
 {
     InvokeScript(() =>
     {
         EdgeJsValue processedValue = MapToScriptType(value);
         EdgeJsValue.GlobalObject.SetProperty(itemName, processedValue, true);
     });
 }
        private void FreezeObject(EdgeJsValue objValue)
        {
            EdgeJsValue freezeMethodValue = EdgeJsValue.GlobalObject
                                            .GetProperty("Object")
                                            .GetProperty("freeze")
            ;

            freezeMethodValue.CallFunction(objValue);
        }
        public override object GetVariableValue(string variableName)
        {
            object result = InvokeScript(() =>
            {
                EdgeJsValue variableValue = EdgeJsValue.GlobalObject.GetProperty(variableName);

                return(MapToHostType(variableValue));
            });

            return(result);
        }
        public override object Evaluate(string expression, string documentName)
        {
            object result = InvokeScript(() =>
            {
                EdgeJsValue resultValue = EdgeJsContext.RunScript(expression, _jsSourceContext++, documentName);

                return(MapToHostType(resultValue));
            });

            return(result);
        }
        private EdgeJsValue CreateObjectFromType(Type type)
        {
            EdgeJsValue typeValue = CreateConstructor(type);

            ProjectFields(typeValue, type, false);
            ProjectProperties(typeValue, type, false);
            ProjectMethods(typeValue, type, false);
            FreezeObject(typeValue);

            return(typeValue);
        }
示例#17
0
        public override object Evaluate(string expression)
        {
            object result = InvokeScript(() =>
            {
                EdgeJsValue resultValue = EdgeJsContext.RunScript(expression);

                return(MapToHostType(resultValue));
            });

            return(result);
        }
        public override void EmbedHostType(string itemName, Type type)
        {
            InvokeScript(() =>
            {
#if NETSTANDARD1_3
                EdgeJsValue typeValue = CreateObjectFromType(type);
#else
                EdgeJsValue typeValue = EdgeJsValue.FromObject(new HostType(type, _engineMode));
#endif
                EdgeJsValue.GlobalObject.SetProperty(itemName, typeValue, true);
            });
        }
        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);
                }
            });
        }
        /// <summary>
        /// Checks whether the value can have references
        /// </summary>
        /// <param name="value">The value</param>
        /// <returns>Result of check (true - may have; false - may not have)</returns>
        private static bool CanHaveReferences(EdgeJsValue value)
        {
            JsValueType valueType = value.ValueType;

            switch (valueType)
            {
            case JsValueType.Null:
            case JsValueType.Undefined:
            case JsValueType.Boolean:
                return(false);

            default:
                return(true);
            }
        }
        /// <summary>
        /// Makes a mapping of value from the host type to a script type
        /// </summary>
        /// <param name="value">The source value</param>
        /// <returns>The mapped value</returns>
        private EdgeJsValue MapToScriptType(object value)
        {
            if (value == null)
            {
                return(EdgeJsValue.Null);
            }

            if (value is Undefined)
            {
                return(EdgeJsValue.Undefined);
            }

            var typeCode = value.GetType().GetTypeCode();

            switch (typeCode)
            {
            case TypeCode.Boolean:
                return((bool)value ? EdgeJsValue.True : EdgeJsValue.False);

            case TypeCode.SByte:
            case TypeCode.Byte:
            case TypeCode.Int16:
            case TypeCode.UInt16:
            case TypeCode.Int32:
            case TypeCode.UInt32:
            case TypeCode.Int64:
            case TypeCode.UInt64:
                return(EdgeJsValue.FromInt32(Convert.ToInt32(value)));

            case TypeCode.Single:
            case TypeCode.Double:
            case TypeCode.Decimal:
                return(EdgeJsValue.FromDouble(Convert.ToDouble(value)));

            case TypeCode.Char:
            case TypeCode.String:
                return(EdgeJsValue.FromString((string)value));

            default:
#if NETSTANDARD1_3
                return(FromObject(value));
#else
                object processedValue = !TypeConverter.IsPrimitiveType(typeCode) ?
                                        new HostObject(value, _engineMode) : value;
                return(EdgeJsValue.FromObject(processedValue));
#endif
            }
        }
        private EdgeJsValue CreateExternalObjectFromObject(object value)
        {
            GCHandle handle = GCHandle.Alloc(value);

            _externalObjects.Add(value);

            EdgeJsValue objValue = EdgeJsValue.CreateExternalObject(
                GCHandle.ToIntPtr(handle), _externalObjectFinalizeCallback);
            Type type = value.GetType();

            ProjectFields(objValue, type, true);
            ProjectProperties(objValue, type, true);
            ProjectMethods(objValue, type, true);
            FreezeObject(objValue);

            return(objValue);
        }
        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);
        }
        private EdgeJsValue CreateFunctionFromDelegate(Delegate value)
        {
            EdgeJsNativeFunction nativeFunction = (callee, isConstructCall, args, argCount, callbackData) =>
            {
                object[]        processedArgs  = MapToHostType(args.Skip(1).ToArray());
                ParameterInfo[] parameters     = value.GetMethodInfo().GetParameters();
                EdgeJsValue     undefinedValue = EdgeJsValue.Undefined;

                ReflectionHelpers.FixArgumentTypes(ref processedArgs, parameters);

                object result;

                try
                {
                    result = value.DynamicInvoke(processedArgs);
                }
                catch (Exception e)
                {
                    EdgeJsValue errorValue = EdgeJsErrorHelpers.CreateError(
                        string.Format(NetCoreStrings.Runtime_HostDelegateInvocationFailed, e.Message));
                    EdgeJsErrorHelpers.SetException(errorValue);

                    return(undefinedValue);
                }

                EdgeJsValue resultValue = MapToScriptType(result);

                return(resultValue);
            };

            _nativeFunctions.Add(nativeFunction);

            EdgeJsValue functionValue = EdgeJsValue.CreateFunction(nativeFunction);

            return(functionValue);
        }
		internal static extern JsErrorCode JsAddRef(EdgeJsValue reference, out uint count);
		internal static extern JsErrorCode JsGetAndClearException(out EdgeJsValue exception);
		internal static extern JsErrorCode JsCreateFunction(EdgeJsNativeFunction nativeFunction, IntPtr externalData, out EdgeJsValue function);
		internal static extern JsErrorCode JsCallFunction(EdgeJsValue function, EdgeJsValue[] arguments, ushort argumentCount, out EdgeJsValue result);
		internal static extern JsErrorCode JsSetExternalData(EdgeJsValue obj, IntPtr externalData);
		internal static extern JsErrorCode JsStrictEquals(EdgeJsValue obj1, EdgeJsValue obj2, out bool result);
        private EdgeJsValue CreateConstructor(Type type)
        {
            TypeInfo     typeInfo            = type.GetTypeInfo();
            string       typeName            = type.FullName;
            BindingFlags defaultBindingFlags = ReflectionHelpers.GetDefaultBindingFlags(true);

            ConstructorInfo[] constructors = type.GetConstructors(defaultBindingFlags);

            EdgeJsNativeFunction nativeFunction = (callee, isConstructCall, args, argCount, callbackData) =>
            {
                EdgeJsValue resultValue;
                EdgeJsValue undefinedValue = EdgeJsValue.Undefined;

                object[] processedArgs = MapToHostType(args.Skip(1).ToArray());
                object   result;

                if (processedArgs.Length == 0 && typeInfo.IsValueType)
                {
                    result      = Activator.CreateInstance(type);
                    resultValue = MapToScriptType(result);

                    return(resultValue);
                }

                if (constructors.Length == 0)
                {
                    EdgeJsValue errorValue = EdgeJsErrorHelpers.CreateError(
                        string.Format(NetCoreStrings.Runtime_HostTypeConstructorNotFound, typeName));
                    EdgeJsErrorHelpers.SetException(errorValue);

                    return(undefinedValue);
                }

                var bestFitConstructor = (ConstructorInfo)ReflectionHelpers.GetBestFitMethod(
                    constructors, processedArgs);
                if (bestFitConstructor == null)
                {
                    EdgeJsValue errorValue = EdgeJsErrorHelpers.CreateReferenceError(
                        string.Format(NetCoreStrings.Runtime_SuitableConstructorOfHostTypeNotFound, typeName));
                    EdgeJsErrorHelpers.SetException(errorValue);

                    return(undefinedValue);
                }

                ReflectionHelpers.FixArgumentTypes(ref processedArgs, bestFitConstructor.GetParameters());

                try
                {
                    result = bestFitConstructor.Invoke(processedArgs);
                }
                catch (Exception e)
                {
                    EdgeJsValue errorValue = EdgeJsErrorHelpers.CreateError(
                        string.Format(NetCoreStrings.Runtime_HostTypeConstructorInvocationFailed, typeName, e.Message));
                    EdgeJsErrorHelpers.SetException(errorValue);

                    return(undefinedValue);
                }

                resultValue = MapToScriptType(result);

                return(resultValue);
            };

            _nativeFunctions.Add(nativeFunction);

            EdgeJsValue constructorValue = EdgeJsValue.CreateFunction(nativeFunction);

            return(constructorValue);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="EdgeJsScriptException"/> class
 /// with a specified error message
 /// </summary>
 /// <param name="errorCode">The error code returned</param>
 /// <param name="error">The JavaScript error object</param>
 /// <param name="message">The error message</param>
 public EdgeJsScriptException(JsErrorCode errorCode, EdgeJsValue error, string message)
     : base(errorCode, message)
 {
     _error = error;
 }
		internal static extern JsErrorCode JsGetTrueValue(out EdgeJsValue trueValue);
        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);
        }
        private void ProjectMethods(EdgeJsValue target, Type type, bool instance)
        {
            string       typeName            = type.FullName;
            BindingFlags defaultBindingFlags = ReflectionHelpers.GetDefaultBindingFlags(instance);

            MethodInfo[] methods = type.GetMethods(defaultBindingFlags);
            IEnumerable <IGrouping <string, MethodInfo> > methodGroups = methods.GroupBy(m => m.Name);

            foreach (IGrouping <string, MethodInfo> methodGroup in methodGroups)
            {
                string       methodName       = methodGroup.Key;
                MethodInfo[] methodCandidates = methodGroup.ToArray();

                EdgeJsNativeFunction nativeFunction = (callee, isConstructCall, args, argCount, callbackData) =>
                {
                    EdgeJsValue thisValue      = args[0];
                    EdgeJsValue undefinedValue = EdgeJsValue.Undefined;

                    object thisObj = null;

                    if (instance)
                    {
                        if (!thisValue.HasExternalData)
                        {
                            EdgeJsValue errorValue = EdgeJsErrorHelpers.CreateTypeError(
                                string.Format(NetCoreStrings.Runtime_InvalidThisContextForHostObjectMethod, methodName));
                            EdgeJsErrorHelpers.SetException(errorValue);

                            return(undefinedValue);
                        }

                        thisObj = MapToHostType(thisValue);
                    }

                    object[] processedArgs = MapToHostType(args.Skip(1).ToArray());

                    var bestFitMethod = (MethodInfo)ReflectionHelpers.GetBestFitMethod(
                        methodCandidates, processedArgs);
                    if (bestFitMethod == null)
                    {
                        EdgeJsValue errorValue = EdgeJsErrorHelpers.CreateReferenceError(
                            string.Format(NetCoreStrings.Runtime_SuitableMethodOfHostObjectNotFound, methodName));
                        EdgeJsErrorHelpers.SetException(errorValue);

                        return(undefinedValue);
                    }

                    ReflectionHelpers.FixArgumentTypes(ref processedArgs, bestFitMethod.GetParameters());

                    object result;

                    try
                    {
                        result = bestFitMethod.Invoke(thisObj, processedArgs);
                    }
                    catch (Exception e)
                    {
                        string errorMessage = instance ?
                                              string.Format(
                            NetCoreStrings.Runtime_HostObjectMethodInvocationFailed, methodName, e.Message)
                                                        :
                                              string.Format(
                            NetCoreStrings.Runtime_HostTypeMethodInvocationFailed, methodName, typeName, e.Message)
                        ;

                        EdgeJsValue errorValue = EdgeJsErrorHelpers.CreateError(errorMessage);
                        EdgeJsErrorHelpers.SetException(errorValue);

                        return(undefinedValue);
                    }

                    EdgeJsValue resultValue = MapToScriptType(result);

                    return(resultValue);
                };
                _nativeFunctions.Add(nativeFunction);

                EdgeJsValue methodValue = EdgeJsValue.CreateFunction(nativeFunction);
                target.SetProperty(methodName, methodValue, true);
            }
        }
        private void ProjectProperties(EdgeJsValue target, Type type, bool instance)
        {
            string       typeName            = type.FullName;
            BindingFlags defaultBindingFlags = ReflectionHelpers.GetDefaultBindingFlags(instance);

            PropertyInfo[] properties = type.GetProperties(defaultBindingFlags);

            foreach (PropertyInfo property in properties)
            {
                string propertyName = property.Name;

                EdgeJsValue descriptorValue = EdgeJsValue.CreateObject();
                descriptorValue.SetProperty("enumerable", EdgeJsValue.True, true);

                if (property.GetGetMethod() != null)
                {
                    EdgeJsNativeFunction nativeFunction = (callee, isConstructCall, args, argCount, callbackData) =>
                    {
                        EdgeJsValue thisValue      = args[0];
                        EdgeJsValue undefinedValue = EdgeJsValue.Undefined;

                        object thisObj = null;

                        if (instance)
                        {
                            if (!thisValue.HasExternalData)
                            {
                                EdgeJsValue errorValue = EdgeJsErrorHelpers.CreateTypeError(
                                    string.Format(NetCoreStrings.Runtime_InvalidThisContextForHostObjectProperty, propertyName));
                                EdgeJsErrorHelpers.SetException(errorValue);

                                return(undefinedValue);
                            }

                            thisObj = MapToHostType(thisValue);
                        }

                        object result;

                        try
                        {
                            result = property.GetValue(thisObj, new object[0]);
                        }
                        catch (Exception e)
                        {
                            string errorMessage = instance ?
                                                  string.Format(
                                NetCoreStrings.Runtime_HostObjectPropertyGettingFailed, propertyName, e.Message)
                                                                :
                                                  string.Format(
                                NetCoreStrings.Runtime_HostTypePropertyGettingFailed, propertyName, typeName, e.Message)
                            ;

                            EdgeJsValue errorValue = EdgeJsErrorHelpers.CreateError(errorMessage);
                            EdgeJsErrorHelpers.SetException(errorValue);

                            return(undefinedValue);
                        }

                        EdgeJsValue resultValue = MapToScriptType(result);

                        return(resultValue);
                    };
                    _nativeFunctions.Add(nativeFunction);

                    EdgeJsValue getMethodValue = EdgeJsValue.CreateFunction(nativeFunction);
                    descriptorValue.SetProperty("get", getMethodValue, true);
                }

                if (property.GetSetMethod() != null)
                {
                    EdgeJsNativeFunction nativeFunction = (callee, isConstructCall, args, argCount, callbackData) =>
                    {
                        EdgeJsValue thisValue      = args[0];
                        EdgeJsValue undefinedValue = EdgeJsValue.Undefined;

                        object thisObj = null;

                        if (instance)
                        {
                            if (!thisValue.HasExternalData)
                            {
                                EdgeJsValue errorValue = EdgeJsErrorHelpers.CreateTypeError(
                                    string.Format(NetCoreStrings.Runtime_InvalidThisContextForHostObjectProperty, propertyName));
                                EdgeJsErrorHelpers.SetException(errorValue);

                                return(undefinedValue);
                            }

                            thisObj = MapToHostType(thisValue);
                        }

                        object value = MapToHostType(args.Skip(1).First());
                        ReflectionHelpers.FixPropertyValueType(ref value, property);

                        try
                        {
                            property.SetValue(thisObj, value, new object[0]);
                        }
                        catch (Exception e)
                        {
                            string errorMessage = instance ?
                                                  string.Format(
                                NetCoreStrings.Runtime_HostObjectPropertySettingFailed, propertyName, e.Message)
                                                                :
                                                  string.Format(
                                NetCoreStrings.Runtime_HostTypePropertySettingFailed, propertyName, typeName, e.Message)
                            ;

                            EdgeJsValue errorValue = EdgeJsErrorHelpers.CreateError(errorMessage);
                            EdgeJsErrorHelpers.SetException(errorValue);

                            return(undefinedValue);
                        }

                        return(undefinedValue);
                    };
                    _nativeFunctions.Add(nativeFunction);

                    EdgeJsValue setMethodValue = EdgeJsValue.CreateFunction(nativeFunction);
                    descriptorValue.SetProperty("set", setMethodValue, true);
                }

                target.DefineProperty(propertyName, descriptorValue);
            }
        }
		internal static extern JsErrorCode JsRunScript(string script, JsSourceContext sourceContext, string sourceUrl, out EdgeJsValue result);
		internal static extern JsErrorCode JsGetUndefinedValue(out EdgeJsValue undefinedValue);
		internal static extern JsErrorCode JsGetProperty(EdgeJsValue obj, EdgeJsPropertyId propertyId, out EdgeJsValue value);
		internal static extern JsErrorCode JsDeleteIndexedProperty(EdgeJsValue obj, EdgeJsValue index);
		internal static extern JsErrorCode JsGetOwnPropertyDescriptor(EdgeJsValue obj, EdgeJsPropertyId propertyId, out EdgeJsValue propertyDescriptor);
		internal static extern JsErrorCode JsHasExternalData(EdgeJsValue obj, out bool value);
		internal static extern JsErrorCode JsGetOwnPropertyNames(EdgeJsValue obj, out EdgeJsValue propertyNames);
		internal static extern JsErrorCode JsCreateArray(uint length, out EdgeJsValue result);
		internal static extern JsErrorCode JsSetProperty(EdgeJsValue obj, EdgeJsPropertyId propertyId, EdgeJsValue value, bool useStrictRules);
		internal static extern JsErrorCode JsConstructObject(EdgeJsValue function, EdgeJsValue[] arguments, ushort argumentCount, out EdgeJsValue result);
		internal static extern JsErrorCode JsHasProperty(EdgeJsValue obj, EdgeJsPropertyId propertyId, out bool hasProperty);
		internal static extern JsErrorCode JsCreateURIError(EdgeJsValue message, out EdgeJsValue error);
		internal static extern JsErrorCode JsDeleteProperty(EdgeJsValue obj, EdgeJsPropertyId propertyId, bool useStrictRules, out EdgeJsValue result);
		internal static extern JsErrorCode JsSetException(EdgeJsValue exception);
		internal static extern JsErrorCode JsDefineProperty(EdgeJsValue obj, EdgeJsPropertyId propertyId, EdgeJsValue propertyDescriptor, out bool result);
		internal static extern JsErrorCode JsRelease(EdgeJsValue reference, out uint count);
		internal static extern JsErrorCode JsHasIndexedProperty(EdgeJsValue obj, EdgeJsValue index, out bool result);
		internal static extern JsErrorCode JsRunSerializedScript(string script, byte[] buffer, JsSourceContext sourceContext, string sourceUrl, out EdgeJsValue result);
		internal static extern JsErrorCode JsGetIndexedProperty(EdgeJsValue obj, EdgeJsValue index, out EdgeJsValue result);
		internal static extern JsErrorCode JsGetNullValue(out EdgeJsValue nullValue);
		internal static extern JsErrorCode JsSetIndexedProperty(EdgeJsValue obj, EdgeJsValue index, EdgeJsValue value);
		internal static extern JsErrorCode JsGetFalseValue(out EdgeJsValue falseValue);
		internal static extern JsErrorCode JsPreventExtension(EdgeJsValue obj);