aboutsummaryrefslogtreecommitdiff
path: root/src/LuaRuntime.hpp
blob: 6c45ca59ec6f7b1636e856805843aafbc095d3fe (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
#pragma once

#include <QtCore>
#include <functional>
#include <lua.hpp>

#include "AsyncEventLoop.hpp"
#include "widgets/WebViewStack.hpp"

class LuaRuntime : public QObject {
  Q_OBJECT

public:
  static LuaRuntime *instance() {
    static LuaRuntime inst;
    return &inst;
  }

  void evaluate(QString code);
  QVariant evaluateSync(QString code);

  void loadFile(QString code);

  void stopEventLoop();
  void startEventLoop();

  QVariant getValue(int idx);

  DELEGATE(eventLoop, queueTask, queueTask)
  DEFINE_GETTER(getState, state)

signals:
  void urlOpened(QString url, OpenType openType);
  void evaluationCompleted(QVariant value);
  void evaluationFailed(QString value);
  void keymapAddRequested(QString mode, QString keyseq, std::function<void()>);
  // void outputProduced(QVariant value);

protected:
  LuaRuntime();
  ~LuaRuntime();
  static int lua_onUrlOpen(lua_State *state);
  static int lua_onUrlTabOpen(lua_State *state);
  static int lua_addKeymap(lua_State *state);

private:
  lua_State *state;
  AsyncEventLoop *eventLoop = nullptr;
};