added boot.lua to drive the execution of external scripts

This commit is contained in:
Andre Leiradella 2015-05-28 16:54:49 -03:00
parent 456c674178
commit c1a895f4fa
3 changed files with 37 additions and 17 deletions

View File

@ -9,5 +9,10 @@ all: luai.exe
luai.exe: main.o image.o path.o realpath.o
gcc $(LFLAGS) -o $@ $+ -llua
main.o: main.c boot_lua.h
boot_lua.h: boot.lua
xxd -i $< | sed "s@unsigned@@" > $@
clean:
rm -f luai.exe main.o image.o path.o realpath.o
rm -f luai.exe main.o image.o path.o realpath.o boot_lua.h

20
etc/luai/boot.lua Normal file
View File

@ -0,0 +1,20 @@
local path = require 'path'
return function( args )
-- Check for script file name
if not args[ 0 ] then
error( 'Lua script missing' )
end
-- Resolve full path to the Lua script
args[ 0 ] = path.realpath( args[ 0 ] )
-- Add the Lua script folder to the Lua path
--local dir = path.split( args[ 0 ] )
--package.path = package.path .. ';' .. dir .. '/?.lua'
-- Run the script
local chunk = loadfile( args[ 0 ] )
local main = chunk()
return main( args )
end

View File

@ -7,6 +7,8 @@
#include "image.h"
#include "path.h"
#include "boot_lua.h"
char __cdecl *realpath( const char *__restrict__ name, char *__restrict__ resolved );
static int luamain( lua_State* L )
@ -15,35 +17,28 @@ static int luamain( lua_State* L )
const char** argv = (const char**)lua_touserdata( L, lua_upvalueindex( 2 ) );
int i;
if ( argc < 2 )
/* Load boot.lua */
if ( luaL_loadbufferx( L, boot_lua, boot_lua_len, "boot.lua", "t" ) != LUA_OK )
{
return luaL_error( L, "Lua file missing\n" );
}
if ( luaL_loadfile( L, argv[ 1 ] ) != LUA_OK )
{
return luaL_error( L, "%s", lua_tostring( L, -1 ) );
return lua_error( L );
}
/* Run it, it returns the bootstrap main function */
lua_call( L, 0, 1 );
/* Prepare the args table */
lua_newtable( L );
{
char buffer[ _MAX_PATH ];
char* resolved = realpath( argv[ 1 ], buffer );
lua_pushstring( L, resolved ? resolved : argv[ 1 ] );
lua_rawseti( L, -2, 0 );
}
for ( i = 2; i < argc; i++ )
for ( i = 1; i < argc; i++ )
{
lua_pushstring( L, argv[ i ] );
lua_rawseti( L, -2, i - 1 );
}
/* Call the bootstrap main function with the args table */
lua_call( L, 1, 1 );
/* Returns the integer result */
return (int)lua_tointeger( L, -1 );
}