Skip to content

Commit

Permalink
Upgraded json parser (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
britzl committed Nov 2, 2022
1 parent 62ba21d commit e6c621f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 54 deletions.
9 changes: 1 addition & 8 deletions extension-push/src/push_android.cpp
Expand Up @@ -100,14 +100,7 @@ static int Push_Schedule(lua_State* L)
const char* payload = 0;
if (top > 3) {
payload = luaL_checkstring(L, 4);

// Verify that the payload is valid and can be delivered later on.
char payload_err[128];
if (!dmPush::VerifyPayload(L, payload, payload_err, sizeof(payload_err))) {
lua_pushnil(L);
lua_pushstring(L, payload_err);
return 2;
}
dmPush::VerifyPayload(L, payload);
}

// param: notification_settings
Expand Down
12 changes: 4 additions & 8 deletions extension-push/src/push_ios.mm
Expand Up @@ -268,15 +268,11 @@ static int Push_Schedule(lua_State* L)
userdata[@"id"] = [NSNumber numberWithInt:g_Push.m_ScheduledID];
if (top > 3) {
const char* payload = luaL_checkstring(L, 4);
userdata[@"payload"] = [NSString stringWithUTF8String:payload];


// Verify that the payload is valid and can be delivered later on.
char payload_err[128];
if (!dmPush::VerifyPayload(L, payload, payload_err, sizeof(payload_err))) {
lua_pushnil(L);
lua_pushstring(L, payload_err);
return 2;
}
dmPush::VerifyPayload(L, payload);

userdata[@"payload"] = [NSString stringWithUTF8String:payload];
} else {
userdata[@"payload"] = nil;
}
Expand Down
53 changes: 16 additions & 37 deletions extension-push/src/push_utils.cpp
Expand Up @@ -4,27 +4,17 @@

#include "push_utils.h"

bool dmPush::VerifyPayload(lua_State* L, const char* payload, char* error_str_out, size_t error_str_size)
void dmPush::VerifyPayload(lua_State* L, const char* payload)
{
int top = lua_gettop(L);
bool success = false;

dmJson::Document doc;
dmJson::Result r = dmJson::Parse(payload, &doc);
if (r == dmJson::RESULT_OK && doc.m_NodeCount > 0) {
if (dmScript::JsonToLua(L, &doc, 0, error_str_out, error_str_size) >= 0) {
success = true;
// JsonToLua will push Lua values on the stack, but they will not be used
// since we only want to verify that the JSON can be converted to Lua here.
lua_pop(L, lua_gettop(L) - top);
}
} else {
dmLogError("Failed to parse JSON payload string (%d)", r);
}
dmJson::Free(&doc);

dmScript::JsonToLua(L, payload, strlen(payload)); // throws lua error if it fails

// JsonToLua will push Lua values on the stack, but they will not be used
// since we only want to verify that the JSON can be converted to Lua here.
lua_pop(L, lua_gettop(L) - top);

assert(top == lua_gettop(L));
return success;
}

static void PushError(lua_State* L, const char* error)
Expand Down Expand Up @@ -86,26 +76,15 @@ static void HandlePushMessageResult(const dmPush::Command* cmd, bool local)
return;
}

dmJson::Document doc;
dmJson::Result r = dmJson::Parse((const char*) cmd->m_Result, &doc);
if (r == dmJson::RESULT_OK && doc.m_NodeCount > 0) {
char err_str[128];
if (dmScript::JsonToLua(L, &doc, 0, err_str, sizeof(err_str)) < 0) {
dmLogError("Failed converting push result JSON to Lua; %s", err_str);
dmJson::Free(&doc);
return;
}

lua_pushnumber(L, local ? dmPush::ORIGIN_LOCAL : dmPush::ORIGIN_REMOTE);
lua_pushboolean(L, cmd->m_WasActivated);

int ret = dmScript::PCall(L, 4, 0);
(void)ret;
} else {
lua_pop(L, 2);
dmLogError("Failed to parse push response (%d)", r);
}
dmJson::Free(&doc);
const char* json = (const char*) cmd->m_Result;

dmScript::JsonToLua(L, json, strlen(json)); // throws lua error if it fails

lua_pushnumber(L, local ? dmPush::ORIGIN_LOCAL : dmPush::ORIGIN_REMOTE);
lua_pushboolean(L, cmd->m_WasActivated);

int ret = dmScript::PCall(L, 4, 0);
(void)ret;

dmScript::TeardownCallback(cmd->m_Callback);
}
Expand Down
2 changes: 1 addition & 1 deletion extension-push/src/push_utils.h
Expand Up @@ -49,7 +49,7 @@ namespace dmPush

void HandleCommand(dmPush::Command* push, void* ctx);

bool VerifyPayload(lua_State* L, const char* payload, char* error_str_out, size_t error_str_size);
void VerifyPayload(lua_State* L, const char* payload);
}

#endif
Expand Down

0 comments on commit e6c621f

Please sign in to comment.