diff options
author | Aaron Marks <nymacro@gmail.com> | 2005-05-10 11:57:53 +0000 |
---|---|---|
committer | Aaron Marks <nymacro@gmail.com> | 2005-05-10 11:57:53 +0000 |
commit | 846784f652e9b30b67c04bfa800be1448c8a15e7 (patch) | |
tree | 1d03f92e34a5fb179bb93471b55d5b49f141484b /src/script-squirrel.cpp | |
parent | ef30b72e8ad5379a7c7ee49b0ff680bde15daa66 (diff) | |
download | manaserv-846784f652e9b30b67c04bfa800be1448c8a15e7.tar.gz manaserv-846784f652e9b30b67c04bfa800be1448c8a15e7.tar.bz2 manaserv-846784f652e9b30b67c04bfa800be1448c8a15e7.tar.xz manaserv-846784f652e9b30b67c04bfa800be1448c8a15e7.zip |
Updated scripting interface & added some class documentation.
- Updated Squirrel scripting interface.
- Added registerFunction which will register a function for the specified
scripting VM.
Diffstat (limited to 'src/script-squirrel.cpp')
-rw-r--r-- | src/script-squirrel.cpp | 109 |
1 files changed, 97 insertions, 12 deletions
diff --git a/src/script-squirrel.cpp b/src/script-squirrel.cpp index d8001de4..cb3d7601 100644 --- a/src/script-squirrel.cpp +++ b/src/script-squirrel.cpp @@ -20,7 +20,7 @@ void printfunc(HSQUIRRELVM v, const SQChar *s, ...) * 'i' = Integer * 'f' = Float */ -void functionCall(HSQUIRRELVM v, char *fn, char *args, ...) +bool functionCall(HSQUIRRELVM v, const char *fn, const char *args, ...) { int argCount = 0; va_list arglist; @@ -62,38 +62,118 @@ void functionCall(HSQUIRRELVM v, char *fn, char *args, ...) } sq_call(v, argCount + 1, 0); - } + } else + return false; sq_settop(v, top); va_end(arglist); + return true; } -ScriptSquirrel::ScriptSquirrel(const std::string &file) : - Script(file) +/* + * functionRegister + * Registers a function in Squirrel VM + */ +void functionRegister(HSQUIRRELVM v, SQFUNCTION f, const char *name) { + sq_pushroottable(v); + sq_pushstring(v, name, -1); + sq_newclosure(v, f, 0); + sq_createslot(v, -3); + sq_pop(v, 1); } -ScriptSquirrel::~ScriptSquirrel() +/* + * Test function called from Squirrel (modified form Squirrel docs) + * Prints the type of all arguments. + */ +int testFunc(HSQUIRRELVM v) { - sq_pop(vm, 1); - sq_close(vm); + int nargs = sq_gettop(v); + + for (int n = 1; n <= nargs; n++) { + printf("arg: %d is ", n); + switch (sq_gettype(v, n)) + { + case OT_NULL: + printf("null"); + break; + case OT_INTEGER: + printf("integer"); + break; + case OT_FLOAT: + printf("float"); + break; + case OT_STRING: + printf("string"); + break; + case OT_TABLE: + printf("table"); + break; + case OT_ARRAY: + printf("array"); + break; + case OT_USERDATA: + printf("userdata"); + break; + case OT_CLOSURE: + printf("closure"); + break; + case OT_NATIVECLOSURE: + printf("nativeclosure"); + break; + case OT_GENERATOR: + printf("generator"); + break; + case OT_USERPOINTER: + printf("userpointer"); + break; + default: + printf("unknown"); + } + } + printf("\n"); + sq_pushinteger(v, nargs); + return 1; } -void ScriptSquirrel::init() +/* + * Another test function + */ +int worldTime(HSQUIRRELVM v) { - vm = sq_open(2048); + sq_pushinteger(v, 10); + return 1; +} + +/******/ + +ScriptSquirrel::ScriptSquirrel(const std::string &file) : + Script(file) +{ + vm = sq_open(1024); //1024 byte stack sqstd_seterrorhandlers(vm); sq_setprintfunc(vm, printfunc); sq_pushroottable(vm); - sqstd_dofile(vm, _SC(scriptName.c_str()), 0, 1); - if (SQ_SUCCEEDED(sqstd_dofile(vm, _SC("test.nut"), 0, 1))) + if (!SQ_SUCCEEDED(sqstd_dofile(vm, _SC(scriptName.c_str()), 0, 1))) { + std::cerr << "Error: ScriptSquirrel: could not execute " << + scriptName << std::endl; + } else { + functionRegister(vm, testFunc, "printargs"); + functionRegister(vm, worldTime, "worldtime"); + + functionCall(vm, "", NULL); functionCall(vm, "init", NULL); + } } -void ScriptSquirrel::destroy() +ScriptSquirrel::~ScriptSquirrel() { functionCall(vm, "destroy", NULL); + + sq_pop(vm, 1); + sq_close(vm); } void ScriptSquirrel::update() @@ -101,6 +181,11 @@ void ScriptSquirrel::update() functionCall(vm, "update", NULL); } +bool ScriptSquirrel::execute(const std::string &functionName) +{ + return functionCall(vm, functionName.c_str(), NULL); +} + void ScriptSquirrel::message(char *msg) { functionCall(vm, "message", "s", msg); |