fix: directive 序言字符串按源序先发射且完成值参与收敛

This commit is contained in:
2026-09-14 21:07:10 +00:00
parent 647fb31af7
commit ca5c27574e
3 changed files with 12 additions and 1 deletions
+6
View File
@@ -1870,6 +1870,12 @@ impl Emitter {
// 第二个 sub-pass:发其余所有语句。
let mut last_result: Option<u32> = None;
// directive 序言是 AST 独立字段(顶格字符串字面量,不在 body):作为语句
// 序列的头前缀按源序先于 body 发射。完成值即字符串值本身,参与"最后非空
// 完成值"收敛;严格模式标志由 has_use_strict_directive 独立处理,此处不涉。
for dir in &program.directives {
last_result = Some(self.emit_string_literal_expression(&dir.expression, &mut ctx)?);
}
for stmt in &program.body {
if matches!(stmt, Statement::FunctionDeclaration(_)) {
continue; // Already emitted above
+1 -1
View File
@@ -49,7 +49,7 @@ impl Emitter {
Ok(r)
}
fn emit_string_literal_expression(
pub(crate) fn emit_string_literal_expression(
&self, s: &oxide_parser::StringLiteral, ctx: &mut CompileCtx,
) -> Result<u32, String> {
let idx = ctx
+5
View File
@@ -365,6 +365,11 @@ impl Emitter {
}
// —— body(函数声明 hoisting 先发,随后其余;import 已由 prelude 处理)——
// directive 序言是 AST 独立字段(顶格字符串字面量,不在 body):纯字符串
// 无副作用,按源序先于 body 发射,与脚本 emit_program 的发射口径一致。
for dir in &program.directives {
self.emit_string_literal_expression(&dir.expression, ctx)?;
}
for stmt in body {
if is_hoisted_function_decl(stmt) {
self.emit_statement(stmt, ctx)?;