src/squirrel_std.cpp
author truebrain
Thu, 12 Jun 2008 18:03:50 +0000
branchnoai
changeset 10938 df6235dd2b7a
parent 10649 9034b80fdbdb
permissions -rw-r--r--
(svn r13492) [NoAI] -Add: added the 'standard' functions max() and min() in global scope
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>
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
     3
#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
     4
#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
     5
#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
     6
#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
     7
#include "core/alloc_func.hpp"
10938
df6235dd2b7a (svn r13492) [NoAI] -Add: added the 'standard' functions max() and min() in global scope
truebrain
parents: 10649
diff changeset
     8
#include "core/math_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
10938
df6235dd2b7a (svn r13492) [NoAI] -Add: added the 'standard' functions max() and min() in global scope
truebrain
parents: 10649
diff changeset
    22
SQInteger SquirrelStd::min(HSQUIRRELVM vm)
df6235dd2b7a (svn r13492) [NoAI] -Add: added the 'standard' functions max() and min() in global scope
truebrain
parents: 10649
diff changeset
    23
{
df6235dd2b7a (svn r13492) [NoAI] -Add: added the 'standard' functions max() and min() in global scope
truebrain
parents: 10649
diff changeset
    24
	SQInteger tmp1, tmp2;
df6235dd2b7a (svn r13492) [NoAI] -Add: added the 'standard' functions max() and min() in global scope
truebrain
parents: 10649
diff changeset
    25
df6235dd2b7a (svn r13492) [NoAI] -Add: added the 'standard' functions max() and min() in global scope
truebrain
parents: 10649
diff changeset
    26
	sq_getinteger(vm, 2, &tmp1);
df6235dd2b7a (svn r13492) [NoAI] -Add: added the 'standard' functions max() and min() in global scope
truebrain
parents: 10649
diff changeset
    27
	sq_getinteger(vm, 3, &tmp2);
df6235dd2b7a (svn r13492) [NoAI] -Add: added the 'standard' functions max() and min() in global scope
truebrain
parents: 10649
diff changeset
    28
	sq_pushinteger(vm, ::min(tmp1, tmp2));
df6235dd2b7a (svn r13492) [NoAI] -Add: added the 'standard' functions max() and min() in global scope
truebrain
parents: 10649
diff changeset
    29
	return 1;
df6235dd2b7a (svn r13492) [NoAI] -Add: added the 'standard' functions max() and min() in global scope
truebrain
parents: 10649
diff changeset
    30
}
df6235dd2b7a (svn r13492) [NoAI] -Add: added the 'standard' functions max() and min() in global scope
truebrain
parents: 10649
diff changeset
    31
df6235dd2b7a (svn r13492) [NoAI] -Add: added the 'standard' functions max() and min() in global scope
truebrain
parents: 10649
diff changeset
    32
SQInteger SquirrelStd::max(HSQUIRRELVM vm)
df6235dd2b7a (svn r13492) [NoAI] -Add: added the 'standard' functions max() and min() in global scope
truebrain
parents: 10649
diff changeset
    33
{
df6235dd2b7a (svn r13492) [NoAI] -Add: added the 'standard' functions max() and min() in global scope
truebrain
parents: 10649
diff changeset
    34
	SQInteger tmp1, tmp2;
df6235dd2b7a (svn r13492) [NoAI] -Add: added the 'standard' functions max() and min() in global scope
truebrain
parents: 10649
diff changeset
    35
df6235dd2b7a (svn r13492) [NoAI] -Add: added the 'standard' functions max() and min() in global scope
truebrain
parents: 10649
diff changeset
    36
	sq_getinteger(vm, 2, &tmp1);
df6235dd2b7a (svn r13492) [NoAI] -Add: added the 'standard' functions max() and min() in global scope
truebrain
parents: 10649
diff changeset
    37
	sq_getinteger(vm, 3, &tmp2);
df6235dd2b7a (svn r13492) [NoAI] -Add: added the 'standard' functions max() and min() in global scope
truebrain
parents: 10649
diff changeset
    38
	sq_pushinteger(vm, ::max(tmp1, tmp2));
df6235dd2b7a (svn r13492) [NoAI] -Add: added the 'standard' functions max() and min() in global scope
truebrain
parents: 10649
diff changeset
    39
	return 1;
df6235dd2b7a (svn r13492) [NoAI] -Add: added the 'standard' functions max() and min() in global scope
truebrain
parents: 10649
diff changeset
    40
}
df6235dd2b7a (svn r13492) [NoAI] -Add: added the 'standard' functions max() and min() in global scope
truebrain
parents: 10649
diff changeset
    41
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
    42
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
    43
{
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
	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
    45
	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
    46
	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
    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
	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
    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
	/* 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
    51
	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
    52
	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
    53
	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
    54
		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
    55
		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
    56
	}
9578
2e05c1b002f0 (svn r9600) [NoAI] -Fix r9594: restore compilation with _UNICODE
glx
parents: 9577
diff changeset
    57
	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
    58
	/* Keep the dir, remove the rest */
9578
2e05c1b002f0 (svn r9600) [NoAI] -Fix r9594: restore compilation with _UNICODE
glx
parents: 9577
diff changeset
    59
	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
    60
	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
    61
		/* 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
    62
		*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
    63
		*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
    64
	}
9725
a73b33a966be (svn r12110) [NoAI] -Fix [FS#1746]: possible segmentation fault when trying to load another script file
smatz
parents: 9723
diff changeset
    65
	/* 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
    66
	 * 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
    67
	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
    68
	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
    69
10649
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 9727
diff changeset
    70
	bool ret = Squirrel::LoadScript(vm, FS2OTTD(real_filename));
9727
a5fdea13a991 (svn r12113) [NoAI] -Fix: memory leak in require()
glx
parents: 9725
diff changeset
    71
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
    72
	/* 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
    73
	sq_settop(vm, top);
9727
a5fdea13a991 (svn r12113) [NoAI] -Fix: memory leak in require()
glx
parents: 9725
diff changeset
    74
	free(real_filename);
a5fdea13a991 (svn r12113) [NoAI] -Fix: memory leak in require()
glx
parents: 9725
diff changeset
    75
10649
9034b80fdbdb (svn r13193) [NoAI] -Add: allow AIs to be packed in a .tar. one AI per tar, always in a subdir. It looks for main.nut in the first dir it finds in the archive
truebrain
parents: 9727
diff changeset
    76
	return ret ? 0: 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
    77
}
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    78
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    79
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
    80
{
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    81
	/* 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
    82
	 *  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
    83
	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
    84
}
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9487
diff changeset
    85
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
    86
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
    87
{
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
    88
	/* 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
    89
	 *  scope and not to a class. */
10938
df6235dd2b7a (svn r13492) [NoAI] -Add: added the 'standard' functions max() and min() in global scope
truebrain
parents: 10649
diff changeset
    90
	engine->AddMethod("abs", &SquirrelStd::abs, 2, "?i");
df6235dd2b7a (svn r13492) [NoAI] -Add: added the 'standard' functions max() and min() in global scope
truebrain
parents: 10649
diff changeset
    91
	engine->AddMethod("min", &SquirrelStd::min, 3, "?ii");
df6235dd2b7a (svn r13492) [NoAI] -Add: added the 'standard' functions max() and min() in global scope
truebrain
parents: 10649
diff changeset
    92
	engine->AddMethod("max", &SquirrelStd::max, 3, "?ii");
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
    93
}