Skip to content

Commit cab59e7

Browse files
committed
fix surrogate
1 parent a23315a commit cab59e7

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

crates/codegen/src/compile.rs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7622,23 +7622,8 @@ impl Compiler {
76227622
self.compile_expr_tstring(tstring)?;
76237623
}
76247624
ast::Expr::StringLiteral(string) => {
7625-
let value = string.value.to_str();
7626-
if value.contains(char::REPLACEMENT_CHARACTER) {
7627-
let value = string
7628-
.value
7629-
.iter()
7630-
.map(|lit| {
7631-
let source = self.source_file.slice(lit.range);
7632-
crate::string_parser::parse_string_literal(source, lit.flags.into())
7633-
})
7634-
.collect();
7635-
// might have a surrogate literal; should reparse to be sure
7636-
self.emit_load_const(ConstantData::Str { value });
7637-
} else {
7638-
self.emit_load_const(ConstantData::Str {
7639-
value: value.into(),
7640-
});
7641-
}
7625+
let value = self.compile_string_value(string);
7626+
self.emit_load_const(ConstantData::Str { value });
76427627
}
76437628
ast::Expr::BytesLiteral(bytes) => {
76447629
let iter = bytes.value.iter().flat_map(|x| x.iter().copied());
@@ -8569,6 +8554,24 @@ impl Compiler {
85698554

85708555
// fn block_done()
85718556

8557+
/// Convert a string literal AST node to Wtf8Buf, handling surrogates correctly.
8558+
fn compile_string_value(&self, string: &ast::ExprStringLiteral) -> Wtf8Buf {
8559+
let value = string.value.to_str();
8560+
if value.contains(char::REPLACEMENT_CHARACTER) {
8561+
// Might have a surrogate literal; reparse from source to preserve them
8562+
string
8563+
.value
8564+
.iter()
8565+
.map(|lit| {
8566+
let source = self.source_file.slice(lit.range);
8567+
crate::string_parser::parse_string_literal(source, lit.flags.into())
8568+
})
8569+
.collect()
8570+
} else {
8571+
value.into()
8572+
}
8573+
}
8574+
85728575
fn arg_constant(&mut self, constant: ConstantData) -> oparg::ConstIdx {
85738576
let info = self.current_code_info();
85748577
info.metadata.consts.insert_full(constant).0.to_u32().into()
@@ -8598,9 +8601,8 @@ impl Compiler {
85988601
}
85998602
},
86008603
ast::Expr::StringLiteral(s) => {
8601-
constants.push(ConstantData::Str {
8602-
value: s.value.to_string().into(),
8603-
});
8604+
let value = self.compile_string_value(s);
8605+
constants.push(ConstantData::Str { value });
86048606
}
86058607
ast::Expr::BytesLiteral(b) => {
86068608
constants.push(ConstantData::Bytes {

0 commit comments

Comments
 (0)