MCPcopy Create free account
hub / github.com/9chu/LuaSTGPlus / json_protect_conversion

Function json_protect_conversion

LuaSTGPlus/LuaExtensions/cjson4lua/lua_cjson.c:1321–1344  ·  view source on GitHub ↗

Call target function in protected mode with all supplied args. * Assumes target function only returns a single non-nil value. * Convert and return thrown errors as: nil, "error message" */

Source from the content-addressed store, hash-verified

1319 * Assumes target function only returns a single non-nil value.
1320 * Convert and return thrown errors as: nil, "error message" */
1321static int json_protect_conversion(lua_State *l)
1322{
1323 int err;
1324
1325 /* Deliberately throw an error for invalid arguments */
1326 luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument");
1327
1328 /* pcall() the function stored as upvalue(1) */
1329 lua_pushvalue(l, lua_upvalueindex(1));
1330 lua_insert(l, 1);
1331 err = lua_pcall(l, 1, 1, 0);
1332 if (!err)
1333 return 1;
1334
1335 if (err == LUA_ERRRUN) {
1336 lua_pushnil(l);
1337 lua_insert(l, -2);
1338 return 2;
1339 }
1340
1341 /* Since we are not using a custom error handler, the only remaining
1342 * errors are memory related */
1343 return luaL_error(l, "Memory allocation error in CJSON protected call");
1344}
1345
1346/* Return cjson module table */
1347static int lua_cjson_new(lua_State *l)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected