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

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

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

revision 2594 by schoenebeck, Thu Jun 5 00:16:25 2014 UTC revision 3221 by schoenebeck, Fri May 26 18:30:42 2017 UTC
# Line 1  Line 1 
1  /*  /*
2   * Copyright (c) 2014 Christian Schoenebeck   * Copyright (c) 2014-2017 Christian Schoenebeck
3   *   *
4   * http://www.linuxsampler.org   * http://www.linuxsampler.org
5   *   *
# Line 15  Line 15 
15    
16  #include "../common/global.h"  #include "../common/global.h"
17  #include "common.h"  #include "common.h"
 #include "CoreVMFunctions.h"  
18    
19  namespace LinuxSampler {  namespace LinuxSampler {
20    
# Line 24  namespace LinuxSampler { Line 23  namespace LinuxSampler {
23    
24      /** @brief Core virtual machine for real-time instrument scripts.      /** @brief Core virtual machine for real-time instrument scripts.
25       *       *
26       * This is the core of the virtual machine, used for running real-time       * This is the core of the virtual machine and main entry class, used for
27       * instrument scripts. The core encompasses the instrument script parser,       * running real-time instrument scripts. This VM core encompasses the
28       * generalized virtual machine and very generic built-in script functions.       * instrument script parser, generalized virtual machine and very generic
29       * Thus this class only provides functionalities which are yet independent       * built-in script functions. Thus this class only provides functionalities
30       * of the actual purpose the virtual machine is going to be used for.       * which are yet independent of the actual purpose the virtual machine is
31         * going to be used for.
32       *       *
33       * The actual use case specific functionalites (i.e. MIDI processing) is       * The actual use case specific functionalites (i.e. MIDI processing) is
34       * then implemented by VM classes which are derived from this generalized       * then implemented by sampler engines' VM classes which are derived from
35       * ScriptVM class.       * this generalized ScriptVM class.
36         *
37         * Typical usage of this class:
38         *
39         * - 1. Create an instance of this ScriptVM class (or of one of its deriving
40         *      classes).
41         * - 2. Load a script by passing its source code to method loadScript(),
42         *      which will return the parsed representation of the script.
43         * - 3. Create a VM execution context by calling createExecContext().
44         * - 4. Execute the script by calling method exec().
45       *       *
46       * This class is re-entrant safe, but not thread safe. So you can share one       * This class is re-entrant safe, but not thread safe. So you can share one
47       * instance of this class between multiple (native) threads, but you @b must       * instance of this class between multiple (native) threads, but you @b must
# Line 48  namespace LinuxSampler { Line 57  namespace LinuxSampler {
57      public:      public:
58          ScriptVM();          ScriptVM();
59          virtual ~ScriptVM();          virtual ~ScriptVM();
60    
61            /**
62             * Loads a script given by its source code (passed as argument @a s to
63             * this method) and returns the parsed representation of that script.
64             * After calling this method you must check the returned VMParserContext
65             * object whether there had been any parser errors. If there were no
66             * parser errors, you may pass the VMParserContext object to method
67             * exec() for actually executing the script.
68             *
69             * It is your responsibility to free the returned VMParserContext
70             * object once you don't need it anymore.
71             *
72             * @param s - entire source code of the script to be loaded
73             * @returns parsed representation of the script
74             */
75          VMParserContext* loadScript(const String& s);          VMParserContext* loadScript(const String& s);
76    
77            /**
78             * Same as above's loadScript() method, but this one reads the script's
79             * source code from an input stream object (i.e. stdin or a file).
80             *
81             * @param is - input stream from which the entire source code of the
82             *             script is to be read and loaded from
83             * @returns parsed representation of the script
84             */
85          VMParserContext* loadScript(std::istream* is);          VMParserContext* loadScript(std::istream* is);
86    
87            /**
88             * Parses a script's source code (passed as argument @a s to this
89             * method), splits that input up in its individual tokens (i.e.
90             * keyword, variable name, event name, etc.) and returns all those
91             * tokens, for the purpose that the caller can provide syntax syntax
92             * highlighting for the passed script.
93             *
94             * This method is actually not used by the sampler at all, it is rather
95             * provided for external script editor applications, to provide them a
96             * convenient backend for parsing scripts and providing syntax
97             * highlighting.
98             *
99             * @returns recognized tokens of passed script's source code
100             */
101            std::vector<VMSourceToken> syntaxHighlighting(const String& s);
102    
103            /**
104             * Same as above's syntaxHighlighting() method, but this one reads the
105             * script's source code from an input stream object (i.e. stdin or a
106             * file).
107             *
108             * @param is - input stream from which the entire source code of the
109             *             script is to be read and loaded from
110             * @returns recognized tokens of passed script's source code
111             */
112            std::vector<VMSourceToken> syntaxHighlighting(std::istream* is);
113    
114            /**
115             * Dumps the translated tree of the already parsed script, given by
116             * argument @a context, to stdout. This method is for debugging purposes
117             * only.
118             *
119             * @param context - parsed representation of the script
120             * @see loadScript()
121             */
122          void dumpParsedScript(VMParserContext* context);          void dumpParsedScript(VMParserContext* context);
123    
124            /**
125             * Creates a so called VM exceution context for a specific, already
126             * parsed script (provided by argument @a parserContext). Due to the
127             * general real-time design of this virtual machine, the VM execution
128             * context differs for every script. So you must (re)create the
129             * execution context for each script being loaded.
130             *
131             * @param parserContext - parsed representation of the script
132             * @see loadScript()
133             */
134          VMExecContext* createExecContext(VMParserContext* parserContext);          VMExecContext* createExecContext(VMParserContext* parserContext);
135          VMExecStatus_t exec(VMParserContext* parserContext, VMExecContext* execContex, VMEventHandler* handler);  
136            /**
137             * Execute a script by virtual machine. Since scripts are event-driven,
138             * you actually execute only one specific event handler block (i.e. a
139             * "on note ... end on" code block) by calling this method (not the
140             * entire script), and hence you must provide one precise handler of the
141             * script to be executed by this method.
142             *
143             * This method usually blocks until the entire script event handler
144             * block has been executed completely. It may however also return before
145             * completion if either a) a script runtime error occurred or b) the
146             * script was suspended by the VM (either because script execution
147             * exceeded a certain limit of time or the script called the built-in
148             * wait() function). You must check the return value of this method to
149             * find out which case applies.
150             *
151             * @param parserContext - parsed representation of the script (see loadScript())
152             * @param execContext - VM execution context (see createExecContext())
153             * @param handler - precise event handler (i.e. "on note ... end on"
154             *                  code block) to be executed
155             *                  (see VMParserContext::eventHandlerByName())
156             * @returns current status of the vitual machine (i.e. script succeeded,
157             *          script runtime error occurred or script was suspended for
158             *          some reason).
159             */
160            VMExecStatus_t exec(VMParserContext* parserContext, VMExecContext* execContext, VMEventHandler* handler);
161    
162            /**
163             * Returns built-in script function for the given function @a name. To
164             * get the implementation of the built-in message() script function for
165             * example, you would pass "message" here).
166             *
167             * This method is re-implemented by deriving classes to add more use
168             * case specific built-in functions.
169             *
170             * @param name - name of the function to be retrieved (i.e. "wait" for the
171             *               built-in wait() function).
172             */
173          VMFunction* functionByName(const String& name) OVERRIDE;          VMFunction* functionByName(const String& name) OVERRIDE;
174    
175            /**
176             * Returns all built-in integer script variables. This method returns a
177             * STL map, where the map's key is the variable name and the map's value
178             * is the native pointer to the actual built-in variable.
179             *
180             * This method is re-implemented by deriving classes to add more use
181             * case specific built-in variables.
182             */
183          std::map<String,VMIntRelPtr*> builtInIntVariables() OVERRIDE;          std::map<String,VMIntRelPtr*> builtInIntVariables() OVERRIDE;
184    
185            /**
186             * Returns all built-in (8 bit) integer array script variables. This
187             * method returns a STL map, where the map's key is the array variable
188             * name and the map's value is the native pointer to the actual built-in
189             * array variable.
190             *
191             * This method is re-implemented by deriving classes to add more use
192             * case specific built-in array variables.
193             */
194          std::map<String,VMInt8Array*> builtInIntArrayVariables() OVERRIDE;          std::map<String,VMInt8Array*> builtInIntArrayVariables() OVERRIDE;
195    
196            /**
197             * Returns all built-in constant integer script variables, which are
198             * constant and their final data is already available at parser time
199             * and won't change during runtime. Providing your built-in constants
200             * this way may lead to performance benefits compared to using other
201             * ways of providing built-in variables, because the script parser
202             * can perform optimizations when the script is refering to such
203             * constants.
204             *
205             * This type of built-in variable can only be read, but not be altered
206             * by scripts. This method returns a STL map, where the map's key is
207             * the variable name and the map's value is the final constant data.
208             *
209             * This method is re-implemented by deriving classes to add more use
210             * case specific built-in constant integers.
211             *
212             * @b Note: In case your built-in variable should be read-only but its
213             * value is not already available at parser time (i.e. because its
214             * value may change at runtime), then you should add it to
215             * builtInIntVariables() instead and use the macro
216             * DECLARE_VMINT_READONLY() to define the variable for read-only
217             * access by scripts.
218             */
219          std::map<String,int> builtInConstIntVariables() OVERRIDE;          std::map<String,int> builtInConstIntVariables() OVERRIDE;
220    
221            /**
222             * Returns all built-in dynamic variables. This method returns a STL
223             * map, where the map's key is the dynamic variable's name and the
224             * map's value is the pointer to the actual object implementing the
225             * behavior which is actually generating the content of the dynamic
226             * variable.
227             *
228             * This method is re-implemented by deriving classes to add more use
229             * case specific built-in dynamic variables.
230             */
231            std::map<String,VMDynVar*> builtInDynamicVariables() OVERRIDE;
232    
233            /**
234             * Enables or disables automatic suspension of scripts by the VM.
235             * If automatic suspension is enabled then scripts are monitored
236             * regarding their execution time and in case they are execution
237             * for too long, then they are automatically suspended by the VM for
238             * a certain amount of time in order to avoid any RT instablity
239             * issues caused by bugs in the script, i.e. endless while() loops
240             * or very large scripts.
241             *
242             * Automatic suspension is enabled by default due to the aimed
243             * real-time context of this virtual machine.
244             *
245             * @param b - true: enable auto suspension [default],
246             *            false: disable auto suspension
247             */
248            void setAutoSuspendEnabled(bool b = true);
249    
250            /**
251             * Returns true in case automatic suspension of scripts by the VM is
252             * enabled. See setAutoSuspendEnabled() for details.
253             *
254             * Automatic suspension is enabled by default due to the aimed
255             * real-time context of this virtual machine.
256             */
257            bool isAutoSuspendEnabled() const;
258    
259            VMEventHandler* currentVMEventHandler(); //TODO: should be protected (only usable during exec() calls, intended only for VMFunctions)
260          VMParserContext* currentVMParserContext(); //TODO: should be protected (only usable during exec() calls, intended only for VMFunctions)          VMParserContext* currentVMParserContext(); //TODO: should be protected (only usable during exec() calls, intended only for VMFunctions)
261          VMExecContext* currentVMExecContext(); //TODO: should be protected (only usable during exec() calls, intended only for VMFunctions)          VMExecContext* currentVMExecContext(); //TODO: should be protected (only usable during exec() calls, intended only for VMFunctions)
262    
263      protected:      protected:
264            VMEventHandler* m_eventHandler;
265          ParserContext* m_parserContext;          ParserContext* m_parserContext;
266          CoreVMFunction_message fnMessage;          bool m_autoSuspend;
267          CoreVMFunction_exit fnExit;          class CoreVMFunction_message* m_fnMessage;
268          CoreVMFunction_wait fnWait;          class CoreVMFunction_exit* m_fnExit;
269            class CoreVMFunction_wait* m_fnWait;
270            class CoreVMFunction_abs* m_fnAbs;
271            class CoreVMFunction_random* m_fnRandom;
272            class CoreVMFunction_num_elements* m_fnNumElements;
273            class CoreVMFunction_inc* m_fnInc;
274            class CoreVMFunction_dec* m_fnDec;
275            class CoreVMFunction_in_range* m_fnInRange;
276            class CoreVMFunction_sh_left* m_fnShLeft;
277            class CoreVMFunction_sh_right* m_fnShRight;
278            class CoreVMFunction_min* m_fnMin;
279            class CoreVMFunction_max* m_fnMax;
280            class CoreVMFunction_array_equal* m_fnArrayEqual;
281            class CoreVMFunction_search* m_fnSearch;
282            class CoreVMFunction_sort* m_fnSort;
283            class CoreVMDynVar_NKSP_REAL_TIMER* m_varRealTimer;
284            class CoreVMDynVar_NKSP_PERF_TIMER* m_varPerfTimer;
285      };      };
286    
287  } // namespace LinuxSampler  } // namespace LinuxSampler

Legend:
Removed from v.2594  
changed lines
  Added in v.3221

  ViewVC Help
Powered by ViewVC