diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/js_ast.zig | 17 | ||||
-rw-r--r-- | src/js_lexer.zig | 13 |
2 files changed, 23 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); diff --git a/src/js_lexer.zig b/src/js_lexer.zig index a0ad75c7b..e54e738e0 100644 --- a/src/js_lexer.zig +++ b/src/js_lexer.zig @@ -75,6 +75,19 @@ pub const JSONOptions = struct { was_originally_macro: bool = false, }; +pub fn decodeUTF8(bytes: string, allocator: std.mem.Allocator) ![]const u16 { + var log = logger.Log.init(allocator); + defer log.deinit(); + var source = logger.Source.initEmptyFile(""); + var lexer = try NewLexer(.{}).init(&log, source, allocator); + defer lexer.deinit(); + + var buf = std.ArrayList(u16).init(allocator); + try lexer.decodeEscapeSequences(0, bytes, @TypeOf(buf), &buf); + + return buf.items; +} + pub fn NewLexer( comptime json_options: JSONOptions, ) type { |