Exemplo n.º 1
0
        protected bool GetOption(EcmaPropertyKey propertyKey, bool defaultValue)
        {
            if (parsedOptions.ContainsKey(propertyKey) && parsedOptions[propertyKey] is bool savedValue)
            {
                return(savedValue);
            }
            if (options == null)
            {
                return(defaultValue);
            }
            EcmaValue value       = options[propertyKey];
            bool      parsedValue = value == default ? defaultValue : value.ToBoolean();

            parsedOptions[propertyKey] = parsedValue;
            return(parsedValue);
        }
Exemplo n.º 2
0
        protected string GetOption(EcmaPropertyKey propertyKey, string defaultValue)
        {
            if (parsedOptions.ContainsKey(propertyKey))
            {
                return(parsedOptions[propertyKey] as string);
            }
            if (options == null)
            {
                return(defaultValue);
            }
            EcmaValue value       = options[propertyKey];
            string    parsedValue = value != default ? options[propertyKey].ToStringOrThrow() : defaultValue;

            parsedOptions[propertyKey] = parsedValue;
            return(parsedValue);
        }
Exemplo n.º 3
0
 public EcmaArray ToPartArray(EcmaPropertyKey annotationProperty, string[] annotations)
 {
     Guard.ArgumentNotNull(annotations, "annotations");
     if (annotations.Length != parts.Length)
     {
         throw new ArgumentException("Supplied array must have the same length of part array", "unitAnnotations");
     }
     return(new EcmaArray(parts.Select((v, i) => {
         RuntimeObject obj = v.ToValue().ToObject();
         if (annotations[i] != null)
         {
             obj.CreateDataPropertyOrThrow(annotationProperty, annotations[i]);
         }
         return obj.ToValue();
     }).ToArray()));
 }
Exemplo n.º 4
0
 protected virtual void WriteProperty(EcmaPropertyKey propertyKey, EcmaPropertyDescriptor descriptor)
 {
     if (this.LastToken != InspectorTokenType.ObjectStart && this.LastToken != InspectorTokenType.ArrayStart)
     {
         WriteToken(InspectorTokenType.EntrySeparator);
     }
     WritePropertyName(propertyKey, descriptor);
     WriteToken(InspectorTokenType.PropertyNameValueSeparator);
     if (descriptor.IsAccessorDescriptor)
     {
         WriteToken(InspectorTokenType.UnexpandedAccessor);
     }
     else
     {
         WritePropertyValue(propertyKey, descriptor);
     }
 }
Exemplo n.º 5
0
        public override EcmaPropertyDescriptor GetOwnProperty(EcmaPropertyKey propertyKey)
        {
            EcmaPropertyDescriptor result = base.GetOwnProperty(propertyKey);

            if (result != null)
            {
                if (propertyKey == WellKnownProperty.Arguments)
                {
                    return(new EcmaPropertyDescriptor(GetCurrentArguments(), EcmaPropertyAttributes.None));
                }
                if (propertyKey == WellKnownProperty.Caller)
                {
                    return(new EcmaPropertyDescriptor(GetCurrentCallee(), EcmaPropertyAttributes.None));
                }
            }
            return(result);
        }
Exemplo n.º 6
0
 public void OverrideProperty(Enum objectType, EcmaPropertyKey propertyKey, RuntimeObject obj)
 {
     Guard.ArgumentNotNull(objectType, "objectType");
     Guard.ArgumentNotNull(obj, "obj");
     if (objectType.GetType() != this.EnumType)
     {
         throw new ArgumentException(String.Format("Value must be of type {0}", this.EnumType.FullName), "objectType");
     }
     if (obj.Realm != this.Realm)
     {
         throw new ArgumentException("Object must be created in the shared realm", "obj");
     }
     if (!overridables.TryGetValue(new SharedObjectKey(objectType, propertyKey), out int index))
     {
         throw new InvalidOperationException(String.Format("Property {0} is not overridable", propertyKey));
     }
     runtimeObjects[index] = obj;
 }
Exemplo n.º 7
0
        protected T GetOption <T>(EcmaPropertyKey propertyKey, T defaultValue) where T : struct, Enum
        {
            if (parsedOptions.ContainsKey(propertyKey) && parsedOptions[propertyKey] is T savedValue)
            {
                return(savedValue);
            }
            if (options == null)
            {
                return(defaultValue);
            }
            EcmaValue value = options[propertyKey];

            if (value == default)
            {
                parsedOptions[propertyKey] = defaultValue;
                return(defaultValue);
            }
            T parsedValue = ParseEnum <T>(value.ToStringOrThrow());

            parsedOptions[propertyKey] = parsedValue;
            return(parsedValue);
        }
Exemplo n.º 8
0
 protected override void WritePropertyValue(EcmaPropertyKey propertyKey, EcmaPropertyDescriptor descriptor)
 {
     if (descriptor.Value.Type != EcmaValueType.Object)
     {
         base.WritePropertyValue(propertyKey, descriptor);
     }
     else
     {
         RuntimeObject obj = descriptor.Value.ToObject();
         if (stack.Count == maxDepth || stack.Contains(obj))
         {
             WriteToken(InspectorTokenType.UnexpandedAccessor);
         }
         else if (propertyKey == "__proto__")
         {
             singleLine.Serialize(descriptor.Value);
         }
         else
         {
             base.WritePropertyValue(propertyKey, descriptor);
         }
     }
 }
Exemplo n.º 9
0
        public override bool DefineOwnProperty(EcmaPropertyKey propertyKey, EcmaPropertyDescriptor descriptor)
        {
            RuntimeObject target = ThrowIfProxyRevoked();
            RuntimeObject trap   = handler.GetMethod(WellKnownProperty.DefineProperty);

            if (trap == null)
            {
                return(target.DefineOwnProperty(propertyKey, descriptor));
            }
            if (!(bool)trap.Call(handler, target, propertyKey.ToValue(), descriptor.ToValue()))
            {
                return(false);
            }
            bool settingUnconfigurable     = descriptor.Configurable == false;
            EcmaPropertyDescriptor current = target.GetOwnProperty(propertyKey);

            if (current == null)
            {
                if (!target.IsExtensible || settingUnconfigurable)
                {
                    throw new EcmaTypeErrorException(InternalString.Error.InvalidTrapResult);
                }
            }
            else
            {
                if (!EcmaPropertyDescriptor.ValidateAndApplyPropertyDescriptor(ref descriptor, current, !target.IsExtensible))
                {
                    throw new EcmaTypeErrorException(InternalString.Error.InvalidTrapResult);
                }
                if (settingUnconfigurable && current.Configurable)
                {
                    throw new EcmaTypeErrorException(InternalString.Error.InvalidTrapResult);
                }
            }
            return(true);
        }
Exemplo n.º 10
0
 private void DefineIntrinsicMethodProperty(Dictionary <EcmaPropertyKey, EcmaPropertyDescriptor> ht, EcmaPropertyKey name, EcmaValue sharedValue, EcmaPropertyAttributes?attributes)
 {
     ht[name] = new EcmaPropertyDescriptor(sharedValue, attributes.GetValueOrDefault(EcmaPropertyAttributes.DefaultMethodProperty));
 }
Exemplo n.º 11
0
        private void DefineIntrinsicAccessorProperty(Dictionary <EcmaPropertyKey, EcmaPropertyDescriptor> ht, EcmaPropertyKey name, EcmaValue sharedValue, EcmaPropertyAttributes?attributes, bool isGetter)
        {
            attributes = attributes.GetValueOrDefault(EcmaPropertyAttributes.DefaultDataProperty);
            if (name.IsSymbol)
            {
                attributes &= ~EcmaPropertyAttributes.Enumerable;
            }
            EcmaPropertyDescriptor descriptor;

            if (!ht.TryGetValue(name, out descriptor))
            {
                descriptor = new EcmaPropertyDescriptor(attributes.Value);
                ht[name]   = descriptor;
            }
            if (isGetter)
            {
                descriptor.Get = sharedValue;
            }
            else
            {
                descriptor.Set = sharedValue;
            }
        }
Exemplo n.º 12
0
 private void DefineIntrinsicDataProperty(Dictionary <EcmaPropertyKey, EcmaPropertyDescriptor> ht, EcmaPropertyKey name, EcmaValue value, EcmaPropertyAttributes?attributes)
 {
     ht[name] = new EcmaPropertyDescriptor(value, attributes.GetValueOrDefault(EcmaPropertyAttributes.DefaultDataProperty) & (EcmaPropertyAttributes.Configurable | EcmaPropertyAttributes.Writable));
 }
Exemplo n.º 13
0
        private void DefineIntrinsicObjectFromType(Type type, IntrinsicObjectAttribute typeAttr)
        {
            int objectIndex = ToValidIndex((Enum)typeAttr.ObjectType);
            Dictionary <EcmaPropertyKey, EcmaPropertyDescriptor> ht = properties[objectIndex];
            SharedObjectHandle handle = new SharedObjectHandle(this.Container.ID, objectIndex);

            if (IsValidReference(typeAttr.Prototype, out SharedObjectHandle protoHandle))
            {
                RuntimeObject thisObj = EnsureObject(objectIndex);
                thisObj.SetPrototypeOf(this.Realm.GetRuntimeObject(protoHandle));
            }
            if (typeAttr.Global)
            {
                globals[typeAttr.Name ?? type.Name] = CreateSharedObjectDescriptor(handle, EcmaPropertyAttributes.Configurable | EcmaPropertyAttributes.Writable);
            }
            foreach (MemberInfo member in type.GetMembers())
            {
                // special handling if the type defines an instrinsic contructor
                // replace the default object created from EnsureWellKnownObject to InstrincFunction
                if (member.HasAttribute(out IntrinsicConstructorAttribute ctorAttr))
                {
                    string ctorName = ctorAttr.Name ?? member.Name;
                    runtimeObjects[objectIndex] = CreateIntrinsicFunction(ctorName, (MethodInfo)member, ctorAttr.SuperClass as Enum, WellKnownObject.Global, ctorName);
                    if (IsValidReference(ctorAttr.Prototype, out SharedObjectHandle p1))
                    {
                        Dictionary <EcmaPropertyKey, EcmaPropertyDescriptor> hp = properties[ToValidIndex((Enum)ctorAttr.Prototype)];
                        ht[WellKnownProperty.Prototype]   = CreateSharedObjectDescriptor(p1, EcmaPropertyAttributes.None);
                        hp[WellKnownProperty.Constructor] = CreateSharedObjectDescriptor(handle, EcmaPropertyAttributes.Configurable | EcmaPropertyAttributes.Writable);
                    }
                    if (ctorAttr.Global)
                    {
                        globals[ctorName] = CreateSharedObjectDescriptor(handle, EcmaPropertyAttributes.DefaultMethodProperty);
                    }
                    continue;
                }

                object[] propAttrs = member.GetCustomAttributes(typeof(IntrinsicMemberAttribute), false);
                if (propAttrs.Length > 0)
                {
                    EcmaValue sharedValue = default;
                    if (member.HasAttribute(out AliasOfAttribute aliasOf) && aliasOf.ObjectType is Enum aliasOfType)
                    {
                        EcmaPropertyKey aliasOfKey = aliasOf.Name != null ? (EcmaPropertyKey)aliasOf.Name : aliasOf.Symbol;
                        if (!properties[ToValidIndex(aliasOfType)].TryGetValue(aliasOfKey, out EcmaPropertyDescriptor descriptor))
                        {
                            // for sake of simplicity the aliased target should be defined on intrinsic object with smaller WellKnownObject enum value
                            // to avoid the need of topological sort
                            throw new InvalidOperationException();
                        }
                        sharedValue = descriptor.Value;
                    }
                    if (sharedValue == default)
                    {
                        switch (member.MemberType)
                        {
                        case MemberTypes.Method:
                            IntrinsicMemberAttribute propAttr = (IntrinsicMemberAttribute)propAttrs[0];
                            EcmaPropertyKey          name     = GetNameFromMember(propAttr, member);
                            string runtimeName = (propAttr.Getter ? "get " : propAttr.Setter ? "set " : "") + (name.IsSymbol ? "[" + name.Symbol.Description + "]" : name.Name);
                            sharedValue = this.Container.Add(CreateIntrinsicFunction(runtimeName, (MethodInfo)member, null, typeAttr.ObjectType as Enum, name)).ToValue();
                            if (propAttr.Overridable)
                            {
                                overridables.Add(new SharedObjectKey(typeAttr.ObjectType, name), sharedValue.ToInt32() & 0xFFFF);
                            }
                            break;

                        case MemberTypes.Field:
                            object fieldValue = ((FieldInfo)member).GetValue(null);
                            sharedValue = IsValidReference(fieldValue, out SharedObjectHandle p1) ? p1.ToValue() : new EcmaValue(fieldValue);
                            break;

                        case MemberTypes.Property:
                            object propertyValue = ((PropertyInfo)member).GetValue(null, null);
                            sharedValue = IsValidReference(propertyValue, out SharedObjectHandle p2) ? p2.ToValue() : new EcmaValue(propertyValue);
                            break;
                        }
                    }
                    foreach (IntrinsicMemberAttribute propAttr in propAttrs)
                    {
                        EcmaPropertyKey name = GetNameFromMember(propAttr, member);
                        if (propAttr.Getter)
                        {
                            DefineIntrinsicAccessorProperty(ht, name, sharedValue, propAttr.Attributes, true);
                        }
                        else if (propAttr.Setter)
                        {
                            DefineIntrinsicAccessorProperty(ht, name, sharedValue, propAttr.Attributes, false);
                        }
                        else if (member.MemberType != MemberTypes.Method)
                        {
                            DefineIntrinsicDataProperty(ht, name, sharedValue, propAttr.Attributes);
                        }
                        else
                        {
                            DefineIntrinsicMethodProperty(ht, name, sharedValue, propAttr.Attributes);
                            if (propAttr.Global)
                            {
                                DefineIntrinsicMethodProperty(globals, name, sharedValue, propAttr.Attributes);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 14
0
 public bool HasProperty(EcmaValueHandle handle, EcmaPropertyKey name)
 {
     throw new InvalidOperationException();
 }
Exemplo n.º 15
0
 public bool TrySet(EcmaValueHandle handle, EcmaPropertyKey name, EcmaValue value)
 {
     throw new InvalidOperationException();
 }
Exemplo n.º 16
0
 public bool HasOwnProperty(EcmaValueHandle handle, EcmaPropertyKey name)
 {
     return(binder.HasOwnProperty(value, name));
 }
Exemplo n.º 17
0
 public DataPropertyConstraint(EcmaPropertyKey name, EcmaValue?value, EcmaPropertyAttributes attributes)
 {
     this.name       = name;
     this.attributes = attributes;
     this.expected   = value;
 }
Exemplo n.º 18
0
 public EcmaValue this[IStaticModifier staticModifier, PropertyDefinitionType type, EcmaPropertyKey key] {
     set {
         Guard.ArgumentNotNull(staticModifier, "staticModifier");
         if (staticModifier.IsStatic)
         {
             EnsureBuilder(ref staticProperties).Add(new PropertyDefinition(type, key, value));
         }
         else
         {
             this[type, key] = value;
         }
     }
 }
Exemplo n.º 19
0
 public EcmaValue this[EcmaValue key] {
     get { return(this[EcmaPropertyKey.FromValue(key)]); }
     set { this[EcmaPropertyKey.FromValue(key)] = value; }
 }
Exemplo n.º 20
0
 public EcmaValue this[PropertyDefinitionType type, EcmaPropertyKey key] {
     set { EnsureBuilder(ref properties).Add(new PropertyDefinition(type, key, value)); }
 }
Exemplo n.º 21
0
        public static EcmaValue GetOwnPropertyDescriptor(EcmaValue target, EcmaValue key)
        {
            Guard.ArgumentIsObject(target);
            EcmaPropertyDescriptor descriptor = target.ToObject().GetOwnProperty(EcmaPropertyKey.FromValue(key));

            return(descriptor != null?descriptor.ToValue() : default);
Exemplo n.º 22
0
 public static EcmaValue Has(EcmaValue target, EcmaValue key)
 {
     Guard.ArgumentIsObject(target);
     return(target.ToObject().HasProperty(EcmaPropertyKey.FromValue(key)));
 }
Exemplo n.º 23
0
        private RuntimeFunction CreateIntrinsicFunction(string name, MethodInfo method, Enum superClass, Enum parentObjectType, EcmaPropertyKey propertyKey)
        {
            RuntimeFunction fn = parentObjectType is WellKnownObject knownObject ? new IntrinsicFunction(name, method, knownObject, propertyKey) : new NativeRuntimeFunction(name, method, true);

            if (IsValidReference(superClass, out SharedObjectHandle handle))
            {
                fn.SetPrototypeOf(this.Realm.GetRuntimeObject(handle));
            }
            return(fn);
        }
Exemplo n.º 24
0
 public static bool CreateMethodProperty(this RuntimeObject obj, EcmaPropertyKey propertyKey, EcmaValue value)
 {
     Guard.ArgumentNotNull(obj, "obj");
     return(obj.DefineOwnProperty(propertyKey, new EcmaPropertyDescriptor(value, EcmaPropertyAttributes.DefaultMethodProperty)));
 }
Exemplo n.º 25
0
 public SharedObjectKey(object objectType, EcmaPropertyKey name)
 {
     Guard.ArgumentNotNull(objectType, "objectType");
     this.ObjectType = objectType;
     this.Name       = name;
 }
Exemplo n.º 26
0
 public bool TrySet(EcmaValueHandle handle, EcmaPropertyKey name, EcmaValue value)
 {
     return(binder.TrySet(this.value, name, value));
 }
Exemplo n.º 27
0
 public EcmaValue this[EcmaPropertyKey index] {
     get { return(Get(index, this)); }
     set { Set(index, value, this); }
 }
Exemplo n.º 28
0
        public static bool IsIntrinsicFunction(this RuntimeObject obj, WellKnownObject type, EcmaPropertyKey name)
        {
            if (obj is IntrinsicFunction fn)
            {
                return(fn.IsIntrinsicFunction(type, name));
            }
            Guard.ArgumentNotNull(obj, "obj");
            if (!obj.IsCallable)
            {
                return(false);
            }
            RuntimeObject sourceObj = RuntimeRealm.SharedRealm.GetRuntimeObject(type).GetMethod(name);

            return(obj.Realm.ResolveRuntimeObjectInRealm(sourceObj) == obj);
        }
Exemplo n.º 29
0
 public EcmaValue Get(EcmaPropertyKey propertyKey)
 {
     return(Get(propertyKey, this));
 }
Exemplo n.º 30
0
 public RuntimeFunctionInjectionAttribute(WellKnownObject type, WellKnownSymbol property)
 {
     this.Type = type;
     this.Name = property;
 }