Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing NewTarget handling for basic types #875

Merged
merged 1 commit into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 17 additions & 3 deletions Jint.Tests/Runtime/ErrorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ public void StackTraceCollectedOnThreeLevels()
at a (v) <anonymous>:2:18
at b (v) <anonymous>:6:12
at <anonymous>:9:9";

EqualIgnoringNewLineDifferences(expected, ex.ToString());
}

[Fact]
public void StackTraceCollectedForImmediatelyInvokedFunctionExpression()
{
Expand Down Expand Up @@ -188,10 +188,24 @@ public void StackTraceCollectedForImmediatelyInvokedFunctionExpression()
at getItem (items, itemIndex) get-item.js:2:22
at (anonymous) (getItem) get-item.js:9:16
at get-item.js:13:2";

EqualIgnoringNewLineDifferences(expected, ex.ToString());
}

[Theory]
[InlineData("Error")]
[InlineData("EvalError")]
[InlineData("RangeError")]
[InlineData("SyntaxError")]
[InlineData("TypeError")]
[InlineData("ReferenceError")]
public void ErrorsHaveCorrectConstructor(string type)
{
var engine = new Engine();
engine.Execute($"const o = new {type}();");
Assert.True(engine.Execute($"o.constructor === {type}").GetCompletionValue().AsBoolean());
Assert.Equal(type, engine.Execute("o.constructor.name").GetCompletionValue().AsString());
}

private static void EqualIgnoringNewLineDifferences(string expected, string actual)
{
Expand Down
23 changes: 23 additions & 0 deletions Jint.Tests/Runtime/ObjectInstanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,28 @@ public void RemovingFirstPropertyFromObjectInstancePropertiesBucketAndEnumeratin
var propertyNames = instance.GetOwnProperties().Select(x => x.Key).ToList();
Assert.Equal(new JsValue[] { "scope" }, propertyNames);
}

[Theory]
[InlineData("Array")]
[InlineData("Boolean")]
[InlineData("Date")]
[InlineData("Error")]
[InlineData("Number")]
[InlineData("Object")]
[InlineData("String")]
public void ExtendingObjects(string baseType)
{
var code = $@"
class My{baseType} extends {baseType} {{
constructor(...args) {{
super(...args);
}}
}}
const o = new My{baseType}();
o.constructor === My{baseType}";

var engine = new Engine();
Assert.True(engine.Execute(code).GetCompletionValue().AsBoolean());
}
}
}
46 changes: 35 additions & 11 deletions Jint/Native/Array/ArrayConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ private JsValue From(JsValue thisObj, JsValue[] arguments)
}
else
{
instance = _engine.Array.ConstructFast(0);
instance = _engine.Array.ConstructFast(0);
}

if (objectInstance.TryGetIterator(_engine, out var iterator))
{
var protocol = new ArrayProtocol(_engine, thisArg, instance, iterator, callable);
Expand All @@ -115,8 +115,8 @@ private JsValue From(JsValue thisObj, JsValue[] arguments)

private ObjectInstance ConstructArrayFromArrayLike(
JsValue thisObj,
ObjectInstance objectInstance,
ICallable callable,
ObjectInstance objectInstance,
ICallable callable,
JsValue thisArg)
{
var source = ArrayOperations.For(objectInstance);
Expand All @@ -133,9 +133,9 @@ private JsValue From(JsValue thisObj, JsValue[] arguments)
}
else
{
a = _engine.Array.ConstructFast(length);
a = _engine.Array.ConstructFast(length);
}

var args = !ReferenceEquals(callable, null)
? _engine._jsValueArrayPool.RentArray(2)
: null;
Expand Down Expand Up @@ -181,7 +181,7 @@ internal sealed class ArrayProtocol : IteratorProtocol
private long _index = -1;

public ArrayProtocol(
Engine engine,
Engine engine,
JsValue thisArg,
ObjectInstance instance,
IIterator iterator,
Expand All @@ -200,7 +200,7 @@ protected override void ProcessItem(JsValue[] args, JsValue currentValue)
if (!ReferenceEquals(_callable, null))
{
args[0] = sourceValue;
args[1] = _index;
args[1] = _index;
jsValue = _callable.Call(_thisArg, args);
}
else
Expand Down Expand Up @@ -286,6 +286,13 @@ public ObjectInstance Construct(JsValue[] arguments)

public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
{
if (newTarget.IsUndefined())
{
newTarget = this;
}

var proto = GetPrototypeFromConstructor(newTarget, PrototypeObject);

// check if we can figure out good size
var capacity = arguments.Length > 0 ? (uint) arguments.Length : 0;
if (arguments.Length == 1 && arguments[0].IsNumber())
Expand All @@ -294,7 +301,7 @@ public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
ValidateLength(number);
capacity = (uint) number;
}
return Construct(arguments, capacity);
return Construct(arguments, capacity, proto);
}

public ArrayInstance Construct(int capacity)
Expand All @@ -309,8 +316,12 @@ public ArrayInstance Construct(uint capacity)

public ArrayInstance Construct(JsValue[] arguments, uint capacity)
{
var instance = new ArrayInstance(Engine, capacity);
instance._prototype = PrototypeObject;
return Construct(arguments, capacity, PrototypeObject);
}

private ArrayInstance Construct(JsValue[] arguments, uint capacity, ObjectInstance prototypeObject)
{
var instance = ArrayCreate(capacity, prototypeObject);

if (arguments.Length == 1 && arguments.At(0).IsNumber())
{
Expand Down Expand Up @@ -342,6 +353,19 @@ public ArrayInstance Construct(JsValue[] arguments, uint capacity)
return instance;
}

/// <summary>
/// https://tc39.es/ecma262/#sec-arraycreate
/// </summary>
private ArrayInstance ArrayCreate(uint capacity, ObjectInstance proto)
{
proto ??= PrototypeObject;
var instance = new ArrayInstance(Engine, capacity)
{
_prototype = proto
};
return instance;
}

private ArrayInstance ConstructArrayFromIEnumerable(IEnumerable enumerable)
{
var jsArray = (ArrayInstance) Construct(Arguments.Empty);
Expand Down
6 changes: 3 additions & 3 deletions Jint/Native/Boolean/BooleanConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ private BooleanConstructor(Engine engine)
: base(engine, _functionName)
{
}

public BooleanPrototype PrototypeObject { get; private set; }

public static BooleanConstructor CreateBooleanConstructor(Engine engine)
Expand Down Expand Up @@ -47,7 +47,7 @@ public override JsValue Call(JsValue thisObject, JsValue[] arguments)
/// </summary>
public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
{
var b = TypeConverter.ToBoolean(arguments.At(0))
var b = TypeConverter.ToBoolean(arguments.At(0))
? JsBoolean.True
: JsBoolean.False;

Expand All @@ -57,7 +57,7 @@ public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
}

var o = OrdinaryCreateFromConstructor(newTarget, PrototypeObject, static (engine, state) => new BooleanInstance(engine, (JsBoolean) state), b);
return Construct(b);
return o;
}

public BooleanInstance Construct(JsBoolean value)
Expand Down
29 changes: 14 additions & 15 deletions Jint/Native/Date/DateConstructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,24 +150,25 @@ public override JsValue Call(JsValue thisObject, JsValue[] arguments)
/// </summary>
public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
{
if (arguments.Length == 0)
double dv;
if (arguments.Length == 0 || newTarget.IsUndefined())
{
return Construct(DateTime.UtcNow);
dv = FromDateTime(DateTime.UtcNow);
}
else if (arguments.Length == 1)
{
if (arguments[0] is DateInstance date)
{
return Construct(date.PrimitiveValue);
}

var v = TypeConverter.ToPrimitive(arguments[0]);
if (v.IsString())
{
return Construct(((JsNumber) Parse(Undefined, Arguments.From(v)))._value);
}

return Construct(TimeClip(TypeConverter.ToNumber(v)));
dv = TimeClip(TypeConverter.ToNumber(v));
}
else
{
Expand All @@ -189,8 +190,12 @@ public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
DatePrototype.MakeDay(y, m, dt),
DatePrototype.MakeTime(h, min, s, milli));

return Construct(TimeClip(PrototypeObject.Utc(finalDate)));
dv = TimeClip(PrototypeObject.Utc(finalDate));
}

var o = OrdinaryCreateFromConstructor(newTarget, PrototypeObject, static (engine, _) => new DateInstance(engine));
o.PrimitiveValue = dv;
return o;
}

public DatePrototype PrototypeObject { get; private set; }
Expand All @@ -202,18 +207,12 @@ public DateInstance Construct(DateTimeOffset value)

public DateInstance Construct(DateTime value)
{
var instance = new DateInstance(Engine)
{
_prototype = PrototypeObject,
PrimitiveValue = FromDateTime(value)
};

return instance;
return Construct(FromDateTime(value));
}

public DateInstance Construct(double time)
{
var instance = new DateInstance(Engine)
var instance = new DateInstance(_engine)
{
_prototype = PrototypeObject,
PrimitiveValue = TimeClip(time)
Expand All @@ -222,7 +221,7 @@ public DateInstance Construct(double time)
return instance;
}

public static double TimeClip(double time)
private static double TimeClip(double time)
{
if (double.IsInfinity(time) || double.IsNaN(time))
{
Expand All @@ -237,7 +236,7 @@ public static double TimeClip(double time)
return TypeConverter.ToInteger(time) + 0;
}

public double FromDateTime(DateTime dt)
private double FromDateTime(DateTime dt)
{
var convertToUtcAfter = (dt.Kind == DateTimeKind.Unspecified);

Expand Down