123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404 |
- using System.Collections.Generic;
- using System.Linq;
- using Jint.Native.Function;
- using Jint.Native.String;
- using Jint.Runtime;
- using Jint.Runtime.Descriptors;
- using Jint.Runtime.Interop;
- namespace Jint.Native.Object
- {
- public sealed class ObjectConstructor : FunctionInstance, IConstructor
- {
- private readonly Engine _engine;
- private ObjectConstructor(Engine engine) : base(engine, null, null, false)
- {
- _engine = engine;
- }
- public static ObjectConstructor CreateObjectConstructor(Engine engine)
- {
- var obj = new ObjectConstructor(engine);
- obj.Extensible = true;
- obj.PrototypeObject = ObjectPrototype.CreatePrototypeObject(engine, obj);
- obj.FastAddProperty("length", 1, false, false, false);
- obj.FastAddProperty("prototype", obj.PrototypeObject, false, false, false);
- return obj;
- }
- public void Configure()
- {
- Prototype = Engine.Function.PrototypeObject;
- FastAddProperty("getPrototypeOf", new ClrFunctionInstance(Engine, GetPrototypeOf, 1), true, false, true);
- FastAddProperty("getOwnPropertyDescriptor", new ClrFunctionInstance(Engine, GetOwnPropertyDescriptor, 2), true, false, true);
- FastAddProperty("getOwnPropertyNames", new ClrFunctionInstance(Engine, GetOwnPropertyNames, 1), true, false, true);
- FastAddProperty("create", new ClrFunctionInstance(Engine, Create, 2), true, false, true);
- FastAddProperty("defineProperty", new ClrFunctionInstance(Engine, DefineProperty, 3), true, false, true);
- FastAddProperty("defineProperties", new ClrFunctionInstance(Engine, DefineProperties, 2), true, false, true);
- FastAddProperty("seal", new ClrFunctionInstance(Engine, Seal, 1), true, false, true);
- FastAddProperty("freeze", new ClrFunctionInstance(Engine, Freeze, 1), true, false, true);
- FastAddProperty("preventExtensions", new ClrFunctionInstance(Engine, PreventExtensions, 1), true, false, true);
- FastAddProperty("isSealed", new ClrFunctionInstance(Engine, IsSealed, 1), true, false, true);
- FastAddProperty("isFrozen", new ClrFunctionInstance(Engine, IsFrozen, 1), true, false, true);
- FastAddProperty("isExtensible", new ClrFunctionInstance(Engine, IsExtensible, 1), true, false, true);
- FastAddProperty("keys", new ClrFunctionInstance(Engine, Keys, 1), true, false, true);
- }
- public ObjectPrototype PrototypeObject { get; private set; }
- /// <summary>
- /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.1.1
- /// </summary>
- /// <param name="thisObject"></param>
- /// <param name="arguments"></param>
- /// <returns></returns>
- public override JsValue Call(JsValue thisObject, JsValue[] arguments)
- {
- if (arguments.Length == 0)
- {
- return Construct(arguments);
- }
-
- if(arguments[0] == Null.Instance || arguments[0] == Undefined.Instance)
- {
- return Construct(arguments);
- }
- return TypeConverter.ToObject(_engine, arguments[0]);
- }
- /// <summary>
- /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.2.1
- /// </summary>
- /// <param name="arguments"></param>
- /// <returns></returns>
- public ObjectInstance Construct(JsValue[] arguments)
- {
- if (arguments.Length > 0)
- {
- var value = arguments[0];
- var valueObj = value.TryCast<ObjectInstance>();
- if (valueObj != null)
- {
- return valueObj;
- }
- var type = value.Type;
- if (type == Types.String || type == Types.Number || type == Types.Boolean)
- {
- return TypeConverter.ToObject(_engine, value);
- }
- }
- var obj = new ObjectInstance(_engine)
- {
- Extensible = true,
- Prototype = Engine.Object.PrototypeObject
- };
- return obj;
- }
- public JsValue GetPrototypeOf(JsValue thisObject, JsValue[] arguments)
- {
- var oArg = arguments.At(0);
- var o = oArg.TryCast<ObjectInstance>();
- if (o == null)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
-
- return o.Prototype ?? Null.Instance;
- }
- public JsValue GetOwnPropertyDescriptor(JsValue thisObject, JsValue[] arguments)
- {
- var oArg = arguments.At(0);
- var o = oArg.TryCast<ObjectInstance>();
- if (o == null)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- var p = arguments.At(1);
- var name = TypeConverter.ToString(p);
- var desc = o.GetOwnProperty(name);
- return PropertyDescriptor.FromPropertyDescriptor(Engine, desc);
- }
- public JsValue GetOwnPropertyNames(JsValue thisObject, JsValue[] arguments)
- {
- var oArg = arguments.At(0);
- var o = oArg.TryCast<ObjectInstance>();
- if (o == null)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- var array = Engine.Array.Construct(Arguments.Empty);
- var n = 0;
- var s = o as StringInstance;
- if (s != null)
- {
- for (var i = 0; i < s.PrimitiveValue.AsString().Length; i++)
- {
- array.DefineOwnProperty(n.ToString(), new PropertyDescriptor(i.ToString(), true, true, true), false);
- n++;
- }
- }
- foreach (var p in o.GetOwnProperties())
- {
- array.DefineOwnProperty(n.ToString(), new PropertyDescriptor(p.Key, true, true, true), false);
- n++;
- }
- return array;
- }
- public JsValue Create(JsValue thisObject, JsValue[] arguments)
- {
- var oArg = arguments.At(0);
- var o = oArg.TryCast<ObjectInstance>();
- if (o == null && oArg != Null.Instance)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- var obj = Engine.Object.Construct(Arguments.Empty);
- obj.Prototype = o;
- var properties = arguments.At(1);
- if (properties != Undefined.Instance)
- {
- DefineProperties(thisObject, new [] {obj, properties});
- }
- return obj;
- }
- public JsValue DefineProperty(JsValue thisObject, JsValue[] arguments)
- {
- var oArg = arguments.At(0);
- var o = oArg.TryCast<ObjectInstance>();
- if (o == null)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- var p = arguments.At(1);
- var name = TypeConverter.ToString(p);
- var attributes = arguments.At(2);
- var desc = PropertyDescriptor.ToPropertyDescriptor(Engine, attributes);
- o.DefineOwnProperty(name, desc, true);
- return o;
- }
- public JsValue DefineProperties(JsValue thisObject, JsValue[] arguments)
- {
- var oArg = arguments.At(0);
- var o = oArg.TryCast<ObjectInstance>();
- if (o == null)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- var properties = arguments.At(1);
- var props = TypeConverter.ToObject(Engine, properties);
- var descriptors = new List<KeyValuePair<string, PropertyDescriptor>>();
- foreach (var p in props.GetOwnProperties())
- {
- if (!p.Value.Enumerable.HasValue || !p.Value.Enumerable.Value)
- {
- continue;
- }
- var descObj = props.Get(p.Key);
- var desc = PropertyDescriptor.ToPropertyDescriptor(Engine, descObj);
- descriptors.Add(new KeyValuePair<string, PropertyDescriptor>(p.Key, desc));
- }
- foreach (var pair in descriptors)
- {
- o.DefineOwnProperty(pair.Key, pair.Value, true);
- }
- return o;
- }
- public JsValue Seal(JsValue thisObject, JsValue[] arguments)
- {
- var oArg = arguments.At(0);
- var o = oArg.TryCast<ObjectInstance>();
- if (o == null)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- foreach (var prop in o.GetOwnProperties())
- {
- if (prop.Value.Configurable.HasValue && prop.Value.Configurable.Value)
- {
- prop.Value.Configurable = false;
- }
- o.DefineOwnProperty(prop.Key, prop.Value, true);
- }
- o.Extensible = false;
- return o;
- }
- public JsValue Freeze(JsValue thisObject, JsValue[] arguments)
- {
- var oArg = arguments.At(0);
- var o = oArg.TryCast<ObjectInstance>();
- if (o == null)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- var keys = o.GetOwnProperties().Select(x => x.Key);
- foreach (var p in keys)
- {
- var desc = o.GetOwnProperty(p);
- if (desc.IsDataDescriptor())
- {
- if (desc.Writable.HasValue && desc.Writable.Value)
- {
- desc.Writable = false;
- }
- }
- if (desc.Configurable.HasValue && desc.Configurable.Value)
- {
- desc.Configurable = false;
- }
- o.DefineOwnProperty(p, desc, true);
- }
-
- o.Extensible = false;
-
- return o;
- }
- public JsValue PreventExtensions(JsValue thisObject, JsValue[] arguments)
- {
- var oArg = arguments.At(0);
- var o = oArg.TryCast<ObjectInstance>();
- if (o == null)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- o.Extensible = false;
- return o;
- }
- public JsValue IsSealed(JsValue thisObject, JsValue[] arguments)
- {
- var oArg = arguments.At(0);
- var o = oArg.TryCast<ObjectInstance>();
- if (o == null)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- foreach (var prop in o.GetOwnProperties())
- {
- if (prop.Value.Configurable.Value == true)
- {
- return false;
- }
- }
- if (o.Extensible == false)
- {
- return true;
- }
- return false;
- }
- public JsValue IsFrozen(JsValue thisObject, JsValue[] arguments)
- {
- var oArg = arguments.At(0);
- var o = oArg.TryCast<ObjectInstance>();
- if (o == null)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- foreach (var p in o.GetOwnProperties().Select(x => x.Key))
- {
- var desc = o.GetOwnProperty(p);
- if (desc.IsDataDescriptor())
- {
- if (desc.Writable.HasValue && desc.Writable.Value)
- {
- return false;
- }
- }
- if (desc.Configurable.HasValue && desc.Configurable.Value)
- {
- return false;
- }
- }
- if (o.Extensible == false)
- {
- return true;
- }
- return false;
- }
- public JsValue IsExtensible(JsValue thisObject, JsValue[] arguments)
- {
- var oArg = arguments.At(0);
- var o = oArg.TryCast<ObjectInstance>();
- if (o == null)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- return o.Extensible;
- }
- public JsValue Keys(JsValue thisObject, JsValue[] arguments)
- {
- var oArg = arguments.At(0);
- var o = oArg.TryCast<ObjectInstance>();
- if (o == null)
- {
- throw new JavaScriptException(Engine.TypeError);
- }
- var enumerableProperties = o.GetOwnProperties()
- .Where(x => x.Value.Enumerable.HasValue && x.Value.Enumerable.Value)
- .ToArray();
- var n = enumerableProperties.Length;
- var array = Engine.Array.Construct(new JsValue[] {n});
- var index = 0;
- foreach (var prop in enumerableProperties)
- {
- var p = prop.Key;
- array.DefineOwnProperty(
- TypeConverter.ToString(index),
- new PropertyDescriptor(p, true, true, true),
- false);
- index++;
- }
- return array;
- }
- }
- }
|