Fix scoping logic to respect the caller's package.
This change makes the ModuleContext and SingletonContext methods respect the
caller's Go package when performing name lookups. It does this by re-parenting
the module or singleton's scope to the scope of the caller's package, thus
allowing a Module or Singleton to pass its context to a function defined in
another Go package and have the called function access variables and rules
defined in its own Go package.
Change-Id: Ifdec87ba3095a453b36fb00e38c0bb3a928a2b9b
diff --git a/blueprint/module_ctx.go b/blueprint/module_ctx.go
index 0029d11..0de8c4e 100644
--- a/blueprint/module_ctx.go
+++ b/blueprint/module_ctx.go
@@ -93,6 +93,9 @@
}
func (m *moduleContext) Variable(name, value string) {
+ const skip = 2
+ m.scope.ReparentToCallerPackage(skip)
+
v, err := m.scope.AddLocalVariable(name, value)
if err != nil {
panic(err)
@@ -104,6 +107,9 @@
func (m *moduleContext) Rule(name string, params RuleParams,
argNames ...string) Rule {
+ const skip = 2
+ m.scope.ReparentToCallerPackage(skip)
+
r, err := m.scope.AddLocalRule(name, ¶ms, argNames...)
if err != nil {
panic(err)
@@ -115,6 +121,9 @@
}
func (m *moduleContext) Build(params BuildParams) {
+ const skip = 2
+ m.scope.ReparentToCallerPackage(skip)
+
def, err := parseBuildParams(m.scope, ¶ms)
if err != nil {
panic(err)