sfml.js 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (313) hide show
  1. package/README.ja.md +10 -9
  2. package/README.ko.md +10 -9
  3. package/README.md +10 -9
  4. package/README.zh-CN.md +10 -9
  5. package/binding.gyp +23 -16
  6. package/dist/audio/effect-processor.js +9 -3
  7. package/dist/audio/input-sound-file.js +41 -15
  8. package/dist/audio/listener.js +33 -11
  9. package/dist/audio/music.js +41 -18
  10. package/dist/audio/output-sound-file.js +4 -2
  11. package/dist/audio/playback-device.js +11 -3
  12. package/dist/audio/sound-buffer-recorder.js +8 -5
  13. package/dist/audio/sound-buffer.js +40 -17
  14. package/dist/audio/sound-file-factory.js +21 -9
  15. package/dist/audio/sound-file-reader.js +21 -7
  16. package/dist/audio/sound-recorder.js +36 -15
  17. package/dist/audio/sound-source.js +117 -40
  18. package/dist/audio/sound-stream.js +14 -5
  19. package/dist/audio/sound.js +9 -4
  20. package/dist/audio/value.js +42 -16
  21. package/dist/graphics/blend-mode.js +2 -1
  22. package/dist/graphics/boolean-vector.d.ts +1 -2
  23. package/dist/graphics/boolean-vector.js +19 -9
  24. package/dist/graphics/color.js +2 -1
  25. package/dist/graphics/convex-shape.js +7 -3
  26. package/dist/graphics/font.js +52 -23
  27. package/dist/graphics/glsl-matrix.js +8 -3
  28. package/dist/graphics/glsl.js +47 -16
  29. package/dist/graphics/glyph.js +27 -10
  30. package/dist/graphics/image.js +30 -14
  31. package/dist/graphics/rect.js +15 -8
  32. package/dist/graphics/render-states.js +18 -9
  33. package/dist/graphics/render-target.js +49 -23
  34. package/dist/graphics/render-texture.js +24 -12
  35. package/dist/graphics/render-window.js +6 -3
  36. package/dist/graphics/shader.js +67 -32
  37. package/dist/graphics/shape.js +25 -13
  38. package/dist/graphics/sprite.js +19 -9
  39. package/dist/graphics/stencil-mode.js +10 -6
  40. package/dist/graphics/swap.js +2 -1
  41. package/dist/graphics/text.js +123 -48
  42. package/dist/graphics/texture.js +48 -24
  43. package/dist/graphics/transform.js +29 -13
  44. package/dist/graphics/transformable.js +2 -1
  45. package/dist/graphics/vertex-array.js +40 -18
  46. package/dist/graphics/vertex-buffer.js +39 -19
  47. package/dist/graphics/vertex.js +6 -3
  48. package/dist/graphics/view.js +30 -14
  49. package/dist/index.d.ts +4 -4
  50. package/dist/internal/audio-codec-registry.js +12 -6
  51. package/dist/internal/audio-input-stream.js +2 -1
  52. package/dist/internal/audio-music-pump.js +58 -24
  53. package/dist/internal/audio-recorder-session.js +17 -7
  54. package/dist/internal/audio-worker-session.js +28 -10
  55. package/dist/internal/audio-worker.js +40 -21
  56. package/dist/internal/glsl-value.d.ts +3 -1
  57. package/dist/internal/glsl-value.js +18 -8
  58. package/dist/internal/graphics-state.js +29 -15
  59. package/dist/internal/input-stream.d.ts +3 -1
  60. package/dist/internal/input-stream.js +16 -7
  61. package/dist/internal/numeric-properties.js +2 -1
  62. package/dist/internal/render-states.js +2 -1
  63. package/dist/internal/scalar.js +22 -13
  64. package/dist/internal/shaped-glyph.d.ts +3 -1
  65. package/dist/internal/shaped-glyph.js +12 -4
  66. package/dist/internal/struct-view.d.ts +3 -1
  67. package/dist/internal/struct-view.js +3 -1
  68. package/dist/internal/transformable.js +2 -1
  69. package/dist/network/common.js +23 -11
  70. package/dist/network/dns.js +25 -17
  71. package/dist/network/ftp.js +43 -18
  72. package/dist/network/http.js +126 -56
  73. package/dist/network/ip-address.js +50 -25
  74. package/dist/network/packet.js +63 -25
  75. package/dist/network/sftp.js +175 -74
  76. package/dist/network/socket-selector.js +28 -14
  77. package/dist/network/socket.js +71 -30
  78. package/dist/network/tcp-listener.js +42 -19
  79. package/dist/network/tcp-socket.js +111 -53
  80. package/dist/network/tls-material.js +2 -1
  81. package/dist/network/udp-socket.js +68 -33
  82. package/dist/system/angle.js +2 -1
  83. package/dist/system/clock.js +4 -2
  84. package/dist/system/err.js +5 -2
  85. package/dist/system/file-input-stream.js +8 -4
  86. package/dist/system/input-stream.js +6 -3
  87. package/dist/system/operators.d.ts +1 -1
  88. package/dist/system/operators.js +90 -50
  89. package/dist/system/sleep.js +4 -2
  90. package/dist/system/time.js +12 -6
  91. package/dist/system/vector2.js +6 -7
  92. package/dist/system/vector3.js +11 -6
  93. package/dist/window/clipboard.js +2 -1
  94. package/dist/window/context.js +10 -5
  95. package/dist/window/cursor.js +2 -1
  96. package/dist/window/event.js +9 -5
  97. package/dist/window/mouse.js +4 -2
  98. package/dist/window/video-mode.js +2 -1
  99. package/dist/window/window.js +19 -12
  100. package/native/addon.cc +25 -12
  101. package/native/audio-binding.cc +266 -126
  102. package/native/audio-codec-binding.cc +147 -77
  103. package/native/audio-device-binding.cc +76 -49
  104. package/native/audio-file-binding.cc +116 -67
  105. package/native/audio-recorder-binding.cc +147 -98
  106. package/native/audio-recorder.h +1 -1
  107. package/native/audio-state.h +53 -30
  108. package/native/audio-stream-binding.cc +148 -94
  109. package/native/audio-stream.h +5 -4
  110. package/native/audio-worker-binding.cc +163 -81
  111. package/native/audio-worker.h +35 -19
  112. package/native/batch-drawable.cc +29 -16
  113. package/native/batch-drawable.h +11 -8
  114. package/native/context-binding.cc +40 -27
  115. package/native/context-settings.h +15 -8
  116. package/native/deferred-delete.cc +9 -6
  117. package/native/deferred-delete.h +7 -8
  118. package/native/error-stream-binding.cc +42 -28
  119. package/native/font-binding.cc +89 -48
  120. package/native/graphics-binding.cc +122 -72
  121. package/native/graphics-state.h +50 -36
  122. package/native/image-binding.cc +38 -24
  123. package/native/image-state.h +26 -18
  124. package/native/input-binding.cc +42 -27
  125. package/native/render-window-binding.cc +38 -23
  126. package/native/shader-binding.cc +115 -50
  127. package/native/shape-binding.cc +94 -49
  128. package/native/shape-state.h +18 -9
  129. package/native/shaped-glyph-storage.h +9 -5
  130. package/native/stream-binding.cc +143 -117
  131. package/native/system-binding.cc +17 -12
  132. package/native/text-binding.cc +68 -35
  133. package/native/text-state.h +25 -18
  134. package/native/texture-binding.cc +42 -33
  135. package/native/texture-resource.h +20 -12
  136. package/native/time-value.h +3 -1
  137. package/native/transformable-binding.cc +12 -6
  138. package/native/transformable-state.h +6 -6
  139. package/native/value-binding.h +46 -28
  140. package/native/value-layout.h +10 -10
  141. package/native/values-binding.cc +29 -23
  142. package/native/vertex-binding.cc +18 -12
  143. package/native/vertex-buffer-binding.cc +38 -24
  144. package/native/vertex-storage.h +2 -2
  145. package/native/view-data.h +4 -3
  146. package/native/window-binding.cc +75 -45
  147. package/native/window-event.h +20 -11
  148. package/native/window-owner.h +15 -12
  149. package/package.json +40 -48
  150. package/src/audio/effect-processor.ts +9 -3
  151. package/src/audio/input-sound-file.ts +108 -36
  152. package/src/audio/listener.ts +39 -13
  153. package/src/audio/music.ts +112 -47
  154. package/src/audio/output-sound-file.ts +40 -11
  155. package/src/audio/playback-device.ts +23 -8
  156. package/src/audio/sound-buffer-recorder.ts +35 -13
  157. package/src/audio/sound-buffer.ts +166 -58
  158. package/src/audio/sound-file-factory.ts +127 -49
  159. package/src/audio/sound-file-reader.ts +49 -15
  160. package/src/audio/sound-recorder.ts +100 -34
  161. package/src/audio/sound-source.ts +142 -45
  162. package/src/audio/sound-stream.ts +15 -5
  163. package/src/audio/sound.ts +12 -4
  164. package/src/audio/value.ts +89 -26
  165. package/src/graphics/blend-mode.ts +3 -1
  166. package/src/graphics/boolean-vector.ts +56 -26
  167. package/src/graphics/color.ts +3 -1
  168. package/src/graphics/convex-shape.ts +10 -3
  169. package/src/graphics/font.ts +171 -50
  170. package/src/graphics/glsl-matrix.ts +9 -3
  171. package/src/graphics/glsl.ts +81 -35
  172. package/src/graphics/glyph.ts +67 -26
  173. package/src/graphics/image.ts +119 -45
  174. package/src/graphics/rect.ts +56 -24
  175. package/src/graphics/render-states.ts +31 -14
  176. package/src/graphics/render-target.ts +197 -81
  177. package/src/graphics/render-texture.ts +36 -12
  178. package/src/graphics/render-window.ts +9 -3
  179. package/src/graphics/shader.ts +186 -61
  180. package/src/graphics/shape.ts +93 -35
  181. package/src/graphics/sprite.ts +64 -25
  182. package/src/graphics/stencil-mode.ts +12 -6
  183. package/src/graphics/swap.ts +3 -2
  184. package/src/graphics/text.ts +229 -74
  185. package/src/graphics/texture.ts +180 -61
  186. package/src/graphics/transform.ts +132 -47
  187. package/src/graphics/transformable.ts +3 -1
  188. package/src/graphics/vertex-array.ts +87 -35
  189. package/src/graphics/vertex-buffer.ts +97 -33
  190. package/src/graphics/vertex.ts +9 -3
  191. package/src/graphics/view.ts +59 -21
  192. package/src/index.ts +101 -23
  193. package/src/internal/audio-codec-registry.ts +18 -6
  194. package/src/internal/audio-input-stream.ts +6 -2
  195. package/src/internal/audio-music-pump.ts +116 -41
  196. package/src/internal/audio-native.ts +249 -96
  197. package/src/internal/audio-recorder-session.ts +68 -24
  198. package/src/internal/audio-worker-session.ts +65 -21
  199. package/src/internal/audio-worker.ts +85 -41
  200. package/src/internal/glsl-value.ts +26 -11
  201. package/src/internal/graphics-state.ts +57 -20
  202. package/src/internal/input-stream.ts +23 -10
  203. package/src/internal/native.ts +252 -99
  204. package/src/internal/numeric-properties.ts +2 -1
  205. package/src/internal/render-states.ts +3 -1
  206. package/src/internal/scalar.ts +33 -14
  207. package/src/internal/shaped-glyph.ts +25 -12
  208. package/src/internal/struct-view.ts +40 -13
  209. package/src/internal/transformable.ts +3 -1
  210. package/src/network/common.ts +35 -11
  211. package/src/network/dns.ts +95 -38
  212. package/src/network/ftp.ts +129 -47
  213. package/src/network/http.ts +232 -77
  214. package/src/network/ip-address.ts +118 -48
  215. package/src/network/packet.ts +161 -54
  216. package/src/network/sftp.ts +498 -168
  217. package/src/network/socket-selector.ts +66 -26
  218. package/src/network/socket.ts +133 -46
  219. package/src/network/tcp-listener.ts +78 -29
  220. package/src/network/tcp-socket.ts +240 -95
  221. package/src/network/tls-material.ts +6 -2
  222. package/src/network/udp-socket.ts +147 -53
  223. package/src/system/angle.ts +3 -1
  224. package/src/system/clock.ts +12 -4
  225. package/src/system/err.ts +5 -2
  226. package/src/system/file-input-stream.ts +12 -4
  227. package/src/system/input-stream.ts +9 -3
  228. package/src/system/operators.ts +290 -115
  229. package/src/system/sleep.ts +6 -2
  230. package/src/system/time.ts +18 -6
  231. package/src/system/timeout-with-predicate.ts +19 -8
  232. package/src/system/vector2.ts +101 -49
  233. package/src/system/vector3.ts +85 -36
  234. package/src/window/clipboard.ts +3 -1
  235. package/src/window/context.ts +15 -5
  236. package/src/window/cursor.ts +3 -1
  237. package/src/window/event.ts +26 -12
  238. package/src/window/mouse.ts +6 -2
  239. package/src/window/video-mode.ts +3 -1
  240. package/src/window/window.ts +119 -37
  241. package/test/native/audio-probes.cc +74 -36
  242. package/test/native/deferred-delete-probes.cc +17 -11
  243. package/test/native/drawable-probes.cc +70 -36
  244. package/test/native/exception-probes.cc +14 -9
  245. package/test/native/image-probes.cc +20 -10
  246. package/test/native/matrix-probes.cc +8 -4
  247. package/test/native/network-oracle.cc +17 -11
  248. package/test/native/render-texture-probes.cc +33 -24
  249. package/test/native/shader-probes.cc +9 -9
  250. package/test/native/shape-probes.cc +37 -23
  251. package/test/native/sprite-probes.cc +41 -25
  252. package/test/native/system-probes.cc +58 -35
  253. package/test/native/text-probes.cc +77 -44
  254. package/test/native/texture-probes.cc +43 -26
  255. package/test/native/time-probes.cc +18 -12
  256. package/test/native/transform-probes.cc +15 -10
  257. package/test/native/transformable-probes.cc +23 -17
  258. package/test/native/value-oracle.h +9 -4
  259. package/test/native/value-probes.cc +31 -23
  260. package/test/native/vertex-array-probes.cc +22 -14
  261. package/test/native/vertex-buffer-probes.cc +28 -17
  262. package/test/native/view-probes.cc +29 -17
  263. package/test/native/window-probes.cc +66 -33
  264. package/tools/ensure-sfml.cjs +9 -0
  265. package/tools/generate-network-constants.cjs +11 -6
  266. package/tools/generate-window-constants.cjs +31 -12
  267. package/docs/README.ja.md +0 -30
  268. package/docs/README.ko.md +0 -30
  269. package/docs/README.md +0 -30
  270. package/docs/README.zh-CN.md +0 -30
  271. package/docs/api.ja.md +0 -84
  272. package/docs/api.ko.md +0 -84
  273. package/docs/api.md +0 -81
  274. package/docs/api.zh-CN.md +0 -81
  275. package/docs/audio.ja.md +0 -249
  276. package/docs/audio.ko.md +0 -251
  277. package/docs/audio.md +0 -252
  278. package/docs/audio.zh-CN.md +0 -252
  279. package/docs/concepts.ja.md +0 -130
  280. package/docs/concepts.ko.md +0 -130
  281. package/docs/concepts.md +0 -131
  282. package/docs/concepts.zh-CN.md +0 -131
  283. package/docs/development.ja.md +0 -144
  284. package/docs/development.ko.md +0 -144
  285. package/docs/development.md +0 -146
  286. package/docs/development.zh-CN.md +0 -146
  287. package/docs/getting-started.ja.md +0 -154
  288. package/docs/getting-started.ko.md +0 -154
  289. package/docs/getting-started.md +0 -156
  290. package/docs/getting-started.zh-CN.md +0 -156
  291. package/docs/graphics.ja.md +0 -256
  292. package/docs/graphics.ko.md +0 -254
  293. package/docs/graphics.md +0 -255
  294. package/docs/graphics.zh-CN.md +0 -257
  295. package/docs/network.ja.md +0 -214
  296. package/docs/network.ko.md +0 -216
  297. package/docs/network.md +0 -218
  298. package/docs/network.zh-CN.md +0 -218
  299. package/docs/system.ja.md +0 -169
  300. package/docs/system.ko.md +0 -171
  301. package/docs/system.md +0 -173
  302. package/docs/system.zh-CN.md +0 -173
  303. package/docs/window.ja.md +0 -138
  304. package/docs/window.ko.md +0 -140
  305. package/docs/window.md +0 -141
  306. package/docs/window.zh-CN.md +0 -141
  307. package/native/exception-boundary.h +0 -97
  308. package/native/numeric-fields.h +0 -96
  309. package/native/shared-handle.h +0 -107
  310. package/test/tsconfig.json +0 -8
  311. package/tools/configure-lsp.cjs +0 -23
  312. package/tools/lib_downloader.cjs +0 -92
  313. package/tools/lib_downloader_urls.cjs +0 -23
@@ -1,28 +1,29 @@
1
1
  #pragma once
2
2
 
3
+ #include <array>
4
+ #include <functional>
5
+ #include <span> // NOLINT(build/include_order): cpplint lacks C++20 headers
6
+ #include <string>
7
+ #include <vector>
8
+
3
9
  #include "deferred-delete.h"
4
10
  #include "exception-boundary.h"
5
11
  #include "numeric-fields.h"
6
-
7
12
  #include <SFML/Audio.hpp>
8
13
  #include <SFML/System/MemoryInputStream.hpp>
9
14
 
10
- #include <array>
11
- #include <functional>
12
- #include <span>
13
- #include <string>
14
- #include <vector>
15
-
16
15
  namespace sfmljs::audio {
17
16
 
18
17
  using Info = Nan::FunctionCallbackInfo<v8::Value>;
19
18
 
20
- inline void Set(v8::Local<v8::Object> object, const char* name,
19
+ inline void Set(v8::Local<v8::Object> object,
20
+ const char* name,
21
21
  v8::Local<v8::Value> value) {
22
22
  Nan::Set(object, Nan::New(name).ToLocalChecked(), value).FromJust();
23
23
  }
24
24
 
25
- inline bool Unsigned(v8::Local<v8::Value> value, unsigned& result) {
25
+ inline bool Unsigned(v8::Local<v8::Value> value,
26
+ unsigned& result) { // NOLINT(runtime/references)
26
27
  if (!value->IsUint32()) {
27
28
  Nan::ThrowRangeError("Expected an unsigned 32-bit integer");
28
29
  return false;
@@ -31,7 +32,8 @@ inline bool Unsigned(v8::Local<v8::Value> value, unsigned& result) {
31
32
  return true;
32
33
  }
33
34
 
34
- inline bool String(v8::Local<v8::Value> value, std::string& result) {
35
+ inline bool String(v8::Local<v8::Value> value,
36
+ std::string& result) { // NOLINT(runtime/references)
35
37
  if (!value->IsString()) {
36
38
  Nan::ThrowTypeError("Expected a string");
37
39
  return false;
@@ -56,10 +58,12 @@ struct Bytes {
56
58
  std::size_t size{};
57
59
  };
58
60
 
59
- inline bool ReadBytes(v8::Local<v8::Value> value, Bytes& result,
61
+ inline bool ReadBytes(v8::Local<v8::Value> value,
62
+ Bytes& result, // NOLINT(runtime/references)
60
63
  bool samples = false) {
61
64
  if (samples ? !value->IsInt16Array() : !value->IsUint8Array()) {
62
- Nan::ThrowTypeError(samples ? "Expected an Int16Array" : "Expected a Uint8Array");
65
+ Nan::ThrowTypeError(samples ? "Expected an Int16Array"
66
+ : "Expected a Uint8Array");
63
67
  return false;
64
68
  }
65
69
  auto view = value.As<v8::ArrayBufferView>();
@@ -73,13 +77,14 @@ inline bool ReadBytes(v8::Local<v8::Value> value, Bytes& result,
73
77
  Nan::ThrowTypeError("Expected attached, fixed, non-shared storage");
74
78
  return false;
75
79
  }
76
- result = {backing, static_cast<std::byte*>(backing->Data()) + view->ByteOffset(),
80
+ result = {backing,
81
+ static_cast<std::byte*>(backing->Data()) + view->ByteOffset(),
77
82
  view->ByteLength()};
78
83
  return true;
79
84
  }
80
85
 
81
86
  inline bool Channels(v8::Local<v8::Value> value,
82
- std::vector<sf::SoundChannel>& result) {
87
+ std::vector<sf::SoundChannel>& result) { // NOLINT
83
88
  if (!value->IsInt32Array()) {
84
89
  Nan::ThrowTypeError("Expected an Int32Array channel map");
85
90
  return false;
@@ -91,7 +96,8 @@ inline bool Channels(v8::Local<v8::Value> value,
91
96
  }
92
97
  Nan::TypedArrayContents<std::int32_t> data(value);
93
98
  for (std::size_t i = 0; i < view->Length(); ++i) {
94
- if ((*data)[i] < 0 || (*data)[i] > static_cast<int>(sf::SoundChannel::TopBackCenter)) {
99
+ if ((*data)[i] < 0 ||
100
+ (*data)[i] > static_cast<int>(sf::SoundChannel::TopBackCenter)) {
95
101
  Nan::ThrowRangeError("Unknown sound channel");
96
102
  return false;
97
103
  }
@@ -101,12 +107,15 @@ inline bool Channels(v8::Local<v8::Value> value,
101
107
  }
102
108
 
103
109
  template <typename Owner>
104
- v8::Local<v8::ArrayBuffer> MapChannels(const std::shared_ptr<Owner>& owner,
105
- const std::vector<sf::SoundChannel>& map) {
110
+ v8::Local<v8::ArrayBuffer> MapChannels(
111
+ const std::shared_ptr<Owner>& owner,
112
+ const std::vector<sf::SoundChannel>& map) {
106
113
  static_assert(sizeof(sf::SoundChannel) == 4);
107
114
  return map.empty() ? v8::ArrayBuffer::New(v8::Isolate::GetCurrent(), 0)
108
- : MapShared(v8::Isolate::GetCurrent(), owner,
109
- const_cast<sf::SoundChannel*>(map.data()), map.size() * sizeof(map[0]));
115
+ : MapShared(v8::Isolate::GetCurrent(),
116
+ owner,
117
+ const_cast<sf::SoundChannel*>(map.data()),
118
+ map.size() * sizeof(map[0]));
110
119
  }
111
120
 
112
121
  // Derived facts and async results only. SFML-owned scalar members map directly
@@ -147,7 +156,7 @@ struct Buffer {
147
156
  // Control/result storage is confined to the owning JS thread. The SFML calls
148
157
  // implement the engine's conversions and synchronization, not naked writes.
149
158
  struct Source {
150
- unsigned kind{1}; // 0 Sound, 1 Music, 2 custom SoundStream
159
+ unsigned kind{1}; // 0 Sound, 1 Music, 2 custom SoundStream
151
160
  std::shared_ptr<Buffer> buffer;
152
161
  std::shared_ptr<BufferData> samples;
153
162
  Bytes encoded;
@@ -177,10 +186,18 @@ using SourceHandle = SharedHandle<Source>;
177
186
  inline v8::Local<v8::Object> MapSource(const std::shared_ptr<Source>& source) {
178
187
  auto result = Nan::New<v8::Object>();
179
188
  Set(result, "handle", SourceHandle::Create(source));
180
- Set(result, "values", MapShared(v8::Isolate::GetCurrent(), source,
181
- source->values.data(), sizeof(source->values)));
182
- Set(result, "times", MapShared(v8::Isolate::GetCurrent(), source,
183
- source->times.data(), sizeof(source->times)));
189
+ Set(result,
190
+ "values",
191
+ MapShared(v8::Isolate::GetCurrent(),
192
+ source,
193
+ source->values.data(),
194
+ sizeof(source->values)));
195
+ Set(result,
196
+ "times",
197
+ MapShared(v8::Isolate::GetCurrent(),
198
+ source,
199
+ source->times.data(),
200
+ sizeof(source->times)));
184
201
  return result;
185
202
  }
186
203
 
@@ -189,7 +206,8 @@ class Job final : public sfmljs::AsyncWorker {
189
206
  using Action = std::function<void()>;
190
207
  using Result = std::function<v8::Local<v8::Value>()>;
191
208
  Job(Nan::Callback* callback, Action work, Result result)
192
- : AsyncWorker(callback, "sfml:Audio"), work_(std::move(work)),
209
+ : AsyncWorker(callback, "sfml:Audio"),
210
+ work_(std::move(work)),
193
211
  result_(std::move(result)) {}
194
212
  void ExecuteWork() override { work_(); }
195
213
  void CompleteWork() override {
@@ -197,16 +215,20 @@ class Job final : public sfmljs::AsyncWorker {
197
215
  v8::Local<v8::Value> args[] = {Nan::Null(), result_()};
198
216
  callback->Call(2, args, async_resource);
199
217
  }
218
+
200
219
  private:
201
220
  Action work_;
202
221
  Result result_;
203
222
  };
204
223
 
205
- inline void Queue(v8::Local<v8::Value> callback, Job::Action work,
224
+ inline void Queue(v8::Local<v8::Value> callback,
225
+ Job::Action work,
206
226
  Job::Result result) {
207
- if (!callback->IsFunction()) return Nan::ThrowTypeError("Expected a callback");
227
+ if (!callback->IsFunction())
228
+ return Nan::ThrowTypeError("Expected a callback");
208
229
  auto cb = std::make_unique<Nan::Callback>(callback.As<v8::Function>());
209
- auto job = std::make_unique<Job>(cb.get(), std::move(work), std::move(result));
230
+ auto job =
231
+ std::make_unique<Job>(cb.get(), std::move(work), std::move(result));
210
232
  cb.release();
211
233
  Nan::AsyncQueueWorker(job.release());
212
234
  }
@@ -214,7 +236,8 @@ inline void Queue(v8::Local<v8::Value> callback, Job::Action work,
214
236
  v8::Local<v8::Object> MapBuffer(const std::shared_ptr<BufferData>& data);
215
237
  void CommitBuffer(const std::shared_ptr<Buffer>& buffer,
216
238
  const std::shared_ptr<BufferData>& data);
217
- void CopySourceSettings(sf::SoundSource& to, const sf::SoundSource& from);
239
+ void CopySourceSettings(sf::SoundSource& to, // NOLINT(runtime/references)
240
+ const sf::SoundSource& from);
218
241
  void InstallSourceEffect(const std::shared_ptr<Source>& source);
219
242
 
220
- } // namespace sfmljs::audio
243
+ } // namespace sfmljs::audio
@@ -1,9 +1,9 @@
1
- #include "audio-stream.h"
2
-
3
1
  #include <atomic>
4
- #include <condition_variable>
2
+ #include <condition_variable> // NOLINT(build/c++11)
5
3
  #include <cstring>
6
- #include <thread>
4
+ #include <thread> // NOLINT(build/c++11)
5
+
6
+ #include "audio-stream.h"
7
7
 
8
8
  namespace sfmljs::audio {
9
9
  namespace {
@@ -28,58 +28,80 @@ NAN_METHOD(CompleteStreamRequest) {
28
28
  auto request = handle->owner();
29
29
  std::lock_guard lock(request->mutex);
30
30
  if (request->complete) return;
31
- GuardExceptions([&] {
32
- if (!info[0]->IsNullOrUndefined()) {
33
- Nan::Utf8String error(info[0]);
34
- request->error.assign(*error, error.length());
35
- if (request->error.empty()) request->error = "Audio InputStream failed";
36
- } else if (!info[1]->IsUndefined()) {
37
- bool lossless{};
38
- const auto value = info[1]->IsBigInt()
39
- ? info[1].As<v8::BigInt>()->Uint64Value(&lossless) : 0;
40
- if (!lossless || value > std::numeric_limits<std::size_t>::max() ||
41
- (request->operation == 0 && value > request->bytes.size())) {
42
- request->error = "Stream result must be a non-negative bigint within range";
43
- } else {
44
- request->result = static_cast<std::size_t>(value);
45
- if (request->operation == 0) {
46
- request->snapshot.assign(request->bytes.begin(), request->bytes.begin() + value);
31
+ GuardExceptions(
32
+ [&] {
33
+ if (!info[0]->IsNullOrUndefined()) {
34
+ Nan::Utf8String error(info[0]);
35
+ request->error.assign(*error, error.length());
36
+ if (request->error.empty())
37
+ request->error = "Audio InputStream failed";
38
+ } else if (!info[1]->IsUndefined()) {
39
+ bool lossless{};
40
+ const auto value =
41
+ info[1]->IsBigInt()
42
+ ? info[1].As<v8::BigInt>()->Uint64Value(&lossless)
43
+ : 0;
44
+ if (!lossless || value > std::numeric_limits<std::size_t>::max() ||
45
+ (request->operation == 0 && value > request->bytes.size())) {
46
+ request->error =
47
+ "Stream result must be a non-negative bigint within range";
48
+ } else {
49
+ request->result = static_cast<std::size_t>(value);
50
+ if (request->operation == 0) {
51
+ request->snapshot.assign(request->bytes.begin(),
52
+ request->bytes.begin() + value);
53
+ }
54
+ }
47
55
  }
48
- }
49
- }
50
- }, [&](const char* error) { request->error = error; });
56
+ },
57
+ [&](const char* error) { request->error = error; });
51
58
  request->complete = true;
52
59
  request->ready.notify_one();
53
60
  }
54
61
 
55
62
  class ThreadJob {
56
63
  public:
57
- ThreadJob(v8::Local<v8::Function> callback, Job::Action work,
58
- Job::Result result, Job::Action cancel)
59
- : callback_(callback), resource_("sfml:AudioStream"),
60
- work_(std::move(work)), result_(std::move(result)), cancel_(std::move(cancel)) {}
64
+ ThreadJob(v8::Local<v8::Function> callback,
65
+ Job::Action work,
66
+ Job::Result result,
67
+ Job::Action cancel)
68
+ : callback_(callback),
69
+ resource_("sfml:AudioStream"),
70
+ work_(std::move(work)),
71
+ result_(std::move(result)),
72
+ cancel_(std::move(cancel)) {}
61
73
  void Start() {
62
- const int status = uv_async_init(node::GetCurrentEventLoop(v8::Isolate::GetCurrent()),
63
- &async_, [](uv_async_t* handle) { static_cast<ThreadJob*>(handle->data)->Finish(); });
74
+ const int status =
75
+ uv_async_init(node::GetCurrentEventLoop(v8::Isolate::GetCurrent()),
76
+ &async_,
77
+ [](uv_async_t* handle) {
78
+ static_cast<ThreadJob*>(handle->data)->Finish();
79
+ });
64
80
  if (status) throw std::runtime_error(uv_strerror(status));
65
81
  async_.data = this;
66
- cleanup_ = node::AddEnvironmentCleanupHook(v8::Isolate::GetCurrent(),
82
+ cleanup_ = node::AddEnvironmentCleanupHook(
83
+ v8::Isolate::GetCurrent(),
67
84
  [](void* pointer, void (*done)(void*), void* argument) {
68
85
  auto* job = static_cast<ThreadJob*>(pointer);
69
86
  job->cleanupDone_ = done;
70
87
  job->cleanupArgument_ = argument;
71
88
  if (job->cancel_) job->cancel_();
72
- }, this);
73
- GuardExceptions([&] {
74
- thread_ = std::thread([this] {
75
- GuardExceptions(work_, [this](const char* error) { error_ = error; });
76
- uv_async_send(&async_);
77
- });
78
- }, [this](const char* error) {
79
- error_ = error;
80
- uv_async_send(&async_);
81
- });
89
+ },
90
+ this);
91
+ GuardExceptions(
92
+ [&] {
93
+ thread_ = std::thread([this] {
94
+ GuardExceptions(work_,
95
+ [this](const char* error) { error_ = error; });
96
+ uv_async_send(&async_);
97
+ });
98
+ },
99
+ [this](const char* error) {
100
+ error_ = error;
101
+ uv_async_send(&async_);
102
+ });
82
103
  }
104
+
83
105
  private:
84
106
  void Finish() {
85
107
  if (thread_.joinable()) thread_.join();
@@ -92,7 +114,8 @@ class ThreadJob {
92
114
  [this](const char* error) { error_ = error; });
93
115
  }
94
116
  v8::Local<v8::Value> error = error_.empty()
95
- ? v8::Local<v8::Value>(Nan::Null()) : Error(error_.c_str());
117
+ ? v8::Local<v8::Value>(Nan::Null())
118
+ : Error(error_.c_str());
96
119
  v8::Local<v8::Value> args[] = {error, value};
97
120
  callback_.Call(2, args, &resource_);
98
121
  }
@@ -113,16 +136,21 @@ class ThreadJob {
113
136
  Job::Action cancel_;
114
137
  std::string error_;
115
138
  node::AsyncCleanupHookHandle cleanup_;
116
- void (*cleanupDone_)(void*){};
139
+ void (*cleanupDone_)(void*) = nullptr;
117
140
  void* cleanupArgument_{};
118
141
  };
119
- }
142
+ } // namespace
120
143
 
121
- void QueueThread(v8::Local<v8::Value> callback, Job::Action work,
122
- Job::Result result, Job::Action cancel) {
123
- if (!callback->IsFunction()) return Nan::ThrowTypeError("Expected a callback");
144
+ void QueueThread(v8::Local<v8::Value> callback,
145
+ Job::Action work,
146
+ Job::Result result,
147
+ Job::Action cancel) {
148
+ if (!callback->IsFunction())
149
+ return Nan::ThrowTypeError("Expected a callback");
124
150
  auto job = std::make_unique<ThreadJob>(callback.As<v8::Function>(),
125
- std::move(work), std::move(result), std::move(cancel));
151
+ std::move(work),
152
+ std::move(result),
153
+ std::move(cancel));
126
154
  job->Start();
127
155
  job.release();
128
156
  }
@@ -148,8 +176,8 @@ struct StreamClient::Bridge : std::enable_shared_from_this<Bridge> {
148
176
  }
149
177
  void Start() {
150
178
  isolate = v8::Isolate::GetCurrent();
151
- const int status = uv_async_init(node::GetCurrentEventLoop(isolate), &async,
152
- [](uv_async_t* handle) {
179
+ const int status = uv_async_init(
180
+ node::GetCurrentEventLoop(isolate), &async, [](uv_async_t* handle) {
153
181
  auto self = *static_cast<std::shared_ptr<Bridge>*>(handle->data);
154
182
  self->Dispatch();
155
183
  });
@@ -175,7 +203,8 @@ struct StreamClient::Bridge : std::enable_shared_from_this<Bridge> {
175
203
  if (closing) return;
176
204
  closing = true;
177
205
  }
178
- if (!cleanup) node::RemoveEnvironmentCleanupHook(isolate, Cleanup, async.data);
206
+ if (!cleanup)
207
+ node::RemoveEnvironmentCleanupHook(isolate, Cleanup, async.data);
179
208
  dispatch.Reset();
180
209
  resource.reset();
181
210
  uv_close(reinterpret_cast<uv_handle_t*>(&async), [](uv_handle_t* handle) {
@@ -187,7 +216,8 @@ struct StreamClient::Bridge : std::enable_shared_from_this<Bridge> {
187
216
  next->operation = operation;
188
217
  next->position = value;
189
218
  if (operation == 0) {
190
- if (value > node::Buffer::kMaxLength) throw std::length_error("Stream read too large");
219
+ if (value > node::Buffer::kMaxLength)
220
+ throw std::length_error("Stream read too large");
191
221
  next->bytes.resize(value);
192
222
  }
193
223
  {
@@ -209,49 +239,67 @@ struct StreamClient::Bridge : std::enable_shared_from_this<Bridge> {
209
239
  cancel = cancelled;
210
240
  next = request;
211
241
  }
212
- if (cancel) { Close(); return; }
242
+ if (cancel) {
243
+ Close();
244
+ return;
245
+ }
213
246
  if (!next || next->dispatched) return;
214
247
  next->dispatched = true;
215
248
  Nan::HandleScope scope;
216
- GuardExceptions([&] {
217
- auto complete = Nan::GetFunction(Nan::New<v8::FunctionTemplate>(
218
- CompleteStreamRequest, RequestHandle::Create(next))).ToLocalChecked();
219
- v8::Local<v8::Value> data = Nan::Undefined();
220
- if (next->operation == 0) {
221
- auto buffer = next->bytes.empty() ? v8::ArrayBuffer::New(isolate, 0)
222
- : MapShared(isolate, next, next->bytes.data(), next->bytes.size());
223
- data = v8::Uint8Array::New(buffer, 0, next->bytes.size());
224
- }
225
- v8::Local<v8::Value> args[] = {
226
- Nan::New(next->operation), data,
227
- v8::BigInt::NewFromUnsigned(isolate, next->position), complete,
228
- };
229
- Nan::TryCatch caught;
230
- dispatch.Call(4, args, resource.get());
231
- if (caught.HasCaught()) {
232
- std::lock_guard lock(next->mutex);
233
- if (!next->complete) {
234
- Nan::Utf8String error(caught.Exception());
235
- next->error.assign(*error, error.length());
236
- next->complete = true;
237
- next->ready.notify_one();
238
- }
239
- }
240
- }, [&](const char* error) {
241
- std::lock_guard lock(next->mutex);
242
- if (!next->complete) {
243
- next->error = error;
244
- next->complete = true;
245
- next->ready.notify_one();
246
- }
247
- });
249
+ GuardExceptions(
250
+ [&] {
251
+ auto complete = Nan::GetFunction(Nan::New<v8::FunctionTemplate>(
252
+ CompleteStreamRequest,
253
+ RequestHandle::Create(next)))
254
+ .ToLocalChecked();
255
+ v8::Local<v8::Value> data = Nan::Undefined();
256
+ if (next->operation == 0) {
257
+ auto buffer = next->bytes.empty() ? v8::ArrayBuffer::New(isolate, 0)
258
+ : MapShared(isolate,
259
+ next,
260
+ next->bytes.data(),
261
+ next->bytes.size());
262
+ data = v8::Uint8Array::New(buffer, 0, next->bytes.size());
263
+ }
264
+ v8::Local<v8::Value> args[] = {
265
+ Nan::New(next->operation),
266
+ data,
267
+ v8::BigInt::NewFromUnsigned(isolate, next->position),
268
+ complete,
269
+ };
270
+ Nan::TryCatch caught;
271
+ dispatch.Call(4, args, resource.get());
272
+ if (caught.HasCaught()) {
273
+ std::lock_guard lock(next->mutex);
274
+ if (!next->complete) {
275
+ Nan::Utf8String error(caught.Exception());
276
+ next->error.assign(*error, error.length());
277
+ next->complete = true;
278
+ next->ready.notify_one();
279
+ }
280
+ }
281
+ },
282
+ [&](const char* error) {
283
+ std::lock_guard lock(next->mutex);
284
+ if (!next->complete) {
285
+ next->error = error;
286
+ next->complete = true;
287
+ next->ready.notify_one();
288
+ }
289
+ });
248
290
  }
249
291
  };
250
292
 
251
293
  StreamClient::StreamClient(v8::Local<v8::Function> dispatch)
252
- : bridge(std::make_shared<Bridge>(dispatch)) { bridge->Start(); }
253
- StreamClient::~StreamClient() { bridge->Cancel(); }
254
- void StreamClient::Cancel() { bridge->Cancel(); }
294
+ : bridge(std::make_shared<Bridge>(dispatch)) {
295
+ bridge->Start();
296
+ }
297
+ StreamClient::~StreamClient() {
298
+ bridge->Cancel();
299
+ }
300
+ void StreamClient::Cancel() {
301
+ bridge->Cancel();
302
+ }
255
303
  std::optional<std::size_t> StreamClient::read(void* data, std::size_t size) {
256
304
  auto request = bridge->Exchange(0, size);
257
305
  if (request->result && *request->result) {
@@ -262,11 +310,16 @@ std::optional<std::size_t> StreamClient::read(void* data, std::size_t size) {
262
310
  std::optional<std::size_t> StreamClient::seek(std::size_t position) {
263
311
  return bridge->Exchange(1, position)->result;
264
312
  }
265
- std::optional<std::size_t> StreamClient::tell() { return bridge->Exchange(2)->result; }
266
- std::optional<std::size_t> StreamClient::getSize() { return bridge->Exchange(3)->result; }
313
+ std::optional<std::size_t> StreamClient::tell() {
314
+ return bridge->Exchange(2)->result;
315
+ }
316
+ std::optional<std::size_t> StreamClient::getSize() {
317
+ return bridge->Exchange(3)->result;
318
+ }
267
319
 
268
320
  NAN_METHOD(CreateAudioInputStream) {
269
- if (!info[0]->IsFunction()) return Nan::ThrowTypeError("Expected stream dispatch");
321
+ if (!info[0]->IsFunction())
322
+ return Nan::ThrowTypeError("Expected stream dispatch");
270
323
  info.GetReturnValue().Set(StreamClientHandle::Create(
271
324
  std::make_shared<StreamClient>(info[0].As<v8::Function>())));
272
325
  }
@@ -275,10 +328,11 @@ NAN_METHOD(CloseAudioInputStream) {
275
328
  if (handle) handle->owner()->Cancel();
276
329
  }
277
330
 
278
- } // namespace sfmljs::audio
331
+ } // namespace sfmljs::audio
279
332
 
280
333
  NAN_MODULE_INIT(InitializeAudioStreams) {
281
- using namespace sfmljs::audio;
282
- sfmljs::SetMethod<CreateAudioInputStream>(target, "createAudioInputStream");
283
- sfmljs::SetMethod<CloseAudioInputStream>(target, "closeAudioInputStream");
334
+ sfmljs::SetMethod<sfmljs::audio::CreateAudioInputStream>(
335
+ target, "createAudioInputStream");
336
+ sfmljs::SetMethod<sfmljs::audio::CloseAudioInputStream>(
337
+ target, "closeAudioInputStream");
284
338
  }
@@ -1,7 +1,6 @@
1
1
  #pragma once
2
2
 
3
3
  #include "audio-state.h"
4
-
5
4
  #include <SFML/System/InputStream.hpp>
6
5
 
7
6
  namespace sfmljs::audio {
@@ -22,7 +21,9 @@ struct StreamClient : sf::InputStream {
22
21
 
23
22
  using StreamClientHandle = SharedHandle<StreamClient>;
24
23
 
25
- void QueueThread(v8::Local<v8::Value> callback, Job::Action work,
26
- Job::Result result, Job::Action cancel = {});
24
+ void QueueThread(v8::Local<v8::Value> callback,
25
+ Job::Action work,
26
+ Job::Result result,
27
+ Job::Action cancel = {});
27
28
 
28
- } // namespace sfmljs::audio
29
+ } // namespace sfmljs::audio