1 |
/* |
2 |
* Copyright (c) 2014 Christian Schoenebeck |
3 |
* |
4 |
* http://www.linuxsampler.org |
5 |
* |
6 |
* This file is part of LinuxSampler and released under the same terms. |
7 |
* See README file for details. |
8 |
*/ |
9 |
|
10 |
#include "CoreVMFunctions.h" |
11 |
|
12 |
#include <iostream> |
13 |
#include <math.h> |
14 |
#include <stdlib.h> |
15 |
#include "tree.h" |
16 |
#include "ScriptVM.h" |
17 |
|
18 |
namespace LinuxSampler { |
19 |
|
20 |
VMFnResult* VMEmptyResultFunction::errorResult() { |
21 |
result.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED); |
22 |
return &result; |
23 |
} |
24 |
|
25 |
VMFnResult* VMEmptyResultFunction::successResult() { |
26 |
result.flags = STMT_SUCCESS; |
27 |
return &result; |
28 |
} |
29 |
|
30 |
VMFnResult* VMIntResultFunction::errorResult(int i) { |
31 |
result.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED); |
32 |
result.value = i; |
33 |
return &result; |
34 |
} |
35 |
|
36 |
VMFnResult* VMIntResultFunction::successResult(int i) { |
37 |
result.flags = STMT_SUCCESS; |
38 |
result.value = i; |
39 |
return &result; |
40 |
} |
41 |
|
42 |
VMFnResult* VMStringResultFunction::errorResult(const String& s) { |
43 |
result.flags = StmtFlags_t(STMT_ABORT_SIGNALLED | STMT_ERROR_OCCURRED); |
44 |
result.value = s; |
45 |
return &result; |
46 |
} |
47 |
|
48 |
VMFnResult* VMStringResultFunction::successResult(const String& s) { |
49 |
result.flags = STMT_SUCCESS; |
50 |
result.value = s; |
51 |
return &result; |
52 |
} |
53 |
|
54 |
bool CoreVMFunction_message::acceptsArgType(int iArg, ExprType_t type) const { |
55 |
return type == INT_EXPR || type == STRING_EXPR; |
56 |
} |
57 |
|
58 |
VMFnResult* CoreVMFunction_message::exec(VMFnArgs* args) { |
59 |
if (!args->argsCount()) return errorResult(); |
60 |
|
61 |
VMStringExpr* strExpr = dynamic_cast<VMStringExpr*>(args->arg(0)); |
62 |
if (strExpr) { |
63 |
std::cout << "[ScriptVM] " << strExpr->evalStr() << "\n"; |
64 |
return successResult(); |
65 |
} |
66 |
|
67 |
VMIntExpr* intExpr = dynamic_cast<VMIntExpr*>(args->arg(0)); |
68 |
if (intExpr) { |
69 |
std::cout << "[ScriptVM] " << intExpr->evalInt() << "\n"; |
70 |
return successResult(); |
71 |
} |
72 |
|
73 |
return errorResult(); |
74 |
} |
75 |
|
76 |
VMFnResult* CoreVMFunction_exit::exec(VMFnArgs* args) { |
77 |
this->result.flags = STMT_ABORT_SIGNALLED; |
78 |
return &result; |
79 |
} |
80 |
|
81 |
VMFnResult* CoreVMFunction_wait::exec(VMFnArgs* args) { |
82 |
ExecContext* ctx = dynamic_cast<ExecContext*>(vm->currentVMExecContext()); |
83 |
VMIntExpr* expr = dynamic_cast<VMIntExpr*>(args->arg(0)); |
84 |
ctx->suspendMicroseconds = expr->evalInt(); |
85 |
this->result.flags = STMT_SUSPEND_SIGNALLED; |
86 |
return &result; |
87 |
} |
88 |
|
89 |
bool CoreVMFunction_abs::acceptsArgType(int iArg, ExprType_t type) const { |
90 |
return type == INT_EXPR; |
91 |
} |
92 |
|
93 |
VMFnResult* CoreVMFunction_abs::exec(VMFnArgs* args) { |
94 |
return successResult( ::abs(args->arg(0)->asInt()->evalInt()) ); |
95 |
} |
96 |
|
97 |
bool CoreVMFunction_random::acceptsArgType(int iArg, ExprType_t type) const { |
98 |
return type == INT_EXPR; |
99 |
} |
100 |
|
101 |
VMFnResult* CoreVMFunction_random::exec(VMFnArgs* args) { |
102 |
int iMin = args->arg(0)->asInt()->evalInt(); |
103 |
int iMax = args->arg(1)->asInt()->evalInt(); |
104 |
float f = float(::random()) / float(RAND_MAX); |
105 |
return successResult( |
106 |
iMin + roundf( f * float(iMax - iMin) ) |
107 |
); |
108 |
} |
109 |
|
110 |
bool CoreVMFunction_num_elements::acceptsArgType(int iArg, ExprType_t type) const { |
111 |
return type == INT_ARR_EXPR; |
112 |
} |
113 |
|
114 |
VMFnResult* CoreVMFunction_num_elements::exec(VMFnArgs* args) { |
115 |
return successResult( args->arg(0)->asIntArray()->arraySize() ); |
116 |
} |
117 |
|
118 |
} // namespace LinuxSampler |