truelight@9487: truelight@9487: #include truelight@9577: #include truelight@9487: #include "stdafx.h" truelight@9577: #include "debug.h" truelight@9487: #include "squirrel.hpp" truelight@9487: #include "squirrel_std.hpp" smatz@9725: #include "core/alloc_func.hpp" truelight@9487: truelight@9487: /* abs() is normally defined to myabs(), which we don't want in this file */ truelight@9487: #undef abs truelight@9487: truelight@9487: SQInteger SquirrelStd::abs(HSQUIRRELVM vm) truelight@9487: { truelight@9487: SQInteger tmp; truelight@9487: truelight@9487: sq_getinteger(vm, 2, &tmp); rubidium@9722: sq_pushinteger(vm, ::abs(tmp)); truelight@9487: return 1; truelight@9487: } truelight@9487: truelight@9577: SQInteger SquirrelStd::require(HSQUIRRELVM vm) truelight@9577: { truelight@9577: SQInteger top = sq_gettop(vm); truelight@9577: const SQChar *filename; truelight@9577: SQChar *real_filename; truelight@9577: truelight@9577: sq_getstring(vm, 2, &filename); truelight@9577: truelight@9577: /* Get the script-name of the current file, so we can work relative from it */ truelight@9577: SQStackInfos si; truelight@9577: sq_stackinfos(vm, 1, &si); truelight@9577: if (si.source == NULL) { truelight@9577: DEBUG(misc, 0, "[squirrel] Couldn't detect the script-name of the 'require'-caller; this should never happen!"); truelight@9577: return SQ_ERROR; truelight@9577: } glx@9578: real_filename = scstrdup(si.source); truelight@9577: /* Keep the dir, remove the rest */ glx@9578: SQChar *s = scstrrchr(real_filename, PATHSEPCHAR); truelight@9577: if (s != NULL) { truelight@9577: /* Keep the PATHSEPCHAR there, remove the rest */ truelight@9577: *s++; truelight@9577: *s = '\0'; truelight@9577: } smatz@9725: /* And now we concat, so we are relative from the current script smatz@9725: * First, we have to make sure we have enough space for the full path */ smatz@9725: real_filename = ReallocT(real_filename, scstrlen(real_filename) + scstrlen(filename) + 1); glx@9578: scstrcat(real_filename, filename); truelight@9577: truelight@9577: /* Make sure we are in the root-table, so everything is included in the root-level */ truelight@9577: sq_pushroottable(vm); glx@9727: glx@9727: SQInteger ret = 0; glx@9727: truelight@9577: /* Compile and load the file */ truelight@9577: if (!SQ_SUCCEEDED(sqstd_dofile(vm, real_filename, SQFalse, SQTrue))) { truelight@9577: DEBUG(misc, 0, "[squirrel] Failed to compile '%s'", filename); glx@9727: ret = SQ_ERROR; truelight@9577: } truelight@9577: /* Reset the top, so the stack stays correct */ truelight@9577: sq_settop(vm, top); glx@9727: free(real_filename); glx@9727: glx@9727: return ret; truelight@9577: } truelight@9577: truelight@9577: void squirrel_register_global_std(Squirrel *engine) truelight@9577: { truelight@9577: /* We don't use squirrel_helper here, as we want to register to the global truelight@9577: * scope and not to a class. */ truelight@9577: engine->AddMethod("require", &SquirrelStd::require, 2, "?s"); truelight@9577: } truelight@9577: truelight@9487: void squirrel_register_std(Squirrel *engine) truelight@9487: { truelight@9487: /* We don't use squirrel_helper here, as we want to register to the global truelight@9487: * scope and not to a class. */ truelight@9487: engine->AddMethod("abs", &SquirrelStd::abs, 2, "xi"); truelight@9487: }