VectorAdapter.cpp 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  1. #include "VectorAdapter.h"
  2. #include "../ScriptEngine.h"
  3. #include "WallpaperEngine/Data/Utils/SFINAE.h"
  4. #include "WallpaperEngine/Data/Utils/ScopeGuard.h"
  5. #include <variant>
  6. using namespace WallpaperEngine::Data::Utils;
  7. using namespace WallpaperEngine::Data::Model;
  8. using namespace WallpaperEngine::Scripting::Adapters;
  9. static uint32_t VectorInstanceId = 0;
  10. static uint32_t VectorAdapterInstanceId = 0;
  11. static constexpr int InvalidVectorInstanceId = 0;
  12. // magic value used to ensure the assigned opaque value we got back is valid
  13. #define VEC_OPAQUE_MAGIC 0xdeadbee0
  14. #define VEC_MAGIC_CHECK_EXCEPTION(container, components) \
  15. do { \
  16. if (!container || container->magic != (int)(VEC_OPAQUE_MAGIC + components)) { \
  17. return JS_EXCEPTION; \
  18. } \
  19. } while (0)
  20. #define VEC_MAGIC_CHECK_ERROR(container, components) \
  21. do { \
  22. if (!container || container->magic != (int)(VEC_OPAQUE_MAGIC + components)) { \
  23. return -1; \
  24. } \
  25. } while (0)
  26. template <int components> std::map<uint32_t, VectorAdapter<components>&> vectorAdapterInstances;
  27. template <int components> struct VectorOpaqueContainer {
  28. int magic;
  29. VectorAdapter<components>& adapter;
  30. DynamicValue& value;
  31. uint32_t id;
  32. };
  33. template <int components> auto vector_new () -> decltype (auto) {
  34. static_assert (components >= 2 && components <= 4, "Unsupported vector type");
  35. if constexpr (components == 2) {
  36. return glm::vec2 {};
  37. } else if constexpr (components == 3) {
  38. return glm::vec3 {};
  39. } else if constexpr (components == 4) {
  40. return glm::vec4 {};
  41. }
  42. }
  43. template auto vector_new<2> () -> decltype (auto);
  44. template auto vector_new<3> () -> decltype (auto);
  45. template auto vector_new<4> () -> decltype (auto);
  46. template <int components> auto vector_new (float value) -> decltype (auto) {
  47. static_assert (components >= 2 && components <= 4, "Unsupported vector type");
  48. if constexpr (components == 2) {
  49. return glm::vec2 (value);
  50. } else if constexpr (components == 3) {
  51. return glm::vec3 (value);
  52. } else if constexpr (components == 4) {
  53. return glm::vec4 (value);
  54. }
  55. }
  56. template auto vector_new<2> (float value) -> decltype (auto);
  57. template auto vector_new<3> (float value) -> decltype (auto);
  58. template auto vector_new<4> (float value) -> decltype (auto);
  59. template <int components> auto vector_get (DynamicValue& value) -> decltype (auto) {
  60. static_assert (components >= 2 && components <= 4, "Unsupported vector type");
  61. if constexpr (components == 2) {
  62. return value.getVec2 ();
  63. } else if constexpr (components == 3) {
  64. return value.getVec3 ();
  65. } else if constexpr (components == 4) {
  66. return value.getVec4 ();
  67. }
  68. }
  69. template <int components> auto vector_get (JSContext* ctx, JSValue source) -> decltype (auto) {
  70. static_assert (components >= 2 && components <= 4, "Unsupported vector type");
  71. int tag = JS_VALUE_GET_TAG (source);
  72. if (tag == JS_TAG_INT) {
  73. int32_t value = 0;
  74. JS_ToInt32 (ctx, &value, source);
  75. return vector_new<components> (value);
  76. }
  77. if (JS_TAG_IS_FLOAT64 (tag)) {
  78. double value = 0.0f;
  79. JS_ToFloat64 (ctx, &value, source);
  80. return vector_new<components> (static_cast<float> (value));
  81. }
  82. if (tag == JS_TAG_OBJECT) {
  83. // check components, extract x, y, z and w and create the appropriate vector
  84. JSValue x = JS_GetPropertyStr (ctx, source, "x");
  85. JSValue y = JS_GetPropertyStr (ctx, source, "y");
  86. JSValue z = JS_GetPropertyStr (ctx, source, "z");
  87. JSValue w = JS_GetPropertyStr (ctx, source, "w");
  88. if (!JS_IsNumber (x) || !JS_IsNumber (y)) {
  89. throw std::runtime_error ("Unsupported type conversion for VectorAdapter");
  90. }
  91. // do not accept bigger vectors
  92. if (components <= 2 && JS_IsNumber (z)) {
  93. throw std::runtime_error ("Unsupported type conversion for VectorAdapter");
  94. }
  95. if (components <= 3 && JS_IsNumber (w)) {
  96. throw std::runtime_error ("Unsupported type conversion for VectorAdapter");
  97. }
  98. double xVal = 0.0f, yVal = 0.0f, zVal = 0.0f, wVal = 0.0f;
  99. JS_ToFloat64 (ctx, &xVal, x);
  100. JS_ToFloat64 (ctx, &yVal, y);
  101. if (JS_IsNumber (z)) {
  102. JS_ToFloat64 (ctx, &zVal, z);
  103. }
  104. if (JS_IsNumber (w)) {
  105. JS_ToFloat64 (ctx, &wVal, w);
  106. }
  107. if constexpr (components == 2) {
  108. return glm::vec2 (xVal, yVal);
  109. } else if constexpr (components == 3) {
  110. return glm::vec3 (xVal, yVal, zVal);
  111. } else if constexpr (components == 4) {
  112. return glm::vec4 (xVal, yVal, zVal, wVal);
  113. }
  114. }
  115. throw std::runtime_error ("Unsupported type conversion for VectorAdapter");
  116. }
  117. template auto vector_get<2> (JSContext* ctx, JSValue source) -> decltype (auto);
  118. template auto vector_get<3> (JSContext* ctx, JSValue source) -> decltype (auto);
  119. template auto vector_get<4> (JSContext* ctx, JSValue source) -> decltype (auto);
  120. template auto vector_get<2> (DynamicValue& value) -> decltype (auto);
  121. template auto vector_get<3> (DynamicValue& value) -> decltype (auto);
  122. template auto vector_get<4> (DynamicValue& value) -> decltype (auto);
  123. template <int components>
  124. JSValue vector_property_get (JSContext* ctx, JSValueConst obj_val, JSAtom atom, JSValueConst receiver) {
  125. JSClassID classId = 0;
  126. auto* container = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (obj_val, &classId));
  127. VEC_MAGIC_CHECK_EXCEPTION (container, components);
  128. const char* name = JS_AtomToCString (ctx, atom);
  129. if (name == nullptr) {
  130. return JS_EXCEPTION;
  131. }
  132. ScopeGuard guard ([=] { JS_FreeCString (ctx, name); });
  133. const auto value = vector_get<components> (container->value);
  134. if (strcmp (name, "x") == 0) {
  135. return JS_NewFloat64 (ctx, value.x);
  136. }
  137. if (strcmp (name, "y") == 0) {
  138. return JS_NewFloat64 (ctx, value.y);
  139. }
  140. if constexpr (components >= 3) {
  141. if (strcmp (name, "z") == 0) {
  142. return JS_NewFloat64 (ctx, value.z);
  143. }
  144. if constexpr (components >= 4) {
  145. if (strcmp (name, "w") == 0) {
  146. return JS_NewFloat64 (ctx, value.w);
  147. }
  148. }
  149. }
  150. return JS_EXCEPTION;
  151. }
  152. template JSValue vector_property_get<2> (JSContext* ctx, JSValueConst obj_val, JSAtom atom, JSValueConst receiver);
  153. template JSValue vector_property_get<3> (JSContext* ctx, JSValueConst obj_val, JSAtom atom, JSValueConst receiver);
  154. template JSValue vector_property_get<4> (JSContext* ctx, JSValueConst obj_val, JSAtom atom, JSValueConst receiver);
  155. template <int components>
  156. int vector_property_set (
  157. JSContext* ctx, JSValueConst obj_val, JSAtom atom, JSValueConst val, JSValueConst receiver, int flags
  158. ) {
  159. JSClassID classId = 0;
  160. auto* container = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (obj_val, &classId));
  161. VEC_MAGIC_CHECK_ERROR (container, components);
  162. int tag = JS_VALUE_GET_TAG (val);
  163. if (tag != JS_TAG_INT && !JS_TAG_IS_FLOAT64 (tag)) {
  164. return -1;
  165. }
  166. const char* name = JS_AtomToCString (ctx, atom);
  167. if (name == nullptr) {
  168. return -1;
  169. }
  170. ScopeGuard guard ([=] { JS_FreeCString (ctx, name); });
  171. auto vec = vector_get<components> (container->value);
  172. void* into = nullptr;
  173. if (strcmp (name, "x") == 0) {
  174. if constexpr (std::is_same_v<decltype (vec), glm::vec2> || std::is_same_v<decltype (vec), glm::vec3>
  175. || std::is_same_v<decltype (vec), glm::vec4>) {
  176. into = &vec.x;
  177. } else if constexpr (std::is_same_v<decltype (vec), Color>) {
  178. into = &vec.r;
  179. }
  180. } else if (strcmp (name, "y") == 0) {
  181. if constexpr (std::is_same_v<decltype (vec), glm::vec2> || std::is_same_v<decltype (vec), glm::vec3>
  182. || std::is_same_v<decltype (vec), glm::vec4>) {
  183. into = &vec.y;
  184. } else if constexpr (std::is_same_v<decltype (vec), Color>) {
  185. into = &vec.g;
  186. }
  187. } else if constexpr (components >= 3) {
  188. if (strcmp (name, "z") == 0) {
  189. if constexpr (std::is_same_v<decltype (vec), glm::vec3> || std::is_same_v<decltype (vec), glm::vec4>) {
  190. into = &vec.z;
  191. } else if constexpr (std::is_same_v<decltype (vec), Color>) {
  192. into = &vec.b;
  193. }
  194. } else if constexpr (components >= 4) {
  195. if (strcmp (name, "w") == 0) {
  196. if constexpr (std::is_same_v<decltype (vec), glm::vec4>) {
  197. into = &vec.w;
  198. } else if constexpr (std::is_same_v<decltype (vec), Color>) {
  199. into = &vec.a;
  200. }
  201. }
  202. }
  203. }
  204. if (into == nullptr) {
  205. return -1;
  206. }
  207. double value = 0;
  208. JS_ToFloat64 (ctx, &value, val);
  209. *static_cast<float*> (into) = static_cast<float> (value);
  210. container->value.update (vec, DynamicValue::UpdateSource::Script);
  211. return 0;
  212. }
  213. template int vector_property_set<2> (
  214. JSContext* ctx, JSValueConst obj_val, JSAtom atom, JSValueConst val, JSValueConst receiver, int flags
  215. );
  216. template int vector_property_set<3> (
  217. JSContext* ctx, JSValueConst obj_val, JSAtom atom, JSValueConst val, JSValueConst receiver, int flags
  218. );
  219. template int vector_property_set<4> (
  220. JSContext* ctx, JSValueConst obj_val, JSAtom atom, JSValueConst val, JSValueConst receiver, int flags
  221. );
  222. template <int components> JSValue vector_copy (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
  223. JSClassID classId = 0;
  224. auto* container = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (this_val, &classId));
  225. VEC_MAGIC_CHECK_EXCEPTION (container, components);
  226. // create a new DynamicValue
  227. return container->adapter.instantiate (container->value, true);
  228. }
  229. template JSValue vector_copy<2> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  230. template JSValue vector_copy<3> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  231. template JSValue vector_copy<4> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  232. bool vector_value_equals (JSContext* ctx, JSValue left, JSValue right) {
  233. if (JS_TAG_IS_FLOAT64 (JS_VALUE_GET_TAG (left)) || JS_TAG_IS_FLOAT64 (JS_VALUE_GET_TAG (right))) {
  234. double x1Val = 0.0f, x2Val = 0.0f;
  235. JS_ToFloat64 (ctx, &x1Val, left);
  236. JS_ToFloat64 (ctx, &x2Val, right);
  237. if (std::abs (x1Val - x2Val) > 0.00001) {
  238. return false;
  239. }
  240. } else {
  241. if (!JS_IsEqual (ctx, left, right)) {
  242. return false;
  243. }
  244. }
  245. return true;
  246. }
  247. template <int components> JSValue vector_equals (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
  248. if (argc == 0) {
  249. return JS_FALSE;
  250. }
  251. JSClassID classId = 0;
  252. auto* container = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (this_val, &classId));
  253. VEC_MAGIC_CHECK_EXCEPTION (container, components);
  254. JSValue other = argv[0];
  255. auto* otherContainer = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (other, &classId));
  256. VEC_MAGIC_CHECK_EXCEPTION (otherContainer, components);
  257. const auto vector = vector_get<components> (container->value);
  258. const auto otherVector = vector_get<components> (otherContainer->value);
  259. if constexpr (components == 2) {
  260. return vector.x == otherVector.x && vector.y == otherVector.y ? JS_TRUE : JS_FALSE;
  261. } else if constexpr (components == 3) {
  262. return vector.x == otherVector.x && vector.y == otherVector.y && vector.z == otherVector.z ? JS_TRUE : JS_FALSE;
  263. } else if constexpr (components == 4) {
  264. return vector.x == otherVector.x && vector.y == otherVector.y && vector.z == otherVector.z
  265. && vector.w == otherVector.w
  266. ? JS_TRUE
  267. : JS_FALSE;
  268. }
  269. return JS_FALSE;
  270. }
  271. template JSValue vector_equals<2> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  272. template JSValue vector_equals<3> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  273. template JSValue vector_equals<4> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  274. template <int components> JSValue vector_length (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
  275. JSClassID classId = 0;
  276. auto* container = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (this_val, &classId));
  277. VEC_MAGIC_CHECK_EXCEPTION (container, components);
  278. return JS_NewFloat64 (ctx, glm::length (vector_get<components> (container->value)));
  279. }
  280. template JSValue vector_length<2> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  281. template JSValue vector_length<3> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  282. template JSValue vector_length<4> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  283. template <int components>
  284. JSValue vector_constructor (JSContext* ctx, JSValueConst new_target, int argc, JSValueConst* argv, int magic) {
  285. if (argc == 0) {
  286. return JS_EXCEPTION;
  287. }
  288. auto it = vectorAdapterInstances<components>.find (magic);
  289. if (it == vectorAdapterInstances<components>.end ()) {
  290. return JS_EXCEPTION;
  291. }
  292. JSValue result = it->second.instantiate ();
  293. JSClassID classId = 0;
  294. auto* container = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (result, &classId));
  295. VEC_MAGIC_CHECK_EXCEPTION (container, components);
  296. container->value.update (vector_get<components> (ctx, argv[0]), DynamicValue::UpdateSource::Initialization);
  297. return result;
  298. }
  299. template JSValue vector_constructor<2> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv, int magic);
  300. template JSValue vector_constructor<3> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv, int magic);
  301. template JSValue vector_constructor<4> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv, int magic);
  302. template <int components> void vector_finalizer (JSRuntime* rt, JSValueConst val) {
  303. JSClassID classId = 0;
  304. const auto* container = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (val, &classId));
  305. if (!container || container->magic != (int)(VEC_OPAQUE_MAGIC + components)) {
  306. return;
  307. }
  308. // free container and the associated DynamicValue if temporal
  309. if (container->id != InvalidVectorInstanceId) {
  310. container->adapter.free (container->id);
  311. }
  312. delete container;
  313. }
  314. template void vector_finalizer<2> (JSRuntime* rt, JSValueConst val);
  315. template void vector_finalizer<3> (JSRuntime* rt, JSValueConst val);
  316. template void vector_finalizer<4> (JSRuntime* rt, JSValueConst val);
  317. template <int components>
  318. JSValue vector_normalize (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
  319. JSClassID classId = 0;
  320. const auto* container = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (this_val, &classId));
  321. VEC_MAGIC_CHECK_EXCEPTION (container, components);
  322. JSValue newVector = container->adapter.instantiate (container->value, true);
  323. const auto* newContainer = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (newVector, &classId));
  324. newContainer->value.update (
  325. glm::normalize (vector_get<components> (container->value)), DynamicValue::UpdateSource::Script
  326. );
  327. return newVector;
  328. }
  329. template JSValue vector_normalize<2> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  330. template JSValue vector_normalize<3> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  331. template JSValue vector_normalize<4> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  332. template <int components> JSValue vector_add (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
  333. if (argc != 1) {
  334. return JS_UNDEFINED;
  335. }
  336. JSClassID classId = 0;
  337. const auto* container = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (this_val, &classId));
  338. VEC_MAGIC_CHECK_EXCEPTION (container, components);
  339. JSValue newVector = container->adapter.instantiate ();
  340. const auto* newContainer = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (newVector, &classId));
  341. VEC_MAGIC_CHECK_EXCEPTION (newContainer, components);
  342. newContainer->value.update (
  343. vector_get<components> (ctx, argv[0]) + vector_get<components> (container->value),
  344. DynamicValue::UpdateSource::Initialization
  345. );
  346. return newVector;
  347. }
  348. template JSValue vector_add<2> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  349. template JSValue vector_add<3> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  350. template JSValue vector_add<4> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  351. template <int components>
  352. JSValue vector_subtract (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
  353. if (argc != 1) {
  354. return JS_UNDEFINED;
  355. }
  356. JSClassID classId = 0;
  357. const auto* container = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (this_val, &classId));
  358. VEC_MAGIC_CHECK_EXCEPTION (container, components);
  359. JSValue newVector = container->adapter.instantiate ();
  360. const auto* newContainer = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (newVector, &classId));
  361. VEC_MAGIC_CHECK_EXCEPTION (newContainer, components);
  362. newContainer->value.update (
  363. vector_get<components> (ctx, argv[0]) - vector_get<components> (container->value),
  364. DynamicValue::UpdateSource::Initialization
  365. );
  366. return newVector;
  367. }
  368. template JSValue vector_subtract<2> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  369. template JSValue vector_subtract<3> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  370. template JSValue vector_subtract<4> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  371. template <int components>
  372. JSValue vector_multiply (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
  373. if (argc != 1) {
  374. return JS_UNDEFINED;
  375. }
  376. JSClassID classId = 0;
  377. const auto* container = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (this_val, &classId));
  378. VEC_MAGIC_CHECK_EXCEPTION (container, components);
  379. JSValue newVector = container->adapter.instantiate ();
  380. const auto* newContainer = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (newVector, &classId));
  381. VEC_MAGIC_CHECK_EXCEPTION (newContainer, components);
  382. newContainer->value.update (
  383. vector_get<components> (ctx, argv[0]) * vector_get<components> (container->value),
  384. DynamicValue::UpdateSource::Initialization
  385. );
  386. return newVector;
  387. }
  388. template JSValue vector_multiply<2> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  389. template JSValue vector_multiply<3> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  390. template JSValue vector_multiply<4> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  391. template <int components> JSValue vector_divide (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
  392. if (argc != 1) {
  393. return JS_UNDEFINED;
  394. }
  395. JSClassID classId = 0;
  396. const auto* container = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (this_val, &classId));
  397. VEC_MAGIC_CHECK_EXCEPTION (container, components);
  398. JSValue newVector = container->adapter.instantiate ();
  399. const auto* newContainer = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (newVector, &classId));
  400. VEC_MAGIC_CHECK_EXCEPTION (newContainer, components);
  401. newContainer->value.update (
  402. vector_get<components> (ctx, argv[0]) / vector_get<components> (container->value),
  403. DynamicValue::UpdateSource::Initialization
  404. );
  405. return newVector;
  406. }
  407. template JSValue vector_divide<2> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  408. template JSValue vector_divide<3> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  409. template JSValue vector_divide<4> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  410. template <int components> JSValue vector_dot (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
  411. if (argc != 1) {
  412. return JS_UNDEFINED;
  413. }
  414. JSClassID classId = 0;
  415. const auto* container = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (this_val, &classId));
  416. VEC_MAGIC_CHECK_EXCEPTION (container, components);
  417. JSValue newVector = container->adapter.instantiate ();
  418. const auto* newContainer = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (newVector, &classId));
  419. VEC_MAGIC_CHECK_EXCEPTION (newContainer, components);
  420. newContainer->value.update (
  421. glm::dot (vector_get<components> (ctx, argv[0]), vector_get<components> (container->value)),
  422. DynamicValue::UpdateSource::Initialization
  423. );
  424. return newVector;
  425. }
  426. template JSValue vector_dot<2> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  427. template JSValue vector_dot<3> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  428. template JSValue vector_dot<4> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  429. template <int components> JSValue vector_cross (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
  430. if (argc != 1) {
  431. return JS_UNDEFINED;
  432. }
  433. JSClassID classId = 0;
  434. const auto* container = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (this_val, &classId));
  435. VEC_MAGIC_CHECK_EXCEPTION (container, components);
  436. JSValue newVector = container->adapter.instantiate ();
  437. const auto* newContainer = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (newVector, &classId));
  438. VEC_MAGIC_CHECK_EXCEPTION (newContainer, components);
  439. newContainer->value.update (
  440. glm::cross (vector_get<components> (ctx, argv[0]), vector_get<components> (container->value)),
  441. DynamicValue::UpdateSource::Initialization
  442. );
  443. return newVector;
  444. }
  445. template JSValue vector_cross<3> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  446. template <int components> JSValue vector_mix (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
  447. if (argc != 2) {
  448. return JS_EXCEPTION;
  449. }
  450. if (!JS_IsNumber (argv[1])) {
  451. return JS_EXCEPTION;
  452. }
  453. double amount = 0.0f;
  454. JS_ToFloat64 (ctx, &amount, argv[1]);
  455. JSClassID classId = 0;
  456. const auto* container = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (this_val, &classId));
  457. VEC_MAGIC_CHECK_EXCEPTION (container, components);
  458. JSValue newVector = container->adapter.instantiate ();
  459. const auto* newContainer = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (newVector, &classId));
  460. VEC_MAGIC_CHECK_EXCEPTION (newContainer, components);
  461. newContainer->value.update (
  462. glm::mix (vector_get<components> (ctx, argv[0]), vector_get<components> (container->value), amount),
  463. DynamicValue::UpdateSource::Initialization
  464. );
  465. return newVector;
  466. }
  467. template JSValue vector_mix<2> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  468. template JSValue vector_mix<3> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  469. template JSValue vector_mix<4> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  470. template <int components> JSValue vector_min (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
  471. if (argc != 1) {
  472. return JS_UNDEFINED;
  473. }
  474. JSClassID classId = 0;
  475. const auto* container = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (this_val, &classId));
  476. VEC_MAGIC_CHECK_EXCEPTION (container, components);
  477. JSValue newVector = container->adapter.instantiate ();
  478. const auto* newContainer = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (newVector, &classId));
  479. VEC_MAGIC_CHECK_EXCEPTION (newContainer, components);
  480. newContainer->value.update (
  481. glm::min (vector_get<components> (ctx, argv[0]), vector_get<components> (container->value)),
  482. DynamicValue::UpdateSource::Initialization
  483. );
  484. return newVector;
  485. }
  486. template JSValue vector_min<2> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  487. template JSValue vector_min<3> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  488. template JSValue vector_min<4> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  489. template <int components> JSValue vector_max (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
  490. if (argc != 1) {
  491. return JS_UNDEFINED;
  492. }
  493. JSClassID classId = 0;
  494. const auto* container = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (this_val, &classId));
  495. VEC_MAGIC_CHECK_EXCEPTION (container, components);
  496. JSValue newVector = container->adapter.instantiate ();
  497. const auto* newContainer = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (newVector, &classId));
  498. VEC_MAGIC_CHECK_EXCEPTION (newContainer, components);
  499. newContainer->value.update (
  500. glm::max (vector_get<components> (ctx, argv[0]), vector_get<components> (container->value)),
  501. DynamicValue::UpdateSource::Initialization
  502. );
  503. return newVector;
  504. }
  505. template JSValue vector_max<2> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  506. template JSValue vector_max<3> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  507. template JSValue vector_max<4> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  508. template <int components> JSValue vector_abs (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
  509. JSClassID classId = 0;
  510. const auto* container = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (this_val, &classId));
  511. VEC_MAGIC_CHECK_EXCEPTION (container, components);
  512. JSValue newVector = container->adapter.instantiate ();
  513. const auto* newContainer = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (newVector, &classId));
  514. VEC_MAGIC_CHECK_EXCEPTION (newContainer, components);
  515. newContainer->value.update (
  516. glm::abs (vector_get<components> (container->value)), DynamicValue::UpdateSource::Initialization
  517. );
  518. return newVector;
  519. }
  520. template JSValue vector_abs<2> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  521. template JSValue vector_abs<3> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  522. template JSValue vector_abs<4> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  523. template <int components> JSValue vector_sign (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
  524. JSClassID classId = 0;
  525. const auto* container = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (this_val, &classId));
  526. VEC_MAGIC_CHECK_EXCEPTION (container, components);
  527. JSValue newVector = container->adapter.instantiate ();
  528. const auto* newContainer = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (newVector, &classId));
  529. VEC_MAGIC_CHECK_EXCEPTION (newContainer, components);
  530. newContainer->value.update (
  531. glm::sign (vector_get<components> (container->value)), DynamicValue::UpdateSource::Initialization
  532. );
  533. return newVector;
  534. }
  535. template JSValue vector_sign<2> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  536. template JSValue vector_sign<3> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  537. template JSValue vector_sign<4> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  538. template <int components> JSValue vector_round (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
  539. JSClassID classId = 0;
  540. const auto* container = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (this_val, &classId));
  541. VEC_MAGIC_CHECK_EXCEPTION (container, components);
  542. JSValue newVector = container->adapter.instantiate ();
  543. const auto* newContainer = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (newVector, &classId));
  544. VEC_MAGIC_CHECK_EXCEPTION (newContainer, components);
  545. newContainer->value.update (
  546. glm::round (vector_get<components> (container->value)), DynamicValue::UpdateSource::Initialization
  547. );
  548. return newVector;
  549. }
  550. template JSValue vector_round<2> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  551. template JSValue vector_round<3> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  552. template JSValue vector_round<4> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  553. template <int components> JSValue vector_floor (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
  554. JSClassID classId = 0;
  555. const auto* container = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (this_val, &classId));
  556. VEC_MAGIC_CHECK_EXCEPTION (container, components);
  557. JSValue newVector = container->adapter.instantiate ();
  558. const auto* newContainer = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (newVector, &classId));
  559. VEC_MAGIC_CHECK_EXCEPTION (newContainer, components);
  560. newContainer->value.update (
  561. glm::floor (vector_get<components> (container->value)), DynamicValue::UpdateSource::Initialization
  562. );
  563. return newVector;
  564. }
  565. template JSValue vector_floor<2> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  566. template JSValue vector_floor<3> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  567. template JSValue vector_floor<4> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  568. template <int components> JSValue vector_ceil (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
  569. JSClassID classId = 0;
  570. const auto* container = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (this_val, &classId));
  571. VEC_MAGIC_CHECK_EXCEPTION (container, components);
  572. JSValue newVector = container->adapter.instantiate ();
  573. const auto* newContainer = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (newVector, &classId));
  574. const auto vector = vector_get<components> (container->value);
  575. VEC_MAGIC_CHECK_EXCEPTION (newContainer, components);
  576. newContainer->value.update (glm::ceil (vector), DynamicValue::UpdateSource::Initialization);
  577. return newVector;
  578. }
  579. template JSValue vector_ceil<2> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  580. template JSValue vector_ceil<3> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  581. template JSValue vector_ceil<4> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  582. template <int components>
  583. JSValue vector_toString (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv) {
  584. JSClassID classId = 0;
  585. const auto* container = static_cast<VectorOpaqueContainer<components>*> (JS_GetAnyOpaque (this_val, &classId));
  586. VEC_MAGIC_CHECK_EXCEPTION (container, components);
  587. return JS_NewString (ctx, container->value.toString ().c_str ());
  588. }
  589. template JSValue vector_toString<2> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  590. template JSValue vector_toString<3> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  591. template JSValue vector_toString<4> (JSContext* ctx, JSValueConst this_val, int argc, JSValueConst* argv);
  592. template <int components>
  593. VectorAdapter<components>::VectorAdapter (ScriptEngine& engine) :
  594. ObjectAdapter (engine), m_instanceId (++VectorAdapterInstanceId), m_name ("Vec" + std::to_string (components)),
  595. m_exoticMethods (
  596. {
  597. .get_property = vector_property_get<components>,
  598. .set_property = vector_property_set<components>,
  599. }
  600. ) {
  601. vectorAdapterInstances<components>.emplace (this->m_instanceId, *this);
  602. this->registerType (
  603. {
  604. .class_name = this->m_name.c_str (),
  605. .finalizer = vector_finalizer<components>,
  606. .exotic = &this->m_exoticMethods,
  607. }
  608. );
  609. // build the prototype for the Vector and assign the required methods
  610. m_prototype = JS_NewObject (this->m_engine.getContext ());
  611. JS_DupValue (this->m_engine.getContext (), m_prototype);
  612. JSValue ctor = JS_NewCFunctionMagic (
  613. this->m_engine.getContext (), vector_constructor<components>, this->m_name.c_str (), 1,
  614. JS_CFUNC_constructor_magic, this->m_instanceId
  615. );
  616. JS_SetConstructor (this->m_engine.getContext (), ctor, m_prototype);
  617. JS_DefinePropertyValueStr (
  618. this->m_engine.getContext (), m_prototype, "copy",
  619. JS_NewCFunction (this->m_engine.getContext (), vector_copy<components>, "copy", 0), JS_PROP_ENUMERABLE
  620. );
  621. JS_DefinePropertyValueStr (
  622. this->m_engine.getContext (), m_prototype, "equals",
  623. JS_NewCFunction (this->m_engine.getContext (), vector_equals<components>, "equals", 1), JS_PROP_ENUMERABLE
  624. );
  625. JS_DefinePropertyValueStr (
  626. this->m_engine.getContext (), m_prototype, "length",
  627. JS_NewCFunction (this->m_engine.getContext (), vector_length<components>, "length", 0), JS_PROP_ENUMERABLE
  628. );
  629. JS_DefinePropertyValueStr (
  630. this->m_engine.getContext (), m_prototype, "lengthSqr",
  631. JS_NewCFunction (this->m_engine.getContext (), vector_length<components>, "lengthSqr", 0), JS_PROP_ENUMERABLE
  632. );
  633. JS_DefinePropertyValueStr (
  634. this->m_engine.getContext (), m_prototype, "normalize",
  635. JS_NewCFunction (this->m_engine.getContext (), vector_normalize<components>, "normalize", 0), JS_PROP_ENUMERABLE
  636. );
  637. JS_DefinePropertyValueStr (
  638. this->m_engine.getContext (), m_prototype, "add",
  639. JS_NewCFunction (this->m_engine.getContext (), vector_add<components>, "add", 1), JS_PROP_ENUMERABLE
  640. );
  641. JS_DefinePropertyValueStr (
  642. this->m_engine.getContext (), m_prototype, "subtract",
  643. JS_NewCFunction (this->m_engine.getContext (), vector_subtract<components>, "subtract", 1), JS_PROP_ENUMERABLE
  644. );
  645. JS_DefinePropertyValueStr (
  646. this->m_engine.getContext (), m_prototype, "multiply",
  647. JS_NewCFunction (this->m_engine.getContext (), vector_multiply<components>, "multiply", 1), JS_PROP_ENUMERABLE
  648. );
  649. JS_DefinePropertyValueStr (
  650. this->m_engine.getContext (), m_prototype, "divide",
  651. JS_NewCFunction (this->m_engine.getContext (), vector_divide<components>, "divide", 1), JS_PROP_ENUMERABLE
  652. );
  653. JS_DefinePropertyValueStr (
  654. this->m_engine.getContext (), m_prototype, "dot",
  655. JS_NewCFunction (this->m_engine.getContext (), vector_dot<components>, "dot", 1), JS_PROP_ENUMERABLE
  656. );
  657. if constexpr (components == 3) {
  658. JS_DefinePropertyValueStr (
  659. this->m_engine.getContext (), m_prototype, "cross",
  660. JS_NewCFunction (this->m_engine.getContext (), vector_cross<components>, "cross", 1), JS_PROP_ENUMERABLE
  661. );
  662. }
  663. JS_DefinePropertyValueStr (
  664. this->m_engine.getContext (), m_prototype, "mix",
  665. JS_NewCFunction (this->m_engine.getContext (), vector_mix<components>, "mix", 2), JS_PROP_ENUMERABLE
  666. );
  667. JS_DefinePropertyValueStr (
  668. this->m_engine.getContext (), m_prototype, "min",
  669. JS_NewCFunction (this->m_engine.getContext (), vector_min<components>, "min", 1), JS_PROP_ENUMERABLE
  670. );
  671. JS_DefinePropertyValueStr (
  672. this->m_engine.getContext (), m_prototype, "max",
  673. JS_NewCFunction (this->m_engine.getContext (), vector_max<components>, "max", 1), JS_PROP_ENUMERABLE
  674. );
  675. JS_DefinePropertyValueStr (
  676. this->m_engine.getContext (), m_prototype, "abs",
  677. JS_NewCFunction (this->m_engine.getContext (), vector_abs<components>, "abs", 0), JS_PROP_ENUMERABLE
  678. );
  679. JS_DefinePropertyValueStr (
  680. this->m_engine.getContext (), m_prototype, "sign",
  681. JS_NewCFunction (this->m_engine.getContext (), vector_sign<components>, "sign", 0), JS_PROP_ENUMERABLE
  682. );
  683. JS_DefinePropertyValueStr (
  684. this->m_engine.getContext (), m_prototype, "round",
  685. JS_NewCFunction (this->m_engine.getContext (), vector_round<components>, "round", 0), JS_PROP_ENUMERABLE
  686. );
  687. JS_DefinePropertyValueStr (
  688. this->m_engine.getContext (), m_prototype, "floor",
  689. JS_NewCFunction (this->m_engine.getContext (), vector_floor<components>, "floor", 0), JS_PROP_ENUMERABLE
  690. );
  691. JS_DefinePropertyValueStr (
  692. this->m_engine.getContext (), m_prototype, "ceil",
  693. JS_NewCFunction (this->m_engine.getContext (), vector_ceil<components>, "ceil", 0), JS_PROP_ENUMERABLE
  694. );
  695. JS_DefinePropertyValueStr (
  696. this->m_engine.getContext (), m_prototype, "toString",
  697. JS_NewCFunction (this->m_engine.getContext (), vector_toString<components>, "toString", 0), JS_PROP_ENUMERABLE
  698. );
  699. JS_SetClassProto (this->m_engine.getContext (), this->m_classId, m_prototype);
  700. JS_FreeValue (this->m_engine.getContext (), ctor);
  701. }
  702. template <int components> VectorAdapter<components>::~VectorAdapter () {
  703. vectorAdapterInstances<components>.erase (this->m_instanceId);
  704. JS_FreeValue (this->m_engine.getContext (), m_prototype);
  705. }
  706. template <int components> JSValue VectorAdapter<components>::instantiate (ScriptableObject& object) {
  707. throw new std::runtime_error ("Cannot create a Vector4 instance from a ScriptableObject");
  708. }
  709. template <int components> JSValue VectorAdapter<components>::instantiate (DynamicValue& value) {
  710. JSValue result = this->ObjectAdapter::instantiate (value);
  711. JS_SetOpaque (
  712. result,
  713. new VectorOpaqueContainer<components> {
  714. .magic = VEC_OPAQUE_MAGIC + components,
  715. .adapter = *this,
  716. .value = value,
  717. .id = InvalidVectorInstanceId,
  718. }
  719. );
  720. return result;
  721. }
  722. template <int components> JSValue VectorAdapter<components>::instantiate (DynamicValue& source, bool temporal) {
  723. auto value = std::make_unique<DynamicValue> (source);
  724. uint32_t id = ++VectorInstanceId;
  725. JSValue result = this->ObjectAdapter::instantiate (*value);
  726. JS_SetOpaque (
  727. result,
  728. new VectorOpaqueContainer<components> {
  729. .magic = VEC_OPAQUE_MAGIC + components,
  730. .adapter = *this,
  731. .value = *value,
  732. .id = id,
  733. }
  734. );
  735. this->m_values.emplace (id, std::move (value));
  736. return result;
  737. }
  738. template <int components> JSValue VectorAdapter<components>::instantiate () {
  739. auto value = std::make_unique<DynamicValue> (vector_new<components> ());
  740. uint32_t id = ++VectorInstanceId;
  741. JSValue result = this->ObjectAdapter::instantiate (*value);
  742. JS_SetOpaque (
  743. result,
  744. new VectorOpaqueContainer<components> {
  745. .magic = VEC_OPAQUE_MAGIC + components,
  746. .adapter = *this,
  747. .value = *value,
  748. .id = id,
  749. }
  750. );
  751. this->m_values.emplace (id, std::move (value));
  752. return result;
  753. }
  754. template <int components> void VectorAdapter<components>::free (uint32_t vectorId) {
  755. auto it = this->m_values.find (vectorId);
  756. if (it != this->m_values.end ()) {
  757. this->m_values.erase (it);
  758. }
  759. }
  760. namespace WallpaperEngine::Scripting::Adapters {
  761. template class VectorAdapter<2>;
  762. template class VectorAdapter<3>;
  763. template class VectorAdapter<4>;
  764. } // namespace WallpaperEngine::Scripting::Adapters