1 |
/* |
/* |
2 |
* Copyright (c) 2014 Christian Schoenebeck |
* Copyright (c) 2014 - 2016 Christian Schoenebeck |
3 |
* |
* |
4 |
* http://www.linuxsampler.org |
* http://www.linuxsampler.org |
5 |
* |
* |
10 |
#include "ScriptVM.h" |
#include "ScriptVM.h" |
11 |
|
|
12 |
#include <string.h> |
#include <string.h> |
13 |
|
#include <assert.h> |
14 |
#include "../common/global_private.h" |
#include "../common/global_private.h" |
15 |
#include "tree.h" |
#include "tree.h" |
16 |
|
#include "CoreVMFunctions.h" |
17 |
|
#include "CoreVMDynVars.h" |
18 |
|
#include "editor/NkspScanner.h" |
19 |
|
|
20 |
#define DEBUG_SCRIPTVM_CORE 0 |
#define DEBUG_SCRIPTVM_CORE 0 |
21 |
|
|
92 |
return max; |
return max; |
93 |
} |
} |
94 |
|
|
95 |
ScriptVM::ScriptVM() : m_parserContext(NULL), fnWait(this) { |
ScriptVM::ScriptVM() : m_eventHandler(NULL), m_parserContext(NULL) { |
96 |
|
m_fnMessage = new CoreVMFunction_message; |
97 |
|
m_fnExit = new CoreVMFunction_exit; |
98 |
|
m_fnWait = new CoreVMFunction_wait(this); |
99 |
|
m_fnAbs = new CoreVMFunction_abs; |
100 |
|
m_fnRandom = new CoreVMFunction_random; |
101 |
|
m_fnNumElements = new CoreVMFunction_num_elements; |
102 |
|
m_varRealTimer = new CoreVMDynVar_NKSP_REAL_TIMER; |
103 |
|
m_varPerfTimer = new CoreVMDynVar_NKSP_PERF_TIMER; |
104 |
} |
} |
105 |
|
|
106 |
ScriptVM::~ScriptVM() { |
ScriptVM::~ScriptVM() { |
107 |
|
delete m_fnMessage; |
108 |
|
delete m_fnExit; |
109 |
|
delete m_fnWait; |
110 |
|
delete m_fnAbs; |
111 |
|
delete m_fnRandom; |
112 |
|
delete m_fnNumElements; |
113 |
|
delete m_varRealTimer; |
114 |
|
delete m_varPerfTimer; |
115 |
} |
} |
116 |
|
|
117 |
VMParserContext* ScriptVM::loadScript(const String& s) { |
VMParserContext* ScriptVM::loadScript(const String& s) { |
126 |
context->registerBuiltInConstIntVariables( builtInConstIntVariables() ); |
context->registerBuiltInConstIntVariables( builtInConstIntVariables() ); |
127 |
context->registerBuiltInIntVariables( builtInIntVariables() ); |
context->registerBuiltInIntVariables( builtInIntVariables() ); |
128 |
context->registerBuiltInIntArrayVariables( builtInIntArrayVariables() ); |
context->registerBuiltInIntArrayVariables( builtInIntArrayVariables() ); |
129 |
|
context->registerBuiltInDynVariables( builtInDynamicVariables() ); |
130 |
|
|
131 |
context->createScanner(is); |
context->createScanner(is); |
132 |
|
|
133 |
InstrScript_parse(context); |
InstrScript_parse(context); |
134 |
dmsg(2,("Allocating %d bytes of global int VM memory.\n", context->globalIntVarCount * sizeof(int))); |
dmsg(2,("Allocating %ld bytes of global int VM memory.\n", long(context->globalIntVarCount * sizeof(int)))); |
135 |
dmsg(2,("Allocating %d of global VM string variables.\n", context->globalStrVarCount)); |
dmsg(2,("Allocating %d of global VM string variables.\n", context->globalStrVarCount)); |
136 |
if (!context->globalIntMemory) |
if (!context->globalIntMemory) |
137 |
context->globalIntMemory = new ArrayList<int>(); |
context->globalIntMemory = new ArrayList<int>(); |
173 |
_requiredMaxStackSizeFor(&*parserCtx->handlers); |
_requiredMaxStackSizeFor(&*parserCtx->handlers); |
174 |
} |
} |
175 |
execCtx->stack.resize(parserCtx->requiredMaxStackSize); |
execCtx->stack.resize(parserCtx->requiredMaxStackSize); |
176 |
dmsg(2,("Created VM exec context with %d bytes VM stack size.\n", |
dmsg(2,("Created VM exec context with %ld bytes VM stack size.\n", |
177 |
parserCtx->requiredMaxStackSize * sizeof(ExecContext::StackFrame))); |
long(parserCtx->requiredMaxStackSize * sizeof(ExecContext::StackFrame)))); |
178 |
//printf("execCtx=0x%lx\n", (uint64_t)execCtx); |
//printf("execCtx=0x%lx\n", (uint64_t)execCtx); |
179 |
const int polySize = parserCtx->polyphonicIntVarCount; |
const int polySize = parserCtx->polyphonicIntVarCount; |
180 |
execCtx->polyphonicIntMemory.resize(polySize); |
execCtx->polyphonicIntMemory.resize(polySize); |
181 |
memset(&execCtx->polyphonicIntMemory[0], 0, polySize * sizeof(int)); |
memset(&execCtx->polyphonicIntMemory[0], 0, polySize * sizeof(int)); |
182 |
|
|
183 |
dmsg(2,("Allocated %d bytes polyphonic memory.\n", polySize * sizeof(int))); |
dmsg(2,("Allocated %ld bytes polyphonic memory.\n", long(polySize * sizeof(int)))); |
184 |
return execCtx; |
return execCtx; |
185 |
} |
} |
186 |
|
|
187 |
|
std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(const String& s) { |
188 |
|
std::istringstream iss(s); |
189 |
|
return syntaxHighlighting(&iss); |
190 |
|
} |
191 |
|
|
192 |
|
std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(std::istream* is) { |
193 |
|
NkspScanner scanner(is); |
194 |
|
std::vector<SourceToken> tokens = scanner.tokens(); |
195 |
|
std::vector<VMSourceToken> result; |
196 |
|
result.resize(tokens.size()); |
197 |
|
for (int i = 0; i < tokens.size(); ++i) { |
198 |
|
SourceToken* st = new SourceToken; |
199 |
|
*st = tokens[i]; |
200 |
|
result[i] = VMSourceToken(st); |
201 |
|
} |
202 |
|
return result; |
203 |
|
} |
204 |
|
|
205 |
VMFunction* ScriptVM::functionByName(const String& name) { |
VMFunction* ScriptVM::functionByName(const String& name) { |
206 |
if (name == "message") return &fnMessage; |
if (name == "message") return m_fnMessage; |
207 |
else if (name == "exit") return &fnExit; |
else if (name == "exit") return m_fnExit; |
208 |
else if (name == "wait") return &fnWait; |
else if (name == "wait") return m_fnWait; |
209 |
|
else if (name == "abs") return m_fnAbs; |
210 |
|
else if (name == "random") return m_fnRandom; |
211 |
|
else if (name == "num_elements") return m_fnNumElements; |
212 |
return NULL; |
return NULL; |
213 |
} |
} |
214 |
|
|
220 |
return std::map<String,VMInt8Array*>(); |
return std::map<String,VMInt8Array*>(); |
221 |
} |
} |
222 |
|
|
223 |
|
std::map<String,VMDynVar*> ScriptVM::builtInDynamicVariables() { |
224 |
|
std::map<String,VMDynVar*> m; |
225 |
|
|
226 |
|
m["$NKSP_PERF_TIMER"] = m_varPerfTimer; |
227 |
|
m["$NKSP_REAL_TIMER"] = m_varRealTimer; |
228 |
|
m["$KSP_TIMER"] = m_varRealTimer; |
229 |
|
|
230 |
|
return m; |
231 |
|
} |
232 |
|
|
233 |
std::map<String,int> ScriptVM::builtInConstIntVariables() { |
std::map<String,int> ScriptVM::builtInConstIntVariables() { |
234 |
return std::map<String,int>(); |
return std::map<String,int>(); |
235 |
} |
} |
236 |
|
|
237 |
|
VMEventHandler* ScriptVM::currentVMEventHandler() { |
238 |
|
return m_eventHandler; |
239 |
|
} |
240 |
|
|
241 |
VMParserContext* ScriptVM::currentVMParserContext() { |
VMParserContext* ScriptVM::currentVMParserContext() { |
242 |
return m_parserContext; |
return m_parserContext; |
243 |
} |
} |
264 |
} |
} |
265 |
EventHandler* h = dynamic_cast<EventHandler*>(handler); |
EventHandler* h = dynamic_cast<EventHandler*>(handler); |
266 |
if (!h) return VM_EXEC_NOT_RUNNING; |
if (!h) return VM_EXEC_NOT_RUNNING; |
267 |
|
m_eventHandler = handler; |
268 |
|
|
269 |
m_parserContext->execContext = ctx; |
m_parserContext->execContext = ctx; |
270 |
|
|
360 |
ctx->reset(); |
ctx->reset(); |
361 |
} |
} |
362 |
|
|
363 |
|
m_eventHandler = NULL; |
364 |
m_parserContext->execContext = NULL; |
m_parserContext->execContext = NULL; |
365 |
m_parserContext = NULL; |
m_parserContext = NULL; |
366 |
return ctx->status; |
return ctx->status; |