Skip to content

Commit

Permalink
preserve this binding in generator functions
Browse files Browse the repository at this point in the history
fixes #93
  • Loading branch information
y21 committed Dec 28, 2024
1 parent 5d023dd commit 3af6a7c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
4 changes: 2 additions & 2 deletions crates/dash_vm/src/eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ use dash_compiler::FunctionCompiler;
use dash_lexer::Lexer;
use dash_middle::compiler::StaticImportKind;
use dash_middle::interner::sym;
use dash_optimizer::type_infer::name_res;
use dash_optimizer::OptLevel;
use dash_optimizer::type_infer::name_res;
use dash_parser::Parser;

use crate::frame::Frame;
use crate::localscope::LocalScope;
use crate::value::object::{NamedObject, Object, PropertyValue};
use crate::value::{Root, Unrooted, Value};
use crate::{throw, Vm};
use crate::{Vm, throw};

#[derive(Debug)]
pub enum EvalError {
Expand Down
10 changes: 6 additions & 4 deletions crates/dash_vm/src/js_std/generator.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::mem;

use crate::dispatch::HandleResult;
use crate::frame::{Frame, This};
use crate::frame::Frame;
use crate::localscope::LocalScope;
use crate::throw;
use crate::value::function::generator::{GeneratorIterator, GeneratorState};
Expand All @@ -18,14 +18,15 @@ pub fn next(cx: CallContext) -> Result<Value, Value> {
let generator = receiver_t::<GeneratorIterator>(cx.scope, &cx.this, "GeneratorIterator.prototype.next")?;
let arg = cx.args.first().unwrap_or_undefined();
let frame = {
let (ip, old_stack, arguments, try_blocks) = match &mut *generator.state().borrow_mut() {
let (ip, old_stack, arguments, try_blocks, this) = match &mut *generator.state().borrow_mut() {
GeneratorState::Finished => return create_generator_value(cx.scope, true, None),
GeneratorState::Running {
ip,
stack,
arguments,
try_blocks,
} => (*ip, mem::take(stack), arguments.take(), mem::take(try_blocks)),
this,
} => (*ip, mem::take(stack), arguments.take(), mem::take(try_blocks), *this),
};

let function = generator.function();
Expand All @@ -39,7 +40,7 @@ pub fn next(cx: CallContext) -> Result<Value, Value> {
let current_sp = cx.scope.stack_size();
cx.scope.try_extend_stack(old_stack).root_err(cx.scope)?;

let mut frame = Frame::from_function(This::Default, function, None, false, arguments);
let mut frame = Frame::from_function(this, function, None, false, arguments);
frame.set_ip(ip);
frame.set_sp(current_sp);

Expand Down Expand Up @@ -99,6 +100,7 @@ pub fn next(cx: CallContext) -> Result<Value, Value> {
stack,
arguments: frame.arguments,
try_blocks,
this: frame.this,
});

create_generator_value(cx.scope, false, Some(value))
Expand Down
5 changes: 1 addition & 4 deletions crates/dash_vm/src/value/function/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@ impl AsyncFunction {
Ok(promise)
}
}
Err(value) => {
let promise = wrap_promise(scope, value);
Err(promise.into())
}
Err(value) => Err(value.into()),
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions crates/dash_vm/src/value/function/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl GeneratorFunction {
&self,
scope: &mut LocalScope,
callee: ObjectId,
_this: This,
this: This,
args: Vec<Value>,
_new_target: Option<ObjectId>,
) -> Result<Value, Unrooted> {
Expand All @@ -50,7 +50,7 @@ impl GeneratorFunction {
scope.stack.drain(sp..).collect::<Vec<_>>()
};

let iter = GeneratorIterator::new(callee, scope, args, arguments, Vec::new());
let iter = GeneratorIterator::new(callee, scope, args, arguments, Vec::new(), this);
Ok(Value::object(scope.register(iter)))
}
}
Expand All @@ -63,6 +63,7 @@ pub enum GeneratorState {
stack: Vec<Value>,
try_blocks: Vec<TryBlock>,
arguments: Option<ObjectId>,
this: This,
},
}

Expand All @@ -84,10 +85,12 @@ unsafe impl Trace for GeneratorState {
stack,
arguments,
try_blocks,
this,
} => {
stack.trace(cx);
arguments.trace(cx);
try_blocks.trace(cx);
this.trace(cx);
}
}
}
Expand All @@ -107,6 +110,7 @@ impl GeneratorIterator {
stack: Vec<Value>,
arguments: Option<ObjectId>,
try_blocks: Vec<TryBlock>,
this: This,
) -> Self {
Self {
function,
Expand All @@ -116,6 +120,7 @@ impl GeneratorIterator {
stack,
arguments,
try_blocks,
this,
}),
}
}
Expand Down

0 comments on commit 3af6a7c

Please sign in to comment.