123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562 |
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Diagnostics.Contracts;
- using System.Runtime.CompilerServices;
- using System.Threading;
- using Jint.Native.Array;
- using Jint.Native.Date;
- using Jint.Native.Iterator;
- using Jint.Native.Object;
- using Jint.Native.RegExp;
- using Jint.Native.Symbol;
- using Jint.Runtime;
- using Jint.Runtime.Descriptors;
- using Jint.Runtime.Interop;
- namespace Jint.Native
- {
- [DebuggerTypeProxy(typeof(JsValueDebugView))]
- public abstract class JsValue : IEquatable<JsValue>
- {
- public static readonly JsValue Undefined = new JsUndefined();
- public static readonly JsValue Null = new JsNull();
- internal readonly Types _type;
- protected JsValue(Types type)
- {
- _type = type;
- }
- [Pure]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool IsPrimitive()
- {
- return _type != Types.Object && _type != Types.None;
- }
- [Pure]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool IsUndefined()
- {
- return _type == Types.Undefined;
- }
- [Pure]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal bool IsNullOrUndefined()
- {
- return _type < Types.Boolean;
- }
- [Pure]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool IsArray()
- {
- return this is ArrayInstance;
- }
- [Pure]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool IsDate()
- {
- return this is DateInstance;
- }
- [Pure]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool IsRegExp()
- {
- return this is RegExpInstance;
- }
- [Pure]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool IsObject()
- {
- return _type == Types.Object;
- }
- [Pure]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool IsString()
- {
- return _type == Types.String;
- }
- [Pure]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool IsNumber()
- {
- return _type == Types.Number;
- }
- [Pure]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool IsBoolean()
- {
- return _type == Types.Boolean;
- }
- [Pure]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool IsNull()
- {
- return _type == Types.Null;
- }
- [Pure]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool IsCompletion()
- {
- return _type == Types.Completion;
- }
- [Pure]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool IsSymbol()
- {
- return _type == Types.Symbol;
- }
- [Pure]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public ObjectInstance AsObject()
- {
- if (!IsObject())
- {
- ExceptionHelper.ThrowArgumentException("The value is not an object");
- }
- return this as ObjectInstance;
- }
- [Pure]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public TInstance AsInstance<TInstance>() where TInstance : class
- {
- if (!IsObject())
- {
- ExceptionHelper.ThrowArgumentException("The value is not an object");
- }
- return this as TInstance;
- }
- [Pure]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public ArrayInstance AsArray()
- {
- if (!IsArray())
- {
- ExceptionHelper.ThrowArgumentException("The value is not an array");
- }
- return this as ArrayInstance;
- }
- [Pure]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal IIterator GetIterator(Engine engine)
- {
- if (!TryGetIterator(engine, out var iterator))
- {
- return ExceptionHelper.ThrowTypeError<IIterator>(engine, "The value is not iterable");
- }
- return iterator;
- }
-
- [Pure]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal bool TryGetIterator(Engine engine, out IIterator iterator)
- {
- var objectInstance = TypeConverter.ToObject(engine, this);
- if (!objectInstance.TryGetValue(GlobalSymbolRegistry.Iterator._value, out var value)
- || !(value is ICallable callable))
- {
- iterator = null;
- return false;
- }
- var obj = callable.Call(this, Arguments.Empty) as ObjectInstance
- ?? ExceptionHelper.ThrowTypeError<ObjectInstance>(engine, "Result of the Symbol.iterator method is not an object");
- if (obj is IIterator i)
- {
- iterator = i;
- }
- else
- {
- iterator = new IteratorInstance.ObjectWrapper(obj);
- }
- return true;
- }
- [Pure]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public DateInstance AsDate()
- {
- if (!IsDate())
- {
- ExceptionHelper.ThrowArgumentException("The value is not a date");
- }
- return this as DateInstance;
- }
- [Pure]
- public RegExpInstance AsRegExp()
- {
- if (!IsRegExp())
- {
- ExceptionHelper.ThrowArgumentException("The value is not a regex");
- }
- return this as RegExpInstance;
- }
- [Pure]
- public Completion AsCompletion()
- {
- if (_type != Types.Completion)
- {
- ExceptionHelper.ThrowArgumentException("The value is not a completion record");
- }
- // TODO not implemented
- return new Completion(CompletionType.Normal, Native.Undefined.Instance, null, default);
- }
- [Pure]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public T TryCast<T>() where T : class
- {
- return this as T;
- }
- [Pure]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public T TryCast<T>(Action<JsValue> fail) where T : class
- {
- if (this is T o)
- {
- return o;
- }
- fail.Invoke(this);
- return null;
- }
- [Pure]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public bool Is<T>()
- {
- return IsObject() && this is T;
- }
- [Pure]
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public T As<T>() where T : ObjectInstance
- {
- if (IsObject())
- {
- return this as T;
- }
- return null;
- }
- // ReSharper disable once ConvertToAutoPropertyWhenPossible // PERF
- public Types Type
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- get { return _type; }
- }
- /// <summary>
- /// Creates a valid <see cref="JsValue"/> instance from any <see cref="Object"/> instance
- /// </summary>
- /// <param name="engine"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- public static JsValue FromObject(Engine engine, object value)
- {
- if (value == null)
- {
- return Null;
- }
- if (value is JsValue jsValue)
- {
- return jsValue;
- }
- var converters = engine.Options._ObjectConverters;
- var convertersCount = converters.Count;
- for (var i = 0; i < convertersCount; i++)
- {
- var converter = converters[i];
- if (converter.TryConvert(engine, value, out var result))
- {
- return result;
- }
- }
- var valueType = value.GetType();
- var typeMappers = Engine.TypeMappers;
- if (typeMappers.TryGetValue(valueType, out var typeMapper))
- {
- return typeMapper(engine, value);
- }
- var type = value as Type;
- if (type != null)
- {
- var typeReference = TypeReference.CreateTypeReference(engine, type);
- return typeReference;
- }
- if (value is System.Array a)
- {
- // racy, we don't care, worst case we'll catch up later
- Interlocked.CompareExchange(ref Engine.TypeMappers, new Dictionary<Type, Func<Engine, object, JsValue>>(typeMappers)
- {
- [valueType] = Convert
- }, typeMappers);
- return Convert(engine, a);
- }
- if (value is Delegate d)
- {
- return new DelegateWrapper(engine, d);
- }
- Type t = value.GetType();
- if (t.IsEnum)
- {
- Type ut = Enum.GetUnderlyingType(t);
- if (ut == typeof(ulong))
- return JsNumber.Create(System.Convert.ToDouble(value));
- if (ut == typeof(uint) || ut == typeof(long))
- return JsNumber.Create(System.Convert.ToInt64(value));
- return JsNumber.Create(System.Convert.ToInt32(value));
- }
- // if no known type could be guessed, wrap it as an ObjectInstance
- var h = engine.Options._WrapObjectHandler;
- ObjectInstance o = h != null ? h(value) : null;
- return o ?? new ObjectWrapper(engine, value);
- }
- private static JsValue Convert(Engine e, object v)
- {
- var array = (System.Array) v;
- var arrayLength = (uint) array.Length;
- var jsArray = new ArrayInstance(e, arrayLength);
- jsArray.Prototype = e.Array.PrototypeObject;
- jsArray.Extensible = true;
- for (uint i = 0; i < arrayLength; ++i)
- {
- var jsItem = FromObject(e, array.GetValue(i));
- jsArray.WriteArrayValue(i, new PropertyDescriptor(jsItem, PropertyFlag.ConfigurableEnumerableWritable));
- }
- jsArray.SetOwnProperty(KnownKeys.Length, new PropertyDescriptor(arrayLength, PropertyFlag.OnlyWritable));
- return jsArray;
- }
- /// <summary>
- /// Converts a <see cref="JsValue"/> to its underlying CLR value.
- /// </summary>
- /// <returns>The underlying CLR value of the <see cref="JsValue"/> instance.</returns>
- public abstract object ToObject();
- /// <summary>
- /// Invoke the current value as function.
- /// </summary>
- /// <param name="arguments">The arguments of the function call.</param>
- /// <returns>The value returned by the function call.</returns>
- public JsValue Invoke(params JsValue[] arguments)
- {
- return Invoke(Undefined, arguments);
- }
- /// <summary>
- /// Invoke the current value as function.
- /// </summary>
- /// <param name="thisObj">The this value inside the function call.</param>
- /// <param name="arguments">The arguments of the function call.</param>
- /// <returns>The value returned by the function call.</returns>
- public JsValue Invoke(JsValue thisObj, JsValue[] arguments)
- {
- var callable = this as ICallable ?? ExceptionHelper.ThrowArgumentException<ICallable>("Can only invoke functions");
- return callable.Call(thisObj, arguments);
- }
- public static bool ReturnOnAbruptCompletion(ref JsValue argument)
- {
- if (!argument.IsCompletion())
- {
- return false;
- }
- var completion = argument.AsCompletion();
- if (completion.IsAbrupt())
- {
- return true;
- }
- argument = completion.Value;
- return false;
- }
- public override string ToString()
- {
- return "None";
- }
- public static bool operator ==(JsValue a, JsValue b)
- {
- if ((object)a == null)
- {
- if ((object)b == null)
- {
- return true;
- }
- return false;
- }
- if ((object)b == null)
- {
- return false;
- }
- return a.Equals(b);
- }
- public static bool operator !=(JsValue a, JsValue b)
- {
- if ((object)a == null)
- {
- if ((object)b == null)
- {
- return false;
- }
- return true;
- }
- if ((object)b == null)
- {
- return true;
- }
- return !a.Equals(b);
- }
- static public implicit operator JsValue(char value)
- {
- return JsString.Create(value);
- }
- static public implicit operator JsValue(int value)
- {
- return JsNumber.Create(value);
- }
- static public implicit operator JsValue(uint value)
- {
- return JsNumber.Create(value);
- }
- static public implicit operator JsValue(double value)
- {
- return JsNumber.Create(value);
- }
- public static implicit operator JsValue(bool value)
- {
- return value ? JsBoolean.True : JsBoolean.False;
- }
- public static implicit operator JsValue(string value)
- {
- if (value == null)
- {
- return Null;
- }
-
- return JsString.Create(value);
- }
- public override bool Equals(object obj)
- {
- if (ReferenceEquals(null, obj))
- {
- return false;
- }
- if (ReferenceEquals(this, obj))
- {
- return true;
- }
- return obj is JsValue value && Equals(value);
- }
- public abstract bool Equals(JsValue other);
- public override int GetHashCode()
- {
- return _type.GetHashCode();
- }
- 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 = ((JsBoolean) value)._value + " (bool)";
- break;
- case Types.String:
- Value = value.AsStringWithoutTypeCheck() + " (string)";
- break;
- case Types.Number:
- Value = ((JsNumber) value)._value + " (number)";
- break;
- case Types.Object:
- Value = value.AsObject().GetType().Name;
- break;
- case Types.Symbol:
- Value = value.AsSymbol() + " (symbol)";
- break;
- default:
- Value = "Unknown";
- break;
- }
- }
- }
- }
- }
|