How JavaScript Objects Are Implemented
How JavaScript Objects Are Implemented
are Implemented
Eddy Jean-Paul Bruel
Overview
How objects are specied
How objects are represented
Object operations:
Adding properties
Finding properties
Removing properties
Arrays and elements
Overview
How objects are specied
How objects are represented
Object operations:
Adding properties
Finding properties
Removing properties
Arrays and elements
Properties
An object is a collection of named properties.
Each property has the following attributes:
[[Enumerable]]
Whether the property will be enumerated by for-in.
[[Congurable]]
Whether the property can be deleted, or its
attributes (other than [[Value]]) modied.
Each property is either a data property or an accessor
property.
Data Properties
A data property is a property that has the
following attributes:
[[Value]]
The value of the property.
[[Writable]]
Whether the value of the property can be
modied.
Accessor Properties
An accessor property is a property that has
the following attributes:
[[Get]]
A function to access the value of the
property.
[[Set]]
A function to modify the value of the
property.
Overview
How objects are specied
How objects are represented
Object operations:
Adding properties
Finding properties
Removing properties
Arrays and elements
Shapes and Slots
In SpiderMonkey, an object
consists of:
A linked list of shapes.
A vector of slots.
Each shape stores the name and
attributes of a property (except
[[Value]]).
Each slot stores the [[Value]]
attribute of a property.
Each shape contains an index
into the slot vector.
Shapes and Slots
In SpiderMonkey, an object
consists of:
A linked list of shapes.
A vector of slots.
Each shape stores the name and
attributes of a property (except
[[Value]]).
Each slot stores the [[Value]]
attribute of a property.
Each shape contains an index
into the slot vector.
Shape Trees
Because the [[Value]] attribute
is stored separately, shapes can
be (partially) shared for a set of
properties IF:
they have the same name,
AND they have the same
attributes (except [[Value]]),
AND they are dened in the
same order.
This leads to the notion of shape
trees.
Shape Trees
Because the [[Value]] attribute
is stored separately, shapes can
be (partially) shared for a set of
properties IF:
they have the same name,
AND they have the same
attributes (except [[Value]]),
AND they are dened in the
same order.
This leads to the notion of shape
trees.
Empty Shapes
Every object starts out with
an empty shape.
Objects start out with the
same empty shape IF:
they have the same
compartment (i.e. global),
AND they have the same
class,
AND they have the same
prototype.
Empty Shapes
Every object starts out with
an empty shape.
Objects start out with the
same empty shape IF:
they have the same
compartment (i.e. global),
AND they have the same
class,
AND they have the same
prototype.
Empty Shapes
Every object starts out with
an empty shape.
Objects start out with the
same empty shape IF:
they have the same
compartment (i.e. global),
AND they have the same
class,
AND they have the same
prototype.
Overview
How objects are specied
How objects are represented
Object operations:
Adding properties
Finding properties
Removing properties
Arrays and elements
Adding Properties
To add a property P to an object O:
1. IF the current shape of O has a
child C that stores P:
1. Let S be C.
2. ELSE:
1. Let S be a new shape that
stores P.
2. Insert S as a child of the
current shape of O.
3. Set the current shape of O to S.
Adding Properties
To add a property P to an object O:
1. IF the current shape of O has a
child C that stores P:
1. Let S be C.
2. ELSE:
1. Let S be a new shape that
stores P.
2. Insert S as a child of the
current shape of O.
3. Set the current shape of O to S.
Adding Properties
To add a property P to an object O:
1. IF the current shape of O has a
child C that stores P:
1. Let S be C.
2. ELSE:
1. Let S be a new shape that
stores P.
2. Insert S as a child of the
current shape of O.
3. Set the current shape of O to S.
Adding Properties
To add a property P to an object O:
1. IF the current shape of O has a
child C that stores P:
1. Let S be C.
2. ELSE:
1. Let S be a new shape that
stores P.
2. Insert S as a child of the
current shape of O.
3. Set the current shape of O to S.
Adding Properties
To add a property P to an object O:
1. IF the current shape of O has a
child C that stores P:
1. Let S be C.
2. ELSE:
1. Let S be a new shape that
stores P.
2. Insert S as a child of the
current shape of O.
3. Set the current shape of O to S.
Adding Properties
To add a property P to an object O:
1. IF the current shape of O has a
child C that stores P:
1. Let S be C.
2. ELSE:
1. Let S be a new shape that
stores P.
2. Insert S as a child of the
current shape of O.
3. Set the current shape of O to S.
Adding Properties
To add a property P to an object O:
1. IF the current shape of O has a
child C that stores P:
1. Let S be C.
2. ELSE:
1. Let S be a new shape that
stores P.
2. Insert S as a child of the
current shape of O.
3. Set the current shape of O to S.
Adding Properties
To add a property P to an object O:
1. IF the current shape of O has a
child C that stores P:
1. Let S be C.
2. ELSE:
1. Let S be a new shape that
stores P.
2. Insert S as a child of the
current shape of O.
3. Set the current shape of O to S.
Overview
How objects are specied
How objects are represented
Object operations:
Adding properties
Finding properties
Removing properties
Arrays and elements
Shape Lookup