src/squirrel_std.cpp
author glx
Mon, 11 Feb 2008 18:08:09 +0000
branchnoai
changeset 9727 a5fdea13a991
parent 9725 a73b33a966be
child 10649 9034b80fdbdb
permissions -rw-r--r--
(svn r12113) [NoAI] -Fix: memory leak in require()
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"
9725
a73b33a966be (svn r12110) [NoAI] -Fix [FS#1746]: possible segmentation fault when trying to load another script file
smatz
parents: 9723
diff changeset
     8
#include "core/alloc_func.hpp"
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
     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);
9722
ebf0ece7d8f6 (svn r11503) [NoAI] -Sync: with trunk r11308:11502.
rubidium
parents: 9578
diff changeset
    18
	sq_pushinteger(vm, ::abs(tmp));
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
    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
	}
9578
2e05c1b002f0 (svn r9600) [NoAI] -Fix r9594: restore compilation with _UNICODE
glx
parents: 9577
diff changeset
    37
	real_filename = scstrdup(si.source);
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
    38
	/* Keep the dir, remove the rest */
9578
2e05c1b002f0 (svn r9600) [NoAI] -Fix r9594: restore compilation with _UNICODE
glx
parents: 9577
diff changeset
    39
	SQChar *s = scstrrchr(real_filename, PATHSEPCHAR);
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
    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
	}
9725
a73b33a966be (svn r12110) [NoAI] -Fix [FS#1746]: possible segmentation fault when trying to load another script file
smatz
parents: 9723
diff changeset
    45
	/* And now we concat, so we are relative from the current script
a73b33a966be (svn r12110) [NoAI] -Fix [FS#1746]: possible segmentation fault when trying to load another script file
smatz
parents: 9723
diff changeset
    46
	 * First, we have to make sure we have enough space for the full path */
a73b33a966be (svn r12110) [NoAI] -Fix [FS#1746]: possible segmentation fault when trying to load another script file
smatz
parents: 9723
diff changeset
    47
	real_filename = ReallocT(real_filename, scstrlen(real_filename) + scstrlen(filename) + 1);
9578
2e05c1b002f0 (svn r9600) [NoAI] -Fix r9594: restore compilation with _UNICODE
glx
parents: 9577
diff changeset
    48
	scstrcat(real_filename, filename);
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
    49
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
	/* 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
    51
	sq_pushroottable(vm);
9727
a5fdea13a991 (svn r12113) [NoAI] -Fix: memory leak in require()
glx
parents: 9725
diff changeset
    52
a5fdea13a991 (svn r12113) [NoAI] -Fix: memory leak in require()
glx
parents: 9725
diff changeset
    53
	SQInteger ret = 0;
a5fdea13a991 (svn r12113) [NoAI] -Fix: memory leak in require()
glx
parents: 9725
diff changeset
    54
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
    55
	/* 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
    56
	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
    57
		DEBUG(misc, 0, "[squirrel] Failed to compile '%s'", filename);
9727
a5fdea13a991 (svn r12113) [NoAI] -Fix: memory leak in require()
glx
parents: 9725
diff changeset
    58
		ret = SQ_ERROR;
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
    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
	/* 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
    61
	sq_settop(vm, top);
9727
a5fdea13a991 (svn r12113) [NoAI] -Fix: memory leak in require()
glx
parents: 9725
diff changeset
    62
	free(real_filename);
a5fdea13a991 (svn r12113) [NoAI] -Fix: memory leak in require()
glx
parents: 9725
diff changeset
    63
a5fdea13a991 (svn r12113) [NoAI] -Fix: memory leak in require()
glx
parents: 9725
diff changeset
    64
	return ret;
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
    65
}
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
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
    68
{
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    69
	/* 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
    70
	 *  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
    71
	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
    72
}
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    73
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
    74
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
    75
{
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
    76
	/* 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
    77
	 *  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
    78
	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
    79
}