lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


How do I set a global when using strict.lua and Lua 5.1.1?  The
following demonstrates what I'm trying to do.  The following emits

PANIC: unprotected error in call to Lua API
(/usr/local/share/lua/5.1/strict.lua:19: attempt to index a nil value)

when main.lua contains "require('strict')" and works when it doesn't.
I suppose I need a way to be able to declare, 'global_from_c' as a
local in the file level closure from C but I don't know how to do
that.  Any ideas?


// bootstrap.cpp
#include <lua.hpp>

int main()
{
  lua_State* L = luaL_newstate();
  luaL_openlibs(L);

  if (luaL_loadfile(L, "main.lua") || lua_pcall(L, 0, 0, 0))
  {
     luaL_error(L, "Cannot load script `main.lua': %s", lua_tostring(L, -1));
  }

  lua_pushnumber(L, 10);
  lua_setglobal(L, "global_from_c");

  // eventually, do lua_call() on print_global()

  lua_close(L);
  return 0;
}


-- main.lua
require('strict')

function print_global()
  print(global_from_c)
end


# GNUmakefile
prog: bootstrap.o ; g++ -o $@ $< -llua

.PHONY: clean
clean: ; rm -f *.o prog