src/squirrel_std.cpp
author truelight
Wed, 11 Apr 2007 19:53:33 +0000
branchnoai
changeset 9577 8e93be390b45
parent 9487 0575126e0267
child 9578 2e05c1b002f0
permissions -rw-r--r--
(svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
-Note: it always includes the file on the ROOT-level, no matter where you include it
-Note: the filename is relative from the script you call it from (absolute filenames simply aren't allowed / possible)
-Note: avoid including too many files on 'root-level', rather do it in Start() or where you need it.
9487
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
     1
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
     2
#include <squirrel.h>
9577
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
     3
#include <sqstdio.h>
9487
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
     4
#include "stdafx.h"
9577
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
     5
#include "debug.h"
9487
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
     6
#include "squirrel.hpp"
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
     7
#include "squirrel_std.hpp"
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
     8
#include "helpers.hpp"
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
     9
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    10
/* abs() is normally defined to myabs(), which we don't want in this file */
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    11
#undef abs
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    12
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    13
SQInteger SquirrelStd::abs(HSQUIRRELVM vm)
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    14
{
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    15
	SQInteger tmp;
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    16
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    17
	sq_getinteger(vm, 2, &tmp);
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    18
	sq_pushinteger(vm, myabs(tmp));
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    19
	return 1;
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    20
}
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    21
9577
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    22
SQInteger SquirrelStd::require(HSQUIRRELVM vm)
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    23
{
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    24
	SQInteger top = sq_gettop(vm);
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    25
	const SQChar *filename;
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    26
	SQChar *real_filename;
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    27
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    28
	sq_getstring(vm, 2, &filename);
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    29
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    30
	/* Get the script-name of the current file, so we can work relative from it */
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    31
	SQStackInfos si;
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    32
	sq_stackinfos(vm, 1, &si);
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    33
	if (si.source == NULL) {
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    34
		DEBUG(misc, 0, "[squirrel] Couldn't detect the script-name of the 'require'-caller; this should never happen!");
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    35
		return SQ_ERROR;
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    36
	}
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    37
	real_filename = strdup(si.source);
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    38
	/* Keep the dir, remove the rest */
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    39
	SQChar *s = strrchr(real_filename, PATHSEPCHAR);
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    40
	if (s != NULL) {
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    41
		/* Keep the PATHSEPCHAR there, remove the rest */
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    42
		*s++;
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    43
		*s = '\0';
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    44
	}
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    45
	/* And now we concat, so we are relative from the current script */
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    46
	strcat(real_filename, filename);
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    47
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    48
	/* Make sure we are in the root-table, so everything is included in the root-level */
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    49
	sq_pushroottable(vm);
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    50
	/* Compile and load the file */
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    51
	if (!SQ_SUCCEEDED(sqstd_dofile(vm, real_filename, SQFalse, SQTrue))) {
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    52
		DEBUG(misc, 0, "[squirrel] Failed to compile '%s'", filename);
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    53
		sq_settop(vm, top);
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    54
		return SQ_ERROR;
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    55
	}
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    56
	/* Reset the top, so the stack stays correct */
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    57
	sq_settop(vm, top);
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    58
	return 0;
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    59
}
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    60
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    61
void squirrel_register_global_std(Squirrel *engine)
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    62
{
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    63
	/* We don't use squirrel_helper here, as we want to register to the global
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    64
	 *  scope and not to a class. */
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    65
	engine->AddMethod("require", &SquirrelStd::require, 2, "?s");
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    66
}
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    67
9487
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    68
void squirrel_register_std(Squirrel *engine)
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    69
{
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    70
	/* We don't use squirrel_helper here, as we want to register to the global
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    71
	 *  scope and not to a class. */
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    72
	engine->AddMethod("abs", &SquirrelStd::abs, 2, "xi");
0575126e0267 (svn r9356) [NoAI] -Add: add abs() function to global scope in SQ. This means we now have an own squirrel_std class which registers such functions. (on request by Zuu)
truelight
parents:
diff changeset
    73
}