Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
ink_release_assert(this->lua == nullptr);
if ((this->lua = luaL_newstate())) {
// Push a pointer to ourself into the well-known registry key.
// We do not use lightuserdata here because BindingInstance variables
// are often declared on stack which would make "this" a stack variable.
// While this might seem fine and actually work on many platforms, those
// 64bit platforms with split VA space where heap and stack may live in
// a separate 47bit VA will violate internal assumptions that luajit
// places on lightuserdata. Plain userdata will provide luajit-happy
// address in which we have the full 64bits to store our pointer to this.
// see:
BindingInstance **lua_surrogate;
lua_surrogate = (BindingInstance **)lua_newuserdata(this->lua, sizeof(BindingInstance *));
*lua_surrogate = this;
lua_setfield(this->lua, LUA_REGISTRYINDEX, selfkey);
ink_release_assert(BindingInstance::self(this->lua) == this);
return this->lua;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment