Ejemplo n.º 1
0
void mg_exec_lua_script(struct mg_connection *conn, const char *path,
                        const void **exports) {
  int i;
  lua_State *L;

  if (path != NULL && (L = luaL_newstate()) != NULL) {
    prepare_lua_environment(conn, L);
    lua_pushcclosure(L, &lua_error_handler, 0);

    lua_pushglobaltable(L);
    if (exports != NULL) {
      for (i = 0; exports[i] != NULL && exports[i + 1] != NULL; i += 2) {
        lua_pushstring(L, exports[i]);
        lua_pushcclosure(L, (lua_CFunction) exports[i + 1], 0);
        lua_rawset(L, -3);
      }
    }

    if (luaL_loadfile(L, path) != 0) {
      lua_error_handler(L);
    }
    lua_pcall(L, 0, 0, -2);
    lua_close(L);
  }
}
Ejemplo n.º 2
0
int main(void) {
	char buff[256];
	int error;
	lua_State *L = luaL_newstate(); /* opens Lua */
	luaL_openlibs(L); /* Load Lua libraries */
	printf("This is the address of lua state: 0x%p\n", L);
	printf("This is the address of luaL_newstate: 0x%p\n", luaL_newstate);
	printf("This is the address of luaL_loadstring: 0x%p\n", luaL_loadstring);
	printf("This is the address of luaL_loadfilex: 0x%p\n", luaL_loadfilex);
	printf("This is the address of luaL_loadbufferx: 0x%p\n", luaL_loadbufferx);
	printf("This is the address of lua_pcallk: 0x%p\n", lua_pcallk);

	FILE * fstate = fopen("luastate", "w");
	fprintf(fstate, "%i", (int)L);
	fclose(fstate);

	error = luaL_dostring(L, "print(\"[Exe] This is a dostring print\")");
	if(error) {
		lua_error_handler(L,"%s", lua_tostring(L, -1));
	}

	// register functions in exe
	register_internal_functions(L);

	#ifdef DLL_LINKED
	// register functions in dll
	luaopen_libloadable(L);
	error = luaL_dofile(L, "use_loadable.lua");
	if(error) {
		fprintf(stderr, "%s", lua_tostring(L, -1));
		lua_pop(L, 1); /* pop error message from the stack */
	}
	#endif

	error = luaL_dofile(L, "use_internal.lua");
	if(error) {
		fprintf(stderr, "%s", lua_tostring(L, -1));
		lua_pop(L, 1); /* pop error message from the stack */
	}

	//This function should be executed by the DLL, this is only for testing
	//error = luaL_dofile(L, "dump_global_state.lua");

	while (fgets(buff, sizeof(buff), stdin) != NULL) {
		error = luaL_loadbuffer(L, buff, strlen(buff),
				"line") || lua_pcall(L, 0, 0, 0);
		if (error) {
			fprintf(stderr, "%s", lua_tostring(L, -1));
			lua_pop(L, 1); /* pop error message from the stack */
		}
	}

	lua_close(L);
	return 0;
}