123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- using System.Collections.Generic;
- using System.Threading;
- using Jint.Native.Function;
- using Jint.Native.Object;
- using Jint.Runtime;
- using Jint.Runtime.Descriptors;
- using Jint.Runtime.Descriptors.Specialized;
- using Jint.Runtime.Environments;
- namespace Jint.Native.Argument
- {
- /// <summary>
- /// http://www.ecma-international.org/ecma-262/5.1/#sec-10.6
- /// </summary>
- public sealed class ArgumentsInstance : ObjectInstance
- {
- // cache key container for array iteration for less allocations
- private static readonly ThreadLocal<HashSet<string>> _mappedNamed = new ThreadLocal<HashSet<string>>(() => new HashSet<string>());
- private FunctionInstance _func;
- private string[] _names;
- private JsValue[] _args;
- private EnvironmentRecord _env;
- private bool _strict;
- private bool _initialized;
- internal ArgumentsInstance(Engine engine) : base(engine, objectClass: "Arguments")
- {
- }
- internal void Prepare(FunctionInstance func, string[] names, JsValue[] args, EnvironmentRecord env, bool strict)
- {
- Clear();
- _func = func;
- _names = names;
- _args = args;
- _env = env;
- _strict = strict;
- _initialized = false;
- }
- public bool Strict { get; set; }
- protected override void EnsureInitialized()
- {
- if (_initialized)
- {
- return;
- }
- _initialized = true;
- var self = this;
- var len = _args.Length;
- self.SetOwnProperty("length", new PropertyDescriptor(len, PropertyFlag.NonEnumerable));
- if (_args.Length > 0)
- {
- var map = Engine.Object.Construct(Arguments.Empty);
- var mappedNamed = _mappedNamed.Value;
- mappedNamed.Clear();
- for (var indx = 0; indx < len; indx++)
- {
- var indxStr = TypeConverter.ToString(indx);
- var val = _args[indx];
- self.SetOwnProperty(indxStr, new PropertyDescriptor(val, PropertyFlag.ConfigurableEnumerableWritable));
- if (indx < _names.Length)
- {
- var name = _names[indx];
- if (!_strict && !mappedNamed.Contains(name))
- {
- mappedNamed.Add(name);
- map.SetOwnProperty(indxStr, new ClrAccessDescriptor(_env, Engine, name));
- }
- }
- }
- // step 12
- if (mappedNamed.Count > 0)
- {
- self.ParameterMap = map;
- }
- }
- // step 13
- if (!_strict)
- {
- self.SetOwnProperty("callee", new PropertyDescriptor(_func, PropertyFlag.NonEnumerable));
- }
- // step 14
- else
- {
- var thrower = Engine.Function.ThrowTypeError;
- const PropertyFlag flags = PropertyFlag.EnumerableSet | PropertyFlag.ConfigurableSet;
- self.DefineOwnProperty("caller", new GetSetPropertyDescriptor(get: thrower, set: thrower, flags), false);
- self.DefineOwnProperty("callee", new GetSetPropertyDescriptor(get: thrower, set: thrower, flags), false);
- }
- }
- public ObjectInstance ParameterMap { get; set; }
- public override PropertyDescriptor GetOwnProperty(string propertyName)
- {
- EnsureInitialized();
- if (!Strict && !ReferenceEquals(ParameterMap, null))
- {
- var desc = base.GetOwnProperty(propertyName);
- if (desc == PropertyDescriptor.Undefined)
- {
- return desc;
- }
- var isMapped = ParameterMap.GetOwnProperty(propertyName);
- if (isMapped != PropertyDescriptor.Undefined)
- {
- desc.Value = ParameterMap.Get(propertyName);
- }
- return desc;
- }
- return base.GetOwnProperty(propertyName);
- }
- /// Implementation from ObjectInstance official specs as the one
- /// in ObjectInstance is optimized for the general case and wouldn't work
- /// for arrays
- public override void Put(string propertyName, JsValue value, bool throwOnError)
- {
- EnsureInitialized();
- if (!CanPut(propertyName))
- {
- if (throwOnError)
- {
- ExceptionHelper.ThrowTypeError(Engine);
- }
- return;
- }
- var ownDesc = GetOwnProperty(propertyName);
- if (ownDesc.IsDataDescriptor())
- {
- var valueDesc = new PropertyDescriptor(value, PropertyFlag.None);
- DefineOwnProperty(propertyName, valueDesc, throwOnError);
- return;
- }
- // property is an accessor or inherited
- var desc = GetProperty(propertyName);
- if (desc.IsAccessorDescriptor())
- {
- var setter = desc.Set.TryCast<ICallable>();
- setter.Call(this, new[] {value});
- }
- else
- {
- var newDesc = new PropertyDescriptor(value, PropertyFlag.ConfigurableEnumerableWritable);
- DefineOwnProperty(propertyName, newDesc, throwOnError);
- }
- }
- public override bool DefineOwnProperty(string propertyName, PropertyDescriptor desc, bool throwOnError)
- {
- EnsureInitialized();
- if (!Strict && !ReferenceEquals(ParameterMap, null))
- {
- var map = ParameterMap;
- var isMapped = map.GetOwnProperty(propertyName);
- var allowed = base.DefineOwnProperty(propertyName, desc, false);
- if (!allowed)
- {
- if (throwOnError)
- {
- ExceptionHelper.ThrowTypeError(Engine);
- }
- }
- if (isMapped != PropertyDescriptor.Undefined)
- {
- if (desc.IsAccessorDescriptor())
- {
- map.Delete(propertyName, false);
- }
- else
- {
- var descValue = desc.Value;
- if (!ReferenceEquals(descValue, null) && !descValue.IsUndefined())
- {
- map.Put(propertyName, descValue, throwOnError);
- }
- if (desc.WritableSet && !desc.Writable)
- {
- map.Delete(propertyName, false);
- }
- }
- }
- return true;
- }
- return base.DefineOwnProperty(propertyName, desc, throwOnError);
- }
- public override bool Delete(string propertyName, bool throwOnError)
- {
- EnsureInitialized();
- if (!Strict && !ReferenceEquals(ParameterMap, null))
- {
- var map = ParameterMap;
- var isMapped = map.GetOwnProperty(propertyName);
- var result = base.Delete(propertyName, throwOnError);
- if (result && isMapped != PropertyDescriptor.Undefined)
- {
- map.Delete(propertyName, false);
- }
- return result;
- }
- return base.Delete(propertyName, throwOnError);
- }
- }
- }
|