aboutsummaryrefslogtreecommitdiff
path: root/src/LuaRuntime.cpp
blob: eaf3df7c483496419564baeef6da1b2157e743c1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <QtCore>
#include <cstdlib>
#include <cstring>
#include <lua.hpp>
#include <qvariant.h>
extern "C" {
#include <luv/luv.h>
}

#include "AsyncEventLoop.hpp"
#include "LuaRuntime.hpp"
#include "LuaRuntimeApi.hpp"

LuaRuntime::LuaRuntime() {
  state = luaL_newstate();
  luaL_openlibs(state);

  init_builtins_package_path();
  init_web_api();
}

void LuaRuntime::start_event_loop() {
  if (event_loop != nullptr)
    stop_event_loop();

  // Init event loop
  event_loop = new AsyncEventLoop();

  // Load `uv` (luv)
  luv_set_loop(state, event_loop->get_uv_loop());
  luaopen_luv(state);
  lua_setglobal(state, uv_global_name);
}

void LuaRuntime::stop_event_loop() {
  if (event_loop != nullptr) {
    delete event_loop;
    event_loop = nullptr;
  }

  // Clear the uv global
  lua_pushnil(state);
  lua_setglobal(state, uv_global_name);
  lua_gc(state, LUA_GCCOLLECT, 0);
}

void LuaRuntime::init_web_api() {
  luaL_newlib(state, internals_api);
  lua_setglobal(state, internals_global_name);

  require_module("null-browser.api");
}

void LuaRuntime::require_module(const QString &module_name) {
  preserve_top(state, {
    lua_getglobal(state, "require");
    lua_pushstring(state, module_name.toStdString().c_str());
    if (lua_pcall(state, 1, 0, 0) != LUA_OK) {
      qCritical() << "Unable to require module" << module_name << ":" << lua_tostring(state, -1);
    }
  });
}

void LuaRuntime::init_builtins_package_path() {
  auto builtins_lua_path = QString(PROJECT_LUA_PATH);
  append_package_path(builtins_lua_path);
}

void LuaRuntime::evaluate(const QString &code) {
  queue_task([this, code]() {
    preserve_top(state, {
      if (luaL_dostring(state, code.toStdString().c_str()) != LUA_OK) {
        const char *value = lua_tostring(state, -1);
        qDebug() << "Lua Error: " << value;
        emit evaluation_failed(value);
      } else {
        const QVariant value = get_lua_value(state, -1);
        emit evaluation_completed(value);
      }
    })
  });
}

void LuaRuntime::append_package_path(const QString &path) {
  preserve_top(state, {
    lua_getglobal(state, "package");
    lua_getfield(state, -1, "path");
    auto pkg_path = QString(lua_tostring(state, -1)) + ";" + path;
    lua_pop(state, 1);
    lua_pushstring(state, pkg_path.toStdString().c_str());
    lua_setfield(state, -2, "path");
  });
}

void LuaRuntime::load_file_sync(const QString &path) {
  preserve_top(state, {
    if (luaL_dofile(state, path.toStdString().c_str()) != LUA_OK) {
      lua_error(state);
    }
  });
}

LuaRuntime::~LuaRuntime() {
  stop_event_loop();
  lua_close(state);
  state = nullptr;
}