1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- using Jint.Native.Object;
- using Jint.Runtime;
- using Jint.Runtime.Descriptors;
- namespace Jint.Native.String
- {
- public class StringInstance : ObjectInstance, IPrimitiveInstance
- {
- public StringInstance(Engine engine)
- : base(engine)
- {
- }
- public override string Class => "String";
- Types IPrimitiveInstance.Type => Types.String;
- JsValue IPrimitiveInstance.PrimitiveValue => PrimitiveValue;
- public JsValue PrimitiveValue { get; set; }
- private static bool IsInt(double d)
- {
- if (d >= long.MinValue && d <= long.MaxValue)
- {
- var l = (long) d;
- return l >= int.MinValue && l <= int.MaxValue;
- }
- return false;
- }
- public override PropertyDescriptor GetOwnProperty(string propertyName)
- {
- if (propertyName == "Infinity")
- {
- return PropertyDescriptor.Undefined;
- }
- var desc = base.GetOwnProperty(propertyName);
- if (desc != PropertyDescriptor.Undefined)
- {
- return desc;
- }
- var integer = TypeConverter.ToInteger(propertyName);
- if (integer == 0 && propertyName != "0" || propertyName != System.Math.Abs(integer).ToString())
- {
- return PropertyDescriptor.Undefined;
- }
- var str = PrimitiveValue;
- var dIndex = integer;
- if (!IsInt(dIndex))
- return PropertyDescriptor.Undefined;
- var index = (int) dIndex;
- var len = str.AsString().Length;
- if (len <= index || index < 0)
- {
- return PropertyDescriptor.Undefined;
- }
- var resultStr = TypeConverter.ToString(str.AsString()[index]);
- return new PropertyDescriptor(resultStr, false, true, false);
- }
- }
- }
|