put body of function behind Rc for cheaper copy

This commit is contained in:
Moritz Gmeiner 2024-09-03 17:07:29 +02:00
commit 7c4faebf9c
3 changed files with 8 additions and 5 deletions

View file

@ -32,7 +32,8 @@ impl LoxClass {
let name = init.name().clone();
let closure = init.closure().clone();
let param_names = init.param_names().to_vec();
let mut body = init.body().clone();
let mut body = init.body().as_ref().clone();
if let Stmt::Block { ref mut statements } = body {
statements.push(Stmt::return_stmt(Expr::local_variable("this", 1)));
@ -40,6 +41,8 @@ impl LoxClass {
panic!("Body of init method of class {name} wasn't a block");
}
let body = Rc::new(body);
let new_init = Value::function(LoxFunction::new(name, closure, param_names, body));
methods.insert("init".into(), new_init);