1010#include " parse/parselo.h"
1111#include " parse/sexp.h"
1212#include " parse/sexp/sexp_lookup.h"
13+ #include " scripting/api/objs/enums.h"
1314#include " scripting/api/objs/hudgauge.h"
1415#include " scripting/api/objs/message.h"
1516#include " scripting/api/objs/model.h"
@@ -32,7 +33,8 @@ using namespace luacpp;
3233
3334namespace sexp {
3435
35- static SCP_unordered_map<SCP_string, int > parameter_type_mapping{{ " boolean" , OPF_BOOL },
36+ static SCP_unordered_map<SCP_string, int > parameter_type_mapping {
37+ { " boolean" , OPF_BOOL },
3638 { " number" , OPF_NUMBER },
3739 { " ship" , OPF_SHIP },
3840 { " shipname" , OPF_SHIP },
@@ -337,6 +339,34 @@ luacpp::LuaValue LuaSEXP::sexpToLua(int node, int argnum, int parent_node) const
337339 }
338340 }
339341}
342+ bool LuaSEXP::maybeExtractSexpSpecialRetVal (const luacpp::LuaValue& value, int & sexp_retval) {
343+ // see if this return value is actually a SEXP_ enum
344+ if (value.getValueType () == ValueType::USERDATA) {
345+ try {
346+ using namespace scripting ::api;
347+ enum_h enumeration;
348+ value.getValue (l_Enum.Get (&enumeration)); // this might throw a LuaException
349+ switch (enumeration.index ) {
350+ case LE_SEXP_TRUE:
351+ case LE_SEXP_FALSE:
352+ case LE_SEXP_KNOWN_FALSE:
353+ case LE_SEXP_KNOWN_TRUE:
354+ case LE_SEXP_UNKNOWN:
355+ case LE_SEXP_NAN:
356+ case LE_SEXP_NAN_FOREVER:
357+ case LE_SEXP_CANT_EVAL:
358+ sexp_retval = *enumeration.value ;
359+ return true ;
360+ default :
361+ break ;
362+ }
363+ } catch (const LuaException& le) {
364+ // just ignore the exception because we follow a different code path if the conversion failed
365+ SCP_UNUSED (le);
366+ }
367+ }
368+ return false ;
369+ }
340370int LuaSEXP::getSexpReturnValue (const LuaValueList& retVals) const {
341371 switch (_return_type) {
342372 case OPR_NUMBER:
@@ -347,6 +377,10 @@ int LuaSEXP::getSexpReturnValue(const LuaValueList& retVals) const {
347377 retVals.size ());
348378 return 0 ;
349379 } else if (retVals[0 ].getValueType () != ValueType::NUMBER) {
380+ int sexp_retval;
381+ if (maybeExtractSexpSpecialRetVal (retVals[0 ], sexp_retval)) {
382+ return sexp_retval;
383+ }
350384 Warning (LOCATION, " Wrong return type detected for Lua SEXP '%s', expected a number." , _name.c_str ());
351385 return 0 ;
352386 } else {
@@ -360,6 +394,10 @@ int LuaSEXP::getSexpReturnValue(const LuaValueList& retVals) const {
360394 retVals.size ());
361395 return SEXP_FALSE;
362396 } else if (retVals[0 ].getValueType () != ValueType::BOOLEAN) {
397+ int sexp_retval;
398+ if (maybeExtractSexpSpecialRetVal (retVals[0 ], sexp_retval)) {
399+ return sexp_retval;
400+ }
363401 Warning (LOCATION, " Wrong return type detected for Lua SEXP '%s', expected a boolean." , _name.c_str ());
364402 return SEXP_FALSE;
365403 } else {
0 commit comments