@ -97,7 +97,8 @@ use crate::{
js_string ,
js_string ,
native_function ::{ NativeFunction , NativeFunctionPointer } ,
native_function ::{ NativeFunction , NativeFunctionPointer } ,
object ::{
object ::{
FunctionBinding , JsFunction , JsObject , JsPrototype , ObjectData , CONSTRUCTOR , PROTOTYPE ,
FunctionBinding , JsFunction , JsObject , JsPrototype , Object , ObjectData , CONSTRUCTOR ,
PROTOTYPE ,
} ,
} ,
property ::{ Attribute , PropertyDescriptor , PropertyKey } ,
property ::{ Attribute , PropertyDescriptor , PropertyKey } ,
realm ::Realm ,
realm ::Realm ,
@ -390,6 +391,85 @@ pub(crate) fn set_default_global_bindings(context: &mut Context<'_>) -> JsResult
// === Builder typestate ===
// === Builder typestate ===
#[ derive(Debug) ]
enum BuiltInObjectInitializer {
Shared ( JsObject ) ,
Unique { object : Object , data : ObjectData } ,
}
impl BuiltInObjectInitializer {
/// Inserts a new property descriptor into the builtin.
fn insert < K , P > ( & mut self , key : K , property : P )
where
K : Into < PropertyKey > ,
P : Into < PropertyDescriptor > ,
{
match self {
BuiltInObjectInitializer ::Shared ( obj ) = > obj . borrow_mut ( ) . insert ( key , property ) ,
BuiltInObjectInitializer ::Unique { object , .. } = > object . insert ( key , property ) ,
} ;
}
/// Sets the prototype of the builtin
fn set_prototype ( & mut self , prototype : JsObject ) {
match self {
BuiltInObjectInitializer ::Shared ( obj ) = > {
let mut obj = obj . borrow_mut ( ) ;
obj . set_prototype ( prototype ) ;
}
BuiltInObjectInitializer ::Unique { object , .. } = > {
object . set_prototype ( prototype ) ;
}
}
}
/// Sets the `ObjectData` of the builtin.
///
/// # Panics
///
/// Panics if the builtin is a shared builtin and the data's vtable is not the same as the
/// builtin's vtable.
fn set_data ( & mut self , new_data : ObjectData ) {
match self {
BuiltInObjectInitializer ::Shared ( obj ) = > {
assert! (
std ::ptr ::eq ( obj . vtable ( ) , new_data . internal_methods ) ,
"intrinsic object's vtable didn't match with new data"
) ;
* obj . borrow_mut ( ) . kind_mut ( ) = new_data . kind ;
}
BuiltInObjectInitializer ::Unique { ref mut data , .. } = > * data = new_data ,
}
}
/// Gets a shared object from the builtin, transitioning its state if it's necessary.
fn as_shared ( & mut self ) -> JsObject {
match std ::mem ::replace (
self ,
BuiltInObjectInitializer ::Unique {
object : Object ::default ( ) ,
data : ObjectData ::ordinary ( ) ,
} ,
) {
BuiltInObjectInitializer ::Shared ( obj ) = > {
* self = BuiltInObjectInitializer ::Shared ( obj . clone ( ) ) ;
obj
}
BuiltInObjectInitializer ::Unique { mut object , data } = > {
* object . kind_mut ( ) = data . kind ;
let obj = JsObject ::from_object_and_vtable ( object , data . internal_methods ) ;
* self = BuiltInObjectInitializer ::Shared ( obj . clone ( ) ) ;
obj
}
}
}
/// Converts the builtin into a shared object.
fn into_shared ( mut self ) -> JsObject {
self . as_shared ( )
}
}
/// Marker for a constructor function.
/// Marker for a constructor function.
struct Constructor {
struct Constructor {
prototype : JsObject ,
prototype : JsObject ,
@ -434,77 +514,78 @@ struct OrdinaryObject;
/// Applies the pending builder data to the object.
/// Applies the pending builder data to the object.
trait ApplyToObject {
trait ApplyToObject {
fn apply_to ( self , object : & JsObject ) ;
fn apply_to ( self , object : & mut BuiltInObjectInitializer ) ;
}
}
impl ApplyToObject for Constructor {
impl ApplyToObject for Constructor {
fn apply_to ( self , object : & JsObject ) {
fn apply_to ( self , object : & mut BuiltInObjectInitializer ) {
object . insert (
PROTOTYPE ,
PropertyDescriptor ::builder ( )
. value ( self . prototype . clone ( ) )
. writable ( false )
. enumerable ( false )
. configurable ( false ) ,
) ;
let object = object . as_shared ( ) ;
{
{
let mut prototype = self . prototype . borrow_mut ( ) ;
let mut prototype = self . prototype . borrow_mut ( ) ;
prototype . set_prototype ( self . inherits ) ;
prototype . set_prototype ( self . inherits ) ;
prototype . insert (
prototype . insert (
CONSTRUCTOR ,
CONSTRUCTOR ,
PropertyDescriptor ::builder ( )
PropertyDescriptor ::builder ( )
. value ( object . clone ( ) )
. value ( object )
. writable ( self . attributes . writable ( ) )
. writable ( self . attributes . writable ( ) )
. enumerable ( self . attributes . enumerable ( ) )
. enumerable ( self . attributes . enumerable ( ) )
. configurable ( self . attributes . configurable ( ) ) ,
. configurable ( self . attributes . configurable ( ) ) ,
) ;
) ;
}
}
let mut object = object . borrow_mut ( ) ;
object . insert (
PROTOTYPE ,
PropertyDescriptor ::builder ( )
. value ( self . prototype )
. writable ( false )
. enumerable ( false )
. configurable ( false ) ,
) ;
}
}
}
}
impl ApplyToObject for ConstructorNoProto {
impl ApplyToObject for ConstructorNoProto {
fn apply_to ( self , _ : & JsObject ) { }
fn apply_to ( self , _ : & mut BuiltInObjectInitializer ) { }
}
}
impl ApplyToObject for OrdinaryFunction {
impl ApplyToObject for OrdinaryFunction {
fn apply_to ( self , _ : & JsObject ) { }
fn apply_to ( self , _ : & mut BuiltInObjectInitializer ) { }
}
}
impl < S : ApplyToObject + IsConstructor > ApplyToObject for Callable < S > {
impl < S : ApplyToObject + IsConstructor > ApplyToObject for Callable < S > {
fn apply_to ( self , object : & JsObject ) {
fn apply_to ( self , object : & mut BuiltInObjectInitializer ) {
self . kind . apply_to ( object ) ;
let function = ObjectData ::function ( function ::Function ::new (
let function = function ::Function ::new (
function ::FunctionKind ::Native {
function ::FunctionKind ::Native {
function : NativeFunction ::from_fn_ptr ( self . function ) ,
function : NativeFunction ::from_fn_ptr ( self . function ) ,
constructor : S ::IS_CONSTRUCTOR . then_some ( function ::ConstructorKind ::Base ) ,
constructor : S ::IS_CONSTRUCTOR . then_some ( function ::ConstructorKind ::Base ) ,
} ,
} ,
self . realm ,
self . realm ,
) ;
) ) ;
object . set_data ( function ) ;
let length = PropertyDescriptor ::builder ( )
object . insert (
utf16 ! ( "length" ) ,
PropertyDescriptor ::builder ( )
. value ( self . length )
. value ( self . length )
. writable ( false )
. writable ( false )
. enumerable ( false )
. enumerable ( false )
. configurable ( true ) ;
. configurable ( true ) ,
let name = PropertyDescriptor ::builder ( )
) ;
object . insert (
utf16 ! ( "name" ) ,
PropertyDescriptor ::builder ( )
. value ( self . name )
. value ( self . name )
. writable ( false )
. writable ( false )
. enumerable ( false )
. enumerable ( false )
. configurable ( true ) ;
. configurable ( true ) ,
) ;
{
self . kind . apply_to ( object ) ;
let mut constructor = object . borrow_mut ( ) ;
constructor . data = ObjectData ::function ( function ) ;
constructor . insert ( utf16 ! ( "length" ) , length ) ;
constructor . insert ( utf16 ! ( "name" ) , name ) ;
}
}
}
}
}
impl ApplyToObject for OrdinaryObject {
impl ApplyToObject for OrdinaryObject {
fn apply_to ( self , _ : & JsObject ) { }
fn apply_to ( self , _ : & mut BuiltInObjectInitializer ) { }
}
}
/// Builder for creating built-in objects, like `Array`.
/// Builder for creating built-in objects, like `Array`.
@ -515,7 +596,7 @@ impl ApplyToObject for OrdinaryObject {
#[ must_use = " You need to call the `build` method in order for this to correctly assign the inner data " ]
#[ must_use = " You need to call the `build` method in order for this to correctly assign the inner data " ]
struct BuiltInBuilder < ' ctx , Kind > {
struct BuiltInBuilder < ' ctx , Kind > {
realm : & ' ctx Realm ,
realm : & ' ctx Realm ,
object : JsObject ,
object : BuiltInObjectInitializer ,
kind : Kind ,
kind : Kind ,
prototype : JsObject ,
prototype : JsObject ,
}
}
@ -524,7 +605,10 @@ impl<'ctx> BuiltInBuilder<'ctx, OrdinaryObject> {
fn new ( realm : & ' ctx Realm ) -> BuiltInBuilder < ' ctx , OrdinaryObject > {
fn new ( realm : & ' ctx Realm ) -> BuiltInBuilder < ' ctx , OrdinaryObject > {
BuiltInBuilder {
BuiltInBuilder {
realm ,
realm ,
object : JsObject ::with_null_proto ( ) ,
object : BuiltInObjectInitializer ::Unique {
object : Object ::default ( ) ,
data : ObjectData ::ordinary ( ) ,
} ,
kind : OrdinaryObject ,
kind : OrdinaryObject ,
prototype : realm . intrinsics ( ) . constructors ( ) . object ( ) . prototype ( ) ,
prototype : realm . intrinsics ( ) . constructors ( ) . object ( ) . prototype ( ) ,
}
}
@ -535,7 +619,7 @@ impl<'ctx> BuiltInBuilder<'ctx, OrdinaryObject> {
) -> BuiltInBuilder < ' ctx , OrdinaryObject > {
) -> BuiltInBuilder < ' ctx , OrdinaryObject > {
BuiltInBuilder {
BuiltInBuilder {
realm ,
realm ,
object : I ::get ( realm . intrinsics ( ) ) ,
object : BuiltInObjectInitializer ::Shared ( I ::get ( realm . intrinsics ( ) ) ) ,
kind : OrdinaryObject ,
kind : OrdinaryObject ,
prototype : realm . intrinsics ( ) . constructors ( ) . object ( ) . prototype ( ) ,
prototype : realm . intrinsics ( ) . constructors ( ) . object ( ) . prototype ( ) ,
}
}
@ -544,7 +628,7 @@ impl<'ctx> BuiltInBuilder<'ctx, OrdinaryObject> {
fn with_object ( realm : & ' ctx Realm , object : JsObject ) -> BuiltInBuilder < ' ctx , OrdinaryObject > {
fn with_object ( realm : & ' ctx Realm , object : JsObject ) -> BuiltInBuilder < ' ctx , OrdinaryObject > {
BuiltInBuilder {
BuiltInBuilder {
realm ,
realm ,
object ,
object : BuiltInObjectInitializer ::Shared ( object ) ,
kind : OrdinaryObject ,
kind : OrdinaryObject ,
prototype : realm . intrinsics ( ) . constructors ( ) . object ( ) . prototype ( ) ,
prototype : realm . intrinsics ( ) . constructors ( ) . object ( ) . prototype ( ) ,
}
}
@ -583,7 +667,7 @@ impl<'ctx> BuiltInBuilder<'ctx, Callable<Constructor>> {
let constructor = SC ::STANDARD_CONSTRUCTOR ( realm . intrinsics ( ) . constructors ( ) ) ;
let constructor = SC ::STANDARD_CONSTRUCTOR ( realm . intrinsics ( ) . constructors ( ) ) ;
BuiltInBuilder {
BuiltInBuilder {
realm ,
realm ,
object : constructor . constructor ( ) ,
object : BuiltInObjectInitializer ::Shared ( constructor . constructor ( ) ) ,
kind : Callable {
kind : Callable {
function : SC ::constructor ,
function : SC ::constructor ,
name : js_string ! ( SC ::NAME ) ,
name : js_string ! ( SC ::NAME ) ,
@ -617,7 +701,12 @@ impl<'ctx> BuiltInBuilder<'ctx, Callable<Constructor>> {
impl < T > BuiltInBuilder < ' _ , T > {
impl < T > BuiltInBuilder < ' _ , T > {
/// Adds a new static method to the builtin object.
/// Adds a new static method to the builtin object.
fn static_method < B > ( self , function : NativeFunctionPointer , binding : B , length : usize ) -> Self
fn static_method < B > (
mut self ,
function : NativeFunctionPointer ,
binding : B ,
length : usize ,
) -> Self
where
where
B : Into < FunctionBinding > ,
B : Into < FunctionBinding > ,
{
{
@ -628,7 +717,7 @@ impl<T> BuiltInBuilder<'_, T> {
. length ( length )
. length ( length )
. build ( ) ;
. build ( ) ;
self . object . borrow_mut ( ) . insert (
self . object . insert (
binding . binding ,
binding . binding ,
PropertyDescriptor ::builder ( )
PropertyDescriptor ::builder ( )
. value ( function )
. value ( function )
@ -640,7 +729,7 @@ impl<T> BuiltInBuilder<'_, T> {
}
}
/// Adds a new static data property to the builtin object.
/// Adds a new static data property to the builtin object.
fn static_property < K , V > ( self , key : K , value : V , attribute : Attribute ) -> Self
fn static_property < K , V > ( mut self , key : K , value : V , attribute : Attribute ) -> Self
where
where
K : Into < PropertyKey > ,
K : Into < PropertyKey > ,
V : Into < JsValue > ,
V : Into < JsValue > ,
@ -650,13 +739,13 @@ impl<T> BuiltInBuilder<'_, T> {
. writable ( attribute . writable ( ) )
. writable ( attribute . writable ( ) )
. enumerable ( attribute . enumerable ( ) )
. enumerable ( attribute . enumerable ( ) )
. configurable ( attribute . configurable ( ) ) ;
. configurable ( attribute . configurable ( ) ) ;
self . object . borrow_mut ( ) . insert ( key , property ) ;
self . object . insert ( key , property ) ;
self
self
}
}
/// Adds a new static accessor property to the builtin object.
/// Adds a new static accessor property to the builtin object.
fn static_accessor < K > (
fn static_accessor < K > (
self ,
mut self ,
key : K ,
key : K ,
get : Option < JsFunction > ,
get : Option < JsFunction > ,
set : Option < JsFunction > ,
set : Option < JsFunction > ,
@ -670,7 +759,7 @@ impl<T> BuiltInBuilder<'_, T> {
. maybe_set ( set )
. maybe_set ( set )
. enumerable ( attribute . enumerable ( ) )
. enumerable ( attribute . enumerable ( ) )
. configurable ( attribute . configurable ( ) ) ;
. configurable ( attribute . configurable ( ) ) ;
self . object . borrow_mut ( ) . insert ( key , property ) ;
self . object . insert ( key , property ) ;
self
self
}
}
@ -779,28 +868,22 @@ impl<FnTyp> BuiltInBuilder<'_, Callable<FnTyp>> {
impl BuiltInBuilder < ' _ , OrdinaryObject > {
impl BuiltInBuilder < ' _ , OrdinaryObject > {
/// Build the builtin object.
/// Build the builtin object.
fn build ( self ) -> JsObject {
fn build ( mut self ) -> JsObject {
self . kind . apply_to ( & self . object ) ;
self . kind . apply_to ( & mut self . object ) ;
{
self . object . set_prototype ( self . prototype ) ;
let mut object = self . object . borrow_mut ( ) ;
object . set_prototype ( self . prototype ) ;
}
self . object
self . object . into_shared ( )
}
}
}
}
impl < FnTyp : ApplyToObject + IsConstructor > BuiltInBuilder < ' _ , Callable < FnTyp > > {
impl < FnTyp : ApplyToObject + IsConstructor > BuiltInBuilder < ' _ , Callable < FnTyp > > {
/// Build the builtin callable.
/// Build the builtin callable.
fn build ( self ) -> JsFunction {
fn build ( mut self ) -> JsFunction {
self . kind . apply_to ( & self . object ) ;
self . kind . apply_to ( & mut self . object ) ;
{
self . object . set_prototype ( self . prototype ) ;
let mut object = self . object . borrow_mut ( ) ;
object . set_prototype ( self . prototype ) ;
}
JsFunction ::from_object_unchecked ( self . object )
JsFunction ::from_object_unchecked ( self . object . into_shared ( ) )
}
}
}
}