123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- using System;
- using Jint.Native;
- using Jint.Native.Errors;
- using Jint.Native.Object;
- namespace Jint.Runtime
- {
- public class TypeConverter
- {
- /// <summary>
- /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.1
- /// </summary>
- /// <param name="input"></param>
- /// <param name="preferredType"></param>
- /// <returns></returns>
- public static object ToPrimitive(object input, TypeCode preferredType)
- {
- return Undefined.Instance;
- }
- /// <summary>
- /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.2
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- public static bool ToBoolean(object o)
- {
- return (bool) o;
- }
- /// <summary>
- /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.3
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- public static double ToNumber(object o)
- {
- if (o == Undefined.Instance)
- {
- return double.NaN;
- }
- if (o == Null.Instance)
- {
- return 0;
- }
- if (o is bool)
- {
- return (bool)o ? 1 : 0;
- }
- if (o is double)
- {
- return (double)o;
- }
- var s = o as string;
- if (s != null)
- {
- return double.Parse(s);
- }
- return ToNumber(ToPrimitive(o, TypeCode.Double));
- }
- /// <summary>
- /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.4
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- public static int ToInteger(object o)
- {
- return (int) o;
- }
- /// <summary>
- /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.5
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- public static int ToInt32(object o)
- {
- return (int)o;
- }
- /// <summary>
- /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.6
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- public static uint ToUint32(object o)
- {
- return (uint)o;
- }
- /// <summary>
- /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.7
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- public static ushort ToUint16(object o)
- {
- return (ushort)o;
- }
- /// <summary>
- /// http://www.ecma-international.org/ecma-262/5.1/#sec-9.8
- /// </summary>
- /// <param name="o"></param>
- /// <returns></returns>
- public static string ToString(object o)
- {
- return (string)o;
- }
- public static ObjectInstance ToObject(Engine engine, object value)
- {
- var o = value as ObjectInstance;
- if (o != null)
- {
- return o;
- }
- if (value == Undefined.Instance)
- {
- throw new TypeError();
- }
- if (value == Null.Instance)
- {
- throw new TypeError();
- }
- if (value is bool)
- {
- return engine.Boolean.Construct((bool) value);
- }
- if (value is int)
- {
- return engine.Number.Construct((int) value);
- }
- if (value is uint)
- {
- return engine.Number.Construct((uint) value);
- }
- if (value is double)
- {
- return engine.Number.Construct((double) value);
- }
- if (value is string)
- {
- return engine.String.Construct((string) value);
- }
- throw new TypeError();
- }
- public static TypeCode GetType(object value)
- {
- if (value == null || value == Undefined.Instance || value == Null.Instance)
- {
- return TypeCode.Empty;
- }
- if (value is string)
- {
- return TypeCode.String;
- }
- if (value is double || value is int || value is uint || value is ushort)
- {
- return TypeCode.Double;
- }
- if (value is bool)
- {
- return TypeCode.Boolean;
- }
- return TypeCode.Object;
- }
- }
- }
|