Skip to content

Commit

Permalink
Merge pull request #51 from zhangjiequan/feat/extend_all_lua_types_fo…
Browse files Browse the repository at this point in the history
…r_pr

Extend queryHelper
  • Loading branch information
CppCXY committed Feb 19, 2024
2 parents 261c623 + 005e38a commit 61d8cb5
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 6 deletions.
1 change: 1 addition & 0 deletions emmy_core/src/emmy_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ static const luaL_Reg lib[] = {
{"breakHere", breakHere},
{"stop", stop},
{"tcpSharedListen", tcpSharedListen},
{"registerTypeName", registerTypeName},
{nullptr, nullptr}
};

Expand Down
7 changes: 7 additions & 0 deletions emmy_debugger/include/emmy_debugger/debugger/emmy_debugger.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <functional>
#include <memory>
#include <set>
#include <bitset>

#include "emmy_debugger/api/lua_api.h"
#include "hook_state.h"
Expand Down Expand Up @@ -94,6 +95,8 @@ class Debugger: public std::enable_shared_from_this<Debugger>

void ClearVariableArenaRef();

bool RegisterTypeName(const std::string& typeName, std::string& err);

private:
std::shared_ptr<BreakPoint> FindBreakPoint(lua_Debug* ar);
std::shared_ptr<BreakPoint> FindBreakPoint(const std::string& file, int line);
Expand All @@ -112,6 +115,8 @@ class Debugger: public std::enable_shared_from_this<Debugger>
// bool HasCacheValue(int valueIndex) const;
void ClearCache() const;

int GetTypeFromName(const char* typeName);

lua_State* currentL;
lua_State* mainL;

Expand All @@ -137,4 +142,6 @@ class Debugger: public std::enable_shared_from_this<Debugger>
std::queue<std::shared_ptr<EvalContext>> evalQueue;

Arena<Variable> *arenaRef;

std::bitset<LUA_TTHREAD+1> registeredTypes;
};
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ int tcpSharedListen(lua_State* L);
// emmy.stop()
int stop(lua_State* L);

// emmy.registerTypeName(typeName: string): bool
int registerTypeName(lua_State* L);

bool install_emmy_debugger(struct lua_State* L);

std::string prepareEvalExpr(const std::string& eval);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ class ExtensionPoint {
void Initialize(lua_State *L);
//
bool QueryVariable(lua_State *L, Idx<Variable> variable, const char *typeName, int object, int depth);
bool QueryVariableCustom(lua_State *L, Idx<Variable> variable, const char *typeName, int object, int depth);

lua_State *QueryParentThread(lua_State *L);

private:
bool QueryVariableGeneric(lua_State *L, Idx<Variable> variable, const char *typeName, int object, int depth, const char* queryFunction);
};
1 change: 1 addition & 0 deletions emmy_debugger/include/emmy_debugger/emmy_facade.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class EmmyFacade
bool PipeListen(lua_State* L, const std::string& name, std::string& err);
bool PipeConnect(lua_State* L, const std::string& name, std::string& err);
int BreakHere(lua_State* L);
bool RegisterTypeName(lua_State *L, const std::string &typeName, std::string &err);

int OnConnect(bool suc);
int OnDisconnect();
Expand Down
33 changes: 30 additions & 3 deletions emmy_debugger/src/debugger/emmy_debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,13 @@ void Debugger::GetVariable(lua_State *L, Idx<Variable> variable, int index, int
variable->valueTypeName = typeName;
variable->valueType = type;


if (queryHelper && (type == LUA_TTABLE || type == LUA_TUSERDATA || type == LUA_TFUNCTION)) {
if (manager->extension.QueryVariable(L, variable, typeName, index, depth)) {
if (queryHelper) {
if (type >= 0 && type < registeredTypes.size() && registeredTypes.test(type)
&& manager->extension.QueryVariableCustom(L, variable, typeName, index, depth)) {
return;
}
else if ((type == LUA_TTABLE || type == LUA_TUSERDATA || type == LUA_TFUNCTION)
&& manager->extension.QueryVariable(L, variable, typeName, index, depth)) {
return;
}
}
Expand Down Expand Up @@ -1424,3 +1428,26 @@ void Debugger::ExecuteOnLuaThread(const Executor &exec) {
std::unique_lock<std::mutex> lock(luaThreadMtx);
luaThreadExecutors.push_back(exec);
}

int Debugger::GetTypeFromName(const char* typeName) {
if (strcmp(typeName, "nil") == 0) return LUA_TNIL;
if (strcmp(typeName, "boolean") == 0) return LUA_TBOOLEAN;
if (strcmp(typeName, "lightuserdata") == 0) return LUA_TLIGHTUSERDATA;
if (strcmp(typeName, "number") == 0) return LUA_TNUMBER;
if (strcmp(typeName, "string") == 0) return LUA_TSTRING;
if (strcmp(typeName, "table") == 0) return LUA_TTABLE;
if (strcmp(typeName, "function") == 0) return LUA_TFUNCTION;
if (strcmp(typeName, "userdata") == 0) return LUA_TUSERDATA;
if (strcmp(typeName, "thread") == 0) return LUA_TTHREAD;
return -1; // 未知类型
}

bool Debugger::RegisterTypeName(const std::string& typeName, std::string& err) {
int type = GetTypeFromName(typeName.c_str());
if (type == -1) {
err = "Unknown type name: " + typeName;
return false;
}
registeredTypes.set(type);
return true;
}
13 changes: 13 additions & 0 deletions emmy_debugger/src/debugger/emmy_debugger_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,19 @@ int stop(lua_State* L)
return 0;
}

// emmy.registerTypeName(typeName: string): bool
int registerTypeName(lua_State* L)
{
luaL_checkstring(L, 1);
std::string err;
const auto typeName = lua_tostring(L, 1);
const auto suc = EmmyFacade::Get().RegisterTypeName(L, typeName, err);
lua_pushboolean(L, suc);
if (suc) return 1;
lua_pushstring(L, err.c_str());
return 2;
}

int gc(lua_State* L)
{
EmmyFacade::Get().OnLuaStateGC(L);
Expand Down
14 changes: 11 additions & 3 deletions emmy_debugger/src/debugger/extension_point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ void ExtensionPoint::Initialize(lua_State *L) {
lua_rawset(L, LUA_REGISTRYINDEX);
}

bool ExtensionPoint::QueryVariable(lua_State *L, Idx<Variable> variable, const char *typeName, int object, int depth) {
bool ExtensionPoint::QueryVariableGeneric(lua_State *L, Idx<Variable> variable, const char *typeName, int object, int depth, const char* queryFunction) {
bool result = false;
object = lua_absindex(L, object);
const int t = lua_gettop(L);
lua_getglobal(L, ExtensionTable.c_str());

if (lua_istable(L, -1)) {
lua_getfield(L, -1, "queryVariable");
lua_getfield(L, -1, queryFunction);
if (lua_isfunction(L, -1)) {
pushVariable(L, variable);
lua_pushvalue(L, object);
Expand All @@ -140,7 +140,7 @@ bool ExtensionPoint::QueryVariable(lua_State *L, Idx<Variable> variable, const c
result = lua_toboolean(L, -1);
} else {
const auto err = lua_tostring(L, -1);
printf("query error: %s\n", err);
printf("query error in %s: %s\n", queryFunction, err);
}
}
}
Expand All @@ -149,6 +149,14 @@ bool ExtensionPoint::QueryVariable(lua_State *L, Idx<Variable> variable, const c
return result;
}

bool ExtensionPoint::QueryVariable(lua_State *L, Idx<Variable> variable, const char *typeName, int object, int depth) {
return QueryVariableGeneric(L, variable, typeName, object, depth, "queryVariable");
}

bool ExtensionPoint::QueryVariableCustom(lua_State *L, Idx<Variable> variable, const char *typeName, int object, int depth) {
return QueryVariableGeneric(L, variable, typeName, object, depth, "queryVariableCustom");
}

lua_State *ExtensionPoint::QueryParentThread(lua_State *L) {
lua_State *PL = nullptr;
const int t = lua_gettop(L);
Expand Down
10 changes: 10 additions & 0 deletions emmy_debugger/src/emmy_facade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,13 @@ void EmmyFacade::Attach(lua_State *L) {

lua_sethook(L, EmmyFacade::HookLua, LUA_MASKCALL | LUA_MASKLINE | LUA_MASKRET, 0);
}

bool EmmyFacade::RegisterTypeName(lua_State *L, const std::string &typeName, std::string &err) {
auto debugger = GetDebugger(L);
if (!debugger) {
err = "Debugger does not exist";
return false;
}
const auto suc = debugger->RegisterTypeName(typeName, err);
return suc;
}

0 comments on commit 61d8cb5

Please sign in to comment.