Permalink
Browse files

#451 optimize object expressions and type conversions (#521)

  • Loading branch information...
lahma authored and sebastienros committed Jul 11, 2018
1 parent 8bbd390 commit c6881dc92c738a6f726ad5f3f63547917623b6e7
@@ -57,10 +57,17 @@ public ObjectInstance Construct(JsValue[] arguments)
public BooleanInstance Construct(bool value)
{
var instance = new BooleanInstance(Engine);
instance.Prototype = PrototypeObject;
instance.PrimitiveValue = value;
instance.Extensible = true;
return Construct(value ? JsBoolean.True : JsBoolean.False);
}
public BooleanInstance Construct(JsBoolean value)
{
var instance = new BooleanInstance(Engine)
{
Prototype = PrototypeObject,
PrimitiveValue = value,
Extensible = true
};
return instance;
}
View
@@ -5,8 +5,8 @@ namespace Jint.Native
{
public sealed class JsBoolean : JsValue, IEquatable<JsBoolean>
{
public static readonly JsValue False = new JsBoolean(false);
public static readonly JsValue True = new JsBoolean(true);
public static readonly JsBoolean False = new JsBoolean(false);
public static readonly JsBoolean True = new JsBoolean(true);
internal static readonly object BoxedTrue = true;
internal static readonly object BoxedFalse = false;
View
@@ -10,7 +10,7 @@ public class JsString : JsValue, IEquatable<JsString>
private static readonly JsString[] _charToJsValue;
private static readonly JsString[] _charToStringJsValue;
private static readonly JsString Empty = new JsString("");
public static readonly JsString Empty = new JsString("");
private static readonly JsString NullString = new JsString("null");
internal string _value;
@@ -57,6 +57,8 @@ internal virtual bool IsNullOrEmpty()
return string.IsNullOrEmpty(_value);
}
public virtual int Length => _value.Length;
internal static JsString Create(string value)
{
if (value.Length == 0)
@@ -178,6 +180,8 @@ internal override bool IsNullOrEmpty()
|| _stringBuilder != null && _stringBuilder.Length == 0;
}
public override int Length => _stringBuilder?.Length ?? _value?.Length ?? 0;
public override object ToObject()
{
return _stringBuilder.ToString();
@@ -190,15 +194,14 @@ public override bool Equals(JsValue other)
return _stringBuilder.Equals(cs._stringBuilder);
}
if (other.Type == Types.String)
if (other is JsString jsString)
{
var otherString = other.AsStringWithoutTypeCheck();
if (otherString.Length != _stringBuilder.Length)
if (jsString._value.Length != Length)
{
return false;
}
return ToString() == otherString;
return ToString() == jsString._value;
}
return base.Equals(other);
@@ -63,10 +63,17 @@ public ObjectInstance Construct(JsValue[] arguments)
public NumberInstance Construct(double value)
{
var instance = new NumberInstance(Engine);
instance.Prototype = PrototypeObject;
instance.NumberData = value;
instance.Extensible = true;
return Construct(JsNumber.Create(value));
}
public NumberInstance Construct(JsNumber value)
{
var instance = new NumberInstance(Engine)
{
Prototype = PrototypeObject,
NumberData = value,
Extensible = true
};
return instance;
}
@@ -18,7 +18,7 @@ public NumberInstance(Engine engine)
JsValue IPrimitiveInstance.PrimitiveValue => NumberData;
public JsValue NumberData { get; set; }
public JsNumber NumberData { get; set; }
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNegativeZero(double x)
@@ -21,10 +21,12 @@ private NumberPrototype(Engine engine)
public static NumberPrototype CreatePrototypeObject(Engine engine, NumberConstructor numberConstructor)
{
var obj = new NumberPrototype(engine);
obj.Prototype = engine.Object.PrototypeObject;
obj.NumberData = 0;
obj.Extensible = true;
var obj = new NumberPrototype(engine)
{
Prototype = engine.Object.PrototypeObject,
NumberData = JsNumber.Create(0),
Extensible = true
};
obj.FastAddProperty("constructor", numberConstructor, true, false, true);
@@ -100,6 +100,18 @@ public ObjectInstance Construct(JsValue[] arguments)
return obj;
}
internal ObjectInstance Construct(int propertyCount)
{
var obj = new ObjectInstance(_engine)
{
Extensible = true,
Prototype = Engine.Object.PrototypeObject,
_properties = propertyCount > 0 ? new Dictionary<string, PropertyDescriptor>(propertyCount) : null
};
return obj;
}
public JsValue GetPrototypeOf(JsValue thisObject, JsValue[] arguments)
{
@@ -144,7 +156,7 @@ public JsValue GetOwnPropertyNames(JsValue thisObject, JsValue[] arguments)
var ownProperties = o.GetOwnProperties().ToList();
if (o is StringInstance s)
{
var length = s.PrimitiveValue.AsStringWithoutTypeCheck().Length;
var length = s.PrimitiveValue.Length;
array = Engine.Array.ConstructFast((uint) (ownProperties.Count + length));
for (var i = 0; i < length; i++)
{
@@ -19,7 +19,7 @@ namespace Jint.Native.Object
public class ObjectInstance : JsValue, IEquatable<ObjectInstance>
{
protected Dictionary<string, PropertyDescriptor> _intrinsicProperties;
protected Dictionary<string, PropertyDescriptor> _properties;
protected internal Dictionary<string, PropertyDescriptor> _properties;
private readonly string _class;
protected readonly Engine _engine;
@@ -780,7 +780,7 @@ public override object ToObject()
case "String":
if (this is StringInstance stringInstance)
{
return stringInstance.PrimitiveValue.AsStringWithoutTypeCheck();
return stringInstance.PrimitiveValue.ToString();
}
break;
@@ -814,7 +814,7 @@ public override object ToObject()
case "Number":
if (this is NumberInstance numberInstance)
{
return ((JsNumber) numberInstance.NumberData)._value;
return numberInstance.NumberData._value;
}
break;
@@ -70,12 +70,18 @@ public ObjectInstance Construct(JsValue[] arguments)
public StringInstance Construct(string value)
{
var instance = new StringInstance(Engine);
instance.Prototype = PrototypeObject;
instance.PrimitiveValue = value;
instance.Extensible = true;
return Construct(JsString.Create(value));
}
instance.SetOwnProperty("length", new PropertyDescriptor(value.Length, PropertyFlag.AllForbidden));
public StringInstance Construct(JsString value)
{
var instance = new StringInstance(Engine)
{
Prototype = PrototypeObject,
PrimitiveValue = value,
Extensible = true,
_length = new PropertyDescriptor(value.Length, PropertyFlag.AllForbidden)
};
return instance;
}
@@ -10,7 +10,7 @@ public class StringInstance : ObjectInstance, IPrimitiveInstance
private const string PropertyNameLength = "length";
private const int PropertyNameLengthLength = 6;
private PropertyDescriptor _length;
internal PropertyDescriptor _length;
public StringInstance(Engine engine)
: base(engine, objectClass: "String")
@@ -21,7 +21,7 @@ public StringInstance(Engine engine)
JsValue IPrimitiveInstance.PrimitiveValue => PrimitiveValue;
public JsValue PrimitiveValue { get; set; }
public JsString PrimitiveValue { get; set; }
private static bool IsInt(double d)
{
@@ -25,7 +25,7 @@ public static StringPrototype CreatePrototypeObject(Engine engine, StringConstru
{
var obj = new StringPrototype(engine);
obj.Prototype = engine.Object.PrototypeObject;
obj.PrimitiveValue = "";
obj.PrimitiveValue = JsString.Empty;
obj.Extensible = true;
obj.SetOwnProperty("length", new PropertyDescriptor(0, PropertyFlag.AllForbidden));
obj.SetOwnProperty("constructor", new PropertyDescriptor(stringConstructor, PropertyFlag.NonEnumerable));
@@ -115,10 +115,17 @@ public ObjectInstance Construct(JsValue[] arguments)
public SymbolInstance Construct(string description)
{
var instance = new SymbolInstance(Engine);
instance.Prototype = PrototypeObject;
instance.SymbolData = new JsSymbol(description);
instance.Extensible = true;
return Construct(new JsSymbol(description));
}
public SymbolInstance Construct(JsSymbol symbol)
{
var instance = new SymbolInstance(Engine)
{
Prototype = PrototypeObject,
SymbolData = symbol,
Extensible = true
};
return instance;
}
Oops, something went wrong.

0 comments on commit c6881dc

Please sign in to comment.