Skip to content

Commit 9b1b1bb

Browse files
committed
show metatable & __index
1 parent 5109d69 commit 9b1b1bb

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

emmy_core/emmy_debugger.cpp

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,37 @@ void Debugger::GetVariable(Variable* variable, lua_State* L, int index, int dept
327327
lua_pop(L, 1);
328328
tableSize++;
329329
}
330-
char buff[100];
331-
snprintf(buff, sizeof(buff), "table(0x%p)", tableAddr);
332-
variable->value = buff;
330+
331+
if (lua_getmetatable(L, index)) {
332+
// metatable
333+
auto* metatable = new Variable;
334+
metatable->name = "metatable";
335+
metatable->nameType = LUA_TSTRING;
336+
GetVariable(metatable, L, -1, 2);
337+
variable->children.push_back(metatable);
338+
339+
// __index
340+
{
341+
if (lua_getfield(L, -1, "__index")) {
342+
Variable v;
343+
GetVariable(&v, L, -1, 2);
344+
if (depth > 1) {
345+
for (auto* child : v.children) {
346+
variable->children.push_back(child->Clone());
347+
}
348+
}
349+
tableSize += v.children.size();
350+
}
351+
lua_pop(L, 1);
352+
}
353+
354+
// metatable
355+
lua_pop(L, 1);
356+
}
357+
358+
std::stringstream ss;
359+
ss << "table(0x" << std::hex << tableAddr << std::dec << ", size = " << tableSize << ")";
360+
variable->value = ss.str();
333361
break;
334362
}
335363
}

emmy_core/stack.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,38 @@ Variable::Variable(): nameType(LUA_TSTRING), valueType(0), cacheId(0){
2020
}
2121

2222
Variable::~Variable() {
23-
for (auto child : children) {
23+
for (auto* child : children) {
2424
delete child;
2525
}
2626
}
2727

28+
Variable* Variable::Clone() {
29+
auto* to = new Variable;
30+
to->name = name;
31+
to->nameType = nameType;
32+
to->value = value;
33+
to->valueType = valueType;
34+
to->valueTypeName = valueTypeName;
35+
for (auto* child : children) {
36+
auto* c = new Variable;
37+
to->children.push_back(child->Clone());
38+
}
39+
return to;
40+
}
41+
2842
Stack::Stack(): level(0), line(0) {
2943
}
3044

3145
Stack::~Stack() {
32-
for (auto var : localVariables) {
46+
for (auto* var : localVariables) {
3347
delete var;
3448
}
35-
for (auto var : upvalueVariables) {
49+
for (auto* var : upvalueVariables) {
3650
delete var;
3751
}
3852
}
3953

4054
Variable* Stack::CreateVariable() {
41-
auto variable = new Variable();
55+
auto* variable = new Variable();
4256
return variable;
4357
}

emmy_core/types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class Variable {
4949
int cacheId;
5050
Variable();
5151
~Variable();
52+
Variable* Clone();
5253
};
5354

5455
class Stack {

0 commit comments

Comments
 (0)