aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/js_ast.zig31
-rw-r--r--test/transpiler/macro-test.test.ts13
-rw-r--r--test/transpiler/macro.ts3
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;
+}