src/squirrel.cpp
branchnoai
changeset 9782 e8d8d8894f23
parent 9679 788e083db48b
child 9789 33d3214a2fce
equal deleted inserted replaced
9781:d583b3eb60e0 9782:e8d8d8894f23
    25 }
    25 }
    26 
    26 
    27 void Squirrel::ErrorPrintFunc(HSQUIRRELVM vm, const SQChar *s, ...)
    27 void Squirrel::ErrorPrintFunc(HSQUIRRELVM vm, const SQChar *s, ...)
    28 {
    28 {
    29 	va_list arglist;
    29 	va_list arglist;
       
    30 	char buf[1024];
       
    31 
    30 	va_start(arglist, s);
    32 	va_start(arglist, s);
    31 	scvfprintf(stderr, s, arglist);
    33 	vsnprintf(buf, lengthof(buf), s, arglist);
    32 	va_end(arglist);
    34 	va_end(arglist);
       
    35 
       
    36 	/* Check if we have a custom print function */
       
    37 	SQPrintFunc *func = ((Squirrel *)sq_getforeignptr(vm))->print_func;
       
    38 	if (func == NULL) fprintf(stderr, "%s", buf);
       
    39 	else (*func)(true, buf);
    33 }
    40 }
    34 
    41 
    35 void Squirrel::RunError(HSQUIRRELVM vm, const char *error)
    42 void Squirrel::RunError(HSQUIRRELVM vm, const char *error)
    36 {
    43 {
    37 	/* Set the print function to something that prints to stderr */
    44 	/* Set the print function to something that prints to stderr */
    38 	SQPRINTFUNCTION pf = sq_getprintfunc(vm);
    45 	SQPRINTFUNCTION pf = sq_getprintfunc(vm);
    39 	sq_setprintfunc(vm, &Squirrel::ErrorPrintFunc);
    46 	sq_setprintfunc(vm, &Squirrel::ErrorPrintFunc);
    40 	DEBUG(misc, 0, "Your script made an error: %s", error);
    47 
       
    48 	/* Check if we have a custom print function */
       
    49 	char buf[1024];
       
    50 	snprintf(buf, lengthof(buf), "Your script made an error: %s\n", error);
       
    51 	SQPrintFunc *func = ((Squirrel *)sq_getforeignptr(vm))->print_func;
       
    52 	if (func == NULL) fprintf(stderr, "%s", buf);
       
    53 	else (*func)(true, buf);
       
    54 
    41 	/* Print below the error the stack, so the users knows what is happening */
    55 	/* Print below the error the stack, so the users knows what is happening */
    42 	sqstd_printcallstack(vm);
    56 	sqstd_printcallstack(vm);
    43 	/* Reset the old print function */
    57 	/* Reset the old print function */
    44 	sq_setprintfunc(vm, pf);
    58 	sq_setprintfunc(vm, pf);
    45 }
    59 }
    60 }
    74 }
    61 
    75 
    62 void Squirrel::PrintFunc(HSQUIRRELVM vm, const SQChar *s, ...)
    76 void Squirrel::PrintFunc(HSQUIRRELVM vm, const SQChar *s, ...)
    63 {
    77 {
    64 	va_list arglist;
    78 	va_list arglist;
       
    79 	char buf[1024];
       
    80 
    65 	va_start(arglist, s);
    81 	va_start(arglist, s);
    66 	scvprintf(s, arglist);
    82 	vsnprintf(buf, lengthof(buf) - 2, s, arglist);
    67 	va_end(arglist);
    83 	va_end(arglist);
    68 	printf("\n");
    84 	strcat(buf, "\n");
       
    85 
       
    86 	/* Check if we have a custom print function */
       
    87 	SQPrintFunc *func = ((Squirrel *)sq_getforeignptr(vm))->print_func;
       
    88 	if (func == NULL) printf("%s", buf);
       
    89 	else (*func)(false, buf);
    69 }
    90 }
    70 
    91 
    71 void Squirrel::AddMethod(const char *method_name, SQFUNCTION proc, uint nparam, const char *params, void *userdata, int size)
    92 void Squirrel::AddMethod(const char *method_name, SQFUNCTION proc, uint nparam, const char *params, void *userdata, int size)
    72 {
    93 {
    73 	sq_pushstring(this->vm, OTTD2FS(method_name), -1);
    94 	sq_pushstring(this->vm, OTTD2FS(method_name), -1);
   196 }
   217 }
   197 
   218 
   198 Squirrel::Squirrel()
   219 Squirrel::Squirrel()
   199 {
   220 {
   200 	this->vm = sq_open(1024);
   221 	this->vm = sq_open(1024);
       
   222 	this->print_func = NULL;
       
   223 	this->global_pointer = NULL;
   201 
   224 
   202 	/* Handle compile-errors ourself, so we can display it nicely */
   225 	/* Handle compile-errors ourself, so we can display it nicely */
   203 	sq_setcompilererrorhandler(this->vm, &Squirrel::CompileError);
   226 	sq_setcompilererrorhandler(this->vm, &Squirrel::CompileError);
   204 	sq_notifyallexceptions(this->vm, SQTrue);
   227 	sq_notifyallexceptions(this->vm, SQTrue);
   205 	/* Set a good print-function */
   228 	/* Set a good print-function */
   206 	sq_setprintfunc(this->vm, &Squirrel::PrintFunc);
   229 	sq_setprintfunc(this->vm, &Squirrel::PrintFunc);
   207 	/* Handle runtime-errors ourself, so we can display it nicely */
   230 	/* Handle runtime-errors ourself, so we can display it nicely */
   208 	sq_newclosure(this->vm, &Squirrel::_RunError, 0);
   231 	sq_newclosure(this->vm, &Squirrel::_RunError, 0);
   209 	sq_seterrorhandler(this->vm);
   232 	sq_seterrorhandler(this->vm);
   210 
   233 
       
   234 	/* Set the foreigh pointer, so we can always find this instance from within the VM */
       
   235 	sq_setforeignptr(this->vm, this);
       
   236 
   211 	sq_pushroottable(this->vm);
   237 	sq_pushroottable(this->vm);
   212 	squirrel_register_global_std(this);
   238 	squirrel_register_global_std(this);
   213 }
   239 }
   214 
   240 
   215 bool Squirrel::LoadScript(const char *script)
   241 bool Squirrel::LoadScript(const char *script)