/[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 2942 by schoenebeck, Wed Jul 13 15:51:06 2016 UTC revision 3733 by schoenebeck, Sat Feb 1 18:11:20 2020 UTC
# Line 1  Line 1 
1  /*  /*
2   * Copyright (c) 2014-2016 Christian Schoenebeck   * Copyright (c) 2014-2020 Christian Schoenebeck
3   *   *
4   * http://www.linuxsampler.org   * http://www.linuxsampler.org
5   *   *
# Line 69  namespace LinuxSampler { Line 69  namespace LinuxSampler {
69           * It is your responsibility to free the returned VMParserContext           * It is your responsibility to free the returned VMParserContext
70           * object once you don't need it anymore.           * object once you don't need it anymore.
71           *           *
72             * The NKSP language supports so called 'patch' variables, which are
73             * declared by the dedicated keyword 'patch' (as variable qualifier) in
74             * real-time instrument scripts, like e.g.:
75             * @code
76             * on init
77             *   declare patch ~foo := 0.435
78             * end on
79             * @endcode
80             * These 'patch' variables allow to override their initial value (i.e.
81             * on a per instrument basis). In the example above, the script variable
82             * @c ~foo would be initialized with value @c 0.435 by default. However
83             * by simply passing an appropriate key-value pair with argument
84             * @p patchVars when calling this method, the NKSP parser will replace
85             * that default initialization value by the passed replacement value.
86             * So key of the optional @p patchVars map argument is the ('patch')
87             * variable name to be patched, and value is the replacement
88             * initialization value for the respective variable. You can see this as
89             * kind of preprocessor mechanism of the NKSP parser, so you are not
90             * limited to simply replace a scalar value with a different scalar
91             * value, you can actually replace any complex default initialization
92             * expression with a new (potentially complex) replacement expression,
93             * e.g. including function calls, formulas, etc.
94             *
95             * The optional 3rd argument @p patchVarsDef allows you to retrieve the
96             * default initialization value(s) of all 'patch' variables declared in
97             * the passed script itself. This is useful for instrument editors.
98             *
99           * @param s - entire source code of the script to be loaded           * @param s - entire source code of the script to be loaded
100             * @param patchVars - (optional) replacement value for patch variables
101             * @param patchVarsDef - (optional) output of original values of patch
102             *                       variables
103           * @returns parsed representation of the script           * @returns parsed representation of the script
104           */           */
105          VMParserContext* loadScript(const String& s);          VMParserContext* loadScript(const String& s,
106                                        const std::map<String,String>& patchVars =
107                                              std::map<String,String>(),
108                                        std::map<String,String>* patchVarsDef = NULL);
109    
110          /**          /**
111           * Same as above's loadScript() method, but this one reads the script's           * Same as above's loadScript() method, but this one reads the script's
# Line 80  namespace LinuxSampler { Line 113  namespace LinuxSampler {
113           *           *
114           * @param is - input stream from which the entire source code of the           * @param is - input stream from which the entire source code of the
115           *             script is to be read and loaded from           *             script is to be read and loaded from
116             * @param patchVars - (optional) replacement value for patch variables
117             * @param patchVarsDef - (optional) output of original values of patch
118             *                       variables
119           * @returns parsed representation of the script           * @returns parsed representation of the script
120           */           */
121          VMParserContext* loadScript(std::istream* is);          VMParserContext* loadScript(std::istream* is,
122                                        const std::map<String,String>& patchVars =
123                                              std::map<String,String>(),
124                                        std::map<String,String>* patchVarsDef = NULL);
125    
126          /**          /**
127           * Parses a script's source code (passed as argument @a s to this           * Parses a script's source code (passed as argument @a s to this
# Line 173  namespace LinuxSampler { Line 212  namespace LinuxSampler {
212          VMFunction* functionByName(const String& name) OVERRIDE;          VMFunction* functionByName(const String& name) OVERRIDE;
213    
214          /**          /**
215             * Whether the passed built-in function is disabled and should thus be
216             * ignored by the parser at the passed parser context (parser state
217             * where the built-in function call occurs).
218             *
219             * @param fn - built-in function to be checked
220             * @param ctx - parser context at the position where the built-in
221             *              function call is located within the script
222             */
223            bool isFunctionDisabled(VMFunction* fn, VMParserContext* ctx) OVERRIDE;
224    
225            /**
226           * Returns all built-in integer script variables. This method returns a           * Returns all built-in integer script variables. This method returns a
227           * STL map, where the map's key is the variable name and the map's value           * STL map, where the map's key is the variable name and the map's value
228           * is the native pointer to the actual built-in variable.           * is the native pointer to the actual built-in variable.
# Line 180  namespace LinuxSampler { Line 230  namespace LinuxSampler {
230           * This method is re-implemented by deriving classes to add more use           * This method is re-implemented by deriving classes to add more use
231           * case specific built-in variables.           * case specific built-in variables.
232           */           */
233          std::map<String,VMIntRelPtr*> builtInIntVariables() OVERRIDE;          std::map<String,VMIntPtr*> builtInIntVariables() OVERRIDE;
234    
235          /**          /**
236           * Returns all built-in (8 bit) integer array script variables. This           * Returns all built-in (8 bit) integer array script variables. This
# Line 194  namespace LinuxSampler { Line 244  namespace LinuxSampler {
244          std::map<String,VMInt8Array*> builtInIntArrayVariables() OVERRIDE;          std::map<String,VMInt8Array*> builtInIntArrayVariables() OVERRIDE;
245    
246          /**          /**
247           * Returns all built-in constant integer script variables, which can           * Returns all built-in constant integer script variables, which are
248           * only be read, but not be altered by scripts. This method returns a           * constant and their final data is already available at parser time
249           * STL map, where the map's key is the variable name and the map's value           * and won't change during runtime. Providing your built-in constants
250           * is the native pointer to the actual built-in constant variable.           * this way may lead to performance benefits compared to using other
251             * ways of providing built-in variables, because the script parser
252             * can perform optimizations when the script is refering to such
253             * constants.
254             *
255             * This type of built-in variable can only be read, but not be altered
256             * by scripts. This method returns a STL map, where the map's key is
257             * the variable name and the map's value is the final constant data.
258           *           *
259           * This method is re-implemented by deriving classes to add more use           * This method is re-implemented by deriving classes to add more use
260           * case specific built-in constant integers.           * case specific built-in constant integers.
261           *           *
262           * @b Note: the term "constant" is a bit misleading here, since           * @b Note: In case your built-in variable should be read-only but its
263           * built-in constant integer variables may indeed change, i.e. for           * value is not already available at parser time (i.e. because its
264           * built-in constant integers which i.e. reflect some kind of status of           * value may change at runtime), then you should add it to
265           * the sampler. So rather see them as "read only" variables, not as           * builtInIntVariables() instead and use the macro
266           * being actually consistent in time.           * DECLARE_VMINT_READONLY() to define the variable for read-only
267             * access by scripts.
268             */
269            std::map<String,vmint> builtInConstIntVariables() OVERRIDE;
270    
271            /**
272             * Returns all built-in constant real number (floating point) script
273             * variables, which are constant and their final data is already
274             * available at parser time and won't change during runtime. Providing
275             * your built-in constants this way may lead to performance benefits
276             * compared to using other ways of providing built-in variables, because
277             * the script parser can perform optimizations when the script is
278             * refering to such constants.
279             *
280             * This type of built-in variable can only be read, but not be altered
281             * by scripts. This method returns a STL map, where the map's key is
282             * the variable name and the map's value is the final constant data.
283             *
284             * This method is re-implemented by deriving classes to add more use
285             * case specific built-in constant real numbers.
286           */           */
287          std::map<String,int> builtInConstIntVariables() OVERRIDE;          std::map<String,vmfloat> builtInConstRealVariables() OVERRIDE;
288    
289          /**          /**
290           * Returns all built-in dynamic variables. This method returns a STL           * Returns all built-in dynamic variables. This method returns a STL
# Line 222  namespace LinuxSampler { Line 298  namespace LinuxSampler {
298           */           */
299          std::map<String,VMDynVar*> builtInDynamicVariables() OVERRIDE;          std::map<String,VMDynVar*> builtInDynamicVariables() OVERRIDE;
300    
301            /**
302             * Enables or disables automatic suspension of scripts by the VM.
303             * If automatic suspension is enabled then scripts are monitored
304             * regarding their execution time and in case they are execution
305             * for too long, then they are automatically suspended by the VM for
306             * a certain amount of time in order to avoid any RT instablity
307             * issues caused by bugs in the script, i.e. endless while() loops
308             * or very large scripts.
309             *
310             * Automatic suspension is enabled by default due to the aimed
311             * real-time context of this virtual machine.
312             *
313             * @param b - true: enable auto suspension [default],
314             *            false: disable auto suspension
315             */
316            void setAutoSuspendEnabled(bool b = true);
317    
318            /**
319             * Returns true in case automatic suspension of scripts by the VM is
320             * enabled. See setAutoSuspendEnabled() for details.
321             *
322             * Automatic suspension is enabled by default due to the aimed
323             * real-time context of this virtual machine.
324             */
325            bool isAutoSuspendEnabled() const;
326    
327            /**
328             * By default (i.e. in production use) the built-in exit() function
329             * prohibits any arguments to be passed to its function by scripts. So
330             * by default, scripts trying to pass any arguments to the built-in
331             * exit() function will yield in a parser error.
332             *
333             * By calling this method the built-in exit() function will optionally
334             * accept one argument to be passed to its function call by scripts. The
335             * value of that function argument will become available by calling
336             * VMExecContext::exitResult() after execution of the script.
337             *
338             * @see VMExecContext::exitResult()
339             */
340            void setExitResultEnabled(bool b = true);
341    
342            /**
343             * Returns @c true if the built-in exit() function optionally accepts
344             * a function argument by scripts.
345             *
346             * @see setExitResultEnabled()
347             */
348            bool isExitResultEnabled() const;
349    
350          VMEventHandler* currentVMEventHandler(); //TODO: should be protected (only usable during exec() calls, intended only for VMFunctions)          VMEventHandler* currentVMEventHandler(); //TODO: should be protected (only usable during exec() calls, intended only for VMFunctions)
351          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)
352          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)
353    
354        private:
355            VMParserContext* loadScriptOnePass(const String& s);
356      protected:      protected:
357          VMEventHandler* m_eventHandler;          VMEventHandler* m_eventHandler;
358          ParserContext* m_parserContext;          ParserContext* m_parserContext;
359            bool m_autoSuspend;
360            bool m_acceptExitRes;
361          class CoreVMFunction_message* m_fnMessage;          class CoreVMFunction_message* m_fnMessage;
362          class CoreVMFunction_exit* m_fnExit;          class CoreVMFunction_exit* m_fnExit;
363          class CoreVMFunction_wait* m_fnWait;          class CoreVMFunction_wait* m_fnWait;
364          class CoreVMFunction_abs* m_fnAbs;          class CoreVMFunction_abs* m_fnAbs;
365          class CoreVMFunction_random* m_fnRandom;          class CoreVMFunction_random* m_fnRandom;
366          class CoreVMFunction_num_elements* m_fnNumElements;          class CoreVMFunction_num_elements* m_fnNumElements;
367            class CoreVMFunction_inc* m_fnInc;
368            class CoreVMFunction_dec* m_fnDec;
369            class CoreVMFunction_in_range* m_fnInRange;
370            class CoreVMFunction_sh_left* m_fnShLeft;
371            class CoreVMFunction_sh_right* m_fnShRight;
372            class CoreVMFunction_msb* m_fnMsb;
373            class CoreVMFunction_lsb* m_fnLsb;
374            class CoreVMFunction_min* m_fnMin;
375            class CoreVMFunction_max* m_fnMax;
376            class CoreVMFunction_array_equal* m_fnArrayEqual;
377            class CoreVMFunction_search* m_fnSearch;
378            class CoreVMFunction_sort* m_fnSort;
379            class CoreVMFunction_int_to_real* m_fnIntToReal;
380            class CoreVMFunction_real_to_int* m_fnRealToInt;
381            class CoreVMFunction_round* m_fnRound;
382            class CoreVMFunction_ceil* m_fnCeil;
383            class CoreVMFunction_floor* m_fnFloor;
384            class CoreVMFunction_sqrt* m_fnSqrt;
385            class CoreVMFunction_log* m_fnLog;
386            class CoreVMFunction_log2* m_fnLog2;
387            class CoreVMFunction_log10* m_fnLog10;
388            class CoreVMFunction_exp* m_fnExp;
389            class CoreVMFunction_pow* m_fnPow;
390            class CoreVMFunction_sin* m_fnSin;
391            class CoreVMFunction_cos* m_fnCos;
392            class CoreVMFunction_tan* m_fnTan;
393            class CoreVMFunction_asin* m_fnAsin;
394            class CoreVMFunction_acos* m_fnAcos;
395            class CoreVMFunction_atan* m_fnAtan;
396          class CoreVMDynVar_NKSP_REAL_TIMER* m_varRealTimer;          class CoreVMDynVar_NKSP_REAL_TIMER* m_varRealTimer;
397          class CoreVMDynVar_NKSP_PERF_TIMER* m_varPerfTimer;          class CoreVMDynVar_NKSP_PERF_TIMER* m_varPerfTimer;
398      };      };

Legend:
Removed from v.2942  
changed lines
  Added in v.3733

  ViewVC Help
Powered by ViewVC