123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- using System.Collections.Generic;
- using Jint.Native.Object;
- using Jint.Runtime;
- using Jint.Runtime.Descriptors;
- namespace Jint.Native.String
- {
- public class StringInstance : ObjectInstance, IPrimitiveInstance
- {
- internal PropertyDescriptor _length;
- public StringInstance(Engine engine)
- : base(engine, ObjectClass.String)
- {
- }
- Types IPrimitiveInstance.Type => Types.String;
- JsValue IPrimitiveInstance.PrimitiveValue => StringData;
- public JsString StringData { get; internal init; }
- private static bool IsInt32(double d, out int intValue)
- {
- if (d >= int.MinValue && d <= int.MaxValue)
- {
- intValue = (int) d;
- return intValue == d;
- }
- intValue = 0;
- return false;
- }
- public override PropertyDescriptor GetOwnProperty(JsValue property)
- {
- if (property == CommonProperties.Infinity)
- {
- return PropertyDescriptor.Undefined;
- }
- if (property == CommonProperties.Length)
- {
- return _length ?? PropertyDescriptor.Undefined;
- }
- var desc = base.GetOwnProperty(property);
- if (desc != PropertyDescriptor.Undefined)
- {
- return desc;
- }
- if ((property._type & (InternalTypes.Number | InternalTypes.Integer | InternalTypes.String)) == 0)
- {
- return PropertyDescriptor.Undefined;
- }
- var str = StringData.ToString();
- var number = TypeConverter.ToNumber(property);
- if (!IsInt32(number, out var index) || index < 0 || index >= str.Length)
- {
- return PropertyDescriptor.Undefined;
- }
- return new PropertyDescriptor(str[index], PropertyFlag.OnlyEnumerable);
- }
- public override IEnumerable<KeyValuePair<JsValue, PropertyDescriptor>> GetOwnProperties()
- {
- foreach (var entry in base.GetOwnProperties())
- {
- yield return entry;
- }
- if (_length != null)
- {
- yield return new KeyValuePair<JsValue, PropertyDescriptor>(CommonProperties.Length, _length);
- }
- }
- internal override IEnumerable<JsValue> GetInitialOwnStringPropertyKeys()
- {
- return new[] { JsString.LengthString };
- }
- public override List<JsValue> GetOwnPropertyKeys(Types types)
- {
- var keys = new List<JsValue>(StringData.Length + 1);
- if ((types & Types.String) != 0)
- {
- for (uint i = 0; i < StringData.Length; ++i)
- {
- keys.Add(JsString.Create(i));
- }
- keys.AddRange(base.GetOwnPropertyKeys(Types.String));
- }
- if ((types & Types.Symbol) != 0)
- {
- keys.AddRange(base.GetOwnPropertyKeys(Types.Symbol));
- }
- return keys;
- }
- protected internal override void SetOwnProperty(JsValue property, PropertyDescriptor desc)
- {
- if (property == CommonProperties.Length)
- {
- _length = desc;
- }
- else
- {
- base.SetOwnProperty(property, desc);
- }
- }
- public override void RemoveOwnProperty(JsValue property)
- {
- if (property == CommonProperties.Length)
- {
- _length = null;
- }
- base.RemoveOwnProperty(property);
- }
- }
- }
|