diff options
author | 2023-07-16 20:32:18 -0700 | |
---|---|---|
committer | 2023-07-16 20:32:18 -0700 | |
commit | 6baa08313cc1347103bc4671be9555608f9b8047 (patch) | |
tree | 07a119b5fe06fc49341f3d00b010503c44b2a056 | |
parent | 131ed0602040d8666c2d5e83fd61c5a682d92ff2 (diff) | |
download | bun-6baa08313cc1347103bc4671be9555608f9b8047.tar.gz bun-6baa08313cc1347103bc4671be9555608f9b8047.tar.zst bun-6baa08313cc1347103bc4671be9555608f9b8047.zip |
Fixes #3641 (#3643)
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
-rw-r--r-- | src/js_ast.zig | 31 | ||||
-rw-r--r-- | test/transpiler/macro-test.test.ts | 13 | ||||
-rw-r--r-- | test/transpiler/macro.ts | 3 |
3 files changed, 44 insertions, 3 deletions
diff --git a/src/js_ast.zig b/src/js_ast.zig index 71e51b5b0..0007cf2ad 100644 --- a/src/js_ast.zig +++ b/src/js_ast.zig @@ -2359,7 +2359,24 @@ pub const E = struct { } pub fn toJS(s: *String, allocator: std.mem.Allocator, globalObject: *JSC.JSGlobalObject) JSC.JSValue { - return s.toZigString(allocator).toValueGC(globalObject); + s.resolveRopeIfNeeded(allocator); + if (!s.isPresent()) { + var emp = bun.String.empty; + return emp.toJS(globalObject); + } + + if (s.is_utf16) { + var out = bun.String.createUninitializedUTF16(s.len()); + defer out.deref(); + @memcpy(@constCast(out.utf16()), s.slice16()); + return out.toJS(globalObject); + } + + { + var out = bun.String.create(s.slice(allocator)); + defer out.deref(); + return out.toJS(globalObject); + } } pub fn toZigString(s: *String, allocator: std.mem.Allocator) JSC.ZigString { @@ -9933,8 +9950,16 @@ pub const Macro = struct { return Expr.init(E.Number, E.Number{ .value = value.asNumber() }, this.caller.loc); }, .String => { - var sliced = value.toSlice(this.global, this.allocator).cloneIfNeeded(this.allocator) catch unreachable; - return Expr.init(E.String, E.String.init(sliced.slice()), this.caller.loc); + 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); + } + } + + 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); }, .Promise => { var _entry = this.visited.getOrPut(this.allocator, value) catch unreachable; diff --git a/test/transpiler/macro-test.test.ts b/test/transpiler/macro-test.test.ts new file mode 100644 index 000000000..2390fac64 --- /dev/null +++ b/test/transpiler/macro-test.test.ts @@ -0,0 +1,13 @@ +import { identity } from "./macro.ts" assert { type: "macro" }; + +test("latin1 string", () => { + expect(identity("©")).toBe("©"); +}); + +test("ascii string", () => { + expect(identity("abc")).toBe("abc"); +}); + +test("utf16 string", () => { + expect(identity("😊 Smiling Face with Smiling Eyes Emoji")).toBe("😊 Smiling Face with Smiling Eyes Emoji"); +}); diff --git a/test/transpiler/macro.ts b/test/transpiler/macro.ts new file mode 100644 index 000000000..8516d7d0d --- /dev/null +++ b/test/transpiler/macro.ts @@ -0,0 +1,3 @@ +export function identity(arg: any) { + return arg; +} |