-
Notifications
You must be signed in to change notification settings - Fork 11
/
script.h
90 lines (70 loc) · 2.52 KB
/
script.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
//---------------------------------------------------------------------------
#pragma once
#include "script_byte_code.h"
#include <assert.h>
#include <stddef.h>
#include <stdint.h>
//---------------------------------------------------------------------------
class Script {
public:
Script(const uint8_t *byteCode, void (*const *functionTable)(Script &))
: byteCode((const ScriptByteCode *)byteCode),
functionTable(functionTable) {}
void Push(intptr_t value) {
assert(stackTop < stack + MAX_STACK_SIZE);
*stackTop++ = value;
}
intptr_t Pop() {
assert(stackTop > stack);
// Expanded for codegen.
// return *--stackTop;
intptr_t *v = stackTop;
const intptr_t result = *--v;
stackTop = v;
return result;
}
void PrintScriptGlobals() const;
bool IsScriptEmpty(size_t offset) const;
bool IsScriptIndexEmpty(size_t index) const {
return IsScriptEmpty(byteCode->scriptOffsets[index]);
}
void ExecuteScript(size_t offset);
void ExecuteScriptIndex(size_t index) {
ExecuteScript(byteCode->scriptOffsets[index]);
}
void ExecuteScript(size_t offset, const intptr_t *parameters,
size_t parameterCount);
void ExecuteScriptIndex(size_t index, const intptr_t *parameters,
size_t parameterCount) {
ExecuteScript(byteCode->scriptOffsets[index], parameters, parameterCount);
}
void Reset();
bool IsValid() const { return byteCode->IsValid(); }
uint32_t Crc() const { return byteCode->Crc(); }
intptr_t *GetStackTop() const { return stackTop; }
void SetStackTop(intptr_t *p) { stackTop = p; }
void SetGlobal(size_t i, intptr_t v) { globals[i] = v; }
intptr_t GetGlobal(size_t i) const { return globals[i]; }
const uint8_t *FindStringOrReturnOriginal(const uint8_t *string) const {
return byteCode->FindStringOrReturnOriginal(string);
}
protected:
template <typename T> const T *GetScriptData(size_t offset) {
return (const T *)(intptr_t(byteCode) + offset);
}
template <typename T> void PushDataOffset(const T *data) {
Push(intptr_t(data) - intptr_t(byteCode));
}
// Executes at offset with no stack and offset checks.
void Run(size_t offset);
private:
class StackPointer;
class ProgramCounter;
static const size_t MAX_STACK_SIZE = 256;
const ScriptByteCode *const byteCode;
intptr_t *stackTop = stack;
void (*const *const functionTable)(Script &);
intptr_t globals[256];
intptr_t stack[MAX_STACK_SIZE];
};
//---------------------------------------------------------------------------