Repository: oven-sh/bun
File: src/ast/Macro.zig (Zig)

Code before:
                    //     value.jsonStringify(this.global, 0, &str);
                    //     var out_buf: []const u8 = std.fmt.bufPrint(iso_string_buf, "{}", .{str}) catch "";
                    //     if (out_buf.len > 2) {
                    //         // trim the quotes
                    //         out_buf = out_buf[1 .. out_buf.len - 1];
                    //     }
                    //     return Expr.init(E.New, E.New{.target = Expr.init(E.Dot{.target = E}) })
                    // }
                },

                .Integer => {
                    return Expr.init(E.Number, E.Number{ .value = @as(f64, @floatFromInt(value.toInt32())) }, this.caller.loc);
                },
                .Double => {
                    return Expr.init(E.Number, E.Number{ .value = value.asNumber() }, this.caller.loc);
                },
                .String => {
                    var bun_str = try value.toBunString(this.global);
                    defer bun_str.deref();

                    // encode into utf16 so the printer escapes the string correctly
                    var utf16_bytes = this.allocator.alloc(u16, bun_str.length()) catch unreachable;
                    const 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 => {
                    const _entry = this.visited.getOrPut(this.allocator, value) catch unreachable;
                    if (_entry.found_existing) {
                        return _entry.value_ptr.*;
                    }

                    const promise = value.asAnyPromise() orelse @panic("Unexpected promise type");

                    this.macro.vm.waitForPromise(promise);

                    const promise_result = promise.result(this.macro.vm.jsc_vm);
                    const rejected = promise.status() == .rejected;

                    if (promise_result.isUndefined() and this.is_top_level) {
                        this.is_top_level = false;
                        return this.caller;
                    }

                    if (rejected or promise_result.isError() or promise_result.isAggregateError(this.global) or promise_result.isException(this.global.vm())) {
                        this.macro.vm.unhandledRejection(this.global, promise_result, promise.asValue());
                        return error.MacroFailed;
                    }
                    this.is_top_level = false;
                    const result = try this.run(promise_result);

                    _entry.value_ptr.* = result;

Review comment (suggestion):
Since we don't use `_entry` anymore, we don't need to create an entry with `getOrPut`. We'll do when we need with `.put`

Code after:
                    //     var out_buf: []const u8 = std.fmt.bufPrint(iso_string_buf, "{}", .{str}) catch "";
                    //     if (out_buf.len > 2) {
                    //         // trim the quotes
                    //         out_buf = out_buf[1 .. out_buf.len - 1];
                    //     }
                    //     return Expr.init(E.New, E.New{.target = Expr.init(E.Dot{.target = E}) })
                    // }
                },

                .Integer => {
                    return Expr.init(E.Number, E.Number{ .value = @as(f64, @floatFromInt(value.toInt32())) }, this.caller.loc);
                },
                .Double => {
                    return Expr.init(E.Number, E.Number{ .value = value.asNumber() }, this.caller.loc);
                },
                .String => {
                    var bun_str = try value.toBunString(this.global);
                    defer bun_str.deref();

                    // encode into utf16 so the printer escapes the string correctly
                    var utf16_bytes = this.allocator.alloc(u16, bun_str.length()) catch unreachable;
                    const 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 => {
                    if (this.visited.get(value)) |cached| {
                        return cached;
                    }

                    const promise = value.asAnyPromise() orelse @panic("Unexpected promise type");

                    this.macro.vm.waitForPromise(promise);

                    const promise_result = promise.result(this.macro.vm.jsc_vm);
                    const rejected = promise.status() == .rejected;

                    if (promise_result.isUndefined() and this.is_top_level) {
                        this.is_top_level = false;
                        return this.caller;
                    }

                    if (rejected or promise_result.isError() or promise_result.isAggregateError(this.global) or promise_result.isException(this.global.vm())) {
                        this.macro.vm.unhandledRejection(this.global, promise_result, promise.asValue());
                        return error.MacroFailed;
                    }
                    this.is_top_level = false;
                    const result = try this.run(promise_result);

                    this.visited.put(this.allocator, value, result) catch unreachable;
                    return result;
                },