@ -266,6 +266,7 @@ impl Environment {
}
}
/// Returns the declarative environment and panic if it is not one.
/// Returns the declarative environment and panic if it is not one.
#[ track_caller ]
pub ( crate ) fn declarative_expect ( & self ) -> & Gc < DeclarativeEnvironment > {
pub ( crate ) fn declarative_expect ( & self ) -> & Gc < DeclarativeEnvironment > {
self . as_declarative ( )
self . as_declarative ( )
. expect ( "environment must be declarative" )
. expect ( "environment must be declarative" )
@ -418,8 +419,8 @@ impl DeclarativeEnvironmentStack {
let environment = self
let environment = self
. stack
. stack
. iter ( )
. iter ( )
. filter_map ( Environment ::as_declarative )
. rev ( )
. last ( )
. find_map ( Environment ::as_declarative )
. expect ( "global environment must always exist" ) ;
. expect ( "global environment must always exist" ) ;
( environment . poisoned . get ( ) , with | | environment . with . get ( ) )
( environment . poisoned . get ( ) , with | | environment . with . get ( ) )
} ;
} ;
@ -464,8 +465,8 @@ impl DeclarativeEnvironmentStack {
let environment = self
let environment = self
. stack
. stack
. iter ( )
. iter ( )
. filter_map ( Environment ::as_declarative )
. rev ( )
. last ( )
. find_map ( Environment ::as_declarative )
. expect ( "global environment must always exist" ) ;
. expect ( "global environment must always exist" ) ;
( environment . poisoned . get ( ) , with | | environment . with . get ( ) )
( environment . poisoned . get ( ) , with | | environment . with . get ( ) )
} ;
} ;
@ -523,9 +524,8 @@ impl DeclarativeEnvironmentStack {
let environment = self
let environment = self
. stack
. stack
. iter ( )
. iter ( )
. filter_map ( Environment ::as_declarative )
. rev ( )
. filter ( | e | e . slots ( ) . is_some ( ) )
. find_map ( | env | env . as_declarative ( ) . filter ( | decl | decl . slots ( ) . is_some ( ) ) )
. last ( )
. expect ( "global environment must always exist" ) ;
. expect ( "global environment must always exist" ) ;
(
(
environment . poisoned . get ( ) ,
environment . poisoned . get ( ) ,
@ -545,6 +545,7 @@ impl DeclarativeEnvironmentStack {
}
}
/// Pop environment from the environments stack.
/// Pop environment from the environments stack.
#[ track_caller ]
pub ( crate ) fn pop ( & mut self ) -> Environment {
pub ( crate ) fn pop ( & mut self ) -> Environment {
debug_assert! ( self . stack . len ( ) > 1 ) ;
debug_assert! ( self . stack . len ( ) > 1 ) ;
self . stack
self . stack
@ -557,11 +558,11 @@ impl DeclarativeEnvironmentStack {
/// # Panics
/// # Panics
///
///
/// Panics if no environment exists on the stack.
/// Panics if no environment exists on the stack.
pub ( crate ) fn current ( & mut self ) -> Gc < DeclarativeEnvironment > {
#[ track_caller ]
pub ( crate ) fn current ( & mut self ) -> Environment {
self . stack
self . stack
. last ( )
. last ( )
. expect ( "global environment must always exist" )
. expect ( "global environment must always exist" )
. declarative_expect ( )
. clone ( )
. clone ( )
}
}
@ -582,68 +583,27 @@ impl DeclarativeEnvironmentStack {
/// Mark that there may be added bindings from the current environment to the next function
/// Mark that there may be added bindings from the current environment to the next function
/// environment.
/// environment.
pub ( crate ) fn poison_last_function ( & mut self ) {
pub ( crate ) fn poison_until_last_function ( & mut self ) {
for env in self . stack . iter_mut ( ) . rev ( ) {
for env in self
if let Some ( env ) = env . as_declarative ( ) {
. stack
if env . compile_env ( ) . borrow ( ) . is_function ( ) {
. iter ( )
env . poisoned . set ( true ) ;
. rev ( )
return ;
. filter_map ( Environment ::as_declarative )
}
{
}
env . poisoned . set ( true ) ;
}
if env . compile_env ( ) . borrow ( ) . is_function ( ) {
}
return ;
/// Get the value of a binding. Ignores object environments.
///
/// # Panics
///
/// Panics if the environment or binding index are out of range.
pub ( crate ) fn get_value_optional (
& self ,
mut environment_index : usize ,
mut binding_index : usize ,
name : Identifier ,
) -> Option < JsValue > {
if environment_index ! = self . stack . len ( ) - 1 {
for env_index in ( environment_index + 1 .. self . stack . len ( ) ) . rev ( ) {
let Environment ::Declarative ( env ) = self
. stack
. get ( env_index )
. expect ( "environment index must be in range" ) else {
continue ;
} ;
if ! env . poisoned . get ( ) {
break ;
}
let compile = env . compile . borrow ( ) ;
if compile . is_function ( ) {
if let Some ( b ) = compile . get_binding ( name ) {
environment_index = b . environment_index ;
binding_index = b . binding_index ;
break ;
}
}
}
}
}
}
self . stack
. get ( environment_index )
. expect ( "environment index must be in range" )
. declarative_expect ( )
. bindings
. borrow ( )
. get ( binding_index )
. expect ( "binding index must be in range" )
. clone ( )
}
}
/// Set the value of a binding.
/// Set the value of a declarative binding.
///
///
/// # Panics
/// # Panics
///
///
/// Panics if the environment or binding index are out of range.
/// Panics if the environment or binding index are out of range.
#[ track_caller ]
#[ track_caller ]
pub ( crate ) fn put_value (
pub ( crate ) fn put_declarative_value (
& mut self ,
& mut self ,
environment_index : usize ,
environment_index : usize ,
binding_index : usize ,
binding_index : usize ,
@ -688,37 +648,6 @@ impl DeclarativeEnvironmentStack {
* binding = Some ( value ) ;
* binding = Some ( value ) ;
}
}
}
}
/// Check if a binding name does exist in a poisoned environment.
///
/// A binding could be marked as `global`, and at the same time, exist in a deeper environment
/// context; if the global context is poisoned, an `eval` call could have added a binding that is
/// not global with the same name as the global binding. This double checks that the binding name
/// is truly a global property.
///
/// # Panics
///
/// Panics if the global environment does not exist.
pub ( crate ) fn binding_in_poisoned_environment ( & mut self , name : Identifier ) -> bool {
for env in self
. stack
. split_first ( )
. expect ( "global environment must exist" )
. 1
. iter ( )
. filter_map ( Environment ::as_declarative )
. rev ( )
{
if ! env . poisoned . get ( ) {
return false ;
}
let compile = env . compile . borrow ( ) ;
if compile . is_function ( ) & & compile . get_binding ( name ) . is_some ( ) {
return true ;
}
}
false
}
}
}
/// A binding locator contains all information about a binding that is needed to resolve it at runtime.
/// A binding locator contains all information about a binding that is needed to resolve it at runtime.
@ -736,7 +665,7 @@ pub(crate) struct BindingLocator {
impl BindingLocator {
impl BindingLocator {
/// Creates a new declarative binding locator that has knows indices.
/// Creates a new declarative binding locator that has knows indices.
pub ( in crate ::environments ) const fn declarative (
pub ( crate ) const fn declarative (
name : Identifier ,
name : Identifier ,
environment_index : usize ,
environment_index : usize ,
binding_index : usize ,
binding_index : usize ,
@ -830,26 +759,33 @@ impl BindingLocator {
}
}
impl Context < ' _ > {
impl Context < ' _ > {
/// Get the value of a binding.
/// Gets the corresponding runtime binding of the provided `BindingLocator`, modifying
///
/// its indexes in place.
/// # Panics
///
///
/// This readjusts a `BindingLocator` to the correct binding if a `with` environment or
/// Panics if the environment or binding index are out of range.
/// `eval` call modified the compile-time bindings.
pub ( crate ) fn get_value_optional (
///
& mut self ,
/// Only use if the binding origin is unknown or comes from a `var` declaration. Lexical bindings
mut environment_index : usize ,
/// are completely removed of runtime checks because the specification guarantees that runtime
mut binding_index : usize ,
/// semantics cannot add or remove lexical bindings.
name : Identifier ,
pub ( crate ) fn find_runtime_binding ( & mut self , locator : & mut BindingLocator ) -> JsResult < ( ) > {
) -> JsResult < Option < JsValue > > {
let current = self . vm . environments . current ( ) ;
for env_index in ( environment_index + 1 .. self . vm . environments . stack . len ( ) ) . rev ( ) {
if let Some ( env ) = current . as_declarative ( ) {
if ! env . with . get ( ) & & ! env . poisoned . get ( ) {
return Ok ( ( ) ) ;
}
}
for env_index in ( locator . environment_index .. self . vm . environments . stack . len ( ) ) . rev ( ) {
match self . environment_expect ( env_index ) {
match self . environment_expect ( env_index ) {
Environment ::Declarative ( env ) = > {
Environment ::Declarative ( env ) = > {
if env . poisoned . get ( ) {
if env . poisoned . get ( ) {
let compile = env . compile . borrow ( ) ;
let compile = env . compile . borrow ( ) ;
if compile . is_function ( ) {
if compile . is_function ( ) {
if let Some ( b ) = compile . get_binding ( name ) {
if let Some ( b ) = compile . get_binding ( locator . name ) {
environment_index = b . environment_index ;
locator . environment_index = b . environment_index ;
binding_index = b . binding_index ;
locator . binding_index = b . binding_index ;
locator . global = false ;
break ;
break ;
}
}
}
}
@ -861,7 +797,7 @@ impl Context<'_> {
let o = o . clone ( ) ;
let o = o . clone ( ) ;
let key : JsString = self
let key : JsString = self
. interner ( )
. interner ( )
. resolve_expect ( name . sym ( ) )
. resolve_expect ( locator . name . sym ( ) )
. into_common ( false ) ;
. into_common ( false ) ;
if o . has_property ( key . clone ( ) , self ) ? {
if o . has_property ( key . clone ( ) , self ) ? {
if let Some ( unscopables ) = o . get ( JsSymbol ::unscopables ( ) , self ) ? . as_object ( )
if let Some ( unscopables ) = o . get ( JsSymbol ::unscopables ( ) , self ) ? . as_object ( )
@ -870,239 +806,140 @@ impl Context<'_> {
continue ;
continue ;
}
}
}
}
return o . get ( key , self ) . map ( Some ) ;
locator . environment_index = env_index ;
locator . global = false ;
break ;
}
}
}
}
}
}
}
}
Ok ( self
Ok ( ( ) )
. environment_expect ( environment_index )
. declarative_expect ( )
. bindings
. borrow ( )
. get ( binding_index )
. expect ( "binding index must be in range" )
. clone ( ) )
}
}
/// Get the value of a binding by it's name .
/// Checks if the binding pointed by `locator` is initialized.
///
///
/// This only considers function environments that are poisoned.
/// # Panics
/// All other bindings are accessed via indices.
///
pub ( crate ) fn get_value_if_global_poisoned (
/// Panics if the environment or binding index are out of range.
& mut self ,
pub ( crate ) fn is_initialized_binding ( & mut self , locator : & BindingLocator ) -> JsResult < bool > {
name : Identifier ,
if locator . global {
) -> JsResult < Option < JsValue > > {
let key : JsString = self
for env_index in ( 0 .. self . vm . environments . stack . len ( ) ) . rev ( ) {
. interner ( )
let env = self . environment_expect ( env_index ) ;
. resolve_expect ( locator . name . sym ( ) )
. into_common ( false ) ;
match env {
self . global_object ( ) . has_property ( key , self )
} else {
match self . environment_expect ( locator . environment_index ) {
Environment ::Declarative ( env ) = > {
Environment ::Declarative ( env ) = > {
if env . poisoned . get ( ) {
Ok ( env . bindings . borrow ( ) [ locator . binding_index ] . is_some ( ) )
let compile = env . compile . borrow ( ) ;
if compile . is_function ( ) {
if let Some ( b ) = compile . get_binding ( name ) {
return Ok ( self
. environment_expect ( b . environment_index )
. declarative_expect ( )
. bindings
. borrow ( )
. get ( b . binding_index )
. expect ( "binding index must be in range" )
. clone ( ) ) ;
}
} else if ! env . with . get ( ) {
return Ok ( None ) ;
}
}
}
Environment ::Object ( o ) = > {
let o = o . clone ( ) ;
let key : JsString = self
. interner ( )
. resolve_expect ( name . sym ( ) )
. into_common ( false ) ;
if o . has_property ( key . clone ( ) , self ) ? {
if let Some ( unscopables ) = o . get ( JsSymbol ::unscopables ( ) , self ) ? . as_object ( )
{
if unscopables . get ( key . clone ( ) , self ) ? . to_boolean ( ) {
continue ;
}
}
return o . get ( key , self ) . map ( Some ) ;
}
}
}
Environment ::Object ( _ ) = > Ok ( true ) ,
}
}
}
}
Ok ( None )
}
}
/// Set the value of a binding if it is initialized.
/// Get the value of a binding.
/// Return `true` if the value has been set.
///
///
/// # Panics
/// # Panics
///
///
/// Panics if the environment or binding index are out of range.
/// Panics if the environment or binding index are out of range.
pub ( crate ) fn put_value_if_initialized (
pub ( crate ) fn get_binding ( & mut self , locator : BindingLocator ) -> JsResult < Option < JsValue > > {
& mut self ,
if locator . global {
mut environment_index : usize ,
let global = self . global_object ( ) ;
mut binding_index : usize ,
let key : JsString = self
name : Identifier ,
. interner ( )
value : JsValue ,
. resolve_expect ( locator . name . sym ( ) )
strict : bool ,
. into_common ( false ) ;
) -> JsResult < bool > {
if global . has_property ( key . clone ( ) , self ) ? {
for env_index in ( environment_index + 1 .. self . vm . environments . stack . len ( ) ) . rev ( ) {
global . get ( key , self ) . map ( Some )
let env = self . environment_expect ( env_index ) ;
} else {
Ok ( None )
match env {
}
} else {
match self . environment_expect ( locator . environment_index ) {
Environment ::Declarative ( env ) = > {
Environment ::Declarative ( env ) = > {
if env . poisoned . get ( ) {
Ok ( env . bindings . borrow ( ) [ locator . binding_index ] . clone ( ) )
let compile = env . compile . borrow ( ) ;
if compile . is_function ( ) {
if let Some ( b ) = compile . get_binding ( name ) {
environment_index = b . environment_index ;
binding_index = b . binding_index ;
break ;
}
}
} else if ! env . with . get ( ) {
break ;
}
}
}
Environment ::Object ( o ) = > {
Environment ::Object ( obj ) = > {
let o = o . clone ( ) ;
let obj = obj . clone ( ) ;
let key : JsString = self
let key : JsString = self
. interner ( )
. interner ( )
. resolve_expect ( name . sym ( ) )
. resolve_expect ( locator . name . sym ( ) )
. into_common ( false ) ;
. into_common ( false ) ;
if o . has_property ( key . clone ( ) , self ) ? {
obj . get ( key , self ) . map ( Some )
if let Some ( unscopables ) = o . get ( JsSymbol ::unscopables ( ) , self ) ? . as_object ( )
{
if unscopables . get ( key . clone ( ) , self ) ? . to_boolean ( ) {
continue ;
}
}
return o . set ( key , value , strict , self ) ;
}
}
}
}
}
}
}
let mut bindings = self
. environment_expect ( environment_index )
. declarative_expect ( )
. bindings
. borrow_mut ( ) ;
let binding = bindings
. get_mut ( binding_index )
. expect ( "binding index must be in range" ) ;
if binding . is_none ( ) {
Ok ( false )
} else {
* binding = Some ( value ) ;
Ok ( true )
}
}
}
/// Set the value of a binding by it's name.
/// Sets the value of a binding.
///
/// This only considers function environments that are poisoned.
/// All other bindings are set via indices.
///
///
/// # Panics
/// # Panics
///
///
/// Panics if the environment or binding index are out of range.
/// Panics if the environment or binding index are out of range.
pub ( crate ) fn put_value_if_global_poisoned (
#[ track_caller ]
pub ( crate ) fn set_binding (
& mut self ,
& mut self ,
name : Identifie r,
locator : BindingLocator ,
value : & JsValue ,
value : JsValue ,
strict : bool ,
strict : bool ,
) -> JsResult < bool > {
) -> JsResult < ( ) > {
for env_index in ( 0 .. self . vm . environments . stack . len ( ) ) . rev ( ) {
if locator . global {
let env = self . environment_expect ( env_index ) ;
let key = self
. interner ( )
match env {
. resolve_expect ( locator . name ( ) . sym ( ) )
Environment ::Declarative ( env ) = > {
. into_common ::< JsString > ( false ) ;
if env . poisoned . get ( ) {
let compile = env . compile . borrow ( ) ;
self . global_object ( ) . set ( key , value , strict , self ) ? ;
if compile . is_function ( ) {
} else {
if let Some ( b ) = compile . get_binding ( name ) {
match self . environment_expect ( locator . environment_index ) {
let mut bindings = self
Environment ::Declarative ( decl ) = > {
. environment_expect ( b . environment_index )
decl . bindings . borrow_mut ( ) [ locator . binding_index ] = Some ( value ) ;
. declarative_expect ( )
. bindings
. borrow_mut ( ) ;
let binding = bindings
. get_mut ( b . binding_index )
. expect ( "binding index must be in range" ) ;
* binding = Some ( value . clone ( ) ) ;
return Ok ( true ) ;
}
}
} else if ! env . with . get ( ) {
return Ok ( false ) ;
}
}
}
Environment ::Object ( o ) = > {
Environment ::Object ( obj ) = > {
let o = o . clone ( ) ;
let obj = obj . clone ( ) ;
let key : JsString = self
let key : JsString = self
. interner ( )
. interner ( )
. resolve_expect ( name . sym ( ) )
. resolve_expect ( locator . name . sym ( ) )
. into_common ( false ) ;
. into_common ( false ) ;
if o . has_property ( key . clone ( ) , self ) ? {
if let Some ( unscopables ) = o . get ( JsSymbol ::unscopables ( ) , self ) ? . as_object ( )
obj . set ( key , value , strict , self ) ? ;
{
if unscopables . get ( key . clone ( ) , self ) ? . to_boolean ( ) {
continue ;
}
}
return o . set ( key , value . clone ( ) , strict , self ) ;
}
}
}
}
}
}
}
Ok ( false )
Ok ( ( ) )
}
}
/// Delete a binding form an object environment if it exists.
/// Deletes a binding if it exists.
///
///
/// Returns a tuple of `(found, deleted)`.
/// Returns `true` if the binding was deleted.
pub ( crate ) fn delete_binding_from_object_environment (
///
& mut self ,
/// # Panics
name : Identifier ,
///
) -> JsResult < ( bool , bool ) > {
/// Panics if the environment or binding index are out of range.
for env_index in ( 0 .. self . vm . environments . stack . len ( ) ) . rev ( ) {
pub ( crate ) fn delete_binding ( & mut self , locator : BindingLocator ) -> JsResult < bool > {
let env = self . environment_expect ( env_index ) ;
if locator . is_global ( ) {
let key : JsString = self
match env {
. interner ( )
Environment ::Object ( o ) = > {
. resolve_expect ( locator . name ( ) . sym ( ) )
let o = o . clone ( ) ;
. into_common ::< JsString > ( false ) ;
self . global_object ( ) . __delete__ ( & key . into ( ) , self )
} else {
match self . environment_expect ( locator . environment_index ) {
Environment ::Declarative ( _ ) = > Ok ( false ) ,
Environment ::Object ( obj ) = > {
let obj = obj . clone ( ) ;
let key : JsString = self
let key : JsString = self
. interner ( )
. interner ( )
. resolve_expect ( name . sym ( ) )
. resolve_expect ( locator . name . sym ( ) )
. into_common ( false ) ;
. into_common ( false ) ;
if o . has_property ( key . clone ( ) , self ) ? {
if let Some ( unscopables ) = o . get ( JsSymbol ::unscopables ( ) , self ) ? . as_object ( )
obj . __delete__ ( & key . into ( ) , self )
{
if unscopables . get ( key . clone ( ) , self ) ? . to_boolean ( ) {
continue ;
}
}
return Ok ( ( true , o . __delete__ ( & key . into ( ) , self ) ? ) ) ;
}
}
Environment ::Declarative ( env ) = > {
if ! env . with . get ( ) {
return Ok ( ( false , false ) ) ;
}
}
}
}
}
}
}
Ok ( ( false , false ) )
}
}
/// Return the environment at the given index. Panics if the index is out of range.
/// Return the environment at the given index. Panics if the index is out of range.