12 #include "../stdafx.h"
15 #include "../fileio_func.h"
16 #include "../string_func.h"
18 #include "../settings_type.h"
20 #include <../squirrel/sqpcheader.h>
21 #include <../squirrel/sqvm.h>
22 #include "../core/alloc_func.hpp"
24 #include "../safeguards.h"
38 #ifdef SCRIPT_DEBUG_ALLOCATIONS
39 std::map<void *, size_t> allocations;
42 void CheckLimit()
const
44 if (this->allocated_size > this->allocation_limit)
throw Script_FatalError(
"Maximum memory allocation exceeded");
47 void *Malloc(SQUnsignedInteger size)
49 void *p = MallocT<char>(size);
50 this->allocated_size += size;
52 #ifdef SCRIPT_DEBUG_ALLOCATIONS
54 assert(this->allocations.find(p) == this->allocations.end());
55 this->allocations[p] = size;
61 void *Realloc(
void *p, SQUnsignedInteger oldsize, SQUnsignedInteger size)
64 return this->Malloc(size);
67 this->Free(p, oldsize);
71 #ifdef SCRIPT_DEBUG_ALLOCATIONS
72 assert(this->allocations[p] == oldsize);
73 this->allocations.erase(p);
76 void *new_p = ReallocT<char>(
static_cast<char *
>(p), size);
78 this->allocated_size -= oldsize;
79 this->allocated_size += size;
81 #ifdef SCRIPT_DEBUG_ALLOCATIONS
82 assert(new_p !=
nullptr);
83 assert(this->allocations.find(p) == this->allocations.end());
84 this->allocations[new_p] = size;
90 void Free(
void *p, SQUnsignedInteger size)
92 if (p ==
nullptr)
return;
94 this->allocated_size -= size;
96 #ifdef SCRIPT_DEBUG_ALLOCATIONS
97 assert(this->allocations.at(p) == size);
98 this->allocations.erase(p);
104 this->allocated_size = 0;
106 if (this->allocation_limit == 0) this->allocation_limit =
SAFE_LIMIT;
111 #ifdef SCRIPT_DEBUG_ALLOCATIONS
112 assert(this->allocations.size() == 0);
120 #ifndef SQUIRREL_DEFAULT_ALLOCATOR
121 void *sq_vm_malloc(SQUnsignedInteger size) {
return _squirrel_allocator->Malloc(size); }
122 void *sq_vm_realloc(
void *p, SQUnsignedInteger oldsize, SQUnsignedInteger size) {
return _squirrel_allocator->Realloc(p, oldsize, size); }
123 void sq_vm_free(
void *p, SQUnsignedInteger size) { _squirrel_allocator->Free(p, size); }
137 seprintf(buf,
lastof(buf),
"Error %s:" OTTD_PRINTF64
"/" OTTD_PRINTF64
": %s", source, line, column, desc);
143 if (func ==
nullptr) {
144 DEBUG(misc, 0,
"[Squirrel] Compile error: %s", buf);
155 va_start(arglist, s);
161 if (func ==
nullptr) {
162 fprintf(stderr,
"%s", buf);
171 SQPRINTFUNCTION pf = sq_getprintfunc(
vm);
179 if (func ==
nullptr) {
180 fprintf(stderr,
"%s", buf);
186 sqstd_printcallstack(
vm);
188 sq_setprintfunc(
vm, pf);
193 const SQChar *sErr = 0;
195 if (sq_gettop(
vm) >= 1) {
196 if (SQ_SUCCEEDED(sq_getstring(
vm, -1, &sErr))) {
211 va_start(arglist, s);
218 if (func ==
nullptr) {
225 void Squirrel::AddMethod(
const char *method_name, SQFUNCTION proc, uint nparam,
const char *params,
void *userdata,
int size)
229 sq_pushstring(this->
vm, method_name, -1);
232 void *ptr = sq_newuserdata(
vm, size);
233 memcpy(ptr, userdata, size);
236 sq_newclosure(this->
vm, proc, size != 0 ? 1 : 0);
237 if (nparam != 0) sq_setparamscheck(this->
vm, nparam, params);
238 sq_setnativeclosurename(this->
vm, -1, method_name);
239 sq_newslot(this->
vm, -3, SQFalse);
246 sq_pushstring(this->
vm, var_name, -1);
247 sq_pushinteger(this->
vm, value);
248 sq_newslot(this->
vm, -3, SQTrue);
255 sq_pushstring(this->
vm, var_name, -1);
256 sq_pushbool(this->
vm, value);
257 sq_newslot(this->
vm, -3, SQTrue);
264 sq_pushroottable(this->
vm);
265 sq_pushstring(this->
vm, class_name, -1);
266 sq_newclass(this->
vm, SQFalse);
273 sq_pushroottable(this->
vm);
274 sq_pushstring(this->
vm, class_name, -1);
275 sq_pushstring(this->
vm, parent_class, -1);
276 if (SQ_FAILED(sq_get(this->
vm, -3))) {
277 DEBUG(misc, 0,
"[squirrel] Failed to initialize class '%s' based on parent class '%s'", class_name, parent_class);
278 DEBUG(misc, 0,
"[squirrel] Make sure that '%s' exists before trying to define '%s'", parent_class, class_name);
281 sq_newclass(this->
vm, SQTrue);
288 sq_newslot(
vm, -3, SQFalse);
297 int top = sq_gettop(this->
vm);
299 sq_pushobject(this->
vm, instance);
301 sq_pushstring(this->
vm, method_name, -1);
302 if (SQ_FAILED(sq_get(this->
vm, -2))) {
303 sq_settop(this->
vm, top);
306 sq_settop(this->
vm, top);
326 this->
crashed = !sq_resumecatch(this->
vm, suspend);
329 return this->
vm->_suspended != 0;
336 sq_resumeerror(this->
vm);
342 sq_collectgarbage(this->
vm);
354 SQInteger last_target = this->
vm->_suspended_target;
356 int top = sq_gettop(this->
vm);
358 sq_pushobject(this->
vm, instance);
360 sq_pushstring(this->
vm, method_name, -1);
361 if (SQ_FAILED(sq_get(this->
vm, -2))) {
362 DEBUG(misc, 0,
"[squirrel] Could not find '%s' in the class", method_name);
363 sq_settop(this->
vm, top);
367 sq_pushobject(this->
vm, instance);
368 if (SQ_FAILED(sq_call(this->
vm, 1, ret ==
nullptr ? SQFalse : SQTrue, SQTrue, suspend)))
return false;
369 if (ret !=
nullptr) sq_getstackobj(
vm, -1, ret);
372 if (suspend == -1 || !this->
IsSuspended()) sq_settop(this->
vm, top);
374 this->
vm->_suspended_target = last_target;
379 bool Squirrel::CallStringMethodStrdup(HSQOBJECT instance,
const char *method_name,
const char **res,
int suspend)
382 if (!this->
CallMethod(instance, method_name, &ret, suspend))
return false;
383 if (ret._type != OT_STRING)
return false;
389 bool Squirrel::CallIntegerMethod(HSQOBJECT instance,
const char *method_name,
int *res,
int suspend)
392 if (!this->
CallMethod(instance, method_name, &ret, suspend))
return false;
393 if (ret._type != OT_INTEGER)
return false;
398 bool Squirrel::CallBoolMethod(HSQOBJECT instance,
const char *method_name,
bool *res,
int suspend)
401 if (!this->
CallMethod(instance, method_name, &ret, suspend))
return false;
402 if (ret._type != OT_BOOL)
return false;
407 bool Squirrel::CreateClassInstanceVM(HSQUIRRELVM vm,
const char *class_name,
void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook,
bool prepend_API_name)
411 int oldtop = sq_gettop(
vm);
414 sq_pushroottable(
vm);
416 if (prepend_API_name) {
417 size_t len = strlen(class_name) + strlen(engine->
GetAPIName()) + 1;
418 char *class_name2 = (
char *)alloca(len);
419 seprintf(class_name2, class_name2 + len - 1,
"%s%s", engine->
GetAPIName(), class_name);
421 sq_pushstring(
vm, class_name2, -1);
423 sq_pushstring(
vm, class_name, -1);
426 if (SQ_FAILED(sq_get(
vm, -2))) {
427 DEBUG(misc, 0,
"[squirrel] Failed to find class by the name '%s%s'", prepend_API_name ? engine->
GetAPIName() :
"", class_name);
428 sq_settop(
vm, oldtop);
433 if (SQ_FAILED(sq_createinstance(
vm, -1))) {
434 DEBUG(misc, 0,
"[squirrel] Failed to create instance for class '%s%s'", prepend_API_name ? engine->
GetAPIName() :
"", class_name);
435 sq_settop(
vm, oldtop);
439 if (instance !=
nullptr) {
441 sq_getstackobj(
vm, -1, instance);
443 sq_addref(
vm, instance);
449 sq_setinstanceup(
vm, -1, real_instance);
450 if (release_hook !=
nullptr) sq_setreleasehook(
vm, -1, release_hook);
452 if (instance !=
nullptr) sq_settop(
vm, oldtop);
463 Squirrel::Squirrel(
const char *APIName) :
477 this->
vm = sq_open(1024);
481 sq_notifyallexceptions(this->
vm, _debug_script_level > 5);
486 sq_seterrorhandler(this->
vm);
489 sq_setforeignptr(this->
vm,
this);
491 sq_pushroottable(this->
vm);
502 SQFile(FILE *file,
size_t size) : file(file), size(size), pos(0) {}
504 size_t Read(
void *buf,
size_t elemsize,
size_t count)
506 assert(elemsize != 0);
507 if (this->pos + (elemsize * count) > this->size) {
508 count = (this->size - this->pos) / elemsize;
510 if (count == 0)
return 0;
511 size_t ret = fread(buf, elemsize, count, this->file);
512 this->pos += ret * elemsize;
517 static WChar _io_file_lexfeed_ASCII(SQUserPointer file)
520 if (((
SQFile *)file)->Read(&c,
sizeof(c), 1) > 0)
return c;
524 static WChar _io_file_lexfeed_UTF8(SQUserPointer file)
529 if (((
SQFile *)file)->Read(buffer,
sizeof(buffer[0]), 1) != 1)
return 0;
531 if (len == 0)
return -1;
534 if (len > 1 && ((
SQFile *)file)->Read(buffer + 1,
sizeof(buffer[0]), len - 1) != len - 1)
return 0;
543 static WChar _io_file_lexfeed_UCS2_no_swap(SQUserPointer file)
546 if (((
SQFile *)file)->Read(&c,
sizeof(c), 1) > 0)
return (
WChar)c;
550 static WChar _io_file_lexfeed_UCS2_swap(SQUserPointer file)
553 if (((
SQFile *)file)->Read(&c,
sizeof(c), 1) > 0) {
554 c = ((c >> 8) & 0x00FF)| ((c << 8) & 0xFF00);
560 static SQInteger _io_file_read(SQUserPointer file, SQUserPointer buf, SQInteger size)
562 SQInteger ret = ((
SQFile *)file)->Read(buf, 1, size);
563 if (ret == 0)
return -1;
573 if (strncmp(this->
GetAPIName(),
"AI", 2) == 0) {
576 }
else if (strncmp(this->
GetAPIName(),
"GS", 2) == 0) {
583 if (file ==
nullptr) {
584 return sq_throwerror(
vm,
"cannot open the file");
586 unsigned short bom = 0;
588 size_t sr = fread(&bom, 1,
sizeof(bom), file);
594 case SQ_BYTECODE_STREAM_TAG: {
595 if (fseek(file, -2, SEEK_CUR) < 0) {
597 return sq_throwerror(
vm,
"cannot seek the file");
601 if (SQ_SUCCEEDED(sq_readclosure(
vm, _io_file_read, &f))) {
606 return sq_throwerror(
vm,
"Couldn't read bytecode");
612 func = _io_file_lexfeed_UCS2_swap;
616 func = _io_file_lexfeed_UCS2_no_swap;
624 return sq_throwerror(
vm,
"I/O error");
627 if (fread(&uc, 1,
sizeof(uc), file) !=
sizeof(uc) || uc != 0xBF) {
629 return sq_throwerror(
vm,
"Unrecognized encoding");
631 func = _io_file_lexfeed_UTF8;
636 func = _io_file_lexfeed_ASCII;
638 if (size >= 2 && fseek(file, -2, SEEK_CUR) < 0) {
640 return sq_throwerror(
vm,
"cannot seek the file");
646 if (SQ_SUCCEEDED(sq_compile(
vm, func, &f, filename, printerror))) {
659 if (in_root) sq_pushroottable(
vm);
661 SQInteger ops_left =
vm->_ops_till_suspend;
663 if (SQ_SUCCEEDED(
LoadFile(
vm, script, SQTrue))) {
665 if (SQ_SUCCEEDED(sq_call(
vm, 1, SQFalse, SQTrue, 100000))) {
668 vm->_ops_till_suspend = ops_left;
673 vm->_ops_till_suspend = ops_left;
674 DEBUG(misc, 0,
"[squirrel] Failed to compile '%s'", script);
683 Squirrel::~Squirrel()
703 void Squirrel::InsertResult(
bool result)
707 sq_pushbool(this->vm, result);
709 vm->GetAt(
vm->_stackbase +
vm->_suspended_target) =
vm->GetUp(-1);
714 void Squirrel::InsertResult(
int result)
718 sq_pushinteger(this->vm, result);
720 vm->GetAt(
vm->_stackbase +
vm->_suspended_target) =
vm->GetUp(-1);
727 vm->DecreaseOps(ops);
732 return this->vm->_suspended != 0;
748 return sq_can_suspend(this->vm);
753 return this->vm->_ops_till_suspend;