bool LuaInterface::Init()
{
	if(!_lua)
	{
		_lua = luaL_newstate();
		if(!_lua)
			return false;

		lua_atpanic(_lua, the_panic);

		// Lua internal functions
		luaL_openlibs(_lua);

		// Own functions.
		lua_register_all(_lua);
	}

	if(luaL_loadfile(_lua, script) != LUA_OK)
	{
		const char *err = lua_tostring(_lua, -1);
		if(err)
			puts(err);
		return false;
	}

	// push args
	for(int i = 0; i < argc; ++i)
		lua_pushstring(_lua, argv[i]);

	return doCall(argc);
}
Beispiel #2
0
static portTASK_FUNCTION_PROTO(angelicTask, pvParameters) {
  (void) pvParameters;

  #ifndef TEST_STATIC_LUA
  if (*(uint32_t *)(code_buffer) == NGL_PACKAGE_MAGIC) {
  #else
  if (0) {
  #endif
    // Load Angelic blob
    ngl_buffer *program = ngl_buffer_alloc(code_buffer_len);
    // TODO(rqou): This is dumb.
    memcpy(NGL_BUFFER_DATA(program), code_buffer, code_buffer_len);
    // TODO(rqou): Dealloc code_buffer???
    ngl_run_package((ngl_package *) NGL_BUFFER_DATA(program));
    // TODO(rqou): Error handling?
    // TODO(rqou): What to do here?
    while (1) {}
  } else {
    // Load Lua blob
    lua_State *L = luaL_newstate();
    luaL_openlibs(L);

    // Register builtins
    lua_register(L, "set_motor_old", lua_set_motor_old);
    lua_register(L, "get_sensor", lua_get_sensor);
    lua_register_all(L);

    // Load the code blob into the Lua state
    const char *read_from_code_buffer(lua_State *L, void *data, size_t *size) {
      int *data_int = (int*)data;
      if (*data_int) {
        return NULL;
      } else {
        *data_int = 1;
        *size = code_buffer_len;
        return (const char *)code_buffer;
      }
    }
    int reader_state = 0;
    #ifndef TEST_STATIC_LUA
      lua_load(L, read_from_code_buffer, &reader_state, "<tenshi>", "b");
    #else
      luaL_loadstring(L, TEST_STATIC_LUA);
    #endif
    // TODO(rqou): Error handling?
    lua_pcall(L, 0, LUA_MULTRET, 0);
    // TODO(rqou): What to do here?
    while (1) {}
  }
}