lua.c
changeset 32 6dc2d52e4b13
parent 22 822094314703
equal deleted inserted replaced
31:9c66c159a3a6 32:6dc2d52e4b13
     1 /* vim: set noet nosta sw=4 ts=4 ft=c : */
     1 /* vim: set noet nosta sw=4 ts=4 ft=c : */
     2 /*
     2 /*
     3 Copyright (c) 2011-2012, Mahlon E. Smith <mahlon@martini.nu>
     3 Copyright (c) 2011-2015, Mahlon E. Smith <mahlon@martini.nu>
     4 All rights reserved.
     4 All rights reserved.
     5 Redistribution and use in source and binary forms, with or without
     5 Redistribution and use in source and binary forms, with or without
     6 modification, are permitted provided that the following conditions are met:
     6 modification, are permitted provided that the following conditions are met:
     7 
     7 
     8     * Redistributions of source code must retain the above copyright
     8     * Redistributions of source code must retain the above copyright
    77 {
    77 {
    78 	lua_State *lua = luaL_newstate();
    78 	lua_State *lua = luaL_newstate();
    79 	luaL_openlibs( lua ); /* include lua standard libraries */
    79 	luaL_openlibs( lua ); /* include lua standard libraries */
    80 
    80 
    81 	/* Predeclare the request table. */
    81 	/* Predeclare the request table. */
    82 	lua_pushstring( lua, "request" );
       
    83 	lua_createtable( lua, 0, 7 );
    82 	lua_createtable( lua, 0, 7 );
    84 	lua_settable( lua, LUA_GLOBALSINDEX );
    83 	lua_setglobal( lua, "request" );
    85 
    84 
    86 	/* Predeclare a table for shared data */
    85 	/* Predeclare a table for shared data */
    87 	lua_pushstring( lua, "shared" );
       
    88 	lua_newtable( lua );
    86 	lua_newtable( lua );
    89 	lua_settable( lua, LUA_GLOBALSINDEX );
    87 	lua_setglobal( lua, "shared" );
    90 
    88 
    91 	/* replace the lua print() function with one that calls debug() instead */
    89 	/* replace the lua print() function with one that calls debug() instead */
    92 	lua_register( lua, "print", luaV_print );
    90 	lua_register( lua, "print", luaV_print );
    93 
    91 
    94 	/* Restrict additional globals. */
    92 	/* Restrict additional globals. */
    95     lua_createtable( lua, 0, 1);
    93 	lua_createtable( lua, 0, 1);
    96     lua_pushcfunction( lua, luaV_globalindex );
    94 	lua_pushcfunction( lua, luaV_globalindex );
    97     lua_setfield( lua, -2, "__newindex");
    95 	lua_setfield( lua, -2, "__newindex");
    98     lua_pushboolean( lua, 0 );
    96 	lua_pushboolean( lua, 0 );
    99     lua_setfield( lua, -2, "__metatable");
    97 	lua_setfield( lua, -2, "__metatable");
   100     lua_setmetatable( lua, LUA_GLOBALSINDEX );
    98 	lua_setmetatable( lua, -2 );
   101 
    99 
   102 	lua_settop( lua, 0 );  /* wipe the stack */
   100 	lua_settop( lua, 0 );  /* wipe the stack */
   103 	return( lua );
   101 	return( lua );
   104 }
   102 }
   105 
   103 
   109  *
   107  *
   110  */
   108  */
   111 void
   109 void
   112 luaV_setup_request( parsed *request )
   110 luaV_setup_request( parsed *request )
   113 {
   111 {
   114 	lua_getfield( v.lua, LUA_GLOBALSINDEX, "request" );
   112 	lua_getglobal( v.lua, "request" );
   115 	lua_pushstring( v.lua, request->scheme );
   113 	lua_pushstring( v.lua, request->scheme );
   116 	lua_setfield( v.lua, 1, "scheme" );
   114 	lua_setfield( v.lua, 1, "scheme" );
   117 	lua_pushstring( v.lua, request->host );
   115 	lua_pushstring( v.lua, request->host );
   118 	lua_setfield( v.lua, 1, "host" );
   116 	lua_setfield( v.lua, 1, "host" );
   119 	lua_pushstring( v.lua, request->path );
   117 	lua_pushstring( v.lua, request->path );