-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathLuaJitEngine.h
185 lines (166 loc) · 5.99 KB
/
LuaJitEngine.h
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#ifndef LUAJITENGINE_H
#define LUAJITENGINE_H
/*
* Copyright 2019 Rochus Keller <mailto:[email protected]>
*
* This file is part of the JuaJIT BC Viewer application.
*
* The following is the license that applies to this copy of the
* application. For a license to use the application under conditions
* other than those described here, please email to [email protected].
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
#include <QObject>
#include <QSet>
#include <QVariant>
#include <QPointer>
#include <LjTools/LuaJitBytecode.h>
namespace Lua
{
class JitBytecode;
// Execute LuaJIT bytecode; focus is on verification and comprehension, not performance
class JitEngine : public QObject
{
Q_OBJECT
public:
class Table;
class TableRef
{
public:
TableRef( Table* t = 0 ):d_table(t)
{
if( t )
d_all.insert(this);
}
TableRef( const TableRef& rhs ):d_table(0)
{
*this = rhs;
}
TableRef& operator=( const TableRef& rhs )
{
if( d_table && rhs.d_table == 0 )
d_all.remove(this);
if( d_table == 0 && rhs.d_table != 0 )
d_all.insert(this);
d_table = rhs.d_table;
return *this;
}
~TableRef()
{
if( d_table )
d_all.remove(this);
}
Table* deref() const { return d_table; }
bool isNull() const { return d_table == 0; }
private:
Table* d_table;
static QSet<TableRef*> d_all;
friend class JitEngine;
};
class Table
{
public:
static Table* create() { return new Table(); }
void allocateUserData( quint32 size );
bool isUserObject() const { return d_userData != 0; }
void* userData() { return d_userData; }
private:
friend class JitEngine;
Table():d_marked(false),d_userData(0)
{
d_all.insert(this);
}
~Table()
{
if( d_userData )
::free(d_userData);
d_all.remove(this);
}
static QSet<Table*> d_all;
QHash<QVariant,QVariant> d_hash;
TableRef d_metaTable;
void* d_userData;
bool d_marked;
};
struct Slot : public QSharedData
{
QVariant d_val;
bool d_outOfScope;
bool d_closed;
};
typedef QExplicitlySharedDataPointer<Slot> SlotRef;
struct Closure
{
Closure(JitBytecode::Function* f = 0 ):d_func(f) {}
JitBytecode::FuncRef d_func;
QList<SlotRef> d_upvals;
};
struct CFunction
{
typedef int (*Imp)(JitEngine*, QVariantList& inout );
CFunction(Imp f = 0):d_func(f){}
Imp d_func;
// returns 0..n ...number of return values, -1 ...error
};
explicit JitEngine(QObject *parent = 0);
~JitEngine();
bool run(JitBytecode*);
signals:
void sigPrint( const QString&, bool err = false );
protected:
void reset();
void collectGarbage();
bool error( const QString& ) const;
void installLibs();
struct Frame
{
Frame(Frame* outer = 0, Closure* f = 0):d_pc(0),d_func(f),d_outer(outer){}
void dump();
Frame* d_outer;
Closure* d_func;
QList<SlotRef> d_slots;
quint32 d_pc;
};
bool run(Frame* outer, Closure*, QVariantList& inout );
bool error2(const Frame&, const QString& msg ) const;
Slot* getSlot(const Frame&, int i) const;
QVariant getSlotVal( const Frame&, int i ) const;
TableRef getTable( const Frame&, int i ) const;
void setSlotVal(const Frame&, int i, const QVariant& );
QVariant getUpvalue( const Frame&, int i ) const;
void setUpvalue(const Frame&, int i, const QVariant& );
QVariant getNumConst( const Frame& f, int i ) const;
QVariant getGcConst( const Frame& f, int i ) const;
QVariant getPriConst(int i );
bool doCompare( Frame&, const JitBytecode::Instruction& bc );
bool doEquality( Frame&, const JitBytecode::Instruction& bc );
bool doJumpAfterCompare(Frame&, bool res );
bool doArith( Frame&, const JitBytecode::Instruction& bc );
bool doGetT( Frame&, const JitBytecode::Instruction& bc );
bool doSetT( Frame&, const JitBytecode::Instruction& bc );
int doCall(Frame&, const QVariant& f, QVariantList& inout );
static int _print(JitEngine*, QVariantList& inout );
static QByteArray tostring( const QVariant& v );
static int _setmetatable(JitEngine*, QVariantList& inout );
static int _getmetatable(JitEngine*, QVariantList& inout );
static bool isTrue( const QVariant& );
static QVariant getBinHandler( const QVariant& lhs, const QVariant& rhs, int event );
static QVariant getHandler( const QVariant& v, int event );
static QVariant getCompHandler( const QVariant& lhs, const QVariant& rhs, int event );
private:
QHash<QVariant,QVariant> d_globals;
Frame d_root;
};
}
Q_DECLARE_METATYPE( Lua::JitEngine::TableRef )
Q_DECLARE_METATYPE( Lua::JitEngine::Closure )
Q_DECLARE_METATYPE( Lua::JitEngine::CFunction )
#endif // LUAJITENGINE_H