Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove some environment clones #3884

Merged
merged 1 commit into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/engine/src/builtins/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ impl Eval {
}
});

let var_environment = context.vm.environments.outer_function_environment();
let var_environment = context.vm.environments.outer_function_environment().clone();
let mut var_env = var_environment.compile_env();

let lex_env = context.vm.environments.current_compile_environment();
Expand Down
2 changes: 1 addition & 1 deletion core/engine/src/builtins/function/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl MappedArguments {
binding_indices: &[Option<u32>],
arguments_list: &[JsValue],
env: &Gc<DeclarativeEnvironment>,
context: &mut Context,
context: &Context,
) -> JsObject {
// 1. Assert: formals does not contain a rest parameter, any binding patterns, or any initializers.
// It may contain duplicate identifiers.
Expand Down
17 changes: 8 additions & 9 deletions core/engine/src/environments/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,27 +77,27 @@ impl EnvironmentStack {
}

/// Gets the current global environment.
pub(crate) fn global(&self) -> Gc<DeclarativeEnvironment> {
let env = self.stack[0].clone();
pub(crate) fn global(&self) -> &Gc<DeclarativeEnvironment> {
let env = &self.stack[0];

match env {
Environment::Declarative(ref env) => env.clone(),
Environment::Declarative(ref env) => env,
Environment::Object(_) => {
unreachable!("first environment should be the global environment")
}
}
}

/// Gets the next outer function environment.
pub(crate) fn outer_function_environment(&self) -> Gc<DeclarativeEnvironment> {
pub(crate) fn outer_function_environment(&self) -> &Gc<DeclarativeEnvironment> {
for env in self
.stack
.iter()
.filter_map(Environment::as_declarative)
.rev()
{
if let DeclarativeEnvironmentKind::Function(_) = &env.kind() {
return env.clone();
return env;
}
}
self.global()
Expand Down Expand Up @@ -288,11 +288,10 @@ impl EnvironmentStack {
///
/// Panics if no environment exists on the stack.
#[track_caller]
pub(crate) fn current(&self) -> Environment {
pub(crate) fn current_ref(&self) -> &Environment {
self.stack
.last()
.expect("global environment must always exist")
.clone()
}

/// Get the compile environment for the current runtime environment.
Expand Down Expand Up @@ -502,7 +501,7 @@ impl Context {
/// are completely removed of runtime checks because the specification guarantees that runtime
/// semantics cannot add or remove lexical bindings.
pub(crate) fn find_runtime_binding(&mut self, locator: &mut BindingLocator) -> JsResult<()> {
let current = self.vm.environments.current();
let current = self.vm.environments.current_ref();
if let Some(env) = current.as_declarative() {
if !env.with() && !env.poisoned() {
return Ok(());
Expand Down Expand Up @@ -553,7 +552,7 @@ impl Context {
&mut self,
locator: &BindingLocator,
) -> JsResult<Option<JsObject>> {
let current = self.vm.environments.current();
let current = self.vm.environments.current_ref();
if let Some(env) = current.as_declarative() {
if !env.with() {
return Ok(None);
Expand Down
4 changes: 2 additions & 2 deletions core/engine/src/module/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1665,7 +1665,7 @@ impl SourceTextModule {
BindingName::Name(name) => context
.vm
.environments
.current()
.current_ref()
.declarative_expect()
.kind()
.as_module()
Expand Down Expand Up @@ -1704,7 +1704,7 @@ impl SourceTextModule {

let env = frame
.environments
.current()
.current_ref()
.as_declarative()
.cloned()
.expect("frame must have a declarative environment");
Expand Down
2 changes: 1 addition & 1 deletion core/engine/src/module/synthetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ impl SyntheticModule {
}

let env = envs
.current()
.current_ref()
.as_declarative()
.cloned()
.expect("should have the module environment");
Expand Down
2 changes: 1 addition & 1 deletion core/engine/src/vm/opcode/arguments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Operation for CreateMappedArgumentsObject {
let code = context.vm.frame().code_block().clone();
let args = context.vm.frame().arguments(&context.vm).to_vec();

let env = context.vm.environments.current();
let env = context.vm.environments.current_ref();
let arguments = MappedArguments::new(
&function_object,
&code.mapped_arguments_binding_indices,
Expand Down
Loading