1 |
/* |
/* |
2 |
* Copyright (c) 2014 - 2017 Christian Schoenebeck |
* Copyright (c) 2014 - 2019 Christian Schoenebeck |
3 |
* |
* |
4 |
* http://www.linuxsampler.org |
* http://www.linuxsampler.org |
5 |
* |
* |
117 |
else |
else |
118 |
return 1; |
return 1; |
119 |
} |
} |
120 |
|
|
121 |
|
case STMT_SYNC: { |
122 |
|
#if DEBUG_SCRIPTVM_CORE |
123 |
|
_printIndents(depth); |
124 |
|
printf("-> STMT_SYNC\n"); |
125 |
|
#endif |
126 |
|
SyncBlock* syncStmt = (SyncBlock*) statement; |
127 |
|
if (syncStmt->statements()) |
128 |
|
return _requiredMaxStackSizeFor( syncStmt->statements() ) + 1; |
129 |
|
else |
130 |
|
return 1; |
131 |
|
} |
132 |
} |
} |
133 |
|
|
134 |
return 1; // actually just to avoid compiler warning |
return 1; // actually just to avoid compiler warning |
143 |
return max; |
return max; |
144 |
} |
} |
145 |
|
|
146 |
ScriptVM::ScriptVM() : m_eventHandler(NULL), m_parserContext(NULL), m_autoSuspend(true) { |
ScriptVM::ScriptVM() : |
147 |
|
m_eventHandler(NULL), m_parserContext(NULL), m_autoSuspend(true), |
148 |
|
m_acceptExitRes(false) |
149 |
|
{ |
150 |
m_fnMessage = new CoreVMFunction_message; |
m_fnMessage = new CoreVMFunction_message; |
151 |
m_fnExit = new CoreVMFunction_exit; |
m_fnExit = new CoreVMFunction_exit(this); |
152 |
m_fnWait = new CoreVMFunction_wait(this); |
m_fnWait = new CoreVMFunction_wait(this); |
153 |
m_fnAbs = new CoreVMFunction_abs; |
m_fnAbs = new CoreVMFunction_abs; |
154 |
m_fnRandom = new CoreVMFunction_random; |
m_fnRandom = new CoreVMFunction_random; |
162 |
m_fnShRight = new CoreVMFunction_sh_right; |
m_fnShRight = new CoreVMFunction_sh_right; |
163 |
m_fnMin = new CoreVMFunction_min; |
m_fnMin = new CoreVMFunction_min; |
164 |
m_fnMax = new CoreVMFunction_max; |
m_fnMax = new CoreVMFunction_max; |
165 |
|
m_fnArrayEqual = new CoreVMFunction_array_equal; |
166 |
|
m_fnSearch = new CoreVMFunction_search; |
167 |
|
m_fnSort = new CoreVMFunction_sort; |
168 |
} |
} |
169 |
|
|
170 |
ScriptVM::~ScriptVM() { |
ScriptVM::~ScriptVM() { |
181 |
delete m_fnShRight; |
delete m_fnShRight; |
182 |
delete m_fnMin; |
delete m_fnMin; |
183 |
delete m_fnMax; |
delete m_fnMax; |
184 |
|
delete m_fnArrayEqual; |
185 |
|
delete m_fnSearch; |
186 |
|
delete m_fnSort; |
187 |
delete m_varRealTimer; |
delete m_varRealTimer; |
188 |
delete m_varPerfTimer; |
delete m_varPerfTimer; |
189 |
} |
} |
264 |
} |
} |
265 |
|
|
266 |
std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(std::istream* is) { |
std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(std::istream* is) { |
267 |
NkspScanner scanner(is); |
try { |
268 |
std::vector<SourceToken> tokens = scanner.tokens(); |
NkspScanner scanner(is); |
269 |
std::vector<VMSourceToken> result; |
std::vector<SourceToken> tokens = scanner.tokens(); |
270 |
result.resize(tokens.size()); |
std::vector<VMSourceToken> result; |
271 |
for (int i = 0; i < tokens.size(); ++i) { |
result.resize(tokens.size()); |
272 |
SourceToken* st = new SourceToken; |
for (int i = 0; i < tokens.size(); ++i) { |
273 |
*st = tokens[i]; |
SourceToken* st = new SourceToken; |
274 |
result[i] = VMSourceToken(st); |
*st = tokens[i]; |
275 |
|
result[i] = VMSourceToken(st); |
276 |
|
} |
277 |
|
return result; |
278 |
|
} catch (...) { |
279 |
|
return std::vector<VMSourceToken>(); |
280 |
} |
} |
|
return result; |
|
281 |
} |
} |
282 |
|
|
283 |
VMFunction* ScriptVM::functionByName(const String& name) { |
VMFunction* ScriptVM::functionByName(const String& name) { |
294 |
else if (name == "sh_right") return m_fnShRight; |
else if (name == "sh_right") return m_fnShRight; |
295 |
else if (name == "min") return m_fnMin; |
else if (name == "min") return m_fnMin; |
296 |
else if (name == "max") return m_fnMax; |
else if (name == "max") return m_fnMax; |
297 |
|
else if (name == "array_equal") return m_fnArrayEqual; |
298 |
|
else if (name == "search") return m_fnSearch; |
299 |
|
else if (name == "sort") return m_fnSort; |
300 |
return NULL; |
return NULL; |
301 |
} |
} |
302 |
|
|
303 |
|
bool ScriptVM::isFunctionDisabled(VMFunction* fn, VMParserContext* ctx) { |
304 |
|
ParserContext* parserCtx = dynamic_cast<ParserContext*>(ctx); |
305 |
|
if (!parserCtx) return false; |
306 |
|
|
307 |
|
if (fn == m_fnMessage && parserCtx->userPreprocessorConditions.count("NKSP_NO_MESSAGE")) |
308 |
|
return true; |
309 |
|
|
310 |
|
return false; |
311 |
|
} |
312 |
|
|
313 |
std::map<String,VMIntRelPtr*> ScriptVM::builtInIntVariables() { |
std::map<String,VMIntRelPtr*> ScriptVM::builtInIntVariables() { |
314 |
return std::map<String,VMIntRelPtr*>(); |
return std::map<String,VMIntRelPtr*>(); |
315 |
} |
} |
360 |
return m_autoSuspend; |
return m_autoSuspend; |
361 |
} |
} |
362 |
|
|
363 |
|
void ScriptVM::setExitResultEnabled(bool b) { |
364 |
|
m_acceptExitRes = b; |
365 |
|
} |
366 |
|
|
367 |
|
bool ScriptVM::isExitResultEnabled() const { |
368 |
|
return m_acceptExitRes; |
369 |
|
} |
370 |
|
|
371 |
VMExecStatus_t ScriptVM::exec(VMParserContext* parserContext, VMExecContext* execContex, VMEventHandler* handler) { |
VMExecStatus_t ScriptVM::exec(VMParserContext* parserContext, VMExecContext* execContex, VMEventHandler* handler) { |
372 |
m_parserContext = dynamic_cast<ParserContext*>(parserContext); |
m_parserContext = dynamic_cast<ParserContext*>(parserContext); |
373 |
if (!m_parserContext) { |
if (!m_parserContext) { |
390 |
m_parserContext->execContext = ctx; |
m_parserContext->execContext = ctx; |
391 |
|
|
392 |
ctx->status = VM_EXEC_RUNNING; |
ctx->status = VM_EXEC_RUNNING; |
393 |
StmtFlags_t flags = STMT_SUCCESS; |
ctx->instructionsCount = 0; |
394 |
|
ctx->clearExitRes(); |
395 |
|
StmtFlags_t& flags = ctx->flags; |
396 |
int instructionsCounter = 0; |
int instructionsCounter = 0; |
397 |
|
int synced = m_autoSuspend ? 0 : 1; |
398 |
|
|
399 |
int& frameIdx = ctx->stackFrame; |
int& frameIdx = ctx->stackFrame; |
400 |
if (frameIdx < 0) { // start condition ... |
if (frameIdx < 0) { // start condition ... |
471 |
ctx->pushStack( |
ctx->pushStack( |
472 |
whileStmt->statements() |
whileStmt->statements() |
473 |
); |
); |
474 |
if (flags == STMT_SUCCESS && m_autoSuspend && |
if (flags == STMT_SUCCESS && !synced && |
475 |
instructionsCounter > SCRIPTVM_MAX_INSTR_PER_CYCLE_SOFT) |
instructionsCounter > SCRIPTVM_MAX_INSTR_PER_CYCLE_SOFT) |
476 |
{ |
{ |
477 |
flags = StmtFlags_t(STMT_SUSPEND_SIGNALLED); |
flags = StmtFlags_t(STMT_SUSPEND_SIGNALLED); |
480 |
} else ctx->popStack(); |
} else ctx->popStack(); |
481 |
break; |
break; |
482 |
} |
} |
483 |
|
|
484 |
|
case STMT_SYNC: { |
485 |
|
#if DEBUG_SCRIPTVM_CORE |
486 |
|
_printIndents(frameIdx); |
487 |
|
printf("-> STMT_SYNC\n"); |
488 |
|
#endif |
489 |
|
SyncBlock* syncStmt = (SyncBlock*) frame.statement; |
490 |
|
if (!frame.subindex++ && syncStmt->statements()) { |
491 |
|
++synced; |
492 |
|
ctx->pushStack( |
493 |
|
syncStmt->statements() |
494 |
|
); |
495 |
|
} else { |
496 |
|
ctx->popStack(); |
497 |
|
--synced; |
498 |
|
} |
499 |
|
break; |
500 |
|
} |
501 |
} |
} |
502 |
|
|
503 |
if (flags == STMT_SUCCESS && m_autoSuspend && |
if (flags == STMT_SUCCESS && !synced && |
504 |
instructionsCounter > SCRIPTVM_MAX_INSTR_PER_CYCLE_HARD) |
instructionsCounter > SCRIPTVM_MAX_INSTR_PER_CYCLE_HARD) |
505 |
{ |
{ |
506 |
flags = StmtFlags_t(STMT_SUSPEND_SIGNALLED); |
flags = StmtFlags_t(STMT_SUSPEND_SIGNALLED); |
510 |
++instructionsCounter; |
++instructionsCounter; |
511 |
} |
} |
512 |
|
|
513 |
if (flags & STMT_SUSPEND_SIGNALLED) { |
if ((flags & STMT_SUSPEND_SIGNALLED) && !(flags & STMT_ABORT_SIGNALLED)) { |
514 |
ctx->status = VM_EXEC_SUSPENDED; |
ctx->status = VM_EXEC_SUSPENDED; |
515 |
|
ctx->flags = STMT_SUCCESS; |
516 |
} else { |
} else { |
517 |
ctx->status = VM_EXEC_NOT_RUNNING; |
ctx->status = VM_EXEC_NOT_RUNNING; |
518 |
if (flags & STMT_ERROR_OCCURRED) |
if (flags & STMT_ERROR_OCCURRED) |
520 |
ctx->reset(); |
ctx->reset(); |
521 |
} |
} |
522 |
|
|
523 |
|
ctx->instructionsCount = instructionsCounter; |
524 |
|
|
525 |
m_eventHandler = NULL; |
m_eventHandler = NULL; |
526 |
m_parserContext->execContext = NULL; |
m_parserContext->execContext = NULL; |
527 |
m_parserContext = NULL; |
m_parserContext = NULL; |