More stub code

This commit is contained in:
Łukasz Magiera 2016-01-04 18:20:40 +01:00
parent 1d1cab98e8
commit 29cbf09403
13 changed files with 127 additions and 23 deletions

View File

@ -20,7 +20,7 @@ OBJECTS := $(patsubst $(SOURCE)%.c, $(BUILD)%.c.o, $(CFILES))
#Rules
#Prepare
$(BUILDDIRECTORIES):
mkdir $@
mkdir -p $@
#Clean

View File

@ -1,4 +1,9 @@
#ifndef LUARES_H
#define LUARES_H
extern char lua_boot[];
extern char lua_component[];
extern char lua_computer[];
extern char lua_init[];
extern char lua_sandbox[];
extern char lua_util_random[];
#endif

View File

@ -0,0 +1,9 @@
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#ifndef LUPI_H
#define LUPI_H
void run_init();
void setup_modules(lua_State *L);
#endif

View File

@ -4,12 +4,38 @@
# $3 file to generate OUTPUTH H
# $4 prefix
LUAFILES="$1/*"
generate() {
LUAFILES="$1/*"
OUTPUTH="$2"
OUTPUTC="$3"
PREFIX="$4"
outname="$(basename "$OUTPUTH")"
outname="${outname%.*}"
guard=$(echo "$outname" | tr '[:lower:]' '[:upper:]')
guard="$guard""_H"
for file in $LUAFILES
do
filename="$(basename "$file")"
if [ -d "$file" ]
then
generate $file $2 $3 ${PREFIX}${filename}_
else
filename="${filename%.*}"
echo "extern char $PREFIX$filename[];" >> "$OUTPUTH"
echo "char $PREFIX$filename[] = {" >> "$OUTPUTC"
echo " " $(xxd -i < "$file") ",0x00" >> "$OUTPUTC"
echo "};" >> "$OUTPUTC"
fi
done
}
OUTPUTH="$2"
OUTPUTC="$3"
PREFIX="$4"
outname="$(basename "$OUTPUTH")"
outname="${outname%.*}"
@ -19,22 +45,8 @@ guard="$guard""_H"
printf "#ifndef %s\n" "$guard" >> "$OUTPUTH"
printf "#define %s\n" "$guard" >> "$OUTPUTH"
for file in $LUAFILES
do
filename="$(basename "$file")"
filename="${filename%.*}"
echo "extern char $PREFIX$filename[];" >> "$OUTPUTH"
echo "char $PREFIX$filename[] = {" >> "$OUTPUTC"
echo " " $(xxd -i < "$file") ",0x00" >> "$OUTPUTC"
echo "};" >> "$OUTPUTC"
done
generate "$1" "$2" "$3" "$4"
echo "#endif" >> "$OUTPUTH"
exit 0

View File

@ -2,7 +2,7 @@
#include "lupi.h"
int main (void) {
puts("LuPI 2\n");
puts("LuPI L0 INIT");
run_init();
return 0;
}

22
src/c/modules.c Normal file
View File

@ -0,0 +1,22 @@
#include "luares.h"
#include "lupi.h"
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <stdlib.h>
//TODO: move to utils
#define pushtuple(state, name, value) lua_pushstring((state), (name)); lua_pushstring((state), (value)); lua_settable((state), -3)
void setup_modules(lua_State *L) {
lua_createtable (L, 0, 1);
pushtuple(L, "boot", lua_boot);
pushtuple(L, "component", lua_component);
pushtuple(L, "computer", lua_computer);
pushtuple(L, "sandbox", lua_sandbox);
pushtuple(L, "random", lua_util_random);
lua_setglobal(L, "moduleCode");
}

View File

@ -3,11 +3,13 @@
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <stdlib.h>
void run_init() {
lua_State *L;
L = luaL_newstate();
luaL_openlibs(L);
setup_modules(L);
int status = luaL_loadstring(L, lua_init);
if (status) {
fprintf(stderr, "Couldn't load init: %s\n", lua_tostring(L, -1));

9
src/lua/core/boot.lua Normal file
View File

@ -0,0 +1,9 @@
local boot = {}
function boot.boot()
print("LuPI L2 INIT")
print("FIXME: boot stub")
error("Unable to boot")
end
return boot

View File

@ -0,0 +1,9 @@
local component = {}
local api = {}
component.api = api
function component.prepare()
print("Assembling initial component tree")
end
return component

View File

@ -0,0 +1 @@

View File

@ -1 +1,24 @@
print("hello")
print("LuPI L1 INIT")
modules = {}
local function loadModule(name)
print("LuPI L1 INIT > Load module > " .. name)
--TODO: PRERELEASE: Module sandboxing, preferably secure-ish
--TODO: ASAP: Handle load errors
if not moduleCode[name] then
error("No code for module " .. tostring(name))
end
modules[name] = load(moduleCode[name])()
end
--Load modules
loadModule("random")
loadModule("component")
loadModule("computer")
loadModule("sandbox")
loadModule("boot")
--Setup core modules
modules.component.prepare()
modules.boot.boot()

1
src/lua/core/sandbox.lua Normal file
View File

@ -0,0 +1 @@

View File

@ -0,0 +1,11 @@
local random = {}
function random.uuid()
local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
return string.gsub(template, '[xy]', function (c)
local v = (c == 'x') and random(0, 0xf) or random(8, 0xb)
return string.format('%x', v)
end)
end
return random