Skip to content

Commit

Permalink
Compute stack size
Browse files Browse the repository at this point in the history
  • Loading branch information
playXE committed Jul 26, 2021
1 parent c3f77dc commit 0f5d078
Show file tree
Hide file tree
Showing 9 changed files with 364 additions and 22 deletions.
37 changes: 20 additions & 17 deletions crates/starlight/src/bytecompiler.rs
Expand Up @@ -115,6 +115,7 @@ pub enum Val {
#[derive(Debug)]
pub enum CompileError {
NotYetImpl(String),
Val(JsValue),
}

pub struct ByteCompiler {
Expand Down Expand Up @@ -444,15 +445,19 @@ impl ByteCompiler {
x => Err(CompileError::NotYetImpl(format!("NYI: Access {:?}", x))),
}
}
pub fn finish(&mut self, ctx: GcPointer<Context>) -> GcPointer<CodeBlock> {
pub fn finish(&mut self, ctx: GcPointer<Context>) -> Result<GcPointer<CodeBlock>, JsValue> {
self.code.compute_stack_size(ctx)?;

if ctx.vm.options.dump_bytecode {
let mut buf = String::new();
let name = ctx.description(self.code.name);
self.code.display_to(&mut buf).unwrap();
eprintln!("Code block '{}' at {:p}: \n {}", name, self.code, buf);
}

self.code.literals_ptr = self.code.literals.as_ptr();
self.code

Ok(self.code)
}
pub fn compile_fn(
&mut self,
Expand Down Expand Up @@ -561,12 +566,7 @@ impl ByteCompiler {
compiler.emit(Opcode::OP_PUSH_UNDEF, &[], false);
compiler.emit(Opcode::OP_RET, &[], false);
//compiler.compile(&script.body);
let mut code = compiler.finish(ctx);

//let mut code = ByteCompiler::compile_script(&mut *vmref, &script, path.to_owned());

//code.display_to(&mut OutBuf).unwrap();

let code = compiler.finish(ctx).map_err(|x| CompileError::Val(x))?;
let env = crate::vm::environment::Environment::new(ctx, 0);
let fun = JsVMFunction::new(ctx, code, env);
Ok(JsValue::new(fun))
Expand Down Expand Up @@ -654,8 +654,7 @@ impl ByteCompiler {
compiler.emit(Opcode::OP_INITIAL_YIELD, &[], false);
}
compiler.compile_fn(ctx, function)?;
compiler.finish(ctx);

compiler.finish(ctx).map_err(|x| CompileError::Val(x))?;
let ix = if expr {
ix as u32
} else {
Expand Down Expand Up @@ -897,8 +896,7 @@ impl ByteCompiler {
}
compiler.emit(Opcode::OP_PUSH_UNDEF, &[], false);
compiler.emit(Opcode::OP_RET, &[], false);
let result = compiler.finish(ctx);

let mut result = compiler.finish(ctx).map_err(|x| CompileError::Val(x))?;
Ok(result)
}
pub fn compile_script(
Expand Down Expand Up @@ -941,8 +939,7 @@ impl ByteCompiler {
compiler.pop_scope();
compiler.emit(Opcode::OP_PUSH_UNDEF, &[], false);
compiler.emit(Opcode::OP_RET, &[], false);
let result = compiler.finish(ctx);

let mut result = compiler.finish(ctx).map_err(|x| CompileError::Val(x))?;
Ok(result)
}

Expand Down Expand Up @@ -986,8 +983,7 @@ impl ByteCompiler {
compiler.pop_scope();
compiler.emit(Opcode::OP_PUSH_UNDEF, &[], false);
compiler.emit(Opcode::OP_RET, &[], false);
let result = compiler.finish(ctx);

let mut result = compiler.finish(ctx).map_err(|x| CompileError::Val(x))?;
Ok(result)
}

Expand Down Expand Up @@ -1713,11 +1709,17 @@ impl ByteCompiler {
x if is_builtin_call(x, self.builtins) => {
if let Expr::Call(call) = x {
self.handle_builtin_call(ctx, call)?;
if !used {
self.emit(Opcode::OP_POP, &[], false);
}
}
}
x if is_codegen_plugin_call(ctx, x, self.builtins) => {
if let Expr::Call(call) = x {
self.handle_codegen_plugin_call(ctx, call)?;
if !used {
self.emit(Opcode::OP_POP, &[], false);
}
}
}
Expr::Call(call) if !is_builtin_call(expr, self.builtins) => {
Expand Down Expand Up @@ -2116,7 +2118,8 @@ impl ByteCompiler {
compiler.emit(Opcode::OP_RET, &[], false);
}
}
let code = compiler.finish(ctx);
let code = compiler.finish(ctx).map_err(|x| CompileError::Val(x))?;

let ix = self.code.codes.len();
self.code.codes.push(code);
let _nix = self.get_sym(name);
Expand Down
1 change: 1 addition & 0 deletions crates/starlight/src/gc/snapshot/deserializer.rs
Expand Up @@ -1563,6 +1563,7 @@ impl Deserializable for CodeBlock {
var_count,
param_count,
is_constructor,
stack_size: u32::deserialize_inplace(deser),
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/starlight/src/gc/snapshot/serializer.rs
Expand Up @@ -640,6 +640,7 @@ impl Serializable for CodeBlock {
self.path.to_string().serialize(serializer);
self.is_generator.serialize(serializer);
self.is_async.serialize(serializer);
self.stack_size.serialize(serializer);
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/starlight/src/jsrt/function.rs
Expand Up @@ -65,8 +65,8 @@ pub fn function_prototype(ctx: GcPointer<Context>, args: &Arguments) -> Result<J
} else {
format!("{{ {} }}", args.at(args.size() - 1).to_string(ctx)?)
};
let rel_path = unsafe { (*ctx.stack.current).code_block.unwrap().path.clone() };
ByteCompiler::compile_code(ctx, &params, &rel_path, body, false)
//let rel_path = unsafe { (*ctx.stack.current).code_block.unwrap().path.clone() };
ByteCompiler::compile_code(ctx, &params, &".", body, false)
.map_err(|e| JsValue::from(ctx.new_syntax_error(format!("Compile Error {:?}", e))))
}

Expand Down
2 changes: 2 additions & 0 deletions crates/starlight/src/vm/builtins.rs
Expand Up @@ -90,3 +90,5 @@ pub type Builtin =
unsafe fn(GcPointer<Context>, &mut CallFrame, &mut *mut u8, u32, u8) -> Result<(), JsValue>;

pub static BUILTIN_FUNCS: [Builtin; 1] = [reflect_apply];

pub const BUILTIN_ARGS: [usize; 1] = [3];

0 comments on commit 0f5d078

Please sign in to comment.