@@ -799,6 +799,9 @@ struct Worker::Script::Impl {
799
799
using DynamicImportHandler = kj::Function<jsg::Value()>;
800
800
801
801
void configureDynamicImports (jsg::Lock& js, jsg::ModuleRegistry& modules) {
802
+ // This is only used with the original module registry implementation.
803
+ KJ_ASSERT (!FeatureFlags::get (js).getNewModuleRegistry (),
804
+ " legacy dynamic imports must not be used with the new module registry" );
802
805
static auto constexpr handleDynamicImport =
803
806
[](kj::Own<const Worker> worker, DynamicImportHandler handler,
804
807
kj::Maybe<jsg::Ref<jsg::AsyncContextFrame>> asyncContext)
@@ -1266,9 +1269,9 @@ Worker::Script::Script(kj::Own<const Isolate> isolateParam,
1266
1269
context = mContext .getHandle (lock);
1267
1270
recordedLock.setupContext (context);
1268
1271
} else {
1269
- // Although we're going to compile a script independent of context, V8 requires that there be
1270
- // an active context, otherwise it will segfault, I guess. So we create a dummy context.
1271
- // (Undocumented, as usual.)
1272
+ // Although we're going to compile a script independent of context, V8 requires that
1273
+ // there be an active context, otherwise it will segfault, I guess. So we create a
1274
+ // dummy context. (Undocumented, as usual.)
1272
1275
context =
1273
1276
v8::Context::New (lock.v8Isolate , nullptr , v8::ObjectTemplate::New (lock.v8Isolate ));
1274
1277
// We need to set the highest used index in every context we create to be a nullptr
@@ -1284,32 +1287,32 @@ Worker::Script::Script(kj::Own<const Isolate> isolateParam,
1284
1287
if (logNewScript) {
1285
1288
// HACK: Log a message indicating that a new script was loaded. This is used only when the
1286
1289
// inspector is enabled. We want to do this immediately after the context is created,
1287
- // before the user gets a chance to modify the behavior of the console, which if they did,
1288
- // we'd then need to be more careful to apply time limits and such.
1290
+ // before the user gets a chance to modify the behavior of the console, which if they
1291
+ // did, we'd then need to be more careful to apply time limits and such.
1289
1292
lockedWorkerIsolate.logMessage (lock, static_cast <uint16_t >(cdp::LogType::WARNING),
1290
1293
" Script modified; context reset." );
1291
1294
}
1292
1295
1293
- // We need to register this context with the inspector, otherwise errors won't be reported. But
1294
- // we want it to be un-registered as soon as the script has been compiled, otherwise the
1295
- // inspector will end up with multiple contexts active which is very confusing for the user
1296
- // (since they'll have to select from the drop-down which context to use).
1296
+ // We need to register this context with the inspector, otherwise errors won't be
1297
+ // reported. But we want it to be un-registered as soon as the script has been
1298
+ // compiled, otherwise the inspector will end up with multiple contexts active which
1299
+ // is very confusing for the user (since they'll have to select from the drop-down
1300
+ // which context to use).
1297
1301
//
1298
1302
// (For modules, the context was already registered by `setupContext()`, above.
1299
1303
KJ_IF_SOME (i, isolate->impl ->inspector ) {
1300
- if (!source. is <ModulesSource>() ) {
1304
+ if (!modular ) {
1301
1305
i.get ()->contextCreated (
1302
1306
v8_inspector::V8ContextInfo (context, 1 , jsg::toInspectorStringView (" Compiler" )));
1303
1307
}
1304
1308
} else {
1305
1309
} // Here to squash a compiler warning
1306
1310
KJ_DEFER ({
1307
- if (!source. is <ModulesSource>() ) {
1311
+ if (!modular ) {
1308
1312
KJ_IF_SOME (i, isolate->impl ->inspector ) {
1309
1313
i.get ()->contextDestroyed (context);
1310
1314
} else {
1311
- // Else block to avoid dangling else clang warning.
1312
- }
1315
+ } // Here to squash a compiler warning
1313
1316
}
1314
1317
});
1315
1318
@@ -1320,23 +1323,26 @@ Worker::Script::Script(kj::Own<const Isolate> isolateParam,
1320
1323
try {
1321
1324
KJ_SWITCH_ONEOF (source) {
1322
1325
KJ_CASE_ONEOF (script, ScriptSource) {
1326
+ // This path is used for the older, service worker syntax workers.
1327
+
1323
1328
impl->globals =
1324
1329
script.compileGlobals (lock, isolate->getApi (), isolate->getApi ().getObserver ());
1325
1330
1326
1331
{
1327
- // It's unclear to me if CompileUnboundScript() can get trapped in any infinite loops or
1328
- // excessively-expensive computation requiring a time limit. We'll go ahead and apply a time
1329
- // limit just to be safe. Don't add it to the rollover bank, though.
1332
+ // It's unclear to me if CompileUnboundScript() can get trapped in any
1333
+ // infinite loops or excessively-expensive computation requiring a time
1334
+ // limit. We'll go ahead and apply a time limit just to be safe. Don't
1335
+ // add it to the rollover bank, though.
1330
1336
auto limitScope =
1331
1337
isolate->getLimitEnforcer ().enterStartupJs (lock, limitErrorOrTime);
1332
1338
impl->unboundScriptOrMainModule =
1333
1339
jsg::NonModuleScript::compile (lock, script.mainScript , script.mainScriptName );
1334
1340
}
1335
-
1336
- break ;
1337
1341
}
1338
1342
1339
1343
KJ_CASE_ONEOF (modulesSource, ModulesSource) {
1344
+ // This path is used for the new ESM worker syntax.
1345
+
1340
1346
this ->isPython = modulesSource.isPython ;
1341
1347
if (!isolate->getApi ().getFeatureFlags ().getNewModuleRegistry ()) {
1342
1348
kj::Own<void > limitScope;
@@ -1351,7 +1357,6 @@ Worker::Script::Script(kj::Own<const Isolate> isolateParam,
1351
1357
modulesSource.compileModules (lock, isolate->getApi ());
1352
1358
}
1353
1359
impl->unboundScriptOrMainModule = kj::Path::parse (modulesSource.mainModule );
1354
- break ;
1355
1360
}
1356
1361
}
1357
1362
@@ -1477,21 +1482,6 @@ void Worker::setupContext(
1477
1482
// =======================================================================================
1478
1483
1479
1484
namespace {
1480
-
1481
- jsg::JsObject resolveNodeInspectModule (jsg::Lock& js) {
1482
- static constexpr auto kSpecifier = " node-internal:internal_inspect" _kj;
1483
- if (FeatureFlags::get (js).getNewModuleRegistry ()) {
1484
- return KJ_ASSERT_NONNULL (
1485
- jsg::modules::ModuleRegistry::tryResolveModuleNamespace (js, kSpecifier ));
1486
- }
1487
-
1488
- // Use the original module registry implementation
1489
- auto registry = jsg::ModuleRegistry::from (js);
1490
- KJ_ASSERT (registry != nullptr );
1491
- auto inspectModule = registry->resolveInternalImport (js, kSpecifier );
1492
- return jsg::JsObject (inspectModule.getHandle (js).As <v8::Object>());
1493
- }
1494
-
1495
1485
kj::Maybe<jsg::JsObject> tryResolveMainModule (jsg::Lock& js,
1496
1486
const kj::Path& mainModule,
1497
1487
jsg::JsContext<api::ServiceWorkerGlobalScope>& jsContext,
@@ -1867,7 +1857,8 @@ void Worker::handleLog(jsg::Lock& js,
1867
1857
// not even evaluate its arguments, so `message()` will not be called at all.
1868
1858
KJ_LOG (INFO, " console.log()" , message ());
1869
1859
} else {
1870
- // Write to stdio if allowed by console mode
1860
+ // Write to stdio if allowed by console mode. This is making use of our internal
1861
+ // built-in implementation of the node:util inspect API.
1871
1862
static const ColorMode COLOR_MODE = permitsColor ();
1872
1863
#if _WIN32
1873
1864
static bool STDOUT_TTY = _isatty (_fileno (stdout));
@@ -1884,7 +1875,8 @@ void Worker::handleLog(jsg::Lock& js,
1884
1875
auto colors =
1885
1876
COLOR_MODE == ColorMode::ENABLED || (COLOR_MODE == ColorMode::ENABLED_IF_TTY && tty);
1886
1877
1887
- auto inspectModule = resolveNodeInspectModule (js);
1878
+ static constexpr auto kSpecifier = " node-internal:internal_inspect" _kj;
1879
+ auto inspectModule = KJ_ASSERT_NONNULL (js.resolveInternalModule (kSpecifier ));
1888
1880
v8::Local<v8::Value> formatLogVal = inspectModule.get (js, " formatLog" _kj);
1889
1881
KJ_ASSERT (formatLogVal->IsFunction ());
1890
1882
auto formatLog = formatLogVal.As <v8::Function>();
0 commit comments