Skip to content

Commit

Permalink
refactor: rework Lua JSON functions
Browse files Browse the repository at this point in the history
  • Loading branch information
edubart committed Sep 20, 2024
1 parent ba303d4 commit 89cf971
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 137 deletions.
2 changes: 1 addition & 1 deletion src/cartesi-machine.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1751,7 +1751,7 @@ for _, r in ipairs(memory_range_replace) do
end

if store_config == true then
io.write(cartesi.tojson(main_config, false, 2), "\n")
io.write(cartesi.tojson(main_config, 2), "\n")
elseif store_config then
local f <close> = assert(io.open(store_config, "w"))
f:write(cartesi.tojson(main_config))
Expand Down
14 changes: 4 additions & 10 deletions src/clua-cartesi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,23 +107,17 @@ static int cartesi_mod_frombase64(lua_State *L) try {
}

static int cartesi_mod_tojson(lua_State *L) try {
lua_settop(L, 3);
const bool base64encode = lua_toboolean(L, 2);
const int indent = static_cast<int>(luaL_optinteger(L, 3, -1));
const std::string s = cartesi::clua_check_json(L, 1, base64encode).dump(indent);
lua_pushlstring(L, s.data(), s.size());
const int indent = static_cast<int>(luaL_optinteger(L, 2, -1));
lua_settop(L, 1);
cartesi::clua_check_json_string(L, 1, indent);
return 1;
} catch (std::exception &e) {
luaL_error(L, "%s", e.what());
return 1;
}

static int cartesi_mod_fromjson(lua_State *L) try {
lua_settop(L, 2);
bool base64decode = lua_toboolean(L, 2);
size_t size = 0;
const char *data = luaL_checklstring(L, 1, &size);
cartesi::clua_push_json(L, nlohmann::json::parse(std::string_view(data, size)), base64decode);
cartesi::clua_push_json_table(L, luaL_checkstring(L, 1));
return 1;
} catch (std::exception &e) {
luaL_error(L, "%s", e.what());
Expand Down
12 changes: 6 additions & 6 deletions src/clua-i-virtual-machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static int machine_obj_index_get_proof(lua_State *L) {
if (cm_get_proof(m.get(), address, log2_size, &proof) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
clua_push_json(L, nlohmann::json::parse(proof));
clua_push_json_table(L, proof);
return 1;
}

Expand All @@ -44,7 +44,7 @@ static int machine_obj_index_get_initial_config(lua_State *L) {
if (cm_get_initial_config(m.get(), &config) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
clua_push_json(L, nlohmann::json::parse(config));
clua_push_json_table(L, config);
return 1;
}

Expand Down Expand Up @@ -259,7 +259,7 @@ static int machine_obj_index_get_memory_ranges(lua_State *L) {
if (cm_get_memory_ranges(m.get(), &ranges) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
clua_push_json(L, nlohmann::json::parse(ranges));
clua_push_json_table(L, ranges);
return 1;
}

Expand All @@ -272,7 +272,7 @@ static int machine_obj_index_log_reset_uarch(lua_State *L) {
if (cm_log_reset_uarch(m.get(), log_type, true, &access_log) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
clua_push_json(L, nlohmann::json::parse(access_log));
clua_push_json_table(L, access_log);
return 1;
}

Expand All @@ -298,7 +298,7 @@ static int machine_obj_index_log_step_uarch(lua_State *L) {
if (cm_log_step_uarch(m.get(), log_type, true, &access_log) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
clua_push_json(L, nlohmann::json::parse(access_log));
clua_push_json_table(L, access_log);
return 1;
}

Expand Down Expand Up @@ -471,7 +471,7 @@ static int machine_obj_index_log_send_cmio_response(lua_State *L) {
if (cm_log_send_cmio_response(m.get(), reason, data, length, log_type, true, &access_log) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
clua_push_json(L, nlohmann::json::parse(access_log));
clua_push_json_table(L, access_log);
return 1;
}

Expand Down
38 changes: 19 additions & 19 deletions src/clua-jsonrpc-machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static int jsonrpc_machine_class_get_default_config(lua_State *L) {
if (cm_jsonrpc_get_default_config(managed_jsonrpc_mgr.get(), &config) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
clua_push_json(L, nlohmann::json::parse(config));
clua_push_json_table(L, config);
return 1;
}

Expand All @@ -61,18 +61,17 @@ static int jsonrpc_machine_class_verify_step_uarch(lua_State *L) {
const int ctxidx = lua_upvalueindex(2);
lua_settop(L, 5);
auto &managed_jsonrpc_mgr = clua_check<clua_managed_cm_ptr<cm_jsonrpc_mgr>>(L, stubidx, ctxidx);
const std::string access_log = clua_check_json(L, 2).dump();
const char *access_log = clua_check_json_string(L, 2);
if (!lua_isnil(L, 1) || !lua_isnil(L, 3)) {
cm_hash root_hash{};
clua_check_cm_hash(L, 1, &root_hash);
cm_hash target_hash{};
clua_check_cm_hash(L, 3, &target_hash);
if (cm_jsonrpc_verify_step_uarch(managed_jsonrpc_mgr.get(), &root_hash, access_log.c_str(), &target_hash,
true) != 0) {
if (cm_jsonrpc_verify_step_uarch(managed_jsonrpc_mgr.get(), &root_hash, access_log, &target_hash, true) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
} else {
if (cm_jsonrpc_verify_step_uarch(managed_jsonrpc_mgr.get(), nullptr, access_log.c_str(), nullptr, true) != 0) {
if (cm_jsonrpc_verify_step_uarch(managed_jsonrpc_mgr.get(), nullptr, access_log, nullptr, true) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
}
Expand All @@ -88,18 +87,17 @@ static int jsonrpc_machine_class_verify_reset_uarch(lua_State *L) {
const int ctxidx = lua_upvalueindex(2);
lua_settop(L, 5);
auto &managed_jsonrpc_mgr = clua_check<clua_managed_cm_ptr<cm_jsonrpc_mgr>>(L, stubidx, ctxidx);
const std::string access_log = clua_check_json(L, 2).dump();
const char *access_log = clua_check_json_string(L, 2);
if (!lua_isnil(L, 1) || !lua_isnil(L, 3)) {
cm_hash root_hash{};
clua_check_cm_hash(L, 1, &root_hash);
cm_hash target_hash{};
clua_check_cm_hash(L, 3, &target_hash);
if (cm_jsonrpc_verify_reset_uarch(managed_jsonrpc_mgr.get(), &root_hash, access_log.c_str(), &target_hash,
true) != 0) {
if (cm_jsonrpc_verify_reset_uarch(managed_jsonrpc_mgr.get(), &root_hash, access_log, &target_hash, true) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
} else {
if (cm_jsonrpc_verify_reset_uarch(managed_jsonrpc_mgr.get(), nullptr, access_log.c_str(), nullptr, true) != 0) {
if (cm_jsonrpc_verify_reset_uarch(managed_jsonrpc_mgr.get(), nullptr, access_log, nullptr, true) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
}
Expand All @@ -119,19 +117,19 @@ static int jsonrpc_machine_class_verify_send_cmio_response(lua_State *L) {
size_t length{0};
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
const auto *data = reinterpret_cast<const unsigned char *>(luaL_checklstring(L, 2, &length));
const std::string access_log = clua_check_json(L, 4).dump();
const char *access_log = clua_check_json_string(L, 4);
if (!lua_isnil(L, 3) || !lua_isnil(L, 5)) {
cm_hash root_hash{};
clua_check_cm_hash(L, 3, &root_hash);
cm_hash target_hash{};
clua_check_cm_hash(L, 5, &target_hash);
if (cm_jsonrpc_verify_send_cmio_response(managed_jsonrpc_mgr.get(), reason, data, length, &root_hash,
access_log.c_str(), &target_hash, true) != 0) {
access_log, &target_hash, true) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
} else {
if (cm_jsonrpc_verify_send_cmio_response(managed_jsonrpc_mgr.get(), reason, data, length, nullptr,
access_log.c_str(), nullptr, true) != 0) {
if (cm_jsonrpc_verify_send_cmio_response(managed_jsonrpc_mgr.get(), reason, data, length, nullptr, access_log,
nullptr, true) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
}
Expand Down Expand Up @@ -163,15 +161,17 @@ static int jsonrpc_machine_ctor(lua_State *L) {
const int ctxidx = lua_upvalueindex(2);
auto &managed_jsonrpc_mgr = clua_check<clua_managed_cm_ptr<cm_jsonrpc_mgr>>(L, lua_upvalueindex(1), ctxidx);
auto &managed_machine = clua_push_to(L, clua_managed_cm_ptr<cm_machine>(nullptr), ctxidx);
const std::string runtime_config = clua_check_json(L, 3).dump();
const char *runtime_config = nullptr;
if (!lua_isnil(L, 3)) {
runtime_config = clua_check_json_string(L, 3);
}
if (lua_type(L, 2) == LUA_TTABLE) {
const std::string config = clua_check_json(L, 2).dump();
if (cm_jsonrpc_create_machine(managed_jsonrpc_mgr.get(), config.c_str(), runtime_config.c_str(),
&managed_machine.get()) != 0) {
const char *config = clua_check_json_string(L, 2);
if (cm_jsonrpc_create_machine(managed_jsonrpc_mgr.get(), config, runtime_config, &managed_machine.get()) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
} else {
if (cm_jsonrpc_load_machine(managed_jsonrpc_mgr.get(), luaL_checkstring(L, 2), runtime_config.c_str(),
if (cm_jsonrpc_load_machine(managed_jsonrpc_mgr.get(), luaL_checkstring(L, 2), runtime_config,
&managed_machine.get()) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
Expand Down Expand Up @@ -206,7 +206,7 @@ static int jsonrpc_server_class_get_version(lua_State *L) {
if (cm_jsonrpc_get_version(managed_jsonrpc_mgr.get(), &version) != 0) {
return luaL_error(L, "%s", cm_get_last_error_message());
}
clua_push_json(L, nlohmann::json::parse(version));
clua_push_json_table(L, version);
return 1;
}

Expand Down
Loading

0 comments on commit 89cf971

Please sign in to comment.