aboutsummaryrefslogtreecommitdiff
path: root/src/js_ast.zig
diff options
context:
space:
mode:
authorGravatar Dylan Conway <35280289+dylan-conway@users.noreply.github.com> 2023-08-04 19:34:09 -0700
committerGravatar GitHub <noreply@github.com> 2023-08-04 19:34:09 -0700
commit6bdee80cfce0205ea1b59679fda93f816a66eed0 (patch)
tree72bfaa306054017d58186343e7bfa56086307d91 /src/js_ast.zig
parent637a38f394704eaea29b503a69d554b5726d6214 (diff)
downloadbun-6bdee80cfce0205ea1b59679fda93f816a66eed0.tar.gz
bun-6bdee80cfce0205ea1b59679fda93f816a66eed0.tar.zst
bun-6bdee80cfce0205ea1b59679fda93f816a66eed0.zip
fix macro string escaping (#3967)
* handle macro escaping * remove printer * use `js_lexer.decodeEscapeSequences`
Diffstat (limited to 'src/js_ast.zig')
-rw-r--r--src/js_ast.zig17
1 files changed, 10 insertions, 7 deletions
diff --git a/src/js_ast.zig b/src/js_ast.zig
index d25c494fa..62089b3b2 100644
--- a/src/js_ast.zig
+++ b/src/js_ast.zig
@@ -25,6 +25,7 @@ const JSONParser = bun.JSON;
const is_bindgen = std.meta.globalOption("bindgen", bool) orelse false;
const ComptimeStringMap = bun.ComptimeStringMap;
const JSPrinter = @import("./js_printer.zig");
+const js_lexer = @import("./js_lexer.zig");
const ThreadlocalArena = @import("./mimalloc_arena.zig").Arena;
/// This is the index to the automatically-generated part containing code that
@@ -2359,7 +2360,6 @@ pub const E = struct {
}
pub fn toJS(s: *String, allocator: std.mem.Allocator, globalObject: *JSC.JSGlobalObject) JSC.JSValue {
- s.resolveRopeIfNeeded(allocator);
if (!s.isPresent()) {
var emp = bun.String.empty;
return emp.toJS(globalObject);
@@ -2372,8 +2372,15 @@ pub const E = struct {
}
{
- var out = bun.String.create(s.slice(allocator));
+ s.resolveRopeIfNeeded(allocator);
+
+ const decoded = js_lexer.decodeUTF8(s.slice(allocator), allocator) catch unreachable;
+ defer allocator.free(decoded);
+
+ var out = bun.String.createUninitializedUTF16(decoded.len);
defer out.deref();
+ @memcpy(@constCast(out.utf16()), decoded);
+
return out.toJS(globalObject);
}
}
@@ -9951,12 +9958,8 @@ pub const Macro = struct {
},
.String => {
var bun_str = value.toBunString(this.global);
- if (bun_str.is8Bit()) {
- if (strings.isAllASCII(bun_str.latin1())) {
- return Expr.init(E.String, E.String.init(this.allocator.dupe(u8, bun_str.latin1()) catch unreachable), this.caller.loc);
- }
- }
+ // encode into utf16 so the printer escapes the string correctly
var utf16_bytes = this.allocator.alloc(u16, bun_str.length()) catch unreachable;
var out_slice = utf16_bytes[0 .. (bun_str.encodeInto(std.mem.sliceAsBytes(utf16_bytes), .utf16le) catch 0) / 2];
return Expr.init(E.String, E.String.init(out_slice), this.caller.loc);