[DEV] remote _closure()

This commit is contained in:
Edouard DUPIN 2018-07-03 22:03:13 +02:00
parent 04cdeeac1a
commit 440bc87551
6 changed files with 37 additions and 38 deletions

View File

@ -113,8 +113,8 @@ bool rabbit::Class::newSlot(rabbit::SharedState *ss,const rabbit::ObjectPtr &key
} else {
rabbit::ObjectPtr theval = val;
if(_base && sq_type(val) == rabbit::OT_CLOSURE) {
theval = _closure(val)->clone();
_closure(theval)->_base = _base;
theval = const_cast<rabbit::Closure*>(val.toClosure())->clone();
theval.toClosure()->_base = _base;
__ObjaddRef(_base); //ref for the closure
}
if(sq_type(temp) == rabbit::OT_NULL) {

View File

@ -149,7 +149,6 @@ namespace rabbit {
#define is_delegable(t) (sq_type(t)&SQOBJECT_DELEGABLE)
#define raw_type(obj) _RAW_TYPE((obj)._type)
#define _closure(obj) ((obj)._unVal.pClosure)
#define _generator(obj) ((obj)._unVal.pGenerator)
#define _nativeclosure(obj) ((obj)._unVal.pNativeClosure)
#define _userdata(obj) ((obj)._unVal.pUserData)

View File

@ -612,7 +612,7 @@ bool rabbit::VirtualMachine::CLOSURE_OP(rabbit::ObjectPtr &target, rabbit::Funct
findOuter(closure->_outervalues[i], &STK(v._src.toInteger()));
break;
case otOUTER:
closure->_outervalues[i] = _closure(ci->_closure)->_outervalues[v._src.toInteger()];
closure->_outervalues[i] = ci->_closure.toClosure()->_outervalues[v._src.toInteger()];
break;
}
}
@ -694,7 +694,7 @@ bool rabbit::VirtualMachine::execute(rabbit::ObjectPtr &closure, int64_t nargs,
switch(et) {
case ET_CALL: {
temp_reg = closure;
if(!startcall(_closure(temp_reg), _top - nargs, nargs, stackbase, false)) {
if(!startcall(temp_reg.toClosure(), _top - nargs, nargs, stackbase, false)) {
//call the handler if there are no calls in the stack, if not relies on the previous node
if(ci == NULL) callerrorHandler(_lasterror);
return false;
@ -723,7 +723,7 @@ exception_restore:
{
const rabbit::Instruction &_i_ = *ci->_ip++;
//dumpstack(_stackbase);
//printf("\n[%d] %s %d %d %d %d\n",ci->_ip-_closure(ci->_closure)->_function->_instructions,g_InstrDesc[_i_.op].name,arg0,arg1,arg2,arg3);
//printf("\n[%d] %s %d %d %d %d\n",ci->_ip-ci->_closure.toClosure()->_function->_instructions,g_InstrDesc[_i_.op].name,arg0,arg1,arg2,arg3);
switch(_i_.op)
{
case _OP_LINE: if (_debughook) callDebugHook('l',arg1); continue;
@ -735,12 +735,12 @@ exception_restore:
case _OP_TAILCALL:{
rabbit::ObjectPtr &t = STK(arg1);
if (sq_type(t) == rabbit::OT_CLOSURE
&& (!_closure(t)->_function->_bgenerator)){
&& (!t.toClosure()->_function->_bgenerator)){
rabbit::ObjectPtr clo = t;
int64_t last_top = _top;
if(_openouters) closeOuters(&(_stack[_stackbase]));
for (int64_t i = 0; i < arg3; i++) STK(i) = STK(arg2 + i);
_GUARD(startcall(_closure(clo), ci->_target, arg3, _stackbase, true));
_GUARD(startcall(clo.toClosure(), ci->_target, arg3, _stackbase, true));
if (last_top >= _top) {
_top = last_top;
}
@ -751,7 +751,7 @@ exception_restore:
rabbit::ObjectPtr clo = STK(arg1);
switch (sq_type(clo)) {
case rabbit::OT_CLOSURE:
_GUARD(startcall(_closure(clo), sarg0, arg3, _stackbase+arg2, false));
_GUARD(startcall(clo.toClosure(), sarg0, arg3, _stackbase+arg2, false));
continue;
case rabbit::OT_NATIVECLOSURE: {
bool suspend;
@ -781,7 +781,7 @@ exception_restore:
case rabbit::OT_CLOSURE:
stkbase = _stackbase+arg2;
_stack[stkbase] = inst;
_GUARD(startcall(_closure(clo), -1, arg3, stkbase, false));
_GUARD(startcall(clo.toClosure(), -1, arg3, stkbase, false));
break;
case rabbit::OT_NATIVECLOSURE:
bool dummy;
@ -874,7 +874,7 @@ exception_restore:
continue;
case _OP_LOADNULLS:{ for(int32_t n=0; n < arg1; n++) STK(arg0+n).Null(); }continue;
case _OP_LOADROOT: {
rabbit::WeakRef *w = _closure(ci->_closure)->_root;
rabbit::WeakRef *w = ci->_closure.toClosure()->_root;
if(sq_type(w->_obj) != rabbit::OT_NULL) {
TARGET = w->_obj;
} else {
@ -892,13 +892,13 @@ exception_restore:
continue;
case _OP_JZ: if(IsFalse(STK(arg0))) ci->_ip+=(sarg1); continue;
case _OP_GETOUTER: {
rabbit::Closure *cur_cls = _closure(ci->_closure);
rabbit::Closure *cur_cls = ci->_closure.toClosure();
rabbit::Outer *otr = _outer(cur_cls->_outervalues[arg1]);
TARGET = *(otr->_valptr);
}
continue;
case _OP_SETOUTER: {
rabbit::Closure *cur_cls = _closure(ci->_closure);
rabbit::Closure *cur_cls = ci->_closure.toClosure();
rabbit::Outer *otr = _outer(cur_cls->_outervalues[arg1]);
*(otr->_valptr) = STK(arg2);
if(arg0 != 0xFF) {
@ -1035,7 +1035,7 @@ exception_restore:
case _OP_CLONE: _GUARD(clone(STK(arg1), TARGET)); continue;
case _OP_TYPEOF: _GUARD(typeOf(STK(arg1), TARGET)) continue;
case _OP_PUSHTRAP:{
rabbit::Instruction *_iv = _closure(ci->_closure)->_function->_instructions;
rabbit::Instruction *_iv = ci->_closure.toClosure()->_function->_instructions;
_etraps.pushBack(rabbit::ExceptionTrap(_top,_stackbase, &_iv[(ci->_ip-_iv)+arg1], arg0)); traps++;
ci->_etraps++;
}
@ -1052,7 +1052,7 @@ exception_restore:
_GUARD(newSlotA(STK(arg1),STK(arg2),STK(arg3),(arg0&NEW_SLOT_ATTRIBUTES_FLAG) ? STK(arg2-1) : rabbit::ObjectPtr(),(arg0&NEW_SLOT_STATIC_FLAG)?true:false,false));
continue;
case _OP_GETBASE:{
rabbit::Closure *clo = _closure(ci->_closure);
rabbit::Closure *clo = ci->_closure.toClosure();
if(clo->_base) {
TARGET = clo->_base;
}
@ -1130,7 +1130,7 @@ void rabbit::VirtualMachine::callerrorHandler(rabbit::ObjectPtr &error)
void rabbit::VirtualMachine::callDebugHook(int64_t type,int64_t forcedline)
{
_debughook = false;
rabbit::FunctionProto *func=_closure(ci->_closure)->_function;
rabbit::FunctionProto *func=ci->_closure.toClosure()->_function;
if(_debughook_native) {
const char *src = sq_type(func->_sourcename) == rabbit::OT_STRING?_stringval(func->_sourcename):NULL;
const char *fname = sq_type(func->_name) == rabbit::OT_STRING?_stringval(func->_name):NULL;
@ -1300,7 +1300,7 @@ bool rabbit::VirtualMachine::get(const rabbit::ObjectPtr &self, const rabbit::Ob
}
//#ifdef ROrabbit::OT_FALLBACK
if(selfidx == 0) {
rabbit::WeakRef *w = _closure(ci->_closure)->_root;
rabbit::WeakRef *w = ci->_closure.toClosure()->_root;
if(sq_type(w->_obj) != rabbit::OT_NULL)
{
if(get(*((const rabbit::ObjectPtr *)&w->_obj),key,dest,0,DONT_FALL_BACK)) return true;
@ -1791,7 +1791,7 @@ void rabbit::VirtualMachine::dumpstack(int64_t stackbase,bool dumpall)
case rabbit::OT_NULL: printf("NULL"); break;
case rabbit::OT_TABLE: printf("TABLE %p[%p]",obj.toTable(),obj.toTable()->_delegate);break;
case rabbit::OT_ARRAY: printf("ARRAY %p",obj.toArray());break;
case rabbit::OT_CLOSURE: printf("CLOSURE [%p]",_closure(obj));break;
case rabbit::OT_CLOSURE: printf("CLOSURE [%p]",obj.toClosure());break;
case rabbit::OT_NATIVECLOSURE: printf("NATIVECLOSURE");break;
case rabbit::OT_USERDATA: printf("USERDATA %p[%p]",_userdataval(obj),_userdata(obj)->_delegate);break;
case rabbit::OT_GENERATOR: printf("GENERATOR %p",_generator(obj));break;

View File

@ -410,7 +410,7 @@ rabbit::Result rabbit::sq_getclosureinfo(rabbit::VirtualMachine* v,int64_t idx,u
{
rabbit::Object o = stack_get(v, idx);
if(sq_type(o) == rabbit::OT_CLOSURE) {
rabbit::Closure *c = _closure(o);
rabbit::Closure *c = o.toClosure();
rabbit::FunctionProto *proto = c->_function;
*nparams = (uint64_t)proto->_nparameters;
*nfreevars = (uint64_t)proto->_noutervalues;
@ -474,12 +474,12 @@ rabbit::Result rabbit::sq_bindenv(rabbit::VirtualMachine* v,int64_t idx)
rabbit::WeakRef *w = _refcounted(env)->getWeakRef(sq_type(env));
rabbit::ObjectPtr ret;
if(sq_isclosure(o)) {
rabbit::Closure *c = _closure(o)->clone();
rabbit::Closure *c = o.toClosure()->clone();
__Objrelease(c->_env);
c->_env = w;
__ObjaddRef(c->_env);
if(_closure(o)->_base) {
c->_base = _closure(o)->_base;
if(o.toClosure()->_base) {
c->_base = o.toClosure()->_base;
__ObjaddRef(c->_base);
}
ret = c;
@ -507,7 +507,7 @@ rabbit::Result rabbit::sq_getclosurename(rabbit::VirtualMachine* v,int64_t idx)
v->push(_nativeclosure(o)->_name);
}
else { //closure
v->push(_closure(o)->_function->_name);
v->push(o.toClosure()->_function->_name);
}
return SQ_OK;
}
@ -518,7 +518,7 @@ rabbit::Result rabbit::sq_setclosureroot(rabbit::VirtualMachine* v,int64_t idx)
rabbit::Object o = stack_get(v, -1);
if(!sq_isclosure(c)) return sq_throwerror(v, "closure expected");
if(sq_istable(o)) {
_closure(c)->setRoot(o.toTable()->getWeakRef(rabbit::OT_TABLE));
c.toClosure()->setRoot(o.toTable()->getWeakRef(rabbit::OT_TABLE));
v->pop();
return SQ_OK;
}
@ -529,7 +529,7 @@ rabbit::Result rabbit::sq_getclosureroot(rabbit::VirtualMachine* v,int64_t idx)
{
rabbit::ObjectPtr &c = stack_get(v,idx);
if(!sq_isclosure(c)) return sq_throwerror(v, "closure expected");
v->push(_closure(c)->_root->_obj);
v->push(c.toClosure()->_root->_obj);
return SQ_OK;
}
@ -1120,7 +1120,7 @@ const char * rabbit::sq_getlocal(rabbit::VirtualMachine* v,uint64_t level,uint64
rabbit::VirtualMachine::callInfo &ci=v->_callsstack[lvl];
if(sq_type(ci._closure)!=rabbit::OT_CLOSURE)
return NULL;
rabbit::Closure *c=_closure(ci._closure);
rabbit::Closure *c=ci._closure.toClosure();
rabbit::FunctionProto *func=c->_function;
if(func->_noutervalues > (int64_t)idx) {
v->push(*_outer(c->_outervalues[idx])->_valptr);
@ -1219,7 +1219,7 @@ rabbit::Result rabbit::sq_tailcall(rabbit::VirtualMachine* v, int64_t nparams)
if (sq_type(res) != rabbit::OT_CLOSURE) {
return sq_throwerror(v, "only closure can be tail called");
}
rabbit::Closure *clo = _closure(res);
rabbit::Closure *clo = res.toClosure();
if (clo->_function->_bgenerator)
{
return sq_throwerror(v, "generators cannot be tail called");
@ -1305,11 +1305,11 @@ rabbit::Result rabbit::sq_writeclosure(rabbit::VirtualMachine* v,SQWRITEFUNC w,r
rabbit::ObjectPtr *o = NULL;
_GETSAFE_OBJ(v, -1, rabbit::OT_CLOSURE,o);
unsigned short tag = SQ_BYTECODE_STREAM_TAG;
if(_closure(*o)->_function->_noutervalues)
if(o->toClosure()->_function->_noutervalues)
return sq_throwerror(v,"a closure with free variables bound cannot be serialized");
if(w(up,&tag,2) != 2)
return sq_throwerror(v,"io error");
if(!_closure(*o)->save(v,up,w))
if(!o->toClosure()->save(v,up,w))
return SQ_ERROR;
return SQ_OK;
}
@ -1362,7 +1362,7 @@ const char * rabbit::sq_getfreevariable(rabbit::VirtualMachine* v,int64_t idx,ui
switch(sq_type(self))
{
case rabbit::OT_CLOSURE:{
rabbit::Closure *clo = _closure(self);
rabbit::Closure *clo = self.toClosure();
rabbit::FunctionProto *fp = clo->_function;
if(((uint64_t)fp->_noutervalues) > nval) {
v->push(*(_outer(clo->_outervalues[nval])->_valptr));
@ -1390,9 +1390,9 @@ rabbit::Result rabbit::sq_setfreevariable(rabbit::VirtualMachine* v,int64_t idx,
switch(sq_type(self))
{
case rabbit::OT_CLOSURE:{
rabbit::FunctionProto *fp = _closure(self)->_function;
rabbit::FunctionProto *fp = self.toClosure()->_function;
if(((uint64_t)fp->_noutervalues) > nval){
*(_outer(_closure(self)->_outervalues[nval])->_valptr) = stack_get(v,-1);
*(_outer(self.toClosure()->_outervalues[nval])->_valptr) = stack_get(v,-1);
}
else return sq_throwerror(v,"invalid free var index");
}

View File

@ -237,7 +237,7 @@ static int64_t base_compilestring(rabbit::VirtualMachine* v)
static int64_t base_newthread(rabbit::VirtualMachine* v)
{
rabbit::ObjectPtr &func = stack_get(v,2);
int64_t stksize = (_closure(func)->_function->_stacksize << 1) +2;
int64_t stksize = (func.toClosure()->_function->_stacksize << 1) +2;
rabbit::VirtualMachine* newv = sq_newthread(v, (stksize < MIN_STACK_OVERHEAD + 2)? MIN_STACK_OVERHEAD + 2 : stksize);
sq_move(newv,v,-2);
return 1;
@ -929,7 +929,7 @@ static int64_t closure_pcall(rabbit::VirtualMachine* v)
static int64_t closure_call(rabbit::VirtualMachine* v)
{
rabbit::ObjectPtr &c = stack_get(v, -1);
if (sq_type(c) == rabbit::OT_CLOSURE && (_closure(c)->_function->_bgenerator == false))
if (sq_type(c) == rabbit::OT_CLOSURE && (c.toClosure()->_function->_bgenerator == false))
{
return sq_tailcall(v, sq_gettop(v) - 1);
}
@ -980,7 +980,7 @@ static int64_t closure_getinfos(rabbit::VirtualMachine* v) {
rabbit::Object o = stack_get(v,1);
rabbit::Table *res = rabbit::Table::create(_get_shared_state(v),4);
if(sq_type(o) == rabbit::OT_CLOSURE) {
rabbit::FunctionProto *f = _closure(o)->_function;
rabbit::FunctionProto *f = o.toClosure()->_function;
int64_t nparams = f->_nparameters + (f->_varparams?1:0);
rabbit::ObjectPtr params = rabbit::Array::create(_get_shared_state(v),nparams);
rabbit::ObjectPtr defparams = rabbit::Array::create(_get_shared_state(v),f->_ndefaultparams);
@ -988,7 +988,7 @@ static int64_t closure_getinfos(rabbit::VirtualMachine* v) {
params.toArray()->set((int64_t)n,f->_parameters[n]);
}
for(int64_t j = 0; j<f->_ndefaultparams; j++) {
defparams.toArray()->set((int64_t)j,_closure(o)->_defaultparams[j]);
defparams.toArray()->set((int64_t)j,o.toClosure()->_defaultparams[j]);
}
if(f->_varparams) {
params.toArray()->set(nparams-1,rabbit::String::create(_get_shared_state(v),"...",-1));

View File

@ -28,7 +28,7 @@ rabbit::Result rabbit::sq_getfunctioninfo(rabbit::VirtualMachine* v,int64_t leve
if (cssize > level) {
rabbit::VirtualMachine::callInfo &ci = v->_callsstack[cssize-level-1];
if(sq_isclosure(ci._closure)) {
rabbit::Closure *c = _closure(ci._closure);
rabbit::Closure *c = ci._closure.toClosure();
rabbit::FunctionProto *proto = c->_function;
fi->funcid = proto;
fi->name = sq_type(proto->_name) == rabbit::OT_STRING?_stringval(proto->_name):"unknown";
@ -48,7 +48,7 @@ rabbit::Result rabbit::sq_stackinfos(rabbit::VirtualMachine* v, int64_t level, r
rabbit::VirtualMachine::callInfo &ci = v->_callsstack[cssize-level-1];
switch (sq_type(ci._closure)) {
case rabbit::OT_CLOSURE:{
rabbit::FunctionProto *func = _closure(ci._closure)->_function;
rabbit::FunctionProto *func = ci._closure.toClosure()->_function;
if (sq_type(func->_name) == rabbit::OT_STRING)
si->funcname = _stringval(func->_name);
if (sq_type(func->_sourcename) == rabbit::OT_STRING)