| 912 | } |
| 913 | |
| 914 | void LuaEngine::setContextRequire(int handleIndex, LuaContext::RequireFunction requireFunction) { |
| 915 | lua_checkstack(m_state, 4); |
| 916 | |
| 917 | pushHandle(m_state, handleIndex); |
| 918 | |
| 919 | auto funcUserdata = (LuaContext::RequireFunction*)lua_newuserdata(m_state, sizeof(LuaContext::RequireFunction)); |
| 920 | new (funcUserdata) LuaContext::RequireFunction(std::move(requireFunction)); |
| 921 | lua_rawgeti(m_state, LUA_REGISTRYINDEX, m_requireFunctionMetatableRegistryId); |
| 922 | lua_setmetatable(m_state, -2); |
| 923 | |
| 924 | lua_pushvalue(m_state, -2); |
| 925 | |
| 926 | auto invokeRequire = [](lua_State* state) { |
| 927 | try { |
| 928 | lua_checkstack(state, 2); |
| 929 | |
| 930 | auto require = (LuaContext::RequireFunction*)lua_touserdata(state, lua_upvalueindex(1)); |
| 931 | auto self = luaEnginePtr(state); |
| 932 | |
| 933 | auto moduleName = self->luaTo<LuaString>(self->popLuaValue(state)); |
| 934 | |
| 935 | lua_pushvalue(state, lua_upvalueindex(2)); |
| 936 | LuaContext context(LuaDetail::LuaHandle(RefPtr<LuaEngine>(self), self->popHandle(state))); |
| 937 | |
| 938 | (*require)(context, moduleName); |
| 939 | return 0; |
| 940 | } catch (LuaInstructionLimitReached const&) { |
| 941 | lua_pushlightuserdata(state, &s_luaInstructionLimitExceptionKey); |
| 942 | return lua_error(state); |
| 943 | } catch (LuaRecursionLimitReached const&) { |
| 944 | lua_pushlightuserdata(state, &s_luaRecursionLimitExceptionKey); |
| 945 | return lua_error(state); |
| 946 | } catch (std::exception const& e) { |
| 947 | luaL_where(state, 1); |
| 948 | lua_pushstring(state, printException(e, true).c_str()); |
| 949 | lua_concat(state, 2); |
| 950 | return lua_error(state); |
| 951 | } |
| 952 | }; |
| 953 | |
| 954 | lua_pushcclosure(m_state, invokeRequire, 2); |
| 955 | |
| 956 | LuaDetail::rawSetField(m_state, -2, "require"); |
| 957 | |
| 958 | lua_pop(m_state, 1); |
| 959 | } |
| 960 | |
| 961 | void LuaEngine::contextLoad(int handleIndex, char const* contents, size_t size, char const* name) { |
| 962 | lua_checkstack(m_state, 2); |
no test coverage detected