src/squirrel.cpp
author glx
Sun, 18 May 2008 16:50:49 +0000
branchnoai
changeset 10620 3dfc77d25d66
parent 10171 d4397d599d78
child 10649 9034b80fdbdb
permissions -rw-r--r--
(svn r13164) [NoAI] -Fix: compilation was not possible with MSVC90 (missing include dir)
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
     1
/* $Id$ */
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
     2
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
     3
/** @file squirrel.cpp the implementation of the Squirrel class. It handles all Squirrel-stuff and gives a nice API back to work with. */
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
     4
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
     5
#include <squirrel.h>
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
     6
#include <sqstdio.h>
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
     7
#include <stdarg.h>
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
     8
#include "stdafx.h"
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
     9
#include "debug.h"
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
    10
#include "squirrel.hpp"
9577
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9576
diff changeset
    11
#include "squirrel_std.hpp"
9576
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    12
#include <sqstdaux.h>
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    13
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
    14
void Squirrel::CompileError(HSQUIRRELVM vm, const SQChar *desc, const SQChar *source, SQInteger line, SQInteger column)
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    15
{
9549
511ad84a22d1 (svn r9481) [NoAI] -Fix: FS2OTTD returns the adress of a static buffer, so strdup the strings to prevent 'overwriting'
glx
parents: 9539
diff changeset
    16
	char *src = strdup(FS2OTTD(source));
511ad84a22d1 (svn r9481) [NoAI] -Fix: FS2OTTD returns the adress of a static buffer, so strdup the strings to prevent 'overwriting'
glx
parents: 9539
diff changeset
    17
	char *dsc = strdup(FS2OTTD(desc));
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    18
#ifdef _SQ64
9549
511ad84a22d1 (svn r9481) [NoAI] -Fix: FS2OTTD returns the adress of a static buffer, so strdup the strings to prevent 'overwriting'
glx
parents: 9539
diff changeset
    19
	DEBUG(misc, 0, "Error %s:%ld/%ld: %s", src, line, column, dsc);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    20
#else
9549
511ad84a22d1 (svn r9481) [NoAI] -Fix: FS2OTTD returns the adress of a static buffer, so strdup the strings to prevent 'overwriting'
glx
parents: 9539
diff changeset
    21
	DEBUG(misc, 0, "Error %s:%d/%d: %s", src, line, column, dsc);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    22
#endif
9549
511ad84a22d1 (svn r9481) [NoAI] -Fix: FS2OTTD returns the adress of a static buffer, so strdup the strings to prevent 'overwriting'
glx
parents: 9539
diff changeset
    23
	free(src);
511ad84a22d1 (svn r9481) [NoAI] -Fix: FS2OTTD returns the adress of a static buffer, so strdup the strings to prevent 'overwriting'
glx
parents: 9539
diff changeset
    24
	free(dsc);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    25
}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    26
9576
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    27
void Squirrel::ErrorPrintFunc(HSQUIRRELVM vm, const SQChar *s, ...)
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    28
{
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    29
	va_list arglist;
9789
33d3214a2fce (svn r12286) [NoAI] -Fix (r12277): restore compilation with _UNICODE
glx
parents: 9782
diff changeset
    30
	SQChar buf[1024];
9782
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
    31
9576
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    32
	va_start(arglist, s);
9789
33d3214a2fce (svn r12286) [NoAI] -Fix (r12277): restore compilation with _UNICODE
glx
parents: 9782
diff changeset
    33
	scvsnprintf(buf, lengthof(buf), s, arglist);
9576
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    34
	va_end(arglist);
9782
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
    35
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
    36
	/* Check if we have a custom print function */
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
    37
	SQPrintFunc *func = ((Squirrel *)sq_getforeignptr(vm))->print_func;
10171
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
    38
	if (func == NULL) {
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
    39
		scfprintf(stderr, _SC("%s"), buf);
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
    40
	} else {
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
    41
		(*func)(true, buf);
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
    42
	}
9576
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    43
}
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    44
9789
33d3214a2fce (svn r12286) [NoAI] -Fix (r12277): restore compilation with _UNICODE
glx
parents: 9782
diff changeset
    45
void Squirrel::RunError(HSQUIRRELVM vm, const SQChar *error)
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    46
{
9576
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    47
	/* Set the print function to something that prints to stderr */
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    48
	SQPRINTFUNCTION pf = sq_getprintfunc(vm);
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    49
	sq_setprintfunc(vm, &Squirrel::ErrorPrintFunc);
9782
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
    50
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
    51
	/* Check if we have a custom print function */
9789
33d3214a2fce (svn r12286) [NoAI] -Fix (r12277): restore compilation with _UNICODE
glx
parents: 9782
diff changeset
    52
	SQChar buf[1024];
33d3214a2fce (svn r12286) [NoAI] -Fix (r12277): restore compilation with _UNICODE
glx
parents: 9782
diff changeset
    53
	scsnprintf(buf, lengthof(buf), _SC("Your script made an error: %s\n"), error);
9782
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
    54
	SQPrintFunc *func = ((Squirrel *)sq_getforeignptr(vm))->print_func;
10171
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
    55
	if (func == NULL) {
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
    56
		scfprintf(stderr, _SC("%s"), buf);
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
    57
	} else {
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
    58
		(*func)(true, buf);
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
    59
	}
9782
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
    60
9576
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    61
	/* Print below the error the stack, so the users knows what is happening */
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    62
	sqstd_printcallstack(vm);
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    63
	/* Reset the old print function */
d650eb70a8d0 (svn r9593) [NoAI] -Add: on runtime-error, print the complete stack, so the user can see where it went wrong
truelight
parents: 9549
diff changeset
    64
	sq_setprintfunc(vm, pf);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    65
}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    66
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
    67
SQInteger Squirrel::_RunError(HSQUIRRELVM vm)
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    68
{
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    69
	const SQChar *sErr = 0;
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    70
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    71
	if (sq_gettop(vm) >= 1) {
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    72
		if (SQ_SUCCEEDED(sq_getstring(vm, -1, &sErr))) {
9789
33d3214a2fce (svn r12286) [NoAI] -Fix (r12277): restore compilation with _UNICODE
glx
parents: 9782
diff changeset
    73
			Squirrel::RunError(vm, sErr);
9535
522eb10df893 (svn r9457) [NoAI] -Fix: the errors reported by SQ were a) not via DEBUG, b) always ending with: "Unknown Error"
truelight
parents: 9525
diff changeset
    74
			return 0;
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    75
		}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    76
	}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    77
9789
33d3214a2fce (svn r12286) [NoAI] -Fix (r12277): restore compilation with _UNICODE
glx
parents: 9782
diff changeset
    78
	Squirrel::RunError(vm, _SC("unknown error"));
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    79
	return 0;
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    80
}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    81
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
    82
void Squirrel::PrintFunc(HSQUIRRELVM vm, const SQChar *s, ...)
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    83
{
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    84
	va_list arglist;
9789
33d3214a2fce (svn r12286) [NoAI] -Fix (r12277): restore compilation with _UNICODE
glx
parents: 9782
diff changeset
    85
	SQChar buf[1024];
9782
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
    86
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    87
	va_start(arglist, s);
9789
33d3214a2fce (svn r12286) [NoAI] -Fix (r12277): restore compilation with _UNICODE
glx
parents: 9782
diff changeset
    88
	scvsnprintf(buf, lengthof(buf) - 2, s, arglist);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    89
	va_end(arglist);
9789
33d3214a2fce (svn r12286) [NoAI] -Fix (r12277): restore compilation with _UNICODE
glx
parents: 9782
diff changeset
    90
	scstrcat(buf, _SC("\n"));
9782
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
    91
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
    92
	/* Check if we have a custom print function */
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
    93
	SQPrintFunc *func = ((Squirrel *)sq_getforeignptr(vm))->print_func;
10171
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
    94
	if (func == NULL) {
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
    95
		scprintf(_SC("%s"), buf);
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
    96
	} else {
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
    97
		(*func)(false, buf);
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
    98
	}
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
    99
}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   100
9539
7416d0694d3a (svn r9461) [NoAI] -Add: allow to set the amount of params that SQ should expect in the SQ define lines. Script to automated this are coming up next.
truelight
parents: 9538
diff changeset
   101
void Squirrel::AddMethod(const char *method_name, SQFUNCTION proc, uint nparam, const char *params, void *userdata, int size)
9396
a05857491d2d (svn r9188) [NoAI] -Cleanup: cleaned up DefSQClass a bit by using the SquirrelEngine
truelight
parents: 9395
diff changeset
   102
{
9435
9dad22394553 (svn r9245) [NoAI] -Fix: allow compilation with _UNICODE
glx
parents: 9434
diff changeset
   103
	sq_pushstring(this->vm, OTTD2FS(method_name), -1);
9396
a05857491d2d (svn r9188) [NoAI] -Cleanup: cleaned up DefSQClass a bit by using the SquirrelEngine
truelight
parents: 9395
diff changeset
   104
9539
7416d0694d3a (svn r9461) [NoAI] -Add: allow to set the amount of params that SQ should expect in the SQ define lines. Script to automated this are coming up next.
truelight
parents: 9538
diff changeset
   105
	if (size != 0) {
10171
d4397d599d78 (svn r12702) [NoAI] -Codechange: some coding style cleanups.
rubidium
parents: 9789
diff changeset
   106
		void *ptr = sq_newuserdata(vm, size);
9539
7416d0694d3a (svn r9461) [NoAI] -Add: allow to set the amount of params that SQ should expect in the SQ define lines. Script to automated this are coming up next.
truelight
parents: 9538
diff changeset
   107
		memcpy(ptr, userdata, size);
7416d0694d3a (svn r9461) [NoAI] -Add: allow to set the amount of params that SQ should expect in the SQ define lines. Script to automated this are coming up next.
truelight
parents: 9538
diff changeset
   108
	}
9396
a05857491d2d (svn r9188) [NoAI] -Cleanup: cleaned up DefSQClass a bit by using the SquirrelEngine
truelight
parents: 9395
diff changeset
   109
9539
7416d0694d3a (svn r9461) [NoAI] -Add: allow to set the amount of params that SQ should expect in the SQ define lines. Script to automated this are coming up next.
truelight
parents: 9538
diff changeset
   110
	sq_newclosure(this->vm, proc, size != 0 ? 1 : 0);
7416d0694d3a (svn r9461) [NoAI] -Add: allow to set the amount of params that SQ should expect in the SQ define lines. Script to automated this are coming up next.
truelight
parents: 9538
diff changeset
   111
	if (nparam != 0) sq_setparamscheck(this->vm, nparam, OTTD2FS(params));
9435
9dad22394553 (svn r9245) [NoAI] -Fix: allow compilation with _UNICODE
glx
parents: 9434
diff changeset
   112
	sq_setnativeclosurename(this->vm, -1, OTTD2FS(method_name));
9525
1d6c509b56ee (svn r9445) [NoAI] -Change: changed from sq_createslot to sq_newslot (first is deprecated)
truelight
parents: 9435
diff changeset
   113
	sq_newslot(this->vm, -3, SQFalse);
1d6c509b56ee (svn r9445) [NoAI] -Change: changed from sq_createslot to sq_newslot (first is deprecated)
truelight
parents: 9435
diff changeset
   114
}
1d6c509b56ee (svn r9445) [NoAI] -Change: changed from sq_createslot to sq_newslot (first is deprecated)
truelight
parents: 9435
diff changeset
   115
1d6c509b56ee (svn r9445) [NoAI] -Change: changed from sq_createslot to sq_newslot (first is deprecated)
truelight
parents: 9435
diff changeset
   116
void Squirrel::AddConst(const char *var_name, int value)
1d6c509b56ee (svn r9445) [NoAI] -Change: changed from sq_createslot to sq_newslot (first is deprecated)
truelight
parents: 9435
diff changeset
   117
{
1d6c509b56ee (svn r9445) [NoAI] -Change: changed from sq_createslot to sq_newslot (first is deprecated)
truelight
parents: 9435
diff changeset
   118
	sq_pushstring(this->vm, OTTD2FS(var_name), -1);
1d6c509b56ee (svn r9445) [NoAI] -Change: changed from sq_createslot to sq_newslot (first is deprecated)
truelight
parents: 9435
diff changeset
   119
	sq_pushinteger(this->vm, value);
1d6c509b56ee (svn r9445) [NoAI] -Change: changed from sq_createslot to sq_newslot (first is deprecated)
truelight
parents: 9435
diff changeset
   120
	sq_newslot(this->vm, -3, SQTrue);
9396
a05857491d2d (svn r9188) [NoAI] -Cleanup: cleaned up DefSQClass a bit by using the SquirrelEngine
truelight
parents: 9395
diff changeset
   121
}
a05857491d2d (svn r9188) [NoAI] -Cleanup: cleaned up DefSQClass a bit by using the SquirrelEngine
truelight
parents: 9395
diff changeset
   122
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
   123
void Squirrel::AddClassBegin(const char *class_name)
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   124
{
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   125
	sq_pushroottable(this->vm);
9435
9dad22394553 (svn r9245) [NoAI] -Fix: allow compilation with _UNICODE
glx
parents: 9434
diff changeset
   126
	sq_pushstring(this->vm, OTTD2FS(class_name), -1);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   127
	sq_newclass(this->vm, SQFalse);
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   128
}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   129
9588
01b2435c977b (svn r9618) [NoAI] -Add: allow an extra param for PreRegister to register classes as extends of others
truelight
parents: 9577
diff changeset
   130
void Squirrel::AddClassBegin(const char *class_name, const char *parent_class)
01b2435c977b (svn r9618) [NoAI] -Add: allow an extra param for PreRegister to register classes as extends of others
truelight
parents: 9577
diff changeset
   131
{
01b2435c977b (svn r9618) [NoAI] -Add: allow an extra param for PreRegister to register classes as extends of others
truelight
parents: 9577
diff changeset
   132
	sq_pushroottable(this->vm);
01b2435c977b (svn r9618) [NoAI] -Add: allow an extra param for PreRegister to register classes as extends of others
truelight
parents: 9577
diff changeset
   133
	sq_pushstring(this->vm, OTTD2FS(class_name), -1);
01b2435c977b (svn r9618) [NoAI] -Add: allow an extra param for PreRegister to register classes as extends of others
truelight
parents: 9577
diff changeset
   134
	sq_pushstring(this->vm, OTTD2FS(parent_class), -1);
01b2435c977b (svn r9618) [NoAI] -Add: allow an extra param for PreRegister to register classes as extends of others
truelight
parents: 9577
diff changeset
   135
	if (SQ_FAILED(sq_get(this->vm, -3))) {
01b2435c977b (svn r9618) [NoAI] -Add: allow an extra param for PreRegister to register classes as extends of others
truelight
parents: 9577
diff changeset
   136
		DEBUG(misc, 0, "[squirrel] Failed to initialize class '%s' based on parent class '%s'", class_name, parent_class);
01b2435c977b (svn r9618) [NoAI] -Add: allow an extra param for PreRegister to register classes as extends of others
truelight
parents: 9577
diff changeset
   137
		DEBUG(misc, 0, "[squirrel] Make sure that '%s' exists before trying to define '%s'", parent_class, class_name);
01b2435c977b (svn r9618) [NoAI] -Add: allow an extra param for PreRegister to register classes as extends of others
truelight
parents: 9577
diff changeset
   138
		return;
01b2435c977b (svn r9618) [NoAI] -Add: allow an extra param for PreRegister to register classes as extends of others
truelight
parents: 9577
diff changeset
   139
	}
01b2435c977b (svn r9618) [NoAI] -Add: allow an extra param for PreRegister to register classes as extends of others
truelight
parents: 9577
diff changeset
   140
	sq_newclass(this->vm, SQTrue);
01b2435c977b (svn r9618) [NoAI] -Add: allow an extra param for PreRegister to register classes as extends of others
truelight
parents: 9577
diff changeset
   141
}
01b2435c977b (svn r9618) [NoAI] -Add: allow an extra param for PreRegister to register classes as extends of others
truelight
parents: 9577
diff changeset
   142
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
   143
void Squirrel::AddClassEnd()
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   144
{
9525
1d6c509b56ee (svn r9445) [NoAI] -Change: changed from sq_createslot to sq_newslot (first is deprecated)
truelight
parents: 9435
diff changeset
   145
	sq_newslot(vm, -3, SQFalse);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   146
}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   147
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
   148
bool Squirrel::MethodExists(HSQOBJECT instance, const char *method_name)
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   149
{
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   150
	int top = sq_gettop(this->vm);
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   151
	/* Go to the instance-root */
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   152
	sq_pushobject(this->vm, instance);
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   153
	/* Find the function-name inside the script */
9435
9dad22394553 (svn r9245) [NoAI] -Fix: allow compilation with _UNICODE
glx
parents: 9434
diff changeset
   154
	sq_pushstring(this->vm, OTTD2FS(method_name), -1);
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   155
	if (SQ_FAILED(sq_get(this->vm, -2))) {
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   156
		sq_settop(this->vm, top);
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   157
		return false;
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   158
	}
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   159
	sq_settop(this->vm, top);
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   160
	return true;
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   161
}
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   162
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
   163
void Squirrel::CallMethod(HSQOBJECT instance, const char *method_name, HSQOBJECT *ret)
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   164
{
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   165
	/* Store the current top */
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   166
	int top = sq_gettop(this->vm);
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   167
	/* Go to the instance-root */
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   168
	sq_pushobject(this->vm, instance);
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   169
	/* Find the function-name inside the script */
9435
9dad22394553 (svn r9245) [NoAI] -Fix: allow compilation with _UNICODE
glx
parents: 9434
diff changeset
   170
	sq_pushstring(this->vm, OTTD2FS(method_name), -1);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   171
	if (SQ_FAILED(sq_get(this->vm, -2))) {
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   172
		DEBUG(misc, 0, "[squirrel] Could not find '%s' in the class", method_name);
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   173
		sq_settop(this->vm, top);
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   174
		return;
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   175
	}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   176
	/* Call the method */
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   177
	sq_pushobject(this->vm, instance);
9404
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   178
	sq_call(this->vm, 1, ret == NULL ? SQFalse : SQTrue, SQFalse);
ef9e171617a3 (svn r9201) [NoAI] -Change: make adding a default-constructor for DefSQClass optional
truelight
parents: 9396
diff changeset
   179
	if (ret != NULL) sq_getstackobj(vm, -1, ret);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   180
	/* Reset the top */
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   181
	sq_settop(this->vm, top);
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   182
}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   183
9674
15f7b310b6cf (svn r10613) [NoAI] -Add r10612: give release-hook for return values of classes for which SQ creates a temporary holder so they are free'd when SQ does
truelight
parents: 9673
diff changeset
   184
/* static */ bool Squirrel::CreateClassInstanceVM(HSQUIRRELVM vm, const char *class_name, void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook)
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   185
{
9673
ee4f133c84ec (svn r10612) [NoAI] -Add: allow a class as return value; pack the result in a SQ instance and it should work perfectly
truelight
parents: 9588
diff changeset
   186
	int oldtop = sq_gettop(vm);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   187
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   188
	/* First, find the class */
9673
ee4f133c84ec (svn r10612) [NoAI] -Add: allow a class as return value; pack the result in a SQ instance and it should work perfectly
truelight
parents: 9588
diff changeset
   189
	sq_pushroottable(vm);
ee4f133c84ec (svn r10612) [NoAI] -Add: allow a class as return value; pack the result in a SQ instance and it should work perfectly
truelight
parents: 9588
diff changeset
   190
	sq_pushstring(vm, OTTD2FS(class_name), -1);
ee4f133c84ec (svn r10612) [NoAI] -Add: allow a class as return value; pack the result in a SQ instance and it should work perfectly
truelight
parents: 9588
diff changeset
   191
	if (SQ_FAILED(sq_get(vm, -2))) {
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   192
		DEBUG(misc, 0, "[squirrel] Failed to find class by the name '%s'", class_name);
9673
ee4f133c84ec (svn r10612) [NoAI] -Add: allow a class as return value; pack the result in a SQ instance and it should work perfectly
truelight
parents: 9588
diff changeset
   193
		sq_settop(vm, oldtop);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   194
		return false;
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   195
	}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   196
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   197
	/* Create the instance */
9673
ee4f133c84ec (svn r10612) [NoAI] -Add: allow a class as return value; pack the result in a SQ instance and it should work perfectly
truelight
parents: 9588
diff changeset
   198
	if (SQ_FAILED(sq_createinstance(vm, -1))) {
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   199
		DEBUG(misc, 0, "[squirrel] Failed to create instance for class '%s'", class_name);
9673
ee4f133c84ec (svn r10612) [NoAI] -Add: allow a class as return value; pack the result in a SQ instance and it should work perfectly
truelight
parents: 9588
diff changeset
   200
		sq_settop(vm, oldtop);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   201
		return false;
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   202
	}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   203
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   204
	if (instance != NULL) {
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   205
		/* Find our instance */
9673
ee4f133c84ec (svn r10612) [NoAI] -Add: allow a class as return value; pack the result in a SQ instance and it should work perfectly
truelight
parents: 9588
diff changeset
   206
		sq_getstackobj(vm, -1, instance);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   207
		/* Add a reference to it, so it survives for ever */
9673
ee4f133c84ec (svn r10612) [NoAI] -Add: allow a class as return value; pack the result in a SQ instance and it should work perfectly
truelight
parents: 9588
diff changeset
   208
		sq_addref(vm, instance);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   209
	}
9679
788e083db48b (svn r10625) [NoAI] -Fix: returning C++ classes, by creating a temp SQ class, had its problems, this should fix them all (double-frees, wrong scope, ..)
truelight
parents: 9674
diff changeset
   210
	sq_remove(vm, -2); // Class-name
788e083db48b (svn r10625) [NoAI] -Fix: returning C++ classes, by creating a temp SQ class, had its problems, this should fix them all (double-frees, wrong scope, ..)
truelight
parents: 9674
diff changeset
   211
	sq_remove(vm, -2); // Root-table
788e083db48b (svn r10625) [NoAI] -Fix: returning C++ classes, by creating a temp SQ class, had its problems, this should fix them all (double-frees, wrong scope, ..)
truelight
parents: 9674
diff changeset
   212
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   213
	/* Store it in the class */
9673
ee4f133c84ec (svn r10612) [NoAI] -Add: allow a class as return value; pack the result in a SQ instance and it should work perfectly
truelight
parents: 9588
diff changeset
   214
	sq_setinstanceup(vm, -1, real_instance);
9674
15f7b310b6cf (svn r10613) [NoAI] -Add r10612: give release-hook for return values of classes for which SQ creates a temporary holder so they are free'd when SQ does
truelight
parents: 9673
diff changeset
   215
	if (release_hook != NULL) sq_setreleasehook(vm, -1, release_hook);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   216
9679
788e083db48b (svn r10625) [NoAI] -Fix: returning C++ classes, by creating a temp SQ class, had its problems, this should fix them all (double-frees, wrong scope, ..)
truelight
parents: 9674
diff changeset
   217
	if (instance != NULL) sq_settop(vm, oldtop);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   218
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   219
	return true;
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   220
}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   221
9673
ee4f133c84ec (svn r10612) [NoAI] -Add: allow a class as return value; pack the result in a SQ instance and it should work perfectly
truelight
parents: 9588
diff changeset
   222
bool Squirrel::CreateClassInstance(const char *class_name, void *real_instance, HSQOBJECT *instance)
ee4f133c84ec (svn r10612) [NoAI] -Add: allow a class as return value; pack the result in a SQ instance and it should work perfectly
truelight
parents: 9588
diff changeset
   223
{
9674
15f7b310b6cf (svn r10613) [NoAI] -Add r10612: give release-hook for return values of classes for which SQ creates a temporary holder so they are free'd when SQ does
truelight
parents: 9673
diff changeset
   224
	return Squirrel::CreateClassInstanceVM(this->vm, class_name, real_instance, instance, NULL);
9673
ee4f133c84ec (svn r10612) [NoAI] -Add: allow a class as return value; pack the result in a SQ instance and it should work perfectly
truelight
parents: 9588
diff changeset
   225
}
ee4f133c84ec (svn r10612) [NoAI] -Add: allow a class as return value; pack the result in a SQ instance and it should work perfectly
truelight
parents: 9588
diff changeset
   226
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
   227
Squirrel::Squirrel()
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   228
{
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   229
	this->vm = sq_open(1024);
9782
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
   230
	this->print_func = NULL;
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
   231
	this->global_pointer = NULL;
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   232
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   233
	/* Handle compile-errors ourself, so we can display it nicely */
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
   234
	sq_setcompilererrorhandler(this->vm, &Squirrel::CompileError);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   235
	sq_notifyallexceptions(this->vm, SQTrue);
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   236
	/* Set a good print-function */
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
   237
	sq_setprintfunc(this->vm, &Squirrel::PrintFunc);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   238
	/* Handle runtime-errors ourself, so we can display it nicely */
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
   239
	sq_newclosure(this->vm, &Squirrel::_RunError, 0);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   240
	sq_seterrorhandler(this->vm);
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   241
9782
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
   242
	/* Set the foreigh pointer, so we can always find this instance from within the VM */
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
   243
	sq_setforeignptr(this->vm, this);
e8d8d8894f23 (svn r12277) [NoAI] -Change: overlay GlobalPointer with local instance access and create sub-node to contain data
truebrain
parents: 9679
diff changeset
   244
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   245
	sq_pushroottable(this->vm);
9577
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9576
diff changeset
   246
	squirrel_register_global_std(this);
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   247
}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   248
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
   249
bool Squirrel::LoadScript(const char *script)
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   250
{
9577
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9576
diff changeset
   251
	/* Make sure we are always in the root-table */
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9576
diff changeset
   252
	sq_pushroottable(this->vm);
8e93be390b45 (svn r9594) [NoAI] -Add: added 'require()' for Squirrel, which allows you to include other files in your .nut
truelight
parents: 9576
diff changeset
   253
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   254
	/* Load and run the script */
9435
9dad22394553 (svn r9245) [NoAI] -Fix: allow compilation with _UNICODE
glx
parents: 9434
diff changeset
   255
	if (SQ_FAILED(sqstd_dofile(this->vm, OTTD2FS(script), SQFalse, SQTrue))) {
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   256
		DEBUG(misc, 0, "[squirrel] Failed to compile '%s'", script);
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   257
		return false;
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   258
	}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   259
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   260
	return true;
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   261
}
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   262
9422
33efcc5f1b09 (svn r9223) [NoAI] -Change: moved squirrel/engine.cpp to squirrel.cpp in root
truelight
parents: 9404
diff changeset
   263
Squirrel::~Squirrel()
9393
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   264
{
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   265
	/* Clean up the stuff */
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   266
	sq_pop(this->vm, 1);
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   267
	sq_close(this->vm);
04bd925b9069 (svn r9185) [NoAI] -Codechange: split up the squirrel code so we have SquirrelCore
truelight
parents:
diff changeset
   268
}