| 197 | { |
| 198 | std::vector<std::string> parameters; |
| 199 | StatementList statementList; |
| 200 | QiCustomFunc() = delete; |
| 201 | QiCustomFunc(const std::vector<std::string>& params, const StatementList& body) : parameters(params), statementList(body) {} |
| 202 | }; |
| 203 | |
| 204 | using QiCustomFuncMap = std::map<std::string, std::shared_ptr<const QiCustomFunc>>; |
| 205 | |
| 206 | static inline const QiFuncMap functions; |
| 207 | |
| 208 | static inline QiCustomFuncMap customFunctions; |
| 209 | static inline std::mutex customFunctionsMutex; |
| 210 | |
| 211 | static inline QiVarMap globalVariables; |
| 212 | static inline std::mutex globalVariablesMutex; |
| 213 | |
| 214 | static inline HANDLE initMutex = nullptr; |
| 215 | |
| 216 | std::mutex this_mutex; |
| 217 | QiVarMap localVariables; |
| 218 | std::mutex localVariablesMutex; |
| 219 | QiWorker* workerPtr{}; // single-threaded |
| 220 | Property propertys; // single-threaded |
| 221 | bool force_stop{}; |
| 222 | |
| 223 | template<class Parsed, class Parse> |
| 224 | static std::shared_ptr<const Parsed> cachedParse(const std::string& code, size_t maxEntries, size_t maxSourceBytes, size_t maxEntryBytes, Parse parse) |
| 225 | { |
| 226 | struct Cache |
| 227 | { |
| 228 | std::unordered_map<std::string, std::shared_ptr<const Parsed>> entries; |
| 229 | size_t sourceBytes = 0; |
| 230 | }; |
| 231 | static thread_local Cache cache; |
| 232 | auto found = cache.entries.find(code); |
| 233 | if (found != cache.entries.end()) return found->second; |
| 234 | auto parsed = std::make_shared<const Parsed>(parse()); |
| 235 | if (code.size() <= maxEntryBytes) |
| 236 | { |
| 237 | if (cache.entries.size() >= maxEntries || code.size() > maxSourceBytes - cache.sourceBytes) |
| 238 | { |
| 239 | cache.entries.clear(); |
| 240 | cache.sourceBytes = 0; |
| 241 | } |
| 242 | cache.entries.emplace(code, parsed); |
| 243 | cache.sourceBytes += code.size(); |
| 244 | } |
| 245 | return parsed; |
| 246 | } |
| 247 | std::shared_ptr<const std::vector<Token>> compiledExpression(const std::string& code) |
| 248 | { |
| 249 | return cachedParse<std::vector<Token>>(code, 256, 256 * 1024, 16 * 1024, [&] { return infixToPostfix(tokenize(code)); }); |
| 250 | } |
| 251 | |
| 252 | auto trim(const std::string& s) -> std::string |
| 253 | { |
| 254 | auto start = s.find_first_not_of(" \t"); |
| 255 | auto end = s.find_last_not_of(" \t"); |
| 256 | return (start == std::string::npos) ? "" : s.substr(start, end - start + 1); |