| 2811 | } |
| 2812 | |
| 2813 | staticfn boolean |
| 2814 | hook_open(lua_State *L) |
| 2815 | { |
| 2816 | boolean rv = FALSE; |
| 2817 | if (!io_open) { |
| 2818 | int tos = lua_gettop(L); |
| 2819 | lua_pushglobaltable(L); |
| 2820 | if (lua_getfield(L, -1, "io") != LUA_TTABLE) |
| 2821 | goto out; |
| 2822 | lua_getfield(L, -1, "open"); |
| 2823 | /* The only way this can happen is if someone is messing with us, |
| 2824 | * and I'm not sure even that is possible. */ |
| 2825 | if (!lua_iscfunction(L, -1)) |
| 2826 | goto out; |
| 2827 | /* XXX This is fragile: C11 says casting func* to void* |
| 2828 | * doesn't have to work, but POSIX says it does. So it |
| 2829 | * _should_ work everywhere but all we can do without messing |
| 2830 | * around inside Lua is to try to keep the compiler quiet. */ |
| 2831 | io_open = (int (*)(lua_State *)) lua_topointer(L, -1); |
| 2832 | lua_pushcfunction(L, hooked_open); |
| 2833 | lua_setfield(L, -1, "open"); |
| 2834 | rv = TRUE; |
| 2835 | out: |
| 2836 | lua_settop(L, tos); |
| 2837 | } |
| 2838 | return rv; |
| 2839 | } |
| 2840 | #endif |
| 2841 | |
| 2842 | DISABLE_WARNING_CONDEXPR_IS_CONSTANT |