Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enabling static access with the _get meta method. #266

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
9 changes: 9 additions & 0 deletions squirrel/sqclass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ bool SQClass::GetAttributes(const SQObjectPtr &key,SQObjectPtr &outval)
return false;
}

bool SQClass::GetMetaMethod(SQVM* SQ_UNUSED_ARG(v), SQMetaMethod mm, SQObjectPtr& res)
{
if (sq_type(_metamethods[mm]) != OT_NULL) {
res = _metamethods[mm];
return true;
}
return false;
}

///////////////////////////////////////////////////////////////////////
void SQInstance::Init(SQSharedState *ss)
{
Expand Down
1 change: 1 addition & 0 deletions squirrel/sqclass.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ struct SQClass : public CHAINABLE_OBJ
#endif
SQInteger Next(const SQObjectPtr &refpos, SQObjectPtr &outkey, SQObjectPtr &outval);
SQInstance *CreateInstance();
bool GetMetaMethod(SQVM* v, SQMetaMethod mm, SQObjectPtr& res);
SQTable *_members;
SQClass *_base;
SQClassMemberVec _defaultvalues;
Expand Down
19 changes: 19 additions & 0 deletions squirrel/sqvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,25 @@ SQInteger SQVM::FallBackGet(const SQObjectPtr &self,const SQObjectPtr &key,SQObj
}
}
break;
case OT_CLASS: {
SQObjectPtr closure;
if (_class(self)->GetMetaMethod(this, MT_GET, closure)) {
Push(self); Push(key);
_nmetamethodscall++;
AutoDec ad(&_nmetamethodscall);
if (Call(closure, 2, _top - 2, dest, SQFalse)) {
Pop(2);
return FALLBACK_OK;
}
else {
Pop(2);
if (sq_type(_lasterror) != OT_NULL) { //NULL means "clean failure" (not found)
return FALLBACK_ERROR;
}
}
}
}
break;
default: break;//shutup GCC 4.x
}
// no metamethod or no fallback type
Expand Down