so schrieb:
I guess something like that is possible.
I'd prefer something like
s.call("main", i);
and also
s.call("foo", 42, x);
though - so you can define multiple functions in a script and call them.
Or maybe, with more magic and support within the D compiler
s.main(i);
s.foo(42,x);
But that means that the compiler needs to know the Script type, create
a class for each script that contains the functions called on the
script ("main", "foo"), create stubs for them that either do something
like call("foo",i) or, if no function foo with an appropriate
argument is found in the script, throws an exception..
Probably possible, but certainly not easy.
The first suggestion however (s.call("foo", i)) should be possible
without support in the D compiler, all the work would be done in the
Script class. Still not trivial, of course, especially with arguments
for called functions and their type etc.
The actual use case in my scenario is manipulating your program. not
necessarily the other way around.
class ScriptBinary {
void run() {..}
}
// [modules] is the list of modules you give access to script
// if [modules] contains "std", script can access "std" and it is child
modules.
ScriptBinary compile(string filename, string[] modules, ...) {
...
}
I think for a proper general purpose scripting solution (like one would expect
in std.script) both ways are needed.
But you're right, of course you need a way to tell the script what modules (and
maybe also functions/delegates so it can call local functions in your code,
but not all of them) it may access and how.