Skip to content

Instantly share code, notes, and snippets.

@jakerr
Created May 13, 2012 12:22
Show Gist options
  • Save jakerr/2688170 to your computer and use it in GitHub Desktop.
Save jakerr/2688170 to your computer and use it in GitHub Desktop.

I'd like to set an upvalue for a lua block from the c-api.

The idea is that I will load a lua file via c, and set an upvalue so the lua block that's executed knows it was loaded from the c-layer. Any subsequent loads that that block makes, should not see that same upvalue, so that code knows it was loaded indirectly from another lua script.

status = luaL_loadfile ( state, "2_upvalfromc.lua" )
// Can I set an upvalue for the block compiled by luaL_loadfile, such that I can check the value in the block?
lua_call ( state, 0, 0 );
if (up_val_from_c) {
-- I want to define this upvalue in c
print ("upvalue came from c: " .. up_val_from_c)
else {
print ("OOPS: up_val_from_c not defined")
}
dofile('3_not_from_c.lua')
if (up_val_from_c) {
-- Unexpected. I don't want this upvalue to be defined.
print ("OOPS: upvalue came from c: " .. up_val_from_c)
} else {
-- I expect this to not have the upvalue defined and print this statement.
print ("Yes. This was loaded from within lua, not c")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment