示例#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);
        }
示例#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);
        }
示例#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()));
 }
示例#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);
     }
 }
示例#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);
        }
示例#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;
 }
示例#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);
        }
示例#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);
         }
     }
 }
示例#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);
        }
示例#10
0
 private void DefineIntrinsicMethodProperty(Dictionary <EcmaPropertyKey, EcmaPropertyDescriptor> ht, EcmaPropertyKey name, EcmaValue sharedValue, EcmaPropertyAttributes?attributes)
 {
     ht[name] = new EcmaPropertyDescriptor(sharedValue, attributes.GetValueOrDefault(EcmaPropertyAttributes.DefaultMethodProperty));
 }
示例#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;
            }
        }
示例#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));
 }
示例#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);
                            }
                        }
                    }
                }
            }
        }
示例#14
0
 public bool HasProperty(EcmaValueHandle handle, EcmaPropertyKey name)
 {
     throw new InvalidOperationException();
 }
示例#15
0
 public bool TrySet(EcmaValueHandle handle, EcmaPropertyKey name, EcmaValue value)
 {
     throw new InvalidOperationException();
 }
示例#16
0
 public bool HasOwnProperty(EcmaValueHandle handle, EcmaPropertyKey name)
 {
     return(binder.HasOwnProperty(value, name));
 }
示例#17
0
 public DataPropertyConstraint(EcmaPropertyKey name, EcmaValue?value, EcmaPropertyAttributes attributes)
 {
     this.name       = name;
     this.attributes = attributes;
     this.expected   = value;
 }
示例#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;
         }
     }
 }
示例#19
0
 public EcmaValue this[EcmaValue key] {
     get { return(this[EcmaPropertyKey.FromValue(key)]); }
     set { this[EcmaPropertyKey.FromValue(key)] = value; }
 }
示例#20
0
 public EcmaValue this[PropertyDefinitionType type, EcmaPropertyKey key] {
     set { EnsureBuilder(ref properties).Add(new PropertyDefinition(type, key, value)); }
 }
示例#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);
示例#22
0
 public static EcmaValue Has(EcmaValue target, EcmaValue key)
 {
     Guard.ArgumentIsObject(target);
     return(target.ToObject().HasProperty(EcmaPropertyKey.FromValue(key)));
 }
示例#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);
        }
示例#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)));
 }
示例#25
0
 public SharedObjectKey(object objectType, EcmaPropertyKey name)
 {
     Guard.ArgumentNotNull(objectType, "objectType");
     this.ObjectType = objectType;
     this.Name       = name;
 }
示例#26
0
 public bool TrySet(EcmaValueHandle handle, EcmaPropertyKey name, EcmaValue value)
 {
     return(binder.TrySet(this.value, name, value));
 }
示例#27
0
 public EcmaValue this[EcmaPropertyKey index] {
     get { return(Get(index, this)); }
     set { Set(index, value, this); }
 }
示例#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);
        }
示例#29
0
 public EcmaValue Get(EcmaPropertyKey propertyKey)
 {
     return(Get(propertyKey, this));
 }
示例#30
0
 public RuntimeFunctionInjectionAttribute(WellKnownObject type, WellKnownSymbol property)
 {
     this.Type = type;
     this.Name = property;
 }