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"
51 #ifdef SCRIPT_DEBUG_ALLOCATIONS
52 std::map<void *, size_t> allocations;
55 void CheckLimit()
const
57 if (this->allocated_size > this->allocation_limit)
throw Script_FatalError(
"Maximum memory allocation exceeded");
71 if (this->allocated_size > this->allocation_limit && !this->error_thrown) {
75 this->error_thrown =
true;
77 seprintf(buff,
lastof(buff),
"Maximum memory allocation exceeded by " PRINTF_SIZE
" bytes when allocating " PRINTF_SIZE
" bytes",
78 this->allocated_size - this->allocation_limit, requested_size);
85 if (this->error_thrown) {
92 this->error_thrown =
true;
94 seprintf(buff,
lastof(buff),
"Out of memory. Cannot allocate " PRINTF_SIZE
" bytes", requested_size);
99 void *Malloc(SQUnsignedInteger size)
101 void *p = malloc(size);
102 this->allocated_size += size;
106 #ifdef SCRIPT_DEBUG_ALLOCATIONS
107 assert(p !=
nullptr);
108 assert(this->allocations.find(p) == this->allocations.end());
109 this->allocations[p] = size;
115 void *Realloc(
void *p, SQUnsignedInteger oldsize, SQUnsignedInteger size)
118 return this->Malloc(size);
121 this->Free(p, oldsize);
125 #ifdef SCRIPT_DEBUG_ALLOCATIONS
126 assert(this->allocations[p] == oldsize);
127 this->allocations.erase(p);
130 void *new_p = realloc(p, size);
132 this->allocated_size -= oldsize;
133 this->allocated_size += size;
137 #ifdef SCRIPT_DEBUG_ALLOCATIONS
138 assert(new_p !=
nullptr);
139 assert(this->allocations.find(p) == this->allocations.end());
140 this->allocations[new_p] = size;
146 void Free(
void *p, SQUnsignedInteger size)
148 if (p ==
nullptr)
return;
150 this->allocated_size -= size;
152 #ifdef SCRIPT_DEBUG_ALLOCATIONS
153 assert(this->allocations.at(p) == size);
154 this->allocations.erase(p);
160 this->allocated_size = 0;
162 if (this->allocation_limit == 0) this->allocation_limit =
SAFE_LIMIT;
163 this->error_thrown =
false;
168 #ifdef SCRIPT_DEBUG_ALLOCATIONS
169 assert(this->allocations.size() == 0);
180 #include "../safeguards.h"
185 #ifndef SQUIRREL_DEFAULT_ALLOCATOR
187 void *sq_vm_realloc(
void *p, SQUnsignedInteger oldsize, SQUnsignedInteger size) {
return _squirrel_allocator->Realloc(p, oldsize, size); }
202 seprintf(buf,
lastof(buf),
"Error %s:" OTTD_PRINTF64
"/" OTTD_PRINTF64
": %s", source, line, column, desc);
208 if (func ==
nullptr) {
209 DEBUG(misc, 0,
"[Squirrel] Compile error: %s", buf);
220 va_start(arglist, s);
226 if (func ==
nullptr) {
227 fprintf(stderr,
"%s", buf);
236 SQPRINTFUNCTION pf = sq_getprintfunc(
vm);
244 if (func ==
nullptr) {
245 fprintf(stderr,
"%s", buf);
251 sqstd_printcallstack(
vm);
253 sq_setprintfunc(
vm, pf);
258 const SQChar *sErr = 0;
260 if (sq_gettop(
vm) >= 1) {
261 if (SQ_SUCCEEDED(sq_getstring(
vm, -1, &sErr))) {
276 va_start(arglist, s);
283 if (func ==
nullptr) {
290 void Squirrel::AddMethod(
const char *method_name, SQFUNCTION proc, uint nparam,
const char *params,
void *userdata,
int size)
294 sq_pushstring(this->
vm, method_name, -1);
297 void *ptr = sq_newuserdata(
vm, size);
298 memcpy(ptr, userdata, size);
301 sq_newclosure(this->
vm, proc, size != 0 ? 1 : 0);
302 if (nparam != 0) sq_setparamscheck(this->
vm, nparam, params);
303 sq_setnativeclosurename(this->
vm, -1, method_name);
304 sq_newslot(this->
vm, -3, SQFalse);
311 sq_pushstring(this->
vm, var_name, -1);
312 sq_pushinteger(this->
vm, value);
313 sq_newslot(this->
vm, -3, SQTrue);
320 sq_pushstring(this->
vm, var_name, -1);
321 sq_pushbool(this->
vm, value);
322 sq_newslot(this->
vm, -3, SQTrue);
329 sq_pushroottable(this->
vm);
330 sq_pushstring(this->
vm, class_name, -1);
331 sq_newclass(this->
vm, SQFalse);
338 sq_pushroottable(this->
vm);
339 sq_pushstring(this->
vm, class_name, -1);
340 sq_pushstring(this->
vm, parent_class, -1);
341 if (SQ_FAILED(sq_get(this->
vm, -3))) {
342 DEBUG(misc, 0,
"[squirrel] Failed to initialize class '%s' based on parent class '%s'", class_name, parent_class);
343 DEBUG(misc, 0,
"[squirrel] Make sure that '%s' exists before trying to define '%s'", parent_class, class_name);
346 sq_newclass(this->
vm, SQTrue);
353 sq_newslot(
vm, -3, SQFalse);
362 int top = sq_gettop(this->
vm);
364 sq_pushobject(this->
vm, instance);
366 sq_pushstring(this->
vm, method_name, -1);
367 if (SQ_FAILED(sq_get(this->
vm, -2))) {
368 sq_settop(this->
vm, top);
371 sq_settop(this->
vm, top);
391 this->
crashed = !sq_resumecatch(this->
vm, suspend);
394 return this->
vm->_suspended != 0;
401 sq_resumeerror(this->
vm);
407 sq_collectgarbage(this->
vm);
419 SQInteger last_target = this->
vm->_suspended_target;
421 int top = sq_gettop(this->
vm);
423 sq_pushobject(this->
vm, instance);
425 sq_pushstring(this->
vm, method_name, -1);
426 if (SQ_FAILED(sq_get(this->
vm, -2))) {
427 DEBUG(misc, 0,
"[squirrel] Could not find '%s' in the class", method_name);
428 sq_settop(this->
vm, top);
432 sq_pushobject(this->
vm, instance);
433 if (SQ_FAILED(sq_call(this->
vm, 1, ret ==
nullptr ? SQFalse : SQTrue, SQTrue, suspend)))
return false;
434 if (ret !=
nullptr) sq_getstackobj(
vm, -1, ret);
437 if (suspend == -1 || !this->
IsSuspended()) sq_settop(this->
vm, top);
439 this->
vm->_suspended_target = last_target;
444 bool Squirrel::CallStringMethodStrdup(HSQOBJECT instance,
const char *method_name,
const char **res,
int suspend)
447 if (!this->
CallMethod(instance, method_name, &ret, suspend))
return false;
448 if (ret._type != OT_STRING)
return false;
454 bool Squirrel::CallIntegerMethod(HSQOBJECT instance,
const char *method_name,
int *res,
int suspend)
457 if (!this->
CallMethod(instance, method_name, &ret, suspend))
return false;
458 if (ret._type != OT_INTEGER)
return false;
463 bool Squirrel::CallBoolMethod(HSQOBJECT instance,
const char *method_name,
bool *res,
int suspend)
466 if (!this->
CallMethod(instance, method_name, &ret, suspend))
return false;
467 if (ret._type != OT_BOOL)
return false;
472 bool Squirrel::CreateClassInstanceVM(HSQUIRRELVM vm,
const char *class_name,
void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook,
bool prepend_API_name)
476 int oldtop = sq_gettop(
vm);
479 sq_pushroottable(
vm);
481 if (prepend_API_name) {
482 size_t len = strlen(class_name) + strlen(engine->
GetAPIName()) + 1;
483 char *class_name2 = (
char *)alloca(len);
484 seprintf(class_name2, class_name2 + len - 1,
"%s%s", engine->
GetAPIName(), class_name);
486 sq_pushstring(
vm, class_name2, -1);
488 sq_pushstring(
vm, class_name, -1);
491 if (SQ_FAILED(sq_get(
vm, -2))) {
492 DEBUG(misc, 0,
"[squirrel] Failed to find class by the name '%s%s'", prepend_API_name ? engine->
GetAPIName() :
"", class_name);
493 sq_settop(
vm, oldtop);
498 if (SQ_FAILED(sq_createinstance(
vm, -1))) {
499 DEBUG(misc, 0,
"[squirrel] Failed to create instance for class '%s%s'", prepend_API_name ? engine->
GetAPIName() :
"", class_name);
500 sq_settop(
vm, oldtop);
504 if (instance !=
nullptr) {
506 sq_getstackobj(
vm, -1, instance);
508 sq_addref(
vm, instance);
514 sq_setinstanceup(
vm, -1, real_instance);
515 if (release_hook !=
nullptr) sq_setreleasehook(
vm, -1, release_hook);
517 if (instance !=
nullptr) sq_settop(
vm, oldtop);
528 Squirrel::Squirrel(
const char *APIName) :
542 this->
vm = sq_open(1024);
546 sq_notifyallexceptions(this->
vm, _debug_script_level > 5);
551 sq_seterrorhandler(this->
vm);
554 sq_setforeignptr(this->
vm,
this);
556 sq_pushroottable(this->
vm);
567 SQFile(FILE *file,
size_t size) : file(file), size(size), pos(0) {}
569 size_t Read(
void *buf,
size_t elemsize,
size_t count)
571 assert(elemsize != 0);
572 if (this->pos + (elemsize * count) > this->size) {
573 count = (this->size - this->pos) / elemsize;
575 if (count == 0)
return 0;
576 size_t ret = fread(buf, elemsize, count, this->file);
577 this->pos += ret * elemsize;
582 static WChar _io_file_lexfeed_ASCII(SQUserPointer file)
585 if (((
SQFile *)file)->Read(&c,
sizeof(c), 1) > 0)
return c;
589 static WChar _io_file_lexfeed_UTF8(SQUserPointer file)
594 if (((
SQFile *)file)->Read(buffer,
sizeof(buffer[0]), 1) != 1)
return 0;
596 if (len == 0)
return -1;
599 if (len > 1 && ((
SQFile *)file)->Read(buffer + 1,
sizeof(buffer[0]), len - 1) != len - 1)
return 0;
608 static WChar _io_file_lexfeed_UCS2_no_swap(SQUserPointer file)
611 if (((
SQFile *)file)->Read(&c,
sizeof(c), 1) > 0)
return (
WChar)c;
615 static WChar _io_file_lexfeed_UCS2_swap(SQUserPointer file)
618 if (((
SQFile *)file)->Read(&c,
sizeof(c), 1) > 0) {
619 c = ((c >> 8) & 0x00FF)| ((c << 8) & 0xFF00);
625 static SQInteger _io_file_read(SQUserPointer file, SQUserPointer buf, SQInteger size)
627 SQInteger ret = ((
SQFile *)file)->Read(buf, 1, size);
628 if (ret == 0)
return -1;
638 if (strncmp(this->
GetAPIName(),
"AI", 2) == 0) {
641 }
else if (strncmp(this->
GetAPIName(),
"GS", 2) == 0) {
648 if (file ==
nullptr) {
649 return sq_throwerror(
vm,
"cannot open the file");
651 unsigned short bom = 0;
653 size_t sr = fread(&bom, 1,
sizeof(bom), file);
659 case SQ_BYTECODE_STREAM_TAG: {
660 if (fseek(file, -2, SEEK_CUR) < 0) {
662 return sq_throwerror(
vm,
"cannot seek the file");
666 if (SQ_SUCCEEDED(sq_readclosure(
vm, _io_file_read, &f))) {
671 return sq_throwerror(
vm,
"Couldn't read bytecode");
677 func = _io_file_lexfeed_UCS2_swap;
681 func = _io_file_lexfeed_UCS2_no_swap;
689 return sq_throwerror(
vm,
"I/O error");
692 if (fread(&uc, 1,
sizeof(uc), file) !=
sizeof(uc) || uc != 0xBF) {
694 return sq_throwerror(
vm,
"Unrecognized encoding");
696 func = _io_file_lexfeed_UTF8;
701 func = _io_file_lexfeed_ASCII;
703 if (size >= 2 && fseek(file, -2, SEEK_CUR) < 0) {
705 return sq_throwerror(
vm,
"cannot seek the file");
711 if (SQ_SUCCEEDED(sq_compile(
vm, func, &f, filename, printerror))) {
724 if (in_root) sq_pushroottable(
vm);
726 SQInteger ops_left =
vm->_ops_till_suspend;
728 if (SQ_SUCCEEDED(
LoadFile(
vm, script, SQTrue))) {
730 if (SQ_SUCCEEDED(sq_call(
vm, 1, SQFalse, SQTrue, 100000))) {
733 vm->_ops_till_suspend = ops_left;
738 vm->_ops_till_suspend = ops_left;
739 DEBUG(misc, 0,
"[squirrel] Failed to compile '%s'", script);
748 Squirrel::~Squirrel()
768 void Squirrel::InsertResult(
bool result)
772 sq_pushbool(this->vm, result);
774 vm->GetAt(
vm->_stackbase +
vm->_suspended_target) =
vm->GetUp(-1);
779 void Squirrel::InsertResult(
int result)
783 sq_pushinteger(this->vm, result);
785 vm->GetAt(
vm->_stackbase +
vm->_suspended_target) =
vm->GetUp(-1);
792 vm->DecreaseOps(ops);
797 return this->vm->_suspended != 0;
813 return sq_can_suspend(this->vm);
818 return this->vm->_ops_till_suspend;