Repository navigation
Expand file tree
/
Copy pathScripting.h
More file actions
465 lines (382 loc) · 10.1 KB
/
Copy pathScripting.h
File metadata and controls
465 lines (382 loc) · 10.1 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/// \file
#ifndef _LGI_SCRIPTING_H_
#define _LGI_SCRIPTING_H_
#include "lgi/common/Variant.h"
#include "lgi/common/List.h"
class LScriptContext;
class LScriptEnginePrivate;
class LVmCallback;
class LVirtualMachine;
typedef bool (LScriptContext::*ScriptCmd)(LScriptArguments &Args);
#define SCOPE_REGISTER 0
#define SCOPE_LOCAL 1
#define SCOPE_GLOBAL 2
#define SCOPE_OBJECT 3
#define SCOPE_RETURN 4
#define SCOPE_MAX 5
/// Execution status
enum LExecutionStatus
{
ScriptNotStarted,
ScriptError,
ScriptWarning,
ScriptSuccess,
};
/// Various type of methods
enum LFuncType
{
/// No method type.
NullFunc,
/// This method is provided by the hosting application.
HostFunc,
/// This method is defined in the script itself.
ScriptFunc,
/// This method is defined in an external library.
ExternFunc
};
struct LFunc
{
LFuncType Type;
LString Method;
bool InUse;
LFunc(const char *m = NULL, LFuncType t = NullFunc)
{
Type = t;
Method = m;
InUse = false;
// LStackTrace("%p alloc\n", this);
}
virtual ~LFunc()
{
// LAssert(!InUse);
}
virtual LExecutionStatus Call(LScriptContext *Ctx, LScriptArguments &Args) = 0;
};
struct LHostFunc : public LFunc
{
LScriptContext *Context = nullptr;
LString Args;
ScriptCmd Func = nullptr;
LHostFunc(const LHostFunc &f)
{
Type = f.Type;
Method = f.Method;
InUse = f.InUse;
Context = f.Context;
Args = f.Args;
Func = f.Func;
}
LHostFunc() :
LFunc()
{
}
LHostFunc(const char *method, const char *args, ScriptCmd proc) : LFunc(method, HostFunc)
{
Args = args;
Func = proc;
}
LExecutionStatus Call(LScriptContext *Ctx, LScriptArguments &Args) override;
};
struct LExternFunc : public LFunc
{
struct ExternType
{
int Ptr;
bool Unsigned;
bool Out;
int ArrayLen;
LVariantType Base;
};
LAutoString Lib;
ExternType ReturnType;
LArray<ExternType> ArgType;
LExecutionStatus Call(LScriptContext *Ctx, LScriptArguments &Args) override;
};
class LFunctionInfo : public LRefCount
{
static int _Infos;
int32 StartAddr = INVALID_ADDR;
LString Name;
// The reason why this is a pointer is because during the function compilation the frame
// size is actually unknown. If the function calls itself then it won't know what
// frame size to insert in the assembly (this is NULL).
// In which case it has to insert a post compilation fix-up for the frame size.
LAutoPtr<uint16> FrameSize;
// The number and names of the parameters to the function.
LArray<LString> Params;
public:
static constexpr int32 INVALID_ADDR = -1;
LFunctionInfo(const char *name)
{
if (name)
Name = name;
}
~LFunctionInfo()
{
}
char *GetName()
{
return Name;
}
LArray<LString> &GetParams()
{
return Params;
}
bool ValidFrameSize()
{
return FrameSize.Get() != NULL;
}
uint16 GetFrameSize()
{
if (!FrameSize)
{
LAssert(!"Invalid frame size");
return 0;
}
return *FrameSize;
}
template<typename T>
bool SetFrameSize(T size)
{
if (size >= 0xffff)
{
LAssert(!"Invalid frame size.");
return false;
}
return FrameSize.Reset(new uint16((uint16)size));
}
bool ValidStartAddr()
{
return StartAddr != INVALID_ADDR;
}
int32 GetStartAddr()
{
LAssert(ValidStartAddr());
return StartAddr;
}
bool SetStartAddr(int32_t addr)
{
if (addr < 0)
{
LAssert(!"Invalid start address");
return false;
}
StartAddr = addr;
return true;
}
LFunctionInfo &operator =(LFunctionInfo &f)
{
StartAddr = f.StartAddr;
FrameSize = f.FrameSize;
Name = f.Name;
for (unsigned i=0; i<f.Params.Length(); i++)
{
Params[i] = f.Params[i];
}
return *this;
}
};
class LScriptUtils
{
public:
virtual ~LScriptUtils() {}
int atoi(char16 *s);
int64 atoi64(char16 *s);
double atof(char16 *s);
int htoi(char16 *s);
};
/// The base context class for scripting engines
class LScriptContext : public LScriptUtils
{
public:
virtual ~LScriptContext() {}
virtual LHostFunc *GetCommands() = 0;
virtual LString GetIncludeFile(const char *FileName) = 0;
virtual LAutoString GetDataFolder() { return LAutoString(); }
virtual LStream *GetLog() { return NULL; }
virtual bool SetLog(LStream *Log) { return false; }
// AddPrimitive: Add your primitives functions in a derived class using the format:
//
// bool MyPrim(LVariant *Ret, ArgumentArray &Args) { ... }
//
// Where you return true if successful or false on error.
};
class LVariables : public LArray<LVariant>
{
friend class LVirtualMachinePriv;
LHashTbl<ConstStrKey<char>,int> Lut;
public:
int Scope;
int NullIndex;
LCustomType *Obj;
LVariables(int scope)
{
Scope = scope;
NullIndex = -1;
Obj = NULL;
}
LVariables(LCustomType *obj)
{
Scope = SCOPE_OBJECT;
NullIndex = -1;
Obj = obj;
}
int Var(const char *n, bool create = false)
{
if (Obj)
{
return Obj->IndexOf(n);
}
else
{
int p = Lut.Find(n);
if (p)
{
return p - 1;
}
if (create)
{
int Len = (int)Length();
Lut.Add(n, Len + 1);
Length(Len + 1);
return Len;
}
}
return -1;
}
};
/// A block of compile byte code
class LCompiledCode
{
friend class LCompilerPriv;
friend class LVirtualMachine;
friend class LVirtualMachinePriv;
friend class LCompiler;
friend class LVmDebuggerWnd;
/// The global variables
LVariables Globals;
/// The byte code of all the instructions
LArray<uint8_t> ByteCode;
/// All the methods defined in the byte code and their arguments.
LArray< LAutoRefPtr<LFunctionInfo> > Methods;
/// All the externs defined in the code.
LArray<LExternFunc*> Externs;
/// All the user types defined
LHashTbl<StrKey<char16>, class LCustomType*> Types;
/// The original script details
LString FileName;
LString Source;
/// The system context (all the system functions)
LScriptContext *SysContext = nullptr;
/// Any user context (application functions)
LScriptContext *UserContext = nullptr;
/// Debug info to map instruction address back to source line numbers
LHashTbl<IntKey<NativeInt>, int> Debug;
public:
LCompiledCode();
LCompiledCode(LCompiledCode ©);
~LCompiledCode();
/// Size of the byte code
size_t Length() { return ByteCode.Length(); }
/// Assignment operator
LCompiledCode &operator =(const LCompiledCode &c);
/// Gets a method defined in the code
LFunctionInfo *GetMethod(const char *Name, bool Create = false);
/// Sets a global variable
LVariant *Set(const char *Name, LVariant &v);
/// Gets the definition of a struct or custom type
LCustomType *GetType(char16 *Name) { return Types.Find(Name); }
/// Gets the file name this code was compiled from
const char *GetFileName() { return FileName; }
/// Gets the source
const char *GetSource() { return Source; }
/// Gets the source line number associated with an address
int ObjectToSourceAddress(size_t ObjAddr);
/// Turns an object address into a FileName:LineNumber string.
const char *AddrToSourceRef(size_t ObjAddr);
/// Sets the file name this code was compiled from
void SetSource(const char *file, const char *src)
{
if (file != FileName.Get())
FileName = file;
Source = src;
}
};
/// New compiler/byte code/VM scripting engine
class LScriptEngine
{
class LScriptEnginePrivate *d;
public:
LScriptEngine( LViewI *parent = nullptr,
LScriptContext *UserContext = nullptr,
LVmCallback *Callback = nullptr);
~LScriptEngine();
LStream *GetConsole();
bool SetConsole(LStream *t);
LCompiledCode *GetCurrentCode();
bool Compile( LAutoPtr<LCompiledCode> &Obj,
LScriptContext *UserContext,
const char *Script,
const char *FileName = NULL,
LDom *Args = NULL);
LExecutionStatus Run(LCompiledCode *Obj, LVariant *Ret = NULL, const char *TempPath = NULL);
LExecutionStatus RunTemporary(LCompiledCode *Obj, char *Script, LVariant *Ret = NULL);
bool EvaluateExpression(LVariant *Result, LDom *VariableSource, const char *Expression);
bool CallMethod(LCompiledCode *Obj, const char *Method, LScriptArguments &Args);
LScriptContext *GetSystemContext();
};
class LVirtualMachine;
class LVmDebugger : public LDom
{
public:
/// Makes the debugger the owner of the compiled code
virtual void SetCode(LAutoPtr<LCompiledCode> Cc) = 0;
/// Gets the code owned by the debugger
virtual LCompiledCode *GetCode() = 0;
/// Set the source and asm
virtual void SetSource(const char *Mixed) = 0;
/// Show UI and wait for user response
virtual void Run() = 0;
// Events
/// Called to update the debugger UI to the new current execution position
virtual void OnAddress(size_t Addr) = 0;
/// Called when an error occurs executing the script
virtual void OnError(const char *Msg) = 0;
/// Called when execution starts or ends
virtual void OnRun(bool Running) = 0;
};
class LVmCallback : public LDom
{
public:
/// Call a callback by name.
virtual bool CallCallback(LVirtualMachine &Vm, LString CallbackName, LScriptArguments &Args) = 0;
/// Start a debugger instance to handle the execution in the Vm.
/// Implementors should COPY the Vm and Code objects to give to the debugger, because their state
/// will go away when the stack unwinds. The copy will maintain the state for the debugger to inspect.
virtual LVmDebugger *AttachVm(LVirtualMachine *Vm, LCompiledCode *Code, const char *Assembly) = 0;
/// Compile a new script
virtual bool CompileScript(LAutoPtr<LCompiledCode> &Output, const char *FileName, const char *Source) = 0;
};
/// Debugger for vm script
class LVmDebuggerWnd : public LWindow, public LVmDebugger
{
struct LScriptVmDebuggerPriv *d;
void UpdateVariables(LList *Lst, LVariant *Arr, ssize_t Len, char Prefix);
public:
LVmDebuggerWnd(LView *Parent, LVmCallback *Callback, LAutoPtr<LVirtualMachine> Vm, LAutoPtr<LCompiledCode> Code, const char *Assembly);
~LVmDebuggerWnd();
void OnAddress(size_t Addr) override;
void OnError(const char *Msg) override;
void OnRun(bool Running) override;
void SetSource(const char *Mixed) override;
int OnNotify(LViewI *Ctrl, const LNotification &n) override;
int OnCommand(int Cmd, int Event, OsView Wnd) override;
bool OnRequestClose(bool OsShuttingDown) override;
LMessage::Param OnEvent(LMessage *Msg) override;
void LoadFile(const char *File);
LStream *GetLog();
void SetCode(LAutoPtr<LCompiledCode> Cc) override;
LCompiledCode *GetCode() override;
void Run() override;
};
#endif