Language:
Lua     Change language:
Pastebin: 92254
Author: snogglethorpe
Subject: package preloading code
Created: 2008-07-23 00:50:12
Download and save
Toggle line numbers
1 
2^L 
3// Module "pre-loading" 
4 
5extern "C" int luaopen_lpeg (lua_State *L); 
6extern "C" int luaopen_snograw (lua_State *L); 
7 
8struct preload_module 
9{ 
10  const char *name; 
11  lua_CFunction loader; 
12}
13 
14// A list of modules which are statically linked into our executable and 
15// should be preloaded (which allows Lua's require mechanism to find 
16// them). 
17// 
18static preload_module preloaded_modules[] = { 
19  { "snograw", luaopen_snograw }, 
20  { "lpeg", luaopen_lpeg }, 
21  { 0, 0 } 
22}; 
23 
24^L 
25// Lua initialization 
26 
27// Setup Lua environment, and initialze the global variable L. 
28// 
29static void 
30setup_lua () 
31{ 
32  // Do one-time setup of lua environment. 
33 
34  L = lua_open(); 
35 
36  luaL_openlibs (L);        // load standard libraries 
37 
38  // register preloaded modules 
39  // 
40  lua_getfield (L, LUA_GLOBALSINDEX, "package"); 
41  lua_getfield (L, -1, "preload"); 
42  for (preload_module *pm = preloaded_modules; pm->name; pm++) 
43    { 
44      lua_pushcfunction (L, pm->loader); 
45      lua_setfield (L, -2, pm->name); 
46    } 
47  lua_pop (L, 1);       // pop package.preload table 
48 
49  // require ("snogray") 
50  // 
51  lua_getfield (L, LUA_GLOBALSINDEX, "require"); // function 
52  lua_pushstring (L, "snogray");         // arg 0 
53  do_call (L, 1, 0); 
54} 
55 
Download and save
Toggle line numbers
Thread:
[92254] package preloading code by snogglethorpe at 2008-07-23 00:50:12
Tip: Click the line numbers to toggle highliting on that line.

Paste followup:

Language:
Author:
Subject:


    Tabstop:     bigger biggest
Note: You can prefix a line with "@@@" to highlight it.