Skip to content

Commit 34bd797

Browse files
committed
fix(lua): std::any assignment
1 parent b1b240d commit 34bd797

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed

src/plugins/Plugin.cpp

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "../extensions/ExtensionManager.h"
55
#include "../server/commands/CommandsManager.h"
66

7-
LUALIB_API int luaopen_cmsgpack(lua_State *L);
7+
LUALIB_API int luaopen_cmsgpack(lua_State* L);
88
extern "C"
99
{
1010
LUALIB_API int luaopen_rapidjson(lua_State* L);
@@ -66,7 +66,7 @@ EventResult Plugin::TriggerEvent(std::string invokedBy, std::string eventName, s
6666
PERF_RECORD(string_format("event:%s:%s", invokedBy.c_str(), eventName.c_str()), this->GetName());
6767

6868
EValue payload(ctx, eventPayload);
69-
if(ctx->GetKind() == ContextKinds::JavaScript)
69+
if (ctx->GetKind() == ContextKinds::JavaScript)
7070
payload = EValue(ctx, JS_NewUint8ArrayCopy((JSContext*)ctx->GetState(), (uint8_t*)(eventPayload.data()), eventPayload.size()));
7171

7272
int res = (int)EventResult::Continue;
@@ -86,7 +86,7 @@ EventResult Plugin::TriggerEvent(std::string invokedBy, std::string eventName, s
8686
{
8787
PRINTF("An error has occured while executing event '%s':\nError: %s\n", eventName.c_str(), e.what());
8888

89-
if(eventName == "OnPluginStart") this->SetLoadError(e.what());
89+
if (eventName == "OnPluginStart") this->SetLoadError(e.what());
9090
}
9191

9292
return (EventResult)res;
@@ -159,8 +159,8 @@ void Plugin::ExecuteCommand(void* functionPtr, std::string name, int slot, std::
159159
{
160160
PERF_RECORD(string_format("command:%s", name.c_str()), this->GetName())
161161

162-
if (functionPtr == nullptr)
163-
return;
162+
if (functionPtr == nullptr)
163+
return;
164164

165165
EValue* commandRef = (EValue*)functionPtr;
166166

@@ -183,17 +183,17 @@ void Plugin::ExecuteCommand(void* functionPtr, std::string name, int slot, std::
183183
bool Plugin::LoadScriptingEnvironment()
184184
{
185185
this->SetLoadError("");
186-
186+
187187
ctx = new EContext(GetKind() == PluginKind_t::Lua ? ContextKinds::Lua : ContextKinds::JavaScript);
188188

189-
if(ctx->GetKind() == ContextKinds::Lua) {
189+
if (ctx->GetKind() == ContextKinds::Lua) {
190190
ctx->RegisterLuaLib("msgpack", luaopen_cmsgpack);
191191
ctx->RegisterLuaLib("json", luaopen_rapidjson);
192192
}
193193

194194
SetupScriptingEnvironment(this, ctx);
195195

196-
if(GetKind() == PluginKind_t::JavaScript) {
196+
if (GetKind() == PluginKind_t::JavaScript) {
197197
for (Extension* ext : extManager->GetExtensionsList())
198198
if (ext->IsLoaded()) {
199199
std::string error = "";
@@ -239,7 +239,8 @@ bool Plugin::LoadScriptingEnvironment()
239239
this->SetLoadError(error);
240240
return false;
241241
}
242-
} catch(EException& e) {
242+
}
243+
catch (EException& e) {
243244
std::string error = e.what();
244245
PRINTF("Failed to load plugin file '%s'.\n", file.c_str());
245246
PRINTF("Error: %s\n", error.c_str());
@@ -269,7 +270,8 @@ bool Plugin::LoadScriptingEnvironment()
269270
this->SetLoadError(error);
270271
return false;
271272
}
272-
} catch(EException& e) {
273+
}
274+
catch (EException& e) {
273275
std::string error = e.what();
274276
PRINTF("Failed to load plugin file '%s'.\n", file.c_str());
275277
PRINTF("Error: %s\n", error.c_str());
@@ -338,7 +340,7 @@ bool Plugin::ExecuteStart()
338340
TriggerEvent("core", "OnPluginStart", encoders::msgpack::SerializeToString({}), event);
339341
delete event;
340342

341-
if(GetLoadError() != "") return false;
343+
if (GetLoadError() != "") return false;
342344

343345
return true;
344346
}
@@ -388,7 +390,7 @@ EValue SerializeData(std::any data, EContext* state)
388390
if (starts_with(val, "JSON⇚") && ends_with(val, "")) {
389391
std::string json = explode(explode(val, "")[1], "")[0];
390392

391-
if(state->GetKind() == ContextKinds::Lua) {
393+
if (state->GetKind() == ContextKinds::Lua) {
392394
EValue rapidJsonTable = EValue::getGlobal(state, "json");
393395
if (!rapidJsonTable["decode"].isFunction())
394396
return EValue(state, emptyTable);
@@ -408,9 +410,11 @@ EValue SerializeData(std::any data, EContext* state)
408410
return EValue(state, emptyTable);
409411

410412
return decodedResult;
411-
} else if(state->GetKind() == ContextKinds::JavaScript) {
413+
}
414+
else if (state->GetKind() == ContextKinds::JavaScript) {
412415
return EValue(state, JS_ParseJSON((JSContext*)state->GetState(), json.c_str(), json.length(), "SerializeData"));
413-
} else return EValue(state);
416+
}
417+
else return EValue(state, nullptr);
414418
}
415419
else return EValue(state, val);
416420
}
@@ -454,29 +458,30 @@ EValue SerializeData(std::any data, EContext* state)
454458
return EValue(state, nullptr);
455459
else if (value.type() == typeid(std::vector<std::string>))
456460
{
457-
if(state->GetKind() == ContextKinds::Lua) {
461+
if (state->GetKind() == ContextKinds::Lua) {
458462
std::vector<std::string> tmpval = std::any_cast<std::vector<std::string>>(value);
459463
std::string tbl = tmpval[0];
460-
464+
461465
EValue load = EValue::getGlobal(state, "load");
462466
try
463467
{
464468
EValue loadReturnValue = load(tbl);
465469
if (!loadReturnValue.isFunction())
466470
return EValue(state, emptyTable);
467-
471+
468472
EValue loadFuncRetVal = loadReturnValue();
469473
if (!loadFuncRetVal.isTable())
470474
return EValue(state, emptyTable);
471-
475+
472476
return loadFuncRetVal;
473477
}
474478
catch (EException& e)
475479
{
476480
PRINTF("Exception: %s\n", e.what());
477481
return EValue(state, emptyTable);
478482
}
479-
} else {
483+
}
484+
else {
480485
PRINT("Cannot convert Lua table to JS object.\n");
481486
return EValue(state);
482487
}
@@ -512,18 +517,20 @@ std::any DeserializeData(EValue ref, EContext* state)
512517
return ref.cast<std::string>();
513518
else if (ref.isTable())
514519
{
515-
if(state->GetKind() == ContextKinds::Lua) {
520+
if (state->GetKind() == ContextKinds::Lua) {
516521
EValue serpentDump = EValue::getGlobal(state, "serpent")["dump"];
517522
EValue serpentDumpReturnValue = serpentDump(ref);
518523

519524
std::vector<std::string> tmptbl;
520525
tmptbl.push_back(serpentDumpReturnValue.cast<std::string>());
521526

522527
return tmptbl;
523-
} else if(state->GetKind() == ContextKinds::JavaScript) {
528+
}
529+
else if (state->GetKind() == ContextKinds::JavaScript) {
524530
std::vector<std::string> tmptbl;
525531
return tmptbl;
526-
} else return nullptr;
532+
}
533+
else return nullptr;
527534
}
528535
else if (ref.isInstance<Color>())
529536
return ref.cast<Color>();

src/plugins/Plugin.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct Stack<std::any>
3333

3434
static std::any getLua(EContext* ctx, int ref)
3535
{
36-
return DeserializeData(EValue(ctx, ref), ctx);
36+
return DeserializeData(EValue(ctx, ref, false), ctx);
3737
}
3838

3939
static std::any getJS(EContext* ctx, JSValue value)

vendor/embedder

0 commit comments

Comments
 (0)