From 6bdee80cfce0205ea1b59679fda93f816a66eed0 Mon Sep 17 00:00:00 2001 From: Dylan Conway <35280289+dylan-conway@users.noreply.github.com> Date: Fri, 4 Aug 2023 19:34:09 -0700 Subject: fix macro string escaping (#3967) * handle macro escaping * remove printer * use `js_lexer.decodeEscapeSequences` --- src/js_ast.zig | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'src/js_ast.zig') 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); -- cgit v1.2.3