diff options
author | 2023-06-01 18:04:09 -0700 | |
---|---|---|
committer | 2023-06-01 18:04:09 -0700 | |
commit | 3e84f18cc03a6bab898235ff2a9827b47a83aac9 (patch) | |
tree | 160b0bd3cf1b707bf68f500f0a82e85807a0fa99 /src/js_parser.zig | |
parent | 42606d6aed323fa24b6783b24624e9f57cb9ef9d (diff) | |
download | bun-3e84f18cc03a6bab898235ff2a9827b47a83aac9.tar.gz bun-3e84f18cc03a6bab898235ff2a9827b47a83aac9.tar.zst bun-3e84f18cc03a6bab898235ff2a9827b47a83aac9.zip |
Implement `__dirname` and `__filename`, allow direct eval in CommonJS (#3164)
* Implement `__dirname` and `__filename`, allow direct eval in CommonJS
* Fixup dirname and add test
---------
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'src/js_parser.zig')
-rw-r--r-- | src/js_parser.zig | 46 |
1 files changed, 27 insertions, 19 deletions
diff --git a/src/js_parser.zig b/src/js_parser.zig index 0d15ffd9e..5f46507d7 100644 --- a/src/js_parser.zig +++ b/src/js_parser.zig @@ -16483,14 +16483,12 @@ fn NewParser_( // require.resolve(FOO) => import.meta.resolveSync(FOO, pathsObject) return p.newExpr( E.Call{ - .target = p.newExpr( - E.Dot{ - .target = p.newExpr(E.ImportMeta{}, e_.target.loc), - .name = "resolveSync", - .name_loc = e_.target.data.e_dot.name_loc, + .target = Expr{ + .data = .{ + .e_require_resolve_call_target = {}, }, - e_.target.loc, - ), + .loc = e_.target.loc, + }, .args = e_.args, .close_paren_loc = e_.close_paren_loc, }, @@ -21088,15 +21086,23 @@ fn NewParser_( // // })(module, exports, require); .bun_js => { - var args = allocator.alloc(Arg, 3) catch unreachable; - args[0] = Arg{ - .binding = p.b(B.Identifier{ .ref = p.module_ref }, logger.Loc.Empty), - }; - args[1] = Arg{ - .binding = p.b(B.Identifier{ .ref = p.exports_ref }, logger.Loc.Empty), - }; - args[2] = Arg{ - .binding = p.b(B.Identifier{ .ref = p.require_ref }, logger.Loc.Empty), + var args = allocator.alloc(Arg, 5) catch unreachable; + args[0..5].* = .{ + Arg{ + .binding = p.b(B.Identifier{ .ref = p.module_ref }, logger.Loc.Empty), + }, + Arg{ + .binding = p.b(B.Identifier{ .ref = p.exports_ref }, logger.Loc.Empty), + }, + Arg{ + .binding = p.b(B.Identifier{ .ref = p.require_ref }, logger.Loc.Empty), + }, + Arg{ + .binding = p.b(B.Identifier{ .ref = p.dirname_ref }, logger.Loc.Empty), + }, + Arg{ + .binding = p.b(B.Identifier{ .ref = p.filename_ref }, logger.Loc.Empty), + }, }; var total_stmts_count: usize = 0; for (parts) |part| { @@ -21124,15 +21130,17 @@ fn NewParser_( }, logger.Loc.Empty, ); - var call_args = allocator.alloc(Expr, 4) catch unreachable; + var call_args = allocator.alloc(Expr, 6) catch unreachable; // - // (function(module, exports, require) {}).call(exports, module, exports, require) - call_args[0..4].* = .{ + // (function(module, exports, require, __dirname, __filename) {}).call(exports, module, exports, require, __dirname, __filename) + call_args[0..6].* = .{ p.newExpr(E.Identifier{ .ref = p.exports_ref }, logger.Loc.Empty), p.newExpr(E.Identifier{ .ref = p.module_ref }, logger.Loc.Empty), p.newExpr(E.Identifier{ .ref = p.exports_ref }, logger.Loc.Empty), p.newExpr(E.Identifier{ .ref = p.require_ref }, logger.Loc.Empty), + p.newExpr(E.Identifier{ .ref = p.dirname_ref }, logger.Loc.Empty), + p.newExpr(E.Identifier{ .ref = p.filename_ref }, logger.Loc.Empty), }; const call = p.newExpr( |