Skip to content

Commit

Permalink
JSS.Lib: Implement the object constructor as a singleton
Browse files Browse the repository at this point in the history
The Object constructor in javascript is a global object. So we imitate
that behaviour as a singleton.
  • Loading branch information
PrestonLTaylor committed Jan 11, 2024
1 parent 3168e94 commit 063f502
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
3 changes: 1 addition & 2 deletions JSS.Lib/Execution/Realm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ private Dictionary<string, Property> CreateGlobalProperties()
Dictionary<string, Property> globalProperties = new();

// 20.1.1 The Object Constructor, https://tc39.es/ecma262/#sec-object-constructor
var objectConstructor = new ObjectConstructor();
globalProperties.Add("Object", new Property(objectConstructor, new(true, false, true)));
globalProperties.Add("Object", new Property(ObjectConstructor.The, new(true, false, true)));

return globalProperties;
}
Expand Down
12 changes: 11 additions & 1 deletion JSS.Lib/Runtime/Object.constructor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ namespace JSS.Lib.Runtime;
internal class ObjectConstructor : Object, ICallable, IConstructable
{
// FIXME: The Object constructor has a [[Prototype]] internal slot whose value is %Function.prototype%.
public ObjectConstructor() : base(null)
private ObjectConstructor() : base(null)
{
// The Object constructor has a "length" property whose value is 1𝔽.
// FIXME: We should probably have a method for internally defining properties
DataProperties.Add("length", new Property(new Number(1), new Attributes(true, false, true)));
}

Expand Down Expand Up @@ -38,4 +39,13 @@ public Completion Construct(VM vm, List argumentList)
Debug.Assert(asObject.IsNormalCompletion());
return asObject;
}

static public ObjectConstructor The
{
get
{
return _constructor;
}
}
static readonly private ObjectConstructor _constructor = new();
}

0 comments on commit 063f502

Please sign in to comment.