Cleanup
[carla.git] / source / modules / ysfx / sources / ysfx_api_eel.cpp
blob2e59b0dbb2e1b9958d55c0c0f3278be93495410f
1 // Copyright 2021 Jean Pierre Cimalando
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 // SPDX-License-Identifier: Apache-2.0
18 #include "ysfx.hpp"
19 #include "ysfx_api_eel.hpp"
20 #include "ysfx_utils.hpp"
21 #include <cstring>
22 #include <cstdlib>
23 #include <cstddef>
25 #include "WDL/ptrlist.h"
26 #include "WDL/assocarray.h"
27 #include "WDL/mutex.h"
29 #ifndef EELSCRIPT_NO_STDIO
30 # define EEL_STRING_STDOUT_WRITE(x,len) { fwrite(x,len,1,stdout); fflush(stdout); }
31 #endif
33 //TODO: thread-safety considerations with strings
35 #define EEL_STRING_MAXUSERSTRING_LENGTH_HINT ysfx_string_max_length
37 static ysfx::mutex atomic_mutex;
38 #define EEL_ATOMIC_SET_SCOPE(opaque) ysfx::mutex *mutex = ((opaque) ? &((ysfx_t *)(opaque))->atomic_mutex : &atomic_mutex);
39 #define EEL_ATOMIC_ENTER mutex->lock()
40 #define EEL_ATOMIC_LEAVE mutex->unlock()
42 #include "WDL/eel2/eel_strings.h"
43 #include "WDL/eel2/eel_misc.h"
44 #include "WDL/eel2/eel_fft.h"
45 #include "WDL/eel2/eel_mdct.h"
46 #include "WDL/eel2/eel_atomic.h"
48 //------------------------------------------------------------------------------
49 void ysfx_api_init_eel()
51 EEL_string_register();
52 EEL_fft_register();
53 EEL_mdct_register();
54 EEL_string_register();
55 EEL_misc_register();
56 EEL_atomic_register();
59 //------------------------------------------------------------------------------
60 void ysfx_eel_string_initvm(NSEEL_VMCTX vm)
62 eel_string_initvm(vm);
65 //------------------------------------------------------------------------------
66 eel_string_context_state *ysfx_eel_string_context_new()
68 return new eel_string_context_state;
71 void ysfx_eel_string_context_free(eel_string_context_state *state)
73 delete state;
76 void ysfx_eel_string_context_update_named_vars(eel_string_context_state *state, NSEEL_VMCTX vm)
78 state->update_named_vars(vm);
81 //------------------------------------------------------------------------------
82 static_assert(
83 ysfx_string_max_length == EEL_STRING_MAXUSERSTRING_LENGTH_HINT,
84 "string max lengths do not match");
86 bool ysfx_string_access(ysfx_t *fx, ysfx_real id, bool for_write, void (*access)(void *, WDL_FastString &), void *userdata)
88 void *opaque = fx;
89 eel_string_context_state *ctx = EEL_STRING_GET_CONTEXT_POINTER(opaque);
90 EEL_STRING_MUTEXLOCK_SCOPE
92 EEL_STRING_STORAGECLASS *wr = nullptr;
93 ctx->GetStringForIndex(id, &wr, for_write);
94 if (!wr)
95 return false;
97 access(userdata, *wr);
98 return true;
101 bool ysfx_string_get(ysfx_t *fx, ysfx_real id, std::string &txt)
103 return ysfx_string_access(fx, id, false, [](void *ud, WDL_FastString &str) {
104 ((std::string *)ud)->assign(str.Get(), (uint32_t)str.GetLength());
105 }, &txt);
108 bool ysfx_string_set(ysfx_t *fx, ysfx_real id, const std::string &txt)
110 return ysfx_string_access(fx, id, true, [](void *ud, WDL_FastString &str) {
111 const std::string *txt = (const std::string *)ud;
112 size_t size = txt->size();
113 if (size > ysfx_string_max_length)
114 size = ysfx_string_max_length;
115 str.SetRaw(txt->data(), (int)size);
116 }, (void *)&txt);
119 void ysfx_string_lock(ysfx_t *fx)
121 fx->string_mutex.lock();
124 void ysfx_string_unlock(ysfx_t *fx)
126 fx->string_mutex.unlock();
129 const char *ysfx_string_access_unlocked(ysfx_t *fx, ysfx_real id, WDL_FastString **fs, bool for_write)
131 return fx->string_ctx->GetStringForIndex(id, fs, for_write);
134 //------------------------------------------------------------------------------
135 // NOTE(jpc) implement this? I guess probably not.
136 // DSP and UI should not mutex each other.
138 void NSEEL_HOSTSTUB_EnterMutex()
142 void NSEEL_HOSTSTUB_LeaveMutex()