Lua: Use some Genode functions from example
The example provides Timer::msleep() and the process' quota() to Lua code.
This commit is contained in:
parent
0200c27e33
commit
28d4bdba83
|
@ -2,7 +2,7 @@
|
||||||
# Lua C++ library test
|
# Lua C++ library test
|
||||||
#
|
#
|
||||||
|
|
||||||
build "core init test/moon"
|
build "core init drivers/timer test/moon"
|
||||||
|
|
||||||
create_boot_directory
|
create_boot_directory
|
||||||
|
|
||||||
|
@ -11,11 +11,16 @@ install_config {
|
||||||
<parent-provides>
|
<parent-provides>
|
||||||
<service name="ROM"/>
|
<service name="ROM"/>
|
||||||
<service name="RM"/>
|
<service name="RM"/>
|
||||||
|
<service name="CAP"/>
|
||||||
<service name="LOG"/>
|
<service name="LOG"/>
|
||||||
</parent-provides>
|
</parent-provides>
|
||||||
<default-route>
|
<default-route>
|
||||||
<any-service> <parent/> <any-child/> </any-service>
|
<any-service> <parent/> <any-child/> </any-service>
|
||||||
</default-route>
|
</default-route>
|
||||||
|
<start name="timer">
|
||||||
|
<resource name="RAM" quantum="1M"/>
|
||||||
|
<provides> <service name="Timer"/> </provides>
|
||||||
|
</start>
|
||||||
<start name="test-moon">
|
<start name="test-moon">
|
||||||
<resource name="RAM" quantum="1100K"/>
|
<resource name="RAM" quantum="1100K"/>
|
||||||
</start>
|
</start>
|
||||||
|
@ -23,11 +28,11 @@ install_config {
|
||||||
}
|
}
|
||||||
|
|
||||||
build_boot_image {
|
build_boot_image {
|
||||||
core init test-moon
|
core init timer test-moon
|
||||||
ld.lib.so libc.lib.so libc_log.lib.so libm.lib.so
|
ld.lib.so libc.lib.so libc_log.lib.so libm.lib.so
|
||||||
}
|
}
|
||||||
|
|
||||||
append qemu_args " -nographic -m 64 "
|
append qemu_args " -nographic -m 64 "
|
||||||
|
|
||||||
run_genode_until forever
|
run_genode_until {.*test-moon.*Finished\..*} 30
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,9 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* Genode includes */
|
/* Genode includes */
|
||||||
|
#include <base/env.h>
|
||||||
#include <base/printf.h>
|
#include <base/printf.h>
|
||||||
|
#include <timer_session/connection.h>
|
||||||
|
|
||||||
/* Lua includes */
|
/* Lua includes */
|
||||||
#include <lua.h>
|
#include <lua.h>
|
||||||
|
@ -20,48 +22,103 @@
|
||||||
#include <lauxlib.h>
|
#include <lauxlib.h>
|
||||||
|
|
||||||
|
|
||||||
static int log(lua_State *L)
|
static Timer::Session & timer_session()
|
||||||
{
|
{
|
||||||
int n = lua_gettop(L);
|
static Timer::Connection timer;
|
||||||
|
|
||||||
|
return timer;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lua: Sleep for milliseconds
|
||||||
|
*/
|
||||||
|
static int l_msleep(lua_State *lua)
|
||||||
|
{
|
||||||
|
if ((lua_gettop(lua) != 1)
|
||||||
|
|| !lua_isnumber(lua, 1)) {
|
||||||
|
lua_pushstring(lua, "msleep: invalid argument");
|
||||||
|
lua_error(lua);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
timer_session().msleep(lua_tonumber(lua, 1));
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lua: Return RAM quota
|
||||||
|
*/
|
||||||
|
static int l_quota(lua_State *lua)
|
||||||
|
{
|
||||||
|
if ((lua_gettop(lua) != 0)) {
|
||||||
|
lua_pushstring(lua, "quota: invalid argument");
|
||||||
|
lua_error(lua);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
lua_pushnumber(lua, Genode::env()->ram_session()->quota());
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Lua: Log arguments
|
||||||
|
*/
|
||||||
|
static int l_log(lua_State *lua)
|
||||||
|
{
|
||||||
|
int n = lua_gettop(lua);
|
||||||
|
|
||||||
for (int i = 1; i <= n; ++i) {
|
for (int i = 1; i <= n; ++i) {
|
||||||
if (lua_isstring(L, i))
|
if (lua_isstring(lua, i))
|
||||||
PLOG("%s", lua_tostring(L, i));
|
PLOG("%s", lua_tostring(lua, i));
|
||||||
else if (lua_isnil(L, i))
|
else if (lua_isnil(lua, i))
|
||||||
PLOG("%s", "nil");
|
PLOG("%s", "nil");
|
||||||
else if (lua_isboolean(L, i))
|
else if (lua_isboolean(lua, i))
|
||||||
PLOG("%s", lua_toboolean(L, i) ? "true" : "false");
|
PLOG("%s", lua_toboolean(lua, i) ? "true" : "false");
|
||||||
else
|
else
|
||||||
PLOG("%s: %p", luaL_typename(L, i), lua_topointer(L, i));
|
PLOG("%s: %p", luaL_typename(lua, i), lua_topointer(lua, i));
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static char const *exec_string =
|
static char const *exec_string =
|
||||||
"i = 10000000000000000 + 1\n"
|
"local a = { }\n"
|
||||||
"log(\"your result is: \"..i)\n"
|
|
||||||
"a = { }\n"
|
|
||||||
"log(a)\n"
|
"log(a)\n"
|
||||||
"log(type(a))\n"
|
|
||||||
"a.foo = \"foo\"\n"
|
"a.foo = \"foo\"\n"
|
||||||
"a.bar = \"bar\"\n"
|
"a.bar = \"bar\"\n"
|
||||||
"log(a.foo .. \" \" .. a.bar)\n"
|
"log(a.foo .. \" \" .. a.bar)\n"
|
||||||
|
"\n"
|
||||||
|
"print(\"Our RAM quota is \"..quota()..\" bytes.\")\n"
|
||||||
|
"\n"
|
||||||
|
"print(\"Going to sleep...\")\n"
|
||||||
|
"for i=1,4 do\n"
|
||||||
|
" msleep(i * 1000)\n"
|
||||||
|
" print(\"Slept well for \"..i..\" seconds.\")\n"
|
||||||
|
"end\n"
|
||||||
|
"print(\"Finished.\")\n"
|
||||||
;
|
;
|
||||||
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
lua_State *L = lua_open();
|
lua_State *lua = lua_open();
|
||||||
|
|
||||||
/* initialize libs */
|
/* initialize libs */
|
||||||
luaopen_base(L);
|
luaopen_base(lua);
|
||||||
|
|
||||||
/* register simple log function */
|
/* register local functions */
|
||||||
lua_register(L, "log", log);
|
lua_register(lua, "log", l_log);
|
||||||
|
lua_register(lua, "msleep", l_msleep);
|
||||||
|
lua_register(lua, "quota", l_quota);
|
||||||
|
|
||||||
if (luaL_dostring(L, exec_string) != 0)
|
if (luaL_dostring(lua, exec_string) != 0)
|
||||||
PLOG("%s\n", lua_tostring(L, -1));
|
PLOG("%s\n", lua_tostring(lua, -1));
|
||||||
|
|
||||||
lua_close(L);
|
lua_close(lua);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue