@@ -409,6 +409,7 @@ async function getNetworkPeerId(rtHandlers) {
409409
410410
411411export async function start ( f ) {
412+ // Set up p2p network
412413 await initTrustMap ( ) ;
413414
414415 let peerid = await getNetworkPeerId ( {
@@ -427,18 +428,25 @@ export async function start(f) {
427428
428429 __nodeManager . setLocalPeerId ( peerid ) ;
429430
430- let stopWhenAllThreadsAreDone = ! __p2pRunning
431- __sched . initScheduler ( __nodeManager . getLocalNode ( )
432- , stopWhenAllThreadsAreDone
433- , cleanupAsync ) ;
431+ // ---------------------------------------------------------------------------
432+ // Initialise 'scheduler' for Troupe code execution
433+ __sched . initScheduler ( __nodeManager . getLocalNode ( ) , ! __p2pRunning , cleanupAsync ) ;
434434
435- await loadServiceCode ( )
436- await __userRuntime . linkLibs ( f ) ;
437- let mainAuthority = new LVal ( new Authority ( levels . ROOT ) , levels . BOT ) ;
435+ // ---------------------------------------------------------------------------
436+ // Set up 'service' thread
437+
438+ // HACK: Despite the fact that service code is only spawned, if `__p2pRunning`,
439+ // we need to populate the runtime.$service object.
440+ //
441+ // TODO: Instead, treat these fields as nullable in `builtins/receive.mts` and
442+ // elsewhere. Best is to also put this into the typesystem.
443+ await loadServiceCode ( ) ;
438444
439445 if ( __p2pRunning ) {
446+ const serviceAuthority = new LVal ( new Authority ( levels . ROOT ) , levels . BOT ) ;
447+
440448 let service_arg =
441- new LVal ( new Record ( [ [ "authority" , mainAuthority ] ,
449+ new LVal ( new Record ( [ [ "authority" , serviceAuthority ] ,
442450 [ "options" , __unit ] ] ) ,
443451 levels . BOT ) ;
444452 __sched . scheduleNewThread ( __service [ 'service' ]
@@ -448,12 +456,20 @@ export async function start(f) {
448456 , ThreadType . System ) ;
449457 }
450458
459+ // Set up 'main' thread
460+ const mainAuthority = new LVal ( new Authority ( levels . ROOT ) , levels . BOT ) ;
461+
462+ await __userRuntime . linkLibs ( f ) ;
463+
451464 __sched . scheduleNewThread (
452465 ( ) => f . main ( { __dataLevel :levels . BOT } )
453466 , mainAuthority
454467 , levels . BOT
455468 , levels . BOT
456469 , ThreadType . Main
457470 ) ;
471+
472+ // ---------------------------------------------------------------------------
473+ // Start code execution
458474 __sched . loop ( ) ;
459475}
0 commit comments