From 39201aa31d267f9bdd21f544e1c0260d57f85df4 Mon Sep 17 00:00:00 2001 From: Chengzhong Wu Date: Fri, 12 Sep 2025 11:20:48 +0100 Subject: [PATCH] vm: expose hasTopLevelAwait on SourceTextModule Expose `hasTopLevelAwait` and `hasAsyncGraph` on `vm.SourceTextModule`. `hasAsyncGraph` requires the module to be instantiated first. --- doc/api/vm.md | 30 +++++++++ lib/internal/vm/module.js | 13 ++++ test/parallel/test-vm-module-hasasyncgraph.js | 67 +++++++++++++++++++ .../test-vm-module-hastoplevelawait.js | 59 ++++++++++++++++ 4 files changed, 169 insertions(+) create mode 100644 test/parallel/test-vm-module-hasasyncgraph.js create mode 100644 test/parallel/test-vm-module-hastoplevelawait.js diff --git a/doc/api/vm.md b/doc/api/vm.md index dd36293859fe1e..81f8c9d1f76372 100644 --- a/doc/api/vm.md +++ b/doc/api/vm.md @@ -920,6 +920,36 @@ to disallow any changes to it. Corresponds to the `[[RequestedModules]]` field of [Cyclic Module Record][]s in the ECMAScript specification. +### `sourceTextModule.hasAsyncGraph()` + + + +* Returns: {boolean} + +Iterates over the dependency graph and returns `true` if any module in its +dependencies or this module itself contains top-level `await` expressions, +otherwise returns `false`. + +The search may be slow if the graph is big enough. + +This requires the module to be instantiated first. If the module is not +instantiated yet, an error will be thrown. + +### `sourceTextModule.hasTopLevelAwait()` + + + +* Returns: {boolean} + +Returns whether the module itself contains any top-level `await` expressions. + +This corresponds to the field `[[HasTLA]]` in [Cyclic Module Record][] in the +ECMAScript specification. + ### `sourceTextModule.instantiate()`