diff options
Diffstat (limited to 'third_party/rust/jsparagus-emitter/src/script_emitter.rs')
-rw-r--r-- | third_party/rust/jsparagus-emitter/src/script_emitter.rs | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/third_party/rust/jsparagus-emitter/src/script_emitter.rs b/third_party/rust/jsparagus-emitter/src/script_emitter.rs new file mode 100644 index 0000000000..36c24ca594 --- /dev/null +++ b/third_party/rust/jsparagus-emitter/src/script_emitter.rs @@ -0,0 +1,43 @@ +use crate::ast_emitter::AstEmitter; +use crate::emitter::EmitError; + +pub struct ScriptEmitter<'a, FuncT, FuncF, StmtT, StmtF> +where + FuncF: Fn(&mut AstEmitter, &FuncT) -> Result<(), EmitError>, + StmtF: Fn(&mut AstEmitter, &StmtT) -> Result<(), EmitError>, +{ + pub top_level_functions: std::slice::Iter<'a, FuncT>, + pub top_level_function: FuncF, + pub statements: std::slice::Iter<'a, StmtT>, + pub statement: StmtF, +} + +impl<'a, FuncT, FuncF, StmtT, StmtF> ScriptEmitter<'a, FuncT, FuncF, StmtT, StmtF> +where + FuncF: Fn(&mut AstEmitter, &FuncT) -> Result<(), EmitError>, + StmtF: Fn(&mut AstEmitter, &StmtT) -> Result<(), EmitError>, +{ + pub fn emit(self, emitter: &mut AstEmitter) -> Result<(), EmitError> { + let scope_data_map = &emitter.compilation_info.scope_data_map; + + emitter.scope_stack.enter_global( + &mut emitter.emit, + scope_data_map, + self.top_level_functions.len() as u32, + ); + + for fun in self.top_level_functions { + (self.top_level_function)(emitter, fun)?; + } + + for statement in self.statements { + (self.statement)(emitter, statement)?; + } + + emitter.emit.ret_rval(); + + emitter.scope_stack.leave_global(&mut emitter.emit); + + Ok(()) + } +} |