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" */
| 1706 | * Assumes target function only returns a single non-nil value. |
| 1707 | * Convert and return thrown errors as: nil, "error message" */ |
| 1708 | static 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 */ |
| 1734 | static int lua_cjson_new(lua_State *l) |
nothing calls this directly
no test coverage detected