diff options
Diffstat (limited to 'src/js_parser.zig')
-rw-r--r-- | src/js_parser.zig | 216 |
1 files changed, 86 insertions, 130 deletions
diff --git a/src/js_parser.zig b/src/js_parser.zig index 449d1cbab..fce928a6e 100644 --- a/src/js_parser.zig +++ b/src/js_parser.zig @@ -21241,42 +21241,50 @@ fn NewParser_( parts[parts.len - 1].stmts = new_stmts_list; }, - // This becomes + // This transforms the user's code into. // - // (function (module, exports, require) { + // (function (exports, require, module, __filename, __dirname) { + // ... + // }).call( + // this.module.exports, + // this.module.exports, + // this.require, + // this.module, + // this.__filename, + // this.__dirname, + // ); // - // })(module, exports, require); + // `this` is a `CommonJSFunctionArgumentsStructure` + // which is initialized in `evaluateCommonJSModuleOnce` .bun_js => { 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), - }, + 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.module_ref }, logger.Loc.Empty) }, + Arg{ .binding = p.b(B.Identifier{ .ref = p.filename_ref }, logger.Loc.Empty) }, + Arg{ .binding = p.b(B.Identifier{ .ref = p.dirname_ref }, logger.Loc.Empty) }, + }; + + const cjsArguments = Expr{ + .data = .{ .e_this = .{} }, + .loc = logger.Loc.Empty, }; + var total_stmts_count: usize = 0; for (parts) |part| { total_stmts_count += part.stmts.len; } var stmts_to_copy = allocator.alloc(Stmt, total_stmts_count) catch unreachable; - var remaining_stmts = stmts_to_copy; - for (parts) |part| { - for (part.stmts, remaining_stmts[0..part.stmts.len]) |src, *dest| { - dest.* = src; + { + var remaining_stmts = stmts_to_copy; + for (parts) |part| { + for (part.stmts, remaining_stmts[0..part.stmts.len]) |src, *dest| { + dest.* = src; + } + remaining_stmts = remaining_stmts[part.stmts.len..]; } - remaining_stmts = remaining_stmts[part.stmts.len..]; } const wrapper = p.newExpr( @@ -21284,147 +21292,57 @@ fn NewParser_( .func = G.Fn{ .name = null, .open_parens_loc = logger.Loc.Empty, - .args = args, + .args = args[0..5], .body = .{ .loc = logger.Loc.Empty, .stmts = stmts_to_copy }, .flags = Flags.Function.init(.{ .is_export = false }), }, }, logger.Loc.Empty, ); - const cjsGlobal = p.newSymbol(.unbound, "$_BunCommonJSModule_$") catch unreachable; - var all_call_args = allocator.alloc(Expr, 8) catch unreachable; + const this_module = p.newExpr( E.Dot{ .name = "module", - .target = p.newExpr(E.Identifier{ .ref = cjsGlobal }, logger.Loc.Empty), + .target = cjsArguments, .name_loc = logger.Loc.Empty, }, logger.Loc.Empty, ); - var bind_args = all_call_args[0..1]; - bind_args[0] = this_module; - var bind_resolve_args = all_call_args[1..2]; - var call_args = all_call_args[2..]; - - const module_id = p.newExpr(E.Dot{ - .name = "id", - .target = this_module, - .name_loc = logger.Loc.Empty, - }, logger.Loc.Empty); - - bind_resolve_args[0] = module_id; - const get_require = p.newExpr( + const module_exports = p.newExpr( E.Dot{ - .name = "require", + .name = "exports", .target = this_module, .name_loc = logger.Loc.Empty, }, logger.Loc.Empty, ); - const create_binding = p.newExpr( - E.Call{ - .target = p.newExpr(E.Dot{ - .name = "bind", - .name_loc = logger.Loc.Empty, - .target = get_require, - }, logger.Loc.Empty), - .args = bun.BabyList(Expr).init(bind_args), - }, - logger.Loc.Empty, - ); - - const get_resolve = p.newExpr(E.Dot{ - .name = "resolve", - .name_loc = logger.Loc.Empty, - .target = get_require, - }, logger.Loc.Empty); - - const create_resolve_binding = p.newExpr( - E.Call{ - .target = p.newExpr(E.Dot{ - .name = "bind", - .name_loc = logger.Loc.Empty, - .target = get_resolve, - }, logger.Loc.Empty), - .args = bun.BabyList(Expr).init(bind_resolve_args), - }, - logger.Loc.Empty, - ); - - const require_path = p.newExpr( - E.Dot{ - .name = "path", - .target = get_require, - .name_loc = logger.Loc.Empty, - }, - logger.Loc.Empty, - ); - const assign_binding = p.newExpr( - E.Binary{ - .left = get_require, - .right = create_binding, - .op = .bin_assign, - }, - logger.Loc.Empty, - ); - - const assign_resolve_binding = p.newExpr( - E.Binary{ - .left = get_resolve, - .right = create_resolve_binding, - .op = .bin_assign, - }, - logger.Loc.Empty, - ); - - const assign_id = p.newExpr(E.Binary{ - .left = require_path, - .right = module_id, - .op = .bin_assign, - }, logger.Loc.Empty); - - var create_require = [4]Expr{ - assign_binding, - assign_id, - assign_resolve_binding, - get_require, - }; - - // - // (function(module, exports, require, __dirname, __filename) {}).call(this.exports, this.module, this.exports, this.module.require = this.module.require.bind(module), (this.module.require.id = this.module.id, this.module.require), __dirname, __filename) + var call_args = allocator.alloc(Expr, 6) catch unreachable; call_args[0..6].* = .{ + module_exports, // this.module.exports (this value inside fn) + module_exports, // this.module.exports (arg 1) p.newExpr( E.Dot{ - .name = "exports", - .target = this_module, + .name = "require", + .target = cjsArguments, .name_loc = logger.Loc.Empty, }, logger.Loc.Empty, ), - this_module, + this_module, // this.module p.newExpr( E.Dot{ - .name = "exports", - .target = this_module, + .name = "__filename", + .target = cjsArguments, .name_loc = logger.Loc.Empty, }, logger.Loc.Empty, ), - Expr.joinAllWithComma(&create_require, p.allocator), p.newExpr( E.Dot{ .name = "__dirname", - .target = p.newExpr(E.Identifier{ .ref = cjsGlobal }, logger.Loc.Empty), - .name_loc = logger.Loc.Empty, - }, - logger.Loc.Empty, - ), - p.newExpr( - E.Dot{ - .name = "__filename", - .target = p.newExpr(E.Identifier{ .ref = cjsGlobal }, logger.Loc.Empty), + .target = cjsArguments, .name_loc = logger.Loc.Empty, }, logger.Loc.Empty, @@ -21446,14 +21364,50 @@ fn NewParser_( logger.Loc.Empty, ); - var only_stmt = try p.allocator.alloc(Stmt, 1); - only_stmt[0] = p.s( + var top_level_stmts = p.allocator.alloc(Stmt, 1 + @as(usize, @intFromBool(p.has_import_meta))) catch unreachable; + parts[0].stmts = top_level_stmts; + + // var $Bun_import_meta = this.createImportMeta(this.filename); + if (p.has_import_meta) { + p.import_meta_ref = p.newSymbol(.other, "$Bun_import_meta") catch unreachable; + var decl = allocator.alloc(Decl, 1) catch unreachable; + decl[0] = Decl{ + .binding = Binding.alloc( + p.allocator, + B.Identifier{ + .ref = p.import_meta_ref, + }, + logger.Loc.Empty, + ), + .value = p.newExpr( + E.Call{ + .target = p.newExpr(E.Dot{ + .target = cjsArguments, + .name = "createImportMeta", + .name_loc = logger.Loc.Empty, + }, logger.Loc.Empty), + // reuse the `this.__filename` argument + .args = ExprNodeList.init(call_args[5..6]), + }, + logger.Loc.Empty, + ), + }; + + top_level_stmts[0] = p.s( + S.Local{ + .decls = G.Decl.List.init(decl), + .kind = .k_var, + }, + logger.Loc.Empty, + ); + top_level_stmts = top_level_stmts[1..]; + } + top_level_stmts[0] = p.s( S.SExpr{ .value = call, }, logger.Loc.Empty, ); - parts[0].stmts = only_stmt; parts.len = 1; }, @@ -21984,6 +21938,8 @@ fn NewParser_( // TODO: // .const_values = p.const_values, + + .import_meta_ref = p.import_meta_ref, }; } |