10 #include "../stdafx.h"
13 #include "../fileio_func.h"
14 #include "../string_func.h"
16 #include "../settings_type.h"
18 #include <../squirrel/sqpcheader.h>
19 #include <../squirrel/sqvm.h>
20 #include "../core/alloc_func.hpp"
52 #ifdef SCRIPT_DEBUG_ALLOCATIONS
53 std::map<void *, size_t> allocations;
56 void CheckLimit()
const
58 if (this->allocated_size > this->allocation_limit)
throw Script_FatalError(
"Maximum memory allocation exceeded");
72 if (this->allocated_size > this->allocation_limit && !this->error_thrown) {
76 this->error_thrown =
true;
78 seprintf(buff,
lastof(buff),
"Maximum memory allocation exceeded by " PRINTF_SIZE
" bytes when allocating " PRINTF_SIZE
" bytes",
79 this->allocated_size - this->allocation_limit, requested_size);
86 if (this->error_thrown) {
93 this->error_thrown =
true;
95 seprintf(buff,
lastof(buff),
"Out of memory. Cannot allocate " PRINTF_SIZE
" bytes", requested_size);
100 void *Malloc(SQUnsignedInteger size)
102 void *p = malloc(size);
103 this->allocated_size += size;
107 #ifdef SCRIPT_DEBUG_ALLOCATIONS
108 assert(p !=
nullptr);
109 assert(this->allocations.find(p) == this->allocations.end());
110 this->allocations[p] = size;
116 void *Realloc(
void *p, SQUnsignedInteger oldsize, SQUnsignedInteger size)
119 return this->Malloc(size);
122 this->Free(p, oldsize);
126 #ifdef SCRIPT_DEBUG_ALLOCATIONS
127 assert(this->allocations[p] == oldsize);
128 this->allocations.erase(p);
131 void *new_p = realloc(p, size);
133 this->allocated_size -= oldsize;
134 this->allocated_size += size;
138 #ifdef SCRIPT_DEBUG_ALLOCATIONS
139 assert(new_p !=
nullptr);
140 assert(this->allocations.find(p) == this->allocations.end());
141 this->allocations[new_p] = size;
147 void Free(
void *p, SQUnsignedInteger size)
149 if (p ==
nullptr)
return;
151 this->allocated_size -= size;
153 #ifdef SCRIPT_DEBUG_ALLOCATIONS
154 assert(this->allocations.at(p) == size);
155 this->allocations.erase(p);
161 this->allocated_size = 0;
163 if (this->allocation_limit == 0) this->allocation_limit =
SAFE_LIMIT;
164 this->error_thrown =
false;
169 #ifdef SCRIPT_DEBUG_ALLOCATIONS
170 assert(this->allocations.size() == 0);
181 #include "../safeguards.h"
186 #ifndef SQUIRREL_DEFAULT_ALLOCATOR
188 void *sq_vm_realloc(
void *p, SQUnsignedInteger oldsize, SQUnsignedInteger size) {
return _squirrel_allocator->Realloc(p, oldsize, size); }
203 seprintf(buf,
lastof(buf),
"Error %s:" OTTD_PRINTF64
"/" OTTD_PRINTF64
": %s", source, line, column, desc);
209 if (func ==
nullptr) {
210 Debug(misc, 0,
"[Squirrel] Compile error: {}", buf);
221 va_start(arglist, s);
227 if (func ==
nullptr) {
228 fprintf(stderr,
"%s", buf);
237 SQPRINTFUNCTION pf = sq_getprintfunc(
vm);
245 if (func ==
nullptr) {
246 fprintf(stderr,
"%s", buf);
252 sqstd_printcallstack(
vm);
254 sq_setprintfunc(
vm, pf);
259 const SQChar *sErr =
nullptr;
261 if (sq_gettop(
vm) >= 1) {
262 if (SQ_SUCCEEDED(sq_getstring(
vm, -1, &sErr))) {
277 va_start(arglist, s);
284 if (func ==
nullptr) {
291 void Squirrel::AddMethod(
const char *method_name, SQFUNCTION proc, uint nparam,
const char *params,
void *userdata,
int size)
295 sq_pushstring(this->
vm, method_name, -1);
298 void *ptr = sq_newuserdata(
vm, size);
299 memcpy(ptr, userdata, size);
302 sq_newclosure(this->
vm, proc, size != 0 ? 1 : 0);
303 if (nparam != 0) sq_setparamscheck(this->
vm, nparam, params);
304 sq_setnativeclosurename(this->
vm, -1, method_name);
305 sq_newslot(this->
vm, -3, SQFalse);
312 sq_pushstring(this->
vm, var_name, -1);
313 sq_pushinteger(this->
vm, value);
314 sq_newslot(this->
vm, -3, SQTrue);
321 sq_pushstring(this->
vm, var_name, -1);
322 sq_pushbool(this->
vm, value);
323 sq_newslot(this->
vm, -3, SQTrue);
330 sq_pushroottable(this->
vm);
331 sq_pushstring(this->
vm, class_name, -1);
332 sq_newclass(this->
vm, SQFalse);
339 sq_pushroottable(this->
vm);
340 sq_pushstring(this->
vm, class_name, -1);
341 sq_pushstring(this->
vm, parent_class, -1);
342 if (SQ_FAILED(sq_get(this->
vm, -3))) {
343 Debug(misc, 0,
"[squirrel] Failed to initialize class '{}' based on parent class '{}'", class_name, parent_class);
344 Debug(misc, 0,
"[squirrel] Make sure that '{}' exists before trying to define '{}'", parent_class, class_name);
347 sq_newclass(this->
vm, SQTrue);
354 sq_newslot(
vm, -3, SQFalse);
363 int top = sq_gettop(this->
vm);
365 sq_pushobject(this->
vm, instance);
367 sq_pushstring(this->
vm, method_name, -1);
368 if (SQ_FAILED(sq_get(this->
vm, -2))) {
369 sq_settop(this->
vm, top);
372 sq_settop(this->
vm, top);
392 this->
crashed = !sq_resumecatch(this->
vm, suspend);
395 return this->
vm->_suspended != 0;
402 sq_resumeerror(this->
vm);
408 sq_collectgarbage(this->
vm);
420 SQInteger last_target = this->
vm->_suspended_target;
422 int top = sq_gettop(this->
vm);
424 sq_pushobject(this->
vm, instance);
426 sq_pushstring(this->
vm, method_name, -1);
427 if (SQ_FAILED(sq_get(this->
vm, -2))) {
428 Debug(misc, 0,
"[squirrel] Could not find '{}' in the class", method_name);
429 sq_settop(this->
vm, top);
433 sq_pushobject(this->
vm, instance);
434 if (SQ_FAILED(sq_call(this->
vm, 1, ret ==
nullptr ? SQFalse : SQTrue, SQTrue, suspend)))
return false;
435 if (ret !=
nullptr) sq_getstackobj(
vm, -1, ret);
438 if (suspend == -1 || !this->
IsSuspended()) sq_settop(this->
vm, top);
440 this->
vm->_suspended_target = last_target;
445 bool Squirrel::CallStringMethodStrdup(HSQOBJECT instance,
const char *method_name,
const char **res,
int suspend)
448 if (!this->
CallMethod(instance, method_name, &ret, suspend))
return false;
449 if (ret._type != OT_STRING)
return false;
455 bool Squirrel::CallIntegerMethod(HSQOBJECT instance,
const char *method_name,
int *res,
int suspend)
458 if (!this->
CallMethod(instance, method_name, &ret, suspend))
return false;
459 if (ret._type != OT_INTEGER)
return false;
464 bool Squirrel::CallBoolMethod(HSQOBJECT instance,
const char *method_name,
bool *res,
int suspend)
467 if (!this->
CallMethod(instance, method_name, &ret, suspend))
return false;
468 if (ret._type != OT_BOOL)
return false;
473 bool Squirrel::CreateClassInstanceVM(HSQUIRRELVM vm,
const char *class_name,
void *real_instance, HSQOBJECT *instance, SQRELEASEHOOK release_hook,
bool prepend_API_name)
477 int oldtop = sq_gettop(
vm);
480 sq_pushroottable(
vm);
482 if (prepend_API_name) {
483 size_t len = strlen(class_name) + strlen(engine->
GetAPIName()) + 1;
484 char *class_name2 = (
char *)alloca(len);
485 seprintf(class_name2, class_name2 + len - 1,
"%s%s", engine->
GetAPIName(), class_name);
487 sq_pushstring(
vm, class_name2, -1);
489 sq_pushstring(
vm, class_name, -1);
492 if (SQ_FAILED(sq_get(
vm, -2))) {
493 Debug(misc, 0,
"[squirrel] Failed to find class by the name '{}{}'", prepend_API_name ? engine->
GetAPIName() :
"", class_name);
494 sq_settop(
vm, oldtop);
499 if (SQ_FAILED(sq_createinstance(
vm, -1))) {
500 Debug(misc, 0,
"[squirrel] Failed to create instance for class '{}{}'", prepend_API_name ? engine->
GetAPIName() :
"", class_name);
501 sq_settop(
vm, oldtop);
505 if (instance !=
nullptr) {
507 sq_getstackobj(
vm, -1, instance);
509 sq_addref(
vm, instance);
515 sq_setinstanceup(
vm, -1, real_instance);
516 if (release_hook !=
nullptr) sq_setreleasehook(
vm, -1, release_hook);
518 if (instance !=
nullptr) sq_settop(
vm, oldtop);
529 Squirrel::Squirrel(
const char *APIName) :
543 this->
vm = sq_open(1024);
547 sq_notifyallexceptions(this->
vm, _debug_script_level > 5);
552 sq_seterrorhandler(this->
vm);
555 sq_setforeignptr(this->
vm,
this);
557 sq_pushroottable(this->
vm);
568 SQFile(FILE *file,
size_t size) : file(file), size(size), pos(0) {}
570 size_t Read(
void *buf,
size_t elemsize,
size_t count)
572 assert(elemsize != 0);
573 if (this->pos + (elemsize * count) > this->size) {
574 count = (this->size - this->pos) / elemsize;
576 if (count == 0)
return 0;
577 size_t ret = fread(buf, elemsize, count, this->file);
578 this->pos += ret * elemsize;
583 static WChar _io_file_lexfeed_ASCII(SQUserPointer file)
586 if (((
SQFile *)file)->Read(&c,
sizeof(c), 1) > 0)
return c;
590 static WChar _io_file_lexfeed_UTF8(SQUserPointer file)
595 if (((
SQFile *)file)->Read(buffer,
sizeof(buffer[0]), 1) != 1)
return 0;
597 if (len == 0)
return -1;
600 if (len > 1 && ((
SQFile *)file)->Read(buffer + 1,
sizeof(buffer[0]), len - 1) != len - 1)
return 0;
609 static WChar _io_file_lexfeed_UCS2_no_swap(SQUserPointer file)
612 if (((
SQFile *)file)->Read(&c,
sizeof(c), 1) > 0)
return (
WChar)c;
616 static WChar _io_file_lexfeed_UCS2_swap(SQUserPointer file)
619 if (((
SQFile *)file)->Read(&c,
sizeof(c), 1) > 0) {
620 c = ((c >> 8) & 0x00FF)| ((c << 8) & 0xFF00);
626 static SQInteger _io_file_read(SQUserPointer file, SQUserPointer buf, SQInteger size)
628 SQInteger ret = ((
SQFile *)file)->Read(buf, 1, size);
629 if (ret == 0)
return -1;
639 if (strncmp(this->
GetAPIName(),
"AI", 2) == 0) {
642 }
else if (strncmp(this->
GetAPIName(),
"GS", 2) == 0) {
649 if (file ==
nullptr) {
650 return sq_throwerror(
vm,
"cannot open the file");
652 unsigned short bom = 0;
654 [[maybe_unused]]
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 '{}'", 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;