Ejemplo n.º 1
0
        public static JsValue ToJsValue(this Object obj, EngineInstance engine)
        {
            if (obj != null)
            {
                if (obj is String)
                {
                    return(new JsValue((String)obj));
                }
                else if (obj is Int32)
                {
                    return(new JsValue((Int32)obj));
                }
                else if (obj is UInt32)
                {
                    return(new JsValue((UInt32)obj));
                }
                else if (obj is Double)
                {
                    return(new JsValue((Double)obj));
                }
                else if (obj is Single)
                {
                    return(new JsValue((Single)obj));
                }
                else if (obj is Boolean)
                {
                    return(new JsValue((Boolean)obj));
                }
                else if (obj is Enum)
                {
                    return(new JsValue(Convert.ToInt32(obj)));
                }

                return(engine.GetDomNode(obj));
            }

            return(JsValue.Null);
        }
Ejemplo n.º 2
0
        public static Object As(this JsValue value, Type targetType, EngineInstance engine)
        {
            if (value != JsValue.Null)
            {
                if (targetType == typeof(Int32))
                {
                    return(TypeConverter.ToInt32(value));
                }
                else if (targetType == typeof(Double))
                {
                    return(TypeConverter.ToNumber(value));
                }
                else if (targetType == typeof(String))
                {
                    return(value.IsPrimitive() ? TypeConverter.ToString(value) : value.ToString());
                }
                else if (targetType == typeof(Boolean))
                {
                    return(TypeConverter.ToBoolean(value));
                }
                else if (targetType == typeof(UInt32))
                {
                    return(TypeConverter.ToUint32(value));
                }
                else if (targetType == typeof(UInt16))
                {
                    return(TypeConverter.ToUint16(value));
                }
                else
                {
                    return(value.AsComplex(targetType, engine));
                }
            }

            return(null);
        }
 public static JsValue RunScript(this EngineInstance engine, String source, INode context) =>
 engine.RunScript(source, context.ToJsValue(engine));
 public static JsValue RunScript(this EngineInstance engine, String source) =>
 engine.RunScript(source, engine.Window);
Ejemplo n.º 5
0
 partial void Setup(EngineInstance engine);
Ejemplo n.º 6
0
 public DomConstructors(EngineInstance engine)
 {
     Object  = engine.Jint.Object;
     _engine = engine;
 }
Ejemplo n.º 7
0
 public DomConstructorInstance(EngineInstance engine, ConstructorInfo constructor)
     : this(engine, constructor.DeclaringType)
 {
     _constructor = constructor;
 }
Ejemplo n.º 8
0
        public static Object[] BuildArgs(this EngineInstance context, MethodBase method, JsValue[] arguments)
        {
            var parameters = method.GetParameters();
            var initDict   = method.GetCustomAttribute <DomInitDictAttribute>();
            var max        = parameters.Length;
            var args       = new Object[max];
            var offset     = 0;

            if (parameters.Length > 0 && parameters[0].ParameterType == typeof(IWindow))
            {
                if (arguments.Length == 0 || arguments[0].FromJsValue() is IWindow == false)
                {
                    args[offset++] = context.Window.Value;
                }
            }

            if (max > 0 && parameters[max - 1].GetCustomAttribute <ParamArrayAttribute>() != null)
            {
                max--;
            }

            if (initDict != null && arguments.Length + offset > initDict.Offset)
            {
                arguments = ExpandInitDict(arguments, parameters, initDict, max, offset);
            }

            var n = Math.Min(arguments.Length, max - offset);

            for (var i = 0; i < n; i++)
            {
                if (parameters[i].IsOptional && arguments[i].IsUndefined())
                {
                    args[i + offset] = parameters[i].DefaultValue;
                }
                else
                {
                    args[i + offset] = arguments[i].As(parameters[i].ParameterType, context);
                }
            }

            for (var i = n + offset; i < max; i++)
            {
                if (parameters[i].IsOptional)
                {
                    args[i] = parameters[i].DefaultValue;
                }
                else
                {
                    args[i] = parameters[i].ParameterType.GetDefaultValue();
                }
            }


            if (max != parameters.Length)
            {
                var array = Array.CreateInstance(parameters[max].ParameterType.GetElementType(), Math.Max(0, arguments.Length - max));

                for (var i = max; i < arguments.Length; i++)
                {
                    array.SetValue(arguments[i].FromJsValue(), i - max);
                }

                args[max] = array;
            }

            return(args);
        }
Ejemplo n.º 9
0
        public static void AddInstance(this EngineInstance engine, ObjectInstance obj, Type type)
        {
            var apply = type.GetInstanceAction();

            apply.Invoke(engine, obj);
        }