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); }
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); }
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())); }
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); } }
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); }
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; }
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); }
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); } } }
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); }
private void DefineIntrinsicMethodProperty(Dictionary <EcmaPropertyKey, EcmaPropertyDescriptor> ht, EcmaPropertyKey name, EcmaValue sharedValue, EcmaPropertyAttributes?attributes) { ht[name] = new EcmaPropertyDescriptor(sharedValue, attributes.GetValueOrDefault(EcmaPropertyAttributes.DefaultMethodProperty)); }
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; } }
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)); }
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); } } } } } }
public bool HasProperty(EcmaValueHandle handle, EcmaPropertyKey name) { throw new InvalidOperationException(); }
public bool TrySet(EcmaValueHandle handle, EcmaPropertyKey name, EcmaValue value) { throw new InvalidOperationException(); }
public bool HasOwnProperty(EcmaValueHandle handle, EcmaPropertyKey name) { return(binder.HasOwnProperty(value, name)); }
public DataPropertyConstraint(EcmaPropertyKey name, EcmaValue?value, EcmaPropertyAttributes attributes) { this.name = name; this.attributes = attributes; this.expected = value; }
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; } } }
public EcmaValue this[EcmaValue key] { get { return(this[EcmaPropertyKey.FromValue(key)]); } set { this[EcmaPropertyKey.FromValue(key)] = value; } }
public EcmaValue this[PropertyDefinitionType type, EcmaPropertyKey key] { set { EnsureBuilder(ref properties).Add(new PropertyDefinition(type, key, value)); } }
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);
public static EcmaValue Has(EcmaValue target, EcmaValue key) { Guard.ArgumentIsObject(target); return(target.ToObject().HasProperty(EcmaPropertyKey.FromValue(key))); }
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); }
public static bool CreateMethodProperty(this RuntimeObject obj, EcmaPropertyKey propertyKey, EcmaValue value) { Guard.ArgumentNotNull(obj, "obj"); return(obj.DefineOwnProperty(propertyKey, new EcmaPropertyDescriptor(value, EcmaPropertyAttributes.DefaultMethodProperty))); }
public SharedObjectKey(object objectType, EcmaPropertyKey name) { Guard.ArgumentNotNull(objectType, "objectType"); this.ObjectType = objectType; this.Name = name; }
public bool TrySet(EcmaValueHandle handle, EcmaPropertyKey name, EcmaValue value) { return(binder.TrySet(this.value, name, value)); }
public EcmaValue this[EcmaPropertyKey index] { get { return(Get(index, this)); } set { Set(index, value, this); } }
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); }
public EcmaValue Get(EcmaPropertyKey propertyKey) { return(Get(propertyKey, this)); }
public RuntimeFunctionInjectionAttribute(WellKnownObject type, WellKnownSymbol property) { this.Type = type; this.Name = property; }