ysfx_api_eel.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2021 Jean Pierre Cimalando
  2. // Copyright 2024 Joep Vanlier
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. // Modifications by Joep Vanlier, 2024
  17. //
  18. // SPDX-License-Identifier: Apache-2.0
  19. //
  20. #include "ysfx.hpp"
  21. #include "ysfx_api_eel.hpp"
  22. #include "ysfx_utils.hpp"
  23. #include <cstring>
  24. #include <cstdlib>
  25. #include <cstddef>
  26. #include "WDL/ptrlist.h"
  27. #include "WDL/assocarray.h"
  28. #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
  32. //TODO: thread-safety considerations with strings
  33. #define EEL_STRING_MAXUSERSTRING_LENGTH_HINT ysfx_string_max_length
  34. static ysfx::mutex atomic_mutex;
  35. #define EEL_ATOMIC_SET_SCOPE(opaque) ysfx::mutex *mutex = ((opaque) ? &((ysfx_t *)(opaque))->atomic_mutex : &atomic_mutex);
  36. #define EEL_ATOMIC_ENTER mutex->lock()
  37. #define EEL_ATOMIC_LEAVE mutex->unlock()
  38. #include "WDL/eel2/eel_strings.h"
  39. #include "WDL/eel2/eel_misc.h"
  40. #include "WDL/eel2/eel_fft.h"
  41. #include "WDL/eel2/eel_mdct.h"
  42. #include "WDL/eel2/eel_atomic.h"
  43. //------------------------------------------------------------------------------
  44. void ysfx_api_init_eel()
  45. {
  46. EEL_string_register();
  47. EEL_fft_register();
  48. EEL_mdct_register();
  49. EEL_string_register();
  50. EEL_misc_register();
  51. EEL_atomic_register();
  52. }
  53. //------------------------------------------------------------------------------
  54. void ysfx_eel_string_initvm(NSEEL_VMCTX vm)
  55. {
  56. eel_string_initvm(vm);
  57. }
  58. //------------------------------------------------------------------------------
  59. eel_string_context_state *ysfx_eel_string_context_new()
  60. {
  61. return new eel_string_context_state;
  62. }
  63. void ysfx_eel_string_context_free(eel_string_context_state *state)
  64. {
  65. delete state;
  66. }
  67. void ysfx_eel_string_context_update_named_vars(eel_string_context_state *state, NSEEL_VMCTX vm)
  68. {
  69. state->update_named_vars(vm);
  70. }
  71. //------------------------------------------------------------------------------
  72. static_assert(
  73. ysfx_string_max_length == EEL_STRING_MAXUSERSTRING_LENGTH_HINT,
  74. "string max lengths do not match");
  75. bool ysfx_string_access(ysfx_t *fx, ysfx_real id, bool for_write, void (*access)(void *, WDL_FastString &), void *userdata)
  76. {
  77. void *opaque = fx;
  78. eel_string_context_state *ctx = EEL_STRING_GET_CONTEXT_POINTER(opaque);
  79. EEL_STRING_MUTEXLOCK_SCOPE
  80. EEL_STRING_STORAGECLASS *wr = nullptr;
  81. ctx->GetStringForIndex(id, &wr, for_write);
  82. if (!wr)
  83. return false;
  84. access(userdata, *wr);
  85. return true;
  86. }
  87. bool ysfx_string_get(ysfx_t *fx, ysfx_real id, std::string &txt)
  88. {
  89. return ysfx_string_access(fx, id, false, [](void *ud, WDL_FastString &str) {
  90. ((std::string *)ud)->assign(str.Get(), (uint32_t)str.GetLength());
  91. }, &txt);
  92. }
  93. bool ysfx_string_set(ysfx_t *fx, ysfx_real id, const std::string &txt)
  94. {
  95. return ysfx_string_access(fx, id, true, [](void *ud, WDL_FastString &str) {
  96. const std::string *txt = (const std::string *)ud;
  97. size_t size = txt->size();
  98. if (size > ysfx_string_max_length)
  99. size = ysfx_string_max_length;
  100. str.SetRaw(txt->data(), (int)size);
  101. }, (void *)&txt);
  102. }
  103. void ysfx_string_lock(ysfx_t *fx)
  104. {
  105. fx->string_mutex.lock();
  106. }
  107. void ysfx_string_unlock(ysfx_t *fx)
  108. {
  109. fx->string_mutex.unlock();
  110. }
  111. void ysfx_image_lock(ysfx_t *fx)
  112. {
  113. fx->image_mutex.lock();
  114. }
  115. void ysfx_image_unlock(ysfx_t *fx)
  116. {
  117. fx->image_mutex.unlock();
  118. }
  119. const char *ysfx_string_access_unlocked(ysfx_t *fx, ysfx_real id, WDL_FastString **fs, bool for_write)
  120. {
  121. return fx->string_ctx->GetStringForIndex(id, fs, for_write);
  122. }
  123. //------------------------------------------------------------------------------
  124. // NOTE(jpc) implement this? I guess probably not.
  125. // DSP and UI should not mutex each other.
  126. void NSEEL_HOSTSTUB_EnterMutex()
  127. {
  128. }
  129. void NSEEL_HOSTSTUB_LeaveMutex()
  130. {
  131. }