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

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

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

revision 3551 by schoenebeck, Thu Aug 1 10:22:56 2019 UTC revision 3564 by schoenebeck, Sat Aug 24 09:18:57 2019 UTC
# Line 35  VMFnResult* VMEmptyResultFunction::succe Line 35  VMFnResult* VMEmptyResultFunction::succe
35  ///////////////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////////////
36  // class VMIntResultFunction  // class VMIntResultFunction
37    
38  VMFnResult* VMIntResultFunction::errorResult(int i) {  VMFnResult* VMIntResultFunction::errorResult(vmint i) {
39      result.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED);      result.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED);
40      result.value = i;      result.value = i;
41      return &result;      return &result;
42  }  }
43    
44  VMFnResult* VMIntResultFunction::successResult(int i) {  VMFnResult* VMIntResultFunction::successResult(vmint i) {
45      result.flags = STMT_SUCCESS;      result.flags = STMT_SUCCESS;
46      result.value = i;      result.value = i;
47      return &result;      return &result;
# Line 65  VMFnResult* VMStringResultFunction::succ Line 65  VMFnResult* VMStringResultFunction::succ
65  ///////////////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////////////
66  // built-in script function:  message()  // built-in script function:  message()
67    
68  bool CoreVMFunction_message::acceptsArgType(int iArg, ExprType_t type) const {  bool CoreVMFunction_message::acceptsArgType(vmint iArg, ExprType_t type) const {
69      return type == INT_EXPR || type == STRING_EXPR;      return type == INT_EXPR || type == STRING_EXPR;
70  }  }
71    
# Line 82  VMFnResult* CoreVMFunction_message::exec Line 82  VMFnResult* CoreVMFunction_message::exec
82    
83      VMIntExpr* intExpr = dynamic_cast<VMIntExpr*>(args->arg(0));      VMIntExpr* intExpr = dynamic_cast<VMIntExpr*>(args->arg(0));
84      if (intExpr) {      if (intExpr) {
85          printf("[ScriptVM %.3f] %d\n", usecs/1000000.f, intExpr->evalInt());          printf("[ScriptVM %.3f] %lld\n", usecs/1000000.f, (int64_t)intExpr->evalInt());
86          return successResult();          return successResult();
87      }      }
88    
# Line 92  VMFnResult* CoreVMFunction_message::exec Line 92  VMFnResult* CoreVMFunction_message::exec
92  ///////////////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////////////
93  // built-in script function:  exit()  // built-in script function:  exit()
94    
95  int CoreVMFunction_exit::maxAllowedArgs() const {  vmint CoreVMFunction_exit::maxAllowedArgs() const {
96      return (vm->isExitResultEnabled()) ? 1 : 0;      return (vm->isExitResultEnabled()) ? 1 : 0;
97  }  }
98    
99  bool CoreVMFunction_exit::acceptsArgType(int iArg, ExprType_t type) const {  bool CoreVMFunction_exit::acceptsArgType(vmint iArg, ExprType_t type) const {
100      if (!vm->isExitResultEnabled()) return false;      if (!vm->isExitResultEnabled()) return false;
101      return type == INT_EXPR || type == STRING_EXPR;      return type == INT_EXPR || type == STRING_EXPR;
102  }  }
# Line 124  VMFnResult* CoreVMFunction_exit::exec(VM Line 124  VMFnResult* CoreVMFunction_exit::exec(VM
124  ///////////////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////////////
125  // built-in script function:  wait()  // built-in script function:  wait()
126    
127    bool CoreVMFunction_wait::acceptsArgUnitType(vmint iArg, StdUnit_t type) const {
128        if (iArg == 0)
129            return type == VM_NO_UNIT || type == VM_SECOND;
130        else
131            return type == VM_NO_UNIT;
132    }
133    
134    bool CoreVMFunction_wait::acceptsArgUnitPrefix(vmint iArg, StdUnit_t type) const {
135        return iArg == 0 && type == VM_SECOND;
136    }
137    
138  VMFnResult* CoreVMFunction_wait::exec(VMFnArgs* args) {  VMFnResult* CoreVMFunction_wait::exec(VMFnArgs* args) {
139      ExecContext* ctx = dynamic_cast<ExecContext*>(vm->currentVMExecContext());      ExecContext* ctx = dynamic_cast<ExecContext*>(vm->currentVMExecContext());
140      VMIntExpr* expr = dynamic_cast<VMIntExpr*>(args->arg(0));      VMIntExpr* expr = dynamic_cast<VMIntExpr*>(args->arg(0));
141      int us = expr->evalInt();      StdUnit_t unit = expr->unitType();
142        vmint us = (unit) ? expr->evalInt(VM_MICRO) : expr->evalInt();
143      if (us < 0) {      if (us < 0) {
144          wrnMsg("wait(): argument may not be negative! Aborting script!");          wrnMsg("wait(): argument may not be negative! Aborting script!");
145          this->result.flags = STMT_ABORT_SIGNALLED;          this->result.flags = STMT_ABORT_SIGNALLED;
# Line 144  VMFnResult* CoreVMFunction_wait::exec(VM Line 156  VMFnResult* CoreVMFunction_wait::exec(VM
156  ///////////////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////////////
157  // built-in script function:  abs()  // built-in script function:  abs()
158    
159  bool CoreVMFunction_abs::acceptsArgType(int iArg, ExprType_t type) const {  bool CoreVMFunction_abs::acceptsArgType(vmint iArg, ExprType_t type) const {
160      return type == INT_EXPR;      return type == INT_EXPR;
161  }  }
162    
# Line 155  VMFnResult* CoreVMFunction_abs::exec(VMF Line 167  VMFnResult* CoreVMFunction_abs::exec(VMF
167  ///////////////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////////////
168  // built-in script function:  random()  // built-in script function:  random()
169    
170  bool CoreVMFunction_random::acceptsArgType(int iArg, ExprType_t type) const {  bool CoreVMFunction_random::acceptsArgType(vmint iArg, ExprType_t type) const {
171      return type == INT_EXPR;      return type == INT_EXPR;
172  }  }
173    
174  VMFnResult* CoreVMFunction_random::exec(VMFnArgs* args) {  VMFnResult* CoreVMFunction_random::exec(VMFnArgs* args) {
175      int iMin = args->arg(0)->asInt()->evalInt();      vmint iMin = args->arg(0)->asInt()->evalInt();
176      int iMax = args->arg(1)->asInt()->evalInt();      vmint iMax = args->arg(1)->asInt()->evalInt();
177      float f = float(::rand()) / float(RAND_MAX);      float f = float(::rand()) / float(RAND_MAX);
178      return successResult(      return successResult(
179          iMin + roundf( f * float(iMax - iMin) )          iMin + roundf( f * float(iMax - iMin) )
# Line 171  VMFnResult* CoreVMFunction_random::exec( Line 183  VMFnResult* CoreVMFunction_random::exec(
183  ///////////////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////////////
184  // built-in script function:  num_elements()  // built-in script function:  num_elements()
185    
186  bool CoreVMFunction_num_elements::acceptsArgType(int iArg, ExprType_t type) const {  bool CoreVMFunction_num_elements::acceptsArgType(vmint iArg, ExprType_t type) const {
187      return type == INT_ARR_EXPR;      return type == INT_ARR_EXPR;
188  }  }
189    
# Line 187  VMFnResult* CoreVMFunction_inc::exec(VMF Line 199  VMFnResult* CoreVMFunction_inc::exec(VMF
199      VMIntExpr* in = dynamic_cast<VMIntExpr*>(arg);      VMIntExpr* in = dynamic_cast<VMIntExpr*>(arg);
200      VMVariable* out = dynamic_cast<VMVariable*>(arg);      VMVariable* out = dynamic_cast<VMVariable*>(arg);
201      if (!in || !out) successResult(0);      if (!in || !out) successResult(0);
202      int i = in->evalInt() + 1;      vmint i = in->evalInt() + 1;
203      IntLiteral tmp(i);      IntLiteral tmp(i);
204      out->assignExpr(&tmp);      out->assignExpr(&tmp);
205      return successResult(i);      return successResult(i);
# Line 201  VMFnResult* CoreVMFunction_dec::exec(VMF Line 213  VMFnResult* CoreVMFunction_dec::exec(VMF
213      VMIntExpr* in = dynamic_cast<VMIntExpr*>(arg);      VMIntExpr* in = dynamic_cast<VMIntExpr*>(arg);
214      VMVariable* out = dynamic_cast<VMVariable*>(arg);      VMVariable* out = dynamic_cast<VMVariable*>(arg);
215      if (!in || !out) successResult(0);      if (!in || !out) successResult(0);
216      int i = in->evalInt() - 1;      vmint i = in->evalInt() - 1;
217      IntLiteral tmp(i);      IntLiteral tmp(i);
218      out->assignExpr(&tmp);      out->assignExpr(&tmp);
219      return successResult(i);      return successResult(i);
# Line 211  VMFnResult* CoreVMFunction_dec::exec(VMF Line 223  VMFnResult* CoreVMFunction_dec::exec(VMF
223  // built-in script function:  in_range()  // built-in script function:  in_range()
224    
225  VMFnResult* CoreVMFunction_in_range::exec(VMFnArgs* args) {  VMFnResult* CoreVMFunction_in_range::exec(VMFnArgs* args) {
226      int i  = args->arg(0)->asInt()->evalInt();      vmint i  = args->arg(0)->asInt()->evalInt();
227      int lo = args->arg(1)->asInt()->evalInt();      vmint lo = args->arg(1)->asInt()->evalInt();
228      int hi = args->arg(2)->asInt()->evalInt();      vmint hi = args->arg(2)->asInt()->evalInt();
229      if (lo > hi) { // swap lo and hi      if (lo > hi) { // swap lo and hi
230          int tmp = lo;          vmint tmp = lo;
231          lo = hi;          lo = hi;
232          hi = tmp;          hi = tmp;
233      }      }
# Line 226  VMFnResult* CoreVMFunction_in_range::exe Line 238  VMFnResult* CoreVMFunction_in_range::exe
238  // built-in script function:  sh_left()  // built-in script function:  sh_left()
239    
240  VMFnResult* CoreVMFunction_sh_left::exec(VMFnArgs* args) {  VMFnResult* CoreVMFunction_sh_left::exec(VMFnArgs* args) {
241      int i = args->arg(0)->asInt()->evalInt();      vmint i = args->arg(0)->asInt()->evalInt();
242      int n = args->arg(1)->asInt()->evalInt();      vmint n = args->arg(1)->asInt()->evalInt();
243      return successResult(i << n);      return successResult(i << n);
244  }  }
245    
# Line 235  VMFnResult* CoreVMFunction_sh_left::exec Line 247  VMFnResult* CoreVMFunction_sh_left::exec
247  // built-in script function:  sh_right()  // built-in script function:  sh_right()
248    
249  VMFnResult* CoreVMFunction_sh_right::exec(VMFnArgs* args) {  VMFnResult* CoreVMFunction_sh_right::exec(VMFnArgs* args) {
250      int i = args->arg(0)->asInt()->evalInt();      vmint i = args->arg(0)->asInt()->evalInt();
251      int n = args->arg(1)->asInt()->evalInt();      vmint n = args->arg(1)->asInt()->evalInt();
252      return successResult(i >> n);      return successResult(i >> n);
253  }  }
254    
# Line 244  VMFnResult* CoreVMFunction_sh_right::exe Line 256  VMFnResult* CoreVMFunction_sh_right::exe
256  // built-in script function:  min()  // built-in script function:  min()
257    
258  VMFnResult* CoreVMFunction_min::exec(VMFnArgs* args) {  VMFnResult* CoreVMFunction_min::exec(VMFnArgs* args) {
259      int l = args->arg(0)->asInt()->evalInt();      vmint l = args->arg(0)->asInt()->evalInt();
260      int r = args->arg(1)->asInt()->evalInt();      vmint r = args->arg(1)->asInt()->evalInt();
261      return successResult(l < r ? l : r);      return successResult(l < r ? l : r);
262  }  }
263    
# Line 253  VMFnResult* CoreVMFunction_min::exec(VMF Line 265  VMFnResult* CoreVMFunction_min::exec(VMF
265  // built-in script function:  max()  // built-in script function:  max()
266    
267  VMFnResult* CoreVMFunction_max::exec(VMFnArgs* args) {  VMFnResult* CoreVMFunction_max::exec(VMFnArgs* args) {
268      int l = args->arg(0)->asInt()->evalInt();      vmint l = args->arg(0)->asInt()->evalInt();
269      int r = args->arg(1)->asInt()->evalInt();      vmint r = args->arg(1)->asInt()->evalInt();
270      return successResult(l > r ? l : r);      return successResult(l > r ? l : r);
271  }  }
272    
# Line 268  VMFnResult* CoreVMFunction_array_equal:: Line 280  VMFnResult* CoreVMFunction_array_equal::
280          wrnMsg("array_equal(): the two arrays differ in size");          wrnMsg("array_equal(): the two arrays differ in size");
281          return successResult(0); // false          return successResult(0); // false
282      }      }
283      const int n = l->arraySize();      const vmint n = l->arraySize();
284      for (int i = 0; i < n; ++i)      for (vmint i = 0; i < n; ++i)
285          if (l->evalIntElement(i) != r->evalIntElement(i))          if (l->evalIntElement(i) != r->evalIntElement(i))
286              return successResult(0); // false              return successResult(0); // false
287      return successResult(1); // true      return successResult(1); // true
# Line 278  VMFnResult* CoreVMFunction_array_equal:: Line 290  VMFnResult* CoreVMFunction_array_equal::
290  ///////////////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////////////
291  // built-in script function:  search()  // built-in script function:  search()
292    
293  ExprType_t CoreVMFunction_search::argType(int iArg) const {  ExprType_t CoreVMFunction_search::argType(vmint iArg) const {
294      return (iArg == 0) ? INT_ARR_EXPR : INT_EXPR;      return (iArg == 0) ? INT_ARR_EXPR : INT_EXPR;
295  }  }
296    
297  bool CoreVMFunction_search::acceptsArgType(int iArg, ExprType_t type) const {  bool CoreVMFunction_search::acceptsArgType(vmint iArg, ExprType_t type) const {
298      if (iArg == 0)      if (iArg == 0)
299          return type == INT_ARR_EXPR;          return type == INT_ARR_EXPR;
300      else      else
# Line 291  bool CoreVMFunction_search::acceptsArgTy Line 303  bool CoreVMFunction_search::acceptsArgTy
303    
304  VMFnResult* CoreVMFunction_search::exec(VMFnArgs* args) {  VMFnResult* CoreVMFunction_search::exec(VMFnArgs* args) {
305      VMIntArrayExpr* a = args->arg(0)->asIntArray();      VMIntArrayExpr* a = args->arg(0)->asIntArray();
306      const int needle = args->arg(1)->asInt()->evalInt();      const vmint needle = args->arg(1)->asInt()->evalInt();
307      const int n = a->arraySize();      const vmint n = a->arraySize();
308      for (int i = 0; i < n; ++i)      for (vmint i = 0; i < n; ++i)
309          if (a->evalIntElement(i) == needle)          if (a->evalIntElement(i) == needle)
310              return successResult(i);              return successResult(i);
311      return successResult(-1); // not found      return successResult(-1); // not found
# Line 302  VMFnResult* CoreVMFunction_search::exec( Line 314  VMFnResult* CoreVMFunction_search::exec(
314  ///////////////////////////////////////////////////////////////////////////  ///////////////////////////////////////////////////////////////////////////
315  // built-in script function:  sort()  // built-in script function:  sort()
316    
317  ExprType_t CoreVMFunction_sort::argType(int iArg) const {  ExprType_t CoreVMFunction_sort::argType(vmint iArg) const {
318      return (iArg == 0) ? INT_ARR_EXPR : INT_EXPR;      return (iArg == 0) ? INT_ARR_EXPR : INT_EXPR;
319  }  }
320    
321  bool CoreVMFunction_sort::acceptsArgType(int iArg, ExprType_t type) const {  bool CoreVMFunction_sort::acceptsArgType(vmint iArg, ExprType_t type) const {
322      if (iArg == 0)      if (iArg == 0)
323          return type == INT_ARR_EXPR;          return type == INT_ARR_EXPR;
324      else      else
# Line 315  bool CoreVMFunction_sort::acceptsArgType Line 327  bool CoreVMFunction_sort::acceptsArgType
327    
328  struct ArrElemPOD {  struct ArrElemPOD {
329      VMIntArrayExpr* m_array;      VMIntArrayExpr* m_array;
330      int m_index;      vmint m_index;
331  };  };
332    
333  static inline void swap(class ArrElemRef a, class ArrElemRef b);  static inline void swap(class ArrElemRef a, class ArrElemRef b);
# Line 326  public: Line 338  public:
338          m_array = NULL;          m_array = NULL;
339          m_index = 0;          m_index = 0;
340      }      }
341      ArrElemRef(VMIntArrayExpr* a, int index) {      ArrElemRef(VMIntArrayExpr* a, vmint index) {
342          m_array = a;          m_array = a;
343          m_index = index;          m_index = index;
344      }      }
# Line 334  public: Line 346  public:
346          setValue(e.getValue());          setValue(e.getValue());
347          return *this;          return *this;
348      }      }
349      inline ArrElemRef& operator=(int val) {      inline ArrElemRef& operator=(vmint val) {
350          setValue(val);          setValue(val);
351          return *this;          return *this;
352      }      }
# Line 343  public: Line 355  public:
355              return true;              return true;
356          return getValue() == e.getValue();          return getValue() == e.getValue();
357      }      }
358      inline bool operator==(int val) const {      inline bool operator==(vmint val) const {
359          return getValue() == val;          return getValue() == val;
360      }      }
361      inline bool operator!=(const ArrElemRef& e) const {      inline bool operator!=(const ArrElemRef& e) const {
362          return !(operator==(e));          return !(operator==(e));
363      }      }
364      inline bool operator!=(int val) const {      inline bool operator!=(vmint val) const {
365          return !(operator==(val));          return !(operator==(val));
366      }      }
367      inline bool operator<(const ArrElemRef& e) const {      inline bool operator<(const ArrElemRef& e) const {
# Line 357  public: Line 369  public:
369              return false;              return false;
370          return getValue() < e.getValue();          return getValue() < e.getValue();
371      }      }
372      inline bool operator<(int val) const {      inline bool operator<(vmint val) const {
373          return getValue() < val;          return getValue() < val;
374      }      }
375      inline bool operator>(const ArrElemRef& e) const {      inline bool operator>(const ArrElemRef& e) const {
# Line 365  public: Line 377  public:
377              return false;              return false;
378          return getValue() > e.getValue();          return getValue() > e.getValue();
379      }      }
380      inline bool operator>(int val) const {      inline bool operator>(vmint val) const {
381          return getValue() > val;          return getValue() > val;
382      }      }
383      inline bool operator<=(const ArrElemRef& e) const {      inline bool operator<=(const ArrElemRef& e) const {
# Line 373  public: Line 385  public:
385              return true;              return true;
386          return getValue() <= e.getValue();          return getValue() <= e.getValue();
387      }      }
388      inline bool operator<=(int val) const {      inline bool operator<=(vmint val) const {
389          return getValue() <= val;          return getValue() <= val;
390      }      }
391      inline bool operator>=(const ArrElemRef& e) const {      inline bool operator>=(const ArrElemRef& e) const {
# Line 381  public: Line 393  public:
393              return true;              return true;
394          return getValue() >= e.getValue();          return getValue() >= e.getValue();
395      }      }
396      inline bool operator>=(int val) const {      inline bool operator>=(vmint val) const {
397          return getValue() >= val;          return getValue() >= val;
398      }      }
399      inline operator int() const {      inline operator vmint() const {
400          return getValue();          return getValue();
401      }      }
402  protected:  protected:
403      inline int getValue() const {      inline vmint getValue() const {
404          return m_array->evalIntElement(m_index);          return m_array->evalIntElement(m_index);
405      }      }
406      inline void setValue(int value) {      inline void setValue(vmint value) {
407          m_array->assignIntElement(m_index, value);          m_array->assignIntElement(m_index, value);
408      }      }
409    
# Line 404  public: Line 416  public:
416          m_array = NULL;          m_array = NULL;
417          m_index = 0;          m_index = 0;
418      }      }
419      ArrElemPtr(VMIntArrayExpr* a, int index) {      ArrElemPtr(VMIntArrayExpr* a, vmint index) {
420          m_array = a;          m_array = a;
421          m_index = index;          m_index = index;
422      }      }
# Line 414  public: Line 426  public:
426  };  };
427    
428  static inline void swap(ArrElemRef a, ArrElemRef b) {  static inline void swap(ArrElemRef a, ArrElemRef b) {
429      int valueA = a.getValue();      vmint valueA = a.getValue();
430      int valueB = b.getValue();      vmint valueB = b.getValue();
431      a.setValue(valueB);      a.setValue(valueB);
432      b.setValue(valueA);      b.setValue(valueA);
433  }  }
# Line 423  static inline void swap(ArrElemRef a, Ar Line 435  static inline void swap(ArrElemRef a, Ar
435  class ArrExprIter : public ArrElemPOD {  class ArrExprIter : public ArrElemPOD {
436  public:  public:
437      typedef std::random_access_iterator_tag iterator_category;      typedef std::random_access_iterator_tag iterator_category;
438      typedef int value_type;      typedef vmint value_type;
439      typedef ssize_t difference_type;      typedef ssize_t difference_type;
440      typedef ArrElemPtr pointer;      typedef ArrElemPtr pointer;
441      typedef ArrElemRef reference;      typedef ArrElemRef reference;
442    
443      ArrExprIter(VMIntArrayExpr* a, int index) {      ArrExprIter(VMIntArrayExpr* a, vmint index) {
444          m_array = a;          m_array = a;
445          m_index = index;          m_index = index;
446      }      }
# Line 497  public: Line 509  public:
509  };  };
510    
511  struct DescArrExprSorter {  struct DescArrExprSorter {
512      inline bool operator()(const int& a, const int& b) const {      inline bool operator()(const vmint& a, const vmint& b) const {
513          return a > b;          return a > b;
514      }      }
515  };  };
# Line 506  VMFnResult* CoreVMFunction_sort::exec(VM Line 518  VMFnResult* CoreVMFunction_sort::exec(VM
518      VMIntArrayExpr* a = args->arg(0)->asIntArray();      VMIntArrayExpr* a = args->arg(0)->asIntArray();
519      bool bAscending =      bool bAscending =
520          (args->argsCount() < 2) ? true : !args->arg(1)->asInt()->evalInt();          (args->argsCount() < 2) ? true : !args->arg(1)->asInt()->evalInt();
521      int n = a->arraySize();      vmint n = a->arraySize();
522      ArrExprIter itBegin(a, 0);      ArrExprIter itBegin(a, 0);
523      ArrExprIter itEnd(a, n);      ArrExprIter itEnd(a, n);
524      if (bAscending) {      if (bAscending) {

Legend:
Removed from v.3551  
changed lines
  Added in v.3564

  ViewVC Help
Powered by ViewVC