/[svn]/linuxsampler/trunk/src/scriptvm/ScriptVM.cpp
ViewVC logotype

Diff of /linuxsampler/trunk/src/scriptvm/ScriptVM.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2581 by schoenebeck, Fri May 30 12:48:05 2014 UTC revision 2885 by schoenebeck, Fri Apr 22 15:37:45 2016 UTC
# Line 1  Line 1 
1  /*  /*
2   * Copyright (c) 2014 Christian Schoenebeck   * Copyright (c) 2014 - 2016 Christian Schoenebeck
3   *   *
4   * http://www.linuxsampler.org   * http://www.linuxsampler.org
5   *   *
# Line 9  Line 9 
9    
10  #include "ScriptVM.h"  #include "ScriptVM.h"
11    
12    #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 "editor/NkspScanner.h"
18    
19  #define DEBUG_SCRIPTVM_CORE 0  #define DEBUG_SCRIPTVM_CORE 0
20    
# Line 87  namespace LinuxSampler { Line 91  namespace LinuxSampler {
91          return max;          return max;
92      }      }
93    
94      ScriptVM::ScriptVM() : fnWait(this) {      ScriptVM::ScriptVM() : m_eventHandler(NULL), m_parserContext(NULL) {
95          m_context = new ParserContext(this);          m_fnMessage = new CoreVMFunction_message;
96            m_fnExit = new CoreVMFunction_exit;
97            m_fnWait = new CoreVMFunction_wait(this);
98            m_fnAbs = new CoreVMFunction_abs;
99            m_fnRandom = new CoreVMFunction_random;
100            m_fnNumElements = new CoreVMFunction_num_elements;
101      }      }
102    
103      ScriptVM::~ScriptVM() {      ScriptVM::~ScriptVM() {
104          if (m_context) {          delete m_fnMessage;
105              if (m_context->globalIntMemory) {          delete m_fnExit;
106                  delete m_context->globalIntMemory;          delete m_fnWait;
107                  m_context->globalIntMemory = NULL;          delete m_fnAbs;
108              }          delete m_fnRandom;
109              delete m_context;          delete m_fnNumElements;
         }  
110      }      }
111    
112      void ScriptVM::loadScript(const String& s) {      VMParserContext* ScriptVM::loadScript(const String& s) {
113          std::istringstream iss(s);          std::istringstream iss(s);
114          loadScript(&iss);          return loadScript(&iss);
115      }      }
116            
117      void ScriptVM::loadScript(std::istream* is) {      VMParserContext* ScriptVM::loadScript(std::istream* is) {
118          m_context->createScanner(is);          ParserContext* context = new ParserContext(this);
119          InstrScript_parse(m_context);          //printf("parserCtx=0x%lx\n", (uint64_t)context);
120          std::cout << "Allocating " << m_context->globalIntVarCount * sizeof(int) << " bytes of global int VM memory.\n";  
121          std::cout << "Allocating " << m_context->globalStrVarCount << " of global VM string variables.\n";          context->registerBuiltInConstIntVariables( builtInConstIntVariables() );
122          if (!m_context->globalIntMemory)          context->registerBuiltInIntVariables( builtInIntVariables() );
123              m_context->globalIntMemory = new ArrayList<int>();          context->registerBuiltInIntArrayVariables( builtInIntArrayVariables() );
124          if (!m_context->globalStrMemory)  
125              m_context->globalStrMemory = new ArrayList<String>();          context->createScanner(is);
126          m_context->globalIntMemory->resize(m_context->globalIntVarCount);  
127          m_context->globalStrMemory->resize(m_context->globalStrVarCount);          InstrScript_parse(context);
128      }          dmsg(2,("Allocating %ld bytes of global int VM memory.\n", long(context->globalIntVarCount * sizeof(int))));
129            dmsg(2,("Allocating %d of global VM string variables.\n", context->globalStrVarCount));
130            if (!context->globalIntMemory)
131                context->globalIntMemory = new ArrayList<int>();
132            if (!context->globalStrMemory)
133                context->globalStrMemory = new ArrayList<String>();
134            context->globalIntMemory->resize(context->globalIntVarCount);
135            memset(&((*context->globalIntMemory)[0]), 0, context->globalIntVarCount * sizeof(int));
136            
137            context->globalStrMemory->resize(context->globalStrVarCount);
138    
139      std::vector<ParserIssue> ScriptVM::issues() const {          context->destroyScanner();
         return m_context->issues;  
     }  
140    
141      std::vector<ParserIssue> ScriptVM::errors() const {          return context;
         return m_context->errors;  
142      }      }
143    
144      std::vector<ParserIssue> ScriptVM::warnings() const {      void ScriptVM::dumpParsedScript(VMParserContext* context) {
145          return m_context->warnings;          ParserContext* ctx = dynamic_cast<ParserContext*>(context);
146      }          if (!ctx) {
   
     void ScriptVM::dumpParsedScript() {  
         if (!m_context) {  
147              std::cerr << "No VM context. So nothing to dump.\n";              std::cerr << "No VM context. So nothing to dump.\n";
148              return;              return;
149          }          }
150          if (!m_context->handlers) {          if (!ctx->handlers) {
151              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";
152              return;              return;
153          }          }
154          if (!m_context->globalIntMemory) {          if (!ctx->globalIntMemory) {
155              std::cerr << "Internal error: no global memory assigend to script VM.\n";              std::cerr << "Internal error: no global memory assigend to script VM.\n";
156              return;              return;
157          }          }
158          m_context->handlers->dump();          ctx->handlers->dump();
159      }      }
160    
161      VMExecContext* ScriptVM::createExecContext() {      VMExecContext* ScriptVM::createExecContext(VMParserContext* parserContext) {
162          ExecContext* ctx = new ExecContext();          ParserContext* parserCtx = dynamic_cast<ParserContext*>(parserContext);
163          const int stackSize = _requiredMaxStackSizeFor(&*m_context->handlers);          ExecContext* execCtx = new ExecContext();
164          ctx->stack.resize(stackSize);          
165          std::cout << "Created VM exec context with "          if (parserCtx->requiredMaxStackSize < 0) {
166                    << stackSize * sizeof(ExecContext::StackFrame)               parserCtx->requiredMaxStackSize =
167                    << " bytes VM stack size.\n";                  _requiredMaxStackSizeFor(&*parserCtx->handlers);
168          const int polySize = m_context->polyphonicIntVarCount;          }
169          ctx->polyphonicIntMemory.resize(polySize);          execCtx->stack.resize(parserCtx->requiredMaxStackSize);
170          std::cout << "Allocated " << polySize * sizeof(int)          dmsg(2,("Created VM exec context with %ld bytes VM stack size.\n",
171                    << " bytes polyphonic memory.\n";                  long(parserCtx->requiredMaxStackSize * sizeof(ExecContext::StackFrame))));
172          return ctx;          //printf("execCtx=0x%lx\n", (uint64_t)execCtx);
173      }          const int polySize = parserCtx->polyphonicIntVarCount;
174            execCtx->polyphonicIntMemory.resize(polySize);
175      VMEventHandler* ScriptVM::eventHandler(uint index) {          memset(&execCtx->polyphonicIntMemory[0], 0, polySize * sizeof(int));
176          if (!m_context) return NULL;  
177          if (!m_context->handlers) return NULL;          dmsg(2,("Allocated %ld bytes polyphonic memory.\n", long(polySize * sizeof(int))));
178          return m_context->handlers->eventHandler(index);          return execCtx;
179      }      }
180    
181      VMEventHandler* ScriptVM::eventHandlerByName(const String& name) {      std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(const String& s) {
182          if (!m_context) return NULL;          std::istringstream iss(s);
183          if (!m_context->handlers) return NULL;          return syntaxHighlighting(&iss);
184          return m_context->handlers->eventHandlerByName(name);      }
185    
186        std::vector<VMSourceToken> ScriptVM::syntaxHighlighting(std::istream* is) {
187            NkspScanner scanner(is);
188            std::vector<SourceToken> tokens = scanner.tokens();
189            std::vector<VMSourceToken> result;
190            result.resize(tokens.size());
191            for (int i = 0; i < tokens.size(); ++i) {
192                SourceToken* st = new SourceToken;
193                *st = tokens[i];
194                result[i] = VMSourceToken(st);
195            }
196            return result;
197      }      }
198    
199      VMFunction* ScriptVM::functionByName(const String& name) {      VMFunction* ScriptVM::functionByName(const String& name) {
200          if (name == "message") return &fnMessage;          if (name == "message") return m_fnMessage;
201          else if (name == "exit") return &fnExit;          else if (name == "exit") return m_fnExit;
202          else if (name == "wait") return &fnWait;          else if (name == "wait") return m_fnWait;
203            else if (name == "abs") return m_fnAbs;
204            else if (name == "random") return m_fnRandom;
205            else if (name == "num_elements") return m_fnNumElements;
206          return NULL;          return NULL;
207      }      }
208        
209        std::map<String,VMIntRelPtr*> ScriptVM::builtInIntVariables() {
210            return std::map<String,VMIntRelPtr*>();
211        }
212    
213        std::map<String,VMInt8Array*> ScriptVM::builtInIntArrayVariables() {
214            return std::map<String,VMInt8Array*>();
215        }
216    
217        std::map<String,int> ScriptVM::builtInConstIntVariables() {
218            return std::map<String,int>();
219        }
220    
221        VMEventHandler* ScriptVM::currentVMEventHandler() {
222            return m_eventHandler;
223        }
224    
225        VMParserContext* ScriptVM::currentVMParserContext() {
226            return m_parserContext;
227        }
228    
229      VMExecContext* ScriptVM::currentVMExecContext() {      VMExecContext* ScriptVM::currentVMExecContext() {
230          if (!m_context) return NULL;          if (!m_parserContext) return NULL;
231          return m_context->execContext;          return m_parserContext->execContext;
232      }      }
233    
234      VMExecStatus_t ScriptVM::exec(VMEventHandler* handler, VMExecContext* execContex) {      VMExecStatus_t ScriptVM::exec(VMParserContext* parserContext, VMExecContext* execContex, VMEventHandler* handler) {
235          if (!m_context) {          m_parserContext = dynamic_cast<ParserContext*>(parserContext);
236              std::cerr << "No VM parser context. Did you load a script?.\n";          if (!m_parserContext) {
237                std::cerr << "No VM parser context provided. Did you load a script?.\n";
238              return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR);              return VMExecStatus_t(VM_EXEC_NOT_RUNNING | VM_EXEC_ERROR);
239          }          }
240    
241            // a ParserContext object is always tied to exactly one ScriptVM object
242            assert(m_parserContext->functionProvider == this);
243    
244          ExecContext* ctx = dynamic_cast<ExecContext*>(execContex);          ExecContext* ctx = dynamic_cast<ExecContext*>(execContex);
245          if (!ctx) {          if (!ctx) {
246              std::cerr << "Invalid VM exec context.\n";              std::cerr << "Invalid VM exec context.\n";
# Line 198  namespace LinuxSampler { Line 248  namespace LinuxSampler {
248          }          }
249          EventHandler* h = dynamic_cast<EventHandler*>(handler);          EventHandler* h = dynamic_cast<EventHandler*>(handler);
250          if (!h) return VM_EXEC_NOT_RUNNING;          if (!h) return VM_EXEC_NOT_RUNNING;
251            m_eventHandler = handler;
252    
253          m_context->execContext = ctx;          m_parserContext->execContext = ctx;
254    
255          ctx->status = VM_EXEC_RUNNING;          ctx->status = VM_EXEC_RUNNING;
256          StmtFlags_t flags = STMT_SUCCESS;          StmtFlags_t flags = STMT_SUCCESS;
# Line 293  namespace LinuxSampler { Line 344  namespace LinuxSampler {
344              ctx->reset();              ctx->reset();
345          }          }
346    
347          m_context->execContext = NULL;          m_eventHandler = NULL;
348            m_parserContext->execContext = NULL;
349            m_parserContext = NULL;
350          return ctx->status;          return ctx->status;
351      }      }
352    

Legend:
Removed from v.2581  
changed lines
  Added in v.2885

  ViewVC Help
Powered by ViewVC