Skip to content

Commit

Permalink
Add some utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
raskad committed Mar 20, 2023
1 parent 6477f40 commit 25301e2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 69 deletions.
100 changes: 33 additions & 67 deletions boa_engine/src/environments/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,12 @@ impl Environment {
Self::Object(_) => None,
}
}

/// Returns the declarative environment and panic if it is not one.
pub(crate) fn declarative_expect(&self) -> &Gc<DeclarativeEnvironment> {
self.as_declarative()
.expect("environment must be declarative")
}
}

impl DeclarativeEnvironmentStack {
Expand Down Expand Up @@ -560,8 +566,7 @@ impl DeclarativeEnvironmentStack {
self.stack
.last()
.expect("global environment must always exist")
.as_declarative()
.expect("global environment must always exist")
.declarative_expect()
.clone()
}

Expand Down Expand Up @@ -643,8 +648,7 @@ impl DeclarativeEnvironmentStack {
self.stack
.get(environment_index)
.expect("environment index must be in range")
.as_declarative()
.expect("environment must be declarative")
.declarative_expect()
.bindings
.borrow()
.get(binding_index)
Expand All @@ -667,8 +671,7 @@ impl DeclarativeEnvironmentStack {
.stack
.get(environment_index)
.expect("environment index must be in range")
.as_declarative()
.expect("environment must be declarative")
.declarative_expect()
.bindings
.borrow_mut();
let binding = bindings
Expand All @@ -692,8 +695,7 @@ impl DeclarativeEnvironmentStack {
.stack
.get(environment_index)
.expect("environment index must be in range")
.as_declarative()
.expect("environment must be declarative")
.declarative_expect()
.bindings
.borrow_mut();
let binding = bindings
Expand Down Expand Up @@ -857,13 +859,7 @@ impl Context<'_> {
name: Identifier,
) -> JsResult<Option<JsValue>> {
for env_index in (environment_index + 1..self.realm.environments.stack.len()).rev() {
match self
.realm
.environments
.stack
.get(env_index)
.expect("environment index must be in range")
{
match self.environment_expect(env_index) {
Environment::Declarative(env) => {
if env.poisoned.get() {
let compile = env.compile.borrow();
Expand Down Expand Up @@ -898,13 +894,8 @@ impl Context<'_> {
}

Ok(self
.realm
.environments
.stack
.get(environment_index)
.expect("environment index must be in range")
.as_declarative()
.expect("environment must be declarative")
.environment_expect(environment_index)
.declarative_expect()
.bindings
.borrow()
.get(binding_index)
Expand All @@ -921,12 +912,7 @@ impl Context<'_> {
name: Identifier,
) -> JsResult<Option<JsValue>> {
for env_index in (0..self.realm.environments.stack.len()).rev() {
let env = self
.realm
.environments
.stack
.get(env_index)
.expect("environment index must be in range");
let env = self.environment_expect(env_index);

match env {
Environment::Declarative(env) => {
Expand All @@ -935,13 +921,8 @@ impl Context<'_> {
if compile.is_function() {
if let Some(b) = compile.get_binding(name) {
return Ok(self
.realm
.environments
.stack
.get(b.environment_index)
.expect("environment index must be in range")
.as_declarative()
.expect("environment must be declarative")
.environment_expect(b.environment_index)
.declarative_expect()
.bindings
.borrow()
.get(b.binding_index)
Expand Down Expand Up @@ -989,12 +970,8 @@ impl Context<'_> {
value: JsValue,
) -> JsResult<bool> {
for env_index in (environment_index + 1..self.realm.environments.stack.len()).rev() {
let env = self
.realm
.environments
.stack
.get(env_index)
.expect("environment index must be in range");
let env = self.environment_expect(env_index);

match env {
Environment::Declarative(env) => {
if env.poisoned.get() {
Expand Down Expand Up @@ -1030,13 +1007,8 @@ impl Context<'_> {
}

let mut bindings = self
.realm
.environments
.stack
.get(environment_index)
.expect("environment index must be in range")
.as_declarative()
.expect("msg")
.environment_expect(environment_index)
.declarative_expect()
.bindings
.borrow_mut();
let binding = bindings
Expand Down Expand Up @@ -1064,12 +1036,7 @@ impl Context<'_> {
value: &JsValue,
) -> JsResult<bool> {
for env_index in (0..self.realm.environments.stack.len()).rev() {
let env = self
.realm
.environments
.stack
.get(env_index)
.expect("environment index must be in range");
let env = self.environment_expect(env_index);

match env {
Environment::Declarative(env) => {
Expand All @@ -1078,13 +1045,8 @@ impl Context<'_> {
if compile.is_function() {
if let Some(b) = compile.get_binding(name) {
let mut bindings = self
.realm
.environments
.stack
.get(b.environment_index)
.expect("environment index must be in range")
.as_declarative()
.expect("environment must be declarative")
.environment_expect(b.environment_index)
.declarative_expect()
.bindings
.borrow_mut();
let binding = bindings
Expand Down Expand Up @@ -1128,12 +1090,7 @@ impl Context<'_> {
name: Identifier,
) -> JsResult<(bool, bool)> {
for env_index in (0..self.realm.environments.stack.len()).rev() {
let env = self
.realm
.environments
.stack
.get(env_index)
.expect("environment index must be in range");
let env = self.environment_expect(env_index);

match env {
Environment::Object(o) => {
Expand Down Expand Up @@ -1162,4 +1119,13 @@ impl Context<'_> {

Ok((false, false))
}

/// Return the environment at the given index. Panics if the index is out of range.
fn environment_expect(&self, index: usize) -> &Environment {
self.realm
.environments
.stack
.get(index)
.expect("environment index must be in range")
}
}
3 changes: 1 addition & 2 deletions boa_engine/src/vm/code_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1475,8 +1475,7 @@ impl JsObject {
.into())
} else {
let function_env = environment
.as_declarative()
.expect("must be function environment")
.declarative_expect()
.slots()
.expect("must be function environment")
.as_function_slots()
Expand Down

0 comments on commit 25301e2

Please sign in to comment.