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