Skip to content

Commit ff9fc54

Browse files
Add Builtins to the preinit hook (#319)
Co-authored-by: jayaprabhakar <jayaprabhakar@gmail.com>
1 parent 646c8b2 commit ff9fc54

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

modelchecker/processor.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,11 +1472,24 @@ func processPreInit(init *Node, stmts []*ast.Statement, preinitHookContent strin
14721472
},
14731473
}
14741474

1475-
// Execute the hook with the current globals
1476-
_, err := init.Process.Evaluator.ExecPyStmt("<preinit-hook>", pyStmt, globals)
1475+
// Build a temporary dict with builtins and modules so the hook
1476+
// can use record(), enum(), symmetry.nominal(), etc.
1477+
hookGlobals := starlark.StringDict{}
1478+
maps.Copy(hookGlobals, globals)
1479+
maps.Copy(hookGlobals, lib.Builtins)
1480+
maps.Copy(hookGlobals, init.Process.Modules)
1481+
1482+
_, err := init.Process.Evaluator.ExecPyStmt("<preinit-hook>", pyStmt, hookGlobals)
14771483
if err != nil {
14781484
panic(fmt.Sprintf("Error executing preinit hook: %v", err))
14791485
}
1486+
1487+
// Copy back only top-level vars (may have been overridden by the hook)
1488+
for _, name := range init.Process.topLevelVars {
1489+
if v, ok := hookGlobals[name]; ok {
1490+
globals[name] = v
1491+
}
1492+
}
14801493
}
14811494

14821495
globals.Freeze()

0 commit comments

Comments
 (0)