[plt-scheme] Evaluating code in namespace of calling module
At Mon, 14 Mar 2005 09:22:49 +0100, Erich Rast wrote:
> I'd like to evaluate code by a function or macro that is defined in a
> module foo, but the code should be evaluated with respect to all of the
> bindings that are currently active in the module bar that calls the
> custom evaluation function required from foo. So far, I've tried
> passing (current-namespace) to the evaluation function and then use
> parameterize in module foo, but this doesn't seem to work.
Normally, `(current-namespace)' gives you the top-level environment,
outside of any module.
There's no simple way to get a namespace corresponding to the top-level
of the current module.
You can use `module->namespace' with a specific module name to get a
namespace for the named module. In this case, for example,
> (module bar mzscheme
> (require foo)
> (require (lib "class.ss"))
>
> (let* ((test% (class* object% ()
> (public hello-world)
> (define (hello-world)
> (display "hello world!")(newline)))))
> (object? (my-eval 'test% (current-namespace)))
> )
> )
I think that `(module->namespace 'bar)' would do what you want.
It's possible to get the name of the enclosing module by using
`syntax-source-module' on a bit of syntax from the module. You'll
likely get back a module-path index, so see `resolve-module-path-index'
from `(lib "moddep.ss" "syntax")'.
Matthew