Skip to content
Browse files

Milestone. works as shebang interpreter

Change-Id: Ica599241a943706533380d370e971d2ade9c1a78
  • Loading branch information...
1 parent 789443e commit 6477d9ac4e395a58d328ea7b96d4649657dda277 @creationix creationix committed Oct 4, 2011
Showing with 31 additions and 12 deletions.
  1. +5 −5 Makefile
  2. +4 −2 examples/http_server.lua
  3. +6 −3 lib/luvit.lua
  4. +16 −2 src/luvit.c
View
10 Makefile
@@ -4,7 +4,7 @@ HTTPDIR=deps/http-parser
BUILDDIR=build
GENDIR=${BUILDDIR}/generated
-all: ${GENDIR} luvit
+all: luvit
${GENDIR}:
mkdir -p ${GENDIR}
@@ -35,15 +35,15 @@ ${BUILDDIR}/%.o: src/%.c
mkdir -p ${BUILDDIR}
$(CC) -Wall -c $< -o $@ -I${HTTPDIR} -I${UVDIR}/include -I${LUADIR}/src
-${BUILDDIR}/luvit: ${BUILDDIR}/luvit.o ${BUILDDIR}/utils.o ${BUILDDIR}/luv.o ${BUILDDIR}/lhttp_parser.o ${LUADIR}/src/libluajit.a ${UVDIR}/uv.a ${HTTPDIR}/http_parser.o ${GENDIR}/http.o ${GENDIR}/tcp.o ${GENDIR}/luvit.o ${GENDIR}/utils.o
- $(CC) -Wall -Wl,-E ${BUILDDIR}/luvit.o ${BUILDDIR}/utils.o ${BUILDDIR}/luv.o ${BUILDDIR}/lhttp_parser.o ${LUADIR}/src/libluajit.a ${UVDIR}/uv.a ${HTTPDIR}/http_parser.o ${GENDIR}/http.o ${GENDIR}/tcp.o ${GENDIR}/luvit.o ${GENDIR}/utils.o -o ${BUILDDIR}/luvit -lm -ldl -lrt
+${BUILDDIR}/luvit: ${GENDIR} ${BUILDDIR}/luvit.o ${BUILDDIR}/utils.o ${BUILDDIR}/luv.o ${BUILDDIR}/lhttp_parser.o ${LUADIR}/src/libluajit.a ${UVDIR}/uv.a ${HTTPDIR}/http_parser.o ${GENDIR}/http.o ${GENDIR}/tcp.o ${GENDIR}/luvit.o ${GENDIR}/utils.o
+ $(CC) -o ${BUILDDIR}/luvit ${BUILDDIR}/luvit.o ${BUILDDIR}/utils.o ${BUILDDIR}/luv.o ${BUILDDIR}/lhttp_parser.o ${LUADIR}/src/libluajit.a ${UVDIR}/uv.a ${HTTPDIR}/http_parser.o ${GENDIR}/http.o ${GENDIR}/tcp.o ${GENDIR}/luvit.o ${GENDIR}/utils.o -Wall -lm -ldl -lrt -Wl,-E
clean:
make -C ${LUADIR} clean
make -C ${HTTPDIR} clean
make -C ${UVDIR} distclean
rm -rf build
-
-
+install: ${BUILDDIR}/luvit
+ install ${BUILDDIR}/luvit -s -v /usr/local/bin/luvit
View
6 examples/http_server.lua 100644 → 100755
@@ -1,14 +1,16 @@
+#!/usr/bin/env luvit
local http = require("http")
http.create_server(function (req, res)
--- p("req", req)
--- p("res", res)
+
res:write_head(200, {
["Content-Type"] = "text/plain",
["Content-Length"] = "11"
})
+
res:write("Hello World")
res:finish()
+
end):listen(8080)
print("Server listening at http://localhost:8080/")
View
9 lib/luvit.lua
@@ -1,15 +1,18 @@
function p(first, ...)
- local dump = require('lib/utils').dump
+ local dump = require('utils').dump
local l = dump(first)
for i, v in ipairs{...} do
l = l .. "\t" .. dump(v)
end
print(l)
end
+if not process.argv[1] then
+ print("usage:\n\t" .. process.argv[0] .. " progname.lua\n")
+ return;
+end
-require('examples/http_server')
---require('examples/tcp_test')
+dofile(process.argv[1])
require('uv').run()
View
18 src/luvit.c
@@ -8,7 +8,7 @@
#include "luv.h"
#include "lhttp_parser.h"
-int main()
+int main(int argc, char *argv[])
{
lua_State *L = lua_open();
@@ -29,12 +29,26 @@ int main()
// We're done with preload, put it away
lua_pop(L, 1);
+ // Populate a couple global things that lua can't do
+ lua_newtable(L); // Create the `process` table
+
+ lua_createtable (L, argc, 0); // Create the `process.argv` table
+ int index;
+ for (index = 0; index < argc; index++) {
+ lua_pushstring (L, argv[index]);
+ lua_rawseti(L, -2, index);
+ }
+ lua_setfield(L, -2, "argv");
+
+ lua_setglobal(L, "process");
+
+
// Run the main lua script
if (luaL_dostring(L, "assert(require('luvit'))")) {
printf("%s\n", lua_tostring(L, -1));
lua_pop(L, 1);
return -1;
- }
+ }
lua_close(L);
return 0;

0 comments on commit 6477d9a

Please sign in to comment.
Something went wrong with that request. Please try again.