MCPcopy Create free account
hub / github.com/defold/defold / json_protect_conversion

Function json_protect_conversion

engine/script/src/luacjson/lua_cjson.c:1708–1731  ·  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

1706 * Assumes target function only returns a single non-nil value.
1707 * Convert and return thrown errors as: nil, "error message" */
1708static int json_protect_conversion(lua_State *l)
1709{
1710 int err;
1711
1712 /* Deliberately throw an error for invalid arguments */
1713 luaL_argcheck(l, lua_gettop(l) == 1, 1, "expected 1 argument");
1714
1715 /* pcall() the function stored as upvalue(1) */
1716 lua_pushvalue(l, lua_upvalueindex(1));
1717 lua_insert(l, 1);
1718 err = lua_pcall(l, 1, 1, 0);
1719 if (!err)
1720 return 1;
1721
1722 if (err == LUA_ERRRUN) {
1723 lua_pushnil(l);
1724 lua_insert(l, -2);
1725 return 2;
1726 }
1727
1728 /* Since we are not using a custom error handler, the only remaining
1729 * errors are memory related */
1730 return luaL_error(l, "Memory allocation error in CJSON protected call");
1731}
1732
1733/* Return cjson module table */
1734static int lua_cjson_new(lua_State *l)

Callers

nothing calls this directly

Calls 6

lua_gettopFunction · 0.85
lua_pushvalueFunction · 0.85
lua_insertFunction · 0.85
lua_pcallFunction · 0.85
lua_pushnilFunction · 0.85
luaL_errorFunction · 0.85

Tested by

no test coverage detected