123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340 |
- using System;
- using System.Diagnostics;
- using System.Diagnostics.Contracts;
- using Jint.Native.Object;
- using Jint.Runtime;
- namespace Jint.Native
- {
- [DebuggerTypeProxy(typeof(JsValueDebugView))]
- public struct JsValue : IEquatable<JsValue>
- {
- public static JsValue Undefined = new JsValue(Types.Undefined);
- public static JsValue Null = new JsValue(Types.Null);
- public static JsValue False = new JsValue(false);
- public static JsValue True = new JsValue(true);
- public JsValue(bool value)
- {
- _bool = value;
- _double = null;
- _object = null;
- _string = null;
- _type = Types.Boolean;
- }
- public JsValue(double value)
- {
- _bool = null;
- _double = value;
- _object = null;
- _string = null;
- _type = Types.Number;
- }
- public JsValue(string value)
- {
- _bool = null;
- _double = null;
- _object = null;
- _string = value;
- _type = Types.String;
- }
- public JsValue(ObjectInstance value)
- {
- _bool = null;
- _double = null;
- _object = value;
- _string = null;
- _type = Types.Object;
- }
- private JsValue(Types type)
- {
- _bool = null;
- _double = null;
- _object = null;
- _string = null;
- _type = type;
- }
- private readonly bool? _bool;
- private readonly double? _double;
- private readonly ObjectInstance _object;
- private readonly string _string;
- private readonly Types _type;
- [Pure]
- public bool IsPrimitive()
- {
- return _type != Types.Object && _type != Types.None;
- }
- [Pure]
- public bool IsUndefined()
- {
- return _type == Types.Undefined;
- }
-
- [Pure]
- public bool IsObject()
- {
- return _type == Types.Object;
- }
-
- [Pure]
- public bool IsString()
- {
- return _type == Types.String;
- }
- [Pure]
- public bool IsNumber()
- {
- return _type == Types.Number;
- }
- [Pure]
- public bool IsBoolean()
- {
- return _type == Types.Boolean;
- }
- [Pure]
- public bool IsNull()
- {
- return _type == Types.Null;
- }
- [Pure]
- public ObjectInstance AsObject()
- {
- if (_type != Types.Object)
- {
- throw new ArgumentException("The value is not an object");
- }
- return _object;
- }
- public T TryCast<T>(Action<JsValue> fail = null) where T: class
- {
- if (!this.IsObject())
- {
- return null;
- }
- var o = this.AsObject();
- var t = o as T;
- if (t != null)
- {
- return t;
- }
-
- if (fail != null)
- {
- fail(this);
- }
- return null;
- }
- public bool Is<T>() where T : ObjectInstance
- {
- return IsObject() && AsObject() is T;
- }
- public T As<T>() where T : ObjectInstance
- {
- return _object as T;
- }
-
- [Pure]
- public bool AsBoolean()
- {
- if (_type != Types.Boolean)
- {
- throw new ArgumentException("The value is not a boolean");
- }
- if (!_bool.HasValue)
- {
- throw new ArgumentException("The value is not defined");
- }
- return _bool.Value;
- }
- [Pure]
- public string AsString()
- {
- if (_type != Types.String)
- {
- throw new ArgumentException("The value is not a string");
- }
- if (_string == null)
- {
- throw new ArgumentException("The value is not defined");
- }
- return _string;
- }
- [Pure]
- public double AsNumber()
- {
- if (_type != Types.Number)
- {
- throw new ArgumentException("The value is not a number");
- }
- if (!_double.HasValue)
- {
- throw new ArgumentException("The value is not defined");
- }
- return _double.Value;
- }
- public bool Equals(JsValue other)
- {
- if (_type != other._type)
- {
- return false;
- }
- switch (_type)
- {
- case Types.None:
- return false;
- case Types.Undefined:
- return true;
- case Types.Null:
- return true;
- case Types.Boolean:
- return _bool == other._bool;
- case Types.String:
- return _string == other._string;
- case Types.Number:
- return _double == other._double;
- case Types.Object:
- return _object == other._object;
- default:
- throw new ArgumentOutOfRangeException();
- }
- }
- public Types Type
- {
- get { return _type; }
- }
- private static readonly Type[] NumberTypes = { typeof(double), typeof(int), typeof(float), typeof(uint), typeof(byte), typeof(short), typeof(ushort), typeof(long), typeof(ulong) };
- public static JsValue FromObject(object value)
- {
- var s = value as string;
- if (s != null)
- {
- return s;
- }
- if (System.Array.IndexOf(NumberTypes, value.GetType()) != -1)
- {
- return (double) value;
- }
- if (value is bool)
- {
- return (bool) value;
- }
- return Undefined;
- }
- public static bool operator ==(JsValue a, JsValue b)
- {
- return a.Equals(b);
- }
- public static bool operator !=(JsValue a, JsValue b)
- {
- return !a.Equals(b);
- }
- static public implicit operator JsValue(double value)
- {
- return new JsValue(value);
- }
- static public implicit operator JsValue(bool value)
- {
- return new JsValue(value);
- }
- static public implicit operator JsValue(string value)
- {
- return new JsValue(value);
- }
- static public implicit operator JsValue(ObjectInstance value)
- {
- return new JsValue(value);
- }
- internal class JsValueDebugView
- {
- public string Value;
- public JsValueDebugView(JsValue value)
- {
- switch (value.Type)
- {
- case Types.None:
- Value = "None";
- break;
- case Types.Undefined:
- Value = "undefined";
- break;
- case Types.Null:
- Value = "null";
- break;
- case Types.Boolean:
- Value = value.AsBoolean() + " (bool)";
- break;
- case Types.String:
- Value = value.AsString() + " (string)";
- break;
- case Types.Number:
- Value = value.AsNumber() + " (number)";
- break;
- case Types.Object:
- Value = value.AsObject().GetType().Name;
- break;
- default:
- Value = "Unknown";
- break;
- }
- }
- }
- }
- public static class Undefined
- {
- public static JsValue Instance = JsValue.Undefined;
- public static string Text = "undefined";
- }
- public static class Null
- {
- public static JsValue Instance = JsValue.Null;
- public static string Text = "null";
- }
- }
|