| 1935 | } |
| 1936 | |
| 1937 | bool SetupCallback(LuaCallbackInfo* cbk) |
| 1938 | { |
| 1939 | lua_State* L = cbk->m_L; |
| 1940 | int top = lua_gettop(L); |
| 1941 | if(cbk->m_CallbackInfoRef == LUA_NOREF) |
| 1942 | { |
| 1943 | dmLogWarning("Failed to invoke callback (it was not registered)"); |
| 1944 | assert(top == lua_gettop(L)); |
| 1945 | return false; |
| 1946 | } |
| 1947 | |
| 1948 | GetInstance(L); |
| 1949 | // [-1] old instance |
| 1950 | |
| 1951 | lua_rawgeti(L, LUA_REGISTRYINDEX, cbk->m_ContextTableRef); |
| 1952 | // [-2] old instance |
| 1953 | // [-1] context table |
| 1954 | |
| 1955 | if (lua_type(L, -1) != LUA_TTABLE) |
| 1956 | { |
| 1957 | lua_pop(L, 2); |
| 1958 | assert(top == lua_gettop(L)); |
| 1959 | return false; |
| 1960 | } |
| 1961 | |
| 1962 | const int context_table_stack_index = lua_gettop(L); |
| 1963 | |
| 1964 | lua_rawgeti(L, context_table_stack_index, cbk->m_Callback); |
| 1965 | // [-3] old instance |
| 1966 | // [-2] context table |
| 1967 | // [-1] callback |
| 1968 | |
| 1969 | if (lua_type(L, -1) != LUA_TFUNCTION) |
| 1970 | { |
| 1971 | lua_pop(L, 3); |
| 1972 | assert(top == lua_gettop(L)); |
| 1973 | return false; |
| 1974 | } |
| 1975 | |
| 1976 | lua_rawgeti(L, context_table_stack_index, cbk->m_Self); // Setup self (the script instance) |
| 1977 | // [-4] old instance |
| 1978 | // [-3] context table |
| 1979 | // [-2] callback |
| 1980 | // [-1] self |
| 1981 | |
| 1982 | if (lua_isnil(L, -1)) |
| 1983 | { |
| 1984 | lua_pop(L, 4); |
| 1985 | assert(top == lua_gettop(L)); |
| 1986 | return false; |
| 1987 | } |
| 1988 | |
| 1989 | lua_pushvalue(L, -1); |
| 1990 | // [-5] old instance |
| 1991 | // [-4] context table |
| 1992 | // [-3] callback |
| 1993 | // [-2] self |
| 1994 | // [-1] self |
no test coverage detected