1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 00:37:15 +01:00

Update squirrel to the latest version from the official repository.

Include the standard library that was filtered out by a mistake in the gitignore file.
This commit is contained in:
Sandu Liviu Catalin 2016-03-22 02:32:03 +02:00
parent 93dcf84263
commit a35643ae6e
46 changed files with 11417 additions and 8563 deletions

View File

@ -59,23 +59,23 @@ typedef SQInteger SQRESULT;
typedef wchar_t SQChar;
#define scstrcmp wcscmp
#ifdef _MSC_VER
#define scsprintf _snwprintf
#define scstrcmp wcscmp
#ifdef _WIN32
#define scsprintf _snwprintf
#else
#define scsprintf snwprintf
#define scsprintf swprintf
#endif
#define scstrlen wcslen
#define scstrtod wcstod
#define scstrlen wcslen
#define scstrtod wcstod
#ifdef _SQ64
#define scstrtol wcstoll
#define scstrtol wcstoll
#else
#define scstrtol wcstol
#define scstrtol wcstol
#endif
#define scstrtoul wcstoul
#define scvsprintf vswprintf
#define scstrstr wcsstr
#define scprintf wprintf
#define scstrtoul wcstoul
#define scvsprintf vswprintf
#define scstrstr wcsstr
#define scprintf wprintf
#ifdef _WIN32
#define WCHAR_SIZE 2
@ -90,13 +90,13 @@ typedef wchar_t SQChar;
#define _SC(a) L##a
#define scisspace iswspace
#define scisdigit iswdigit
#define scisprint iswprint
#define scisxdigit iswxdigit
#define scisalpha iswalpha
#define sciscntrl iswcntrl
#define scisalnum iswalnum
#define scisspace iswspace
#define scisdigit iswdigit
#define scisprint iswprint
#define scisxdigit iswxdigit
#define scisalpha iswalpha
#define sciscntrl iswcntrl
#define scisalnum iswalnum
#define sq_rsl(l) ((l)<<WCHAR_SHIFT_MUL)
@ -104,34 +104,34 @@ typedef wchar_t SQChar;
#else
typedef char SQChar;
#define _SC(a) a
#define scstrcmp strcmp
#define scstrcmp strcmp
#ifdef _MSC_VER
#define scsprintf _snprintf
#define scsprintf _snprintf
#else
#define scsprintf snprintf
#define scsprintf snprintf
#endif
#define scstrlen strlen
#define scstrtod strtod
#define scstrlen strlen
#define scstrtod strtod
#ifdef _SQ64
#ifdef _MSC_VER
#define scstrtol _strtoi64
#define scstrtol _strtoi64
#else
#define scstrtol strtoll
#define scstrtol strtoll
#endif
#else
#define scstrtol strtol
#define scstrtol strtol
#endif
#define scstrtoul strtoul
#define scvsprintf vsnprintf
#define scstrstr strstr
#define scisspace isspace
#define scisdigit isdigit
#define scisprint isprint
#define scisxdigit isxdigit
#define sciscntrl iscntrl
#define scisalpha isalpha
#define scisalnum isalnum
#define scprintf printf
#define scstrtoul strtoul
#define scvsprintf vsnprintf
#define scstrstr strstr
#define scisspace isspace
#define scisdigit isdigit
#define scisprint isprint
#define scisxdigit isxdigit
#define sciscntrl iscntrl
#define scisalpha isalpha
#define scisalnum isalnum
#define scprintf printf
#define MAX_CHAR 0xFF
#define sq_rsl(l) (l)

130
external/Squirrel/Lib/sqstdaux.cpp vendored Normal file
View File

@ -0,0 +1,130 @@
/* see copyright notice in squirrel.h */
#include <squirrel.h>
#include <sqstdaux.h>
#include <assert.h>
void sqstd_printcallstack(HSQUIRRELVM v)
{
SQPRINTFUNCTION pf = sq_geterrorfunc(v);
if(pf) {
SQStackInfos si;
SQInteger i;
SQFloat f;
const SQChar *s;
SQInteger level=1; //1 is to skip this function that is level 0
const SQChar *name=0;
SQInteger seq=0;
pf(v,_SC("\nCALLSTACK\n"));
while(SQ_SUCCEEDED(sq_stackinfos(v,level,&si)))
{
const SQChar *fn=_SC("unknown");
const SQChar *src=_SC("unknown");
if(si.funcname)fn=si.funcname;
if(si.source)src=si.source;
pf(v,_SC("*FUNCTION [%s()] %s line [%d]\n"),fn,src,si.line);
level++;
}
level=0;
pf(v,_SC("\nLOCALS\n"));
for(level=0;level<10;level++){
seq=0;
while((name = sq_getlocal(v,level,seq)))
{
seq++;
switch(sq_gettype(v,-1))
{
case OT_NULL:
pf(v,_SC("[%s] NULL\n"),name);
break;
case OT_INTEGER:
sq_getinteger(v,-1,&i);
pf(v,_SC("[%s] %d\n"),name,i);
break;
case OT_FLOAT:
sq_getfloat(v,-1,&f);
pf(v,_SC("[%s] %.14g\n"),name,f);
break;
case OT_USERPOINTER:
pf(v,_SC("[%s] USERPOINTER\n"),name);
break;
case OT_STRING:
sq_getstring(v,-1,&s);
pf(v,_SC("[%s] \"%s\"\n"),name,s);
break;
case OT_TABLE:
pf(v,_SC("[%s] TABLE\n"),name);
break;
case OT_ARRAY:
pf(v,_SC("[%s] ARRAY\n"),name);
break;
case OT_CLOSURE:
pf(v,_SC("[%s] CLOSURE\n"),name);
break;
case OT_NATIVECLOSURE:
pf(v,_SC("[%s] NATIVECLOSURE\n"),name);
break;
case OT_GENERATOR:
pf(v,_SC("[%s] GENERATOR\n"),name);
break;
case OT_USERDATA:
pf(v,_SC("[%s] USERDATA\n"),name);
break;
case OT_THREAD:
pf(v,_SC("[%s] THREAD\n"),name);
break;
case OT_CLASS:
pf(v,_SC("[%s] CLASS\n"),name);
break;
case OT_INSTANCE:
pf(v,_SC("[%s] INSTANCE\n"),name);
break;
case OT_WEAKREF:
pf(v,_SC("[%s] WEAKREF\n"),name);
break;
case OT_BOOL:{
SQBool bval;
sq_getbool(v,-1,&bval);
pf(v,_SC("[%s] %s\n"),name,bval == SQTrue ? _SC("true"):_SC("false"));
}
break;
default: assert(0); break;
}
sq_pop(v,1);
}
}
}
}
static SQInteger _sqstd_aux_printerror(HSQUIRRELVM v)
{
SQPRINTFUNCTION pf = sq_geterrorfunc(v);
if(pf) {
const SQChar *sErr = 0;
if(sq_gettop(v)>=1) {
if(SQ_SUCCEEDED(sq_getstring(v,2,&sErr))) {
pf(v,_SC("\nAN ERROR HAS OCCURED [%s]\n"),sErr);
}
else{
pf(v,_SC("\nAN ERROR HAS OCCURED [unknown]\n"));
}
sqstd_printcallstack(v);
}
}
return 0;
}
void _sqstd_compiler_error(HSQUIRRELVM v,const SQChar *sErr,const SQChar *sSource,SQInteger line,SQInteger column)
{
SQPRINTFUNCTION pf = sq_geterrorfunc(v);
if(pf) {
pf(v,_SC("%s line = (%d) column = (%d) : error %s\n"),sSource,line,column,sErr);
}
}
void sqstd_seterrorhandlers(HSQUIRRELVM v)
{
sq_setcompilererrorhandler(v,_sqstd_compiler_error);
sq_newclosure(v,_sqstd_aux_printerror,0);
sq_seterrorhandler(v);
}

277
external/Squirrel/Lib/sqstdblob.cpp vendored Normal file
View File

@ -0,0 +1,277 @@
/* see copyright notice in squirrel.h */
#include <new>
#include <squirrel.h>
#include <sqstdio.h>
#include <string.h>
#include <sqstdblob.h>
#include "sqstdstream.h"
#include "sqstdblobimpl.h"
#define SQSTD_BLOB_TYPE_TAG (SQSTD_STREAM_TYPE_TAG | 0x00000002)
//Blob
#define SETUP_BLOB(v) \
SQBlob *self = NULL; \
{ if(SQ_FAILED(sq_getinstanceup(v,1,(SQUserPointer*)&self,(SQUserPointer)SQSTD_BLOB_TYPE_TAG))) \
return sq_throwerror(v,_SC("invalid type tag")); } \
if(!self || !self->IsValid()) \
return sq_throwerror(v,_SC("the blob is invalid"));
static SQInteger _blob_resize(HSQUIRRELVM v)
{
SETUP_BLOB(v);
SQInteger size;
sq_getinteger(v,2,&size);
if(!self->Resize(size))
return sq_throwerror(v,_SC("resize failed"));
return 0;
}
static void __swap_dword(unsigned int *n)
{
*n=(unsigned int)(((*n&0xFF000000)>>24) |
((*n&0x00FF0000)>>8) |
((*n&0x0000FF00)<<8) |
((*n&0x000000FF)<<24));
}
static void __swap_word(unsigned short *n)
{
*n=(unsigned short)((*n>>8)&0x00FF)| ((*n<<8)&0xFF00);
}
static SQInteger _blob_swap4(HSQUIRRELVM v)
{
SETUP_BLOB(v);
SQInteger num=(self->Len()-(self->Len()%4))>>2;
unsigned int *t=(unsigned int *)self->GetBuf();
for(SQInteger i = 0; i < num; i++) {
__swap_dword(&t[i]);
}
return 0;
}
static SQInteger _blob_swap2(HSQUIRRELVM v)
{
SETUP_BLOB(v);
SQInteger num=(self->Len()-(self->Len()%2))>>1;
unsigned short *t = (unsigned short *)self->GetBuf();
for(SQInteger i = 0; i < num; i++) {
__swap_word(&t[i]);
}
return 0;
}
static SQInteger _blob__set(HSQUIRRELVM v)
{
SETUP_BLOB(v);
SQInteger idx,val;
sq_getinteger(v,2,&idx);
sq_getinteger(v,3,&val);
if(idx < 0 || idx >= self->Len())
return sq_throwerror(v,_SC("index out of range"));
((unsigned char *)self->GetBuf())[idx] = (unsigned char) val;
sq_push(v,3);
return 1;
}
static SQInteger _blob__get(HSQUIRRELVM v)
{
SETUP_BLOB(v);
SQInteger idx;
sq_getinteger(v,2,&idx);
if(idx < 0 || idx >= self->Len())
return sq_throwerror(v,_SC("index out of range"));
sq_pushinteger(v,((unsigned char *)self->GetBuf())[idx]);
return 1;
}
static SQInteger _blob__nexti(HSQUIRRELVM v)
{
SETUP_BLOB(v);
if(sq_gettype(v,2) == OT_NULL) {
sq_pushinteger(v, 0);
return 1;
}
SQInteger idx;
if(SQ_SUCCEEDED(sq_getinteger(v, 2, &idx))) {
if(idx+1 < self->Len()) {
sq_pushinteger(v, idx+1);
return 1;
}
sq_pushnull(v);
return 1;
}
return sq_throwerror(v,_SC("internal error (_nexti) wrong argument type"));
}
static SQInteger _blob__typeof(HSQUIRRELVM v)
{
sq_pushstring(v,_SC("blob"),-1);
return 1;
}
static SQInteger _blob_releasehook(SQUserPointer p, SQInteger SQ_UNUSED_ARG(size))
{
SQBlob *self = (SQBlob*)p;
self->~SQBlob();
sq_free(self,sizeof(SQBlob));
return 1;
}
static SQInteger _blob_constructor(HSQUIRRELVM v)
{
SQInteger nparam = sq_gettop(v);
SQInteger size = 0;
if(nparam == 2) {
sq_getinteger(v, 2, &size);
}
if(size < 0) return sq_throwerror(v, _SC("cannot create blob with negative size"));
//SQBlob *b = new SQBlob(size);
SQBlob *b = new (sq_malloc(sizeof(SQBlob)))SQBlob(size);
if(SQ_FAILED(sq_setinstanceup(v,1,b))) {
b->~SQBlob();
sq_free(b,sizeof(SQBlob));
return sq_throwerror(v, _SC("cannot create blob"));
}
sq_setreleasehook(v,1,_blob_releasehook);
return 0;
}
static SQInteger _blob__cloned(HSQUIRRELVM v)
{
SQBlob *other = NULL;
{
if(SQ_FAILED(sq_getinstanceup(v,2,(SQUserPointer*)&other,(SQUserPointer)SQSTD_BLOB_TYPE_TAG)))
return SQ_ERROR;
}
//SQBlob *thisone = new SQBlob(other->Len());
SQBlob *thisone = new (sq_malloc(sizeof(SQBlob)))SQBlob(other->Len());
memcpy(thisone->GetBuf(),other->GetBuf(),thisone->Len());
if(SQ_FAILED(sq_setinstanceup(v,1,thisone))) {
thisone->~SQBlob();
sq_free(thisone,sizeof(SQBlob));
return sq_throwerror(v, _SC("cannot clone blob"));
}
sq_setreleasehook(v,1,_blob_releasehook);
return 0;
}
#define _DECL_BLOB_FUNC(name,nparams,typecheck) {_SC(#name),_blob_##name,nparams,typecheck}
static const SQRegFunction _blob_methods[] = {
_DECL_BLOB_FUNC(constructor,-1,_SC("xn")),
_DECL_BLOB_FUNC(resize,2,_SC("xn")),
_DECL_BLOB_FUNC(swap2,1,_SC("x")),
_DECL_BLOB_FUNC(swap4,1,_SC("x")),
_DECL_BLOB_FUNC(_set,3,_SC("xnn")),
_DECL_BLOB_FUNC(_get,2,_SC("xn")),
_DECL_BLOB_FUNC(_typeof,1,_SC("x")),
_DECL_BLOB_FUNC(_nexti,2,_SC("x")),
_DECL_BLOB_FUNC(_cloned,2,_SC("xx")),
{NULL,(SQFUNCTION)0,0,NULL}
};
//GLOBAL FUNCTIONS
static SQInteger _g_blob_casti2f(HSQUIRRELVM v)
{
SQInteger i;
sq_getinteger(v,2,&i);
sq_pushfloat(v,*((const SQFloat *)&i));
return 1;
}
static SQInteger _g_blob_castf2i(HSQUIRRELVM v)
{
SQFloat f;
sq_getfloat(v,2,&f);
sq_pushinteger(v,*((const SQInteger *)&f));
return 1;
}
static SQInteger _g_blob_swap2(HSQUIRRELVM v)
{
SQInteger i;
sq_getinteger(v,2,&i);
short s=(short)i;
sq_pushinteger(v,(s<<8)|((s>>8)&0x00FF));
return 1;
}
static SQInteger _g_blob_swap4(HSQUIRRELVM v)
{
SQInteger i;
sq_getinteger(v,2,&i);
unsigned int t4 = (unsigned int)i;
__swap_dword(&t4);
sq_pushinteger(v,(SQInteger)t4);
return 1;
}
static SQInteger _g_blob_swapfloat(HSQUIRRELVM v)
{
SQFloat f;
sq_getfloat(v,2,&f);
__swap_dword((unsigned int *)&f);
sq_pushfloat(v,f);
return 1;
}
#define _DECL_GLOBALBLOB_FUNC(name,nparams,typecheck) {_SC(#name),_g_blob_##name,nparams,typecheck}
static const SQRegFunction bloblib_funcs[]={
_DECL_GLOBALBLOB_FUNC(casti2f,2,_SC(".n")),
_DECL_GLOBALBLOB_FUNC(castf2i,2,_SC(".n")),
_DECL_GLOBALBLOB_FUNC(swap2,2,_SC(".n")),
_DECL_GLOBALBLOB_FUNC(swap4,2,_SC(".n")),
_DECL_GLOBALBLOB_FUNC(swapfloat,2,_SC(".n")),
{NULL,(SQFUNCTION)0,0,NULL}
};
SQRESULT sqstd_getblob(HSQUIRRELVM v,SQInteger idx,SQUserPointer *ptr)
{
SQBlob *blob;
if(SQ_FAILED(sq_getinstanceup(v,idx,(SQUserPointer *)&blob,(SQUserPointer)SQSTD_BLOB_TYPE_TAG)))
return -1;
*ptr = blob->GetBuf();
return SQ_OK;
}
SQInteger sqstd_getblobsize(HSQUIRRELVM v,SQInteger idx)
{
SQBlob *blob;
if(SQ_FAILED(sq_getinstanceup(v,idx,(SQUserPointer *)&blob,(SQUserPointer)SQSTD_BLOB_TYPE_TAG)))
return -1;
return blob->Len();
}
SQUserPointer sqstd_createblob(HSQUIRRELVM v, SQInteger size)
{
SQInteger top = sq_gettop(v);
sq_pushregistrytable(v);
sq_pushstring(v,_SC("std_blob"),-1);
if(SQ_SUCCEEDED(sq_get(v,-2))) {
sq_remove(v,-2); //removes the registry
sq_push(v,1); // push the this
sq_pushinteger(v,size); //size
SQBlob *blob = NULL;
if(SQ_SUCCEEDED(sq_call(v,2,SQTrue,SQFalse))
&& SQ_SUCCEEDED(sq_getinstanceup(v,-1,(SQUserPointer *)&blob,(SQUserPointer)SQSTD_BLOB_TYPE_TAG))) {
sq_remove(v,-2);
return blob->GetBuf();
}
}
sq_settop(v,top);
return NULL;
}
SQRESULT sqstd_register_bloblib(HSQUIRRELVM v)
{
return declare_stream(v,_SC("blob"),(SQUserPointer)SQSTD_BLOB_TYPE_TAG,_SC("std_blob"),_blob_methods,bloblib_funcs);
}

108
external/Squirrel/Lib/sqstdblobimpl.h vendored Normal file
View File

@ -0,0 +1,108 @@
/* see copyright notice in squirrel.h */
#ifndef _SQSTD_BLOBIMPL_H_
#define _SQSTD_BLOBIMPL_H_
struct SQBlob : public SQStream
{
SQBlob(SQInteger size) {
_size = size;
_allocated = size;
_buf = (unsigned char *)sq_malloc(size);
memset(_buf, 0, _size);
_ptr = 0;
_owns = true;
}
virtual ~SQBlob() {
sq_free(_buf, _allocated);
}
SQInteger Write(void *buffer, SQInteger size) {
if(!CanAdvance(size)) {
GrowBufOf(_ptr + size - _size);
}
memcpy(&_buf[_ptr], buffer, size);
_ptr += size;
return size;
}
SQInteger Read(void *buffer,SQInteger size) {
SQInteger n = size;
if(!CanAdvance(size)) {
if((_size - _ptr) > 0)
n = _size - _ptr;
else return 0;
}
memcpy(buffer, &_buf[_ptr], n);
_ptr += n;
return n;
}
bool Resize(SQInteger n) {
if(!_owns) return false;
if(n != _allocated) {
unsigned char *newbuf = (unsigned char *)sq_malloc(n);
memset(newbuf,0,n);
if(_size > n)
memcpy(newbuf,_buf,n);
else
memcpy(newbuf,_buf,_size);
sq_free(_buf,_allocated);
_buf=newbuf;
_allocated = n;
if(_size > _allocated)
_size = _allocated;
if(_ptr > _allocated)
_ptr = _allocated;
}
return true;
}
bool GrowBufOf(SQInteger n)
{
bool ret = true;
if(_size + n > _allocated) {
if(_size + n > _size * 2)
ret = Resize(_size + n);
else
ret = Resize(_size * 2);
}
_size = _size + n;
return ret;
}
bool CanAdvance(SQInteger n) {
if(_ptr+n>_size)return false;
return true;
}
SQInteger Seek(SQInteger offset, SQInteger origin) {
switch(origin) {
case SQ_SEEK_SET:
if(offset > _size || offset < 0) return -1;
_ptr = offset;
break;
case SQ_SEEK_CUR:
if(_ptr + offset > _size || _ptr + offset < 0) return -1;
_ptr += offset;
break;
case SQ_SEEK_END:
if(_size + offset > _size || _size + offset < 0) return -1;
_ptr = _size + offset;
break;
default: return -1;
}
return 0;
}
bool IsValid() {
return _buf?true:false;
}
bool EOS() {
return _ptr == _size;
}
SQInteger Flush() { return 0; }
SQInteger Tell() { return _ptr; }
SQInteger Len() { return _size; }
SQUserPointer GetBuf(){ return _buf; }
private:
SQInteger _size;
SQInteger _allocated;
SQInteger _ptr;
unsigned char *_buf;
bool _owns;
};
#endif //_SQSTD_BLOBIMPL_H_

485
external/Squirrel/Lib/sqstdio.cpp vendored Normal file
View File

@ -0,0 +1,485 @@
/* see copyright notice in squirrel.h */
#include <new>
#include <stdio.h>
#include <squirrel.h>
#include <sqstdio.h>
#include "sqstdstream.h"
#define SQSTD_FILE_TYPE_TAG (SQSTD_STREAM_TYPE_TAG | 0x00000001)
//basic API
SQFILE sqstd_fopen(const SQChar *filename ,const SQChar *mode)
{
#ifndef SQUNICODE
return (SQFILE)fopen(filename,mode);
#else
return (SQFILE)_wfopen(filename,mode);
#endif
}
SQInteger sqstd_fread(void* buffer, SQInteger size, SQInteger count, SQFILE file)
{
SQInteger ret = (SQInteger)fread(buffer,size,count,(FILE *)file);
return ret;
}
SQInteger sqstd_fwrite(const SQUserPointer buffer, SQInteger size, SQInteger count, SQFILE file)
{
return (SQInteger)fwrite(buffer,size,count,(FILE *)file);
}
SQInteger sqstd_fseek(SQFILE file, SQInteger offset, SQInteger origin)
{
SQInteger realorigin;
switch(origin) {
case SQ_SEEK_CUR: realorigin = SEEK_CUR; break;
case SQ_SEEK_END: realorigin = SEEK_END; break;
case SQ_SEEK_SET: realorigin = SEEK_SET; break;
default: return -1; //failed
}
return fseek((FILE *)file,(long)offset,(int)realorigin);
}
SQInteger sqstd_ftell(SQFILE file)
{
return ftell((FILE *)file);
}
SQInteger sqstd_fflush(SQFILE file)
{
return fflush((FILE *)file);
}
SQInteger sqstd_fclose(SQFILE file)
{
return fclose((FILE *)file);
}
SQInteger sqstd_feof(SQFILE file)
{
return feof((FILE *)file);
}
//File
struct SQFile : public SQStream {
SQFile() { _handle = NULL; _owns = false;}
SQFile(SQFILE file, bool owns) { _handle = file; _owns = owns;}
virtual ~SQFile() { Close(); }
bool Open(const SQChar *filename ,const SQChar *mode) {
Close();
if( (_handle = sqstd_fopen(filename,mode)) ) {
_owns = true;
return true;
}
return false;
}
void Close() {
if(_handle && _owns) {
sqstd_fclose(_handle);
_handle = NULL;
_owns = false;
}
}
SQInteger Read(void *buffer,SQInteger size) {
return sqstd_fread(buffer,1,size,_handle);
}
SQInteger Write(void *buffer,SQInteger size) {
return sqstd_fwrite(buffer,1,size,_handle);
}
SQInteger Flush() {
return sqstd_fflush(_handle);
}
SQInteger Tell() {
return sqstd_ftell(_handle);
}
SQInteger Len() {
SQInteger prevpos=Tell();
Seek(0,SQ_SEEK_END);
SQInteger size=Tell();
Seek(prevpos,SQ_SEEK_SET);
return size;
}
SQInteger Seek(SQInteger offset, SQInteger origin) {
return sqstd_fseek(_handle,offset,origin);
}
bool IsValid() { return _handle?true:false; }
bool EOS() { return Tell()==Len()?true:false;}
SQFILE GetHandle() {return _handle;}
private:
SQFILE _handle;
bool _owns;
};
static SQInteger _file__typeof(HSQUIRRELVM v)
{
sq_pushstring(v,_SC("file"),-1);
return 1;
}
static SQInteger _file_releasehook(SQUserPointer p, SQInteger SQ_UNUSED_ARG(size))
{
SQFile *self = (SQFile*)p;
self->~SQFile();
sq_free(self,sizeof(SQFile));
return 1;
}
static SQInteger _file_constructor(HSQUIRRELVM v)
{
const SQChar *filename,*mode;
bool owns = true;
SQFile *f;
SQFILE newf;
if(sq_gettype(v,2) == OT_STRING && sq_gettype(v,3) == OT_STRING) {
sq_getstring(v, 2, &filename);
sq_getstring(v, 3, &mode);
newf = sqstd_fopen(filename, mode);
if(!newf) return sq_throwerror(v, _SC("cannot open file"));
} else if(sq_gettype(v,2) == OT_USERPOINTER) {
owns = !(sq_gettype(v,3) == OT_NULL);
sq_getuserpointer(v,2,&newf);
} else {
return sq_throwerror(v,_SC("wrong parameter"));
}
f = new (sq_malloc(sizeof(SQFile)))SQFile(newf,owns);
if(SQ_FAILED(sq_setinstanceup(v,1,f))) {
f->~SQFile();
sq_free(f,sizeof(SQFile));
return sq_throwerror(v, _SC("cannot create blob with negative size"));
}
sq_setreleasehook(v,1,_file_releasehook);
return 0;
}
static SQInteger _file_close(HSQUIRRELVM v)
{
SQFile *self = NULL;
if(SQ_SUCCEEDED(sq_getinstanceup(v,1,(SQUserPointer*)&self,(SQUserPointer)SQSTD_FILE_TYPE_TAG))
&& self != NULL)
{
self->Close();
}
return 0;
}
//bindings
#define _DECL_FILE_FUNC(name,nparams,typecheck) {_SC(#name),_file_##name,nparams,typecheck}
static const SQRegFunction _file_methods[] = {
_DECL_FILE_FUNC(constructor,3,_SC("x")),
_DECL_FILE_FUNC(_typeof,1,_SC("x")),
_DECL_FILE_FUNC(close,1,_SC("x")),
{NULL,(SQFUNCTION)0,0,NULL}
};
SQRESULT sqstd_createfile(HSQUIRRELVM v, SQFILE file,SQBool own)
{
SQInteger top = sq_gettop(v);
sq_pushregistrytable(v);
sq_pushstring(v,_SC("std_file"),-1);
if(SQ_SUCCEEDED(sq_get(v,-2))) {
sq_remove(v,-2); //removes the registry
sq_pushroottable(v); // push the this
sq_pushuserpointer(v,file); //file
if(own){
sq_pushinteger(v,1); //true
}
else{
sq_pushnull(v); //false
}
if(SQ_SUCCEEDED( sq_call(v,3,SQTrue,SQFalse) )) {
sq_remove(v,-2);
return SQ_OK;
}
}
sq_settop(v,top);
return SQ_ERROR;
}
SQRESULT sqstd_getfile(HSQUIRRELVM v, SQInteger idx, SQFILE *file)
{
SQFile *fileobj = NULL;
if(SQ_SUCCEEDED(sq_getinstanceup(v,idx,(SQUserPointer*)&fileobj,(SQUserPointer)SQSTD_FILE_TYPE_TAG))) {
*file = fileobj->GetHandle();
return SQ_OK;
}
return sq_throwerror(v,_SC("not a file"));
}
#define IO_BUFFER_SIZE 2048
struct IOBuffer {
unsigned char buffer[IO_BUFFER_SIZE];
SQInteger size;
SQInteger ptr;
SQFILE file;
};
SQInteger _read_byte(IOBuffer *iobuffer)
{
if(iobuffer->ptr < iobuffer->size) {
SQInteger ret = iobuffer->buffer[iobuffer->ptr];
iobuffer->ptr++;
return ret;
}
else {
if( (iobuffer->size = sqstd_fread(iobuffer->buffer,1,IO_BUFFER_SIZE,iobuffer->file )) > 0 )
{
SQInteger ret = iobuffer->buffer[0];
iobuffer->ptr = 1;
return ret;
}
}
return 0;
}
SQInteger _read_two_bytes(IOBuffer *iobuffer)
{
if(iobuffer->ptr < iobuffer->size) {
if(iobuffer->size < 2) return 0;
SQInteger ret = *((const wchar_t*)&iobuffer->buffer[iobuffer->ptr]);
iobuffer->ptr += 2;
return ret;
}
else {
if( (iobuffer->size = sqstd_fread(iobuffer->buffer,1,IO_BUFFER_SIZE,iobuffer->file )) > 0 )
{
if(iobuffer->size < 2) return 0;
SQInteger ret = *((const wchar_t*)&iobuffer->buffer[0]);
iobuffer->ptr = 2;
return ret;
}
}
return 0;
}
static SQInteger _io_file_lexfeed_PLAIN(SQUserPointer iobuf)
{
IOBuffer *iobuffer = (IOBuffer *)iobuf;
return _read_byte(iobuffer);
}
#ifdef SQUNICODE
static SQInteger _io_file_lexfeed_UTF8(SQUserPointer iobuf)
{
IOBuffer *iobuffer = (IOBuffer *)iobuf;
#define READ(iobuf) \
if((inchar = (unsigned char)_read_byte(iobuf)) == 0) \
return 0;
static const SQInteger utf8_lengths[16] =
{
1,1,1,1,1,1,1,1, /* 0000 to 0111 : 1 byte (plain ASCII) */
0,0,0,0, /* 1000 to 1011 : not valid */
2,2, /* 1100, 1101 : 2 bytes */
3, /* 1110 : 3 bytes */
4 /* 1111 :4 bytes */
};
static const unsigned char byte_masks[5] = {0,0,0x1f,0x0f,0x07};
unsigned char inchar;
SQInteger c = 0;
READ(iobuffer);
c = inchar;
//
if(c >= 0x80) {
SQInteger tmp;
SQInteger codelen = utf8_lengths[c>>4];
if(codelen == 0)
return 0;
//"invalid UTF-8 stream";
tmp = c&byte_masks[codelen];
for(SQInteger n = 0; n < codelen-1; n++) {
tmp<<=6;
READ(iobuffer);
tmp |= inchar & 0x3F;
}
c = tmp;
}
return c;
}
#endif
static SQInteger _io_file_lexfeed_UCS2_LE(SQUserPointer iobuf)
{
SQInteger ret;
IOBuffer *iobuffer = (IOBuffer *)iobuf;
if( (ret = _read_two_bytes(iobuffer)) > 0 )
return ret;
return 0;
}
static SQInteger _io_file_lexfeed_UCS2_BE(SQUserPointer iobuf)
{
SQInteger c;
IOBuffer *iobuffer = (IOBuffer *)iobuf;
if( (c = _read_two_bytes(iobuffer)) > 0 ) {
c = ((c>>8)&0x00FF)| ((c<<8)&0xFF00);
return c;
}
return 0;
}
SQInteger file_read(SQUserPointer file,SQUserPointer buf,SQInteger size)
{
SQInteger ret;
if( ( ret = sqstd_fread(buf,1,size,(SQFILE)file ))!=0 )return ret;
return -1;
}
SQInteger file_write(SQUserPointer file,SQUserPointer p,SQInteger size)
{
return sqstd_fwrite(p,1,size,(SQFILE)file);
}
SQRESULT sqstd_loadfile(HSQUIRRELVM v,const SQChar *filename,SQBool printerror)
{
SQFILE file = sqstd_fopen(filename,_SC("rb"));
SQInteger ret;
unsigned short us;
unsigned char uc;
SQLEXREADFUNC func = _io_file_lexfeed_PLAIN;
if(file){
ret = sqstd_fread(&us,1,2,file);
if(ret != 2) {
//probably an empty file
us = 0;
}
if(us == SQ_BYTECODE_STREAM_TAG) { //BYTECODE
sqstd_fseek(file,0,SQ_SEEK_SET);
if(SQ_SUCCEEDED(sq_readclosure(v,file_read,file))) {
sqstd_fclose(file);
return SQ_OK;
}
}
else { //SCRIPT
switch(us)
{
//gotta swap the next 2 lines on BIG endian machines
case 0xFFFE: func = _io_file_lexfeed_UCS2_BE; break;//UTF-16 little endian;
case 0xFEFF: func = _io_file_lexfeed_UCS2_LE; break;//UTF-16 big endian;
case 0xBBEF:
if(sqstd_fread(&uc,1,sizeof(uc),file) == 0) {
sqstd_fclose(file);
return sq_throwerror(v,_SC("io error"));
}
if(uc != 0xBF) {
sqstd_fclose(file);
return sq_throwerror(v,_SC("Unrecognozed ecoding"));
}
#ifdef SQUNICODE
func = _io_file_lexfeed_UTF8;
#else
func = _io_file_lexfeed_PLAIN;
#endif
break;//UTF-8 ;
default: sqstd_fseek(file,0,SQ_SEEK_SET); break; // ascii
}
IOBuffer buffer;
buffer.ptr = 0;
buffer.size = 0;
buffer.file = file;
if(SQ_SUCCEEDED(sq_compile(v,func,&buffer,filename,printerror))){
sqstd_fclose(file);
return SQ_OK;
}
}
sqstd_fclose(file);
return SQ_ERROR;
}
return sq_throwerror(v,_SC("cannot open the file"));
}
SQRESULT sqstd_dofile(HSQUIRRELVM v,const SQChar *filename,SQBool retval,SQBool printerror)
{
if(SQ_SUCCEEDED(sqstd_loadfile(v,filename,printerror))) {
sq_push(v,-2);
if(SQ_SUCCEEDED(sq_call(v,1,retval,SQTrue))) {
sq_remove(v,retval?-2:-1); //removes the closure
return 1;
}
sq_pop(v,1); //removes the closure
}
return SQ_ERROR;
}
SQRESULT sqstd_writeclosuretofile(HSQUIRRELVM v,const SQChar *filename)
{
SQFILE file = sqstd_fopen(filename,_SC("wb+"));
if(!file) return sq_throwerror(v,_SC("cannot open the file"));
if(SQ_SUCCEEDED(sq_writeclosure(v,file_write,file))) {
sqstd_fclose(file);
return SQ_OK;
}
sqstd_fclose(file);
return SQ_ERROR; //forward the error
}
SQInteger _g_io_loadfile(HSQUIRRELVM v)
{
const SQChar *filename;
SQBool printerror = SQFalse;
sq_getstring(v,2,&filename);
if(sq_gettop(v) >= 3) {
sq_getbool(v,3,&printerror);
}
if(SQ_SUCCEEDED(sqstd_loadfile(v,filename,printerror)))
return 1;
return SQ_ERROR; //propagates the error
}
SQInteger _g_io_writeclosuretofile(HSQUIRRELVM v)
{
const SQChar *filename;
sq_getstring(v,2,&filename);
if(SQ_SUCCEEDED(sqstd_writeclosuretofile(v,filename)))
return 1;
return SQ_ERROR; //propagates the error
}
SQInteger _g_io_dofile(HSQUIRRELVM v)
{
const SQChar *filename;
SQBool printerror = SQFalse;
sq_getstring(v,2,&filename);
if(sq_gettop(v) >= 3) {
sq_getbool(v,3,&printerror);
}
sq_push(v,1); //repush the this
if(SQ_SUCCEEDED(sqstd_dofile(v,filename,SQTrue,printerror)))
return 1;
return SQ_ERROR; //propagates the error
}
#define _DECL_GLOBALIO_FUNC(name,nparams,typecheck) {_SC(#name),_g_io_##name,nparams,typecheck}
static const SQRegFunction iolib_funcs[]={
_DECL_GLOBALIO_FUNC(loadfile,-2,_SC(".sb")),
_DECL_GLOBALIO_FUNC(dofile,-2,_SC(".sb")),
_DECL_GLOBALIO_FUNC(writeclosuretofile,3,_SC(".sc")),
{NULL,(SQFUNCTION)0,0,NULL}
};
SQRESULT sqstd_register_iolib(HSQUIRRELVM v)
{
SQInteger top = sq_gettop(v);
//create delegate
declare_stream(v,_SC("file"),(SQUserPointer)SQSTD_FILE_TYPE_TAG,_SC("std_file"),_file_methods,iolib_funcs);
sq_pushstring(v,_SC("stdout"),-1);
sqstd_createfile(v,stdout,SQFalse);
sq_newslot(v,-3,SQFalse);
sq_pushstring(v,_SC("stdin"),-1);
sqstd_createfile(v,stdin,SQFalse);
sq_newslot(v,-3,SQFalse);
sq_pushstring(v,_SC("stderr"),-1);
sqstd_createfile(v,stderr,SQFalse);
sq_newslot(v,-3,SQFalse);
sq_settop(v,top);
return SQ_OK;
}

107
external/Squirrel/Lib/sqstdmath.cpp vendored Normal file
View File

@ -0,0 +1,107 @@
/* see copyright notice in squirrel.h */
#include <squirrel.h>
#include <math.h>
#include <stdlib.h>
#include <sqstdmath.h>
#define SINGLE_ARG_FUNC(_funcname) static SQInteger math_##_funcname(HSQUIRRELVM v){ \
SQFloat f; \
sq_getfloat(v,2,&f); \
sq_pushfloat(v,(SQFloat)_funcname(f)); \
return 1; \
}
#define TWO_ARGS_FUNC(_funcname) static SQInteger math_##_funcname(HSQUIRRELVM v){ \
SQFloat p1,p2; \
sq_getfloat(v,2,&p1); \
sq_getfloat(v,3,&p2); \
sq_pushfloat(v,(SQFloat)_funcname(p1,p2)); \
return 1; \
}
static SQInteger math_srand(HSQUIRRELVM v)
{
SQInteger i;
if(SQ_FAILED(sq_getinteger(v,2,&i)))
return sq_throwerror(v,_SC("invalid param"));
srand((unsigned int)i);
return 0;
}
static SQInteger math_rand(HSQUIRRELVM v)
{
sq_pushinteger(v,rand());
return 1;
}
static SQInteger math_abs(HSQUIRRELVM v)
{
SQInteger n;
sq_getinteger(v,2,&n);
sq_pushinteger(v,(SQInteger)abs((int)n));
return 1;
}
SINGLE_ARG_FUNC(sqrt)
SINGLE_ARG_FUNC(fabs)
SINGLE_ARG_FUNC(sin)
SINGLE_ARG_FUNC(cos)
SINGLE_ARG_FUNC(asin)
SINGLE_ARG_FUNC(acos)
SINGLE_ARG_FUNC(log)
SINGLE_ARG_FUNC(log10)
SINGLE_ARG_FUNC(tan)
SINGLE_ARG_FUNC(atan)
TWO_ARGS_FUNC(atan2)
TWO_ARGS_FUNC(pow)
SINGLE_ARG_FUNC(floor)
SINGLE_ARG_FUNC(ceil)
SINGLE_ARG_FUNC(exp)
#define _DECL_FUNC(name,nparams,tycheck) {_SC(#name),math_##name,nparams,tycheck}
static const SQRegFunction mathlib_funcs[] = {
_DECL_FUNC(sqrt,2,_SC(".n")),
_DECL_FUNC(sin,2,_SC(".n")),
_DECL_FUNC(cos,2,_SC(".n")),
_DECL_FUNC(asin,2,_SC(".n")),
_DECL_FUNC(acos,2,_SC(".n")),
_DECL_FUNC(log,2,_SC(".n")),
_DECL_FUNC(log10,2,_SC(".n")),
_DECL_FUNC(tan,2,_SC(".n")),
_DECL_FUNC(atan,2,_SC(".n")),
_DECL_FUNC(atan2,3,_SC(".nn")),
_DECL_FUNC(pow,3,_SC(".nn")),
_DECL_FUNC(floor,2,_SC(".n")),
_DECL_FUNC(ceil,2,_SC(".n")),
_DECL_FUNC(exp,2,_SC(".n")),
_DECL_FUNC(srand,2,_SC(".n")),
_DECL_FUNC(rand,1,NULL),
_DECL_FUNC(fabs,2,_SC(".n")),
_DECL_FUNC(abs,2,_SC(".n")),
{NULL,(SQFUNCTION)0,0,NULL}
};
#undef _DECL_FUNC
#ifndef M_PI
#define M_PI (3.14159265358979323846)
#endif
SQRESULT sqstd_register_mathlib(HSQUIRRELVM v)
{
SQInteger i=0;
while(mathlib_funcs[i].name!=0) {
sq_pushstring(v,mathlib_funcs[i].name,-1);
sq_newclosure(v,mathlib_funcs[i].f,0);
sq_setparamscheck(v,mathlib_funcs[i].nparamscheck,mathlib_funcs[i].typemask);
sq_setnativeclosurename(v,-1,mathlib_funcs[i].name);
sq_newslot(v,-3,SQFalse);
i++;
}
sq_pushstring(v,_SC("RAND_MAX"),-1);
sq_pushinteger(v,RAND_MAX);
sq_newslot(v,-3,SQFalse);
sq_pushstring(v,_SC("PI"),-1);
sq_pushfloat(v,(SQFloat)M_PI);
sq_newslot(v,-3,SQFalse);
return SQ_OK;
}

663
external/Squirrel/Lib/sqstdrex.cpp vendored Normal file
View File

@ -0,0 +1,663 @@
/* see copyright notice in squirrel.h */
#include <squirrel.h>
#include <string.h>
#include <ctype.h>
#include <setjmp.h>
#include <sqstdstring.h>
#ifdef _DEBUG
#include <stdio.h>
static const SQChar *g_nnames[] =
{
_SC("NONE"),_SC("OP_GREEDY"), _SC("OP_OR"),
_SC("OP_EXPR"),_SC("OP_NOCAPEXPR"),_SC("OP_DOT"), _SC("OP_CLASS"),
_SC("OP_CCLASS"),_SC("OP_NCLASS"),_SC("OP_RANGE"),_SC("OP_CHAR"),
_SC("OP_EOL"),_SC("OP_BOL"),_SC("OP_WB"),_SC("OP_MB")
};
#endif
#define OP_GREEDY (MAX_CHAR+1) // * + ? {n}
#define OP_OR (MAX_CHAR+2)
#define OP_EXPR (MAX_CHAR+3) //parentesis ()
#define OP_NOCAPEXPR (MAX_CHAR+4) //parentesis (?:)
#define OP_DOT (MAX_CHAR+5)
#define OP_CLASS (MAX_CHAR+6)
#define OP_CCLASS (MAX_CHAR+7)
#define OP_NCLASS (MAX_CHAR+8) //negates class the [^
#define OP_RANGE (MAX_CHAR+9)
#define OP_CHAR (MAX_CHAR+10)
#define OP_EOL (MAX_CHAR+11)
#define OP_BOL (MAX_CHAR+12)
#define OP_WB (MAX_CHAR+13)
#define OP_MB (MAX_CHAR+14) //match balanced
#define SQREX_SYMBOL_ANY_CHAR ('.')
#define SQREX_SYMBOL_GREEDY_ONE_OR_MORE ('+')
#define SQREX_SYMBOL_GREEDY_ZERO_OR_MORE ('*')
#define SQREX_SYMBOL_GREEDY_ZERO_OR_ONE ('?')
#define SQREX_SYMBOL_BRANCH ('|')
#define SQREX_SYMBOL_END_OF_STRING ('$')
#define SQREX_SYMBOL_BEGINNING_OF_STRING ('^')
#define SQREX_SYMBOL_ESCAPE_CHAR ('\\')
typedef int SQRexNodeType;
typedef struct tagSQRexNode{
SQRexNodeType type;
SQInteger left;
SQInteger right;
SQInteger next;
}SQRexNode;
struct SQRex{
const SQChar *_eol;
const SQChar *_bol;
const SQChar *_p;
SQInteger _first;
SQInteger _op;
SQRexNode *_nodes;
SQInteger _nallocated;
SQInteger _nsize;
SQInteger _nsubexpr;
SQRexMatch *_matches;
SQInteger _currsubexp;
void *_jmpbuf;
const SQChar **_error;
};
static SQInteger sqstd_rex_list(SQRex *exp);
static SQInteger sqstd_rex_newnode(SQRex *exp, SQRexNodeType type)
{
SQRexNode n;
n.type = type;
n.next = n.right = n.left = -1;
if(type == OP_EXPR)
n.right = exp->_nsubexpr++;
if(exp->_nallocated < (exp->_nsize + 1)) {
SQInteger oldsize = exp->_nallocated;
exp->_nallocated *= 2;
exp->_nodes = (SQRexNode *)sq_realloc(exp->_nodes, oldsize * sizeof(SQRexNode) ,exp->_nallocated * sizeof(SQRexNode));
}
exp->_nodes[exp->_nsize++] = n;
SQInteger newid = exp->_nsize - 1;
return (SQInteger)newid;
}
static void sqstd_rex_error(SQRex *exp,const SQChar *error)
{
if(exp->_error) *exp->_error = error;
longjmp(*((jmp_buf*)exp->_jmpbuf),-1);
}
static void sqstd_rex_expect(SQRex *exp, SQInteger n){
if((*exp->_p) != n)
sqstd_rex_error(exp, _SC("expected paren"));
exp->_p++;
}
static SQChar sqstd_rex_escapechar(SQRex *exp)
{
if(*exp->_p == SQREX_SYMBOL_ESCAPE_CHAR){
exp->_p++;
switch(*exp->_p) {
case 'v': exp->_p++; return '\v';
case 'n': exp->_p++; return '\n';
case 't': exp->_p++; return '\t';
case 'r': exp->_p++; return '\r';
case 'f': exp->_p++; return '\f';
default: return (*exp->_p++);
}
} else if(!scisprint(*exp->_p)) sqstd_rex_error(exp,_SC("letter expected"));
return (*exp->_p++);
}
static SQInteger sqstd_rex_charclass(SQRex *exp,SQInteger classid)
{
SQInteger n = sqstd_rex_newnode(exp,OP_CCLASS);
exp->_nodes[n].left = classid;
return n;
}
static SQInteger sqstd_rex_charnode(SQRex *exp,SQBool isclass)
{
SQChar t;
if(*exp->_p == SQREX_SYMBOL_ESCAPE_CHAR) {
exp->_p++;
switch(*exp->_p) {
case 'n': exp->_p++; return sqstd_rex_newnode(exp,'\n');
case 't': exp->_p++; return sqstd_rex_newnode(exp,'\t');
case 'r': exp->_p++; return sqstd_rex_newnode(exp,'\r');
case 'f': exp->_p++; return sqstd_rex_newnode(exp,'\f');
case 'v': exp->_p++; return sqstd_rex_newnode(exp,'\v');
case 'a': case 'A': case 'w': case 'W': case 's': case 'S':
case 'd': case 'D': case 'x': case 'X': case 'c': case 'C':
case 'p': case 'P': case 'l': case 'u':
{
t = *exp->_p; exp->_p++;
return sqstd_rex_charclass(exp,t);
}
case 'm':
{
SQChar cb, ce; //cb = character begin match ce = character end match
cb = *++exp->_p; //skip 'm'
ce = *++exp->_p;
exp->_p++; //points to the next char to be parsed
if ((!cb) || (!ce)) sqstd_rex_error(exp,_SC("balanced chars expected"));
if ( cb == ce ) sqstd_rex_error(exp,_SC("open/close char can't be the same"));
SQInteger node = sqstd_rex_newnode(exp,OP_MB);
exp->_nodes[node].left = cb;
exp->_nodes[node].right = ce;
return node;
}
case 'b':
case 'B':
if(!isclass) {
SQInteger node = sqstd_rex_newnode(exp,OP_WB);
exp->_nodes[node].left = *exp->_p;
exp->_p++;
return node;
} //else default
default:
t = *exp->_p; exp->_p++;
return sqstd_rex_newnode(exp,t);
}
}
else if(!scisprint(*exp->_p)) {
sqstd_rex_error(exp,_SC("letter expected"));
}
t = *exp->_p; exp->_p++;
return sqstd_rex_newnode(exp,t);
}
static SQInteger sqstd_rex_class(SQRex *exp)
{
SQInteger ret = -1;
SQInteger first = -1,chain;
if(*exp->_p == SQREX_SYMBOL_BEGINNING_OF_STRING){
ret = sqstd_rex_newnode(exp,OP_NCLASS);
exp->_p++;
}else ret = sqstd_rex_newnode(exp,OP_CLASS);
if(*exp->_p == ']') sqstd_rex_error(exp,_SC("empty class"));
chain = ret;
while(*exp->_p != ']' && exp->_p != exp->_eol) {
if(*exp->_p == '-' && first != -1){
SQInteger r;
if(*exp->_p++ == ']') sqstd_rex_error(exp,_SC("unfinished range"));
r = sqstd_rex_newnode(exp,OP_RANGE);
if(exp->_nodes[first].type>*exp->_p) sqstd_rex_error(exp,_SC("invalid range"));
if(exp->_nodes[first].type == OP_CCLASS) sqstd_rex_error(exp,_SC("cannot use character classes in ranges"));
exp->_nodes[r].left = exp->_nodes[first].type;
SQInteger t = sqstd_rex_escapechar(exp);
exp->_nodes[r].right = t;
exp->_nodes[chain].next = r;
chain = r;
first = -1;
}
else{
if(first!=-1){
SQInteger c = first;
exp->_nodes[chain].next = c;
chain = c;
first = sqstd_rex_charnode(exp,SQTrue);
}
else{
first = sqstd_rex_charnode(exp,SQTrue);
}
}
}
if(first!=-1){
SQInteger c = first;
exp->_nodes[chain].next = c;
}
/* hack? */
exp->_nodes[ret].left = exp->_nodes[ret].next;
exp->_nodes[ret].next = -1;
return ret;
}
static SQInteger sqstd_rex_parsenumber(SQRex *exp)
{
SQInteger ret = *exp->_p-'0';
SQInteger positions = 10;
exp->_p++;
while(isdigit(*exp->_p)) {
ret = ret*10+(*exp->_p++-'0');
if(positions==1000000000) sqstd_rex_error(exp,_SC("overflow in numeric constant"));
positions *= 10;
};
return ret;
}
static SQInteger sqstd_rex_element(SQRex *exp)
{
SQInteger ret = -1;
switch(*exp->_p)
{
case '(': {
SQInteger expr;
exp->_p++;
if(*exp->_p =='?') {
exp->_p++;
sqstd_rex_expect(exp,':');
expr = sqstd_rex_newnode(exp,OP_NOCAPEXPR);
}
else
expr = sqstd_rex_newnode(exp,OP_EXPR);
SQInteger newn = sqstd_rex_list(exp);
exp->_nodes[expr].left = newn;
ret = expr;
sqstd_rex_expect(exp,')');
}
break;
case '[':
exp->_p++;
ret = sqstd_rex_class(exp);
sqstd_rex_expect(exp,']');
break;
case SQREX_SYMBOL_END_OF_STRING: exp->_p++; ret = sqstd_rex_newnode(exp,OP_EOL);break;
case SQREX_SYMBOL_ANY_CHAR: exp->_p++; ret = sqstd_rex_newnode(exp,OP_DOT);break;
default:
ret = sqstd_rex_charnode(exp,SQFalse);
break;
}
SQBool isgreedy = SQFalse;
unsigned short p0 = 0, p1 = 0;
switch(*exp->_p){
case SQREX_SYMBOL_GREEDY_ZERO_OR_MORE: p0 = 0; p1 = 0xFFFF; exp->_p++; isgreedy = SQTrue; break;
case SQREX_SYMBOL_GREEDY_ONE_OR_MORE: p0 = 1; p1 = 0xFFFF; exp->_p++; isgreedy = SQTrue; break;
case SQREX_SYMBOL_GREEDY_ZERO_OR_ONE: p0 = 0; p1 = 1; exp->_p++; isgreedy = SQTrue; break;
case '{':
exp->_p++;
if(!isdigit(*exp->_p)) sqstd_rex_error(exp,_SC("number expected"));
p0 = (unsigned short)sqstd_rex_parsenumber(exp);
/*******************************/
switch(*exp->_p) {
case '}':
p1 = p0; exp->_p++;
break;
case ',':
exp->_p++;
p1 = 0xFFFF;
if(isdigit(*exp->_p)){
p1 = (unsigned short)sqstd_rex_parsenumber(exp);
}
sqstd_rex_expect(exp,'}');
break;
default:
sqstd_rex_error(exp,_SC(", or } expected"));
}
/*******************************/
isgreedy = SQTrue;
break;
}
if(isgreedy) {
SQInteger nnode = sqstd_rex_newnode(exp,OP_GREEDY);
exp->_nodes[nnode].left = ret;
exp->_nodes[nnode].right = ((p0)<<16)|p1;
ret = nnode;
}
if((*exp->_p != SQREX_SYMBOL_BRANCH) && (*exp->_p != ')') && (*exp->_p != SQREX_SYMBOL_GREEDY_ZERO_OR_MORE) && (*exp->_p != SQREX_SYMBOL_GREEDY_ONE_OR_MORE) && (*exp->_p != '\0')) {
SQInteger nnode = sqstd_rex_element(exp);
exp->_nodes[ret].next = nnode;
}
return ret;
}
static SQInteger sqstd_rex_list(SQRex *exp)
{
SQInteger ret=-1,e;
if(*exp->_p == SQREX_SYMBOL_BEGINNING_OF_STRING) {
exp->_p++;
ret = sqstd_rex_newnode(exp,OP_BOL);
}
e = sqstd_rex_element(exp);
if(ret != -1) {
exp->_nodes[ret].next = e;
}
else ret = e;
if(*exp->_p == SQREX_SYMBOL_BRANCH) {
SQInteger temp,tright;
exp->_p++;
temp = sqstd_rex_newnode(exp,OP_OR);
exp->_nodes[temp].left = ret;
tright = sqstd_rex_list(exp);
exp->_nodes[temp].right = tright;
ret = temp;
}
return ret;
}
static SQBool sqstd_rex_matchcclass(SQInteger cclass,SQChar c)
{
switch(cclass) {
case 'a': return isalpha(c)?SQTrue:SQFalse;
case 'A': return !isalpha(c)?SQTrue:SQFalse;
case 'w': return (isalnum(c) || c == '_')?SQTrue:SQFalse;
case 'W': return (!isalnum(c) && c != '_')?SQTrue:SQFalse;
case 's': return isspace(c)?SQTrue:SQFalse;
case 'S': return !isspace(c)?SQTrue:SQFalse;
case 'd': return isdigit(c)?SQTrue:SQFalse;
case 'D': return !isdigit(c)?SQTrue:SQFalse;
case 'x': return isxdigit(c)?SQTrue:SQFalse;
case 'X': return !isxdigit(c)?SQTrue:SQFalse;
case 'c': return iscntrl(c)?SQTrue:SQFalse;
case 'C': return !iscntrl(c)?SQTrue:SQFalse;
case 'p': return ispunct(c)?SQTrue:SQFalse;
case 'P': return !ispunct(c)?SQTrue:SQFalse;
case 'l': return islower(c)?SQTrue:SQFalse;
case 'u': return isupper(c)?SQTrue:SQFalse;
}
return SQFalse; /*cannot happen*/
}
static SQBool sqstd_rex_matchclass(SQRex* exp,SQRexNode *node,SQChar c)
{
do {
switch(node->type) {
case OP_RANGE:
if(c >= node->left && c <= node->right) return SQTrue;
break;
case OP_CCLASS:
if(sqstd_rex_matchcclass(node->left,c)) return SQTrue;
break;
default:
if(c == node->type)return SQTrue;
}
} while((node->next != -1) && (node = &exp->_nodes[node->next]));
return SQFalse;
}
static const SQChar *sqstd_rex_matchnode(SQRex* exp,SQRexNode *node,const SQChar *str,SQRexNode *next)
{
SQRexNodeType type = node->type;
switch(type) {
case OP_GREEDY: {
//SQRexNode *greedystop = (node->next != -1) ? &exp->_nodes[node->next] : NULL;
SQRexNode *greedystop = NULL;
SQInteger p0 = (node->right >> 16)&0x0000FFFF, p1 = node->right&0x0000FFFF, nmaches = 0;
const SQChar *s=str, *good = str;
if(node->next != -1) {
greedystop = &exp->_nodes[node->next];
}
else {
greedystop = next;
}
while((nmaches == 0xFFFF || nmaches < p1)) {
const SQChar *stop;
if(!(s = sqstd_rex_matchnode(exp,&exp->_nodes[node->left],s,greedystop)))
break;
nmaches++;
good=s;
if(greedystop) {
//checks that 0 matches satisfy the expression(if so skips)
//if not would always stop(for instance if is a '?')
if(greedystop->type != OP_GREEDY ||
(greedystop->type == OP_GREEDY && ((greedystop->right >> 16)&0x0000FFFF) != 0))
{
SQRexNode *gnext = NULL;
if(greedystop->next != -1) {
gnext = &exp->_nodes[greedystop->next];
}else if(next && next->next != -1){
gnext = &exp->_nodes[next->next];
}
stop = sqstd_rex_matchnode(exp,greedystop,s,gnext);
if(stop) {
//if satisfied stop it
if(p0 == p1 && p0 == nmaches) break;
else if(nmaches >= p0 && p1 == 0xFFFF) break;
else if(nmaches >= p0 && nmaches <= p1) break;
}
}
}
if(s >= exp->_eol)
break;
}
if(p0 == p1 && p0 == nmaches) return good;
else if(nmaches >= p0 && p1 == 0xFFFF) return good;
else if(nmaches >= p0 && nmaches <= p1) return good;
return NULL;
}
case OP_OR: {
const SQChar *asd = str;
SQRexNode *temp=&exp->_nodes[node->left];
while( (asd = sqstd_rex_matchnode(exp,temp,asd,NULL)) ) {
if(temp->next != -1)
temp = &exp->_nodes[temp->next];
else
return asd;
}
asd = str;
temp = &exp->_nodes[node->right];
while( (asd = sqstd_rex_matchnode(exp,temp,asd,NULL)) ) {
if(temp->next != -1)
temp = &exp->_nodes[temp->next];
else
return asd;
}
return NULL;
break;
}
case OP_EXPR:
case OP_NOCAPEXPR:{
SQRexNode *n = &exp->_nodes[node->left];
const SQChar *cur = str;
SQInteger capture = -1;
if(node->type != OP_NOCAPEXPR && node->right == exp->_currsubexp) {
capture = exp->_currsubexp;
exp->_matches[capture].begin = cur;
exp->_currsubexp++;
}
SQInteger tempcap = exp->_currsubexp;
do {
SQRexNode *subnext = NULL;
if(n->next != -1) {
subnext = &exp->_nodes[n->next];
}else {
subnext = next;
}
if(!(cur = sqstd_rex_matchnode(exp,n,cur,subnext))) {
if(capture != -1){
exp->_matches[capture].begin = 0;
exp->_matches[capture].len = 0;
}
return NULL;
}
} while((n->next != -1) && (n = &exp->_nodes[n->next]));
exp->_currsubexp = tempcap;
if(capture != -1)
exp->_matches[capture].len = cur - exp->_matches[capture].begin;
return cur;
}
case OP_WB:
if((str == exp->_bol && !isspace(*str))
|| (str == exp->_eol && !isspace(*(str-1)))
|| (!isspace(*str) && isspace(*(str+1)))
|| (isspace(*str) && !isspace(*(str+1))) ) {
return (node->left == 'b')?str:NULL;
}
return (node->left == 'b')?NULL:str;
case OP_BOL:
if(str == exp->_bol) return str;
return NULL;
case OP_EOL:
if(str == exp->_eol) return str;
return NULL;
case OP_DOT:{
if (str == exp->_eol) return NULL;
str++;
}
return str;
case OP_NCLASS:
case OP_CLASS:
if (str == exp->_eol) return NULL;
if(sqstd_rex_matchclass(exp,&exp->_nodes[node->left],*str)?(type == OP_CLASS?SQTrue:SQFalse):(type == OP_NCLASS?SQTrue:SQFalse)) {
str++;
return str;
}
return NULL;
case OP_CCLASS:
if (str == exp->_eol) return NULL;
if(sqstd_rex_matchcclass(node->left,*str)) {
str++;
return str;
}
return NULL;
case OP_MB:
{
SQInteger cb = node->left; //char that opens a balanced expression
if(*str != cb) return NULL; // string doesnt start with open char
SQInteger ce = node->right; //char that closes a balanced expression
SQInteger cont = 1;
const SQChar *streol = exp->_eol;
while (++str < streol) {
if (*str == ce) {
if (--cont == 0) {
return ++str;
}
}
else if (*str == cb) cont++;
}
}
return NULL; // string ends out of balance
default: /* char */
if (str == exp->_eol) return NULL;
if(*str != node->type) return NULL;
str++;
return str;
}
return NULL;
}
/* public api */
SQRex *sqstd_rex_compile(const SQChar *pattern,const SQChar **error)
{
SQRex * volatile exp = (SQRex *)sq_malloc(sizeof(SQRex)); // "volatile" is needed for setjmp()
exp->_eol = exp->_bol = NULL;
exp->_p = pattern;
exp->_nallocated = (SQInteger)scstrlen(pattern) * sizeof(SQChar);
exp->_nodes = (SQRexNode *)sq_malloc(exp->_nallocated * sizeof(SQRexNode));
exp->_nsize = 0;
exp->_matches = 0;
exp->_nsubexpr = 0;
exp->_first = sqstd_rex_newnode(exp,OP_EXPR);
exp->_error = error;
exp->_jmpbuf = sq_malloc(sizeof(jmp_buf));
if(setjmp(*((jmp_buf*)exp->_jmpbuf)) == 0) {
SQInteger res = sqstd_rex_list(exp);
exp->_nodes[exp->_first].left = res;
if(*exp->_p!='\0')
sqstd_rex_error(exp,_SC("unexpected character"));
#ifdef _DEBUG
{
SQInteger nsize,i;
SQRexNode *t;
nsize = exp->_nsize;
t = &exp->_nodes[0];
scprintf(_SC("\n"));
for(i = 0;i < nsize; i++) {
if(exp->_nodes[i].type>MAX_CHAR)
scprintf(_SC("[%02d] %10s "),i,g_nnames[exp->_nodes[i].type-MAX_CHAR]);
else
scprintf(_SC("[%02d] %10c "),i,exp->_nodes[i].type);
scprintf(_SC("left %02d right %02d next %02d\n"), (SQInt32)exp->_nodes[i].left, (SQInt32)exp->_nodes[i].right, (SQInt32)exp->_nodes[i].next);
}
scprintf(_SC("\n"));
}
#endif
exp->_matches = (SQRexMatch *) sq_malloc(exp->_nsubexpr * sizeof(SQRexMatch));
memset(exp->_matches,0,exp->_nsubexpr * sizeof(SQRexMatch));
}
else{
sqstd_rex_free(exp);
return NULL;
}
return exp;
}
void sqstd_rex_free(SQRex *exp)
{
if(exp) {
if(exp->_nodes) sq_free(exp->_nodes,exp->_nallocated * sizeof(SQRexNode));
if(exp->_jmpbuf) sq_free(exp->_jmpbuf,sizeof(jmp_buf));
if(exp->_matches) sq_free(exp->_matches,exp->_nsubexpr * sizeof(SQRexMatch));
sq_free(exp,sizeof(SQRex));
}
}
SQBool sqstd_rex_match(SQRex* exp,const SQChar* text)
{
const SQChar* res = NULL;
exp->_bol = text;
exp->_eol = text + scstrlen(text);
exp->_currsubexp = 0;
res = sqstd_rex_matchnode(exp,exp->_nodes,text,NULL);
if(res == NULL || res != exp->_eol)
return SQFalse;
return SQTrue;
}
SQBool sqstd_rex_searchrange(SQRex* exp,const SQChar* text_begin,const SQChar* text_end,const SQChar** out_begin, const SQChar** out_end)
{
const SQChar *cur = NULL;
SQInteger node = exp->_first;
if(text_begin >= text_end) return SQFalse;
exp->_bol = text_begin;
exp->_eol = text_end;
do {
cur = text_begin;
while(node != -1) {
exp->_currsubexp = 0;
cur = sqstd_rex_matchnode(exp,&exp->_nodes[node],cur,NULL);
if(!cur)
break;
node = exp->_nodes[node].next;
}
text_begin++;
} while(cur == NULL && text_begin != text_end);
if(cur == NULL)
return SQFalse;
--text_begin;
if(out_begin) *out_begin = text_begin;
if(out_end) *out_end = cur;
return SQTrue;
}
SQBool sqstd_rex_search(SQRex* exp,const SQChar* text, const SQChar** out_begin, const SQChar** out_end)
{
return sqstd_rex_searchrange(exp,text,text + scstrlen(text),out_begin,out_end);
}
SQInteger sqstd_rex_getsubexpcount(SQRex* exp)
{
return exp->_nsubexpr;
}
SQBool sqstd_rex_getsubexp(SQRex* exp, SQInteger n, SQRexMatch *subexp)
{
if( n<0 || n >= exp->_nsubexpr) return SQFalse;
*subexp = exp->_matches[n];
return SQTrue;
}

336
external/Squirrel/Lib/sqstdstream.cpp vendored Normal file
View File

@ -0,0 +1,336 @@
/* see copyright notice in squirrel.h */
#include <new>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <squirrel.h>
#include <sqstdio.h>
#include <sqstdblob.h>
#include "sqstdstream.h"
#include "sqstdblobimpl.h"
#define SETUP_STREAM(v) \
SQStream *self = NULL; \
if(SQ_FAILED(sq_getinstanceup(v,1,(SQUserPointer*)&self,(SQUserPointer)SQSTD_STREAM_TYPE_TAG))) \
return sq_throwerror(v,_SC("invalid type tag")); \
if(!self || !self->IsValid()) \
return sq_throwerror(v,_SC("the stream is invalid"));
SQInteger _stream_readblob(HSQUIRRELVM v)
{
SETUP_STREAM(v);
SQUserPointer data,blobp;
SQInteger size,res;
sq_getinteger(v,2,&size);
if(size > self->Len()) {
size = self->Len();
}
data = sq_getscratchpad(v,size);
res = self->Read(data,size);
if(res <= 0)
return sq_throwerror(v,_SC("no data left to read"));
blobp = sqstd_createblob(v,res);
memcpy(blobp,data,res);
return 1;
}
#define SAFE_READN(ptr,len) { \
if(self->Read(ptr,len) != len) return sq_throwerror(v,_SC("io error")); \
}
SQInteger _stream_readn(HSQUIRRELVM v)
{
SETUP_STREAM(v);
SQInteger format;
sq_getinteger(v, 2, &format);
switch(format) {
case 'l': {
SQInteger i;
SAFE_READN(&i, sizeof(i));
sq_pushinteger(v, i);
}
break;
case 'i': {
SQInt32 i;
SAFE_READN(&i, sizeof(i));
sq_pushinteger(v, i);
}
break;
case 's': {
short s;
SAFE_READN(&s, sizeof(short));
sq_pushinteger(v, s);
}
break;
case 'w': {
unsigned short w;
SAFE_READN(&w, sizeof(unsigned short));
sq_pushinteger(v, w);
}
break;
case 'c': {
char c;
SAFE_READN(&c, sizeof(char));
sq_pushinteger(v, c);
}
break;
case 'b': {
unsigned char c;
SAFE_READN(&c, sizeof(unsigned char));
sq_pushinteger(v, c);
}
break;
case 'f': {
float f;
SAFE_READN(&f, sizeof(float));
sq_pushfloat(v, f);
}
break;
case 'd': {
double d;
SAFE_READN(&d, sizeof(double));
sq_pushfloat(v, (SQFloat)d);
}
break;
default:
return sq_throwerror(v, _SC("invalid format"));
}
return 1;
}
SQInteger _stream_writeblob(HSQUIRRELVM v)
{
SQUserPointer data;
SQInteger size;
SETUP_STREAM(v);
if(SQ_FAILED(sqstd_getblob(v,2,&data)))
return sq_throwerror(v,_SC("invalid parameter"));
size = sqstd_getblobsize(v,2);
if(self->Write(data,size) != size)
return sq_throwerror(v,_SC("io error"));
sq_pushinteger(v,size);
return 1;
}
SQInteger _stream_writen(HSQUIRRELVM v)
{
SETUP_STREAM(v);
SQInteger format, ti;
SQFloat tf;
sq_getinteger(v, 3, &format);
switch(format) {
case 'l': {
SQInteger i;
sq_getinteger(v, 2, &ti);
i = ti;
self->Write(&i, sizeof(SQInteger));
}
break;
case 'i': {
SQInt32 i;
sq_getinteger(v, 2, &ti);
i = (SQInt32)ti;
self->Write(&i, sizeof(SQInt32));
}
break;
case 's': {
short s;
sq_getinteger(v, 2, &ti);
s = (short)ti;
self->Write(&s, sizeof(short));
}
break;
case 'w': {
unsigned short w;
sq_getinteger(v, 2, &ti);
w = (unsigned short)ti;
self->Write(&w, sizeof(unsigned short));
}
break;
case 'c': {
char c;
sq_getinteger(v, 2, &ti);
c = (char)ti;
self->Write(&c, sizeof(char));
}
break;
case 'b': {
unsigned char b;
sq_getinteger(v, 2, &ti);
b = (unsigned char)ti;
self->Write(&b, sizeof(unsigned char));
}
break;
case 'f': {
float f;
sq_getfloat(v, 2, &tf);
f = (float)tf;
self->Write(&f, sizeof(float));
}
break;
case 'd': {
double d;
sq_getfloat(v, 2, &tf);
d = tf;
self->Write(&d, sizeof(double));
}
break;
default:
return sq_throwerror(v, _SC("invalid format"));
}
return 0;
}
SQInteger _stream_seek(HSQUIRRELVM v)
{
SETUP_STREAM(v);
SQInteger offset, origin = SQ_SEEK_SET;
sq_getinteger(v, 2, &offset);
if(sq_gettop(v) > 2) {
SQInteger t;
sq_getinteger(v, 3, &t);
switch(t) {
case 'b': origin = SQ_SEEK_SET; break;
case 'c': origin = SQ_SEEK_CUR; break;
case 'e': origin = SQ_SEEK_END; break;
default: return sq_throwerror(v,_SC("invalid origin"));
}
}
sq_pushinteger(v, self->Seek(offset, origin));
return 1;
}
SQInteger _stream_tell(HSQUIRRELVM v)
{
SETUP_STREAM(v);
sq_pushinteger(v, self->Tell());
return 1;
}
SQInteger _stream_len(HSQUIRRELVM v)
{
SETUP_STREAM(v);
sq_pushinteger(v, self->Len());
return 1;
}
SQInteger _stream_flush(HSQUIRRELVM v)
{
SETUP_STREAM(v);
if(!self->Flush())
sq_pushinteger(v, 1);
else
sq_pushnull(v);
return 1;
}
SQInteger _stream_eos(HSQUIRRELVM v)
{
SETUP_STREAM(v);
if(self->EOS())
sq_pushinteger(v, 1);
else
sq_pushnull(v);
return 1;
}
SQInteger _stream__cloned(HSQUIRRELVM v)
{
return sq_throwerror(v,_SC("this object cannot be cloned"));
}
static const SQRegFunction _stream_methods[] = {
_DECL_STREAM_FUNC(readblob,2,_SC("xn")),
_DECL_STREAM_FUNC(readn,2,_SC("xn")),
_DECL_STREAM_FUNC(writeblob,-2,_SC("xx")),
_DECL_STREAM_FUNC(writen,3,_SC("xnn")),
_DECL_STREAM_FUNC(seek,-2,_SC("xnn")),
_DECL_STREAM_FUNC(tell,1,_SC("x")),
_DECL_STREAM_FUNC(len,1,_SC("x")),
_DECL_STREAM_FUNC(eos,1,_SC("x")),
_DECL_STREAM_FUNC(flush,1,_SC("x")),
_DECL_STREAM_FUNC(_cloned,0,NULL),
{NULL,(SQFUNCTION)0,0,NULL}
};
void init_streamclass(HSQUIRRELVM v)
{
sq_pushregistrytable(v);
sq_pushstring(v,_SC("std_stream"),-1);
if(SQ_FAILED(sq_get(v,-2))) {
sq_pushstring(v,_SC("std_stream"),-1);
sq_newclass(v,SQFalse);
sq_settypetag(v,-1,(SQUserPointer)SQSTD_STREAM_TYPE_TAG);
SQInteger i = 0;
while(_stream_methods[i].name != 0) {
const SQRegFunction &f = _stream_methods[i];
sq_pushstring(v,f.name,-1);
sq_newclosure(v,f.f,0);
sq_setparamscheck(v,f.nparamscheck,f.typemask);
sq_newslot(v,-3,SQFalse);
i++;
}
sq_newslot(v,-3,SQFalse);
sq_pushroottable(v);
sq_pushstring(v,_SC("stream"),-1);
sq_pushstring(v,_SC("std_stream"),-1);
sq_get(v,-4);
sq_newslot(v,-3,SQFalse);
sq_pop(v,1);
}
else {
sq_pop(v,1); //result
}
sq_pop(v,1);
}
SQRESULT declare_stream(HSQUIRRELVM v,const SQChar* name,SQUserPointer typetag,const SQChar* reg_name,const SQRegFunction *methods,const SQRegFunction *globals)
{
if(sq_gettype(v,-1) != OT_TABLE)
return sq_throwerror(v,_SC("table expected"));
SQInteger top = sq_gettop(v);
//create delegate
init_streamclass(v);
sq_pushregistrytable(v);
sq_pushstring(v,reg_name,-1);
sq_pushstring(v,_SC("std_stream"),-1);
if(SQ_SUCCEEDED(sq_get(v,-3))) {
sq_newclass(v,SQTrue);
sq_settypetag(v,-1,typetag);
SQInteger i = 0;
while(methods[i].name != 0) {
const SQRegFunction &f = methods[i];
sq_pushstring(v,f.name,-1);
sq_newclosure(v,f.f,0);
sq_setparamscheck(v,f.nparamscheck,f.typemask);
sq_setnativeclosurename(v,-1,f.name);
sq_newslot(v,-3,SQFalse);
i++;
}
sq_newslot(v,-3,SQFalse);
sq_pop(v,1);
i = 0;
while(globals[i].name!=0)
{
const SQRegFunction &f = globals[i];
sq_pushstring(v,f.name,-1);
sq_newclosure(v,f.f,0);
sq_setparamscheck(v,f.nparamscheck,f.typemask);
sq_setnativeclosurename(v,-1,f.name);
sq_newslot(v,-3,SQFalse);
i++;
}
//register the class in the target table
sq_pushstring(v,name,-1);
sq_pushregistrytable(v);
sq_pushstring(v,reg_name,-1);
sq_get(v,-2);
sq_remove(v,-2);
sq_newslot(v,-3,SQFalse);
sq_settop(v,top);
return SQ_OK;
}
sq_settop(v,top);
return SQ_ERROR;
}

18
external/Squirrel/Lib/sqstdstream.h vendored Normal file
View File

@ -0,0 +1,18 @@
/* see copyright notice in squirrel.h */
#ifndef _SQSTD_STREAM_H_
#define _SQSTD_STREAM_H_
SQInteger _stream_readblob(HSQUIRRELVM v);
SQInteger _stream_readline(HSQUIRRELVM v);
SQInteger _stream_readn(HSQUIRRELVM v);
SQInteger _stream_writeblob(HSQUIRRELVM v);
SQInteger _stream_writen(HSQUIRRELVM v);
SQInteger _stream_seek(HSQUIRRELVM v);
SQInteger _stream_tell(HSQUIRRELVM v);
SQInteger _stream_len(HSQUIRRELVM v);
SQInteger _stream_eos(HSQUIRRELVM v);
SQInteger _stream_flush(HSQUIRRELVM v);
#define _DECL_STREAM_FUNC(name,nparams,typecheck) {_SC(#name),_stream_##name,nparams,typecheck}
SQRESULT declare_stream(HSQUIRRELVM v,const SQChar* name,SQUserPointer typetag,const SQChar* reg_name,const SQRegFunction *methods,const SQRegFunction *globals);
#endif /*_SQSTD_STREAM_H_*/

501
external/Squirrel/Lib/sqstdstring.cpp vendored Normal file
View File

@ -0,0 +1,501 @@
/* see copyright notice in squirrel.h */
#include <squirrel.h>
#include <sqstdstring.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <assert.h>
#define MAX_FORMAT_LEN 20
#define MAX_WFORMAT_LEN 3
#define ADDITIONAL_FORMAT_SPACE (100*sizeof(SQChar))
static SQBool isfmtchr(SQChar ch)
{
switch(ch) {
case '-': case '+': case ' ': case '#': case '0': return SQTrue;
}
return SQFalse;
}
static SQInteger validate_format(HSQUIRRELVM v, SQChar *fmt, const SQChar *src, SQInteger n,SQInteger &width)
{
SQChar *dummy;
SQChar swidth[MAX_WFORMAT_LEN];
SQInteger wc = 0;
SQInteger start = n;
fmt[0] = '%';
while (isfmtchr(src[n])) n++;
while (scisdigit(src[n])) {
swidth[wc] = src[n];
n++;
wc++;
if(wc>=MAX_WFORMAT_LEN)
return sq_throwerror(v,_SC("width format too long"));
}
swidth[wc] = '\0';
if(wc > 0) {
width = scstrtol(swidth,&dummy,10);
}
else
width = 0;
if (src[n] == '.') {
n++;
wc = 0;
while (scisdigit(src[n])) {
swidth[wc] = src[n];
n++;
wc++;
if(wc>=MAX_WFORMAT_LEN)
return sq_throwerror(v,_SC("precision format too long"));
}
swidth[wc] = '\0';
if(wc > 0) {
width += scstrtol(swidth,&dummy,10);
}
}
if (n-start > MAX_FORMAT_LEN )
return sq_throwerror(v,_SC("format too long"));
memcpy(&fmt[1],&src[start],((n-start)+1)*sizeof(SQChar));
fmt[(n-start)+2] = '\0';
return n;
}
SQRESULT sqstd_format(HSQUIRRELVM v,SQInteger nformatstringidx,SQInteger *outlen,SQChar **output)
{
const SQChar *format;
SQChar *dest;
SQChar fmt[MAX_FORMAT_LEN];
sq_getstring(v,nformatstringidx,&format);
SQInteger format_size = sq_getsize(v,nformatstringidx);
SQInteger allocated = (format_size+2)*sizeof(SQChar);
dest = sq_getscratchpad(v,allocated);
SQInteger n = 0,i = 0, nparam = nformatstringidx+1, w = 0;
//while(format[n] != '\0')
while(n < format_size)
{
if(format[n] != '%') {
assert(i < allocated);
dest[i++] = format[n];
n++;
}
else if(format[n+1] == '%') { //handles %%
dest[i++] = '%';
n += 2;
}
else {
n++;
if( nparam > sq_gettop(v) )
return sq_throwerror(v,_SC("not enough paramters for the given format string"));
n = validate_format(v,fmt,format,n,w);
if(n < 0) return -1;
SQInteger addlen = 0;
SQInteger valtype = 0;
const SQChar *ts = NULL;
SQInteger ti = 0;
SQFloat tf = 0;
switch(format[n]) {
case 's':
if(SQ_FAILED(sq_getstring(v,nparam,&ts)))
return sq_throwerror(v,_SC("string expected for the specified format"));
addlen = (sq_getsize(v,nparam)*sizeof(SQChar))+((w+1)*sizeof(SQChar));
valtype = 's';
break;
case 'i': case 'd': case 'o': case 'u': case 'x': case 'X':
#ifdef _SQ64
{
size_t flen = scstrlen(fmt);
SQInteger fpos = flen - 1;
SQChar f = fmt[fpos];
const SQChar *prec = (const SQChar *)_PRINT_INT_PREC;
while(*prec != _SC('\0')) {
fmt[fpos++] = *prec++;
}
fmt[fpos++] = f;
fmt[fpos++] = _SC('\0');
}
#endif
case 'c':
if(SQ_FAILED(sq_getinteger(v,nparam,&ti)))
return sq_throwerror(v,_SC("integer expected for the specified format"));
addlen = (ADDITIONAL_FORMAT_SPACE)+((w+1)*sizeof(SQChar));
valtype = 'i';
break;
case 'f': case 'g': case 'G': case 'e': case 'E':
if(SQ_FAILED(sq_getfloat(v,nparam,&tf)))
return sq_throwerror(v,_SC("float expected for the specified format"));
addlen = (ADDITIONAL_FORMAT_SPACE)+((w+1)*sizeof(SQChar));
valtype = 'f';
break;
default:
return sq_throwerror(v,_SC("invalid format"));
}
n++;
allocated += addlen + sizeof(SQChar);
dest = sq_getscratchpad(v,allocated);
switch(valtype) {
case 's': i += scsprintf(&dest[i],allocated,fmt,ts); break;
case 'i': i += scsprintf(&dest[i],allocated,fmt,ti); break;
case 'f': i += scsprintf(&dest[i],allocated,fmt,tf); break;
};
nparam ++;
}
}
*outlen = i;
dest[i] = '\0';
*output = dest;
return SQ_OK;
}
static SQInteger _string_format(HSQUIRRELVM v)
{
SQChar *dest = NULL;
SQInteger length = 0;
if(SQ_FAILED(sqstd_format(v,2,&length,&dest)))
return -1;
sq_pushstring(v,dest,length);
return 1;
}
static void __strip_l(const SQChar *str,const SQChar **start)
{
const SQChar *t = str;
while(((*t) != '\0') && scisspace(*t)){ t++; }
*start = t;
}
static void __strip_r(const SQChar *str,SQInteger len,const SQChar **end)
{
if(len == 0) {
*end = str;
return;
}
const SQChar *t = &str[len-1];
while(t >= str && scisspace(*t)) { t--; }
*end = t + 1;
}
static SQInteger _string_strip(HSQUIRRELVM v)
{
const SQChar *str,*start,*end;
sq_getstring(v,2,&str);
SQInteger len = sq_getsize(v,2);
__strip_l(str,&start);
__strip_r(str,len,&end);
sq_pushstring(v,start,end - start);
return 1;
}
static SQInteger _string_lstrip(HSQUIRRELVM v)
{
const SQChar *str,*start;
sq_getstring(v,2,&str);
__strip_l(str,&start);
sq_pushstring(v,start,-1);
return 1;
}
static SQInteger _string_rstrip(HSQUIRRELVM v)
{
const SQChar *str,*end;
sq_getstring(v,2,&str);
SQInteger len = sq_getsize(v,2);
__strip_r(str,len,&end);
sq_pushstring(v,str,end - str);
return 1;
}
static SQInteger _string_split(HSQUIRRELVM v)
{
const SQChar *str,*seps;
SQChar *stemp;
sq_getstring(v,2,&str);
sq_getstring(v,3,&seps);
SQInteger sepsize = sq_getsize(v,3);
if(sepsize == 0) return sq_throwerror(v,_SC("empty separators string"));
SQInteger memsize = (sq_getsize(v,2)+1)*sizeof(SQChar);
stemp = sq_getscratchpad(v,memsize);
memcpy(stemp,str,memsize);
SQChar *start = stemp;
SQChar *end = stemp;
sq_newarray(v,0);
while(*end != '\0')
{
SQChar cur = *end;
for(SQInteger i = 0; i < sepsize; i++)
{
if(cur == seps[i])
{
*end = 0;
sq_pushstring(v,start,-1);
sq_arrayappend(v,-2);
start = end + 1;
break;
}
}
end++;
}
if(end != start)
{
sq_pushstring(v,start,-1);
sq_arrayappend(v,-2);
}
return 1;
}
static SQInteger _string_escape(HSQUIRRELVM v)
{
const SQChar *str;
SQChar *dest,*resstr;
SQInteger size;
sq_getstring(v,2,&str);
size = sq_getsize(v,2);
if(size == 0) {
sq_push(v,2);
return 1;
}
#ifdef SQUNICODE
#if WCHAR_SIZE == 2
const SQChar *escpat = _SC("\\x%04x");
const SQInteger maxescsize = 6;
#else //WCHAR_SIZE == 4
const SQChar *escpat = _SC("\\x%08x");
const SQInteger maxescsize = 10;
#endif
#else
const SQChar *escpat = _SC("\\x%02x");
const SQInteger maxescsize = 4;
#endif
SQInteger destcharsize = (size * maxescsize); //assumes every char could be escaped
resstr = dest = (SQChar *)sq_getscratchpad(v,destcharsize * sizeof(SQChar));
SQChar c;
SQChar escch;
SQInteger escaped = 0;
for(int n = 0; n < size; n++){
c = *str++;
escch = 0;
if(scisprint(c) || c == 0) {
switch(c) {
case '\a': escch = 'a'; break;
case '\b': escch = 'b'; break;
case '\t': escch = 't'; break;
case '\n': escch = 'n'; break;
case '\v': escch = 'v'; break;
case '\f': escch = 'f'; break;
case '\r': escch = 'r'; break;
case '\\': escch = '\\'; break;
case '\"': escch = '\"'; break;
case '\'': escch = '\''; break;
case 0: escch = '0'; break;
}
if(escch) {
*dest++ = '\\';
*dest++ = escch;
escaped++;
}
else {
*dest++ = c;
}
}
else {
dest += scsprintf(dest, destcharsize, escpat, c);
escaped++;
}
}
if(escaped) {
sq_pushstring(v,resstr,dest - resstr);
}
else {
sq_push(v,2); //nothing escaped
}
return 1;
}
static SQInteger _string_startswith(HSQUIRRELVM v)
{
const SQChar *str,*cmp;
sq_getstring(v,2,&str);
sq_getstring(v,3,&cmp);
SQInteger len = sq_getsize(v,2);
SQInteger cmplen = sq_getsize(v,3);
SQBool ret = SQFalse;
if(cmplen <= len) {
ret = memcmp(str,cmp,sq_rsl(cmplen)) == 0 ? SQTrue : SQFalse;
}
sq_pushbool(v,ret);
return 1;
}
static SQInteger _string_endswith(HSQUIRRELVM v)
{
const SQChar *str,*cmp;
sq_getstring(v,2,&str);
sq_getstring(v,3,&cmp);
SQInteger len = sq_getsize(v,2);
SQInteger cmplen = sq_getsize(v,3);
SQBool ret = SQFalse;
if(cmplen <= len) {
ret = memcmp(&str[len - cmplen],cmp,sq_rsl(cmplen)) == 0 ? SQTrue : SQFalse;
}
sq_pushbool(v,ret);
return 1;
}
#define SETUP_REX(v) \
SQRex *self = NULL; \
sq_getinstanceup(v,1,(SQUserPointer *)&self,0);
static SQInteger _rexobj_releasehook(SQUserPointer p, SQInteger SQ_UNUSED_ARG(size))
{
SQRex *self = ((SQRex *)p);
sqstd_rex_free(self);
return 1;
}
static SQInteger _regexp_match(HSQUIRRELVM v)
{
SETUP_REX(v);
const SQChar *str;
sq_getstring(v,2,&str);
if(sqstd_rex_match(self,str) == SQTrue)
{
sq_pushbool(v,SQTrue);
return 1;
}
sq_pushbool(v,SQFalse);
return 1;
}
static void _addrexmatch(HSQUIRRELVM v,const SQChar *str,const SQChar *begin,const SQChar *end)
{
sq_newtable(v);
sq_pushstring(v,_SC("begin"),-1);
sq_pushinteger(v,begin - str);
sq_rawset(v,-3);
sq_pushstring(v,_SC("end"),-1);
sq_pushinteger(v,end - str);
sq_rawset(v,-3);
}
static SQInteger _regexp_search(HSQUIRRELVM v)
{
SETUP_REX(v);
const SQChar *str,*begin,*end;
SQInteger start = 0;
sq_getstring(v,2,&str);
if(sq_gettop(v) > 2) sq_getinteger(v,3,&start);
if(sqstd_rex_search(self,str+start,&begin,&end) == SQTrue) {
_addrexmatch(v,str,begin,end);
return 1;
}
return 0;
}
static SQInteger _regexp_capture(HSQUIRRELVM v)
{
SETUP_REX(v);
const SQChar *str,*begin,*end;
SQInteger start = 0;
sq_getstring(v,2,&str);
if(sq_gettop(v) > 2) sq_getinteger(v,3,&start);
if(sqstd_rex_search(self,str+start,&begin,&end) == SQTrue) {
SQInteger n = sqstd_rex_getsubexpcount(self);
SQRexMatch match;
sq_newarray(v,0);
for(SQInteger i = 0;i < n; i++) {
sqstd_rex_getsubexp(self,i,&match);
if(match.len > 0)
_addrexmatch(v,str,match.begin,match.begin+match.len);
else
_addrexmatch(v,str,str,str); //empty match
sq_arrayappend(v,-2);
}
return 1;
}
return 0;
}
static SQInteger _regexp_subexpcount(HSQUIRRELVM v)
{
SETUP_REX(v);
sq_pushinteger(v,sqstd_rex_getsubexpcount(self));
return 1;
}
static SQInteger _regexp_constructor(HSQUIRRELVM v)
{
const SQChar *error,*pattern;
sq_getstring(v,2,&pattern);
SQRex *rex = sqstd_rex_compile(pattern,&error);
if(!rex) return sq_throwerror(v,error);
sq_setinstanceup(v,1,rex);
sq_setreleasehook(v,1,_rexobj_releasehook);
return 0;
}
static SQInteger _regexp__typeof(HSQUIRRELVM v)
{
sq_pushstring(v,_SC("regexp"),-1);
return 1;
}
#define _DECL_REX_FUNC(name,nparams,pmask) {_SC(#name),_regexp_##name,nparams,pmask}
static const SQRegFunction rexobj_funcs[]={
_DECL_REX_FUNC(constructor,2,_SC(".s")),
_DECL_REX_FUNC(search,-2,_SC("xsn")),
_DECL_REX_FUNC(match,2,_SC("xs")),
_DECL_REX_FUNC(capture,-2,_SC("xsn")),
_DECL_REX_FUNC(subexpcount,1,_SC("x")),
_DECL_REX_FUNC(_typeof,1,_SC("x")),
{NULL,(SQFUNCTION)0,0,NULL}
};
#undef _DECL_REX_FUNC
#define _DECL_FUNC(name,nparams,pmask) {_SC(#name),_string_##name,nparams,pmask}
static const SQRegFunction stringlib_funcs[]={
_DECL_FUNC(format,-2,_SC(".s")),
_DECL_FUNC(strip,2,_SC(".s")),
_DECL_FUNC(lstrip,2,_SC(".s")),
_DECL_FUNC(rstrip,2,_SC(".s")),
_DECL_FUNC(split,3,_SC(".ss")),
_DECL_FUNC(escape,2,_SC(".s")),
_DECL_FUNC(startswith,3,_SC(".ss")),
_DECL_FUNC(endswith,3,_SC(".ss")),
{NULL,(SQFUNCTION)0,0,NULL}
};
#undef _DECL_FUNC
SQInteger sqstd_register_stringlib(HSQUIRRELVM v)
{
sq_pushstring(v,_SC("regexp"),-1);
sq_newclass(v,SQFalse);
SQInteger i = 0;
while(rexobj_funcs[i].name != 0) {
const SQRegFunction &f = rexobj_funcs[i];
sq_pushstring(v,f.name,-1);
sq_newclosure(v,f.f,0);
sq_setparamscheck(v,f.nparamscheck,f.typemask);
sq_setnativeclosurename(v,-1,f.name);
sq_newslot(v,-3,SQFalse);
i++;
}
sq_newslot(v,-3,SQFalse);
i = 0;
while(stringlib_funcs[i].name!=0)
{
sq_pushstring(v,stringlib_funcs[i].name,-1);
sq_newclosure(v,stringlib_funcs[i].f,0);
sq_setparamscheck(v,stringlib_funcs[i].nparamscheck,stringlib_funcs[i].typemask);
sq_setnativeclosurename(v,-1,stringlib_funcs[i].name);
sq_newslot(v,-3,SQFalse);
i++;
}
return 1;
}

146
external/Squirrel/Lib/sqstdsystem.cpp vendored Normal file
View File

@ -0,0 +1,146 @@
/* see copyright notice in squirrel.h */
#include <squirrel.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <sqstdsystem.h>
#ifdef SQUNICODE
#include <wchar.h>
#define scgetenv _wgetenv
#define scsystem _wsystem
#define scasctime _wasctime
#define scremove _wremove
#define screname _wrename
#else
#define scgetenv getenv
#define scsystem system
#define scasctime asctime
#define scremove remove
#define screname rename
#endif
static SQInteger _system_getenv(HSQUIRRELVM v)
{
const SQChar *s;
if(SQ_SUCCEEDED(sq_getstring(v,2,&s))){
sq_pushstring(v,scgetenv(s),-1);
return 1;
}
return 0;
}
static SQInteger _system_system(HSQUIRRELVM v)
{
const SQChar *s;
if(SQ_SUCCEEDED(sq_getstring(v,2,&s))){
sq_pushinteger(v,scsystem(s));
return 1;
}
return sq_throwerror(v,_SC("wrong param"));
}
static SQInteger _system_clock(HSQUIRRELVM v)
{
sq_pushfloat(v,((SQFloat)clock())/(SQFloat)CLOCKS_PER_SEC);
return 1;
}
static SQInteger _system_time(HSQUIRRELVM v)
{
SQInteger t = (SQInteger)time(NULL);
sq_pushinteger(v,t);
return 1;
}
static SQInteger _system_remove(HSQUIRRELVM v)
{
const SQChar *s;
sq_getstring(v,2,&s);
if(scremove(s)==-1)
return sq_throwerror(v,_SC("remove() failed"));
return 0;
}
static SQInteger _system_rename(HSQUIRRELVM v)
{
const SQChar *oldn,*newn;
sq_getstring(v,2,&oldn);
sq_getstring(v,3,&newn);
if(screname(oldn,newn)==-1)
return sq_throwerror(v,_SC("rename() failed"));
return 0;
}
static void _set_integer_slot(HSQUIRRELVM v,const SQChar *name,SQInteger val)
{
sq_pushstring(v,name,-1);
sq_pushinteger(v,val);
sq_rawset(v,-3);
}
static SQInteger _system_date(HSQUIRRELVM v)
{
time_t t;
SQInteger it;
SQInteger format = 'l';
if(sq_gettop(v) > 1) {
sq_getinteger(v,2,&it);
t = it;
if(sq_gettop(v) > 2) {
sq_getinteger(v,3,(SQInteger*)&format);
}
}
else {
time(&t);
}
tm *date;
if(format == 'u')
date = gmtime(&t);
else
date = localtime(&t);
if(!date)
return sq_throwerror(v,_SC("crt api failure"));
sq_newtable(v);
_set_integer_slot(v, _SC("sec"), date->tm_sec);
_set_integer_slot(v, _SC("min"), date->tm_min);
_set_integer_slot(v, _SC("hour"), date->tm_hour);
_set_integer_slot(v, _SC("day"), date->tm_mday);
_set_integer_slot(v, _SC("month"), date->tm_mon);
_set_integer_slot(v, _SC("year"), date->tm_year+1900);
_set_integer_slot(v, _SC("wday"), date->tm_wday);
_set_integer_slot(v, _SC("yday"), date->tm_yday);
return 1;
}
#define _DECL_FUNC(name,nparams,pmask) {_SC(#name),_system_##name,nparams,pmask}
static const SQRegFunction systemlib_funcs[]={
_DECL_FUNC(getenv,2,_SC(".s")),
_DECL_FUNC(system,2,_SC(".s")),
_DECL_FUNC(clock,0,NULL),
_DECL_FUNC(time,1,NULL),
_DECL_FUNC(date,-1,_SC(".nn")),
_DECL_FUNC(remove,2,_SC(".s")),
_DECL_FUNC(rename,3,_SC(".ss")),
{NULL,(SQFUNCTION)0,0,NULL}
};
#undef _DECL_FUNC
SQInteger sqstd_register_systemlib(HSQUIRRELVM v)
{
SQInteger i=0;
while(systemlib_funcs[i].name!=0)
{
sq_pushstring(v,systemlib_funcs[i].name,-1);
sq_newclosure(v,systemlib_funcs[i].f,0);
sq_setparamscheck(v,systemlib_funcs[i].nparamscheck,systemlib_funcs[i].typemask);
sq_setnativeclosurename(v,-1,systemlib_funcs[i].name);
sq_newslot(v,-3,SQFalse);
i++;
}
return 1;
}

File diff suppressed because it is too large Load Diff

View File

@ -1,94 +1,94 @@
/* see copyright notice in squirrel.h */
/* see copyright notice in squirrel.h */
#ifndef _SQARRAY_H_
#define _SQARRAY_H_
struct SQArray : public CHAINABLE_OBJ
{
private:
SQArray(SQSharedState *ss,SQInteger nsize){_values.resize(nsize); INIT_CHAIN();ADD_TO_CHAIN(&_ss(this)->_gc_chain,this);}
~SQArray()
{
REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this);
}
SQArray(SQSharedState *ss,SQInteger nsize){_values.resize(nsize); INIT_CHAIN();ADD_TO_CHAIN(&_ss(this)->_gc_chain,this);}
~SQArray()
{
REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this);
}
public:
static SQArray* Create(SQSharedState *ss,SQInteger nInitialSize){
SQArray *newarray=(SQArray*)SQ_MALLOC(sizeof(SQArray));
new (newarray) SQArray(ss,nInitialSize);
return newarray;
}
static SQArray* Create(SQSharedState *ss,SQInteger nInitialSize){
SQArray *newarray=(SQArray*)SQ_MALLOC(sizeof(SQArray));
new (newarray) SQArray(ss,nInitialSize);
return newarray;
}
#ifndef NO_GARBAGE_COLLECTOR
void Mark(SQCollectable **chain);
SQObjectType GetType() {return OT_ARRAY;}
void Mark(SQCollectable **chain);
SQObjectType GetType() {return OT_ARRAY;}
#endif
void Finalize(){
_values.resize(0);
}
bool Get(const SQInteger nidx,SQObjectPtr &val)
{
if(nidx>=0 && nidx<(SQInteger)_values.size()){
SQObjectPtr &o = _values[nidx];
val = _realval(o);
return true;
}
else return false;
}
bool Set(const SQInteger nidx,const SQObjectPtr &val)
{
if(nidx>=0 && nidx<(SQInteger)_values.size()){
_values[nidx]=val;
return true;
}
else return false;
}
SQInteger Next(const SQObjectPtr &refpos,SQObjectPtr &outkey,SQObjectPtr &outval)
{
SQUnsignedInteger idx=TranslateIndex(refpos);
while(idx<_values.size()){
//first found
outkey=(SQInteger)idx;
SQObjectPtr &o = _values[idx];
outval = _realval(o);
//return idx for the next iteration
return ++idx;
}
//nothing to iterate anymore
return -1;
}
SQArray *Clone(){SQArray *anew=Create(_opt_ss(this),0); anew->_values.copy(_values); return anew; }
SQInteger Size() const {return _values.size();}
void Resize(SQInteger size)
{
SQObjectPtr _null;
Resize(size,_null);
}
void Resize(SQInteger size,SQObjectPtr &fill) { _values.resize(size,fill); ShrinkIfNeeded(); }
void Reserve(SQInteger size) { _values.reserve(size); }
void Append(const SQObject &o){_values.push_back(o);}
void Extend(const SQArray *a);
SQObjectPtr &Top(){return _values.top();}
void Pop(){_values.pop_back(); ShrinkIfNeeded(); }
bool Insert(SQInteger idx,const SQObject &val){
if(idx < 0 || idx > (SQInteger)_values.size())
return false;
_values.insert(idx,val);
return true;
}
void ShrinkIfNeeded() {
if(_values.size() <= _values.capacity()>>2) //shrink the array
_values.shrinktofit();
}
bool Remove(SQInteger idx){
if(idx < 0 || idx >= (SQInteger)_values.size())
return false;
_values.remove(idx);
ShrinkIfNeeded();
return true;
}
void Release()
{
sq_delete(this,SQArray);
}
void Finalize(){
_values.resize(0);
}
bool Get(const SQInteger nidx,SQObjectPtr &val)
{
if(nidx>=0 && nidx<(SQInteger)_values.size()){
SQObjectPtr &o = _values[nidx];
val = _realval(o);
return true;
}
else return false;
}
bool Set(const SQInteger nidx,const SQObjectPtr &val)
{
if(nidx>=0 && nidx<(SQInteger)_values.size()){
_values[nidx]=val;
return true;
}
else return false;
}
SQInteger Next(const SQObjectPtr &refpos,SQObjectPtr &outkey,SQObjectPtr &outval)
{
SQUnsignedInteger idx=TranslateIndex(refpos);
while(idx<_values.size()){
//first found
outkey=(SQInteger)idx;
SQObjectPtr &o = _values[idx];
outval = _realval(o);
//return idx for the next iteration
return ++idx;
}
//nothing to iterate anymore
return -1;
}
SQArray *Clone(){SQArray *anew=Create(_opt_ss(this),0); anew->_values.copy(_values); return anew; }
SQInteger Size() const {return _values.size();}
void Resize(SQInteger size)
{
SQObjectPtr _null;
Resize(size,_null);
}
void Resize(SQInteger size,SQObjectPtr &fill) { _values.resize(size,fill); ShrinkIfNeeded(); }
void Reserve(SQInteger size) { _values.reserve(size); }
void Append(const SQObject &o){_values.push_back(o);}
void Extend(const SQArray *a);
SQObjectPtr &Top(){return _values.top();}
void Pop(){_values.pop_back(); ShrinkIfNeeded(); }
bool Insert(SQInteger idx,const SQObject &val){
if(idx < 0 || idx > (SQInteger)_values.size())
return false;
_values.insert(idx,val);
return true;
}
void ShrinkIfNeeded() {
if(_values.size() <= _values.capacity()>>2) //shrink the array
_values.shrinktofit();
}
bool Remove(SQInteger idx){
if(idx < 0 || idx >= (SQInteger)_values.size())
return false;
_values.remove(idx);
ShrinkIfNeeded();
return true;
}
void Release()
{
sq_delete(this,SQArray);
}
SQObjectPtrVec _values;
SQObjectPtrVec _values;
};
#endif //_SQARRAY_H_

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,5 @@
/*
see copyright notice in squirrel.h
see copyright notice in squirrel.h
*/
#include "sqpcheader.h"
#include "sqvm.h"
@ -12,199 +12,199 @@
SQClass::SQClass(SQSharedState *ss,SQClass *base)
{
_base = base;
_typetag = 0;
_hook = NULL;
_udsize = 0;
_locked = false;
_constructoridx = -1;
if(_base) {
_constructoridx = _base->_constructoridx;
_udsize = _base->_udsize;
_defaultvalues.copy(base->_defaultvalues);
_methods.copy(base->_methods);
_COPY_VECTOR(_metamethods,base->_metamethods,MT_LAST);
__ObjAddRef(_base);
}
_members = base?base->_members->Clone() : SQTable::Create(ss,0);
__ObjAddRef(_members);
_base = base;
_typetag = 0;
_hook = NULL;
_udsize = 0;
_locked = false;
_constructoridx = -1;
if(_base) {
_constructoridx = _base->_constructoridx;
_udsize = _base->_udsize;
_defaultvalues.copy(base->_defaultvalues);
_methods.copy(base->_methods);
_COPY_VECTOR(_metamethods,base->_metamethods,MT_LAST);
__ObjAddRef(_base);
}
_members = base?base->_members->Clone() : SQTable::Create(ss,0);
__ObjAddRef(_members);
INIT_CHAIN();
ADD_TO_CHAIN(&_sharedstate->_gc_chain, this);
INIT_CHAIN();
ADD_TO_CHAIN(&_sharedstate->_gc_chain, this);
}
void SQClass::Finalize() {
_attributes.Null();
_NULL_SQOBJECT_VECTOR(_defaultvalues,_defaultvalues.size());
_methods.resize(0);
_NULL_SQOBJECT_VECTOR(_metamethods,MT_LAST);
__ObjRelease(_members);
if(_base) {
__ObjRelease(_base);
}
_attributes.Null();
_NULL_SQOBJECT_VECTOR(_defaultvalues,_defaultvalues.size());
_methods.resize(0);
_NULL_SQOBJECT_VECTOR(_metamethods,MT_LAST);
__ObjRelease(_members);
if(_base) {
__ObjRelease(_base);
}
}
SQClass::~SQClass()
{
REMOVE_FROM_CHAIN(&_sharedstate->_gc_chain, this);
Finalize();
REMOVE_FROM_CHAIN(&_sharedstate->_gc_chain, this);
Finalize();
}
bool SQClass::NewSlot(SQSharedState *ss,const SQObjectPtr &key,const SQObjectPtr &val,bool bstatic)
{
SQObjectPtr temp;
bool belongs_to_static_table = type(val) == OT_CLOSURE || type(val) == OT_NATIVECLOSURE || bstatic;
if(_locked && !belongs_to_static_table)
return false; //the class already has an instance so cannot be modified
if(_members->Get(key,temp) && _isfield(temp)) //overrides the default value
{
_defaultvalues[_member_idx(temp)].val = val;
return true;
}
if(belongs_to_static_table) {
SQInteger mmidx;
if((type(val) == OT_CLOSURE || type(val) == OT_NATIVECLOSURE) &&
(mmidx = ss->GetMetaMethodIdxByName(key)) != -1) {
_metamethods[mmidx] = val;
}
else {
SQObjectPtr theval = val;
if(_base && type(val) == OT_CLOSURE) {
theval = _closure(val)->Clone();
_closure(theval)->_base = _base;
__ObjAddRef(_base); //ref for the closure
}
if(type(temp) == OT_NULL) {
bool isconstructor;
SQVM::IsEqual(ss->_constructoridx, key, isconstructor);
if(isconstructor) {
_constructoridx = (SQInteger)_methods.size();
}
SQClassMember m;
m.val = theval;
_members->NewSlot(key,SQObjectPtr(_make_method_idx(_methods.size())));
_methods.push_back(m);
}
else {
_methods[_member_idx(temp)].val = theval;
}
}
return true;
}
SQClassMember m;
m.val = val;
_members->NewSlot(key,SQObjectPtr(_make_field_idx(_defaultvalues.size())));
_defaultvalues.push_back(m);
return true;
SQObjectPtr temp;
bool belongs_to_static_table = type(val) == OT_CLOSURE || type(val) == OT_NATIVECLOSURE || bstatic;
if(_locked && !belongs_to_static_table)
return false; //the class already has an instance so cannot be modified
if(_members->Get(key,temp) && _isfield(temp)) //overrides the default value
{
_defaultvalues[_member_idx(temp)].val = val;
return true;
}
if(belongs_to_static_table) {
SQInteger mmidx;
if((type(val) == OT_CLOSURE || type(val) == OT_NATIVECLOSURE) &&
(mmidx = ss->GetMetaMethodIdxByName(key)) != -1) {
_metamethods[mmidx] = val;
}
else {
SQObjectPtr theval = val;
if(_base && type(val) == OT_CLOSURE) {
theval = _closure(val)->Clone();
_closure(theval)->_base = _base;
__ObjAddRef(_base); //ref for the closure
}
if(type(temp) == OT_NULL) {
bool isconstructor;
SQVM::IsEqual(ss->_constructoridx, key, isconstructor);
if(isconstructor) {
_constructoridx = (SQInteger)_methods.size();
}
SQClassMember m;
m.val = theval;
_members->NewSlot(key,SQObjectPtr(_make_method_idx(_methods.size())));
_methods.push_back(m);
}
else {
_methods[_member_idx(temp)].val = theval;
}
}
return true;
}
SQClassMember m;
m.val = val;
_members->NewSlot(key,SQObjectPtr(_make_field_idx(_defaultvalues.size())));
_defaultvalues.push_back(m);
return true;
}
SQInstance *SQClass::CreateInstance()
{
if(!_locked) Lock();
return SQInstance::Create(_opt_ss(this),this);
if(!_locked) Lock();
return SQInstance::Create(_opt_ss(this),this);
}
SQInteger SQClass::Next(const SQObjectPtr &refpos, SQObjectPtr &outkey, SQObjectPtr &outval)
{
SQObjectPtr oval;
SQInteger idx = _members->Next(false,refpos,outkey,oval);
if(idx != -1) {
if(_ismethod(oval)) {
outval = _methods[_member_idx(oval)].val;
}
else {
SQObjectPtr &o = _defaultvalues[_member_idx(oval)].val;
outval = _realval(o);
}
}
return idx;
SQObjectPtr oval;
SQInteger idx = _members->Next(false,refpos,outkey,oval);
if(idx != -1) {
if(_ismethod(oval)) {
outval = _methods[_member_idx(oval)].val;
}
else {
SQObjectPtr &o = _defaultvalues[_member_idx(oval)].val;
outval = _realval(o);
}
}
return idx;
}
bool SQClass::SetAttributes(const SQObjectPtr &key,const SQObjectPtr &val)
{
SQObjectPtr idx;
if(_members->Get(key,idx)) {
if(_isfield(idx))
_defaultvalues[_member_idx(idx)].attrs = val;
else
_methods[_member_idx(idx)].attrs = val;
return true;
}
return false;
SQObjectPtr idx;
if(_members->Get(key,idx)) {
if(_isfield(idx))
_defaultvalues[_member_idx(idx)].attrs = val;
else
_methods[_member_idx(idx)].attrs = val;
return true;
}
return false;
}
bool SQClass::GetAttributes(const SQObjectPtr &key,SQObjectPtr &outval)
{
SQObjectPtr idx;
if(_members->Get(key,idx)) {
outval = (_isfield(idx)?_defaultvalues[_member_idx(idx)].attrs:_methods[_member_idx(idx)].attrs);
return true;
}
return false;
SQObjectPtr idx;
if(_members->Get(key,idx)) {
outval = (_isfield(idx)?_defaultvalues[_member_idx(idx)].attrs:_methods[_member_idx(idx)].attrs);
return true;
}
return false;
}
///////////////////////////////////////////////////////////////////////
void SQInstance::Init(SQSharedState *ss)
{
_userpointer = NULL;
_hook = NULL;
__ObjAddRef(_class);
_delegate = _class->_members;
INIT_CHAIN();
ADD_TO_CHAIN(&_sharedstate->_gc_chain, this);
_userpointer = NULL;
_hook = NULL;
__ObjAddRef(_class);
_delegate = _class->_members;
INIT_CHAIN();
ADD_TO_CHAIN(&_sharedstate->_gc_chain, this);
}
SQInstance::SQInstance(SQSharedState *ss, SQClass *c, SQInteger memsize)
{
_memsize = memsize;
_class = c;
SQUnsignedInteger nvalues = _class->_defaultvalues.size();
for(SQUnsignedInteger n = 0; n < nvalues; n++) {
new (&_values[n]) SQObjectPtr(_class->_defaultvalues[n].val);
}
Init(ss);
_memsize = memsize;
_class = c;
SQUnsignedInteger nvalues = _class->_defaultvalues.size();
for(SQUnsignedInteger n = 0; n < nvalues; n++) {
new (&_values[n]) SQObjectPtr(_class->_defaultvalues[n].val);
}
Init(ss);
}
SQInstance::SQInstance(SQSharedState *ss, SQInstance *i, SQInteger memsize)
{
_memsize = memsize;
_class = i->_class;
SQUnsignedInteger nvalues = _class->_defaultvalues.size();
for(SQUnsignedInteger n = 0; n < nvalues; n++) {
new (&_values[n]) SQObjectPtr(i->_values[n]);
}
Init(ss);
_memsize = memsize;
_class = i->_class;
SQUnsignedInteger nvalues = _class->_defaultvalues.size();
for(SQUnsignedInteger n = 0; n < nvalues; n++) {
new (&_values[n]) SQObjectPtr(i->_values[n]);
}
Init(ss);
}
void SQInstance::Finalize()
{
SQUnsignedInteger nvalues = _class->_defaultvalues.size();
__ObjRelease(_class);
_NULL_SQOBJECT_VECTOR(_values,nvalues);
SQUnsignedInteger nvalues = _class->_defaultvalues.size();
__ObjRelease(_class);
_NULL_SQOBJECT_VECTOR(_values,nvalues);
}
SQInstance::~SQInstance()
{
REMOVE_FROM_CHAIN(&_sharedstate->_gc_chain, this);
if(_class){ Finalize(); } //if _class is null it was already finalized by the GC
REMOVE_FROM_CHAIN(&_sharedstate->_gc_chain, this);
if(_class){ Finalize(); } //if _class is null it was already finalized by the GC
}
bool SQInstance::GetMetaMethod(SQVM *v,SQMetaMethod mm,SQObjectPtr &res)
bool SQInstance::GetMetaMethod(SQVM SQ_UNUSED_ARG(*v),SQMetaMethod mm,SQObjectPtr &res)
{
if(type(_class->_metamethods[mm]) != OT_NULL) {
res = _class->_metamethods[mm];
return true;
}
return false;
if(type(_class->_metamethods[mm]) != OT_NULL) {
res = _class->_metamethods[mm];
return true;
}
return false;
}
bool SQInstance::InstanceOf(SQClass *trg)
{
SQClass *parent = _class;
while(parent != NULL) {
if(parent == trg)
return true;
parent = parent->_base;
}
return false;
SQClass *parent = _class;
while(parent != NULL) {
if(parent == trg)
return true;
parent = parent->_base;
}
return false;
}

View File

@ -1,16 +1,16 @@
/* see copyright notice in squirrel.h */
/* see copyright notice in squirrel.h */
#ifndef _SQCLASS_H_
#define _SQCLASS_H_
struct SQInstance;
struct SQClassMember {
SQObjectPtr val;
SQObjectPtr attrs;
void Null() {
val.Null();
attrs.Null();
}
SQObjectPtr val;
SQObjectPtr attrs;
void Null() {
val.Null();
attrs.Null();
}
};
typedef sqvector<SQClassMember> SQClassMemberVec;
@ -27,136 +27,136 @@ typedef sqvector<SQClassMember> SQClassMemberVec;
struct SQClass : public CHAINABLE_OBJ
{
SQClass(SQSharedState *ss,SQClass *base);
SQClass(SQSharedState *ss,SQClass *base);
public:
static SQClass* Create(SQSharedState *ss,SQClass *base) {
SQClass *newclass = (SQClass *)SQ_MALLOC(sizeof(SQClass));
new (newclass) SQClass(ss, base);
return newclass;
}
~SQClass();
bool NewSlot(SQSharedState *ss, const SQObjectPtr &key,const SQObjectPtr &val,bool bstatic);
bool Get(const SQObjectPtr &key,SQObjectPtr &val) {
if(_members->Get(key,val)) {
if(_isfield(val)) {
SQObjectPtr &o = _defaultvalues[_member_idx(val)].val;
val = _realval(o);
}
else {
val = _methods[_member_idx(val)].val;
}
return true;
}
return false;
}
bool GetConstructor(SQObjectPtr &ctor)
{
if(_constructoridx != -1) {
ctor = _methods[_constructoridx].val;
return true;
}
return false;
}
bool SetAttributes(const SQObjectPtr &key,const SQObjectPtr &val);
bool GetAttributes(const SQObjectPtr &key,SQObjectPtr &outval);
void Lock() { _locked = true; if(_base) _base->Lock(); }
void Release() {
if (_hook) { _hook(_typetag,0);}
sq_delete(this, SQClass);
}
void Finalize();
static SQClass* Create(SQSharedState *ss,SQClass *base) {
SQClass *newclass = (SQClass *)SQ_MALLOC(sizeof(SQClass));
new (newclass) SQClass(ss, base);
return newclass;
}
~SQClass();
bool NewSlot(SQSharedState *ss, const SQObjectPtr &key,const SQObjectPtr &val,bool bstatic);
bool Get(const SQObjectPtr &key,SQObjectPtr &val) {
if(_members->Get(key,val)) {
if(_isfield(val)) {
SQObjectPtr &o = _defaultvalues[_member_idx(val)].val;
val = _realval(o);
}
else {
val = _methods[_member_idx(val)].val;
}
return true;
}
return false;
}
bool GetConstructor(SQObjectPtr &ctor)
{
if(_constructoridx != -1) {
ctor = _methods[_constructoridx].val;
return true;
}
return false;
}
bool SetAttributes(const SQObjectPtr &key,const SQObjectPtr &val);
bool GetAttributes(const SQObjectPtr &key,SQObjectPtr &outval);
void Lock() { _locked = true; if(_base) _base->Lock(); }
void Release() {
if (_hook) { _hook(_typetag,0);}
sq_delete(this, SQClass);
}
void Finalize();
#ifndef NO_GARBAGE_COLLECTOR
void Mark(SQCollectable ** );
SQObjectType GetType() {return OT_CLASS;}
void Mark(SQCollectable ** );
SQObjectType GetType() {return OT_CLASS;}
#endif
SQInteger Next(const SQObjectPtr &refpos, SQObjectPtr &outkey, SQObjectPtr &outval);
SQInstance *CreateInstance();
SQTable *_members;
SQClass *_base;
SQClassMemberVec _defaultvalues;
SQClassMemberVec _methods;
SQObjectPtr _metamethods[MT_LAST];
SQObjectPtr _attributes;
SQUserPointer _typetag;
SQRELEASEHOOK _hook;
bool _locked;
SQInteger _constructoridx;
SQInteger _udsize;
SQInteger Next(const SQObjectPtr &refpos, SQObjectPtr &outkey, SQObjectPtr &outval);
SQInstance *CreateInstance();
SQTable *_members;
SQClass *_base;
SQClassMemberVec _defaultvalues;
SQClassMemberVec _methods;
SQObjectPtr _metamethods[MT_LAST];
SQObjectPtr _attributes;
SQUserPointer _typetag;
SQRELEASEHOOK _hook;
bool _locked;
SQInteger _constructoridx;
SQInteger _udsize;
};
#define calcinstancesize(_theclass_) \
(_theclass_->_udsize + sq_aligning(sizeof(SQInstance) + (sizeof(SQObjectPtr)*(_theclass_->_defaultvalues.size()>0?_theclass_->_defaultvalues.size()-1:0))))
(_theclass_->_udsize + sq_aligning(sizeof(SQInstance) + (sizeof(SQObjectPtr)*(_theclass_->_defaultvalues.size()>0?_theclass_->_defaultvalues.size()-1:0))))
struct SQInstance : public SQDelegable
{
void Init(SQSharedState *ss);
SQInstance(SQSharedState *ss, SQClass *c, SQInteger memsize);
SQInstance(SQSharedState *ss, SQInstance *c, SQInteger memsize);
void Init(SQSharedState *ss);
SQInstance(SQSharedState *ss, SQClass *c, SQInteger memsize);
SQInstance(SQSharedState *ss, SQInstance *c, SQInteger memsize);
public:
static SQInstance* Create(SQSharedState *ss,SQClass *theclass) {
static SQInstance* Create(SQSharedState *ss,SQClass *theclass) {
SQInteger size = calcinstancesize(theclass);
SQInstance *newinst = (SQInstance *)SQ_MALLOC(size);
new (newinst) SQInstance(ss, theclass,size);
if(theclass->_udsize) {
newinst->_userpointer = ((unsigned char *)newinst) + (size - theclass->_udsize);
}
return newinst;
}
SQInstance *Clone(SQSharedState *ss)
{
SQInteger size = calcinstancesize(_class);
SQInstance *newinst = (SQInstance *)SQ_MALLOC(size);
new (newinst) SQInstance(ss, this,size);
if(_class->_udsize) {
newinst->_userpointer = ((unsigned char *)newinst) + (size - _class->_udsize);
}
return newinst;
}
~SQInstance();
bool Get(const SQObjectPtr &key,SQObjectPtr &val) {
if(_class->_members->Get(key,val)) {
if(_isfield(val)) {
SQObjectPtr &o = _values[_member_idx(val)];
val = _realval(o);
}
else {
val = _class->_methods[_member_idx(val)].val;
}
return true;
}
return false;
}
bool Set(const SQObjectPtr &key,const SQObjectPtr &val) {
SQObjectPtr idx;
if(_class->_members->Get(key,idx) && _isfield(idx)) {
SQInteger size = calcinstancesize(theclass);
SQInstance *newinst = (SQInstance *)SQ_MALLOC(size);
new (newinst) SQInstance(ss, theclass,size);
if(theclass->_udsize) {
newinst->_userpointer = ((unsigned char *)newinst) + (size - theclass->_udsize);
}
return newinst;
}
SQInstance *Clone(SQSharedState *ss)
{
SQInteger size = calcinstancesize(_class);
SQInstance *newinst = (SQInstance *)SQ_MALLOC(size);
new (newinst) SQInstance(ss, this,size);
if(_class->_udsize) {
newinst->_userpointer = ((unsigned char *)newinst) + (size - _class->_udsize);
}
return newinst;
}
~SQInstance();
bool Get(const SQObjectPtr &key,SQObjectPtr &val) {
if(_class->_members->Get(key,val)) {
if(_isfield(val)) {
SQObjectPtr &o = _values[_member_idx(val)];
val = _realval(o);
}
else {
val = _class->_methods[_member_idx(val)].val;
}
return true;
}
return false;
}
bool Set(const SQObjectPtr &key,const SQObjectPtr &val) {
SQObjectPtr idx;
if(_class->_members->Get(key,idx) && _isfield(idx)) {
_values[_member_idx(idx)] = val;
return true;
}
return false;
}
void Release() {
_uiRef++;
if (_hook) { _hook(_userpointer,0);}
_uiRef--;
if(_uiRef > 0) return;
SQInteger size = _memsize;
this->~SQInstance();
SQ_FREE(this, size);
}
void Finalize();
return true;
}
return false;
}
void Release() {
_uiRef++;
if (_hook) { _hook(_userpointer,0);}
_uiRef--;
if(_uiRef > 0) return;
SQInteger size = _memsize;
this->~SQInstance();
SQ_FREE(this, size);
}
void Finalize();
#ifndef NO_GARBAGE_COLLECTOR
void Mark(SQCollectable ** );
SQObjectType GetType() {return OT_INSTANCE;}
void Mark(SQCollectable ** );
SQObjectType GetType() {return OT_INSTANCE;}
#endif
bool InstanceOf(SQClass *trg);
bool GetMetaMethod(SQVM *v,SQMetaMethod mm,SQObjectPtr &res);
bool InstanceOf(SQClass *trg);
bool GetMetaMethod(SQVM *v,SQMetaMethod mm,SQObjectPtr &res);
SQClass *_class;
SQUserPointer _userpointer;
SQRELEASEHOOK _hook;
SQInteger _memsize;
SQObjectPtr _values[1];
SQClass *_class;
SQUserPointer _userpointer;
SQRELEASEHOOK _hook;
SQInteger _memsize;
SQObjectPtr _values[1];
};
#endif //_SQCLASS_H_

View File

@ -1,4 +1,4 @@
/* see copyright notice in squirrel.h */
/* see copyright notice in squirrel.h */
#ifndef _SQCLOSURE_H_
#define _SQCLOSURE_H_
@ -10,64 +10,64 @@ struct SQClass;
struct SQClosure : public CHAINABLE_OBJ
{
private:
SQClosure(SQSharedState *ss,SQFunctionProto *func){_function = func; __ObjAddRef(_function); _base = NULL; INIT_CHAIN();ADD_TO_CHAIN(&_ss(this)->_gc_chain,this); _env = NULL; _root=NULL;}
SQClosure(SQSharedState *ss,SQFunctionProto *func){_function = func; __ObjAddRef(_function); _base = NULL; INIT_CHAIN();ADD_TO_CHAIN(&_ss(this)->_gc_chain,this); _env = NULL; _root=NULL;}
public:
static SQClosure *Create(SQSharedState *ss,SQFunctionProto *func,SQWeakRef *root){
SQInteger size = _CALC_CLOSURE_SIZE(func);
SQClosure *nc=(SQClosure*)SQ_MALLOC(size);
new (nc) SQClosure(ss,func);
nc->_outervalues = (SQObjectPtr *)(nc + 1);
nc->_defaultparams = &nc->_outervalues[func->_noutervalues];
nc->_root = root;
__ObjAddRef(nc->_root);
_CONSTRUCT_VECTOR(SQObjectPtr,func->_noutervalues,nc->_outervalues);
_CONSTRUCT_VECTOR(SQObjectPtr,func->_ndefaultparams,nc->_defaultparams);
return nc;
}
void Release(){
SQFunctionProto *f = _function;
SQInteger size = _CALC_CLOSURE_SIZE(f);
_DESTRUCT_VECTOR(SQObjectPtr,f->_noutervalues,_outervalues);
_DESTRUCT_VECTOR(SQObjectPtr,f->_ndefaultparams,_defaultparams);
__ObjRelease(_function);
this->~SQClosure();
sq_vm_free(this,size);
}
void SetRoot(SQWeakRef *r)
{
__ObjRelease(_root);
_root = r;
__ObjAddRef(_root);
}
SQClosure *Clone()
{
SQFunctionProto *f = _function;
SQClosure * ret = SQClosure::Create(_opt_ss(this),f,_root);
ret->_env = _env;
if(ret->_env) __ObjAddRef(ret->_env);
_COPY_VECTOR(ret->_outervalues,_outervalues,f->_noutervalues);
_COPY_VECTOR(ret->_defaultparams,_defaultparams,f->_ndefaultparams);
return ret;
}
~SQClosure();
static SQClosure *Create(SQSharedState *ss,SQFunctionProto *func,SQWeakRef *root){
SQInteger size = _CALC_CLOSURE_SIZE(func);
SQClosure *nc=(SQClosure*)SQ_MALLOC(size);
new (nc) SQClosure(ss,func);
nc->_outervalues = (SQObjectPtr *)(nc + 1);
nc->_defaultparams = &nc->_outervalues[func->_noutervalues];
nc->_root = root;
__ObjAddRef(nc->_root);
_CONSTRUCT_VECTOR(SQObjectPtr,func->_noutervalues,nc->_outervalues);
_CONSTRUCT_VECTOR(SQObjectPtr,func->_ndefaultparams,nc->_defaultparams);
return nc;
}
void Release(){
SQFunctionProto *f = _function;
SQInteger size = _CALC_CLOSURE_SIZE(f);
_DESTRUCT_VECTOR(SQObjectPtr,f->_noutervalues,_outervalues);
_DESTRUCT_VECTOR(SQObjectPtr,f->_ndefaultparams,_defaultparams);
__ObjRelease(_function);
this->~SQClosure();
sq_vm_free(this,size);
}
void SetRoot(SQWeakRef *r)
{
__ObjRelease(_root);
_root = r;
__ObjAddRef(_root);
}
SQClosure *Clone()
{
SQFunctionProto *f = _function;
SQClosure * ret = SQClosure::Create(_opt_ss(this),f,_root);
ret->_env = _env;
if(ret->_env) __ObjAddRef(ret->_env);
_COPY_VECTOR(ret->_outervalues,_outervalues,f->_noutervalues);
_COPY_VECTOR(ret->_defaultparams,_defaultparams,f->_ndefaultparams);
return ret;
}
~SQClosure();
bool Save(SQVM *v,SQUserPointer up,SQWRITEFUNC write);
static bool Load(SQVM *v,SQUserPointer up,SQREADFUNC read,SQObjectPtr &ret);
bool Save(SQVM *v,SQUserPointer up,SQWRITEFUNC write);
static bool Load(SQVM *v,SQUserPointer up,SQREADFUNC read,SQObjectPtr &ret);
#ifndef NO_GARBAGE_COLLECTOR
void Mark(SQCollectable **chain);
void Finalize(){
SQFunctionProto *f = _function;
_NULL_SQOBJECT_VECTOR(_outervalues,f->_noutervalues);
_NULL_SQOBJECT_VECTOR(_defaultparams,f->_ndefaultparams);
}
SQObjectType GetType() {return OT_CLOSURE;}
void Mark(SQCollectable **chain);
void Finalize(){
SQFunctionProto *f = _function;
_NULL_SQOBJECT_VECTOR(_outervalues,f->_noutervalues);
_NULL_SQOBJECT_VECTOR(_defaultparams,f->_ndefaultparams);
}
SQObjectType GetType() {return OT_CLOSURE;}
#endif
SQWeakRef *_env;
SQWeakRef *_root;
SQClass *_base;
SQFunctionProto *_function;
SQObjectPtr *_outervalues;
SQObjectPtr *_defaultparams;
SQWeakRef *_env;
SQWeakRef *_root;
SQClass *_base;
SQFunctionProto *_function;
SQObjectPtr *_outervalues;
SQObjectPtr *_defaultparams;
};
//////////////////////////////////////////////
@ -75,71 +75,71 @@ struct SQOuter : public CHAINABLE_OBJ
{
private:
SQOuter(SQSharedState *ss, SQObjectPtr *outer){_valptr = outer; _next = NULL; INIT_CHAIN(); ADD_TO_CHAIN(&_ss(this)->_gc_chain,this); }
SQOuter(SQSharedState *ss, SQObjectPtr *outer){_valptr = outer; _next = NULL; INIT_CHAIN(); ADD_TO_CHAIN(&_ss(this)->_gc_chain,this); }
public:
static SQOuter *Create(SQSharedState *ss, SQObjectPtr *outer)
{
SQOuter *nc = (SQOuter*)SQ_MALLOC(sizeof(SQOuter));
new (nc) SQOuter(ss, outer);
return nc;
}
~SQOuter() { REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this); }
static SQOuter *Create(SQSharedState *ss, SQObjectPtr *outer)
{
SQOuter *nc = (SQOuter*)SQ_MALLOC(sizeof(SQOuter));
new (nc) SQOuter(ss, outer);
return nc;
}
~SQOuter() { REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this); }
void Release()
{
this->~SQOuter();
sq_vm_free(this,sizeof(SQOuter));
}
void Release()
{
this->~SQOuter();
sq_vm_free(this,sizeof(SQOuter));
}
#ifndef NO_GARBAGE_COLLECTOR
void Mark(SQCollectable **chain);
void Finalize() { _value.Null(); }
SQObjectType GetType() {return OT_OUTER;}
void Mark(SQCollectable **chain);
void Finalize() { _value.Null(); }
SQObjectType GetType() {return OT_OUTER;}
#endif
SQObjectPtr *_valptr; /* pointer to value on stack, or _value below */
SQInteger _idx; /* idx in stack array, for relocation */
SQObjectPtr _value; /* value of outer after stack frame is closed */
SQOuter *_next; /* pointer to next outer when frame is open */
SQObjectPtr *_valptr; /* pointer to value on stack, or _value below */
SQInteger _idx; /* idx in stack array, for relocation */
SQObjectPtr _value; /* value of outer after stack frame is closed */
SQOuter *_next; /* pointer to next outer when frame is open */
};
//////////////////////////////////////////////
struct SQGenerator : public CHAINABLE_OBJ
{
enum SQGeneratorState{eRunning,eSuspended,eDead};
enum SQGeneratorState{eRunning,eSuspended,eDead};
private:
SQGenerator(SQSharedState *ss,SQClosure *closure){_closure=closure;_state=eRunning;_ci._generator=NULL;INIT_CHAIN();ADD_TO_CHAIN(&_ss(this)->_gc_chain,this);}
SQGenerator(SQSharedState *ss,SQClosure *closure){_closure=closure;_state=eRunning;_ci._generator=NULL;INIT_CHAIN();ADD_TO_CHAIN(&_ss(this)->_gc_chain,this);}
public:
static SQGenerator *Create(SQSharedState *ss,SQClosure *closure){
SQGenerator *nc=(SQGenerator*)SQ_MALLOC(sizeof(SQGenerator));
new (nc) SQGenerator(ss,closure);
return nc;
}
~SQGenerator()
{
REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this);
}
static SQGenerator *Create(SQSharedState *ss,SQClosure *closure){
SQGenerator *nc=(SQGenerator*)SQ_MALLOC(sizeof(SQGenerator));
new (nc) SQGenerator(ss,closure);
return nc;
}
~SQGenerator()
{
REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this);
}
void Kill(){
_state=eDead;
_stack.resize(0);
_closure.Null();}
void Release(){
sq_delete(this,SQGenerator);
}
_state=eDead;
_stack.resize(0);
_closure.Null();}
void Release(){
sq_delete(this,SQGenerator);
}
bool Yield(SQVM *v,SQInteger target);
bool Resume(SQVM *v,SQObjectPtr &dest);
bool Yield(SQVM *v,SQInteger target);
bool Resume(SQVM *v,SQObjectPtr &dest);
#ifndef NO_GARBAGE_COLLECTOR
void Mark(SQCollectable **chain);
void Finalize(){_stack.resize(0);_closure.Null();}
SQObjectType GetType() {return OT_GENERATOR;}
void Mark(SQCollectable **chain);
void Finalize(){_stack.resize(0);_closure.Null();}
SQObjectType GetType() {return OT_GENERATOR;}
#endif
SQObjectPtr _closure;
SQObjectPtrVec _stack;
SQVM::CallInfo _ci;
ExceptionsTraps _etraps;
SQGeneratorState _state;
SQObjectPtr _closure;
SQObjectPtrVec _stack;
SQVM::CallInfo _ci;
ExceptionsTraps _etraps;
SQGeneratorState _state;
};
#define _CALC_NATVIVECLOSURE_SIZE(noutervalues) (sizeof(SQNativeClosure) + (noutervalues*sizeof(SQObjectPtr)))
@ -147,53 +147,53 @@ public:
struct SQNativeClosure : public CHAINABLE_OBJ
{
private:
SQNativeClosure(SQSharedState *ss,SQFUNCTION func){_function=func;INIT_CHAIN();ADD_TO_CHAIN(&_ss(this)->_gc_chain,this); _env = NULL;}
SQNativeClosure(SQSharedState *ss,SQFUNCTION func){_function=func;INIT_CHAIN();ADD_TO_CHAIN(&_ss(this)->_gc_chain,this); _env = NULL;}
public:
static SQNativeClosure *Create(SQSharedState *ss,SQFUNCTION func,SQInteger nouters)
{
SQInteger size = _CALC_NATVIVECLOSURE_SIZE(nouters);
SQNativeClosure *nc=(SQNativeClosure*)SQ_MALLOC(size);
new (nc) SQNativeClosure(ss,func);
nc->_outervalues = (SQObjectPtr *)(nc + 1);
nc->_noutervalues = nouters;
_CONSTRUCT_VECTOR(SQObjectPtr,nc->_noutervalues,nc->_outervalues);
return nc;
}
SQNativeClosure *Clone()
{
SQNativeClosure * ret = SQNativeClosure::Create(_opt_ss(this),_function,_noutervalues);
ret->_env = _env;
if(ret->_env) __ObjAddRef(ret->_env);
ret->_name = _name;
_COPY_VECTOR(ret->_outervalues,_outervalues,_noutervalues);
ret->_typecheck.copy(_typecheck);
ret->_nparamscheck = _nparamscheck;
return ret;
}
~SQNativeClosure()
{
__ObjRelease(_env);
REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this);
}
void Release(){
SQInteger size = _CALC_NATVIVECLOSURE_SIZE(_noutervalues);
_DESTRUCT_VECTOR(SQObjectPtr,_noutervalues,_outervalues);
this->~SQNativeClosure();
sq_free(this,size);
}
static SQNativeClosure *Create(SQSharedState *ss,SQFUNCTION func,SQInteger nouters)
{
SQInteger size = _CALC_NATVIVECLOSURE_SIZE(nouters);
SQNativeClosure *nc=(SQNativeClosure*)SQ_MALLOC(size);
new (nc) SQNativeClosure(ss,func);
nc->_outervalues = (SQObjectPtr *)(nc + 1);
nc->_noutervalues = nouters;
_CONSTRUCT_VECTOR(SQObjectPtr,nc->_noutervalues,nc->_outervalues);
return nc;
}
SQNativeClosure *Clone()
{
SQNativeClosure * ret = SQNativeClosure::Create(_opt_ss(this),_function,_noutervalues);
ret->_env = _env;
if(ret->_env) __ObjAddRef(ret->_env);
ret->_name = _name;
_COPY_VECTOR(ret->_outervalues,_outervalues,_noutervalues);
ret->_typecheck.copy(_typecheck);
ret->_nparamscheck = _nparamscheck;
return ret;
}
~SQNativeClosure()
{
__ObjRelease(_env);
REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this);
}
void Release(){
SQInteger size = _CALC_NATVIVECLOSURE_SIZE(_noutervalues);
_DESTRUCT_VECTOR(SQObjectPtr,_noutervalues,_outervalues);
this->~SQNativeClosure();
sq_free(this,size);
}
#ifndef NO_GARBAGE_COLLECTOR
void Mark(SQCollectable **chain);
void Finalize() { _NULL_SQOBJECT_VECTOR(_outervalues,_noutervalues); }
SQObjectType GetType() {return OT_NATIVECLOSURE;}
void Mark(SQCollectable **chain);
void Finalize() { _NULL_SQOBJECT_VECTOR(_outervalues,_noutervalues); }
SQObjectType GetType() {return OT_NATIVECLOSURE;}
#endif
SQInteger _nparamscheck;
SQIntVec _typecheck;
SQObjectPtr *_outervalues;
SQUnsignedInteger _noutervalues;
SQWeakRef *_env;
SQFUNCTION _function;
SQObjectPtr _name;
SQInteger _nparamscheck;
SQIntVec _typecheck;
SQObjectPtr *_outervalues;
SQUnsignedInteger _noutervalues;
SQWeakRef *_env;
SQFUNCTION _function;
SQObjectPtr _name;
};

File diff suppressed because it is too large Load Diff

View File

@ -1,43 +1,43 @@
/* see copyright notice in squirrel.h */
/* see copyright notice in squirrel.h */
#ifndef _SQCOMPILER_H_
#define _SQCOMPILER_H_
struct SQVM;
#define TK_IDENTIFIER 258
#define TK_STRING_LITERAL 259
#define TK_INTEGER 260
#define TK_FLOAT 261
#define TK_BASE 262
#define TK_DELETE 263
#define TK_EQ 264
#define TK_NE 265
#define TK_LE 266
#define TK_GE 267
#define TK_SWITCH 268
#define TK_ARROW 269
#define TK_AND 270
#define TK_OR 271
#define TK_IF 272
#define TK_ELSE 273
#define TK_WHILE 274
#define TK_BREAK 275
#define TK_FOR 276
#define TK_DO 277
#define TK_NULL 278
#define TK_FOREACH 279
#define TK_IN 280
#define TK_NEWSLOT 281
#define TK_MODULO 282
#define TK_LOCAL 283
#define TK_CLONE 284
#define TK_FUNCTION 285
#define TK_RETURN 286
#define TK_TYPEOF 287
#define TK_UMINUS 288
#define TK_PLUSEQ 289
#define TK_MINUSEQ 290
#define TK_CONTINUE 291
#define TK_IDENTIFIER 258
#define TK_STRING_LITERAL 259
#define TK_INTEGER 260
#define TK_FLOAT 261
#define TK_BASE 262
#define TK_DELETE 263
#define TK_EQ 264
#define TK_NE 265
#define TK_LE 266
#define TK_GE 267
#define TK_SWITCH 268
#define TK_ARROW 269
#define TK_AND 270
#define TK_OR 271
#define TK_IF 272
#define TK_ELSE 273
#define TK_WHILE 274
#define TK_BREAK 275
#define TK_FOR 276
#define TK_DO 277
#define TK_NULL 278
#define TK_FOREACH 279
#define TK_IN 280
#define TK_NEWSLOT 281
#define TK_MODULO 282
#define TK_LOCAL 283
#define TK_CLONE 284
#define TK_FUNCTION 285
#define TK_RETURN 286
#define TK_TYPEOF 287
#define TK_UMINUS 288
#define TK_PLUSEQ 289
#define TK_MINUSEQ 290
#define TK_CONTINUE 291
#define TK_YIELD 292
#define TK_TRY 293
#define TK_CATCH 294

View File

@ -1,5 +1,5 @@
/*
see copyright notice in squirrel.h
see copyright notice in squirrel.h
*/
#include "sqpcheader.h"
#include <stdarg.h>
@ -10,109 +10,109 @@
SQRESULT sq_getfunctioninfo(HSQUIRRELVM v,SQInteger level,SQFunctionInfo *fi)
{
SQInteger cssize = v->_callsstacksize;
if (cssize > level) {
SQVM::CallInfo &ci = v->_callsstack[cssize-level-1];
if(sq_isclosure(ci._closure)) {
SQClosure *c = _closure(ci._closure);
SQFunctionProto *proto = c->_function;
fi->funcid = proto;
fi->name = type(proto->_name) == OT_STRING?_stringval(proto->_name):_SC("unknown");
fi->source = type(proto->_sourcename) == OT_STRING?_stringval(proto->_sourcename):_SC("unknown");
fi->line = proto->_lineinfos[0]._line;
return SQ_OK;
}
}
return sq_throwerror(v,_SC("the object is not a closure"));
SQInteger cssize = v->_callsstacksize;
if (cssize > level) {
SQVM::CallInfo &ci = v->_callsstack[cssize-level-1];
if(sq_isclosure(ci._closure)) {
SQClosure *c = _closure(ci._closure);
SQFunctionProto *proto = c->_function;
fi->funcid = proto;
fi->name = type(proto->_name) == OT_STRING?_stringval(proto->_name):_SC("unknown");
fi->source = type(proto->_sourcename) == OT_STRING?_stringval(proto->_sourcename):_SC("unknown");
fi->line = proto->_lineinfos[0]._line;
return SQ_OK;
}
}
return sq_throwerror(v,_SC("the object is not a closure"));
}
SQRESULT sq_stackinfos(HSQUIRRELVM v, SQInteger level, SQStackInfos *si)
{
SQInteger cssize = v->_callsstacksize;
if (cssize > level) {
memset(si, 0, sizeof(SQStackInfos));
SQVM::CallInfo &ci = v->_callsstack[cssize-level-1];
switch (type(ci._closure)) {
case OT_CLOSURE:{
SQFunctionProto *func = _closure(ci._closure)->_function;
if (type(func->_name) == OT_STRING)
si->funcname = _stringval(func->_name);
if (type(func->_sourcename) == OT_STRING)
si->source = _stringval(func->_sourcename);
si->line = func->GetLine(ci._ip);
}
break;
case OT_NATIVECLOSURE:
si->source = _SC("NATIVE");
si->funcname = _SC("unknown");
if(type(_nativeclosure(ci._closure)->_name) == OT_STRING)
si->funcname = _stringval(_nativeclosure(ci._closure)->_name);
si->line = -1;
break;
default: break; //shutup compiler
}
return SQ_OK;
}
return SQ_ERROR;
SQInteger cssize = v->_callsstacksize;
if (cssize > level) {
memset(si, 0, sizeof(SQStackInfos));
SQVM::CallInfo &ci = v->_callsstack[cssize-level-1];
switch (type(ci._closure)) {
case OT_CLOSURE:{
SQFunctionProto *func = _closure(ci._closure)->_function;
if (type(func->_name) == OT_STRING)
si->funcname = _stringval(func->_name);
if (type(func->_sourcename) == OT_STRING)
si->source = _stringval(func->_sourcename);
si->line = func->GetLine(ci._ip);
}
break;
case OT_NATIVECLOSURE:
si->source = _SC("NATIVE");
si->funcname = _SC("unknown");
if(type(_nativeclosure(ci._closure)->_name) == OT_STRING)
si->funcname = _stringval(_nativeclosure(ci._closure)->_name);
si->line = -1;
break;
default: break; //shutup compiler
}
return SQ_OK;
}
return SQ_ERROR;
}
void SQVM::Raise_Error(const SQChar *s, ...)
{
va_list vl;
va_start(vl, s);
SQInteger buffersize = (SQInteger)scstrlen(s)+(NUMBER_MAX_CHAR*2);
scvsprintf(_sp(sq_rsl(buffersize)),buffersize, s, vl);
va_end(vl);
_lasterror = SQString::Create(_ss(this),_spval,-1);
va_list vl;
va_start(vl, s);
SQInteger buffersize = (SQInteger)scstrlen(s)+(NUMBER_MAX_CHAR*2);
scvsprintf(_sp(sq_rsl(buffersize)),buffersize, s, vl);
va_end(vl);
_lasterror = SQString::Create(_ss(this),_spval,-1);
}
void SQVM::Raise_Error(const SQObjectPtr &desc)
{
_lasterror = desc;
_lasterror = desc;
}
SQString *SQVM::PrintObjVal(const SQObjectPtr &o)
{
switch(type(o)) {
case OT_STRING: return _string(o);
case OT_INTEGER:
scsprintf(_sp(sq_rsl(NUMBER_MAX_CHAR+1)),sq_rsl(NUMBER_MAX_CHAR), _PRINT_INT_FMT, _integer(o));
return SQString::Create(_ss(this), _spval);
break;
case OT_FLOAT:
scsprintf(_sp(sq_rsl(NUMBER_MAX_CHAR+1)), sq_rsl(NUMBER_MAX_CHAR), _SC("%.14g"), _float(o));
return SQString::Create(_ss(this), _spval);
break;
default:
return SQString::Create(_ss(this), GetTypeName(o));
}
switch(type(o)) {
case OT_STRING: return _string(o);
case OT_INTEGER:
scsprintf(_sp(sq_rsl(NUMBER_MAX_CHAR+1)),sq_rsl(NUMBER_MAX_CHAR), _PRINT_INT_FMT, _integer(o));
return SQString::Create(_ss(this), _spval);
break;
case OT_FLOAT:
scsprintf(_sp(sq_rsl(NUMBER_MAX_CHAR+1)), sq_rsl(NUMBER_MAX_CHAR), _SC("%.14g"), _float(o));
return SQString::Create(_ss(this), _spval);
break;
default:
return SQString::Create(_ss(this), GetTypeName(o));
}
}
void SQVM::Raise_IdxError(const SQObjectPtr &o)
{
SQObjectPtr oval = PrintObjVal(o);
Raise_Error(_SC("the index '%.50s' does not exist"), _stringval(oval));
SQObjectPtr oval = PrintObjVal(o);
Raise_Error(_SC("the index '%.50s' does not exist"), _stringval(oval));
}
void SQVM::Raise_CompareError(const SQObject &o1, const SQObject &o2)
{
SQObjectPtr oval1 = PrintObjVal(o1), oval2 = PrintObjVal(o2);
Raise_Error(_SC("comparison between '%.50s' and '%.50s'"), _stringval(oval1), _stringval(oval2));
SQObjectPtr oval1 = PrintObjVal(o1), oval2 = PrintObjVal(o2);
Raise_Error(_SC("comparison between '%.50s' and '%.50s'"), _stringval(oval1), _stringval(oval2));
}
void SQVM::Raise_ParamTypeError(SQInteger nparam,SQInteger typemask,SQInteger type)
{
SQObjectPtr exptypes = SQString::Create(_ss(this), _SC(""), -1);
SQInteger found = 0;
for(SQInteger i=0; i<16; i++)
{
SQInteger mask = 0x00000001 << i;
if(typemask & (mask)) {
if(found>0) StringCat(exptypes,SQString::Create(_ss(this), _SC("|"), -1), exptypes);
found ++;
StringCat(exptypes,SQString::Create(_ss(this), IdType2Name((SQObjectType)mask), -1), exptypes);
}
}
Raise_Error(_SC("parameter %d has an invalid type '%s' ; expected: '%s'"), nparam, IdType2Name((SQObjectType)type), _stringval(exptypes));
SQObjectPtr exptypes = SQString::Create(_ss(this), _SC(""), -1);
SQInteger found = 0;
for(SQInteger i=0; i<16; i++)
{
SQInteger mask = 0x00000001 << i;
if(typemask & (mask)) {
if(found>0) StringCat(exptypes,SQString::Create(_ss(this), _SC("|"), -1), exptypes);
found ++;
StringCat(exptypes,SQString::Create(_ss(this), IdType2Name((SQObjectType)mask), -1), exptypes);
}
}
Raise_Error(_SC("parameter %d has an invalid type '%s' ; expected: '%s'"), nparam, IdType2Name((SQObjectType)type), _stringval(exptypes));
}

View File

@ -1,49 +1,49 @@
/* see copyright notice in squirrel.h */
/* see copyright notice in squirrel.h */
#ifndef _SQFUNCTION_H_
#define _SQFUNCTION_H_
#include "sqopcodes.h"
enum SQOuterType {
otLOCAL = 0,
otOUTER = 1
otLOCAL = 0,
otOUTER = 1
};
struct SQOuterVar
{
SQOuterVar(){}
SQOuterVar(const SQObjectPtr &name,const SQObjectPtr &src,SQOuterType t)
{
_name = name;
_src=src;
_type=t;
}
SQOuterVar(const SQOuterVar &ov)
{
_type=ov._type;
_src=ov._src;
_name=ov._name;
}
SQOuterType _type;
SQObjectPtr _name;
SQObjectPtr _src;
SQOuterVar(){}
SQOuterVar(const SQObjectPtr &name,const SQObjectPtr &src,SQOuterType t)
{
_name = name;
_src=src;
_type=t;
}
SQOuterVar(const SQOuterVar &ov)
{
_type=ov._type;
_src=ov._src;
_name=ov._name;
}
SQOuterType _type;
SQObjectPtr _name;
SQObjectPtr _src;
};
struct SQLocalVarInfo
{
SQLocalVarInfo():_start_op(0),_end_op(0),_pos(0){}
SQLocalVarInfo(const SQLocalVarInfo &lvi)
{
_name=lvi._name;
_start_op=lvi._start_op;
_end_op=lvi._end_op;
_pos=lvi._pos;
}
SQObjectPtr _name;
SQUnsignedInteger _start_op;
SQUnsignedInteger _end_op;
SQUnsignedInteger _pos;
SQLocalVarInfo():_start_op(0),_end_op(0),_pos(0){}
SQLocalVarInfo(const SQLocalVarInfo &lvi)
{
_name=lvi._name;
_start_op=lvi._start_op;
_end_op=lvi._end_op;
_pos=lvi._pos;
}
SQObjectPtr _name;
SQUnsignedInteger _start_op;
SQUnsignedInteger _end_op;
SQUnsignedInteger _pos;
};
struct SQLineInfo { SQInteger _line;SQInteger _op; };
@ -53,102 +53,102 @@ typedef sqvector<SQLocalVarInfo> SQLocalVarInfoVec;
typedef sqvector<SQLineInfo> SQLineInfoVec;
#define _FUNC_SIZE(ni,nl,nparams,nfuncs,nouters,nlineinf,localinf,defparams) (sizeof(SQFunctionProto) \
+((ni-1)*sizeof(SQInstruction))+(nl*sizeof(SQObjectPtr)) \
+(nparams*sizeof(SQObjectPtr))+(nfuncs*sizeof(SQObjectPtr)) \
+(nouters*sizeof(SQOuterVar))+(nlineinf*sizeof(SQLineInfo)) \
+(localinf*sizeof(SQLocalVarInfo))+(defparams*sizeof(SQInteger)))
+((ni-1)*sizeof(SQInstruction))+(nl*sizeof(SQObjectPtr)) \
+(nparams*sizeof(SQObjectPtr))+(nfuncs*sizeof(SQObjectPtr)) \
+(nouters*sizeof(SQOuterVar))+(nlineinf*sizeof(SQLineInfo)) \
+(localinf*sizeof(SQLocalVarInfo))+(defparams*sizeof(SQInteger)))
struct SQFunctionProto : public CHAINABLE_OBJ
{
private:
SQFunctionProto(SQSharedState *ss);
~SQFunctionProto();
SQFunctionProto(SQSharedState *ss);
~SQFunctionProto();
public:
static SQFunctionProto *Create(SQSharedState *ss,SQInteger ninstructions,
SQInteger nliterals,SQInteger nparameters,
SQInteger nfunctions,SQInteger noutervalues,
SQInteger nlineinfos,SQInteger nlocalvarinfos,SQInteger ndefaultparams)
{
SQFunctionProto *f;
//I compact the whole class and members in a single memory allocation
f = (SQFunctionProto *)sq_vm_malloc(_FUNC_SIZE(ninstructions,nliterals,nparameters,nfunctions,noutervalues,nlineinfos,nlocalvarinfos,ndefaultparams));
new (f) SQFunctionProto(ss);
f->_ninstructions = ninstructions;
f->_literals = (SQObjectPtr*)&f->_instructions[ninstructions];
f->_nliterals = nliterals;
f->_parameters = (SQObjectPtr*)&f->_literals[nliterals];
f->_nparameters = nparameters;
f->_functions = (SQObjectPtr*)&f->_parameters[nparameters];
f->_nfunctions = nfunctions;
f->_outervalues = (SQOuterVar*)&f->_functions[nfunctions];
f->_noutervalues = noutervalues;
f->_lineinfos = (SQLineInfo *)&f->_outervalues[noutervalues];
f->_nlineinfos = nlineinfos;
f->_localvarinfos = (SQLocalVarInfo *)&f->_lineinfos[nlineinfos];
f->_nlocalvarinfos = nlocalvarinfos;
f->_defaultparams = (SQInteger *)&f->_localvarinfos[nlocalvarinfos];
f->_ndefaultparams = ndefaultparams;
static SQFunctionProto *Create(SQSharedState *ss,SQInteger ninstructions,
SQInteger nliterals,SQInteger nparameters,
SQInteger nfunctions,SQInteger noutervalues,
SQInteger nlineinfos,SQInteger nlocalvarinfos,SQInteger ndefaultparams)
{
SQFunctionProto *f;
//I compact the whole class and members in a single memory allocation
f = (SQFunctionProto *)sq_vm_malloc(_FUNC_SIZE(ninstructions,nliterals,nparameters,nfunctions,noutervalues,nlineinfos,nlocalvarinfos,ndefaultparams));
new (f) SQFunctionProto(ss);
f->_ninstructions = ninstructions;
f->_literals = (SQObjectPtr*)&f->_instructions[ninstructions];
f->_nliterals = nliterals;
f->_parameters = (SQObjectPtr*)&f->_literals[nliterals];
f->_nparameters = nparameters;
f->_functions = (SQObjectPtr*)&f->_parameters[nparameters];
f->_nfunctions = nfunctions;
f->_outervalues = (SQOuterVar*)&f->_functions[nfunctions];
f->_noutervalues = noutervalues;
f->_lineinfos = (SQLineInfo *)&f->_outervalues[noutervalues];
f->_nlineinfos = nlineinfos;
f->_localvarinfos = (SQLocalVarInfo *)&f->_lineinfos[nlineinfos];
f->_nlocalvarinfos = nlocalvarinfos;
f->_defaultparams = (SQInteger *)&f->_localvarinfos[nlocalvarinfos];
f->_ndefaultparams = ndefaultparams;
_CONSTRUCT_VECTOR(SQObjectPtr,f->_nliterals,f->_literals);
_CONSTRUCT_VECTOR(SQObjectPtr,f->_nparameters,f->_parameters);
_CONSTRUCT_VECTOR(SQObjectPtr,f->_nfunctions,f->_functions);
_CONSTRUCT_VECTOR(SQOuterVar,f->_noutervalues,f->_outervalues);
//_CONSTRUCT_VECTOR(SQLineInfo,f->_nlineinfos,f->_lineinfos); //not required are 2 integers
_CONSTRUCT_VECTOR(SQLocalVarInfo,f->_nlocalvarinfos,f->_localvarinfos);
return f;
}
void Release(){
_DESTRUCT_VECTOR(SQObjectPtr,_nliterals,_literals);
_DESTRUCT_VECTOR(SQObjectPtr,_nparameters,_parameters);
_DESTRUCT_VECTOR(SQObjectPtr,_nfunctions,_functions);
_DESTRUCT_VECTOR(SQOuterVar,_noutervalues,_outervalues);
//_DESTRUCT_VECTOR(SQLineInfo,_nlineinfos,_lineinfos); //not required are 2 integers
_DESTRUCT_VECTOR(SQLocalVarInfo,_nlocalvarinfos,_localvarinfos);
SQInteger size = _FUNC_SIZE(_ninstructions,_nliterals,_nparameters,_nfunctions,_noutervalues,_nlineinfos,_nlocalvarinfos,_ndefaultparams);
this->~SQFunctionProto();
sq_vm_free(this,size);
}
_CONSTRUCT_VECTOR(SQObjectPtr,f->_nliterals,f->_literals);
_CONSTRUCT_VECTOR(SQObjectPtr,f->_nparameters,f->_parameters);
_CONSTRUCT_VECTOR(SQObjectPtr,f->_nfunctions,f->_functions);
_CONSTRUCT_VECTOR(SQOuterVar,f->_noutervalues,f->_outervalues);
//_CONSTRUCT_VECTOR(SQLineInfo,f->_nlineinfos,f->_lineinfos); //not required are 2 integers
_CONSTRUCT_VECTOR(SQLocalVarInfo,f->_nlocalvarinfos,f->_localvarinfos);
return f;
}
void Release(){
_DESTRUCT_VECTOR(SQObjectPtr,_nliterals,_literals);
_DESTRUCT_VECTOR(SQObjectPtr,_nparameters,_parameters);
_DESTRUCT_VECTOR(SQObjectPtr,_nfunctions,_functions);
_DESTRUCT_VECTOR(SQOuterVar,_noutervalues,_outervalues);
//_DESTRUCT_VECTOR(SQLineInfo,_nlineinfos,_lineinfos); //not required are 2 integers
_DESTRUCT_VECTOR(SQLocalVarInfo,_nlocalvarinfos,_localvarinfos);
SQInteger size = _FUNC_SIZE(_ninstructions,_nliterals,_nparameters,_nfunctions,_noutervalues,_nlineinfos,_nlocalvarinfos,_ndefaultparams);
this->~SQFunctionProto();
sq_vm_free(this,size);
}
const SQChar* GetLocal(SQVM *v,SQUnsignedInteger stackbase,SQUnsignedInteger nseq,SQUnsignedInteger nop);
SQInteger GetLine(SQInstruction *curr);
bool Save(SQVM *v,SQUserPointer up,SQWRITEFUNC write);
static bool Load(SQVM *v,SQUserPointer up,SQREADFUNC read,SQObjectPtr &ret);
const SQChar* GetLocal(SQVM *v,SQUnsignedInteger stackbase,SQUnsignedInteger nseq,SQUnsignedInteger nop);
SQInteger GetLine(SQInstruction *curr);
bool Save(SQVM *v,SQUserPointer up,SQWRITEFUNC write);
static bool Load(SQVM *v,SQUserPointer up,SQREADFUNC read,SQObjectPtr &ret);
#ifndef NO_GARBAGE_COLLECTOR
void Mark(SQCollectable **chain);
void Finalize(){ _NULL_SQOBJECT_VECTOR(_literals,_nliterals); }
SQObjectType GetType() {return OT_FUNCPROTO;}
void Mark(SQCollectable **chain);
void Finalize(){ _NULL_SQOBJECT_VECTOR(_literals,_nliterals); }
SQObjectType GetType() {return OT_FUNCPROTO;}
#endif
SQObjectPtr _sourcename;
SQObjectPtr _name;
SQObjectPtr _sourcename;
SQObjectPtr _name;
SQInteger _stacksize;
bool _bgenerator;
SQInteger _varparams;
bool _bgenerator;
SQInteger _varparams;
SQInteger _nlocalvarinfos;
SQLocalVarInfo *_localvarinfos;
SQInteger _nlocalvarinfos;
SQLocalVarInfo *_localvarinfos;
SQInteger _nlineinfos;
SQLineInfo *_lineinfos;
SQInteger _nlineinfos;
SQLineInfo *_lineinfos;
SQInteger _nliterals;
SQObjectPtr *_literals;
SQInteger _nliterals;
SQObjectPtr *_literals;
SQInteger _nparameters;
SQObjectPtr *_parameters;
SQInteger _nparameters;
SQObjectPtr *_parameters;
SQInteger _nfunctions;
SQObjectPtr *_functions;
SQInteger _nfunctions;
SQObjectPtr *_functions;
SQInteger _noutervalues;
SQOuterVar *_outervalues;
SQInteger _noutervalues;
SQOuterVar *_outervalues;
SQInteger _ndefaultparams;
SQInteger *_defaultparams;
SQInteger _ndefaultparams;
SQInteger *_defaultparams;
SQInteger _ninstructions;
SQInstruction _instructions[1];
SQInteger _ninstructions;
SQInstruction _instructions[1];
};
#endif //_SQFUNCTION_H_

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
/* see copyright notice in squirrel.h */
/* see copyright notice in squirrel.h */
#ifndef _SQFUNCSTATE_H_
#define _SQFUNCSTATE_H_
///////////////////////////////////
@ -6,84 +6,84 @@
struct SQFuncState
{
SQFuncState(SQSharedState *ss,SQFuncState *parent,CompilerErrorFunc efunc,void *ed);
~SQFuncState();
SQFuncState(SQSharedState *ss,SQFuncState *parent,CompilerErrorFunc efunc,void *ed);
~SQFuncState();
#ifdef _DEBUG_DUMP
void Dump(SQFunctionProto *func);
void Dump(SQFunctionProto *func);
#endif
void Error(const SQChar *err);
SQFuncState *PushChildState(SQSharedState *ss);
void PopChildState();
void AddInstruction(SQOpcode _op,SQInteger arg0=0,SQInteger arg1=0,SQInteger arg2=0,SQInteger arg3=0){SQInstruction i(_op,arg0,arg1,arg2,arg3);AddInstruction(i);}
void AddInstruction(SQInstruction &i);
void SetIntructionParams(SQInteger pos,SQInteger arg0,SQInteger arg1,SQInteger arg2=0,SQInteger arg3=0);
void SetIntructionParam(SQInteger pos,SQInteger arg,SQInteger val);
SQInstruction &GetInstruction(SQInteger pos){return _instructions[pos];}
void PopInstructions(SQInteger size){for(SQInteger i=0;i<size;i++)_instructions.pop_back();}
void SetStackSize(SQInteger n);
SQInteger CountOuters(SQInteger stacksize);
void SnoozeOpt(){_optimization=false;}
void AddDefaultParam(SQInteger trg) { _defaultparams.push_back(trg); }
SQInteger GetDefaultParamCount() { return _defaultparams.size(); }
SQInteger GetCurrentPos(){return _instructions.size()-1;}
SQInteger GetNumericConstant(const SQInteger cons);
SQInteger GetNumericConstant(const SQFloat cons);
SQInteger PushLocalVariable(const SQObject &name);
void AddParameter(const SQObject &name);
//void AddOuterValue(const SQObject &name);
SQInteger GetLocalVariable(const SQObject &name);
void MarkLocalAsOuter(SQInteger pos);
SQInteger GetOuterVariable(const SQObject &name);
SQInteger GenerateCode();
SQInteger GetStackSize();
SQInteger CalcStackFrameSize();
void AddLineInfos(SQInteger line,bool lineop,bool force=false);
SQFunctionProto *BuildProto();
SQInteger AllocStackPos();
SQInteger PushTarget(SQInteger n=-1);
SQInteger PopTarget();
SQInteger TopTarget();
SQInteger GetUpTarget(SQInteger n);
void DiscardTarget();
bool IsLocal(SQUnsignedInteger stkpos);
SQObject CreateString(const SQChar *s,SQInteger len = -1);
SQObject CreateTable();
bool IsConstant(const SQObject &name,SQObject &e);
SQInteger _returnexp;
SQLocalVarInfoVec _vlocals;
SQIntVec _targetstack;
SQInteger _stacksize;
bool _varparams;
bool _bgenerator;
SQIntVec _unresolvedbreaks;
SQIntVec _unresolvedcontinues;
SQObjectPtrVec _functions;
SQObjectPtrVec _parameters;
SQOuterVarVec _outervalues;
SQInstructionVec _instructions;
SQLocalVarInfoVec _localvarinfos;
SQObjectPtr _literals;
SQObjectPtr _strings;
SQObjectPtr _name;
SQObjectPtr _sourcename;
SQInteger _nliterals;
SQLineInfoVec _lineinfos;
SQFuncState *_parent;
SQIntVec _scope_blocks;
SQIntVec _breaktargets;
SQIntVec _continuetargets;
SQIntVec _defaultparams;
SQInteger _lastline;
SQInteger _traps; //contains number of nested exception traps
SQInteger _outers;
bool _optimization;
SQSharedState *_sharedstate;
sqvector<SQFuncState*> _childstates;
SQInteger GetConstant(const SQObject &cons);
void Error(const SQChar *err);
SQFuncState *PushChildState(SQSharedState *ss);
void PopChildState();
void AddInstruction(SQOpcode _op,SQInteger arg0=0,SQInteger arg1=0,SQInteger arg2=0,SQInteger arg3=0){SQInstruction i(_op,arg0,arg1,arg2,arg3);AddInstruction(i);}
void AddInstruction(SQInstruction &i);
void SetIntructionParams(SQInteger pos,SQInteger arg0,SQInteger arg1,SQInteger arg2=0,SQInteger arg3=0);
void SetIntructionParam(SQInteger pos,SQInteger arg,SQInteger val);
SQInstruction &GetInstruction(SQInteger pos){return _instructions[pos];}
void PopInstructions(SQInteger size){for(SQInteger i=0;i<size;i++)_instructions.pop_back();}
void SetStackSize(SQInteger n);
SQInteger CountOuters(SQInteger stacksize);
void SnoozeOpt(){_optimization=false;}
void AddDefaultParam(SQInteger trg) { _defaultparams.push_back(trg); }
SQInteger GetDefaultParamCount() { return _defaultparams.size(); }
SQInteger GetCurrentPos(){return _instructions.size()-1;}
SQInteger GetNumericConstant(const SQInteger cons);
SQInteger GetNumericConstant(const SQFloat cons);
SQInteger PushLocalVariable(const SQObject &name);
void AddParameter(const SQObject &name);
//void AddOuterValue(const SQObject &name);
SQInteger GetLocalVariable(const SQObject &name);
void MarkLocalAsOuter(SQInteger pos);
SQInteger GetOuterVariable(const SQObject &name);
SQInteger GenerateCode();
SQInteger GetStackSize();
SQInteger CalcStackFrameSize();
void AddLineInfos(SQInteger line,bool lineop,bool force=false);
SQFunctionProto *BuildProto();
SQInteger AllocStackPos();
SQInteger PushTarget(SQInteger n=-1);
SQInteger PopTarget();
SQInteger TopTarget();
SQInteger GetUpTarget(SQInteger n);
void DiscardTarget();
bool IsLocal(SQUnsignedInteger stkpos);
SQObject CreateString(const SQChar *s,SQInteger len = -1);
SQObject CreateTable();
bool IsConstant(const SQObject &name,SQObject &e);
SQInteger _returnexp;
SQLocalVarInfoVec _vlocals;
SQIntVec _targetstack;
SQInteger _stacksize;
bool _varparams;
bool _bgenerator;
SQIntVec _unresolvedbreaks;
SQIntVec _unresolvedcontinues;
SQObjectPtrVec _functions;
SQObjectPtrVec _parameters;
SQOuterVarVec _outervalues;
SQInstructionVec _instructions;
SQLocalVarInfoVec _localvarinfos;
SQObjectPtr _literals;
SQObjectPtr _strings;
SQObjectPtr _name;
SQObjectPtr _sourcename;
SQInteger _nliterals;
SQLineInfoVec _lineinfos;
SQFuncState *_parent;
SQIntVec _scope_blocks;
SQIntVec _breaktargets;
SQIntVec _continuetargets;
SQIntVec _defaultparams;
SQInteger _lastline;
SQInteger _traps; //contains number of nested exception traps
SQInteger _outers;
bool _optimization;
SQSharedState *_sharedstate;
sqvector<SQFuncState*> _childstates;
SQInteger GetConstant(const SQObject &cons);
private:
CompilerErrorFunc _errfunc;
void *_errtarget;
SQSharedState *_ss;
CompilerErrorFunc _errfunc;
void *_errtarget;
SQSharedState *_ss;
};

View File

@ -1,5 +1,5 @@
/*
see copyright notice in squirrel.h
see copyright notice in squirrel.h
*/
#include "sqpcheader.h"
#include <ctype.h>
@ -21,384 +21,454 @@
SQLexer::SQLexer(){}
SQLexer::~SQLexer()
{
_keywords->Release();
_keywords->Release();
}
void SQLexer::Init(SQSharedState *ss, SQLEXREADFUNC rg, SQUserPointer up,CompilerErrorFunc efunc,void *ed)
{
_errfunc = efunc;
_errtarget = ed;
_sharedstate = ss;
_keywords = SQTable::Create(ss, 37);
ADD_KEYWORD(while, TK_WHILE);
ADD_KEYWORD(do, TK_DO);
ADD_KEYWORD(if, TK_IF);
ADD_KEYWORD(else, TK_ELSE);
ADD_KEYWORD(break, TK_BREAK);
ADD_KEYWORD(continue, TK_CONTINUE);
ADD_KEYWORD(return, TK_RETURN);
ADD_KEYWORD(null, TK_NULL);
ADD_KEYWORD(function, TK_FUNCTION);
ADD_KEYWORD(local, TK_LOCAL);
ADD_KEYWORD(for, TK_FOR);
ADD_KEYWORD(foreach, TK_FOREACH);
ADD_KEYWORD(in, TK_IN);
ADD_KEYWORD(typeof, TK_TYPEOF);
ADD_KEYWORD(base, TK_BASE);
ADD_KEYWORD(delete, TK_DELETE);
ADD_KEYWORD(try, TK_TRY);
ADD_KEYWORD(catch, TK_CATCH);
ADD_KEYWORD(throw, TK_THROW);
ADD_KEYWORD(clone, TK_CLONE);
ADD_KEYWORD(yield, TK_YIELD);
ADD_KEYWORD(resume, TK_RESUME);
ADD_KEYWORD(switch, TK_SWITCH);
ADD_KEYWORD(case, TK_CASE);
ADD_KEYWORD(default, TK_DEFAULT);
ADD_KEYWORD(this, TK_THIS);
ADD_KEYWORD(class,TK_CLASS);
ADD_KEYWORD(extends,TK_EXTENDS);
ADD_KEYWORD(constructor,TK_CONSTRUCTOR);
ADD_KEYWORD(instanceof,TK_INSTANCEOF);
ADD_KEYWORD(true,TK_TRUE);
ADD_KEYWORD(false,TK_FALSE);
ADD_KEYWORD(static,TK_STATIC);
ADD_KEYWORD(enum,TK_ENUM);
ADD_KEYWORD(const,TK_CONST);
ADD_KEYWORD(__LINE__,TK___LINE__);
_errfunc = efunc;
_errtarget = ed;
_sharedstate = ss;
_keywords = SQTable::Create(ss, 37);
ADD_KEYWORD(while, TK_WHILE);
ADD_KEYWORD(do, TK_DO);
ADD_KEYWORD(if, TK_IF);
ADD_KEYWORD(else, TK_ELSE);
ADD_KEYWORD(break, TK_BREAK);
ADD_KEYWORD(continue, TK_CONTINUE);
ADD_KEYWORD(return, TK_RETURN);
ADD_KEYWORD(null, TK_NULL);
ADD_KEYWORD(function, TK_FUNCTION);
ADD_KEYWORD(local, TK_LOCAL);
ADD_KEYWORD(for, TK_FOR);
ADD_KEYWORD(foreach, TK_FOREACH);
ADD_KEYWORD(in, TK_IN);
ADD_KEYWORD(typeof, TK_TYPEOF);
ADD_KEYWORD(base, TK_BASE);
ADD_KEYWORD(delete, TK_DELETE);
ADD_KEYWORD(try, TK_TRY);
ADD_KEYWORD(catch, TK_CATCH);
ADD_KEYWORD(throw, TK_THROW);
ADD_KEYWORD(clone, TK_CLONE);
ADD_KEYWORD(yield, TK_YIELD);
ADD_KEYWORD(resume, TK_RESUME);
ADD_KEYWORD(switch, TK_SWITCH);
ADD_KEYWORD(case, TK_CASE);
ADD_KEYWORD(default, TK_DEFAULT);
ADD_KEYWORD(this, TK_THIS);
ADD_KEYWORD(class,TK_CLASS);
ADD_KEYWORD(extends,TK_EXTENDS);
ADD_KEYWORD(constructor,TK_CONSTRUCTOR);
ADD_KEYWORD(instanceof,TK_INSTANCEOF);
ADD_KEYWORD(true,TK_TRUE);
ADD_KEYWORD(false,TK_FALSE);
ADD_KEYWORD(static,TK_STATIC);
ADD_KEYWORD(enum,TK_ENUM);
ADD_KEYWORD(const,TK_CONST);
ADD_KEYWORD(__LINE__,TK___LINE__);
ADD_KEYWORD(__FILE__,TK___FILE__);
_readf = rg;
_up = up;
_lasttokenline = _currentline = 1;
_currentcolumn = 0;
_prevtoken = -1;
_reached_eof = SQFalse;
Next();
_readf = rg;
_up = up;
_lasttokenline = _currentline = 1;
_currentcolumn = 0;
_prevtoken = -1;
_reached_eof = SQFalse;
Next();
}
void SQLexer::Error(const SQChar *err)
{
_errfunc(_errtarget,err);
_errfunc(_errtarget,err);
}
void SQLexer::Next()
{
SQInteger t = _readf(_up);
if(t > MAX_CHAR) Error(_SC("Invalid character"));
if(t != 0) {
_currdata = (LexChar)t;
return;
}
_currdata = SQUIRREL_EOB;
_reached_eof = SQTrue;
SQInteger t = _readf(_up);
if(t > MAX_CHAR) Error(_SC("Invalid character"));
if(t != 0) {
_currdata = (LexChar)t;
return;
}
_currdata = SQUIRREL_EOB;
_reached_eof = SQTrue;
}
const SQChar *SQLexer::Tok2Str(SQInteger tok)
{
SQObjectPtr itr, key, val;
SQInteger nitr;
while((nitr = _keywords->Next(false,itr, key, val)) != -1) {
itr = (SQInteger)nitr;
if(((SQInteger)_integer(val)) == tok)
return _stringval(key);
}
return NULL;
SQObjectPtr itr, key, val;
SQInteger nitr;
while((nitr = _keywords->Next(false,itr, key, val)) != -1) {
itr = (SQInteger)nitr;
if(((SQInteger)_integer(val)) == tok)
return _stringval(key);
}
return NULL;
}
void SQLexer::LexBlockComment()
{
bool done = false;
while(!done) {
switch(CUR_CHAR) {
case _SC('*'): { NEXT(); if(CUR_CHAR == _SC('/')) { done = true; NEXT(); }}; continue;
case _SC('\n'): _currentline++; NEXT(); continue;
case SQUIRREL_EOB: Error(_SC("missing \"*/\" in comment"));
default: NEXT();
}
}
bool done = false;
while(!done) {
switch(CUR_CHAR) {
case _SC('*'): { NEXT(); if(CUR_CHAR == _SC('/')) { done = true; NEXT(); }}; continue;
case _SC('\n'): _currentline++; NEXT(); continue;
case SQUIRREL_EOB: Error(_SC("missing \"*/\" in comment"));
default: NEXT();
}
}
}
void SQLexer::LexLineComment()
{
do { NEXT(); } while (CUR_CHAR != _SC('\n') && (!IS_EOB()));
do { NEXT(); } while (CUR_CHAR != _SC('\n') && (!IS_EOB()));
}
SQInteger SQLexer::Lex()
{
_lasttokenline = _currentline;
while(CUR_CHAR != SQUIRREL_EOB) {
switch(CUR_CHAR){
case _SC('\t'): case _SC('\r'): case _SC(' '): NEXT(); continue;
case _SC('\n'):
_currentline++;
_prevtoken=_curtoken;
_curtoken=_SC('\n');
NEXT();
_currentcolumn=1;
continue;
case _SC('#'): LexLineComment(); continue;
case _SC('/'):
NEXT();
switch(CUR_CHAR){
case _SC('*'):
NEXT();
LexBlockComment();
continue;
case _SC('/'):
LexLineComment();
continue;
case _SC('='):
NEXT();
RETURN_TOKEN(TK_DIVEQ);
continue;
case _SC('>'):
NEXT();
RETURN_TOKEN(TK_ATTR_CLOSE);
continue;
default:
RETURN_TOKEN('/');
}
case _SC('='):
NEXT();
if (CUR_CHAR != _SC('=')){ RETURN_TOKEN('=') }
else { NEXT(); RETURN_TOKEN(TK_EQ); }
case _SC('<'):
NEXT();
switch(CUR_CHAR) {
case _SC('='):
NEXT();
if(CUR_CHAR == _SC('>')) {
NEXT();
RETURN_TOKEN(TK_3WAYSCMP);
}
RETURN_TOKEN(TK_LE)
break;
case _SC('-'): NEXT(); RETURN_TOKEN(TK_NEWSLOT); break;
case _SC('<'): NEXT(); RETURN_TOKEN(TK_SHIFTL); break;
case _SC('/'): NEXT(); RETURN_TOKEN(TK_ATTR_OPEN); break;
}
RETURN_TOKEN('<');
case _SC('>'):
NEXT();
if (CUR_CHAR == _SC('=')){ NEXT(); RETURN_TOKEN(TK_GE);}
else if(CUR_CHAR == _SC('>')){
NEXT();
if(CUR_CHAR == _SC('>')){
NEXT();
RETURN_TOKEN(TK_USHIFTR);
}
RETURN_TOKEN(TK_SHIFTR);
}
else { RETURN_TOKEN('>') }
case _SC('!'):
NEXT();
if (CUR_CHAR != _SC('=')){ RETURN_TOKEN('!')}
else { NEXT(); RETURN_TOKEN(TK_NE); }
case _SC('@'): {
SQInteger stype;
NEXT();
if(CUR_CHAR != _SC('"')) {
RETURN_TOKEN('@');
}
if((stype=ReadString('"',true))!=-1) {
RETURN_TOKEN(stype);
}
Error(_SC("error parsing the string"));
}
case _SC('"'):
case _SC('\''): {
SQInteger stype;
if((stype=ReadString(CUR_CHAR,false))!=-1){
RETURN_TOKEN(stype);
}
Error(_SC("error parsing the string"));
}
case _SC('{'): case _SC('}'): case _SC('('): case _SC(')'): case _SC('['): case _SC(']'):
case _SC(';'): case _SC(','): case _SC('?'): case _SC('^'): case _SC('~'):
{SQInteger ret = CUR_CHAR;
NEXT(); RETURN_TOKEN(ret); }
case _SC('.'):
NEXT();
if (CUR_CHAR != _SC('.')){ RETURN_TOKEN('.') }
NEXT();
if (CUR_CHAR != _SC('.')){ Error(_SC("invalid token '..'")); }
NEXT();
RETURN_TOKEN(TK_VARPARAMS);
case _SC('&'):
NEXT();
if (CUR_CHAR != _SC('&')){ RETURN_TOKEN('&') }
else { NEXT(); RETURN_TOKEN(TK_AND); }
case _SC('|'):
NEXT();
if (CUR_CHAR != _SC('|')){ RETURN_TOKEN('|') }
else { NEXT(); RETURN_TOKEN(TK_OR); }
case _SC(':'):
NEXT();
if (CUR_CHAR != _SC(':')){ RETURN_TOKEN(':') }
else { NEXT(); RETURN_TOKEN(TK_DOUBLE_COLON); }
case _SC('*'):
NEXT();
if (CUR_CHAR == _SC('=')){ NEXT(); RETURN_TOKEN(TK_MULEQ);}
else RETURN_TOKEN('*');
case _SC('%'):
NEXT();
if (CUR_CHAR == _SC('=')){ NEXT(); RETURN_TOKEN(TK_MODEQ);}
else RETURN_TOKEN('%');
case _SC('-'):
NEXT();
if (CUR_CHAR == _SC('=')){ NEXT(); RETURN_TOKEN(TK_MINUSEQ);}
else if (CUR_CHAR == _SC('-')){ NEXT(); RETURN_TOKEN(TK_MINUSMINUS);}
else RETURN_TOKEN('-');
case _SC('+'):
NEXT();
if (CUR_CHAR == _SC('=')){ NEXT(); RETURN_TOKEN(TK_PLUSEQ);}
else if (CUR_CHAR == _SC('+')){ NEXT(); RETURN_TOKEN(TK_PLUSPLUS);}
else RETURN_TOKEN('+');
case SQUIRREL_EOB:
return 0;
default:{
if (scisdigit(CUR_CHAR)) {
SQInteger ret = ReadNumber();
RETURN_TOKEN(ret);
}
else if (scisalpha(CUR_CHAR) || CUR_CHAR == _SC('_')) {
SQInteger t = ReadID();
RETURN_TOKEN(t);
}
else {
SQInteger c = CUR_CHAR;
if (sciscntrl((int)c)) Error(_SC("unexpected character(control)"));
NEXT();
RETURN_TOKEN(c);
}
RETURN_TOKEN(0);
}
}
}
return 0;
_lasttokenline = _currentline;
while(CUR_CHAR != SQUIRREL_EOB) {
switch(CUR_CHAR){
case _SC('\t'): case _SC('\r'): case _SC(' '): NEXT(); continue;
case _SC('\n'):
_currentline++;
_prevtoken=_curtoken;
_curtoken=_SC('\n');
NEXT();
_currentcolumn=1;
continue;
case _SC('#'): LexLineComment(); continue;
case _SC('/'):
NEXT();
switch(CUR_CHAR){
case _SC('*'):
NEXT();
LexBlockComment();
continue;
case _SC('/'):
LexLineComment();
continue;
case _SC('='):
NEXT();
RETURN_TOKEN(TK_DIVEQ);
continue;
case _SC('>'):
NEXT();
RETURN_TOKEN(TK_ATTR_CLOSE);
continue;
default:
RETURN_TOKEN('/');
}
case _SC('='):
NEXT();
if (CUR_CHAR != _SC('=')){ RETURN_TOKEN('=') }
else { NEXT(); RETURN_TOKEN(TK_EQ); }
case _SC('<'):
NEXT();
switch(CUR_CHAR) {
case _SC('='):
NEXT();
if(CUR_CHAR == _SC('>')) {
NEXT();
RETURN_TOKEN(TK_3WAYSCMP);
}
RETURN_TOKEN(TK_LE)
break;
case _SC('-'): NEXT(); RETURN_TOKEN(TK_NEWSLOT); break;
case _SC('<'): NEXT(); RETURN_TOKEN(TK_SHIFTL); break;
case _SC('/'): NEXT(); RETURN_TOKEN(TK_ATTR_OPEN); break;
}
RETURN_TOKEN('<');
case _SC('>'):
NEXT();
if (CUR_CHAR == _SC('=')){ NEXT(); RETURN_TOKEN(TK_GE);}
else if(CUR_CHAR == _SC('>')){
NEXT();
if(CUR_CHAR == _SC('>')){
NEXT();
RETURN_TOKEN(TK_USHIFTR);
}
RETURN_TOKEN(TK_SHIFTR);
}
else { RETURN_TOKEN('>') }
case _SC('!'):
NEXT();
if (CUR_CHAR != _SC('=')){ RETURN_TOKEN('!')}
else { NEXT(); RETURN_TOKEN(TK_NE); }
case _SC('@'): {
SQInteger stype;
NEXT();
if(CUR_CHAR != _SC('"')) {
RETURN_TOKEN('@');
}
if((stype=ReadString('"',true))!=-1) {
RETURN_TOKEN(stype);
}
Error(_SC("error parsing the string"));
}
case _SC('"'):
case _SC('\''): {
SQInteger stype;
if((stype=ReadString(CUR_CHAR,false))!=-1){
RETURN_TOKEN(stype);
}
Error(_SC("error parsing the string"));
}
case _SC('{'): case _SC('}'): case _SC('('): case _SC(')'): case _SC('['): case _SC(']'):
case _SC(';'): case _SC(','): case _SC('?'): case _SC('^'): case _SC('~'):
{SQInteger ret = CUR_CHAR;
NEXT(); RETURN_TOKEN(ret); }
case _SC('.'):
NEXT();
if (CUR_CHAR != _SC('.')){ RETURN_TOKEN('.') }
NEXT();
if (CUR_CHAR != _SC('.')){ Error(_SC("invalid token '..'")); }
NEXT();
RETURN_TOKEN(TK_VARPARAMS);
case _SC('&'):
NEXT();
if (CUR_CHAR != _SC('&')){ RETURN_TOKEN('&') }
else { NEXT(); RETURN_TOKEN(TK_AND); }
case _SC('|'):
NEXT();
if (CUR_CHAR != _SC('|')){ RETURN_TOKEN('|') }
else { NEXT(); RETURN_TOKEN(TK_OR); }
case _SC(':'):
NEXT();
if (CUR_CHAR != _SC(':')){ RETURN_TOKEN(':') }
else { NEXT(); RETURN_TOKEN(TK_DOUBLE_COLON); }
case _SC('*'):
NEXT();
if (CUR_CHAR == _SC('=')){ NEXT(); RETURN_TOKEN(TK_MULEQ);}
else RETURN_TOKEN('*');
case _SC('%'):
NEXT();
if (CUR_CHAR == _SC('=')){ NEXT(); RETURN_TOKEN(TK_MODEQ);}
else RETURN_TOKEN('%');
case _SC('-'):
NEXT();
if (CUR_CHAR == _SC('=')){ NEXT(); RETURN_TOKEN(TK_MINUSEQ);}
else if (CUR_CHAR == _SC('-')){ NEXT(); RETURN_TOKEN(TK_MINUSMINUS);}
else RETURN_TOKEN('-');
case _SC('+'):
NEXT();
if (CUR_CHAR == _SC('=')){ NEXT(); RETURN_TOKEN(TK_PLUSEQ);}
else if (CUR_CHAR == _SC('+')){ NEXT(); RETURN_TOKEN(TK_PLUSPLUS);}
else RETURN_TOKEN('+');
case SQUIRREL_EOB:
return 0;
default:{
if (scisdigit(CUR_CHAR)) {
SQInteger ret = ReadNumber();
RETURN_TOKEN(ret);
}
else if (scisalpha(CUR_CHAR) || CUR_CHAR == _SC('_')) {
SQInteger t = ReadID();
RETURN_TOKEN(t);
}
else {
SQInteger c = CUR_CHAR;
if (sciscntrl((int)c)) Error(_SC("unexpected character(control)"));
NEXT();
RETURN_TOKEN(c);
}
RETURN_TOKEN(0);
}
}
}
return 0;
}
SQInteger SQLexer::GetIDType(const SQChar *s,SQInteger len)
{
SQObjectPtr t;
if(_keywords->GetStr(s,len, t)) {
return SQInteger(_integer(t));
}
return TK_IDENTIFIER;
SQObjectPtr t;
if(_keywords->GetStr(s,len, t)) {
return SQInteger(_integer(t));
}
return TK_IDENTIFIER;
}
#ifdef SQUNICODE
#if WCHAR_SIZE == 2
SQInteger SQLexer::AddUTF16(SQUnsignedInteger ch)
{
if (ch >= 0x10000)
{
SQUnsignedInteger code = (ch - 0x10000);
APPEND_CHAR((SQChar)(0xD800 | (code >> 10)));
APPEND_CHAR((SQChar)(0xDC00 | (code & 0x3FF)));
return 2;
}
else {
APPEND_CHAR((SQChar)ch);
return 1;
}
}
#endif
#else
SQInteger SQLexer::AddUTF8(SQUnsignedInteger ch)
{
if (ch < 0x80) {
APPEND_CHAR((char)ch);
return 1;
}
if (ch < 0x800) {
APPEND_CHAR((SQChar)((ch >> 6) | 0xC0));
APPEND_CHAR((SQChar)((ch & 0x3F) | 0x80));
return 2;
}
if (ch < 0x10000) {
APPEND_CHAR((SQChar)((ch >> 12) | 0xE0));
APPEND_CHAR((SQChar)(((ch >> 6) & 0x3F) | 0x80));
APPEND_CHAR((SQChar)((ch & 0x3F) | 0x80));
return 3;
}
if (ch < 0x110000) {
APPEND_CHAR((SQChar)((ch >> 18) | 0xF0));
APPEND_CHAR((SQChar)(((ch >> 12) & 0x3F) | 0x80));
APPEND_CHAR((SQChar)(((ch >> 6) & 0x3F) | 0x80));
APPEND_CHAR((SQChar)((ch & 0x3F) | 0x80));
return 4;
}
return 0;
}
#endif
SQInteger SQLexer::ProcessStringHexEscape(SQChar *dest, SQInteger maxdigits)
{
NEXT();
if (!isxdigit(CUR_CHAR)) Error(_SC("hexadecimal number expected"));
SQInteger n = 0;
while (isxdigit(CUR_CHAR) && n < maxdigits) {
dest[n] = CUR_CHAR;
n++;
NEXT();
}
dest[n] = 0;
return n;
}
SQInteger SQLexer::ReadString(SQInteger ndelim,bool verbatim)
{
INIT_TEMP_STRING();
NEXT();
if(IS_EOB()) return -1;
for(;;) {
while(CUR_CHAR != ndelim) {
switch(CUR_CHAR) {
case SQUIRREL_EOB:
Error(_SC("unfinished string"));
return -1;
case _SC('\n'):
if(!verbatim) Error(_SC("newline in a constant"));
APPEND_CHAR(CUR_CHAR); NEXT();
_currentline++;
break;
case _SC('\\'):
if(verbatim) {
APPEND_CHAR('\\'); NEXT();
}
else {
NEXT();
switch(CUR_CHAR) {
case _SC('x'): NEXT(); {
if(!isxdigit(CUR_CHAR)) Error(_SC("hexadecimal number expected"));
const SQInteger maxdigits = 4;
SQChar temp[maxdigits+1];
SQInteger n = 0;
while(isxdigit(CUR_CHAR) && n < maxdigits) {
temp[n] = CUR_CHAR;
n++;
NEXT();
}
temp[n] = 0;
SQChar *sTemp;
APPEND_CHAR((SQChar)scstrtoul(temp,&sTemp,16));
}
break;
case _SC('t'): APPEND_CHAR(_SC('\t')); NEXT(); break;
case _SC('a'): APPEND_CHAR(_SC('\a')); NEXT(); break;
case _SC('b'): APPEND_CHAR(_SC('\b')); NEXT(); break;
case _SC('n'): APPEND_CHAR(_SC('\n')); NEXT(); break;
case _SC('r'): APPEND_CHAR(_SC('\r')); NEXT(); break;
case _SC('v'): APPEND_CHAR(_SC('\v')); NEXT(); break;
case _SC('f'): APPEND_CHAR(_SC('\f')); NEXT(); break;
case _SC('0'): APPEND_CHAR(_SC('\0')); NEXT(); break;
case _SC('\\'): APPEND_CHAR(_SC('\\')); NEXT(); break;
case _SC('"'): APPEND_CHAR(_SC('"')); NEXT(); break;
case _SC('\''): APPEND_CHAR(_SC('\'')); NEXT(); break;
default:
Error(_SC("unrecognised escaper char"));
break;
}
}
break;
default:
APPEND_CHAR(CUR_CHAR);
NEXT();
}
}
NEXT();
if(verbatim && CUR_CHAR == '"') { //double quotation
APPEND_CHAR(CUR_CHAR);
NEXT();
}
else {
break;
}
}
TERMINATE_BUFFER();
SQInteger len = _longstr.size()-1;
if(ndelim == _SC('\'')) {
if(len == 0) Error(_SC("empty constant"));
if(len > 1) Error(_SC("constant too long"));
_nvalue = _longstr[0];
return TK_INTEGER;
}
_svalue = &_longstr[0];
return TK_STRING_LITERAL;
INIT_TEMP_STRING();
NEXT();
if(IS_EOB()) return -1;
for(;;) {
while(CUR_CHAR != ndelim) {
SQInteger x = CUR_CHAR;
switch (x) {
case SQUIRREL_EOB:
Error(_SC("unfinished string"));
return -1;
case _SC('\n'):
if(!verbatim) Error(_SC("newline in a constant"));
APPEND_CHAR(CUR_CHAR); NEXT();
_currentline++;
break;
case _SC('\\'):
if(verbatim) {
APPEND_CHAR('\\'); NEXT();
}
else {
NEXT();
switch(CUR_CHAR) {
case _SC('x'): {
const SQInteger maxdigits = sizeof(SQChar) * 2;
SQChar temp[maxdigits + 1];
ProcessStringHexEscape(temp, maxdigits);
SQChar *stemp;
APPEND_CHAR((SQChar)scstrtoul(temp, &stemp, 16));
}
break;
case _SC('U'):
case _SC('u'): {
const SQInteger maxdigits = x == 'u' ? 4 : 8;
SQChar temp[8 + 1];
ProcessStringHexEscape(temp, maxdigits);
SQChar *stemp;
#ifdef SQUNICODE
#if WCHAR_SIZE == 2
AddUTF16(scstrtoul(temp, &stemp, 16));
#else
ADD_CHAR((SQChar)scstrtoul(temp, &stemp, 16));
#endif
#else
AddUTF8(scstrtoul(temp, &stemp, 16));
#endif
}
break;
case _SC('t'): APPEND_CHAR(_SC('\t')); NEXT(); break;
case _SC('a'): APPEND_CHAR(_SC('\a')); NEXT(); break;
case _SC('b'): APPEND_CHAR(_SC('\b')); NEXT(); break;
case _SC('n'): APPEND_CHAR(_SC('\n')); NEXT(); break;
case _SC('r'): APPEND_CHAR(_SC('\r')); NEXT(); break;
case _SC('v'): APPEND_CHAR(_SC('\v')); NEXT(); break;
case _SC('f'): APPEND_CHAR(_SC('\f')); NEXT(); break;
case _SC('0'): APPEND_CHAR(_SC('\0')); NEXT(); break;
case _SC('\\'): APPEND_CHAR(_SC('\\')); NEXT(); break;
case _SC('"'): APPEND_CHAR(_SC('"')); NEXT(); break;
case _SC('\''): APPEND_CHAR(_SC('\'')); NEXT(); break;
default:
Error(_SC("unrecognised escaper char"));
break;
}
}
break;
default:
APPEND_CHAR(CUR_CHAR);
NEXT();
}
}
NEXT();
if(verbatim && CUR_CHAR == '"') { //double quotation
APPEND_CHAR(CUR_CHAR);
NEXT();
}
else {
break;
}
}
TERMINATE_BUFFER();
SQInteger len = _longstr.size()-1;
if(ndelim == _SC('\'')) {
if(len == 0) Error(_SC("empty constant"));
if(len > 1) Error(_SC("constant too long"));
_nvalue = _longstr[0];
return TK_INTEGER;
}
_svalue = &_longstr[0];
return TK_STRING_LITERAL;
}
void LexHexadecimal(const SQChar *s,SQUnsignedInteger *res)
{
*res = 0;
while(*s != 0)
{
if(scisdigit(*s)) *res = (*res)*16+((*s++)-'0');
else if(scisxdigit(*s)) *res = (*res)*16+(toupper(*s++)-'A'+10);
else { assert(0); }
}
*res = 0;
while(*s != 0)
{
if(scisdigit(*s)) *res = (*res)*16+((*s++)-'0');
else if(scisxdigit(*s)) *res = (*res)*16+(toupper(*s++)-'A'+10);
else { assert(0); }
}
}
void LexInteger(const SQChar *s,SQUnsignedInteger *res)
{
*res = 0;
while(*s != 0)
{
*res = (*res)*10+((*s++)-'0');
}
*res = 0;
while(*s != 0)
{
*res = (*res)*10+((*s++)-'0');
}
}
SQInteger scisodigit(SQInteger c) { return c >= _SC('0') && c <= _SC('7'); }
void LexOctal(const SQChar *s,SQUnsignedInteger *res)
{
*res = 0;
while(*s != 0)
{
if(scisodigit(*s)) *res = (*res)*8+((*s++)-'0');
else { assert(0); }
}
*res = 0;
while(*s != 0)
{
if(scisodigit(*s)) *res = (*res)*8+((*s++)-'0');
else { assert(0); }
}
}
SQInteger isexponent(SQInteger c) { return c == 'e' || c=='E'; }
@ -412,80 +482,80 @@ SQInteger SQLexer::ReadNumber()
#define THEX 3
#define TSCIENTIFIC 4
#define TOCTAL 5
SQInteger type = TINT, firstchar = CUR_CHAR;
SQChar *sTemp;
INIT_TEMP_STRING();
NEXT();
if(firstchar == _SC('0') && (toupper(CUR_CHAR) == _SC('X') || scisodigit(CUR_CHAR)) ) {
if(scisodigit(CUR_CHAR)) {
type = TOCTAL;
while(scisodigit(CUR_CHAR)) {
APPEND_CHAR(CUR_CHAR);
NEXT();
}
if(scisdigit(CUR_CHAR)) Error(_SC("invalid octal number"));
}
else {
NEXT();
type = THEX;
while(isxdigit(CUR_CHAR)) {
APPEND_CHAR(CUR_CHAR);
NEXT();
}
if(_longstr.size() > MAX_HEX_DIGITS) Error(_SC("too many digits for an Hex number"));
}
}
else {
APPEND_CHAR((int)firstchar);
while (CUR_CHAR == _SC('.') || scisdigit(CUR_CHAR) || isexponent(CUR_CHAR)) {
SQInteger type = TINT, firstchar = CUR_CHAR;
SQChar *sTemp;
INIT_TEMP_STRING();
NEXT();
if(firstchar == _SC('0') && (toupper(CUR_CHAR) == _SC('X') || scisodigit(CUR_CHAR)) ) {
if(scisodigit(CUR_CHAR)) {
type = TOCTAL;
while(scisodigit(CUR_CHAR)) {
APPEND_CHAR(CUR_CHAR);
NEXT();
}
if(scisdigit(CUR_CHAR)) Error(_SC("invalid octal number"));
}
else {
NEXT();
type = THEX;
while(isxdigit(CUR_CHAR)) {
APPEND_CHAR(CUR_CHAR);
NEXT();
}
if(_longstr.size() > MAX_HEX_DIGITS) Error(_SC("too many digits for an Hex number"));
}
}
else {
APPEND_CHAR((int)firstchar);
while (CUR_CHAR == _SC('.') || scisdigit(CUR_CHAR) || isexponent(CUR_CHAR)) {
if(CUR_CHAR == _SC('.') || isexponent(CUR_CHAR)) type = TFLOAT;
if(isexponent(CUR_CHAR)) {
if(type != TFLOAT) Error(_SC("invalid numeric format"));
type = TSCIENTIFIC;
APPEND_CHAR(CUR_CHAR);
NEXT();
if(CUR_CHAR == '+' || CUR_CHAR == '-'){
APPEND_CHAR(CUR_CHAR);
NEXT();
}
if(!scisdigit(CUR_CHAR)) Error(_SC("exponent expected"));
}
if(isexponent(CUR_CHAR)) {
if(type != TFLOAT) Error(_SC("invalid numeric format"));
type = TSCIENTIFIC;
APPEND_CHAR(CUR_CHAR);
NEXT();
if(CUR_CHAR == '+' || CUR_CHAR == '-'){
APPEND_CHAR(CUR_CHAR);
NEXT();
}
if(!scisdigit(CUR_CHAR)) Error(_SC("exponent expected"));
}
APPEND_CHAR(CUR_CHAR);
NEXT();
}
}
TERMINATE_BUFFER();
switch(type) {
case TSCIENTIFIC:
case TFLOAT:
_fvalue = (SQFloat)scstrtod(&_longstr[0],&sTemp);
return TK_FLOAT;
case TINT:
LexInteger(&_longstr[0],(SQUnsignedInteger *)&_nvalue);
return TK_INTEGER;
case THEX:
LexHexadecimal(&_longstr[0],(SQUnsignedInteger *)&_nvalue);
return TK_INTEGER;
case TOCTAL:
LexOctal(&_longstr[0],(SQUnsignedInteger *)&_nvalue);
return TK_INTEGER;
}
return 0;
APPEND_CHAR(CUR_CHAR);
NEXT();
}
}
TERMINATE_BUFFER();
switch(type) {
case TSCIENTIFIC:
case TFLOAT:
_fvalue = (SQFloat)scstrtod(&_longstr[0],&sTemp);
return TK_FLOAT;
case TINT:
LexInteger(&_longstr[0],(SQUnsignedInteger *)&_nvalue);
return TK_INTEGER;
case THEX:
LexHexadecimal(&_longstr[0],(SQUnsignedInteger *)&_nvalue);
return TK_INTEGER;
case TOCTAL:
LexOctal(&_longstr[0],(SQUnsignedInteger *)&_nvalue);
return TK_INTEGER;
}
return 0;
}
SQInteger SQLexer::ReadID()
{
SQInteger res;
INIT_TEMP_STRING();
do {
APPEND_CHAR(CUR_CHAR);
NEXT();
} while(scisalnum(CUR_CHAR) || CUR_CHAR == _SC('_'));
TERMINATE_BUFFER();
res = GetIDType(&_longstr[0],_longstr.size() - 1);
if(res == TK_IDENTIFIER || res == TK_CONSTRUCTOR) {
_svalue = &_longstr[0];
}
return res;
SQInteger res;
INIT_TEMP_STRING();
do {
APPEND_CHAR(CUR_CHAR);
NEXT();
} while(scisalnum(CUR_CHAR) || CUR_CHAR == _SC('_'));
TERMINATE_BUFFER();
res = GetIDType(&_longstr[0],_longstr.size() - 1);
if(res == TK_IDENTIFIER || res == TK_CONSTRUCTOR) {
_svalue = &_longstr[0];
}
return res;
}

View File

@ -1,47 +1,55 @@
/* see copyright notice in squirrel.h */
/* see copyright notice in squirrel.h */
#ifndef _SQLEXER_H_
#define _SQLEXER_H_
#ifdef SQUNICODE
typedef SQChar LexChar;
#else
typedef unsigned char LexChar;
typedef unsigned char LexChar;
#endif
struct SQLexer
{
SQLexer();
~SQLexer();
void Init(SQSharedState *ss,SQLEXREADFUNC rg,SQUserPointer up,CompilerErrorFunc efunc,void *ed);
void Error(const SQChar *err);
SQInteger Lex();
const SQChar *Tok2Str(SQInteger tok);
SQLexer();
~SQLexer();
void Init(SQSharedState *ss,SQLEXREADFUNC rg,SQUserPointer up,CompilerErrorFunc efunc,void *ed);
void Error(const SQChar *err);
SQInteger Lex();
const SQChar *Tok2Str(SQInteger tok);
private:
SQInteger GetIDType(const SQChar *s,SQInteger len);
SQInteger ReadString(SQInteger ndelim,bool verbatim);
SQInteger ReadNumber();
void LexBlockComment();
void LexLineComment();
SQInteger ReadID();
void Next();
SQInteger _curtoken;
SQTable *_keywords;
SQBool _reached_eof;
SQInteger GetIDType(const SQChar *s,SQInteger len);
SQInteger ReadString(SQInteger ndelim,bool verbatim);
SQInteger ReadNumber();
void LexBlockComment();
void LexLineComment();
SQInteger ReadID();
void Next();
#ifdef SQUNICODE
#if WCHAR_SIZE == 2
SQInteger AddUTF16(SQUnsignedInteger ch);
#endif
#else
SQInteger AddUTF8(SQUnsignedInteger ch);
#endif
SQInteger ProcessStringHexEscape(SQChar *dest, SQInteger maxdigits);
SQInteger _curtoken;
SQTable *_keywords;
SQBool _reached_eof;
public:
SQInteger _prevtoken;
SQInteger _currentline;
SQInteger _lasttokenline;
SQInteger _currentcolumn;
const SQChar *_svalue;
SQInteger _nvalue;
SQFloat _fvalue;
SQLEXREADFUNC _readf;
SQUserPointer _up;
LexChar _currdata;
SQSharedState *_sharedstate;
sqvector<SQChar> _longstr;
CompilerErrorFunc _errfunc;
void *_errtarget;
SQInteger _prevtoken;
SQInteger _currentline;
SQInteger _lasttokenline;
SQInteger _currentcolumn;
const SQChar *_svalue;
SQInteger _nvalue;
SQFloat _fvalue;
SQLEXREADFUNC _readf;
SQUserPointer _up;
LexChar _currdata;
SQSharedState *_sharedstate;
sqvector<SQChar> _longstr;
CompilerErrorFunc _errfunc;
void *_errtarget;
};
#endif

View File

@ -1,11 +1,11 @@
/*
see copyright notice in squirrel.h
see copyright notice in squirrel.h
*/
#include "sqpcheader.h"
#ifndef SQ_EXCLUDE_DEFAULT_MEMFUNCTIONS
void *sq_vm_malloc(SQUnsignedInteger size){ return malloc(size); }
void *sq_vm_malloc(SQUnsignedInteger size){ return malloc(size); }
void *sq_vm_realloc(void *p, SQUnsignedInteger oldsize, SQUnsignedInteger size){ return realloc(p, size); }
void *sq_vm_realloc(void *p, SQUnsignedInteger SQ_UNUSED_ARG(oldsize), SQUnsignedInteger size){ return realloc(p, size); }
void sq_vm_free(void *p, SQUnsignedInteger size){ free(p); }
void sq_vm_free(void *p, SQUnsignedInteger SQ_UNUSED_ARG(size)){ free(p); }
#endif

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
/* see copyright notice in squirrel.h */
/* see copyright notice in squirrel.h */
#ifndef _SQOBJECT_H_
#define _SQOBJECT_H_
@ -17,115 +17,115 @@
struct SQSharedState;
enum SQMetaMethod{
MT_ADD=0,
MT_SUB=1,
MT_MUL=2,
MT_DIV=3,
MT_UNM=4,
MT_MODULO=5,
MT_SET=6,
MT_GET=7,
MT_TYPEOF=8,
MT_NEXTI=9,
MT_CMP=10,
MT_CALL=11,
MT_CLONED=12,
MT_NEWSLOT=13,
MT_DELSLOT=14,
MT_TOSTRING=15,
MT_NEWMEMBER=16,
MT_INHERITED=17,
MT_LAST = 18
MT_ADD=0,
MT_SUB=1,
MT_MUL=2,
MT_DIV=3,
MT_UNM=4,
MT_MODULO=5,
MT_SET=6,
MT_GET=7,
MT_TYPEOF=8,
MT_NEXTI=9,
MT_CMP=10,
MT_CALL=11,
MT_CLONED=12,
MT_NEWSLOT=13,
MT_DELSLOT=14,
MT_TOSTRING=15,
MT_NEWMEMBER=16,
MT_INHERITED=17,
MT_LAST = 18
};
#define MM_ADD _SC("_add")
#define MM_SUB _SC("_sub")
#define MM_MUL _SC("_mul")
#define MM_DIV _SC("_div")
#define MM_UNM _SC("_unm")
#define MM_MODULO _SC("_modulo")
#define MM_SET _SC("_set")
#define MM_GET _SC("_get")
#define MM_TYPEOF _SC("_typeof")
#define MM_NEXTI _SC("_nexti")
#define MM_CMP _SC("_cmp")
#define MM_CALL _SC("_call")
#define MM_CLONED _SC("_cloned")
#define MM_NEWSLOT _SC("_newslot")
#define MM_DELSLOT _SC("_delslot")
#define MM_TOSTRING _SC("_tostring")
#define MM_ADD _SC("_add")
#define MM_SUB _SC("_sub")
#define MM_MUL _SC("_mul")
#define MM_DIV _SC("_div")
#define MM_UNM _SC("_unm")
#define MM_MODULO _SC("_modulo")
#define MM_SET _SC("_set")
#define MM_GET _SC("_get")
#define MM_TYPEOF _SC("_typeof")
#define MM_NEXTI _SC("_nexti")
#define MM_CMP _SC("_cmp")
#define MM_CALL _SC("_call")
#define MM_CLONED _SC("_cloned")
#define MM_NEWSLOT _SC("_newslot")
#define MM_DELSLOT _SC("_delslot")
#define MM_TOSTRING _SC("_tostring")
#define MM_NEWMEMBER _SC("_newmember")
#define MM_INHERITED _SC("_inherited")
#define _CONSTRUCT_VECTOR(type,size,ptr) { \
for(SQInteger n = 0; n < ((SQInteger)size); n++) { \
new (&ptr[n]) type(); \
} \
for(SQInteger n = 0; n < ((SQInteger)size); n++) { \
new (&ptr[n]) type(); \
} \
}
#define _DESTRUCT_VECTOR(type,size,ptr) { \
for(SQInteger nl = 0; nl < ((SQInteger)size); nl++) { \
ptr[nl].~type(); \
} \
for(SQInteger nl = 0; nl < ((SQInteger)size); nl++) { \
ptr[nl].~type(); \
} \
}
#define _COPY_VECTOR(dest,src,size) { \
for(SQInteger _n_ = 0; _n_ < ((SQInteger)size); _n_++) { \
dest[_n_] = src[_n_]; \
} \
for(SQInteger _n_ = 0; _n_ < ((SQInteger)size); _n_++) { \
dest[_n_] = src[_n_]; \
} \
}
#define _NULL_SQOBJECT_VECTOR(vec,size) { \
for(SQInteger _n_ = 0; _n_ < ((SQInteger)size); _n_++) { \
vec[_n_].Null(); \
} \
for(SQInteger _n_ = 0; _n_ < ((SQInteger)size); _n_++) { \
vec[_n_].Null(); \
} \
}
#define MINPOWER2 4
struct SQRefCounted
{
SQUnsignedInteger _uiRef;
struct SQWeakRef *_weakref;
SQRefCounted() { _uiRef = 0; _weakref = NULL; }
virtual ~SQRefCounted();
SQWeakRef *GetWeakRef(SQObjectType type);
virtual void Release()=0;
SQUnsignedInteger _uiRef;
struct SQWeakRef *_weakref;
SQRefCounted() { _uiRef = 0; _weakref = NULL; }
virtual ~SQRefCounted();
SQWeakRef *GetWeakRef(SQObjectType type);
virtual void Release()=0;
};
struct SQWeakRef : SQRefCounted
{
void Release();
SQObject _obj;
void Release();
SQObject _obj;
};
#define _realval(o) (type((o)) != OT_WEAKREF?(SQObject)o:_weakref(o)->_obj)
struct SQObjectPtr;
#define __AddRef(type,unval) if(ISREFCOUNTED(type)) \
{ \
unval.pRefCounted->_uiRef++; \
}
#define __AddRef(type,unval) if(ISREFCOUNTED(type)) \
{ \
unval.pRefCounted->_uiRef++; \
}
#define __Release(type,unval) if(ISREFCOUNTED(type) && ((--unval.pRefCounted->_uiRef)==0)) \
{ \
unval.pRefCounted->Release(); \
}
#define __Release(type,unval) if(ISREFCOUNTED(type) && ((--unval.pRefCounted->_uiRef)==0)) \
{ \
unval.pRefCounted->Release(); \
}
#define __ObjRelease(obj) { \
if((obj)) { \
(obj)->_uiRef--; \
if((obj)->_uiRef == 0) \
(obj)->Release(); \
(obj) = NULL; \
} \
if((obj)) { \
(obj)->_uiRef--; \
if((obj)->_uiRef == 0) \
(obj)->Release(); \
(obj) = NULL; \
} \
}
#define __ObjAddRef(obj) { \
(obj)->_uiRef++; \
(obj)->_uiRef++; \
}
#define type(obj) ((obj)._type)
@ -160,168 +160,168 @@ struct SQObjectPtr;
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
#if defined(SQUSEDOUBLE) && !defined(_SQ64) || !defined(SQUSEDOUBLE) && defined(_SQ64)
#define SQ_REFOBJECT_INIT() SQ_OBJECT_RAWINIT()
#define SQ_REFOBJECT_INIT() SQ_OBJECT_RAWINIT()
#else
#define SQ_REFOBJECT_INIT()
#endif
#define _REF_TYPE_DECL(type,_class,sym) \
SQObjectPtr(_class * x) \
{ \
SQ_OBJECT_RAWINIT() \
_type=type; \
_unVal.sym = x; \
assert(_unVal.pTable); \
_unVal.pRefCounted->_uiRef++; \
} \
inline SQObjectPtr& operator=(_class *x) \
{ \
SQObjectType tOldType; \
SQObjectValue unOldVal; \
tOldType=_type; \
unOldVal=_unVal; \
_type = type; \
SQ_REFOBJECT_INIT() \
_unVal.sym = x; \
_unVal.pRefCounted->_uiRef++; \
__Release(tOldType,unOldVal); \
return *this; \
}
SQObjectPtr(_class * x) \
{ \
SQ_OBJECT_RAWINIT() \
_type=type; \
_unVal.sym = x; \
assert(_unVal.pTable); \
_unVal.pRefCounted->_uiRef++; \
} \
inline SQObjectPtr& operator=(_class *x) \
{ \
SQObjectType tOldType; \
SQObjectValue unOldVal; \
tOldType=_type; \
unOldVal=_unVal; \
_type = type; \
SQ_REFOBJECT_INIT() \
_unVal.sym = x; \
_unVal.pRefCounted->_uiRef++; \
__Release(tOldType,unOldVal); \
return *this; \
}
#define _SCALAR_TYPE_DECL(type,_class,sym) \
SQObjectPtr(_class x) \
{ \
SQ_OBJECT_RAWINIT() \
_type=type; \
_unVal.sym = x; \
} \
inline SQObjectPtr& operator=(_class x) \
{ \
__Release(_type,_unVal); \
_type = type; \
SQ_OBJECT_RAWINIT() \
_unVal.sym = x; \
return *this; \
}
SQObjectPtr(_class x) \
{ \
SQ_OBJECT_RAWINIT() \
_type=type; \
_unVal.sym = x; \
} \
inline SQObjectPtr& operator=(_class x) \
{ \
__Release(_type,_unVal); \
_type = type; \
SQ_OBJECT_RAWINIT() \
_unVal.sym = x; \
return *this; \
}
struct SQObjectPtr : public SQObject
{
SQObjectPtr()
{
SQ_OBJECT_RAWINIT()
_type=OT_NULL;
_unVal.pUserPointer=NULL;
}
SQObjectPtr(const SQObjectPtr &o)
{
_type = o._type;
_unVal = o._unVal;
__AddRef(_type,_unVal);
}
SQObjectPtr(const SQObject &o)
{
_type = o._type;
_unVal = o._unVal;
__AddRef(_type,_unVal);
}
_REF_TYPE_DECL(OT_TABLE,SQTable,pTable)
_REF_TYPE_DECL(OT_CLASS,SQClass,pClass)
_REF_TYPE_DECL(OT_INSTANCE,SQInstance,pInstance)
_REF_TYPE_DECL(OT_ARRAY,SQArray,pArray)
_REF_TYPE_DECL(OT_CLOSURE,SQClosure,pClosure)
_REF_TYPE_DECL(OT_NATIVECLOSURE,SQNativeClosure,pNativeClosure)
_REF_TYPE_DECL(OT_OUTER,SQOuter,pOuter)
_REF_TYPE_DECL(OT_GENERATOR,SQGenerator,pGenerator)
_REF_TYPE_DECL(OT_STRING,SQString,pString)
_REF_TYPE_DECL(OT_USERDATA,SQUserData,pUserData)
_REF_TYPE_DECL(OT_WEAKREF,SQWeakRef,pWeakRef)
_REF_TYPE_DECL(OT_THREAD,SQVM,pThread)
_REF_TYPE_DECL(OT_FUNCPROTO,SQFunctionProto,pFunctionProto)
SQObjectPtr()
{
SQ_OBJECT_RAWINIT()
_type=OT_NULL;
_unVal.pUserPointer=NULL;
}
SQObjectPtr(const SQObjectPtr &o)
{
_type = o._type;
_unVal = o._unVal;
__AddRef(_type,_unVal);
}
SQObjectPtr(const SQObject &o)
{
_type = o._type;
_unVal = o._unVal;
__AddRef(_type,_unVal);
}
_REF_TYPE_DECL(OT_TABLE,SQTable,pTable)
_REF_TYPE_DECL(OT_CLASS,SQClass,pClass)
_REF_TYPE_DECL(OT_INSTANCE,SQInstance,pInstance)
_REF_TYPE_DECL(OT_ARRAY,SQArray,pArray)
_REF_TYPE_DECL(OT_CLOSURE,SQClosure,pClosure)
_REF_TYPE_DECL(OT_NATIVECLOSURE,SQNativeClosure,pNativeClosure)
_REF_TYPE_DECL(OT_OUTER,SQOuter,pOuter)
_REF_TYPE_DECL(OT_GENERATOR,SQGenerator,pGenerator)
_REF_TYPE_DECL(OT_STRING,SQString,pString)
_REF_TYPE_DECL(OT_USERDATA,SQUserData,pUserData)
_REF_TYPE_DECL(OT_WEAKREF,SQWeakRef,pWeakRef)
_REF_TYPE_DECL(OT_THREAD,SQVM,pThread)
_REF_TYPE_DECL(OT_FUNCPROTO,SQFunctionProto,pFunctionProto)
_SCALAR_TYPE_DECL(OT_INTEGER,SQInteger,nInteger)
_SCALAR_TYPE_DECL(OT_FLOAT,SQFloat,fFloat)
_SCALAR_TYPE_DECL(OT_USERPOINTER,SQUserPointer,pUserPointer)
_SCALAR_TYPE_DECL(OT_INTEGER,SQInteger,nInteger)
_SCALAR_TYPE_DECL(OT_FLOAT,SQFloat,fFloat)
_SCALAR_TYPE_DECL(OT_USERPOINTER,SQUserPointer,pUserPointer)
SQObjectPtr(bool bBool)
{
SQ_OBJECT_RAWINIT()
_type = OT_BOOL;
_unVal.nInteger = bBool?1:0;
}
inline SQObjectPtr& operator=(bool b)
{
__Release(_type,_unVal);
SQ_OBJECT_RAWINIT()
_type = OT_BOOL;
_unVal.nInteger = b?1:0;
return *this;
}
SQObjectPtr(bool bBool)
{
SQ_OBJECT_RAWINIT()
_type = OT_BOOL;
_unVal.nInteger = bBool?1:0;
}
inline SQObjectPtr& operator=(bool b)
{
__Release(_type,_unVal);
SQ_OBJECT_RAWINIT()
_type = OT_BOOL;
_unVal.nInteger = b?1:0;
return *this;
}
~SQObjectPtr()
{
__Release(_type,_unVal);
}
~SQObjectPtr()
{
__Release(_type,_unVal);
}
inline SQObjectPtr& operator=(const SQObjectPtr& obj)
{
SQObjectType tOldType;
SQObjectValue unOldVal;
tOldType=_type;
unOldVal=_unVal;
_unVal = obj._unVal;
_type = obj._type;
__AddRef(_type,_unVal);
__Release(tOldType,unOldVal);
return *this;
}
inline SQObjectPtr& operator=(const SQObject& obj)
{
SQObjectType tOldType;
SQObjectValue unOldVal;
tOldType=_type;
unOldVal=_unVal;
_unVal = obj._unVal;
_type = obj._type;
__AddRef(_type,_unVal);
__Release(tOldType,unOldVal);
return *this;
}
inline void Null()
{
SQObjectType tOldType = _type;
SQObjectValue unOldVal = _unVal;
_type = OT_NULL;
_unVal.raw = (SQRawObjectVal)NULL;
__Release(tOldType ,unOldVal);
}
private:
SQObjectPtr(const SQChar *){} //safety
inline SQObjectPtr& operator=(const SQObjectPtr& obj)
{
SQObjectType tOldType;
SQObjectValue unOldVal;
tOldType=_type;
unOldVal=_unVal;
_unVal = obj._unVal;
_type = obj._type;
__AddRef(_type,_unVal);
__Release(tOldType,unOldVal);
return *this;
}
inline SQObjectPtr& operator=(const SQObject& obj)
{
SQObjectType tOldType;
SQObjectValue unOldVal;
tOldType=_type;
unOldVal=_unVal;
_unVal = obj._unVal;
_type = obj._type;
__AddRef(_type,_unVal);
__Release(tOldType,unOldVal);
return *this;
}
inline void Null()
{
SQObjectType tOldType = _type;
SQObjectValue unOldVal = _unVal;
_type = OT_NULL;
_unVal.raw = (SQRawObjectVal)NULL;
__Release(tOldType ,unOldVal);
}
private:
SQObjectPtr(const SQChar *){} //safety
};
inline void _Swap(SQObject &a,SQObject &b)
{
SQObjectType tOldType = a._type;
SQObjectValue unOldVal = a._unVal;
a._type = b._type;
a._unVal = b._unVal;
b._type = tOldType;
b._unVal = unOldVal;
SQObjectType tOldType = a._type;
SQObjectValue unOldVal = a._unVal;
a._type = b._type;
a._unVal = b._unVal;
b._type = tOldType;
b._unVal = unOldVal;
}
/////////////////////////////////////////////////////////////////////////////////////
#ifndef NO_GARBAGE_COLLECTOR
#define MARK_FLAG 0x80000000
struct SQCollectable : public SQRefCounted {
SQCollectable *_next;
SQCollectable *_prev;
SQSharedState *_sharedstate;
virtual SQObjectType GetType()=0;
virtual void Release()=0;
virtual void Mark(SQCollectable **chain)=0;
void UnMark();
virtual void Finalize()=0;
static void AddToChain(SQCollectable **chain,SQCollectable *c);
static void RemoveFromChain(SQCollectable **chain,SQCollectable *c);
SQCollectable *_next;
SQCollectable *_prev;
SQSharedState *_sharedstate;
virtual SQObjectType GetType()=0;
virtual void Release()=0;
virtual void Mark(SQCollectable **chain)=0;
void UnMark();
virtual void Finalize()=0;
static void AddToChain(SQCollectable **chain,SQCollectable *c);
static void RemoveFromChain(SQCollectable **chain,SQCollectable *c);
};
@ -338,9 +338,9 @@ struct SQCollectable : public SQRefCounted {
#endif
struct SQDelegable : public CHAINABLE_OBJ {
bool SetDelegate(SQTable *m);
virtual bool GetMetaMethod(SQVM *v,SQMetaMethod mm,SQObjectPtr &res);
SQTable *_delegate;
bool SetDelegate(SQTable *m);
virtual bool GetMetaMethod(SQVM *v,SQMetaMethod mm,SQObjectPtr &res);
SQTable *_delegate;
};
SQUnsignedInteger TranslateIndex(const SQObjectPtr &idx);

View File

@ -1,4 +1,4 @@
/* see copyright notice in squirrel.h */
/* see copyright notice in squirrel.h */
#ifndef _SQOPCODES_H_
#define _SQOPCODES_H_
@ -6,127 +6,127 @@
#define MAX_LITERALS ((SQInteger)0x7FFFFFFF)
enum BitWiseOP {
BW_AND = 0,
BW_OR = 2,
BW_XOR = 3,
BW_SHIFTL = 4,
BW_SHIFTR = 5,
BW_USHIFTR = 6
BW_AND = 0,
BW_OR = 2,
BW_XOR = 3,
BW_SHIFTL = 4,
BW_SHIFTR = 5,
BW_USHIFTR = 6
};
enum CmpOP {
CMP_G = 0,
CMP_GE = 2,
CMP_L = 3,
CMP_LE = 4,
CMP_3W = 5
CMP_G = 0,
CMP_GE = 2,
CMP_L = 3,
CMP_LE = 4,
CMP_3W = 5
};
enum NewObjectType {
NOT_TABLE = 0,
NOT_ARRAY = 1,
NOT_CLASS = 2
NOT_TABLE = 0,
NOT_ARRAY = 1,
NOT_CLASS = 2
};
enum AppendArrayType {
AAT_STACK = 0,
AAT_LITERAL = 1,
AAT_INT = 2,
AAT_FLOAT = 3,
AAT_BOOL = 4
AAT_STACK = 0,
AAT_LITERAL = 1,
AAT_INT = 2,
AAT_FLOAT = 3,
AAT_BOOL = 4
};
enum SQOpcode
{
_OP_LINE= 0x00,
_OP_LOAD= 0x01,
_OP_LOADINT= 0x02,
_OP_LOADFLOAT= 0x03,
_OP_DLOAD= 0x04,
_OP_TAILCALL= 0x05,
_OP_CALL= 0x06,
_OP_PREPCALL= 0x07,
_OP_PREPCALLK= 0x08,
_OP_GETK= 0x09,
_OP_MOVE= 0x0A,
_OP_NEWSLOT= 0x0B,
_OP_DELETE= 0x0C,
_OP_SET= 0x0D,
_OP_GET= 0x0E,
_OP_EQ= 0x0F,
_OP_NE= 0x10,
_OP_ADD= 0x11,
_OP_SUB= 0x12,
_OP_MUL= 0x13,
_OP_DIV= 0x14,
_OP_MOD= 0x15,
_OP_BITW= 0x16,
_OP_RETURN= 0x17,
_OP_LOADNULLS= 0x18,
_OP_LOADROOT= 0x19,
_OP_LOADBOOL= 0x1A,
_OP_DMOVE= 0x1B,
_OP_JMP= 0x1C,
//_OP_JNZ= 0x1D,
_OP_JCMP= 0x1D,
_OP_JZ= 0x1E,
_OP_SETOUTER= 0x1F,
_OP_GETOUTER= 0x20,
_OP_NEWOBJ= 0x21,
_OP_APPENDARRAY= 0x22,
_OP_COMPARITH= 0x23,
_OP_INC= 0x24,
_OP_INCL= 0x25,
_OP_PINC= 0x26,
_OP_PINCL= 0x27,
_OP_CMP= 0x28,
_OP_EXISTS= 0x29,
_OP_INSTANCEOF= 0x2A,
_OP_AND= 0x2B,
_OP_OR= 0x2C,
_OP_NEG= 0x2D,
_OP_NOT= 0x2E,
_OP_BWNOT= 0x2F,
_OP_CLOSURE= 0x30,
_OP_YIELD= 0x31,
_OP_RESUME= 0x32,
_OP_FOREACH= 0x33,
_OP_POSTFOREACH= 0x34,
_OP_CLONE= 0x35,
_OP_TYPEOF= 0x36,
_OP_PUSHTRAP= 0x37,
_OP_POPTRAP= 0x38,
_OP_THROW= 0x39,
_OP_NEWSLOTA= 0x3A,
_OP_GETBASE= 0x3B,
_OP_CLOSE= 0x3C
_OP_LINE= 0x00,
_OP_LOAD= 0x01,
_OP_LOADINT= 0x02,
_OP_LOADFLOAT= 0x03,
_OP_DLOAD= 0x04,
_OP_TAILCALL= 0x05,
_OP_CALL= 0x06,
_OP_PREPCALL= 0x07,
_OP_PREPCALLK= 0x08,
_OP_GETK= 0x09,
_OP_MOVE= 0x0A,
_OP_NEWSLOT= 0x0B,
_OP_DELETE= 0x0C,
_OP_SET= 0x0D,
_OP_GET= 0x0E,
_OP_EQ= 0x0F,
_OP_NE= 0x10,
_OP_ADD= 0x11,
_OP_SUB= 0x12,
_OP_MUL= 0x13,
_OP_DIV= 0x14,
_OP_MOD= 0x15,
_OP_BITW= 0x16,
_OP_RETURN= 0x17,
_OP_LOADNULLS= 0x18,
_OP_LOADROOT= 0x19,
_OP_LOADBOOL= 0x1A,
_OP_DMOVE= 0x1B,
_OP_JMP= 0x1C,
//_OP_JNZ= 0x1D,
_OP_JCMP= 0x1D,
_OP_JZ= 0x1E,
_OP_SETOUTER= 0x1F,
_OP_GETOUTER= 0x20,
_OP_NEWOBJ= 0x21,
_OP_APPENDARRAY= 0x22,
_OP_COMPARITH= 0x23,
_OP_INC= 0x24,
_OP_INCL= 0x25,
_OP_PINC= 0x26,
_OP_PINCL= 0x27,
_OP_CMP= 0x28,
_OP_EXISTS= 0x29,
_OP_INSTANCEOF= 0x2A,
_OP_AND= 0x2B,
_OP_OR= 0x2C,
_OP_NEG= 0x2D,
_OP_NOT= 0x2E,
_OP_BWNOT= 0x2F,
_OP_CLOSURE= 0x30,
_OP_YIELD= 0x31,
_OP_RESUME= 0x32,
_OP_FOREACH= 0x33,
_OP_POSTFOREACH= 0x34,
_OP_CLONE= 0x35,
_OP_TYPEOF= 0x36,
_OP_PUSHTRAP= 0x37,
_OP_POPTRAP= 0x38,
_OP_THROW= 0x39,
_OP_NEWSLOTA= 0x3A,
_OP_GETBASE= 0x3B,
_OP_CLOSE= 0x3C
};
struct SQInstructionDesc {
const SQChar *name;
const SQChar *name;
};
struct SQInstruction
{
SQInstruction(){};
SQInstruction(SQOpcode _op,SQInteger a0=0,SQInteger a1=0,SQInteger a2=0,SQInteger a3=0)
{ op = _op;
_arg0 = (unsigned char)a0;_arg1 = (SQInt32)a1;
_arg2 = (unsigned char)a2;_arg3 = (unsigned char)a3;
}
SQInstruction(){};
SQInstruction(SQOpcode _op,SQInteger a0=0,SQInteger a1=0,SQInteger a2=0,SQInteger a3=0)
{ op = (unsigned char)_op;
_arg0 = (unsigned char)a0;_arg1 = (SQInt32)a1;
_arg2 = (unsigned char)a2;_arg3 = (unsigned char)a3;
}
SQInt32 _arg1;
unsigned char op;
unsigned char _arg0;
unsigned char _arg2;
unsigned char _arg3;
SQInt32 _arg1;
unsigned char op;
unsigned char _arg0;
unsigned char _arg2;
unsigned char _arg3;
};
#include "squtils.h"
typedef sqvector<SQInstruction> SQInstructionVec;
#define NEW_SLOT_ATTRIBUTES_FLAG 0x01
#define NEW_SLOT_STATIC_FLAG 0x02
#define NEW_SLOT_ATTRIBUTES_FLAG 0x01
#define NEW_SLOT_STATIC_FLAG 0x02
#endif // _SQOPCODES_H_

View File

@ -1,4 +1,4 @@
/* see copyright notice in squirrel.h */
/* see copyright notice in squirrel.h */
#ifndef _SQPCHEADER_H_
#define _SQPCHEADER_H_

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
/* see copyright notice in squirrel.h */
/* see copyright notice in squirrel.h */
#ifndef _SQSTATE_H_
#define _SQSTATE_H_
@ -11,44 +11,44 @@ struct SQTable;
struct SQStringTable
{
SQStringTable(SQSharedState*ss);
~SQStringTable();
SQString *Add(const SQChar *,SQInteger len);
void Remove(SQString *);
SQStringTable(SQSharedState*ss);
~SQStringTable();
SQString *Add(const SQChar *,SQInteger len);
void Remove(SQString *);
private:
void Resize(SQInteger size);
void AllocNodes(SQInteger size);
SQString **_strings;
SQUnsignedInteger _numofslots;
SQUnsignedInteger _slotused;
SQSharedState *_sharedstate;
void Resize(SQInteger size);
void AllocNodes(SQInteger size);
SQString **_strings;
SQUnsignedInteger _numofslots;
SQUnsignedInteger _slotused;
SQSharedState *_sharedstate;
};
struct RefTable {
struct RefNode {
SQObjectPtr obj;
SQUnsignedInteger refs;
struct RefNode *next;
};
RefTable();
~RefTable();
void AddRef(SQObject &obj);
SQBool Release(SQObject &obj);
SQUnsignedInteger GetRefCount(SQObject &obj);
struct RefNode {
SQObjectPtr obj;
SQUnsignedInteger refs;
struct RefNode *next;
};
RefTable();
~RefTable();
void AddRef(SQObject &obj);
SQBool Release(SQObject &obj);
SQUnsignedInteger GetRefCount(SQObject &obj);
#ifndef NO_GARBAGE_COLLECTOR
void Mark(SQCollectable **chain);
void Mark(SQCollectable **chain);
#endif
void Finalize();
void Finalize();
private:
RefNode *Get(SQObject &obj,SQHash &mainpos,RefNode **prev,bool add);
RefNode *Add(SQHash mainpos,SQObject &obj);
void Resize(SQUnsignedInteger size);
void AllocNodes(SQUnsignedInteger size);
SQUnsignedInteger _numofslots;
SQUnsignedInteger _slotused;
RefNode *_nodes;
RefNode *_freelist;
RefNode **_buckets;
RefNode *Get(SQObject &obj,SQHash &mainpos,RefNode **prev,bool add);
RefNode *Add(SQHash mainpos,SQObject &obj);
void Resize(SQUnsignedInteger size);
void AllocNodes(SQUnsignedInteger size);
SQUnsignedInteger _numofslots;
SQUnsignedInteger _slotused;
RefNode *_nodes;
RefNode *_freelist;
RefNode **_buckets;
};
#define ADD_STRING(ss,str,len) ss->_stringtable->Add(str,len)
@ -58,77 +58,77 @@ struct SQObjectPtr;
struct SQSharedState
{
SQSharedState();
~SQSharedState();
void Init();
SQSharedState();
~SQSharedState();
void Init();
public:
SQChar* GetScratchPad(SQInteger size);
SQInteger GetMetaMethodIdxByName(const SQObjectPtr &name);
SQChar* GetScratchPad(SQInteger size);
SQInteger GetMetaMethodIdxByName(const SQObjectPtr &name);
#ifndef NO_GARBAGE_COLLECTOR
SQInteger CollectGarbage(SQVM *vm);
void RunMark(SQVM *vm,SQCollectable **tchain);
SQInteger ResurrectUnreachable(SQVM *vm);
static void MarkObject(SQObjectPtr &o,SQCollectable **chain);
SQInteger CollectGarbage(SQVM *vm);
void RunMark(SQVM *vm,SQCollectable **tchain);
SQInteger ResurrectUnreachable(SQVM *vm);
static void MarkObject(SQObjectPtr &o,SQCollectable **chain);
#endif
SQObjectPtrVec *_metamethods;
SQObjectPtr _metamethodsmap;
SQObjectPtrVec *_systemstrings;
SQObjectPtrVec *_types;
SQStringTable *_stringtable;
RefTable _refs_table;
SQObjectPtr _registry;
SQObjectPtr _consts;
SQObjectPtr _constructoridx;
SQObjectPtrVec *_metamethods;
SQObjectPtr _metamethodsmap;
SQObjectPtrVec *_systemstrings;
SQObjectPtrVec *_types;
SQStringTable *_stringtable;
RefTable _refs_table;
SQObjectPtr _registry;
SQObjectPtr _consts;
SQObjectPtr _constructoridx;
#ifndef NO_GARBAGE_COLLECTOR
SQCollectable *_gc_chain;
SQCollectable *_gc_chain;
#endif
SQObjectPtr _root_vm;
SQObjectPtr _table_default_delegate;
static SQRegFunction _table_default_delegate_funcz[];
SQObjectPtr _array_default_delegate;
static SQRegFunction _array_default_delegate_funcz[];
SQObjectPtr _string_default_delegate;
static SQRegFunction _string_default_delegate_funcz[];
SQObjectPtr _number_default_delegate;
static SQRegFunction _number_default_delegate_funcz[];
SQObjectPtr _generator_default_delegate;
static SQRegFunction _generator_default_delegate_funcz[];
SQObjectPtr _closure_default_delegate;
static SQRegFunction _closure_default_delegate_funcz[];
SQObjectPtr _thread_default_delegate;
static SQRegFunction _thread_default_delegate_funcz[];
SQObjectPtr _class_default_delegate;
static SQRegFunction _class_default_delegate_funcz[];
SQObjectPtr _instance_default_delegate;
static SQRegFunction _instance_default_delegate_funcz[];
SQObjectPtr _weakref_default_delegate;
static SQRegFunction _weakref_default_delegate_funcz[];
SQObjectPtr _root_vm;
SQObjectPtr _table_default_delegate;
static const SQRegFunction _table_default_delegate_funcz[];
SQObjectPtr _array_default_delegate;
static const SQRegFunction _array_default_delegate_funcz[];
SQObjectPtr _string_default_delegate;
static const SQRegFunction _string_default_delegate_funcz[];
SQObjectPtr _number_default_delegate;
static const SQRegFunction _number_default_delegate_funcz[];
SQObjectPtr _generator_default_delegate;
static const SQRegFunction _generator_default_delegate_funcz[];
SQObjectPtr _closure_default_delegate;
static const SQRegFunction _closure_default_delegate_funcz[];
SQObjectPtr _thread_default_delegate;
static const SQRegFunction _thread_default_delegate_funcz[];
SQObjectPtr _class_default_delegate;
static const SQRegFunction _class_default_delegate_funcz[];
SQObjectPtr _instance_default_delegate;
static const SQRegFunction _instance_default_delegate_funcz[];
SQObjectPtr _weakref_default_delegate;
static const SQRegFunction _weakref_default_delegate_funcz[];
SQCOMPILERERROR _compilererrorhandler;
SQPRINTFUNCTION _printfunc;
SQPRINTFUNCTION _errorfunc;
bool _debuginfo;
bool _notifyallexceptions;
SQUserPointer _foreignptr;
SQRELEASEHOOK _releasehook;
SQCOMPILERERROR _compilererrorhandler;
SQPRINTFUNCTION _printfunc;
SQPRINTFUNCTION _errorfunc;
bool _debuginfo;
bool _notifyallexceptions;
SQUserPointer _foreignptr;
SQRELEASEHOOK _releasehook;
private:
SQChar *_scratchpad;
SQInteger _scratchpadsize;
SQChar *_scratchpad;
SQInteger _scratchpadsize;
};
#define _sp(s) (_sharedstate->GetScratchPad(s))
#define _spval (_sharedstate->GetScratchPad(-1))
#define _table_ddel _table(_sharedstate->_table_default_delegate)
#define _array_ddel _table(_sharedstate->_array_default_delegate)
#define _string_ddel _table(_sharedstate->_string_default_delegate)
#define _number_ddel _table(_sharedstate->_number_default_delegate)
#define _generator_ddel _table(_sharedstate->_generator_default_delegate)
#define _closure_ddel _table(_sharedstate->_closure_default_delegate)
#define _thread_ddel _table(_sharedstate->_thread_default_delegate)
#define _class_ddel _table(_sharedstate->_class_default_delegate)
#define _instance_ddel _table(_sharedstate->_instance_default_delegate)
#define _weakref_ddel _table(_sharedstate->_weakref_default_delegate)
#define _table_ddel _table(_sharedstate->_table_default_delegate)
#define _array_ddel _table(_sharedstate->_array_default_delegate)
#define _string_ddel _table(_sharedstate->_string_default_delegate)
#define _number_ddel _table(_sharedstate->_number_default_delegate)
#define _generator_ddel _table(_sharedstate->_generator_default_delegate)
#define _closure_ddel _table(_sharedstate->_closure_default_delegate)
#define _thread_ddel _table(_sharedstate->_thread_default_delegate)
#define _class_ddel _table(_sharedstate->_class_default_delegate)
#define _instance_ddel _table(_sharedstate->_instance_default_delegate)
#define _weakref_ddel _table(_sharedstate->_weakref_default_delegate)
bool CompileTypemask(SQIntVec &res,const SQChar *typemask);

View File

@ -1,29 +1,29 @@
/* see copyright notice in squirrel.h */
/* see copyright notice in squirrel.h */
#ifndef _SQSTRING_H_
#define _SQSTRING_H_
inline SQHash _hashstr (const SQChar *s, size_t l)
{
SQHash h = (SQHash)l; /* seed */
size_t step = (l>>5)|1; /* if string is too long, don't hash all its chars */
for (; l>=step; l-=step)
h = h ^ ((h<<5)+(h>>2)+(unsigned short)*(s++));
return h;
SQHash h = (SQHash)l; /* seed */
size_t step = (l>>5)|1; /* if string is too long, don't hash all its chars */
for (; l>=step; l-=step)
h = h ^ ((h<<5)+(h>>2)+(unsigned short)*(s++));
return h;
}
struct SQString : public SQRefCounted
{
SQString(){}
~SQString(){}
SQString(){}
~SQString(){}
public:
static SQString *Create(SQSharedState *ss, const SQChar *, SQInteger len = -1 );
SQInteger Next(const SQObjectPtr &refpos, SQObjectPtr &outkey, SQObjectPtr &outval);
void Release();
SQSharedState *_sharedstate;
SQString *_next; //chain for the string table
SQInteger _len;
SQHash _hash;
SQChar _val[1];
static SQString *Create(SQSharedState *ss, const SQChar *, SQInteger len = -1 );
SQInteger Next(const SQObjectPtr &refpos, SQObjectPtr &outkey, SQObjectPtr &outval);
void Release();
SQSharedState *_sharedstate;
SQString *_next; //chain for the string table
SQInteger _len;
SQHash _hash;
SQChar _val[1];
};

View File

@ -9,213 +9,213 @@ see copyright notice in squirrel.h
SQTable::SQTable(SQSharedState *ss,SQInteger nInitialSize)
{
SQInteger pow2size=MINPOWER2;
while(nInitialSize>pow2size)pow2size=pow2size<<1;
AllocNodes(pow2size);
_usednodes = 0;
_delegate = NULL;
INIT_CHAIN();
ADD_TO_CHAIN(&_sharedstate->_gc_chain,this);
SQInteger pow2size=MINPOWER2;
while(nInitialSize>pow2size)pow2size=pow2size<<1;
AllocNodes(pow2size);
_usednodes = 0;
_delegate = NULL;
INIT_CHAIN();
ADD_TO_CHAIN(&_sharedstate->_gc_chain,this);
}
void SQTable::Remove(const SQObjectPtr &key)
{
_HashNode *n = _Get(key, HashObj(key) & (_numofnodes - 1));
if (n) {
n->val.Null();
n->key.Null();
_usednodes--;
Rehash(false);
}
_HashNode *n = _Get(key, HashObj(key) & (_numofnodes - 1));
if (n) {
n->val.Null();
n->key.Null();
_usednodes--;
Rehash(false);
}
}
void SQTable::AllocNodes(SQInteger nSize)
{
_HashNode *nodes=(_HashNode *)SQ_MALLOC(sizeof(_HashNode)*nSize);
for(SQInteger i=0;i<nSize;i++){
_HashNode &n = nodes[i];
new (&n) _HashNode;
n.next=NULL;
}
_numofnodes=nSize;
_nodes=nodes;
_firstfree=&_nodes[_numofnodes-1];
_HashNode *nodes=(_HashNode *)SQ_MALLOC(sizeof(_HashNode)*nSize);
for(SQInteger i=0;i<nSize;i++){
_HashNode &n = nodes[i];
new (&n) _HashNode;
n.next=NULL;
}
_numofnodes=nSize;
_nodes=nodes;
_firstfree=&_nodes[_numofnodes-1];
}
void SQTable::Rehash(bool force)
{
SQInteger oldsize=_numofnodes;
//prevent problems with the integer division
if(oldsize<4)oldsize=4;
_HashNode *nold=_nodes;
SQInteger nelems=CountUsed();
if (nelems >= oldsize-oldsize/4) /* using more than 3/4? */
AllocNodes(oldsize*2);
else if (nelems <= oldsize/4 && /* less than 1/4? */
oldsize > MINPOWER2)
AllocNodes(oldsize/2);
else if(force)
AllocNodes(oldsize);
else
return;
_usednodes = 0;
for (SQInteger i=0; i<oldsize; i++) {
_HashNode *old = nold+i;
if (type(old->key) != OT_NULL)
NewSlot(old->key,old->val);
}
for(SQInteger k=0;k<oldsize;k++)
nold[k].~_HashNode();
SQ_FREE(nold,oldsize*sizeof(_HashNode));
SQInteger oldsize=_numofnodes;
//prevent problems with the integer division
if(oldsize<4)oldsize=4;
_HashNode *nold=_nodes;
SQInteger nelems=CountUsed();
if (nelems >= oldsize-oldsize/4) /* using more than 3/4? */
AllocNodes(oldsize*2);
else if (nelems <= oldsize/4 && /* less than 1/4? */
oldsize > MINPOWER2)
AllocNodes(oldsize/2);
else if(force)
AllocNodes(oldsize);
else
return;
_usednodes = 0;
for (SQInteger i=0; i<oldsize; i++) {
_HashNode *old = nold+i;
if (type(old->key) != OT_NULL)
NewSlot(old->key,old->val);
}
for(SQInteger k=0;k<oldsize;k++)
nold[k].~_HashNode();
SQ_FREE(nold,oldsize*sizeof(_HashNode));
}
SQTable *SQTable::Clone()
{
SQTable *nt=Create(_opt_ss(this),_numofnodes);
SQTable *nt=Create(_opt_ss(this),_numofnodes);
#ifdef _FAST_CLONE
_HashNode *basesrc = _nodes;
_HashNode *basedst = nt->_nodes;
_HashNode *src = _nodes;
_HashNode *dst = nt->_nodes;
SQInteger n = 0;
for(n = 0; n < _numofnodes; n++) {
dst->key = src->key;
dst->val = src->val;
if(src->next) {
assert(src->next > basesrc);
dst->next = basedst + (src->next - basesrc);
assert(dst != dst->next);
}
dst++;
src++;
}
assert(_firstfree > basesrc);
assert(_firstfree != NULL);
nt->_firstfree = basedst + (_firstfree - basesrc);
nt->_usednodes = _usednodes;
_HashNode *basesrc = _nodes;
_HashNode *basedst = nt->_nodes;
_HashNode *src = _nodes;
_HashNode *dst = nt->_nodes;
SQInteger n = 0;
for(n = 0; n < _numofnodes; n++) {
dst->key = src->key;
dst->val = src->val;
if(src->next) {
assert(src->next > basesrc);
dst->next = basedst + (src->next - basesrc);
assert(dst != dst->next);
}
dst++;
src++;
}
assert(_firstfree > basesrc);
assert(_firstfree != NULL);
nt->_firstfree = basedst + (_firstfree - basesrc);
nt->_usednodes = _usednodes;
#else
SQInteger ridx=0;
SQObjectPtr key,val;
while((ridx=Next(true,ridx,key,val))!=-1){
nt->NewSlot(key,val);
}
SQInteger ridx=0;
SQObjectPtr key,val;
while((ridx=Next(true,ridx,key,val))!=-1){
nt->NewSlot(key,val);
}
#endif
nt->SetDelegate(_delegate);
return nt;
nt->SetDelegate(_delegate);
return nt;
}
bool SQTable::Get(const SQObjectPtr &key,SQObjectPtr &val)
{
if(type(key) == OT_NULL)
return false;
_HashNode *n = _Get(key, HashObj(key) & (_numofnodes - 1));
if (n) {
val = _realval(n->val);
return true;
}
return false;
if(type(key) == OT_NULL)
return false;
_HashNode *n = _Get(key, HashObj(key) & (_numofnodes - 1));
if (n) {
val = _realval(n->val);
return true;
}
return false;
}
bool SQTable::NewSlot(const SQObjectPtr &key,const SQObjectPtr &val)
{
assert(type(key) != OT_NULL);
SQHash h = HashObj(key) & (_numofnodes - 1);
_HashNode *n = _Get(key, h);
if (n) {
n->val = val;
return false;
}
_HashNode *mp = &_nodes[h];
n = mp;
assert(type(key) != OT_NULL);
SQHash h = HashObj(key) & (_numofnodes - 1);
_HashNode *n = _Get(key, h);
if (n) {
n->val = val;
return false;
}
_HashNode *mp = &_nodes[h];
n = mp;
//key not found I'll insert it
//main pos is not free
//key not found I'll insert it
//main pos is not free
if(type(mp->key) != OT_NULL) {
n = _firstfree; /* get a free place */
SQHash mph = HashObj(mp->key) & (_numofnodes - 1);
_HashNode *othern; /* main position of colliding node */
if(type(mp->key) != OT_NULL) {
n = _firstfree; /* get a free place */
SQHash mph = HashObj(mp->key) & (_numofnodes - 1);
_HashNode *othern; /* main position of colliding node */
if (mp > n && (othern = &_nodes[mph]) != mp){
/* yes; move colliding node into free position */
while (othern->next != mp){
assert(othern->next != NULL);
othern = othern->next; /* find previous */
}
othern->next = n; /* redo the chain with `n' in place of `mp' */
n->key = mp->key;
n->val = mp->val;/* copy colliding node into free pos. (mp->next also goes) */
n->next = mp->next;
mp->key.Null();
mp->val.Null();
mp->next = NULL; /* now `mp' is free */
}
else{
/* new node will go into free position */
n->next = mp->next; /* chain new position */
mp->next = n;
mp = n;
}
}
mp->key = key;
if (mp > n && (othern = &_nodes[mph]) != mp){
/* yes; move colliding node into free position */
while (othern->next != mp){
assert(othern->next != NULL);
othern = othern->next; /* find previous */
}
othern->next = n; /* redo the chain with `n' in place of `mp' */
n->key = mp->key;
n->val = mp->val;/* copy colliding node into free pos. (mp->next also goes) */
n->next = mp->next;
mp->key.Null();
mp->val.Null();
mp->next = NULL; /* now `mp' is free */
}
else{
/* new node will go into free position */
n->next = mp->next; /* chain new position */
mp->next = n;
mp = n;
}
}
mp->key = key;
for (;;) { /* correct `firstfree' */
if (type(_firstfree->key) == OT_NULL && _firstfree->next == NULL) {
mp->val = val;
_usednodes++;
return true; /* OK; table still has a free place */
}
else if (_firstfree == _nodes) break; /* cannot decrement from here */
else (_firstfree)--;
}
Rehash(true);
return NewSlot(key, val);
for (;;) { /* correct `firstfree' */
if (type(_firstfree->key) == OT_NULL && _firstfree->next == NULL) {
mp->val = val;
_usednodes++;
return true; /* OK; table still has a free place */
}
else if (_firstfree == _nodes) break; /* cannot decrement from here */
else (_firstfree)--;
}
Rehash(true);
return NewSlot(key, val);
}
SQInteger SQTable::Next(bool getweakrefs,const SQObjectPtr &refpos, SQObjectPtr &outkey, SQObjectPtr &outval)
{
SQInteger idx = (SQInteger)TranslateIndex(refpos);
while (idx < _numofnodes) {
if(type(_nodes[idx].key) != OT_NULL) {
//first found
_HashNode &n = _nodes[idx];
outkey = n.key;
outval = getweakrefs?(SQObject)n.val:_realval(n.val);
//return idx for the next iteration
return ++idx;
}
++idx;
}
//nothing to iterate anymore
return -1;
SQInteger idx = (SQInteger)TranslateIndex(refpos);
while (idx < _numofnodes) {
if(type(_nodes[idx].key) != OT_NULL) {
//first found
_HashNode &n = _nodes[idx];
outkey = n.key;
outval = getweakrefs?(SQObject)n.val:_realval(n.val);
//return idx for the next iteration
return ++idx;
}
++idx;
}
//nothing to iterate anymore
return -1;
}
bool SQTable::Set(const SQObjectPtr &key, const SQObjectPtr &val)
{
_HashNode *n = _Get(key, HashObj(key) & (_numofnodes - 1));
if (n) {
n->val = val;
return true;
}
return false;
_HashNode *n = _Get(key, HashObj(key) & (_numofnodes - 1));
if (n) {
n->val = val;
return true;
}
return false;
}
void SQTable::_ClearNodes()
{
for(SQInteger i = 0;i < _numofnodes; i++) { _HashNode &n = _nodes[i]; n.key.Null(); n.val.Null(); }
for(SQInteger i = 0;i < _numofnodes; i++) { _HashNode &n = _nodes[i]; n.key.Null(); n.val.Null(); }
}
void SQTable::Finalize()
{
_ClearNodes();
SetDelegate(NULL);
_ClearNodes();
SetDelegate(NULL);
}
void SQTable::Clear()
{
_ClearNodes();
_usednodes = 0;
Rehash(true);
_ClearNodes();
_usednodes = 0;
Rehash(true);
}

View File

@ -1,4 +1,4 @@
/* see copyright notice in squirrel.h */
/* see copyright notice in squirrel.h */
#ifndef _SQTABLE_H_
#define _SQTABLE_H_
/*
@ -14,96 +14,96 @@
inline SQHash HashObj(const SQObjectPtr &key)
{
switch(type(key)) {
case OT_STRING: return _string(key)->_hash;
case OT_FLOAT: return (SQHash)((SQInteger)_float(key));
case OT_BOOL: case OT_INTEGER: return (SQHash)((SQInteger)_integer(key));
default: return hashptr(key._unVal.pRefCounted);
}
switch(type(key)) {
case OT_STRING: return _string(key)->_hash;
case OT_FLOAT: return (SQHash)((SQInteger)_float(key));
case OT_BOOL: case OT_INTEGER: return (SQHash)((SQInteger)_integer(key));
default: return hashptr(key._unVal.pRefCounted);
}
}
struct SQTable : public SQDelegable
{
private:
struct _HashNode
{
_HashNode() { next = NULL; }
SQObjectPtr val;
SQObjectPtr key;
_HashNode *next;
};
_HashNode *_firstfree;
_HashNode *_nodes;
SQInteger _numofnodes;
SQInteger _usednodes;
struct _HashNode
{
_HashNode() { next = NULL; }
SQObjectPtr val;
SQObjectPtr key;
_HashNode *next;
};
_HashNode *_firstfree;
_HashNode *_nodes;
SQInteger _numofnodes;
SQInteger _usednodes;
///////////////////////////
void AllocNodes(SQInteger nSize);
void Rehash(bool force);
SQTable(SQSharedState *ss, SQInteger nInitialSize);
void _ClearNodes();
void AllocNodes(SQInteger nSize);
void Rehash(bool force);
SQTable(SQSharedState *ss, SQInteger nInitialSize);
void _ClearNodes();
public:
static SQTable* Create(SQSharedState *ss,SQInteger nInitialSize)
{
SQTable *newtable = (SQTable*)SQ_MALLOC(sizeof(SQTable));
new (newtable) SQTable(ss, nInitialSize);
newtable->_delegate = NULL;
return newtable;
}
void Finalize();
SQTable *Clone();
~SQTable()
{
SetDelegate(NULL);
REMOVE_FROM_CHAIN(&_sharedstate->_gc_chain, this);
for (SQInteger i = 0; i < _numofnodes; i++) _nodes[i].~_HashNode();
SQ_FREE(_nodes, _numofnodes * sizeof(_HashNode));
}
static SQTable* Create(SQSharedState *ss,SQInteger nInitialSize)
{
SQTable *newtable = (SQTable*)SQ_MALLOC(sizeof(SQTable));
new (newtable) SQTable(ss, nInitialSize);
newtable->_delegate = NULL;
return newtable;
}
void Finalize();
SQTable *Clone();
~SQTable()
{
SetDelegate(NULL);
REMOVE_FROM_CHAIN(&_sharedstate->_gc_chain, this);
for (SQInteger i = 0; i < _numofnodes; i++) _nodes[i].~_HashNode();
SQ_FREE(_nodes, _numofnodes * sizeof(_HashNode));
}
#ifndef NO_GARBAGE_COLLECTOR
void Mark(SQCollectable **chain);
SQObjectType GetType() {return OT_TABLE;}
void Mark(SQCollectable **chain);
SQObjectType GetType() {return OT_TABLE;}
#endif
inline _HashNode *_Get(const SQObjectPtr &key,SQHash hash)
{
_HashNode *n = &_nodes[hash];
do{
if(_rawval(n->key) == _rawval(key) && type(n->key) == type(key)){
return n;
}
}while((n = n->next));
return NULL;
}
//for compiler use
inline bool GetStr(const SQChar* key,SQInteger keylen,SQObjectPtr &val)
{
SQHash hash = _hashstr(key,keylen);
_HashNode *n = &_nodes[hash & (_numofnodes - 1)];
_HashNode *res = NULL;
do{
if(type(n->key) == OT_STRING && (scstrcmp(_stringval(n->key),key) == 0)){
res = n;
break;
}
}while((n = n->next));
if (res) {
val = _realval(res->val);
return true;
}
return false;
}
bool Get(const SQObjectPtr &key,SQObjectPtr &val);
void Remove(const SQObjectPtr &key);
bool Set(const SQObjectPtr &key, const SQObjectPtr &val);
//returns true if a new slot has been created false if it was already present
bool NewSlot(const SQObjectPtr &key,const SQObjectPtr &val);
SQInteger Next(bool getweakrefs,const SQObjectPtr &refpos, SQObjectPtr &outkey, SQObjectPtr &outval);
inline _HashNode *_Get(const SQObjectPtr &key,SQHash hash)
{
_HashNode *n = &_nodes[hash];
do{
if(_rawval(n->key) == _rawval(key) && type(n->key) == type(key)){
return n;
}
}while((n = n->next));
return NULL;
}
//for compiler use
inline bool GetStr(const SQChar* key,SQInteger keylen,SQObjectPtr &val)
{
SQHash hash = _hashstr(key,keylen);
_HashNode *n = &_nodes[hash & (_numofnodes - 1)];
_HashNode *res = NULL;
do{
if(type(n->key) == OT_STRING && (scstrcmp(_stringval(n->key),key) == 0)){
res = n;
break;
}
}while((n = n->next));
if (res) {
val = _realval(res->val);
return true;
}
return false;
}
bool Get(const SQObjectPtr &key,SQObjectPtr &val);
void Remove(const SQObjectPtr &key);
bool Set(const SQObjectPtr &key, const SQObjectPtr &val);
//returns true if a new slot has been created false if it was already present
bool NewSlot(const SQObjectPtr &key,const SQObjectPtr &val);
SQInteger Next(bool getweakrefs,const SQObjectPtr &refpos, SQObjectPtr &outkey, SQObjectPtr &outval);
SQInteger CountUsed(){ return _usednodes;}
void Clear();
void Release()
{
sq_delete(this, SQTable);
}
SQInteger CountUsed(){ return _usednodes;}
void Clear();
void Release()
{
sq_delete(this, SQTable);
}
};

View File

@ -1,40 +1,40 @@
/* see copyright notice in squirrel.h */
/* see copyright notice in squirrel.h */
#ifndef _SQUSERDATA_H_
#define _SQUSERDATA_H_
struct SQUserData : SQDelegable
{
SQUserData(SQSharedState *ss){ _delegate = 0; _hook = NULL; INIT_CHAIN(); ADD_TO_CHAIN(&_ss(this)->_gc_chain, this); }
~SQUserData()
{
REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain, this);
SetDelegate(NULL);
}
static SQUserData* Create(SQSharedState *ss, SQInteger size)
{
SQUserData* ud = (SQUserData*)SQ_MALLOC(sq_aligning(sizeof(SQUserData))+size);
new (ud) SQUserData(ss);
ud->_size = size;
ud->_typetag = 0;
return ud;
}
SQUserData(SQSharedState *ss){ _delegate = 0; _hook = NULL; INIT_CHAIN(); ADD_TO_CHAIN(&_ss(this)->_gc_chain, this); }
~SQUserData()
{
REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain, this);
SetDelegate(NULL);
}
static SQUserData* Create(SQSharedState *ss, SQInteger size)
{
SQUserData* ud = (SQUserData*)SQ_MALLOC(sq_aligning(sizeof(SQUserData))+size);
new (ud) SQUserData(ss);
ud->_size = size;
ud->_typetag = 0;
return ud;
}
#ifndef NO_GARBAGE_COLLECTOR
void Mark(SQCollectable **chain);
void Finalize(){SetDelegate(NULL);}
SQObjectType GetType(){ return OT_USERDATA;}
void Mark(SQCollectable **chain);
void Finalize(){SetDelegate(NULL);}
SQObjectType GetType(){ return OT_USERDATA;}
#endif
void Release() {
if (_hook) _hook((SQUserPointer)sq_aligning(this + 1),_size);
SQInteger tsize = _size;
this->~SQUserData();
SQ_FREE(this, sq_aligning(sizeof(SQUserData)) + tsize);
}
void Release() {
if (_hook) _hook((SQUserPointer)sq_aligning(this + 1),_size);
SQInteger tsize = _size;
this->~SQUserData();
SQ_FREE(this, sq_aligning(sizeof(SQUserData)) + tsize);
}
SQInteger _size;
SQRELEASEHOOK _hook;
SQUserPointer _typetag;
//SQChar _val[1];
SQInteger _size;
SQRELEASEHOOK _hook;
SQUserPointer _typetag;
//SQChar _val[1];
};
#endif //_SQUSERDATA_H_

View File

@ -1,4 +1,4 @@
/* see copyright notice in squirrel.h */
/* see copyright notice in squirrel.h */
#ifndef _SQUTILS_H_
#define _SQUTILS_H_
@ -18,99 +18,99 @@ void sq_vm_free(void *p,SQUnsignedInteger size);
template<typename T> class sqvector
{
public:
sqvector()
{
_vals = NULL;
_size = 0;
_allocated = 0;
}
sqvector(const sqvector<T>& v)
{
copy(v);
}
void copy(const sqvector<T>& v)
{
if(_size) {
resize(0); //destroys all previous stuff
}
//resize(v._size);
if(v._size > _allocated) {
_realloc(v._size);
}
for(SQUnsignedInteger i = 0; i < v._size; i++) {
new ((void *)&_vals[i]) T(v._vals[i]);
}
_size = v._size;
}
~sqvector()
{
if(_allocated) {
for(SQUnsignedInteger i = 0; i < _size; i++)
_vals[i].~T();
SQ_FREE(_vals, (_allocated * sizeof(T)));
}
}
void reserve(SQUnsignedInteger newsize) { _realloc(newsize); }
void resize(SQUnsignedInteger newsize, const T& fill = T())
{
if(newsize > _allocated)
_realloc(newsize);
if(newsize > _size) {
while(_size < newsize) {
new ((void *)&_vals[_size]) T(fill);
_size++;
}
}
else{
for(SQUnsignedInteger i = newsize; i < _size; i++) {
_vals[i].~T();
}
_size = newsize;
}
}
void shrinktofit() { if(_size > 4) { _realloc(_size); } }
T& top() const { return _vals[_size - 1]; }
inline SQUnsignedInteger size() const { return _size; }
bool empty() const { return (_size <= 0); }
inline T &push_back(const T& val = T())
{
if(_allocated <= _size)
_realloc(_size * 2);
return *(new ((void *)&_vals[_size++]) T(val));
}
inline void pop_back()
{
_size--; _vals[_size].~T();
}
void insert(SQUnsignedInteger idx, const T& val)
{
resize(_size + 1);
for(SQUnsignedInteger i = _size - 1; i > idx; i--) {
_vals[i] = _vals[i - 1];
}
_vals[idx] = val;
}
void remove(SQUnsignedInteger idx)
{
_vals[idx].~T();
if(idx < (_size - 1)) {
memmove(&_vals[idx], &_vals[idx+1], sizeof(T) * (_size - idx - 1));
}
_size--;
}
SQUnsignedInteger capacity() { return _allocated; }
inline T &back() const { return _vals[_size - 1]; }
inline T& operator[](SQUnsignedInteger pos) const{ return _vals[pos]; }
T* _vals;
sqvector()
{
_vals = NULL;
_size = 0;
_allocated = 0;
}
sqvector(const sqvector<T>& v)
{
copy(v);
}
void copy(const sqvector<T>& v)
{
if(_size) {
resize(0); //destroys all previous stuff
}
//resize(v._size);
if(v._size > _allocated) {
_realloc(v._size);
}
for(SQUnsignedInteger i = 0; i < v._size; i++) {
new ((void *)&_vals[i]) T(v._vals[i]);
}
_size = v._size;
}
~sqvector()
{
if(_allocated) {
for(SQUnsignedInteger i = 0; i < _size; i++)
_vals[i].~T();
SQ_FREE(_vals, (_allocated * sizeof(T)));
}
}
void reserve(SQUnsignedInteger newsize) { _realloc(newsize); }
void resize(SQUnsignedInteger newsize, const T& fill = T())
{
if(newsize > _allocated)
_realloc(newsize);
if(newsize > _size) {
while(_size < newsize) {
new ((void *)&_vals[_size]) T(fill);
_size++;
}
}
else{
for(SQUnsignedInteger i = newsize; i < _size; i++) {
_vals[i].~T();
}
_size = newsize;
}
}
void shrinktofit() { if(_size > 4) { _realloc(_size); } }
T& top() const { return _vals[_size - 1]; }
inline SQUnsignedInteger size() const { return _size; }
bool empty() const { return (_size <= 0); }
inline T &push_back(const T& val = T())
{
if(_allocated <= _size)
_realloc(_size * 2);
return *(new ((void *)&_vals[_size++]) T(val));
}
inline void pop_back()
{
_size--; _vals[_size].~T();
}
void insert(SQUnsignedInteger idx, const T& val)
{
resize(_size + 1);
for(SQUnsignedInteger i = _size - 1; i > idx; i--) {
_vals[i] = _vals[i - 1];
}
_vals[idx] = val;
}
void remove(SQUnsignedInteger idx)
{
_vals[idx].~T();
if(idx < (_size - 1)) {
memmove(&_vals[idx], &_vals[idx+1], sizeof(T) * (_size - idx - 1));
}
_size--;
}
SQUnsignedInteger capacity() { return _allocated; }
inline T &back() const { return _vals[_size - 1]; }
inline T& operator[](SQUnsignedInteger pos) const{ return _vals[pos]; }
T* _vals;
private:
void _realloc(SQUnsignedInteger newsize)
{
newsize = (newsize > 0)?newsize:4;
_vals = (T*)SQ_REALLOC(_vals, _allocated * sizeof(T), newsize * sizeof(T));
_allocated = newsize;
}
SQUnsignedInteger _size;
SQUnsignedInteger _allocated;
void _realloc(SQUnsignedInteger newsize)
{
newsize = (newsize > 0)?newsize:4;
_vals = (T*)SQ_REALLOC(_vals, _allocated * sizeof(T), newsize * sizeof(T));
_allocated = newsize;
}
SQUnsignedInteger _size;
SQUnsignedInteger _allocated;
};
#endif //_SQUTILS_H_

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
/* see copyright notice in squirrel.h */
/* see copyright notice in squirrel.h */
#ifndef _SQVM_H_
#define _SQVM_H_
@ -11,19 +11,19 @@
#define DONT_FALL_BACK 666
//#define EXISTS_FALL_BACK -1
#define GET_FLAG_RAW 0x00000001
#define GET_FLAG_DO_NOT_RAISE_ERROR 0x00000002
#define GET_FLAG_RAW 0x00000001
#define GET_FLAG_DO_NOT_RAISE_ERROR 0x00000002
//base lib
void sq_base_register(HSQUIRRELVM v);
struct SQExceptionTrap{
SQExceptionTrap() {}
SQExceptionTrap(SQInteger ss, SQInteger stackbase,SQInstruction *ip, SQInteger ex_target){ _stacksize = ss; _stackbase = stackbase; _ip = ip; _extarget = ex_target;}
SQExceptionTrap(const SQExceptionTrap &et) { (*this) = et; }
SQInteger _stackbase;
SQInteger _stacksize;
SQInstruction *_ip;
SQInteger _extarget;
SQExceptionTrap() {}
SQExceptionTrap(SQInteger ss, SQInteger stackbase,SQInstruction *ip, SQInteger ex_target){ _stacksize = ss; _stackbase = stackbase; _ip = ip; _extarget = ex_target;}
SQExceptionTrap(const SQExceptionTrap &et) { (*this) = et; }
SQInteger _stackbase;
SQInteger _stacksize;
SQInstruction *_ip;
SQInteger _extarget;
};
#define _INLINE
@ -35,155 +35,155 @@ typedef sqvector<SQExceptionTrap> ExceptionsTraps;
struct SQVM : public CHAINABLE_OBJ
{
struct CallInfo{
//CallInfo() { _generator = NULL;}
SQInstruction *_ip;
SQObjectPtr *_literals;
SQObjectPtr _closure;
SQGenerator *_generator;
SQInt32 _etraps;
SQInt32 _prevstkbase;
SQInt32 _prevtop;
SQInt32 _target;
SQInt32 _ncalls;
SQBool _root;
};
struct CallInfo{
//CallInfo() { _generator = NULL;}
SQInstruction *_ip;
SQObjectPtr *_literals;
SQObjectPtr _closure;
SQGenerator *_generator;
SQInt32 _etraps;
SQInt32 _prevstkbase;
SQInt32 _prevtop;
SQInt32 _target;
SQInt32 _ncalls;
SQBool _root;
};
typedef sqvector<CallInfo> CallInfoVec;
public:
void DebugHookProxy(SQInteger type, const SQChar * sourcename, SQInteger line, const SQChar * funcname);
static void _DebugHookProxy(HSQUIRRELVM v, SQInteger type, const SQChar * sourcename, SQInteger line, const SQChar * funcname);
enum ExecutionType { ET_CALL, ET_RESUME_GENERATOR, ET_RESUME_VM,ET_RESUME_THROW_VM };
SQVM(SQSharedState *ss);
~SQVM();
bool Init(SQVM *friendvm, SQInteger stacksize);
bool Execute(SQObjectPtr &func, SQInteger nargs, SQInteger stackbase, SQObjectPtr &outres, SQBool raiseerror, ExecutionType et = ET_CALL);
//starts a native call return when the NATIVE closure returns
bool CallNative(SQNativeClosure *nclosure, SQInteger nargs, SQInteger newbase, SQObjectPtr &retval,bool &suspend);
//starts a SQUIRREL call in the same "Execution loop"
bool StartCall(SQClosure *closure, SQInteger target, SQInteger nargs, SQInteger stackbase, bool tailcall);
bool CreateClassInstance(SQClass *theclass, SQObjectPtr &inst, SQObjectPtr &constructor);
//call a generic closure pure SQUIRREL or NATIVE
bool Call(SQObjectPtr &closure, SQInteger nparams, SQInteger stackbase, SQObjectPtr &outres,SQBool raiseerror);
SQRESULT Suspend();
void DebugHookProxy(SQInteger type, const SQChar * sourcename, SQInteger line, const SQChar * funcname);
static void _DebugHookProxy(HSQUIRRELVM v, SQInteger type, const SQChar * sourcename, SQInteger line, const SQChar * funcname);
enum ExecutionType { ET_CALL, ET_RESUME_GENERATOR, ET_RESUME_VM,ET_RESUME_THROW_VM };
SQVM(SQSharedState *ss);
~SQVM();
bool Init(SQVM *friendvm, SQInteger stacksize);
bool Execute(SQObjectPtr &func, SQInteger nargs, SQInteger stackbase, SQObjectPtr &outres, SQBool raiseerror, ExecutionType et = ET_CALL);
//starts a native call return when the NATIVE closure returns
bool CallNative(SQNativeClosure *nclosure, SQInteger nargs, SQInteger newbase, SQObjectPtr &retval,bool &suspend);
//starts a SQUIRREL call in the same "Execution loop"
bool StartCall(SQClosure *closure, SQInteger target, SQInteger nargs, SQInteger stackbase, bool tailcall);
bool CreateClassInstance(SQClass *theclass, SQObjectPtr &inst, SQObjectPtr &constructor);
//call a generic closure pure SQUIRREL or NATIVE
bool Call(SQObjectPtr &closure, SQInteger nparams, SQInteger stackbase, SQObjectPtr &outres,SQBool raiseerror);
SQRESULT Suspend();
void CallDebugHook(SQInteger type,SQInteger forcedline=0);
void CallErrorHandler(SQObjectPtr &e);
bool Get(const SQObjectPtr &self, const SQObjectPtr &key, SQObjectPtr &dest, SQUnsignedInteger getflags, SQInteger selfidx);
SQInteger FallBackGet(const SQObjectPtr &self,const SQObjectPtr &key,SQObjectPtr &dest);
bool InvokeDefaultDelegate(const SQObjectPtr &self,const SQObjectPtr &key,SQObjectPtr &dest);
bool Set(const SQObjectPtr &self, const SQObjectPtr &key, const SQObjectPtr &val, SQInteger selfidx);
SQInteger FallBackSet(const SQObjectPtr &self,const SQObjectPtr &key,const SQObjectPtr &val);
bool NewSlot(const SQObjectPtr &self, const SQObjectPtr &key, const SQObjectPtr &val,bool bstatic);
bool NewSlotA(const SQObjectPtr &self,const SQObjectPtr &key,const SQObjectPtr &val,const SQObjectPtr &attrs,bool bstatic,bool raw);
bool DeleteSlot(const SQObjectPtr &self, const SQObjectPtr &key, SQObjectPtr &res);
bool Clone(const SQObjectPtr &self, SQObjectPtr &target);
bool ObjCmp(const SQObjectPtr &o1, const SQObjectPtr &o2,SQInteger &res);
bool StringCat(const SQObjectPtr &str, const SQObjectPtr &obj, SQObjectPtr &dest);
static bool IsEqual(const SQObjectPtr &o1,const SQObjectPtr &o2,bool &res);
bool ToString(const SQObjectPtr &o,SQObjectPtr &res);
SQString *PrintObjVal(const SQObjectPtr &o);
void CallDebugHook(SQInteger type,SQInteger forcedline=0);
void CallErrorHandler(SQObjectPtr &e);
bool Get(const SQObjectPtr &self, const SQObjectPtr &key, SQObjectPtr &dest, SQUnsignedInteger getflags, SQInteger selfidx);
SQInteger FallBackGet(const SQObjectPtr &self,const SQObjectPtr &key,SQObjectPtr &dest);
bool InvokeDefaultDelegate(const SQObjectPtr &self,const SQObjectPtr &key,SQObjectPtr &dest);
bool Set(const SQObjectPtr &self, const SQObjectPtr &key, const SQObjectPtr &val, SQInteger selfidx);
SQInteger FallBackSet(const SQObjectPtr &self,const SQObjectPtr &key,const SQObjectPtr &val);
bool NewSlot(const SQObjectPtr &self, const SQObjectPtr &key, const SQObjectPtr &val,bool bstatic);
bool NewSlotA(const SQObjectPtr &self,const SQObjectPtr &key,const SQObjectPtr &val,const SQObjectPtr &attrs,bool bstatic,bool raw);
bool DeleteSlot(const SQObjectPtr &self, const SQObjectPtr &key, SQObjectPtr &res);
bool Clone(const SQObjectPtr &self, SQObjectPtr &target);
bool ObjCmp(const SQObjectPtr &o1, const SQObjectPtr &o2,SQInteger &res);
bool StringCat(const SQObjectPtr &str, const SQObjectPtr &obj, SQObjectPtr &dest);
static bool IsEqual(const SQObjectPtr &o1,const SQObjectPtr &o2,bool &res);
bool ToString(const SQObjectPtr &o,SQObjectPtr &res);
SQString *PrintObjVal(const SQObjectPtr &o);
void Raise_Error(const SQChar *s, ...);
void Raise_Error(const SQObjectPtr &desc);
void Raise_IdxError(const SQObjectPtr &o);
void Raise_CompareError(const SQObject &o1, const SQObject &o2);
void Raise_ParamTypeError(SQInteger nparam,SQInteger typemask,SQInteger type);
void Raise_Error(const SQChar *s, ...);
void Raise_Error(const SQObjectPtr &desc);
void Raise_IdxError(const SQObjectPtr &o);
void Raise_CompareError(const SQObject &o1, const SQObject &o2);
void Raise_ParamTypeError(SQInteger nparam,SQInteger typemask,SQInteger type);
void FindOuter(SQObjectPtr &target, SQObjectPtr *stackindex);
void RelocateOuters();
void CloseOuters(SQObjectPtr *stackindex);
void FindOuter(SQObjectPtr &target, SQObjectPtr *stackindex);
void RelocateOuters();
void CloseOuters(SQObjectPtr *stackindex);
bool TypeOf(const SQObjectPtr &obj1, SQObjectPtr &dest);
bool CallMetaMethod(SQObjectPtr &closure, SQMetaMethod mm, SQInteger nparams, SQObjectPtr &outres);
bool ArithMetaMethod(SQInteger op, const SQObjectPtr &o1, const SQObjectPtr &o2, SQObjectPtr &dest);
bool Return(SQInteger _arg0, SQInteger _arg1, SQObjectPtr &retval);
//new stuff
_INLINE bool ARITH_OP(SQUnsignedInteger op,SQObjectPtr &trg,const SQObjectPtr &o1,const SQObjectPtr &o2);
_INLINE bool BW_OP(SQUnsignedInteger op,SQObjectPtr &trg,const SQObjectPtr &o1,const SQObjectPtr &o2);
_INLINE bool NEG_OP(SQObjectPtr &trg,const SQObjectPtr &o1);
_INLINE bool CMP_OP(CmpOP op, const SQObjectPtr &o1,const SQObjectPtr &o2,SQObjectPtr &res);
bool CLOSURE_OP(SQObjectPtr &target, SQFunctionProto *func);
bool CLASS_OP(SQObjectPtr &target,SQInteger base,SQInteger attrs);
//return true if the loop is finished
bool FOREACH_OP(SQObjectPtr &o1,SQObjectPtr &o2,SQObjectPtr &o3,SQObjectPtr &o4,SQInteger arg_2,int exitpos,int &jump);
//_INLINE bool LOCAL_INC(SQInteger op,SQObjectPtr &target, SQObjectPtr &a, SQObjectPtr &incr);
_INLINE bool PLOCAL_INC(SQInteger op,SQObjectPtr &target, SQObjectPtr &a, SQObjectPtr &incr);
_INLINE bool DerefInc(SQInteger op,SQObjectPtr &target, SQObjectPtr &self, SQObjectPtr &key, SQObjectPtr &incr, bool postfix,SQInteger arg0);
bool TypeOf(const SQObjectPtr &obj1, SQObjectPtr &dest);
bool CallMetaMethod(SQObjectPtr &closure, SQMetaMethod mm, SQInteger nparams, SQObjectPtr &outres);
bool ArithMetaMethod(SQInteger op, const SQObjectPtr &o1, const SQObjectPtr &o2, SQObjectPtr &dest);
bool Return(SQInteger _arg0, SQInteger _arg1, SQObjectPtr &retval);
//new stuff
_INLINE bool ARITH_OP(SQUnsignedInteger op,SQObjectPtr &trg,const SQObjectPtr &o1,const SQObjectPtr &o2);
_INLINE bool BW_OP(SQUnsignedInteger op,SQObjectPtr &trg,const SQObjectPtr &o1,const SQObjectPtr &o2);
_INLINE bool NEG_OP(SQObjectPtr &trg,const SQObjectPtr &o1);
_INLINE bool CMP_OP(CmpOP op, const SQObjectPtr &o1,const SQObjectPtr &o2,SQObjectPtr &res);
bool CLOSURE_OP(SQObjectPtr &target, SQFunctionProto *func);
bool CLASS_OP(SQObjectPtr &target,SQInteger base,SQInteger attrs);
//return true if the loop is finished
bool FOREACH_OP(SQObjectPtr &o1,SQObjectPtr &o2,SQObjectPtr &o3,SQObjectPtr &o4,SQInteger arg_2,int exitpos,int &jump);
//_INLINE bool LOCAL_INC(SQInteger op,SQObjectPtr &target, SQObjectPtr &a, SQObjectPtr &incr);
_INLINE bool PLOCAL_INC(SQInteger op,SQObjectPtr &target, SQObjectPtr &a, SQObjectPtr &incr);
_INLINE bool DerefInc(SQInteger op,SQObjectPtr &target, SQObjectPtr &self, SQObjectPtr &key, SQObjectPtr &incr, bool postfix,SQInteger arg0);
#ifdef _DEBUG_DUMP
void dumpstack(SQInteger stackbase=-1, bool dumpall = false);
void dumpstack(SQInteger stackbase=-1, bool dumpall = false);
#endif
#ifndef NO_GARBAGE_COLLECTOR
void Mark(SQCollectable **chain);
SQObjectType GetType() {return OT_THREAD;}
void Mark(SQCollectable **chain);
SQObjectType GetType() {return OT_THREAD;}
#endif
void Finalize();
void GrowCallStack() {
SQInteger newsize = _alloccallsstacksize*2;
_callstackdata.resize(newsize);
_callsstack = &_callstackdata[0];
_alloccallsstacksize = newsize;
}
bool EnterFrame(SQInteger newbase, SQInteger newtop, bool tailcall);
void LeaveFrame();
void Release(){ sq_delete(this,SQVM); }
void Finalize();
void GrowCallStack() {
SQInteger newsize = _alloccallsstacksize*2;
_callstackdata.resize(newsize);
_callsstack = &_callstackdata[0];
_alloccallsstacksize = newsize;
}
bool EnterFrame(SQInteger newbase, SQInteger newtop, bool tailcall);
void LeaveFrame();
void Release(){ sq_delete(this,SQVM); }
////////////////////////////////////////////////////////////////////////////
//stack functions for the api
void Remove(SQInteger n);
//stack functions for the api
void Remove(SQInteger n);
static bool IsFalse(SQObjectPtr &o);
static bool IsFalse(SQObjectPtr &o);
void Pop();
void Pop(SQInteger n);
void Push(const SQObjectPtr &o);
void PushNull();
SQObjectPtr &Top();
SQObjectPtr &PopGet();
SQObjectPtr &GetUp(SQInteger n);
SQObjectPtr &GetAt(SQInteger n);
void Pop();
void Pop(SQInteger n);
void Push(const SQObjectPtr &o);
void PushNull();
SQObjectPtr &Top();
SQObjectPtr &PopGet();
SQObjectPtr &GetUp(SQInteger n);
SQObjectPtr &GetAt(SQInteger n);
SQObjectPtrVec _stack;
SQObjectPtrVec _stack;
SQInteger _top;
SQInteger _stackbase;
SQOuter *_openouters;
SQObjectPtr _roottable;
SQObjectPtr _lasterror;
SQObjectPtr _errorhandler;
SQInteger _top;
SQInteger _stackbase;
SQOuter *_openouters;
SQObjectPtr _roottable;
SQObjectPtr _lasterror;
SQObjectPtr _errorhandler;
bool _debughook;
SQDEBUGHOOK _debughook_native;
SQObjectPtr _debughook_closure;
bool _debughook;
SQDEBUGHOOK _debughook_native;
SQObjectPtr _debughook_closure;
SQObjectPtr temp_reg;
SQObjectPtr temp_reg;
CallInfo* _callsstack;
SQInteger _callsstacksize;
SQInteger _alloccallsstacksize;
sqvector<CallInfo> _callstackdata;
CallInfo* _callsstack;
SQInteger _callsstacksize;
SQInteger _alloccallsstacksize;
sqvector<CallInfo> _callstackdata;
ExceptionsTraps _etraps;
CallInfo *ci;
SQUserPointer _foreignptr;
//VMs sharing the same state
SQSharedState *_sharedstate;
SQInteger _nnativecalls;
SQInteger _nmetamethodscall;
SQRELEASEHOOK _releasehook;
//suspend infos
SQBool _suspended;
SQBool _suspended_root;
SQInteger _suspended_target;
SQInteger _suspended_traps;
ExceptionsTraps _etraps;
CallInfo *ci;
SQUserPointer _foreignptr;
//VMs sharing the same state
SQSharedState *_sharedstate;
SQInteger _nnativecalls;
SQInteger _nmetamethodscall;
SQRELEASEHOOK _releasehook;
//suspend infos
SQBool _suspended;
SQBool _suspended_root;
SQInteger _suspended_target;
SQInteger _suspended_traps;
};
struct AutoDec{
AutoDec(SQInteger *n) { _n = n; }
~AutoDec() { (*_n)--; }
SQInteger *_n;
AutoDec(SQInteger *n) { _n = n; }
~AutoDec() { (*_n)--; }
SQInteger *_n;
};
inline SQObjectPtr &stack_get(HSQUIRRELVM v,SQInteger idx){return ((idx>=0)?(v->GetAt(idx+v->_stackbase-1)):(v->GetUp(idx)));}
@ -197,18 +197,18 @@ inline SQObjectPtr &stack_get(HSQUIRRELVM v,SQInteger idx){return ((idx>=0)?(v->
#endif
#define PUSH_CALLINFO(v,nci){ \
SQInteger css = v->_callsstacksize; \
if(css == v->_alloccallsstacksize) { \
v->GrowCallStack(); \
} \
v->ci = &v->_callsstack[css]; \
*(v->ci) = nci; \
v->_callsstacksize++; \
SQInteger css = v->_callsstacksize; \
if(css == v->_alloccallsstacksize) { \
v->GrowCallStack(); \
} \
v->ci = &v->_callsstack[css]; \
*(v->ci) = nci; \
v->_callsstacksize++; \
}
#define POP_CALLINFO(v){ \
SQInteger css = --v->_callsstacksize; \
v->ci->_closure.Null(); \
v->ci = css?&v->_callsstack[css-1]:NULL; \
SQInteger css = --v->_callsstacksize; \
v->ci->_closure.Null(); \
v->ci = css?&v->_callsstack[css-1]:NULL; \
}
#endif //_SQVM_H_

View File

@ -1,4 +1,4 @@
/* see copyright notice in squirrel.h */
/* see copyright notice in squirrel.h */
#ifndef _SQSTD_AUXLIB_H_
#define _SQSTD_AUXLIB_H_

View File

@ -1,4 +1,4 @@
/* see copyright notice in squirrel.h */
/* see copyright notice in squirrel.h */
#ifndef _SQSTDBLOB_H_
#define _SQSTDBLOB_H_

View File

@ -1,4 +1,4 @@
/* see copyright notice in squirrel.h */
/* see copyright notice in squirrel.h */
#ifndef _SQSTDIO_H_
#define _SQSTDIO_H_
@ -7,14 +7,14 @@
#define SQSTD_STREAM_TYPE_TAG 0x80000000
struct SQStream {
virtual SQInteger Read(void *buffer, SQInteger size) = 0;
virtual SQInteger Write(void *buffer, SQInteger size) = 0;
virtual SQInteger Flush() = 0;
virtual SQInteger Tell() = 0;
virtual SQInteger Len() = 0;
virtual SQInteger Seek(SQInteger offset, SQInteger origin) = 0;
virtual bool IsValid() = 0;
virtual bool EOS() = 0;
virtual SQInteger Read(void *buffer, SQInteger size) = 0;
virtual SQInteger Write(void *buffer, SQInteger size) = 0;
virtual SQInteger Flush() = 0;
virtual SQInteger Tell() = 0;
virtual SQInteger Len() = 0;
virtual SQInteger Seek(SQInteger offset, SQInteger origin) = 0;
virtual bool IsValid() = 0;
virtual bool EOS() = 0;
};
extern "C" {

View File

@ -1,4 +1,4 @@
/* see copyright notice in squirrel.h */
/* see copyright notice in squirrel.h */
#ifndef _SQSTD_MATH_H_
#define _SQSTD_MATH_H_

View File

@ -1,4 +1,4 @@
/* see copyright notice in squirrel.h */
/* see copyright notice in squirrel.h */
#ifndef _SQSTD_STRING_H_
#define _SQSTD_STRING_H_
@ -10,8 +10,8 @@ typedef unsigned int SQRexBool;
typedef struct SQRex SQRex;
typedef struct {
const SQChar *begin;
SQInteger len;
const SQChar *begin;
SQInteger len;
} SQRexMatch;
SQUIRREL_API SQRex *sqstd_rex_compile(const SQChar *pattern,const SQChar **error);

View File

@ -1,4 +1,4 @@
/* see copyright notice in squirrel.h */
/* see copyright notice in squirrel.h */
#ifndef _SQSTD_SYSTEMLIB_H_
#define _SQSTD_SYSTEMLIB_H_

View File

@ -37,8 +37,8 @@ extern "C" {
#endif
#define SQTrue (1)
#define SQFalse (0)
#define SQTrue (1)
#define SQFalse (0)
struct SQVM;
struct SQTable;
@ -61,66 +61,66 @@ struct SQOuter;
#include "sqconfig.h"
#define SQUIRREL_VERSION _SC("Squirrel 3.1 stable")
#define SQUIRREL_COPYRIGHT _SC("Copyright (C) 2003-2015 Alberto Demichelis")
#define SQUIRREL_AUTHOR _SC("Alberto Demichelis")
#define SQUIRREL_VERSION_NUMBER 310
#define SQUIRREL_VERSION _SC("Squirrel 3.1 stable")
#define SQUIRREL_COPYRIGHT _SC("Copyright (C) 2003-2015 Alberto Demichelis")
#define SQUIRREL_AUTHOR _SC("Alberto Demichelis")
#define SQUIRREL_VERSION_NUMBER 310
#define SQ_VMSTATE_IDLE 0
#define SQ_VMSTATE_RUNNING 1
#define SQ_VMSTATE_SUSPENDED 2
#define SQ_VMSTATE_IDLE 0
#define SQ_VMSTATE_RUNNING 1
#define SQ_VMSTATE_SUSPENDED 2
#define SQUIRREL_EOB 0
#define SQ_BYTECODE_STREAM_TAG 0xFAFA
#define SQ_BYTECODE_STREAM_TAG 0xFAFA
#define SQOBJECT_REF_COUNTED 0x08000000
#define SQOBJECT_NUMERIC 0x04000000
#define SQOBJECT_DELEGABLE 0x02000000
#define SQOBJECT_CANBEFALSE 0x01000000
#define SQOBJECT_REF_COUNTED 0x08000000
#define SQOBJECT_NUMERIC 0x04000000
#define SQOBJECT_DELEGABLE 0x02000000
#define SQOBJECT_CANBEFALSE 0x01000000
#define SQ_MATCHTYPEMASKSTRING (-99999)
#define _RT_MASK 0x00FFFFFF
#define _RAW_TYPE(type) (type&_RT_MASK)
#define _RT_NULL 0x00000001
#define _RT_INTEGER 0x00000002
#define _RT_FLOAT 0x00000004
#define _RT_BOOL 0x00000008
#define _RT_STRING 0x00000010
#define _RT_TABLE 0x00000020
#define _RT_ARRAY 0x00000040
#define _RT_USERDATA 0x00000080
#define _RT_CLOSURE 0x00000100
#define _RT_NATIVECLOSURE 0x00000200
#define _RT_GENERATOR 0x00000400
#define _RT_USERPOINTER 0x00000800
#define _RT_THREAD 0x00001000
#define _RT_FUNCPROTO 0x00002000
#define _RT_CLASS 0x00004000
#define _RT_INSTANCE 0x00008000
#define _RT_WEAKREF 0x00010000
#define _RT_OUTER 0x00020000
#define _RT_NULL 0x00000001
#define _RT_INTEGER 0x00000002
#define _RT_FLOAT 0x00000004
#define _RT_BOOL 0x00000008
#define _RT_STRING 0x00000010
#define _RT_TABLE 0x00000020
#define _RT_ARRAY 0x00000040
#define _RT_USERDATA 0x00000080
#define _RT_CLOSURE 0x00000100
#define _RT_NATIVECLOSURE 0x00000200
#define _RT_GENERATOR 0x00000400
#define _RT_USERPOINTER 0x00000800
#define _RT_THREAD 0x00001000
#define _RT_FUNCPROTO 0x00002000
#define _RT_CLASS 0x00004000
#define _RT_INSTANCE 0x00008000
#define _RT_WEAKREF 0x00010000
#define _RT_OUTER 0x00020000
typedef enum tagSQObjectType{
OT_NULL = (_RT_NULL|SQOBJECT_CANBEFALSE),
OT_INTEGER = (_RT_INTEGER|SQOBJECT_NUMERIC|SQOBJECT_CANBEFALSE),
OT_FLOAT = (_RT_FLOAT|SQOBJECT_NUMERIC|SQOBJECT_CANBEFALSE),
OT_BOOL = (_RT_BOOL|SQOBJECT_CANBEFALSE),
OT_STRING = (_RT_STRING|SQOBJECT_REF_COUNTED),
OT_TABLE = (_RT_TABLE|SQOBJECT_REF_COUNTED|SQOBJECT_DELEGABLE),
OT_ARRAY = (_RT_ARRAY|SQOBJECT_REF_COUNTED),
OT_USERDATA = (_RT_USERDATA|SQOBJECT_REF_COUNTED|SQOBJECT_DELEGABLE),
OT_CLOSURE = (_RT_CLOSURE|SQOBJECT_REF_COUNTED),
OT_NATIVECLOSURE = (_RT_NATIVECLOSURE|SQOBJECT_REF_COUNTED),
OT_GENERATOR = (_RT_GENERATOR|SQOBJECT_REF_COUNTED),
OT_USERPOINTER = _RT_USERPOINTER,
OT_THREAD = (_RT_THREAD|SQOBJECT_REF_COUNTED) ,
OT_FUNCPROTO = (_RT_FUNCPROTO|SQOBJECT_REF_COUNTED), //internal usage only
OT_CLASS = (_RT_CLASS|SQOBJECT_REF_COUNTED),
OT_INSTANCE = (_RT_INSTANCE|SQOBJECT_REF_COUNTED|SQOBJECT_DELEGABLE),
OT_WEAKREF = (_RT_WEAKREF|SQOBJECT_REF_COUNTED),
OT_OUTER = (_RT_OUTER|SQOBJECT_REF_COUNTED) //internal usage only
OT_NULL = (_RT_NULL|SQOBJECT_CANBEFALSE),
OT_INTEGER = (_RT_INTEGER|SQOBJECT_NUMERIC|SQOBJECT_CANBEFALSE),
OT_FLOAT = (_RT_FLOAT|SQOBJECT_NUMERIC|SQOBJECT_CANBEFALSE),
OT_BOOL = (_RT_BOOL|SQOBJECT_CANBEFALSE),
OT_STRING = (_RT_STRING|SQOBJECT_REF_COUNTED),
OT_TABLE = (_RT_TABLE|SQOBJECT_REF_COUNTED|SQOBJECT_DELEGABLE),
OT_ARRAY = (_RT_ARRAY|SQOBJECT_REF_COUNTED),
OT_USERDATA = (_RT_USERDATA|SQOBJECT_REF_COUNTED|SQOBJECT_DELEGABLE),
OT_CLOSURE = (_RT_CLOSURE|SQOBJECT_REF_COUNTED),
OT_NATIVECLOSURE = (_RT_NATIVECLOSURE|SQOBJECT_REF_COUNTED),
OT_GENERATOR = (_RT_GENERATOR|SQOBJECT_REF_COUNTED),
OT_USERPOINTER = _RT_USERPOINTER,
OT_THREAD = (_RT_THREAD|SQOBJECT_REF_COUNTED) ,
OT_FUNCPROTO = (_RT_FUNCPROTO|SQOBJECT_REF_COUNTED), //internal usage only
OT_CLASS = (_RT_CLASS|SQOBJECT_REF_COUNTED),
OT_INSTANCE = (_RT_INSTANCE|SQOBJECT_REF_COUNTED|SQOBJECT_DELEGABLE),
OT_WEAKREF = (_RT_WEAKREF|SQOBJECT_REF_COUNTED),
OT_OUTER = (_RT_OUTER|SQOBJECT_REF_COUNTED) //internal usage only
}SQObjectType;
#define ISREFCOUNTED(t) (t&SQOBJECT_REF_COUNTED)
@ -128,43 +128,43 @@ typedef enum tagSQObjectType{
typedef union tagSQObjectValue
{
struct SQTable *pTable;
struct SQArray *pArray;
struct SQClosure *pClosure;
struct SQOuter *pOuter;
struct SQGenerator *pGenerator;
struct SQNativeClosure *pNativeClosure;
struct SQString *pString;
struct SQUserData *pUserData;
SQInteger nInteger;
SQFloat fFloat;
SQUserPointer pUserPointer;
struct SQFunctionProto *pFunctionProto;
struct SQRefCounted *pRefCounted;
struct SQDelegable *pDelegable;
struct SQVM *pThread;
struct SQClass *pClass;
struct SQInstance *pInstance;
struct SQWeakRef *pWeakRef;
SQRawObjectVal raw;
struct SQTable *pTable;
struct SQArray *pArray;
struct SQClosure *pClosure;
struct SQOuter *pOuter;
struct SQGenerator *pGenerator;
struct SQNativeClosure *pNativeClosure;
struct SQString *pString;
struct SQUserData *pUserData;
SQInteger nInteger;
SQFloat fFloat;
SQUserPointer pUserPointer;
struct SQFunctionProto *pFunctionProto;
struct SQRefCounted *pRefCounted;
struct SQDelegable *pDelegable;
struct SQVM *pThread;
struct SQClass *pClass;
struct SQInstance *pInstance;
struct SQWeakRef *pWeakRef;
SQRawObjectVal raw;
}SQObjectValue;
typedef struct tagSQObject
{
SQObjectType _type;
SQObjectValue _unVal;
SQObjectType _type;
SQObjectValue _unVal;
}SQObject;
typedef struct tagSQMemberHandle{
SQBool _static;
SQInteger _index;
SQBool _static;
SQInteger _index;
}SQMemberHandle;
typedef struct tagSQStackInfos{
const SQChar* funcname;
const SQChar* source;
SQInteger line;
const SQChar* funcname;
const SQChar* source;
SQInteger line;
}SQStackInfos;
typedef struct SQVM* HSQUIRRELVM;
@ -181,17 +181,17 @@ typedef SQInteger (*SQREADFUNC)(SQUserPointer,SQUserPointer,SQInteger);
typedef SQInteger (*SQLEXREADFUNC)(SQUserPointer);
typedef struct tagSQRegFunction{
const SQChar *name;
SQFUNCTION f;
SQInteger nparamscheck;
const SQChar *typemask;
const SQChar *name;
SQFUNCTION f;
SQInteger nparamscheck;
const SQChar *typemask;
}SQRegFunction;
typedef struct tagSQFunctionInfo {
SQUserPointer funcid;
const SQChar *name;
const SQChar *source;
SQInteger line;
SQUserPointer funcid;
const SQChar *name;
const SQChar *source;
SQInteger line;
}SQFunctionInfo;
#ifndef SQMOD_PLUGIN_API
@ -394,6 +394,12 @@ SQUIRREL_API void sq_setnativedebughook(HSQUIRRELVM v,SQDEBUGHOOK hook);
#define SQ_FAILED(res) (res<0)
#define SQ_SUCCEEDED(res) (res>=0)
#ifdef __GNUC__
# define SQ_UNUSED_ARG(x) __attribute__((unused)) x
#else
# define SQ_UNUSED_ARG(x) x
#endif
#ifdef __cplusplus
} /*extern "C"*/
#endif