9 |
|
|
10 |
#include "ScriptVM.h" |
#include "ScriptVM.h" |
11 |
|
|
12 |
|
#include <string.h> |
13 |
#include "../common/global_private.h" |
#include "../common/global_private.h" |
14 |
#include "tree.h" |
#include "tree.h" |
15 |
|
|
88 |
return max; |
return max; |
89 |
} |
} |
90 |
|
|
91 |
ScriptVM::ScriptVM() : fnWait(this) { |
ScriptVM::ScriptVM() : m_parserContext(NULL), fnWait(this) { |
|
m_context = new ParserContext(this); |
|
92 |
} |
} |
93 |
|
|
94 |
ScriptVM::~ScriptVM() { |
ScriptVM::~ScriptVM() { |
|
if (m_context) { |
|
|
if (m_context->globalIntMemory) { |
|
|
delete m_context->globalIntMemory; |
|
|
m_context->globalIntMemory = NULL; |
|
|
} |
|
|
delete m_context; |
|
|
} |
|
95 |
} |
} |
96 |
|
|
97 |
void ScriptVM::loadScript(const String& s) { |
VMParserContext* ScriptVM::loadScript(const String& s) { |
98 |
std::istringstream iss(s); |
std::istringstream iss(s); |
99 |
loadScript(&iss); |
return loadScript(&iss); |
100 |
} |
} |
101 |
|
|
102 |
void ScriptVM::loadScript(std::istream* is) { |
VMParserContext* ScriptVM::loadScript(std::istream* is) { |
103 |
m_context->createScanner(is); |
ParserContext* context = new ParserContext(this); |
104 |
InstrScript_parse(m_context); |
//printf("parserCtx=0x%lx\n", (uint64_t)context); |
105 |
std::cout << "Allocating " << m_context->globalIntVarCount * sizeof(int) << " bytes of global int VM memory.\n"; |
|
106 |
std::cout << "Allocating " << m_context->globalStrVarCount << " of global VM string variables.\n"; |
context->createScanner(is); |
107 |
if (!m_context->globalIntMemory) |
|
108 |
m_context->globalIntMemory = new ArrayList<int>(); |
InstrScript_parse(context); |
109 |
if (!m_context->globalStrMemory) |
std::cout << "Allocating " << context->globalIntVarCount * sizeof(int) << " bytes of global int VM memory.\n"; |
110 |
m_context->globalStrMemory = new ArrayList<String>(); |
std::cout << "Allocating " << context->globalStrVarCount << " of global VM string variables.\n"; |
111 |
m_context->globalIntMemory->resize(m_context->globalIntVarCount); |
if (!context->globalIntMemory) |
112 |
m_context->globalStrMemory->resize(m_context->globalStrVarCount); |
context->globalIntMemory = new ArrayList<int>(); |
113 |
} |
if (!context->globalStrMemory) |
114 |
|
context->globalStrMemory = new ArrayList<String>(); |
115 |
|
context->globalIntMemory->resize(context->globalIntVarCount); |
116 |
|
memset(&((*context->globalIntMemory)[0]), 0, context->globalIntVarCount * sizeof(int)); |
117 |
|
|
118 |
|
context->globalStrMemory->resize(context->globalStrVarCount); |
119 |
|
|
120 |
std::vector<ParserIssue> ScriptVM::issues() const { |
context->destroyScanner(); |
|
return m_context->issues; |
|
|
} |
|
121 |
|
|
122 |
std::vector<ParserIssue> ScriptVM::errors() const { |
return context; |
|
return m_context->errors; |
|
123 |
} |
} |
124 |
|
|
125 |
std::vector<ParserIssue> ScriptVM::warnings() const { |
void ScriptVM::dumpParsedScript(VMParserContext* context) { |
126 |
return m_context->warnings; |
ParserContext* ctx = dynamic_cast<ParserContext*>(context); |
127 |
} |
if (!ctx) { |
|
|
|
|
void ScriptVM::dumpParsedScript() { |
|
|
if (!m_context) { |
|
128 |
std::cerr << "No VM context. So nothing to dump.\n"; |
std::cerr << "No VM context. So nothing to dump.\n"; |
129 |
return; |
return; |
130 |
} |
} |
131 |
if (!m_context->handlers) { |
if (!ctx->handlers) { |
132 |
std::cerr << "No event handlers defined in script. So nothing to dump.\n"; |
std::cerr << "No event handlers defined in script. So nothing to dump.\n"; |
133 |
return; |
return; |
134 |
} |
} |
135 |
if (!m_context->globalIntMemory) { |
if (!ctx->globalIntMemory) { |
136 |
std::cerr << "Internal error: no global memory assigend to script VM.\n"; |
std::cerr << "Internal error: no global memory assigend to script VM.\n"; |
137 |
return; |
return; |
138 |
} |
} |
139 |
m_context->handlers->dump(); |
ctx->handlers->dump(); |
140 |
} |
} |
141 |
|
|
142 |
VMExecContext* ScriptVM::createExecContext() { |
VMExecContext* ScriptVM::createExecContext(VMParserContext* parserContext) { |
143 |
ExecContext* ctx = new ExecContext(); |
ParserContext* parserCtx = dynamic_cast<ParserContext*>(parserContext); |
144 |
const int stackSize = _requiredMaxStackSizeFor(&*m_context->handlers); |
ExecContext* execCtx = new ExecContext(); |
145 |
ctx->stack.resize(stackSize); |
|
146 |
|
if (parserCtx->requiredMaxStackSize < 0) { |
147 |
|
parserCtx->requiredMaxStackSize = |
148 |
|
_requiredMaxStackSizeFor(&*parserCtx->handlers); |
149 |
|
} |
150 |
|
execCtx->stack.resize(parserCtx->requiredMaxStackSize); |
151 |
std::cout << "Created VM exec context with " |
std::cout << "Created VM exec context with " |
152 |
<< stackSize * sizeof(ExecContext::StackFrame) |
<< parserCtx->requiredMaxStackSize * sizeof(ExecContext::StackFrame) |
153 |
<< " bytes VM stack size.\n"; |
<< " bytes VM stack size.\n"; |
154 |
const int polySize = m_context->polyphonicIntVarCount; |
//printf("execCtx=0x%lx\n", (uint64_t)execCtx); |
155 |
ctx->polyphonicIntMemory.resize(polySize); |
const int polySize = parserCtx->polyphonicIntVarCount; |
156 |
|
execCtx->polyphonicIntMemory.resize(polySize); |
157 |
|
memset(&execCtx->polyphonicIntMemory[0], 0, polySize * sizeof(int)); |
158 |
|
|
159 |
std::cout << "Allocated " << polySize * sizeof(int) |
std::cout << "Allocated " << polySize * sizeof(int) |
160 |
<< " bytes polyphonic memory.\n"; |
<< " bytes polyphonic memory.\n"; |
161 |
return ctx; |
return execCtx; |
|
} |
|
|
|
|
|
VMEventHandler* ScriptVM::eventHandler(uint index) { |
|
|
if (!m_context) return NULL; |
|
|
if (!m_context->handlers) return NULL; |
|
|
return m_context->handlers->eventHandler(index); |
|
|
} |
|
|
|
|
|
VMEventHandler* ScriptVM::eventHandlerByName(const String& name) { |
|
|
if (!m_context) return NULL; |
|
|
if (!m_context->handlers) return NULL; |
|
|
return m_context->handlers->eventHandlerByName(name); |
|
162 |
} |
} |
163 |
|
|
164 |
VMFunction* ScriptVM::functionByName(const String& name) { |
VMFunction* ScriptVM::functionByName(const String& name) { |
167 |
else if (name == "wait") return &fnWait; |
else if (name == "wait") return &fnWait; |
168 |
return NULL; |
return NULL; |
169 |
} |
} |
170 |
|
|
171 |
|
VMParserContext* ScriptVM::currentVMParserContext() { |
172 |
|
return m_parserContext; |
173 |
|
} |
174 |
|
|
175 |
VMExecContext* ScriptVM::currentVMExecContext() { |
VMExecContext* ScriptVM::currentVMExecContext() { |
176 |
if (!m_context) return NULL; |
if (!m_parserContext) return NULL; |
177 |
return m_context->execContext; |
return m_parserContext->execContext; |
178 |
} |
} |
179 |
|
|
180 |
VMExecStatus_t ScriptVM::exec(VMEventHandler* handler, VMExecContext* execContex) { |
VMExecStatus_t ScriptVM::exec(VMParserContext* parserContext, VMExecContext* execContex, VMEventHandler* handler) { |
181 |
if (!m_context) { |
m_parserContext = dynamic_cast<ParserContext*>(parserContext); |
182 |
std::cerr << "No VM parser context. Did you load a script?.\n"; |
if (!m_parserContext) { |
183 |
|
std::cerr << "No VM parser context provided. Did you load a script?.\n"; |
184 |
return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR); |
return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR); |
185 |
} |
} |
186 |
|
|
192 |
EventHandler* h = dynamic_cast<EventHandler*>(handler); |
EventHandler* h = dynamic_cast<EventHandler*>(handler); |
193 |
if (!h) return VM_EXEC_NOT_RUNNING; |
if (!h) return VM_EXEC_NOT_RUNNING; |
194 |
|
|
195 |
m_context->execContext = ctx; |
m_parserContext->execContext = ctx; |
196 |
|
|
197 |
ctx->status = VM_EXEC_RUNNING; |
ctx->status = VM_EXEC_RUNNING; |
198 |
StmtFlags_t flags = STMT_SUCCESS; |
StmtFlags_t flags = STMT_SUCCESS; |
286 |
ctx->reset(); |
ctx->reset(); |
287 |
} |
} |
288 |
|
|
289 |
m_context->execContext = NULL; |
m_parserContext->execContext = NULL; |
290 |
|
m_parserContext = NULL; |
291 |
return ctx->status; |
return ctx->status; |
292 |
} |
} |
293 |
|
|