aboutsummaryrefslogtreecommitdiff
path: root/src/js/builtins/Module.ts
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2023-06-24 06:02:16 -0700
committerGravatar GitHub <noreply@github.com> 2023-06-24 06:02:16 -0700
commitff635551436123022ba3980b39580d53973c80a2 (patch)
tree7eb5292a7157e70dd432518f185bc9c39345ae89 /src/js/builtins/Module.ts
parent069b42a7cc1275969859dc60e7c303528ca2dccb (diff)
downloadbun-ff635551436123022ba3980b39580d53973c80a2.tar.gz
bun-ff635551436123022ba3980b39580d53973c80a2.tar.zst
bun-ff635551436123022ba3980b39580d53973c80a2.zip
Rewrite Bun's runtime CommonJS loader (#3379)
* wip changes for CommonJS * this rewrite is almost complete * even more code * wip * Remove usages of `import.meta.require` from builtins * Remove usages of require * Regenerate * :scissors: builtin rewrite commonjs in printer * Use lazy custom getters for import.meta * fixups * Remove depd * ugh * still crashing * fixup undici * comment out import.meta.require.resolve temporarily not a real solution but it stops the crashes * Redo import.meta.primordials * Builtins now have a `builtin://` protocol in source origin * Seems to work? * Finsih getting rid of primordials * switcharoo * No more function * just one more bug * Update launch.json * Implement `require.main` * :scissors: * Bump WebKit * Fixup import cycles * Fixup improt cycles * export more things * Implement `createCommonJSModule` builtin * More exports * regenerate * i broke some stuff * some of these tests work now * We lost the encoding * Sort of fix zlib * Sort of fix util * Update events.js * bump * bump * bump * Fix missing export in fs * fix some bugs with builtin esm modules (stream, worker_threads, events). its not perfect yet. * fix some other internal module bugs * oops * fix some extra require default stuff * uncomment this file but it crsahes on my machine * tidy code here * fixup tls exports * make simdutf happier * Add hasPrefix binding * Add test for `require.main` * Fix CommonJS evaluation order race condition * Make node:http load faster * Add missing exports to tls.js * Use the getter * Regenerate builtins * Fix assertion failure in Bun.write() * revamp dotEnv parser (#3347) - fixes `strings.indexOfAny()` - fixes OOB array access fixes #411 fixes #2823 fixes #3042 * fix tests for `expect()` (#3384) - extend test job time-out for `darwin-aarch64` * `expect().resolves` and `expect().rejects` (#3318) * Move expect and snapshots to their own files * expect().resolves and expect().rejects * Fix promise being added to unhandled rejection list * Handle timeouts in expect(<promise>) * wip merge * Fix merge issue --------- Co-authored-by: Jarred Sumner <jarred@jarredsumner.com> Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> * fixup min/memcopy (#3388) * Fix crash in builtins * Don't attempt to evaluate modules with no source code * Update WebCoreJSBuiltins.cpp * Update WebCoreJSBuiltins.cpp * Update WebCoreJSBuiltins.cpp * Fix crash * cleanup * Fix test cc @paperdave * Fixup Undici * Fix issue in node:http * Create util-deprecate.mjs * Fix several bugs * Use the identifier * Support error.code in `util.deprecate` * make the CJs loader slightly more resilient * Update WebCoreJSBuiltins.cpp * Fix macros --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Co-authored-by: dave caruso <me@paperdave.net> Co-authored-by: Alex Lam S.L <alexlamsl@gmail.com> Co-authored-by: Ashcon Partovi <ashcon@partovi.net> Co-authored-by: Ciro Spaciari <ciro.spaciari@gmail.com>
Diffstat (limited to 'src/js/builtins/Module.ts')
-rw-r--r--src/js/builtins/Module.ts94
1 files changed, 94 insertions, 0 deletions
diff --git a/src/js/builtins/Module.ts b/src/js/builtins/Module.ts
new file mode 100644
index 000000000..ebfc1c477
--- /dev/null
+++ b/src/js/builtins/Module.ts
@@ -0,0 +1,94 @@
+interface Module {
+ id: string;
+ path: string;
+
+ $require(id: string, mod: any): any;
+ children: Module[];
+}
+
+$getter;
+export function main() {
+ return $requireMap.$get(Bun.main);
+}
+
+export function require(this: Module, id: string) {
+ const existing = $requireMap.$get(id) || $requireMap.$get((id = $resolveSync(id, this.path, false)));
+ if (existing) {
+ // Scenario where this is necessary:
+ //
+ // In an ES Module, we have:
+ //
+ // import "react-dom/server"
+ // import "react"
+ //
+ // Synchronously, the "react" import is created first, and then the
+ // "react-dom/server" import is created. Then, at ES Module link time, they
+ // are evaluated. The "react-dom/server" import is evaluated first, and
+ // require("react") was previously created as an ESM module, so we wait
+ // for the ESM module to load
+ //
+ // ...and then when this code is reached, unless
+ // we evaluate it "early", we'll get an empty object instead of the module
+ // exports.
+ //
+ $evaluateCommonJSModule(existing);
+ return existing.exports;
+ }
+
+ if (id.endsWith(".json") || id.endsWith(".toml") || id.endsWith(".node")) {
+ return $internalRequire(id);
+ }
+
+ let esm = Loader.registry.$get(id);
+ if (esm?.evaluated) {
+ const mod = esm.module;
+ const namespace = Loader.getModuleNamespaceObject(mod);
+ const exports =
+ namespace?.[$commonJSSymbol] === 0 || namespace?.default?.[$commonJSSymbol] === 0 ? namespace.default : namespace;
+ $requireMap.$set(id, $createCommonJSModule(id, exports, true));
+ return exports;
+ }
+
+ // To handle import/export cycles, we need to create a module object and put
+ // it into the map before we import it.
+ const mod = $createCommonJSModule(id, {}, false);
+ $requireMap.$set(id, mod);
+
+ // This is where we load the module. We will see if Module._load and
+ // Module._compile are actually important for compatibility.
+ //
+ // Note: we do not need to wrap this in a try/catch, if it throws the C++ code will
+ // clear the module from the map.
+ //
+ var out = this.$require(id, mod);
+
+ // -1 means we need to lookup the module from the ESM registry.
+ if (out === -1) {
+ try {
+ out = $requireESM(id);
+ } catch (exception) {
+ // Since the ESM code is mostly JS, we need to handle exceptions here.
+ $requireMap.$delete(id);
+ throw exception;
+ }
+
+ esm = Loader.registry.$get(id);
+
+ // If we can pull out a ModuleNamespaceObject, let's do it.
+ if (esm?.evaluated) {
+ const namespace = Loader.getModuleNamespaceObject(esm!.module);
+ return (mod.exports =
+ // if they choose a module
+ namespace?.[$commonJSSymbol] === 0 || namespace?.default?.[$commonJSSymbol] === 0
+ ? namespace.default
+ : namespace);
+ }
+ }
+
+ $evaluateCommonJSModule(mod);
+ return mod.exports;
+}
+
+export function requireResolve(this: Module, id: string) {
+ return $resolveSync(id, this.path, false);
+}