Skip to content

Instantly share code, notes, and snippets.

@postwait
Created February 28, 2017 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save postwait/0a0d9dd6c1d0fd07530489f240ca9ea2 to your computer and use it in GitHub Desktop.
Save postwait/0a0d9dd6c1d0fd07530489f240ca9ea2 to your computer and use it in GitHub Desktop.
bool
BindingInstance::construct()
{
ink_release_assert(this->lua == nullptr);
if ((this->lua = luaL_newstate())) {
luaL_openlibs(this->lua);
// 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: https://www.circonus.com/2016/07/luajit-illumos-vm/
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