Add methods to get arguments passed to function from Nasal

This commit is contained in:
Thomas Geymayer 2012-11-29 01:23:15 +01:00
parent 3391c44107
commit 2f0dfc4d74
2 changed files with 28 additions and 2 deletions

View File

@ -223,14 +223,32 @@ namespace nasal
*/
struct CallContext
{
CallContext(naContext c, int argc, naRef* args):
CallContext(naContext c, size_t argc, naRef* args):
c(c),
argc(argc),
args(args)
{}
template<class T>
T get(size_t index, const T& def = T()) const
{
if( index >= argc )
return def;
return from_nasal<T>(c, args[index]);
}
template<class T>
T require(size_t index) const
{
if( index >= argc )
naRuntimeError(c, "Missing required arg #%d", index);
return from_nasal<T>(c, args[index]);
}
naContext c;
int argc;
size_t argc;
naRef *args;
};

View File

@ -153,6 +153,14 @@ int main(int argc, char* argv[])
obj.set("parents", parents);
VERIFY( Ghost<BasePtr>::fromNasal(c, obj.get_naRef()) == d3 );
naRef args[] = {
to_nasal(c, std::string("test-arg"))
};
CallContext cc(c, sizeof(args)/sizeof(args[0]), args);
VERIFY( cc.require<std::string>(0) == "test-arg" );
VERIFY( cc.get<std::string>(0) == "test-arg" );
VERIFY( cc.get<std::string>(1) == "" );
// TODO actually do something with the ghosts...
naFreeContext(c);