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

            return(objValue);
        }
Esempio n. 4
0
        private object ToObject(IeJsValue value)
        {
            object result = value.HasExternalData ?
                            GCHandle.FromIntPtr(value.ExternalData).Target : value.ConvertToObject();

            return(result);
        }
Esempio n. 5
0
        private void FreezeObject(IeJsValue objValue)
        {
            IeJsValue objectValue       = IeJsValue.GlobalObject.GetProperty("Object");
            IeJsValue freezeMethodValue = objectValue.GetProperty("freeze");

            freezeMethodValue.CallFunction(objectValue, objValue);
        }
Esempio n. 6
0
        /// <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(IeJsValue value)
        {
            JsValueType valueType = value.ValueType;
            IeJsValue   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);
        }
Esempio n. 7
0
        /// <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(IeJsValue index)
        {
            bool hasProperty;

            IeJsErrorHelpers.ThrowIfError(IeNativeMethods.JsHasIndexedProperty(this, index, out hasProperty));

            return(hasProperty);
        }
Esempio n. 8
0
        /// <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 IeJsValue GetIndexedProperty(IeJsValue index)
        {
            IeJsValue propertyReference;

            IeJsErrorHelpers.ThrowIfError(IeNativeMethods.JsGetIndexedProperty(this, index, out propertyReference));

            return(propertyReference);
        }
Esempio n. 9
0
        /// <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(IeJsValue other)
        {
            bool equals;

            IeJsErrorHelpers.ThrowIfError(IeNativeMethods.JsStrictEquals(this, other, out equals));

            return(equals);
        }
Esempio n. 10
0
 public override void SetVariableValue(string variableName, object value)
 {
     InvokeScript(() =>
     {
         IeJsValue inputValue = MapToScriptType(value);
         IeJsValue.GlobalObject.SetProperty(variableName, inputValue, true);
     });
 }
Esempio n. 11
0
 public override void EmbedHostObject(string itemName, object value)
 {
     InvokeScript(() =>
     {
         IeJsValue processedValue = MapToScriptType(value);
         IeJsValue.GlobalObject.SetProperty(itemName, processedValue, true);
     });
 }
Esempio n. 12
0
        /// <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 IeJsValue CreateUriError(IeJsValue message)
        {
            IeJsValue reference;

            IeJsErrorHelpers.ThrowIfError(IeNativeMethods.JsCreateURIError(message, out reference));

            return(reference);
        }
Esempio n. 13
0
        /// <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(IeJsPropertyId propertyId, IeJsValue propertyDescriptor)
        {
            bool result;

            IeJsErrorHelpers.ThrowIfError(IeNativeMethods.JsDefineProperty(this, propertyId, propertyDescriptor, out result));

            return(result);
        }
Esempio n. 14
0
        public override object Evaluate(string expression, string documentName)
        {
            object result = InvokeScript(() =>
            {
                IeJsValue resultValue = IeJsContext.RunScript(expression, _jsSourceContext++, documentName);

                return(MapToHostType(resultValue));
            });

            return(result);
        }
Esempio n. 15
0
        public override object Evaluate(string expression)
        {
            object result = InvokeScript(() =>
            {
                IeJsValue resultValue = IeJsContext.RunScript(expression);

                return(MapToHostType(resultValue));
            });

            return(result);
        }
Esempio n. 16
0
        public override object GetVariableValue(string variableName)
        {
            object result = InvokeScript(() =>
            {
                IeJsValue variableValue = IeJsValue.GlobalObject.GetProperty(variableName);

                return(MapToHostType(variableValue));
            });

            return(result);
        }
Esempio n. 17
0
        private IeJsValue CreateObjectFromType(Type type)
        {
            IeJsValue typeValue = CreateConstructor(type);

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

            return(typeValue);
        }
Esempio n. 18
0
        public override void EmbedHostType(string itemName, Type type)
        {
            InvokeScript(() =>
            {
#if NETSTANDARD1_3
                IeJsValue typeValue = CreateObjectFromType(type);
#else
                IeJsValue typeValue = IeJsValue.FromObject(new HostType(type, _engineMode));
#endif
                IeJsValue.GlobalObject.SetProperty(itemName, typeValue, true);
            });
        }
Esempio n. 19
0
        public override void RemoveVariable(string variableName)
        {
            InvokeScript(() =>
            {
                IeJsValue globalObj       = IeJsValue.GlobalObject;
                IeJsPropertyId variableId = IeJsPropertyId.FromString(variableName);

                if (globalObj.HasProperty(variableId))
                {
                    globalObj.SetProperty(variableId, IeJsValue.Undefined, true);
                }
            });
        }
Esempio n. 20
0
        /// <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(IeJsValue value)
        {
            JsValueType valueType = value.ValueType;

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

            default:
                return(true);
            }
        }
Esempio n. 21
0
        /// <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 IeJsValue MapToScriptType(object value)
        {
            if (value == null)
            {
                return(IeJsValue.Null);
            }

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

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

            switch (typeCode)
            {
            case TypeCode.Boolean:
                return((bool)value ? IeJsValue.True : IeJsValue.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(IeJsValue.FromInt32(Convert.ToInt32(value)));

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

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

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

            _externalObjects.Add(value);

            IeJsValue objValue = IeJsValue.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);
        }
Esempio n. 23
0
        public override object CallFunction(string functionName, params object[] args)
        {
            object result = InvokeScript(() =>
            {
                IeJsValue globalObj       = IeJsValue.GlobalObject;
                IeJsPropertyId functionId = IeJsPropertyId.FromString(functionName);

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

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

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

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

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

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

                return(MapToHostType(resultValue));
            });

            return(result);
        }
Esempio n. 24
0
        public override bool HasVariable(string variableName)
        {
            bool result = InvokeScript(() =>
            {
                IeJsValue globalObj       = IeJsValue.GlobalObject;
                IeJsPropertyId variableId = IeJsPropertyId.FromString(variableName);
                bool variableExist        = globalObj.HasProperty(variableId);

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

                return(variableExist);
            });

            return(result);
        }
Esempio n. 25
0
        private IeJsValue CreateFunctionFromDelegate(Delegate value)
        {
            IeJsNativeFunction nativeFunction = (callee, isConstructCall, args, argCount, callbackData) =>
            {
                object[]        processedArgs  = MapToHostType(args.Skip(1).ToArray());
                ParameterInfo[] parameters     = value.GetMethodInfo().GetParameters();
                IeJsValue       undefinedValue = IeJsValue.Undefined;

                ReflectionHelpers.FixArgumentTypes(ref processedArgs, parameters);

                object result;

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

                    return(undefinedValue);
                }

                IeJsValue resultValue = MapToScriptType(result);

                return(resultValue);
            };

            _nativeFunctions.Add(nativeFunction);

            IeJsValue functionValue = IeJsValue.CreateFunction(nativeFunction);

            return(functionValue);
        }
 internal static extern JsErrorCode JsCreateURIError(IeJsValue message, out IeJsValue error);
 internal static extern JsErrorCode JsConvertValueToObject(IeJsValue value, out IeJsValue obj);
 internal static extern JsErrorCode JsCreateArray(uint length, out IeJsValue result);
 internal static extern JsErrorCode JsRunScript(string script, JsSourceContext sourceContext, string sourceUrl, out IeJsValue result);
 internal static extern JsErrorCode JsCreateExternalObject(IntPtr data, JsObjectFinalizeCallback finalizeCallback, out IeJsValue obj);
 internal static extern JsErrorCode JsSetExternalData(IeJsValue obj, IntPtr externalData);
 internal static extern JsErrorCode JsStringToPointer(IeJsValue value, out IntPtr stringValue, out UIntPtr stringLength);
 internal static extern JsErrorCode JsStrictEquals(IeJsValue obj1, IeJsValue obj2, out bool result);
 internal static extern JsErrorCode JsValueToVariant(IeJsValue obj, [MarshalAs(UnmanagedType.Struct)] out object var);
 internal static extern JsErrorCode JsPreventExtension(IeJsValue obj);
 internal static extern JsErrorCode JsDeleteProperty(IeJsValue obj, IeJsPropertyId propertyId, bool useStrictRules, out IeJsValue result);
 internal static extern JsErrorCode JsDeleteIndexedProperty(IeJsValue obj, IeJsValue index);
 internal static extern JsErrorCode JsDefineProperty(IeJsValue obj, IeJsPropertyId propertyId, IeJsValue propertyDescriptor, out bool result);
 internal static extern JsErrorCode JsSetIndexedProperty(IeJsValue obj, IeJsValue index, IeJsValue value);
 internal static extern JsErrorCode JsSetPrototype(IeJsValue obj, IeJsValue prototypeObject);
 internal static extern JsErrorCode JsCreateFunction(IeJsNativeFunction nativeFunction, IntPtr externalData, out IeJsValue function);
Esempio n. 42
0
 /// <summary>
 /// Sets a 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(IeJsValue exception)
 {
     IeJsErrorHelpers.ThrowIfError(IeNativeMethods.JsSetException(exception));
 }
 internal static extern JsErrorCode JsCallFunction(IeJsValue function, IeJsValue[] arguments, ushort argumentCount, out IeJsValue result);
Esempio n. 44
0
        private void ProjectProperties(IeJsValue 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;

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

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

                        object thisObj = null;

                        if (instance)
                        {
                            if (!thisValue.HasExternalData)
                            {
                                IeJsValue errorValue = IeJsErrorHelpers.CreateTypeError(
                                    string.Format(NetCoreStrings.Runtime_InvalidThisContextForHostObjectProperty, propertyName));
                                IeJsErrorHelpers.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)
                            ;

                            IeJsValue errorValue = IeJsErrorHelpers.CreateError(errorMessage);
                            IeJsErrorHelpers.SetException(errorValue);

                            return(undefinedValue);
                        }

                        IeJsValue resultValue = MapToScriptType(result);

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

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

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

                        object thisObj = null;

                        if (instance)
                        {
                            if (!thisValue.HasExternalData)
                            {
                                IeJsValue errorValue = IeJsErrorHelpers.CreateTypeError(
                                    string.Format(NetCoreStrings.Runtime_InvalidThisContextForHostObjectProperty, propertyName));
                                IeJsErrorHelpers.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)
                            ;

                            IeJsValue errorValue = IeJsErrorHelpers.CreateError(errorMessage);
                            IeJsErrorHelpers.SetException(errorValue);

                            return(undefinedValue);
                        }

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

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

                target.DefineProperty(propertyName, descriptorValue);
            }
        }
 internal static extern JsErrorCode JsSetException(IeJsValue exception);
Esempio n. 46
0
        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 IeJsScriptException;

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

                IeJsPropertyId messagePropertyId    = IeJsPropertyId.FromString("message");
                IeJsValue      messagePropertyValue = errorValue.GetProperty(messagePropertyId);
                string         scriptMessage        = messagePropertyValue.ConvertToString().ToString();
                if (!string.IsNullOrWhiteSpace(scriptMessage))
                {
                    message = string.Format("{0}: {1}", message.TrimEnd('.'), scriptMessage);
                }

                IeJsPropertyId linePropertyId = IeJsPropertyId.FromString("line");
                if (errorValue.HasProperty(linePropertyId))
                {
                    IeJsValue linePropertyValue = errorValue.GetProperty(linePropertyId);
                    lineNumber = (int)linePropertyValue.ConvertToNumber().ToDouble() + 1;
                }

                IeJsPropertyId columnPropertyId = IeJsPropertyId.FromString("column");
                if (errorValue.HasProperty(columnPropertyId))
                {
                    IeJsValue columnPropertyValue = errorValue.GetProperty(columnPropertyId);
                    columnNumber = (int)columnPropertyValue.ConvertToNumber().ToDouble() + 1;
                }

                IeJsPropertyId sourcePropertyId = IeJsPropertyId.FromString("source");
                if (errorValue.HasProperty(sourcePropertyId))
                {
                    IeJsValue 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 JsRunSerializedScript(string script, byte[] buffer, JsSourceContext sourceContext, string sourceUrl, out IeJsValue result);
 internal static extern JsErrorCode JsConvertValueToNumber(IeJsValue value, out IeJsValue numberValue);
 internal static extern JsErrorCode JsConvertValueToString(IeJsValue value, out IeJsValue stringValue);
 internal static extern JsErrorCode JsSetProperty(IeJsValue obj, IeJsPropertyId propertyId, IeJsValue value, bool useStrictRules);
 internal static extern JsErrorCode JsRelease(IeJsValue reference, out uint count);
 internal static extern JsErrorCode JsCreateObject(out IeJsValue obj);
Esempio n. 53
0
        private IeJsValue CreateConstructor(Type type)
        {
            TypeInfo     typeInfo            = type.GetTypeInfo();
            string       typeName            = type.FullName;
            BindingFlags defaultBindingFlags = ReflectionHelpers.GetDefaultBindingFlags(true);

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

            IeJsNativeFunction nativeFunction = (callee, isConstructCall, args, argCount, callbackData) =>
            {
                IeJsValue resultValue;
                IeJsValue undefinedValue = IeJsValue.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)
                {
                    IeJsValue errorValue = IeJsErrorHelpers.CreateError(
                        string.Format(NetCoreStrings.Runtime_HostTypeConstructorNotFound, typeName));
                    IeJsErrorHelpers.SetException(errorValue);

                    return(undefinedValue);
                }

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

                    return(undefinedValue);
                }

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

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

                    return(undefinedValue);
                }

                resultValue = MapToScriptType(result);

                return(resultValue);
            };

            _nativeFunctions.Add(nativeFunction);

            IeJsValue constructorValue = IeJsValue.CreateFunction(nativeFunction);

            return(constructorValue);
        }
 internal static extern JsErrorCode JsCreateReferenceError(IeJsValue message, out IeJsValue error);
Esempio n. 55
0
        private void ProjectMethods(IeJsValue 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();

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

                    object thisObj = null;

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

                            return(undefinedValue);
                        }

                        thisObj = MapToHostType(thisValue);
                    }

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

                    var bestFitMethod = (MethodInfo)ReflectionHelpers.GetBestFitMethod(
                        methodCandidates, processedArgs);
                    if (bestFitMethod == null)
                    {
                        IeJsValue errorValue = IeJsErrorHelpers.CreateReferenceError(
                            string.Format(NetCoreStrings.Runtime_SuitableMethodOfHostObjectNotFound, methodName));
                        IeJsErrorHelpers.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)
                        ;

                        IeJsValue errorValue = IeJsErrorHelpers.CreateError(errorMessage);
                        IeJsErrorHelpers.SetException(errorValue);

                        return(undefinedValue);
                    }

                    IeJsValue resultValue = MapToScriptType(result);

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

                IeJsValue methodValue = IeJsValue.CreateFunction(nativeFunction);
                target.SetProperty(methodName, methodValue, true);
            }
        }
		/// <summary>
		/// Initializes a new instance of the <see cref="IeJsScriptException"/> class
		/// </summary>
		/// <param name="code">The error code returned</param>
		/// <param name="error">The JavaScript error object</param>
		public IeJsScriptException(JsErrorCode code, IeJsValue error)
			: this(code, error, "JavaScript Exception")
		{ }
 internal static extern JsErrorCode JsConstructObject(IeJsValue function, IeJsValue[] arguments, ushort argumentCount, out IeJsValue result);
 internal static extern JsErrorCode JsConvertValueToBoolean(IeJsValue value, out IeJsValue booleanValue);
		/// <summary>
		/// Initializes a new instance of the <see cref="IeJsScriptException"/> 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 IeJsScriptException(JsErrorCode code, IeJsValue error, string message)
			: base(code, message)
		{
			_error = error;
		}
 internal static extern JsErrorCode JsVariantToValue([MarshalAs(UnmanagedType.Struct)] ref object var, out IeJsValue value);