Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions squirrel/sqobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,31 @@
#include "sqclass.h"
#include "sqclosure.h"

const SQChar *SQMetaMethodName[] = {
_SC("_add"),
_SC("_sub"),
_SC("_mul"),
_SC("_div"),
_SC("_unm"),
_SC("_modulo"),
_SC("_set"),
_SC("_get"),
_SC("_typeof"),
_SC("_nexti"),
_SC("_cmp"),
_SC("_call"),
_SC("_cloned"),
_SC("_newslot"),
_SC("_delslot"),
_SC("_tostring"),
_SC("_newmember"),
_SC("_inherited"),
_SC("_shiftl"),
_SC("_shiftr"),
_SC("_and"),
_SC("_or"),
NULL
};

const SQChar *IdType2Name(SQObjectType type)
{
Expand Down Expand Up @@ -46,6 +71,14 @@ const SQChar *GetTypeName(const SQObjectPtr &obj1)
return IdType2Name(sq_type(obj1));
}

const SQChar *GetMetaMethodName(SQInteger mmId)
{
if (mmId<MT_LAST)
return SQMetaMethodName[mmId];
else
return _SC("unknown");
}

SQString *SQString::Create(SQSharedState *ss,const SQChar *s,SQInteger len)
{
SQString *str=ADD_STRING(ss,s,len);
Expand Down
27 changes: 7 additions & 20 deletions squirrel/sqobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,14 @@ enum SQMetaMethod{
MT_TOSTRING=15,
MT_NEWMEMBER=16,
MT_INHERITED=17,
MT_LAST = 18
MT_SHIFTL=18,
MT_SHIFTR=19,
MT_AND=20,
MT_OR=21,
MT_LAST = 22
};

#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")

extern const SQChar *SQMetaMethodName[];

#define _CONSTRUCT_VECTOR(type,size,ptr) { \
for(SQInteger n = 0; n < ((SQInteger)size); n++) { \
Expand Down Expand Up @@ -347,6 +333,7 @@ typedef sqvector<SQObjectPtr> SQObjectPtrVec;
typedef sqvector<SQInteger> SQIntVec;
const SQChar *GetTypeName(const SQObjectPtr &obj1);
const SQChar *IdType2Name(SQObjectType type);
const SQChar *GetMetaMethodName(SQInteger mmId);



Expand Down
20 changes: 2 additions & 18 deletions squirrel/sqstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,24 +120,8 @@ void SQSharedState::Init()
newsysstring(_SC("instance"));
newsysstring(_SC("bool"));
//meta methods
newmetamethod(MM_ADD);
newmetamethod(MM_SUB);
newmetamethod(MM_MUL);
newmetamethod(MM_DIV);
newmetamethod(MM_UNM);
newmetamethod(MM_MODULO);
newmetamethod(MM_SET);
newmetamethod(MM_GET);
newmetamethod(MM_TYPEOF);
newmetamethod(MM_NEXTI);
newmetamethod(MM_CMP);
newmetamethod(MM_CALL);
newmetamethod(MM_CLONED);
newmetamethod(MM_NEWSLOT);
newmetamethod(MM_DELSLOT);
newmetamethod(MM_TOSTRING);
newmetamethod(MM_NEWMEMBER);
newmetamethod(MM_INHERITED);
for (int i=0; i<MT_LAST; i++)
newmetamethod(SQMetaMethodName[i]);

_constructoridx = SQString::Create(this,_SC("constructor"));
_registry = SQTable::Create(this,0);
Expand Down
48 changes: 32 additions & 16 deletions squirrel/sqvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,23 @@ bool SQVM::BW_OP(SQUnsignedInteger op,SQObjectPtr &trg,const SQObjectPtr &o1,con
case BW_USHIFTR:res = (SQInteger)(*((SQUnsignedInteger*)&i1) >> i2); break;
default: { Raise_Error(_SC("internal vm error bitwise op failed")); return false; }
}
trg = res;
}
else if( (sq_type(o1) & OT_INSTANCE) )
{
SQMetaMethod mm;
switch(op) {
case BW_AND: mm = MT_AND; break;
case BW_OR: mm = MT_OR; break;
case BW_SHIFTL: mm = MT_SHIFTL; break;
case BW_SHIFTR: mm = MT_SHIFTR; break;
// add other
default: return false;
}
return ArithMetaMethod(mm,o1,o2,trg);
}
else { Raise_Error(_SC("bitwise op between '%s' and '%s'"),GetTypeName(o1),GetTypeName(o2)); return false;}
trg = res;

return true;
}

Expand Down Expand Up @@ -100,8 +114,20 @@ bool SQVM::ARITH_OP(SQUnsignedInteger op,SQObjectPtr &trg,const SQObjectPtr &o1,
if(op == '+' && (tmask & _RT_STRING)){
if(!StringCat(o1, o2, trg)) return false;
}
else if(!ArithMetaMethod(op,o1,o2,trg)) {
return false;
else
{
SQMetaMethod mm;
switch(op) {
case _SC('+'): mm=MT_ADD; break;
case _SC('-'): mm=MT_SUB; break;
case _SC('/'): mm=MT_DIV; break;
case _SC('*'): mm=MT_MUL; break;
case _SC('%'): mm=MT_MODULO; break;
case _SC('&'): mm=MT_AND; break;
case _SC('|'): mm=MT_OR; break;
default: mm=(SQMetaMethod)op;
}
if (!ArithMetaMethod(mm,o1,o2,trg)) return false;
}
}
return true;
Expand Down Expand Up @@ -151,26 +177,16 @@ SQVM::~SQVM()
REMOVE_FROM_CHAIN(&_ss(this)->_gc_chain,this);
}

bool SQVM::ArithMetaMethod(SQInteger op,const SQObjectPtr &o1,const SQObjectPtr &o2,SQObjectPtr &dest)
bool SQVM::ArithMetaMethod(SQMetaMethod mm,const SQObjectPtr &o1,const SQObjectPtr &o2,SQObjectPtr &dest)
{
SQMetaMethod mm;
switch(op){
case _SC('+'): mm=MT_ADD; break;
case _SC('-'): mm=MT_SUB; break;
case _SC('/'): mm=MT_DIV; break;
case _SC('*'): mm=MT_MUL; break;
case _SC('%'): mm=MT_MODULO; break;
default: mm = MT_ADD; assert(0); break; //shutup compiler
}
if(is_delegable(o1) && _delegable(o1)->_delegate) {

if(mm>=0 && mm<MT_LAST && is_delegable(o1) && _delegable(o1)->_delegate) {
SQObjectPtr closure;
if(_delegable(o1)->GetMetaMethod(this, mm, closure)) {
Push(o1);Push(o2);
return CallMetaMethod(closure,mm,2,dest);
}
}
Raise_Error(_SC("arith op %c on between '%s' and '%s'"),op,GetTypeName(o1),GetTypeName(o2));
Raise_Error(_SC("arith op %s on between '%s' and '%s'"), GetMetaMethodName(mm) ,GetTypeName(o1),GetTypeName(o2));
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion squirrel/sqvm.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ typedef sqvector<CallInfo> CallInfoVec;

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 ArithMetaMethod(SQMetaMethod mm, 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);
Expand Down