krillplayer-plugin 0.0.0-stage → 0.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 (276) hide show
  1. package/LICENSE +24 -0
  2. package/README.md +85 -2
  3. package/bin/krillplayer-plugin.js +13 -0
  4. package/dist/cli.js +5309 -0
  5. package/dist/engine/engine.c +140 -0
  6. package/kernel/Sources/CQuickJS/include/kernel_qjs.h +47 -0
  7. package/kernel/Sources/CQuickJS/kernel_qjs.c +407 -0
  8. package/kernel/Sources/CQuickJS/quickjs_vendor.c +8 -0
  9. package/kernel/Sources/CQuickJS/vendor/LICENSE +24 -0
  10. package/kernel/Sources/CQuickJS/vendor/builtin-array-fromasync.h +121 -0
  11. package/kernel/Sources/CQuickJS/vendor/builtin-iterator-zip-keyed.h +313 -0
  12. package/kernel/Sources/CQuickJS/vendor/builtin-iterator-zip.h +321 -0
  13. package/kernel/Sources/CQuickJS/vendor/cutils.h +2374 -0
  14. package/kernel/Sources/CQuickJS/vendor/dtoa.c +1619 -0
  15. package/kernel/Sources/CQuickJS/vendor/dtoa.h +87 -0
  16. package/kernel/Sources/CQuickJS/vendor/libregexp-opcode.h +73 -0
  17. package/kernel/Sources/CQuickJS/vendor/libregexp.c +3530 -0
  18. package/kernel/Sources/CQuickJS/vendor/libregexp.h +106 -0
  19. package/kernel/Sources/CQuickJS/vendor/libunicode-table.h +5160 -0
  20. package/kernel/Sources/CQuickJS/vendor/libunicode.c +2074 -0
  21. package/kernel/Sources/CQuickJS/vendor/libunicode.h +172 -0
  22. package/kernel/Sources/CQuickJS/vendor/list.h +107 -0
  23. package/kernel/Sources/CQuickJS/vendor/quickjs-atom.h +279 -0
  24. package/kernel/Sources/CQuickJS/vendor/quickjs-c-atomics.h +54 -0
  25. package/kernel/Sources/CQuickJS/vendor/quickjs-opcode.h +377 -0
  26. package/kernel/Sources/CQuickJS/vendor/quickjs.c +65536 -0
  27. package/kernel/Sources/CQuickJS/vendor/quickjs.h +1529 -0
  28. package/kernel/Sources/Kernel/PluginPrelude.js +312 -0
  29. package/package.json +35 -4
  30. package/skill/krillplayer-plugin/SKILL.md +100 -0
  31. package/skill/krillplayer-plugin/assets/samples/catalog/cases/list-next-page-passes-the-cursor-back.json +28 -0
  32. package/skill/krillplayer-plugin/assets/samples/catalog/cases/list-popular-movies.json +48 -0
  33. package/skill/krillplayer-plugin/assets/samples/catalog/cases/list-service-unavailable-fails.json +22 -0
  34. package/skill/krillplayer-plugin/assets/samples/catalog/cases/list-top-rated-series.json +68 -0
  35. package/skill/krillplayer-plugin/assets/samples/catalog/cases/list-trending-movies-by-default.json +71 -0
  36. package/skill/krillplayer-plugin/assets/samples/catalog/cases/list-unknown-values-fall-back.json +26 -0
  37. package/skill/krillplayer-plugin/assets/samples/catalog/cases/parameters.json +42 -0
  38. package/skill/krillplayer-plugin/assets/samples/catalog/cases/search-blank.json +11 -0
  39. package/skill/krillplayer-plugin/assets/samples/catalog/cases/search-movies-first.json +50 -0
  40. package/skill/krillplayer-plugin/assets/samples/catalog/cases/search-series-after-the-movies.json +50 -0
  41. package/skill/krillplayer-plugin/assets/samples/catalog/cases/search-series-next-page.json +25 -0
  42. package/skill/krillplayer-plugin/assets/samples/catalog/manifest.ts +12 -0
  43. package/skill/krillplayer-plugin/assets/samples/catalog/plugin.ts +57 -0
  44. package/skill/krillplayer-plugin/assets/samples/danmaku-tool/cases/comments-first-segment.json +77 -0
  45. package/skill/krillplayer-plugin/assets/samples/danmaku-tool/cases/comments-last-segment-stops-at-the-end.json +77 -0
  46. package/skill/krillplayer-plugin/assets/samples/danmaku-tool/cases/comments-past-the-end-empty.json +14 -0
  47. package/skill/krillplayer-plugin/assets/samples/danmaku-tool/cases/comments-segment-covering-the-position.json +77 -0
  48. package/skill/krillplayer-plugin/assets/samples/danmaku-tool/cases/comments-unknown-match-not-found.json +13 -0
  49. package/skill/krillplayer-plugin/assets/samples/danmaku-tool/cases/match-by-filename-with-spaces.json +34 -0
  50. package/skill/krillplayer-plugin/assets/samples/danmaku-tool/cases/match-by-filename-without-a-work.json +28 -0
  51. package/skill/krillplayer-plugin/assets/samples/danmaku-tool/cases/match-episode-by-absolute-number.json +25 -0
  52. package/skill/krillplayer-plugin/assets/samples/danmaku-tool/cases/match-episode-by-season-and-number.json +32 -0
  53. package/skill/krillplayer-plugin/assets/samples/danmaku-tool/cases/match-episode-not-in-the-catalog-empty.json +20 -0
  54. package/skill/krillplayer-plugin/assets/samples/danmaku-tool/cases/match-movie-by-tmdb-gives-both-cuts.json +34 -0
  55. package/skill/krillplayer-plugin/assets/samples/danmaku-tool/cases/match-nothing-to-go-on-empty.json +13 -0
  56. package/skill/krillplayer-plugin/assets/samples/danmaku-tool/cases/match-unknown-work-empty.json +18 -0
  57. package/skill/krillplayer-plugin/assets/samples/danmaku-tool/manifest.ts +12 -0
  58. package/skill/krillplayer-plugin/assets/samples/danmaku-tool/plugin.ts +72 -0
  59. package/skill/krillplayer-plugin/assets/samples/file-share/cases/folder-gone-ended.json +12 -0
  60. package/skill/krillplayer-plugin/assets/samples/file-share/cases/folder-second-page-by-cursor.json +39 -0
  61. package/skill/krillplayer-plugin/assets/samples/file-share/cases/folder-skips-files-that-are-not-media.json +36 -0
  62. package/skill/krillplayer-plugin/assets/samples/file-share/cases/folder-with-subfolder-first-page.json +52 -0
  63. package/skill/krillplayer-plugin/assets/samples/file-share/cases/library-empty-folder.json +11 -0
  64. package/skill/krillplayer-plugin/assets/samples/file-share/cases/library-folder-gone-not-found.json +12 -0
  65. package/skill/krillplayer-plugin/assets/samples/file-share/cases/library-folder-lists-subfolders-and-media.json +28 -0
  66. package/skill/krillplayer-plugin/assets/samples/file-share/cases/library-folder-second-page.json +22 -0
  67. package/skill/krillplayer-plugin/assets/samples/file-share/cases/library-folder-skips-subtitles.json +21 -0
  68. package/skill/krillplayer-plugin/assets/samples/file-share/cases/library-root-first-page.json +25 -0
  69. package/skill/krillplayer-plugin/assets/samples/file-share/cases/library-root-second-page.json +22 -0
  70. package/skill/krillplayer-plugin/assets/samples/file-share/cases/list-cursor-not-a-number-starts-over.json +30 -0
  71. package/skill/krillplayer-plugin/assets/samples/file-share/cases/list-root-first-page.json +29 -0
  72. package/skill/krillplayer-plugin/assets/samples/file-share/cases/list-root-second-page.json +25 -0
  73. package/skill/krillplayer-plugin/assets/samples/file-share/cases/nested-folder-carries-ancestors.json +25 -0
  74. package/skill/krillplayer-plugin/assets/samples/file-share/cases/play-file-at-the-root.json +22 -0
  75. package/skill/krillplayer-plugin/assets/samples/file-share/cases/play-file-encodes-each-path-segment.json +22 -0
  76. package/skill/krillplayer-plugin/assets/samples/file-share/cases/play-file-gone-not-found.json +12 -0
  77. package/skill/krillplayer-plugin/assets/samples/file-share/manifest.ts +12 -0
  78. package/skill/krillplayer-plugin/assets/samples/file-share/plugin.ts +192 -0
  79. package/skill/krillplayer-plugin/assets/samples/internet-archive/cases/list-first-page.json +247 -0
  80. package/skill/krillplayer-plugin/assets/samples/internet-archive/cases/list-missing-docs.json +26 -0
  81. package/skill/krillplayer-plugin/assets/samples/internet-archive/cases/list-not-json.json +26 -0
  82. package/skill/krillplayer-plugin/assets/samples/internet-archive/cases/list-refused.json +26 -0
  83. package/skill/krillplayer-plugin/assets/samples/internet-archive/cases/list-second-page.json +248 -0
  84. package/skill/krillplayer-plugin/assets/samples/internet-archive/cases/list-server-error.json +26 -0
  85. package/skill/krillplayer-plugin/assets/samples/internet-archive/cases/list-skips-incomplete-items.json +225 -0
  86. package/skill/krillplayer-plugin/assets/samples/internet-archive/cases/play-about-bananas.json +45 -0
  87. package/skill/krillplayer-plugin/assets/samples/internet-archive/cases/play-http-404.json +26 -0
  88. package/skill/krillplayer-plugin/assets/samples/internet-archive/cases/play-no-video-files.json +26 -0
  89. package/skill/krillplayer-plugin/assets/samples/internet-archive/cases/play-unknown-id.json +26 -0
  90. package/skill/krillplayer-plugin/assets/samples/internet-archive/cases/search-bad-request.json +26 -0
  91. package/skill/krillplayer-plugin/assets/samples/internet-archive/cases/search-bananas.json +247 -0
  92. package/skill/krillplayer-plugin/assets/samples/internet-archive/cases/search-blank-query.json +11 -0
  93. package/skill/krillplayer-plugin/assets/samples/internet-archive/cases/search-rate-limited.json +26 -0
  94. package/skill/krillplayer-plugin/assets/samples/internet-archive/manifest.ts +14 -0
  95. package/skill/krillplayer-plugin/assets/samples/internet-archive/plugin.ts +85 -0
  96. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/channel-creator-next-page.json +99 -0
  97. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/channel-creator-off-air-has-no-live.json +28 -0
  98. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/channel-creator-on-air.json +92 -0
  99. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/channel-creator-replays-tab.json +69 -0
  100. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/channel-unknown-ended.json +12 -0
  101. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/collection-playlist-around-a-video.json +56 -0
  102. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/collection-playlist.json +78 -0
  103. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/list-live.json +33 -0
  104. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/list-videos-first-page.json +59 -0
  105. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/list-videos-next-page.json +64 -0
  106. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/parameters.json +23 -0
  107. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live.json +22 -0
  108. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-off-air-not-started.json +12 -0
  109. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-replay.json +23 -0
  110. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-upcoming-not-started.json +12 -0
  111. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-video.json +23 -0
  112. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/resolve-channel-link.json +28 -0
  113. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/resolve-live-link.json +29 -0
  114. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/resolve-other-site-unclaimed.json +8 -0
  115. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/resolve-playlist-link.json +27 -0
  116. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/resolve-removed-video-unclaimed.json +8 -0
  117. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/resolve-video-link.json +34 -0
  118. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/search-creator-live-playlist-and-videos.json +115 -0
  119. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/search-off-air-room.json +47 -0
  120. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/status-live-id-comes-from-the-plugin.json +28 -0
  121. package/skill/krillplayer-plugin/assets/samples/live-video-platform/manifest.ts +12 -0
  122. package/skill/krillplayer-plugin/assets/samples/live-video-platform/plugin.ts +123 -0
  123. package/skill/krillplayer-plugin/assets/samples/media-server/cases/collection-box-set.json +80 -0
  124. package/skill/krillplayer-plugin/assets/samples/media-server/cases/collection-empty-subfolder.json +25 -0
  125. package/skill/krillplayer-plugin/assets/samples/media-server/cases/collection-folder-has-a-subfolder-and-files.json +53 -0
  126. package/skill/krillplayer-plugin/assets/samples/media-server/cases/collection-season-around-a-removed-episode-ended.json +15 -0
  127. package/skill/krillplayer-plugin/assets/samples/media-server/cases/collection-season-around-an-episode.json +67 -0
  128. package/skill/krillplayer-plugin/assets/samples/media-server/cases/collection-season-episodes.json +89 -0
  129. package/skill/krillplayer-plugin/assets/samples/media-server/cases/collection-series-lists-seasons.json +79 -0
  130. package/skill/krillplayer-plugin/assets/samples/media-server/cases/collection-series-next-page.json +54 -0
  131. package/skill/krillplayer-plugin/assets/samples/media-server/cases/collection-unknown-ended.json +12 -0
  132. package/skill/krillplayer-plugin/assets/samples/media-server/cases/library-changes-everything-first-page.json +74 -0
  133. package/skill/krillplayer-plugin/assets/samples/media-server/cases/library-changes-everything-last-page.json +86 -0
  134. package/skill/krillplayer-plugin/assets/samples/media-server/cases/library-changes-nothing-new.json +12 -0
  135. package/skill/krillplayer-plugin/assets/samples/media-server/cases/library-changes-since-a-token-with-a-removal.json +88 -0
  136. package/skill/krillplayer-plugin/assets/samples/media-server/cases/library-ids-first-page.json +15 -0
  137. package/skill/krillplayer-plugin/assets/samples/media-server/cases/library-ids-last-page.json +14 -0
  138. package/skill/krillplayer-plugin/assets/samples/media-server/cases/list-files.json +22 -0
  139. package/skill/krillplayer-plugin/assets/samples/media-server/cases/list-movies-first-page.json +69 -0
  140. package/skill/krillplayer-plugin/assets/samples/media-server/cases/list-movies-next-page.json +30 -0
  141. package/skill/krillplayer-plugin/assets/samples/media-server/cases/list-shows.json +27 -0
  142. package/skill/krillplayer-plugin/assets/samples/media-server/cases/parameters.json +27 -0
  143. package/skill/krillplayer-plugin/assets/samples/media-server/cases/play-episode.json +23 -0
  144. package/skill/krillplayer-plugin/assets/samples/media-server/cases/play-file-without-duration.json +22 -0
  145. package/skill/krillplayer-plugin/assets/samples/media-server/cases/play-movie-version.json +23 -0
  146. package/skill/krillplayer-plugin/assets/samples/media-server/cases/play-movie.json +23 -0
  147. package/skill/krillplayer-plugin/assets/samples/media-server/cases/play-unknown-ended.json +12 -0
  148. package/skill/krillplayer-plugin/assets/samples/media-server/cases/search-across-kinds.json +81 -0
  149. package/skill/krillplayer-plugin/assets/samples/media-server/cases/search-blank.json +11 -0
  150. package/skill/krillplayer-plugin/assets/samples/media-server/manifest.ts +12 -0
  151. package/skill/krillplayer-plugin/assets/samples/media-server/plugin.ts +167 -0
  152. package/skill/krillplayer-plugin/assets/samples/music-library/cases/album-compilation-has-no-album-artist.json +63 -0
  153. package/skill/krillplayer-plugin/assets/samples/music-library/cases/album-songs-in-disc-order.json +117 -0
  154. package/skill/krillplayer-plugin/assets/samples/music-library/cases/artist-lists-albums-and-top-songs.json +103 -0
  155. package/skill/krillplayer-plugin/assets/samples/music-library/cases/artist-unknown-not-found.json +12 -0
  156. package/skill/krillplayer-plugin/assets/samples/music-library/cases/list-artists.json +32 -0
  157. package/skill/krillplayer-plugin/assets/samples/music-library/cases/list-newest-albums-first-page.json +51 -0
  158. package/skill/krillplayer-plugin/assets/samples/music-library/cases/list-past-the-end.json +13 -0
  159. package/skill/krillplayer-plugin/assets/samples/music-library/cases/list-playlists.json +22 -0
  160. package/skill/krillplayer-plugin/assets/samples/music-library/cases/list-stations-are-channels.json +28 -0
  161. package/skill/krillplayer-plugin/assets/samples/music-library/cases/list-unknown-section-not-found.json +13 -0
  162. package/skill/krillplayer-plugin/assets/samples/music-library/cases/play-song.json +24 -0
  163. package/skill/krillplayer-plugin/assets/samples/music-library/cases/play-station.json +23 -0
  164. package/skill/krillplayer-plugin/assets/samples/music-library/cases/play-unknown-not-found.json +12 -0
  165. package/skill/krillplayer-plugin/assets/samples/music-library/cases/playlist-first-page.json +67 -0
  166. package/skill/krillplayer-plugin/assets/samples/music-library/cases/playlist-next-page.json +48 -0
  167. package/skill/krillplayer-plugin/assets/samples/music-library/cases/sections-lists-every-kind.json +28 -0
  168. package/skill/krillplayer-plugin/assets/samples/music-library/manifest.ts +12 -0
  169. package/skill/krillplayer-plugin/assets/samples/music-library/plugin.ts +120 -0
  170. package/skill/krillplayer-plugin/assets/samples/network-share/cases/folder-gone-not-found.json +60 -0
  171. package/skill/krillplayer-plugin/assets/samples/network-share/cases/folder-nested-carries-ancestors.json +74 -0
  172. package/skill/krillplayer-plugin/assets/samples/network-share/cases/folder-second-page-by-cursor.json +88 -0
  173. package/skill/krillplayer-plugin/assets/samples/network-share/cases/folder-skips-subtitles-and-system-files.json +101 -0
  174. package/skill/krillplayer-plugin/assets/samples/network-share/cases/folder-without-access-asks-to-log-in.json +60 -0
  175. package/skill/krillplayer-plugin/assets/samples/network-share/cases/library-empty-folder.json +60 -0
  176. package/skill/krillplayer-plugin/assets/samples/network-share/cases/library-folder-gives-size-and-modified-time.json +78 -0
  177. package/skill/krillplayer-plugin/assets/samples/network-share/cases/library-folder-gone-not-found.json +60 -0
  178. package/skill/krillplayer-plugin/assets/samples/network-share/cases/library-folder-second-page.json +71 -0
  179. package/skill/krillplayer-plugin/assets/samples/network-share/cases/library-read-a-range.json +62 -0
  180. package/skill/krillplayer-plugin/assets/samples/network-share/cases/library-read-an-info-file.json +62 -0
  181. package/skill/krillplayer-plugin/assets/samples/network-share/cases/library-read-file-gone-not-found.json +62 -0
  182. package/skill/krillplayer-plugin/assets/samples/network-share/cases/library-root.json +84 -0
  183. package/skill/krillplayer-plugin/assets/samples/network-share/cases/library-source-offline-retryable.json +61 -0
  184. package/skill/krillplayer-plugin/assets/samples/network-share/cases/list-root-first-page.json +71 -0
  185. package/skill/krillplayer-plugin/assets/samples/network-share/cases/list-root-next-page-by-the-kernel-cursor.json +83 -0
  186. package/skill/krillplayer-plugin/assets/samples/network-share/cases/list-root-page-of-only-other-files-still-has-next.json +63 -0
  187. package/skill/krillplayer-plugin/assets/samples/network-share/cases/list-root.json +96 -0
  188. package/skill/krillplayer-plugin/assets/samples/network-share/cases/list-source-offline-retryable.json +61 -0
  189. package/skill/krillplayer-plugin/assets/samples/network-share/cases/list-wrong-account-asks-to-log-in.json +61 -0
  190. package/skill/krillplayer-plugin/assets/samples/network-share/cases/play-a-folder-not-found.json +61 -0
  191. package/skill/krillplayer-plugin/assets/samples/network-share/cases/play-audio-takes-no-subtitles.json +71 -0
  192. package/skill/krillplayer-plugin/assets/samples/network-share/cases/play-file-at-the-root.json +71 -0
  193. package/skill/krillplayer-plugin/assets/samples/network-share/cases/play-file-gone-not-found.json +60 -0
  194. package/skill/krillplayer-plugin/assets/samples/network-share/cases/play-finds-subtitles-across-pages.json +88 -0
  195. package/skill/krillplayer-plugin/assets/samples/network-share/cases/play-lists-subtitles-next-to-the-video.json +87 -0
  196. package/skill/krillplayer-plugin/assets/samples/network-share/cases/play-similar-name-takes-only-its-own-subtitles.json +80 -0
  197. package/skill/krillplayer-plugin/assets/samples/network-share/cases/play-without-access-asks-to-log-in.json +60 -0
  198. package/skill/krillplayer-plugin/assets/samples/network-share/manifest.ts +13 -0
  199. package/skill/krillplayer-plugin/assets/samples/network-share/plugin.ts +122 -0
  200. package/skill/krillplayer-plugin/assets/samples/podcast/cases/channel-around-a-removed-episode-ended.json +15 -0
  201. package/skill/krillplayer-plugin/assets/samples/podcast/cases/channel-around-an-episode.json +62 -0
  202. package/skill/krillplayer-plugin/assets/samples/podcast/cases/channel-episodic-newest-first.json +57 -0
  203. package/skill/krillplayer-plugin/assets/samples/podcast/cases/channel-episodic-next-page.json +42 -0
  204. package/skill/krillplayer-plugin/assets/samples/podcast/cases/channel-serial-oldest-first.json +57 -0
  205. package/skill/krillplayer-plugin/assets/samples/podcast/cases/channel-unknown-ended.json +12 -0
  206. package/skill/krillplayer-plugin/assets/samples/podcast/cases/list-first-page.json +32 -0
  207. package/skill/krillplayer-plugin/assets/samples/podcast/cases/list-past-the-end.json +12 -0
  208. package/skill/krillplayer-plugin/assets/samples/podcast/cases/play-episode.json +23 -0
  209. package/skill/krillplayer-plugin/assets/samples/podcast/cases/play-removed-ended.json +12 -0
  210. package/skill/krillplayer-plugin/assets/samples/podcast/cases/search-next-page.json +47 -0
  211. package/skill/krillplayer-plugin/assets/samples/podcast/cases/search-shows-and-episodes.json +41 -0
  212. package/skill/krillplayer-plugin/assets/samples/podcast/manifest.ts +12 -0
  213. package/skill/krillplayer-plugin/assets/samples/podcast/plugin.ts +77 -0
  214. package/skill/krillplayer-plugin/assets/samples/resource-finder/cases/find-episode-by-absolute-number.json +24 -0
  215. package/skill/krillplayer-plugin/assets/samples/resource-finder/cases/find-episode-by-season-and-number.json +26 -0
  216. package/skill/krillplayer-plugin/assets/samples/resource-finder/cases/find-episode-it-does-not-have.json +17 -0
  217. package/skill/krillplayer-plugin/assets/samples/resource-finder/cases/find-movie-by-imdb-only.json +39 -0
  218. package/skill/krillplayer-plugin/assets/samples/resource-finder/cases/find-movie-by-tmdb.json +40 -0
  219. package/skill/krillplayer-plugin/assets/samples/resource-finder/cases/find-movie-ids-win-over-the-title.json +16 -0
  220. package/skill/krillplayer-plugin/assets/samples/resource-finder/cases/find-movie-title-in-another-year-has-none.json +14 -0
  221. package/skill/krillplayer-plugin/assets/samples/resource-finder/cases/find-movie-without-ids-by-title-and-year.json +25 -0
  222. package/skill/krillplayer-plugin/assets/samples/resource-finder/cases/play-episode-version.json +23 -0
  223. package/skill/krillplayer-plugin/assets/samples/resource-finder/cases/play-movie-version.json +23 -0
  224. package/skill/krillplayer-plugin/assets/samples/resource-finder/cases/play-unknown-ended.json +12 -0
  225. package/skill/krillplayer-plugin/assets/samples/resource-finder/manifest.ts +12 -0
  226. package/skill/krillplayer-plugin/assets/samples/resource-finder/plugin.ts +70 -0
  227. package/skill/krillplayer-plugin/assets/samples/subtitle-tool/cases/fetch-after-login-gives-the-text.json +16 -0
  228. package/skill/krillplayer-plugin/assets/samples/subtitle-tool/cases/fetch-gives-the-text.json +12 -0
  229. package/skill/krillplayer-plugin/assets/samples/subtitle-tool/cases/fetch-login-required-without-credentials.json +12 -0
  230. package/skill/krillplayer-plugin/assets/samples/subtitle-tool/cases/fetch-unknown-track-not-found.json +12 -0
  231. package/skill/krillplayer-plugin/assets/samples/subtitle-tool/cases/login-methods-fields.json +24 -0
  232. package/skill/krillplayer-plugin/assets/samples/subtitle-tool/cases/search-by-filename-without-a-work.json +34 -0
  233. package/skill/krillplayer-plugin/assets/samples/subtitle-tool/cases/search-by-hash-gives-only-that-file.json +29 -0
  234. package/skill/krillplayer-plugin/assets/samples/subtitle-tool/cases/search-episode-by-season-and-number.json +32 -0
  235. package/skill/krillplayer-plugin/assets/samples/subtitle-tool/cases/search-episode-not-in-the-catalog-empty.json +22 -0
  236. package/skill/krillplayer-plugin/assets/samples/subtitle-tool/cases/search-hash-wins-over-a-misleading-filename.json +27 -0
  237. package/skill/krillplayer-plugin/assets/samples/subtitle-tool/cases/search-language-by-primary-subtag.json +24 -0
  238. package/skill/krillplayer-plugin/assets/samples/subtitle-tool/cases/search-movie-by-tmdb-in-language-order.json +51 -0
  239. package/skill/krillplayer-plugin/assets/samples/subtitle-tool/cases/search-nothing-to-go-on-empty.json +13 -0
  240. package/skill/krillplayer-plugin/assets/samples/subtitle-tool/cases/search-unknown-hash-falls-back-to-the-work.json +53 -0
  241. package/skill/krillplayer-plugin/assets/samples/subtitle-tool/cases/search-unknown-work-empty.json +20 -0
  242. package/skill/krillplayer-plugin/assets/samples/subtitle-tool/cases/search-unwanted-language-empty.json +21 -0
  243. package/skill/krillplayer-plugin/assets/samples/subtitle-tool/manifest.ts +12 -0
  244. package/skill/krillplayer-plugin/assets/samples/subtitle-tool/plugin.ts +104 -0
  245. package/skill/krillplayer-plugin/assets/samples/tv/cases/channel-radio-is-audio.json +33 -0
  246. package/skill/krillplayer-plugin/assets/samples/tv/cases/channel-station.json +33 -0
  247. package/skill/krillplayer-plugin/assets/samples/tv/cases/channel-unknown-ended.json +12 -0
  248. package/skill/krillplayer-plugin/assets/samples/tv/cases/list-all-stations.json +81 -0
  249. package/skill/krillplayer-plugin/assets/samples/tv/cases/list-group.json +37 -0
  250. package/skill/krillplayer-plugin/assets/samples/tv/cases/parameters.json +31 -0
  251. package/skill/krillplayer-plugin/assets/samples/tv/cases/play-catch-up-is-on-demand.json +23 -0
  252. package/skill/krillplayer-plugin/assets/samples/tv/cases/play-future-programme-has-no-catch-up.json +12 -0
  253. package/skill/krillplayer-plugin/assets/samples/tv/cases/play-live-two-lines.json +31 -0
  254. package/skill/krillplayer-plugin/assets/samples/tv/cases/play-radio.json +31 -0
  255. package/skill/krillplayer-plugin/assets/samples/tv/cases/schedule-future-window-has-no-items.json +24 -0
  256. package/skill/krillplayer-plugin/assets/samples/tv/cases/schedule-past-programmes-carry-catch-up.json +58 -0
  257. package/skill/krillplayer-plugin/assets/samples/tv/cases/schedule-several-stations-in-one-call.json +127 -0
  258. package/skill/krillplayer-plugin/assets/samples/tv/cases/schedule-window-without-programmes.json +15 -0
  259. package/skill/krillplayer-plugin/assets/samples/tv/cases/search-station.json +35 -0
  260. package/skill/krillplayer-plugin/assets/samples/tv/manifest.ts +12 -0
  261. package/skill/krillplayer-plugin/assets/samples/tv/plugin.ts +81 -0
  262. package/skill/krillplayer-plugin/references/cases.md +86 -0
  263. package/skill/krillplayer-plugin/references/choosing-a-kind.md +69 -0
  264. package/skill/krillplayer-plugin/references/contract.d.ts +1479 -0
  265. package/skill/krillplayer-plugin/references/errors-and-fixes.md +117 -0
  266. package/templates/apple-hls/cases/list-filtered.json +24 -0
  267. package/templates/apple-hls/cases/list-first-page.json +35 -0
  268. package/templates/apple-hls/cases/list-last-page.json +25 -0
  269. package/templates/apple-hls/cases/parameters.json +31 -0
  270. package/templates/apple-hls/cases/play-bipbop.json +23 -0
  271. package/templates/apple-hls/cases/play-unknown.json +12 -0
  272. package/templates/apple-hls/cases/search-blank-query.json +11 -0
  273. package/templates/apple-hls/cases/search-no-match.json +11 -0
  274. package/templates/apple-hls/cases/search-title-ignores-case.json +22 -0
  275. package/templates/apple-hls/manifest.ts +12 -0
  276. package/templates/apple-hls/plugin.ts +77 -0
@@ -0,0 +1,2374 @@
1
+ /*
2
+ * C utilities
3
+ *
4
+ * Copyright (c) 2017 Fabrice Bellard
5
+ * Copyright (c) 2018 Charlie Gordon
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in
15
+ * all copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ * THE SOFTWARE.
24
+ */
25
+ #ifndef CUTILS_H
26
+ #define CUTILS_H
27
+
28
+ #include <assert.h>
29
+ #include <stdio.h>
30
+ #include <stdarg.h>
31
+ #include <time.h>
32
+ #if !defined(_MSC_VER)
33
+ #include <sys/time.h>
34
+ #endif
35
+ #if defined(__APPLE__)
36
+ #include <mach-o/dyld.h>
37
+ #endif
38
+ #include <stdbool.h>
39
+ #include <stdlib.h>
40
+ #include <string.h>
41
+ #include <inttypes.h>
42
+ #include <math.h>
43
+
44
+ #ifdef __cplusplus
45
+ extern "C" {
46
+ #endif
47
+
48
+ #if defined(_MSC_VER)
49
+ #include <intrin.h> // _umul128, __umulh
50
+ #include <malloc.h>
51
+ #define alloca _alloca
52
+ #define ssize_t ptrdiff_t
53
+ #endif
54
+ #if defined(__APPLE__)
55
+ #include <malloc/malloc.h>
56
+ #elif defined(__linux__) || defined(__ANDROID__) || defined(__CYGWIN__) || defined(__GLIBC__) || defined(__wasi__)
57
+ #include <malloc.h>
58
+ #elif defined(__FreeBSD__)
59
+ #include <malloc_np.h>
60
+ #elif defined(_WIN32)
61
+ #include <winsock2.h>
62
+ #include <windows.h>
63
+ #include <process.h> // _beginthread
64
+ #endif
65
+ #if !defined(_WIN32) && !defined(EMSCRIPTEN) && !defined(__wasi__) && !defined(__DJGPP)
66
+ #include <errno.h>
67
+ #include <pthread.h>
68
+ #endif
69
+ #if !defined(_WIN32)
70
+ #include <fcntl.h>
71
+ #include <limits.h>
72
+ #include <unistd.h>
73
+ #endif
74
+ #if defined(__linux__)
75
+ #include <sys/syscall.h>
76
+ #endif
77
+ // msan does not see what a system call made with syscall() writes
78
+ #if defined(__has_feature)
79
+ #if __has_feature(memory_sanitizer)
80
+ #include <sanitizer/msan_interface.h>
81
+ #define js__msan_unpoison(p, n) __msan_unpoison(p, n)
82
+ #endif
83
+ #endif
84
+ #if !defined(js__msan_unpoison)
85
+ #define js__msan_unpoison(p, n)
86
+ #endif
87
+
88
+ #if defined(__sun)
89
+ #undef __maybe_unused
90
+ #endif
91
+
92
+ #if defined(_MSC_VER) && !defined(__clang__)
93
+ # define likely(x) (x)
94
+ # define unlikely(x) (x)
95
+ # define no_inline __declspec(noinline)
96
+ # define __maybe_unused
97
+ # define __attribute__(x)
98
+ # define __attribute(x)
99
+ #else
100
+ # define likely(x) __builtin_expect(!!(x), 1)
101
+ # define unlikely(x) __builtin_expect(!!(x), 0)
102
+ # define no_inline __attribute__((noinline))
103
+ # define __maybe_unused __attribute__((unused))
104
+ #endif
105
+
106
+ #ifndef offsetof
107
+ #define offsetof(type, field) ((size_t) &((type *)0)->field)
108
+ #endif
109
+ #ifndef countof
110
+ #define countof(x) (sizeof(x) / sizeof((x)[0]))
111
+ #ifndef endof
112
+ #define endof(x) ((x) + countof(x))
113
+ #endif
114
+ #endif
115
+ #ifndef container_of
116
+ /* return the pointer of type 'type *' containing 'ptr' as field 'member' */
117
+ #define container_of(ptr, type, member) ((type *)((uint8_t *)(ptr) - offsetof(type, member)))
118
+ #endif
119
+
120
+ #if defined(_MSC_VER) || defined(__cplusplus)
121
+ #define minimum_length(n) n
122
+ #else
123
+ #define minimum_length(n) static n
124
+ #endif
125
+
126
+ /* Borrowed from Folly */
127
+ #ifndef JS_PRINTF_FORMAT
128
+ /* Clang on Windows doesn't seem to support _Printf_format_string_ */
129
+ #if defined(_MSC_VER) && !defined(__clang__)
130
+ #include <sal.h>
131
+ #define JS_PRINTF_FORMAT _Printf_format_string_
132
+ #define JS_PRINTF_FORMAT_ATTR(format_param, dots_param)
133
+ #else
134
+ #define JS_PRINTF_FORMAT
135
+ #if !defined(__clang__) && defined(__GNUC__)
136
+ #define JS_PRINTF_FORMAT_ATTR(format_param, dots_param) \
137
+ __attribute__((format(gnu_printf, format_param, dots_param)))
138
+ #else
139
+ #define JS_PRINTF_FORMAT_ATTR(format_param, dots_param) \
140
+ __attribute__((format(printf, format_param, dots_param)))
141
+ #endif
142
+ #endif
143
+ #endif
144
+
145
+ #if defined(PATH_MAX)
146
+ # define JS__PATH_MAX PATH_MAX
147
+ #elif defined(_WIN32)
148
+ # define JS__PATH_MAX 32767
149
+ #else
150
+ # define JS__PATH_MAX 8192
151
+ #endif
152
+
153
+ static inline void js__pstrcpy(char *buf, int buf_size, const char *str);
154
+ static inline char *js__pstrcat(char *buf, int buf_size, const char *s);
155
+ static inline int js__strstart(const char *str, const char *val, const char **ptr);
156
+ static inline int js__has_suffix(const char *str, const char *suffix);
157
+
158
+ static inline uint8_t is_be(void) {
159
+ union {
160
+ uint16_t a;
161
+ uint8_t b;
162
+ } u = { 0x100 };
163
+ return u.b;
164
+ }
165
+
166
+ static inline int max_int(int a, int b)
167
+ {
168
+ if (a > b)
169
+ return a;
170
+ else
171
+ return b;
172
+ }
173
+
174
+ static inline int min_int(int a, int b)
175
+ {
176
+ if (a < b)
177
+ return a;
178
+ else
179
+ return b;
180
+ }
181
+
182
+ static inline uint32_t max_uint32(uint32_t a, uint32_t b)
183
+ {
184
+ if (a > b)
185
+ return a;
186
+ else
187
+ return b;
188
+ }
189
+
190
+ static inline uint32_t min_uint32(uint32_t a, uint32_t b)
191
+ {
192
+ if (a < b)
193
+ return a;
194
+ else
195
+ return b;
196
+ }
197
+
198
+ static inline int64_t max_int64(int64_t a, int64_t b)
199
+ {
200
+ if (a > b)
201
+ return a;
202
+ else
203
+ return b;
204
+ }
205
+
206
+ static inline int64_t min_int64(int64_t a, int64_t b)
207
+ {
208
+ if (a < b)
209
+ return a;
210
+ else
211
+ return b;
212
+ }
213
+
214
+ static inline uint32_t hash32(uint32_t a)
215
+ {
216
+ // use the negative of the golden ratio, it spreads out the bits nicely
217
+ // and is what the linux kernel does
218
+ //
219
+ // the golden ratio phi is defined as (1+sqrt(5))/2 or 1 + (sqrt(5)-1)/2
220
+ // (approx. 1.618033988), and negated is round(2**32/phi**2) = 0x61c88647
221
+ return a * 0x61c88647;
222
+ }
223
+
224
+ static inline uint64_t hash64(uint64_t a)
225
+ {
226
+ return a * 0x61c8864680b583ebULL;
227
+ }
228
+
229
+ static inline uintptr_t hash_uintptr(uintptr_t a)
230
+ {
231
+ if (sizeof(uintptr_t) == sizeof(uint32_t)) {
232
+ return hash32(a);
233
+ } else {
234
+ return hash64(a);
235
+ }
236
+ }
237
+
238
+ /* WARNING: undefined if a = 0 */
239
+ static inline int clz32(unsigned int a)
240
+ {
241
+ #if defined(_MSC_VER) && !defined(__clang__)
242
+ unsigned long index;
243
+ _BitScanReverse(&index, a);
244
+ return 31 - index;
245
+ #else
246
+ return __builtin_clz(a);
247
+ #endif
248
+ }
249
+
250
+ /* WARNING: undefined if a = 0 */
251
+ static inline int clz64(uint64_t a)
252
+ {
253
+ #if defined(_MSC_VER) && !defined(__clang__)
254
+ #if INTPTR_MAX == INT64_MAX
255
+ unsigned long index;
256
+ _BitScanReverse64(&index, a);
257
+ return 63 - index;
258
+ #else
259
+ if (a >> 32)
260
+ return clz32((unsigned)(a >> 32));
261
+ else
262
+ return clz32((unsigned)a) + 32;
263
+ #endif
264
+ #else
265
+ return __builtin_clzll(a);
266
+ #endif
267
+ }
268
+
269
+ static inline uintptr_t clz_uintptr(uintptr_t a)
270
+ {
271
+ if (sizeof(uintptr_t) == sizeof(uint32_t)) {
272
+ return clz32(a);
273
+ } else {
274
+ return clz64(a);
275
+ }
276
+ }
277
+
278
+ /* WARNING: undefined if a = 0 */
279
+ static inline int ctz32(unsigned int a)
280
+ {
281
+ #if defined(_MSC_VER) && !defined(__clang__)
282
+ unsigned long index;
283
+ _BitScanForward(&index, a);
284
+ return index;
285
+ #else
286
+ return __builtin_ctz(a);
287
+ #endif
288
+ }
289
+
290
+ /* WARNING: undefined if a = 0 */
291
+ static inline int ctz64(uint64_t a)
292
+ {
293
+ #if defined(_MSC_VER) && !defined(__clang__)
294
+ unsigned long index;
295
+ _BitScanForward64(&index, a);
296
+ return index;
297
+ #else
298
+ return __builtin_ctzll(a);
299
+ #endif
300
+ }
301
+
302
+ static inline uint64_t get_u64(const uint8_t *tab)
303
+ {
304
+ uint64_t v;
305
+ memcpy(&v, tab, sizeof(v));
306
+ return v;
307
+ }
308
+
309
+ static inline int64_t get_i64(const uint8_t *tab)
310
+ {
311
+ int64_t v;
312
+ memcpy(&v, tab, sizeof(v));
313
+ return v;
314
+ }
315
+
316
+ static inline void put_u64(uint8_t *tab, uint64_t val)
317
+ {
318
+ memcpy(tab, &val, sizeof(val));
319
+ }
320
+
321
+ static inline uint32_t get_u32(const uint8_t *tab)
322
+ {
323
+ uint32_t v;
324
+ memcpy(&v, tab, sizeof(v));
325
+ return v;
326
+ }
327
+
328
+ static inline int32_t get_i32(const uint8_t *tab)
329
+ {
330
+ int32_t v;
331
+ memcpy(&v, tab, sizeof(v));
332
+ return v;
333
+ }
334
+
335
+ static inline void put_u32(uint8_t *tab, uint32_t val)
336
+ {
337
+ memcpy(tab, &val, sizeof(val));
338
+ }
339
+
340
+ static inline uint32_t get_u16(const uint8_t *tab)
341
+ {
342
+ uint16_t v;
343
+ memcpy(&v, tab, sizeof(v));
344
+ return v;
345
+ }
346
+
347
+ static inline int32_t get_i16(const uint8_t *tab)
348
+ {
349
+ int16_t v;
350
+ memcpy(&v, tab, sizeof(v));
351
+ return v;
352
+ }
353
+
354
+ static inline void put_u16(uint8_t *tab, uint16_t val)
355
+ {
356
+ memcpy(tab, &val, sizeof(val));
357
+ }
358
+
359
+ static inline uint32_t get_u8(const uint8_t *tab)
360
+ {
361
+ return *tab;
362
+ }
363
+
364
+ static inline int32_t get_i8(const uint8_t *tab)
365
+ {
366
+ return (int8_t)*tab;
367
+ }
368
+
369
+ static inline void put_u8(uint8_t *tab, uint8_t val)
370
+ {
371
+ *tab = val;
372
+ }
373
+
374
+ #ifndef bswap16
375
+ static inline uint16_t bswap16(uint16_t x)
376
+ {
377
+ return (x >> 8) | (x << 8);
378
+ }
379
+ #endif
380
+
381
+ #ifndef bswap32
382
+ static inline uint32_t bswap32(uint32_t v)
383
+ {
384
+ return ((v & 0xff000000) >> 24) | ((v & 0x00ff0000) >> 8) |
385
+ ((v & 0x0000ff00) << 8) | ((v & 0x000000ff) << 24);
386
+ }
387
+ #endif
388
+
389
+ #ifndef bswap64
390
+ static inline uint64_t bswap64(uint64_t v)
391
+ {
392
+ return ((v & ((uint64_t)0xff << (7 * 8))) >> (7 * 8)) |
393
+ ((v & ((uint64_t)0xff << (6 * 8))) >> (5 * 8)) |
394
+ ((v & ((uint64_t)0xff << (5 * 8))) >> (3 * 8)) |
395
+ ((v & ((uint64_t)0xff << (4 * 8))) >> (1 * 8)) |
396
+ ((v & ((uint64_t)0xff << (3 * 8))) << (1 * 8)) |
397
+ ((v & ((uint64_t)0xff << (2 * 8))) << (3 * 8)) |
398
+ ((v & ((uint64_t)0xff << (1 * 8))) << (5 * 8)) |
399
+ ((v & ((uint64_t)0xff << (0 * 8))) << (7 * 8));
400
+ }
401
+ #endif
402
+
403
+ // A keyed hash function for hash tables that hold untrusted input: without
404
+ // the key, inputs that collide cannot be computed ahead of time. It is the
405
+ // hash that the Go runtime uses for its maps on CPUs without AES
406
+ // instructions, memHashFallback() in runtime_hash64.go and runtime_hash32.go
407
+ // of go1.27, which is derived from wyhash. The key is WYHASH_KEY_WORDS
408
+ // random numbers; 32-bit targets use the low half of the first three.
409
+ // |seed| keeps apart the hashes of inputs of different kinds that are
410
+ // hashed with the same key.
411
+ #define WYHASH_KEY_WORDS 4
412
+
413
+ #if INTPTR_MAX >= INT64_MAX
414
+
415
+ #define WYHASH_BLOCK 48
416
+ #define WYHASH_BACK 16 // wyhash_finish() reads this far before its input
417
+
418
+ typedef struct {
419
+ uint64_t seed, seed1, seed2;
420
+ } WyHashState;
421
+
422
+ // The 128-bit product of |a| and |b|, folded to 64 bits. Go leaves out
423
+ // |a| and |b|; with them, as in wyhash's "protected" mode, a zero operand
424
+ // does not make the result zero no matter what the other one is.
425
+ static inline uint64_t wyhash_mix(uint64_t a, uint64_t b)
426
+ {
427
+ uint64_t hi, lo;
428
+ #if defined(__SIZEOF_INT128__)
429
+ __uint128_t t = (__uint128_t)a * b;
430
+ hi = t >> 64;
431
+ lo = t;
432
+ #elif defined(_MSC_VER) && defined(_M_X64)
433
+ lo = _umul128(a, b, &hi);
434
+ #elif defined(_MSC_VER) && defined(_M_ARM64)
435
+ hi = __umulh(a, b);
436
+ lo = a * b;
437
+ #else
438
+ uint64_t ll = (uint64_t)(uint32_t)a * (uint32_t)b;
439
+ uint64_t lh = (uint64_t)(uint32_t)a * (b >> 32);
440
+ uint64_t hl = (a >> 32) * (uint32_t)b;
441
+ uint64_t hh = (a >> 32) * (b >> 32);
442
+ uint64_t mid = (ll >> 32) + (uint32_t)lh + (uint32_t)hl;
443
+ hi = hh + (lh >> 32) + (hl >> 32) + (mid >> 32);
444
+ lo = (uint32_t)ll | (mid << 32);
445
+ #endif
446
+ return a ^ b ^ hi ^ lo;
447
+ }
448
+
449
+ static inline void wyhash_start(WyHashState *s, const uint64_t *key,
450
+ uint32_t seed, size_t len)
451
+ {
452
+ s->seed = seed ^ key[0];
453
+ s->seed1 = s->seed;
454
+ s->seed2 = s->seed;
455
+ }
456
+
457
+ // |p| is the next WYHASH_BLOCK bytes of the input, with more to follow
458
+ static inline void wyhash_block(WyHashState *s, const uint64_t *key,
459
+ const uint8_t *p)
460
+ {
461
+ s->seed = wyhash_mix(get_u64(p) ^ key[1], get_u64(p + 8) ^ s->seed);
462
+ s->seed1 = wyhash_mix(get_u64(p + 16) ^ key[2],
463
+ get_u64(p + 24) ^ s->seed1);
464
+ s->seed2 = wyhash_mix(get_u64(p + 32) ^ key[3],
465
+ get_u64(p + 40) ^ s->seed2);
466
+ }
467
+
468
+ // |p| is the last |l| bytes of an input of |len| bytes: all of it if it is
469
+ // no longer than WYHASH_BLOCK, else what the blocks left over, at least one
470
+ // byte. In that case the WYHASH_BACK bytes before |p| are read again.
471
+ static inline uint32_t wyhash_finish(WyHashState *s, const uint64_t *key,
472
+ const uint8_t *p, size_t l, size_t len)
473
+ {
474
+ uint64_t a, b, seed;
475
+
476
+ seed = s->seed;
477
+ if (len == 0) {
478
+ return seed;
479
+ } else if (len < 4) {
480
+ a = p[0] | ((uint64_t)p[len >> 1] << 8) | ((uint64_t)p[len - 1] << 16);
481
+ b = 0;
482
+ } else if (len < 8) {
483
+ a = get_u32(p);
484
+ b = get_u32(p + len - 4);
485
+ } else if (len <= 16) {
486
+ a = get_u64(p);
487
+ b = get_u64(p + len - 8);
488
+ } else {
489
+ if (len > WYHASH_BLOCK)
490
+ seed ^= s->seed1 ^ s->seed2;
491
+ for (; l > 16; l -= 16) {
492
+ seed = wyhash_mix(get_u64(p) ^ key[1], get_u64(p + 8) ^ seed);
493
+ p += 16;
494
+ }
495
+ a = get_u64(p + l - 16);
496
+ b = get_u64(p + l - 8);
497
+ }
498
+ return wyhash_mix(0x1D8E4E27C47D124F ^ len,
499
+ wyhash_mix(a ^ key[1], b ^ seed));
500
+ }
501
+
502
+ // same as wyhash() of the eight bytes of |x|
503
+ static inline uint32_t wyhash_u64(const uint64_t *key, uint32_t seed,
504
+ uint64_t x)
505
+ {
506
+ return wyhash_mix(0x1D8E4E27C47D124F ^ 8,
507
+ wyhash_mix(x ^ key[1], x ^ seed ^ key[0]));
508
+ }
509
+
510
+ #else // 32-bit targets
511
+
512
+ #define WYHASH_BLOCK 8
513
+ #define WYHASH_BACK 0
514
+
515
+ typedef struct {
516
+ uint32_t a, b;
517
+ } WyHashState;
518
+
519
+ static inline void wyhash_mix(WyHashState *s, const uint64_t *key)
520
+ {
521
+ uint64_t c;
522
+
523
+ c = (uint64_t)(s->a ^ (uint32_t)key[1]) * (s->b ^ (uint32_t)key[2]);
524
+ s->a = c;
525
+ s->b = c >> 32;
526
+ }
527
+
528
+ static inline void wyhash_start(WyHashState *s, const uint64_t *key,
529
+ uint32_t seed, size_t len)
530
+ {
531
+ s->a = seed;
532
+ s->b = (uint32_t)len ^ (uint32_t)key[0];
533
+ wyhash_mix(s, key);
534
+ }
535
+
536
+ // |p| is the next WYHASH_BLOCK bytes of the input, with more to follow
537
+ static inline void wyhash_block(WyHashState *s, const uint64_t *key,
538
+ const uint8_t *p)
539
+ {
540
+ s->a ^= get_u32(p);
541
+ s->b ^= get_u32(p + 4);
542
+ wyhash_mix(s, key);
543
+ }
544
+
545
+ // |p| is the last |l| bytes of an input of |len| bytes: all of it if it is
546
+ // no longer than WYHASH_BLOCK, else what the blocks left over, at least one
547
+ // byte
548
+ static inline uint32_t wyhash_finish(WyHashState *s, const uint64_t *key,
549
+ const uint8_t *p, size_t l, size_t len)
550
+ {
551
+ if (l >= 4) {
552
+ s->a ^= get_u32(p);
553
+ s->b ^= get_u32(p + l - 4);
554
+ } else if (l > 0) {
555
+ s->b ^= p[0] | ((uint32_t)p[l >> 1] << 8) | ((uint32_t)p[l - 1] << 16);
556
+ } else {
557
+ return s->a ^ s->b;
558
+ }
559
+ wyhash_mix(s, key);
560
+ wyhash_mix(s, key);
561
+ return s->a ^ s->b;
562
+ }
563
+
564
+ // same as wyhash() of the eight bytes of |x| when the low half comes first
565
+ static inline uint32_t wyhash_u64(const uint64_t *key, uint32_t seed,
566
+ uint64_t x)
567
+ {
568
+ WyHashState s;
569
+
570
+ wyhash_start(&s, key, seed, 8);
571
+ s.a ^= (uint32_t)x;
572
+ s.b ^= (uint32_t)(x >> 32);
573
+ wyhash_mix(&s, key);
574
+ wyhash_mix(&s, key);
575
+ return s.a ^ s.b;
576
+ }
577
+
578
+ #endif
579
+
580
+ static inline uint32_t wyhash(const uint64_t *key, uint32_t seed,
581
+ const void *buf, size_t len)
582
+ {
583
+ const uint8_t *p = buf;
584
+ WyHashState s;
585
+ size_t l;
586
+
587
+ wyhash_start(&s, key, seed, len);
588
+ for (l = len; l > WYHASH_BLOCK; l -= WYHASH_BLOCK) {
589
+ wyhash_block(&s, key, p);
590
+ p += WYHASH_BLOCK;
591
+ }
592
+ return wyhash_finish(&s, key, p, l, len);
593
+ }
594
+
595
+ // To hash an input that is not in one piece. The result is that of
596
+ // wyhash() of the whole input.
597
+ typedef struct {
598
+ WyHashState s;
599
+ const uint64_t *key;
600
+ size_t len; // of the whole input
601
+ size_t n; // bytes at the end of |buf| that are not hashed yet
602
+ uint8_t buf[WYHASH_BACK + WYHASH_BLOCK];
603
+ } WyHash;
604
+
605
+ // |len| is the number of bytes that wyhash_update() is going to get
606
+ static inline void wyhash_init(WyHash *h, const uint64_t *key, uint32_t seed,
607
+ size_t len)
608
+ {
609
+ wyhash_start(&h->s, key, seed, len);
610
+ h->key = key;
611
+ h->len = len;
612
+ h->n = 0;
613
+ }
614
+
615
+ static inline void wyhash_update(WyHash *h, const void *buf, size_t len)
616
+ {
617
+ const uint8_t *p = buf;
618
+ size_t n;
619
+
620
+ while (len > 0) {
621
+ if (h->n == WYHASH_BLOCK) {
622
+ wyhash_block(&h->s, h->key, h->buf + WYHASH_BACK);
623
+ // keep the bytes that wyhash_finish() reads again
624
+ memcpy(h->buf, h->buf + WYHASH_BLOCK, WYHASH_BACK);
625
+ h->n = 0;
626
+ }
627
+ n = WYHASH_BLOCK - h->n;
628
+ if (n > len)
629
+ n = len;
630
+ memcpy(h->buf + WYHASH_BACK + h->n, p, n);
631
+ h->n += n;
632
+ p += n;
633
+ len -= n;
634
+ }
635
+ }
636
+
637
+ static inline uint32_t wyhash_fini(WyHash *h)
638
+ {
639
+ return wyhash_finish(&h->s, h->key, h->buf + WYHASH_BACK, h->n, h->len);
640
+ }
641
+
642
+ static inline double fromfp16(uint16_t v) {
643
+ double d, s;
644
+ int e;
645
+ if ((v & 0x7C00) == 0x7C00) {
646
+ d = (v & 0x3FF) ? NAN : INFINITY;
647
+ } else {
648
+ d = (v & 0x3FF) / 1024.;
649
+ e = (v & 0x7C00) >> 10;
650
+ if (e == 0) {
651
+ e = -14;
652
+ } else {
653
+ d += 1;
654
+ e -= 15;
655
+ }
656
+ d = scalbn(d, e);
657
+ }
658
+ s = (v & 0x8000) ? -1.0 : 1.0;
659
+ return d * s;
660
+ }
661
+
662
+ static inline uint16_t tofp16(double d) {
663
+ uint16_t f, s;
664
+ double t;
665
+ int e;
666
+ s = 0;
667
+ if (copysign(1, d) < 0) { // preserve sign when |d| is negative zero
668
+ d = -d;
669
+ s = 0x8000;
670
+ }
671
+ if (isinf(d))
672
+ return s | 0x7C00;
673
+ if (isnan(d))
674
+ return s | 0x7C01;
675
+ if (d == 0)
676
+ return s | 0;
677
+ d = 2 * frexp(d, &e);
678
+ e--;
679
+ if (e > 15)
680
+ return s | 0x7C00; // out of range, return +/-infinity
681
+ if (e < -25) {
682
+ d = 0;
683
+ e = 0;
684
+ } else if (e < -14) {
685
+ d = scalbn(d, e + 14);
686
+ e = 0;
687
+ } else {
688
+ d -= 1;
689
+ e += 15;
690
+ }
691
+ d *= 1024.;
692
+ f = (uint16_t)d;
693
+ t = d - f;
694
+ if (t < 0.5)
695
+ goto done;
696
+ if (t == 0.5)
697
+ if ((f & 1) == 0)
698
+ goto done;
699
+ // adjust for rounding
700
+ if (++f == 1024) {
701
+ f = 0;
702
+ if (++e == 31)
703
+ return s | 0x7C00; // out of range, return +/-infinity
704
+ }
705
+ done:
706
+ return s | (e << 10) | f;
707
+ }
708
+
709
+ static inline int isfp16nan(uint16_t v) {
710
+ return (v & 0x7FFF) > 0x7C00;
711
+ }
712
+
713
+ static inline int isfp16zero(uint16_t v) {
714
+ return (v & 0x7FFF) == 0;
715
+ }
716
+
717
+ /* XXX: should take an extra argument to pass slack information to the caller */
718
+ typedef void *DynBufReallocFunc(void *opaque, void *ptr, size_t size);
719
+
720
+ typedef struct DynBuf {
721
+ uint8_t *buf;
722
+ size_t size;
723
+ size_t allocated_size;
724
+ bool error; /* true if a memory allocation error occurred */
725
+ DynBufReallocFunc *realloc_func;
726
+ void *opaque; /* for realloc_func */
727
+ } DynBuf;
728
+
729
+ static inline void dbuf_init(DynBuf *s);
730
+ static inline void dbuf_init2(DynBuf *s, void *opaque, DynBufReallocFunc *realloc_func);
731
+ static inline int dbuf_claim(DynBuf *s, size_t len);
732
+ static inline int dbuf_put(DynBuf *s, const void *data, size_t len);
733
+ static inline int dbuf_put_self(DynBuf *s, size_t offset, size_t len);
734
+ static inline int __dbuf_putc(DynBuf *s, uint8_t c);
735
+ static inline int __dbuf_put_u16(DynBuf *s, uint16_t val);
736
+ static inline int __dbuf_put_u32(DynBuf *s, uint32_t val);
737
+ static inline int __dbuf_put_u64(DynBuf *s, uint64_t val);
738
+ static inline int dbuf_putstr(DynBuf *s, const char *str);
739
+ static inline int dbuf_putc(DynBuf *s, uint8_t val)
740
+ {
741
+ if (unlikely((s->allocated_size - s->size) < 1))
742
+ return __dbuf_putc(s, val);
743
+ s->buf[s->size++] = val;
744
+ return 0;
745
+ }
746
+ static inline int dbuf_put_u16(DynBuf *s, uint16_t val)
747
+ {
748
+ if (unlikely((s->allocated_size - s->size) < 2))
749
+ return __dbuf_put_u16(s, val);
750
+ put_u16(s->buf + s->size, val);
751
+ s->size += 2;
752
+ return 0;
753
+ }
754
+ static inline int dbuf_put_u32(DynBuf *s, uint32_t val)
755
+ {
756
+ if (unlikely((s->allocated_size - s->size) < 4))
757
+ return __dbuf_put_u32(s, val);
758
+ put_u32(s->buf + s->size, val);
759
+ s->size += 4;
760
+ return 0;
761
+ }
762
+ static inline int dbuf_put_u64(DynBuf *s, uint64_t val)
763
+ {
764
+ if (unlikely((s->allocated_size - s->size) < 8))
765
+ return __dbuf_put_u64(s, val);
766
+ put_u64(s->buf + s->size, val);
767
+ s->size += 8;
768
+ return 0;
769
+ }
770
+ static inline int JS_PRINTF_FORMAT_ATTR(2, 3) dbuf_printf(DynBuf *s, JS_PRINTF_FORMAT const char *fmt, ...);
771
+ static inline void dbuf_free(DynBuf *s);
772
+ static inline bool dbuf_error(DynBuf *s) {
773
+ return s->error;
774
+ }
775
+ static inline void dbuf_set_error(DynBuf *s)
776
+ {
777
+ s->error = true;
778
+ }
779
+
780
+ /*---- UTF-8 and UTF-16 handling ----*/
781
+
782
+ #define UTF8_CHAR_LEN_MAX 4
783
+
784
+ enum {
785
+ UTF8_PLAIN_ASCII = 0, // 7-bit ASCII plain text
786
+ UTF8_NON_ASCII = 1, // has non ASCII code points (8-bit or more)
787
+ UTF8_HAS_16BIT = 2, // has 16-bit code points
788
+ UTF8_HAS_NON_BMP1 = 4, // has non-BMP1 code points, needs UTF-16 surrogate pairs
789
+ UTF8_HAS_ERRORS = 8, // has encoding errors
790
+ };
791
+ static inline int utf8_scan(const char *buf, size_t len, size_t *plen);
792
+ static inline size_t utf8_encode_len(uint32_t c);
793
+ static inline size_t utf8_encode(uint8_t buf[minimum_length(UTF8_CHAR_LEN_MAX)], uint32_t c);
794
+ static inline uint32_t utf8_decode_len(const uint8_t *p, size_t max_len, const uint8_t **pp);
795
+ static inline uint32_t utf8_decode(const uint8_t *p, const uint8_t **pp);
796
+ static inline size_t utf8_decode_buf8(uint8_t *dest, size_t dest_len, const char *src, size_t src_len);
797
+ static inline size_t utf8_decode_buf16(uint16_t *dest, size_t dest_len, const char *src, size_t src_len);
798
+ static inline size_t utf8_encode_buf8(char *dest, size_t dest_len, const uint8_t *src, size_t src_len);
799
+ static inline size_t utf8_encode_buf16(char *dest, size_t dest_len, const uint16_t *src, size_t src_len);
800
+
801
+ static inline bool is_surrogate(uint32_t c)
802
+ {
803
+ return (c >> 11) == (0xD800 >> 11); // 0xD800-0xDFFF
804
+ }
805
+
806
+ static inline bool is_hi_surrogate(uint32_t c)
807
+ {
808
+ return (c >> 10) == (0xD800 >> 10); // 0xD800-0xDBFF
809
+ }
810
+
811
+ static inline bool is_lo_surrogate(uint32_t c)
812
+ {
813
+ return (c >> 10) == (0xDC00 >> 10); // 0xDC00-0xDFFF
814
+ }
815
+
816
+ static inline uint32_t get_hi_surrogate(uint32_t c)
817
+ {
818
+ return (c >> 10) - (0x10000 >> 10) + 0xD800;
819
+ }
820
+
821
+ static inline uint32_t get_lo_surrogate(uint32_t c)
822
+ {
823
+ return (c & 0x3FF) | 0xDC00;
824
+ }
825
+
826
+ static inline uint32_t from_surrogate(uint32_t hi, uint32_t lo)
827
+ {
828
+ return 65536 + 1024 * (hi & 1023) + (lo & 1023);
829
+ }
830
+
831
+ static inline int from_hex(int c)
832
+ {
833
+ if (c >= '0' && c <= '9')
834
+ return c - '0';
835
+ else if (c >= 'A' && c <= 'F')
836
+ return c - 'A' + 10;
837
+ else if (c >= 'a' && c <= 'f')
838
+ return c - 'a' + 10;
839
+ else
840
+ return -1;
841
+ }
842
+
843
+ static inline uint8_t is_upper_ascii(uint8_t c) {
844
+ return c >= 'A' && c <= 'Z';
845
+ }
846
+
847
+ static inline uint8_t to_upper_ascii(uint8_t c) {
848
+ return c >= 'a' && c <= 'z' ? c - 'a' + 'A' : c;
849
+ }
850
+
851
+ static inline void rqsort(void *base, size_t nmemb, size_t size,
852
+ int (*cmp)(const void *, const void *, void *),
853
+ void *arg);
854
+
855
+ static inline uint64_t float64_as_uint64(double d)
856
+ {
857
+ union {
858
+ double d;
859
+ uint64_t u64;
860
+ } u;
861
+ u.d = d;
862
+ return u.u64;
863
+ }
864
+
865
+ static inline double uint64_as_float64(uint64_t u64)
866
+ {
867
+ union {
868
+ double d;
869
+ uint64_t u64;
870
+ } u;
871
+ u.u64 = u64;
872
+ return u.d;
873
+ }
874
+
875
+ static inline int64_t js__gettimeofday_us(void);
876
+ static inline uint64_t js__hrtime_ns(void);
877
+
878
+ static inline size_t js__malloc_usable_size(const void *ptr)
879
+ {
880
+ #if defined(__APPLE__)
881
+ return malloc_size(ptr);
882
+ #elif defined(_WIN32)
883
+ return _msize((void *)ptr);
884
+ #elif defined(__linux__) || defined(__ANDROID__) || defined(__CYGWIN__) || defined(__FreeBSD__) || defined(__GLIBC__) || defined(__wasi__)
885
+ return malloc_usable_size((void *)ptr);
886
+ #else
887
+ return 0;
888
+ #endif
889
+ }
890
+
891
+ static inline int js_exepath(char* buffer, size_t* size);
892
+
893
+ /* Cross-platform threading APIs. */
894
+
895
+ #if defined(EMSCRIPTEN) || defined(__wasi__) || defined(__DJGPP)
896
+
897
+ #define JS_HAVE_THREADS 0
898
+
899
+ #else
900
+
901
+ #define JS_HAVE_THREADS 1
902
+
903
+ #if defined(_WIN32)
904
+ #define JS_ONCE_INIT INIT_ONCE_STATIC_INIT
905
+ typedef INIT_ONCE js_once_t;
906
+ typedef CRITICAL_SECTION js_mutex_t;
907
+ typedef CONDITION_VARIABLE js_cond_t;
908
+ typedef HANDLE js_thread_t;
909
+ #else
910
+ #define JS_ONCE_INIT PTHREAD_ONCE_INIT
911
+ typedef pthread_once_t js_once_t;
912
+ typedef pthread_mutex_t js_mutex_t;
913
+ typedef pthread_cond_t js_cond_t;
914
+ typedef pthread_t js_thread_t;
915
+ #endif
916
+
917
+ static inline void js_once(js_once_t *guard, void (*callback)(void));
918
+
919
+ static inline void js_mutex_init(js_mutex_t *mutex);
920
+ static inline void js_mutex_destroy(js_mutex_t *mutex);
921
+ static inline void js_mutex_lock(js_mutex_t *mutex);
922
+ static inline void js_mutex_unlock(js_mutex_t *mutex);
923
+
924
+ static inline void js_cond_init(js_cond_t *cond);
925
+ static inline void js_cond_destroy(js_cond_t *cond);
926
+ static inline void js_cond_signal(js_cond_t *cond);
927
+ static inline void js_cond_broadcast(js_cond_t *cond);
928
+ static inline void js_cond_wait(js_cond_t *cond, js_mutex_t *mutex);
929
+ static inline int js_cond_timedwait(js_cond_t *cond, js_mutex_t *mutex, uint64_t timeout);
930
+
931
+ enum {
932
+ JS_THREAD_CREATE_DETACHED = 1,
933
+ };
934
+
935
+ // creates threads with 2 MB stacks (glibc default)
936
+ static inline int js_thread_create(js_thread_t *thrd, void (*start)(void *), void *arg,
937
+ int flags);
938
+ static inline int js_thread_join(js_thread_t thrd);
939
+
940
+ #endif /* !defined(EMSCRIPTEN) && !defined(__wasi__) */
941
+
942
+ // JS requires strict rounding behavior. Turn on 64-bits double precision
943
+ // and disable x87 80-bits extended precision for intermediate floating-point
944
+ // results. 0x300 is extended precision, 0x200 is double precision.
945
+ // Note that `*&cw` in the asm constraints looks redundant but isn't.
946
+ #if defined(__i386__) && !defined(_MSC_VER)
947
+ #define JS_X87_FPCW_SAVE_AND_ADJUST(cw) \
948
+ (void)0; \
949
+ unsigned short cw; \
950
+ __asm__ __volatile__("fnstcw %0" : "=m"(*&cw)); \
951
+ do { \
952
+ unsigned short t = 0x200 | (cw & ~0x300); \
953
+ __asm__ __volatile__("fldcw %0" : /*empty*/ : "m"(*&t)); \
954
+ } while (0)
955
+ #define JS_X87_FPCW_RESTORE(cw) \
956
+ __asm__ __volatile__("fldcw %0" : /*empty*/ : "m"(*&cw))
957
+ #else
958
+ #define JS_X87_FPCW_SAVE_AND_ADJUST(cw)
959
+ #define JS_X87_FPCW_RESTORE(cw)
960
+ #endif
961
+
962
+ #undef NANOSEC
963
+ #define NANOSEC ((uint64_t) 1e9)
964
+
965
+ static inline void js__pstrcpy(char *buf, int buf_size, const char *str)
966
+ {
967
+ int c;
968
+ char *q = buf;
969
+
970
+ if (buf_size <= 0)
971
+ return;
972
+
973
+ for(;;) {
974
+ c = *str++;
975
+ if (c == 0 || q >= buf + buf_size - 1)
976
+ break;
977
+ *q++ = c;
978
+ }
979
+ *q = '\0';
980
+ }
981
+
982
+ /* strcat and truncate. */
983
+ static inline char *js__pstrcat(char *buf, int buf_size, const char *s)
984
+ {
985
+ int len;
986
+ len = strlen(buf);
987
+ if (len < buf_size)
988
+ js__pstrcpy(buf + len, buf_size - len, s);
989
+ return buf;
990
+ }
991
+
992
+ static inline int js__strstart(const char *str, const char *val, const char **ptr)
993
+ {
994
+ const char *p, *q;
995
+ p = str;
996
+ q = val;
997
+ while (*q != '\0') {
998
+ if (*p != *q)
999
+ return 0;
1000
+ p++;
1001
+ q++;
1002
+ }
1003
+ if (ptr)
1004
+ *ptr = p;
1005
+ return 1;
1006
+ }
1007
+
1008
+ static inline int js__has_suffix(const char *str, const char *suffix)
1009
+ {
1010
+ size_t len = strlen(str);
1011
+ size_t slen = strlen(suffix);
1012
+ return (len >= slen && !memcmp(str + len - slen, suffix, slen));
1013
+ }
1014
+
1015
+ /* Dynamic buffer package */
1016
+
1017
+ static void *dbuf_default_realloc(void *opaque, void *ptr, size_t size)
1018
+ {
1019
+ if (unlikely(size == 0)) {
1020
+ free(ptr);
1021
+ return NULL;
1022
+ }
1023
+ return realloc(ptr, size);
1024
+ }
1025
+
1026
+ static inline void dbuf_init2(DynBuf *s, void *opaque, DynBufReallocFunc *realloc_func)
1027
+ {
1028
+ memset(s, 0, sizeof(*s));
1029
+ if (!realloc_func)
1030
+ realloc_func = dbuf_default_realloc;
1031
+ s->opaque = opaque;
1032
+ s->realloc_func = realloc_func;
1033
+ }
1034
+
1035
+ static inline void dbuf_init(DynBuf *s)
1036
+ {
1037
+ dbuf_init2(s, NULL, NULL);
1038
+ }
1039
+
1040
+ /* Try to allocate 'len' more bytes. return < 0 if error */
1041
+ static inline int dbuf_claim(DynBuf *s, size_t len)
1042
+ {
1043
+ size_t new_size, size, new_allocated_size;
1044
+ uint8_t *new_buf;
1045
+ new_size = s->size + len;
1046
+ if (new_size < len)
1047
+ return -1; /* overflow */
1048
+ if (new_size > s->allocated_size) {
1049
+ if (s->error)
1050
+ return -1;
1051
+ size = s->allocated_size + (s->allocated_size / 2);
1052
+ if (size < new_size || size < s->allocated_size) /* overflow test */
1053
+ new_allocated_size = new_size;
1054
+ else
1055
+ new_allocated_size = size;
1056
+ new_buf = s->realloc_func(s->opaque, s->buf, new_allocated_size);
1057
+ if (!new_buf) {
1058
+ s->error = true;
1059
+ return -1;
1060
+ }
1061
+ s->buf = new_buf;
1062
+ s->allocated_size = new_allocated_size;
1063
+ }
1064
+ return 0;
1065
+ }
1066
+
1067
+ static inline int dbuf_put(DynBuf *s, const void *data, size_t len)
1068
+ {
1069
+ if (unlikely((s->size + len) > s->allocated_size)) {
1070
+ if (dbuf_claim(s, len))
1071
+ return -1;
1072
+ }
1073
+ if (len > 0) {
1074
+ memcpy(s->buf + s->size, data, len);
1075
+ s->size += len;
1076
+ }
1077
+ return 0;
1078
+ }
1079
+
1080
+ static inline int dbuf_put_self(DynBuf *s, size_t offset, size_t len)
1081
+ {
1082
+ if (unlikely((s->size + len) > s->allocated_size)) {
1083
+ if (dbuf_claim(s, len))
1084
+ return -1;
1085
+ }
1086
+ if (len > 0) {
1087
+ memcpy(s->buf + s->size, s->buf + offset, len);
1088
+ s->size += len;
1089
+ }
1090
+ return 0;
1091
+ }
1092
+
1093
+ static inline int __dbuf_putc(DynBuf *s, uint8_t c)
1094
+ {
1095
+ return dbuf_put(s, &c, 1);
1096
+ }
1097
+
1098
+ static inline int __dbuf_put_u16(DynBuf *s, uint16_t val)
1099
+ {
1100
+ return dbuf_put(s, (uint8_t *)&val, 2);
1101
+ }
1102
+
1103
+ static inline int __dbuf_put_u32(DynBuf *s, uint32_t val)
1104
+ {
1105
+ return dbuf_put(s, (uint8_t *)&val, 4);
1106
+ }
1107
+
1108
+ static inline int __dbuf_put_u64(DynBuf *s, uint64_t val)
1109
+ {
1110
+ return dbuf_put(s, (uint8_t *)&val, 8);
1111
+ }
1112
+
1113
+ static inline int dbuf_putstr(DynBuf *s, const char *str)
1114
+ {
1115
+ return dbuf_put(s, (const uint8_t *)str, strlen(str));
1116
+ }
1117
+
1118
+ static inline int JS_PRINTF_FORMAT_ATTR(2, 3) dbuf_printf(DynBuf *s, JS_PRINTF_FORMAT const char *fmt, ...)
1119
+ {
1120
+ va_list ap;
1121
+ char buf[128];
1122
+ int len;
1123
+
1124
+ va_start(ap, fmt);
1125
+ len = vsnprintf(buf, sizeof(buf), fmt, ap);
1126
+ va_end(ap);
1127
+ if (len < (int)sizeof(buf)) {
1128
+ /* fast case */
1129
+ return dbuf_put(s, (uint8_t *)buf, len);
1130
+ } else {
1131
+ if (dbuf_claim(s, len + 1))
1132
+ return -1;
1133
+ va_start(ap, fmt);
1134
+ vsnprintf((char *)(s->buf + s->size), s->allocated_size - s->size,
1135
+ fmt, ap);
1136
+ va_end(ap);
1137
+ s->size += len;
1138
+ }
1139
+ return 0;
1140
+ }
1141
+
1142
+ static inline void dbuf_free(DynBuf *s)
1143
+ {
1144
+ /* we test s->buf as a fail safe to avoid crashing if dbuf_free()
1145
+ is called twice */
1146
+ if (s->buf) {
1147
+ s->realloc_func(s->opaque, s->buf, 0);
1148
+ }
1149
+ memset(s, 0, sizeof(*s));
1150
+ }
1151
+
1152
+ /*--- UTF-8 utility functions --*/
1153
+
1154
+ /* Note: only encode valid codepoints (0x0000..0x10FFFF).
1155
+ At most UTF8_CHAR_LEN_MAX bytes are output. */
1156
+
1157
+ /* Compute the number of bytes of the UTF-8 encoding for a codepoint
1158
+ `c` is a code-point.
1159
+ Returns the number of bytes. If a codepoint is beyond 0x10FFFF the
1160
+ return value is 3 as the codepoint would be encoded as 0xFFFD.
1161
+ */
1162
+ static inline size_t utf8_encode_len(uint32_t c)
1163
+ {
1164
+ if (c < 0x80)
1165
+ return 1;
1166
+ if (c < 0x800)
1167
+ return 2;
1168
+ if (c < 0x10000)
1169
+ return 3;
1170
+ if (c < 0x110000)
1171
+ return 4;
1172
+ return 3;
1173
+ }
1174
+
1175
+ /* Encode a codepoint in UTF-8
1176
+ `buf` points to an array of at least `UTF8_CHAR_LEN_MAX` bytes
1177
+ `c` is a code-point.
1178
+ Returns the number of bytes. If a codepoint is beyond 0x10FFFF the
1179
+ return value is 3 and the codepoint is encoded as 0xFFFD.
1180
+ No null byte is stored after the encoded bytes.
1181
+ Return value is in range 1..4
1182
+ */
1183
+ static inline size_t utf8_encode(uint8_t buf[minimum_length(UTF8_CHAR_LEN_MAX)], uint32_t c)
1184
+ {
1185
+ if (c < 0x80) {
1186
+ buf[0] = c;
1187
+ return 1;
1188
+ }
1189
+ if (c < 0x800) {
1190
+ buf[0] = (c >> 6) | 0xC0;
1191
+ buf[1] = (c & 0x3F) | 0x80;
1192
+ return 2;
1193
+ }
1194
+ if (c < 0x10000) {
1195
+ buf[0] = (c >> 12) | 0xE0;
1196
+ buf[1] = ((c >> 6) & 0x3F) | 0x80;
1197
+ buf[2] = (c & 0x3F) | 0x80;
1198
+ return 3;
1199
+ }
1200
+ if (c < 0x110000) {
1201
+ buf[0] = (c >> 18) | 0xF0;
1202
+ buf[1] = ((c >> 12) & 0x3F) | 0x80;
1203
+ buf[2] = ((c >> 6) & 0x3F) | 0x80;
1204
+ buf[3] = (c & 0x3F) | 0x80;
1205
+ return 4;
1206
+ }
1207
+ buf[0] = (0xFFFD >> 12) | 0xE0;
1208
+ buf[1] = ((0xFFFD >> 6) & 0x3F) | 0x80;
1209
+ buf[2] = (0xFFFD & 0x3F) | 0x80;
1210
+ return 3;
1211
+ }
1212
+
1213
+ /* Decode a single code point from a UTF-8 encoded array of bytes
1214
+ `p` is a valid pointer to an array of bytes
1215
+ `pp` is a valid pointer to a `const uint8_t *` to store a pointer
1216
+ to the byte following the current sequence.
1217
+ Return the code point at `p`, in the range `0..0x10FFFF`
1218
+ Return 0xFFFD on error. Only a single byte is consumed in this case
1219
+ The maximum length for a UTF-8 byte sequence is 4 bytes.
1220
+ This implements the algorithm specified in whatwg.org, except it accepts
1221
+ UTF-8 encoded surrogates as JavaScript allows them in strings.
1222
+ The source string is assumed to have at least UTF8_CHAR_LEN_MAX bytes
1223
+ or be null terminated.
1224
+ If `p[0]` is '\0', the return value is `0` and the byte is consumed.
1225
+ cf: https://encoding.spec.whatwg.org/#utf-8-encoder
1226
+ */
1227
+ static inline uint32_t utf8_decode(const uint8_t *p, const uint8_t **pp)
1228
+ {
1229
+ uint32_t c;
1230
+ uint8_t lower, upper;
1231
+
1232
+ c = *p++;
1233
+ if (c < 0x80) {
1234
+ *pp = p;
1235
+ return c;
1236
+ }
1237
+ switch(c) {
1238
+ case 0xC2: case 0xC3:
1239
+ case 0xC4: case 0xC5: case 0xC6: case 0xC7:
1240
+ case 0xC8: case 0xC9: case 0xCA: case 0xCB:
1241
+ case 0xCC: case 0xCD: case 0xCE: case 0xCF:
1242
+ case 0xD0: case 0xD1: case 0xD2: case 0xD3:
1243
+ case 0xD4: case 0xD5: case 0xD6: case 0xD7:
1244
+ case 0xD8: case 0xD9: case 0xDA: case 0xDB:
1245
+ case 0xDC: case 0xDD: case 0xDE: case 0xDF:
1246
+ if (*p >= 0x80 && *p <= 0xBF) {
1247
+ *pp = p + 1;
1248
+ return ((c - 0xC0) << 6) + (*p - 0x80);
1249
+ }
1250
+ // otherwise encoding error
1251
+ break;
1252
+ case 0xE0:
1253
+ lower = 0xA0; /* reject invalid encoding */
1254
+ goto need2;
1255
+ case 0xE1: case 0xE2: case 0xE3:
1256
+ case 0xE4: case 0xE5: case 0xE6: case 0xE7:
1257
+ case 0xE8: case 0xE9: case 0xEA: case 0xEB:
1258
+ case 0xEC: case 0xED: case 0xEE: case 0xEF:
1259
+ lower = 0x80;
1260
+ need2:
1261
+ if (*p >= lower && *p <= 0xBF && p[1] >= 0x80 && p[1] <= 0xBF) {
1262
+ *pp = p + 2;
1263
+ return ((c - 0xE0) << 12) + ((*p - 0x80) << 6) + (p[1] - 0x80);
1264
+ }
1265
+ // otherwise encoding error
1266
+ break;
1267
+ case 0xF0:
1268
+ lower = 0x90; /* reject invalid encoding */
1269
+ upper = 0xBF;
1270
+ goto need3;
1271
+ case 0xF4:
1272
+ lower = 0x80;
1273
+ upper = 0x8F; /* reject values above 0x10FFFF */
1274
+ goto need3;
1275
+ case 0xF1: case 0xF2: case 0xF3:
1276
+ lower = 0x80;
1277
+ upper = 0xBF;
1278
+ need3:
1279
+ if (*p >= lower && *p <= upper && p[1] >= 0x80 && p[1] <= 0xBF
1280
+ && p[2] >= 0x80 && p[2] <= 0xBF) {
1281
+ *pp = p + 3;
1282
+ return ((c - 0xF0) << 18) + ((*p - 0x80) << 12) +
1283
+ ((p[1] - 0x80) << 6) + (p[2] - 0x80);
1284
+ }
1285
+ // otherwise encoding error
1286
+ break;
1287
+ default:
1288
+ // invalid lead byte
1289
+ break;
1290
+ }
1291
+ *pp = p;
1292
+ return 0xFFFD;
1293
+ }
1294
+
1295
+ static inline uint32_t utf8_decode_len(const uint8_t *p, size_t max_len, const uint8_t **pp) {
1296
+ switch (max_len) {
1297
+ case 0:
1298
+ *pp = p;
1299
+ return 0xFFFD;
1300
+ case 1:
1301
+ if (*p < 0x80)
1302
+ goto good;
1303
+ break;
1304
+ case 2:
1305
+ if (*p < 0xE0)
1306
+ goto good;
1307
+ break;
1308
+ case 3:
1309
+ if (*p < 0xF0)
1310
+ goto good;
1311
+ break;
1312
+ default:
1313
+ good:
1314
+ return utf8_decode(p, pp);
1315
+ }
1316
+ *pp = p + 1;
1317
+ return 0xFFFD;
1318
+ }
1319
+
1320
+ /* Scan a UTF-8 encoded buffer for content type
1321
+ `buf` is a valid pointer to a UTF-8 encoded string
1322
+ `len` is the number of bytes to scan
1323
+ `plen` points to a `size_t` variable to receive the number of units
1324
+ Return value is a mask of bits.
1325
+ - `UTF8_PLAIN_ASCII`: return value for 7-bit ASCII plain text
1326
+ - `UTF8_NON_ASCII`: bit for non ASCII code points (8-bit or more)
1327
+ - `UTF8_HAS_16BIT`: bit for 16-bit code points
1328
+ - `UTF8_HAS_NON_BMP1`: bit for non-BMP1 code points, needs UTF-16 surrogate pairs
1329
+ - `UTF8_HAS_ERRORS`: bit for encoding errors
1330
+ */
1331
+ static inline int utf8_scan(const char *buf, size_t buf_len, size_t *plen)
1332
+ {
1333
+ const uint8_t *p, *p_end, *p_next;
1334
+ size_t i, len;
1335
+ int kind;
1336
+ uint8_t cbits;
1337
+
1338
+ kind = UTF8_PLAIN_ASCII;
1339
+ cbits = 0;
1340
+ len = buf_len;
1341
+ // TODO: handle more than 1 byte at a time
1342
+ for (i = 0; i < buf_len; i++)
1343
+ cbits |= buf[i];
1344
+ if (cbits >= 0x80) {
1345
+ p = (const uint8_t *)buf;
1346
+ p_end = p + buf_len;
1347
+ kind = UTF8_NON_ASCII;
1348
+ len = 0;
1349
+ while (p < p_end) {
1350
+ len++;
1351
+ if (*p++ >= 0x80) {
1352
+ /* parse UTF-8 sequence, check for encoding error */
1353
+ uint32_t c = utf8_decode_len(p - 1, p_end - (p - 1), &p_next);
1354
+ if (p_next == p)
1355
+ kind |= UTF8_HAS_ERRORS;
1356
+ p = p_next;
1357
+ if (c > 0xFF) {
1358
+ kind |= UTF8_HAS_16BIT;
1359
+ if (c > 0xFFFF) {
1360
+ len++;
1361
+ kind |= UTF8_HAS_NON_BMP1;
1362
+ }
1363
+ }
1364
+ }
1365
+ }
1366
+ }
1367
+ *plen = len;
1368
+ return kind;
1369
+ }
1370
+
1371
+ /* Decode a string encoded in UTF-8 into an array of bytes
1372
+ `src` points to the source string. It is assumed to be correctly encoded
1373
+ and only contains code points below 0x800
1374
+ `src_len` is the length of the source string
1375
+ `dest` points to the destination array, it can be null if `dest_len` is `0`
1376
+ `dest_len` is the length of the destination array. A null
1377
+ terminator is stored at the end of the array unless `dest_len` is `0`.
1378
+ */
1379
+ static inline size_t utf8_decode_buf8(uint8_t *dest, size_t dest_len, const char *src, size_t src_len)
1380
+ {
1381
+ const uint8_t *p, *p_end;
1382
+ size_t i;
1383
+
1384
+ p = (const uint8_t *)src;
1385
+ p_end = p + src_len;
1386
+ for (i = 0; p < p_end; i++) {
1387
+ uint32_t c = *p++;
1388
+ if (c >= 0xC0)
1389
+ c = (c << 6) + *p++ - ((0xC0 << 6) + 0x80);
1390
+ if (i < dest_len)
1391
+ dest[i] = c;
1392
+ }
1393
+ if (i < dest_len)
1394
+ dest[i] = '\0';
1395
+ else if (dest_len > 0)
1396
+ dest[dest_len - 1] = '\0';
1397
+ return i;
1398
+ }
1399
+
1400
+ /* Decode a string encoded in UTF-8 into an array of 16-bit words
1401
+ `src` points to the source string. It is assumed to be correctly encoded.
1402
+ `src_len` is the length of the source string
1403
+ `dest` points to the destination array, it can be null if `dest_len` is `0`
1404
+ `dest_len` is the length of the destination array. No null terminator is
1405
+ stored at the end of the array.
1406
+ */
1407
+ static inline size_t utf8_decode_buf16(uint16_t *dest, size_t dest_len, const char *src, size_t src_len)
1408
+ {
1409
+ const uint8_t *p, *p_end;
1410
+ size_t i;
1411
+
1412
+ p = (const uint8_t *)src;
1413
+ p_end = p + src_len;
1414
+ for (i = 0; p < p_end; i++) {
1415
+ uint32_t c = *p++;
1416
+ if (c >= 0x80) {
1417
+ /* parse utf-8 sequence */
1418
+ c = utf8_decode_len(p - 1, p_end - (p - 1), &p);
1419
+ /* encoding errors are converted as 0xFFFD and use a single byte */
1420
+ if (c > 0xFFFF) {
1421
+ if (i < dest_len)
1422
+ dest[i] = get_hi_surrogate(c);
1423
+ i++;
1424
+ c = get_lo_surrogate(c);
1425
+ }
1426
+ }
1427
+ if (i < dest_len)
1428
+ dest[i] = c;
1429
+ }
1430
+ return i;
1431
+ }
1432
+
1433
+ /* Encode a buffer of 8-bit bytes as a UTF-8 encoded string
1434
+ `src` points to the source buffer.
1435
+ `src_len` is the length of the source buffer
1436
+ `dest` points to the destination array, it can be null if `dest_len` is `0`
1437
+ `dest_len` is the length in bytes of the destination array. A null
1438
+ terminator is stored at the end of the array unless `dest_len` is `0`.
1439
+ */
1440
+ static inline size_t utf8_encode_buf8(char *dest, size_t dest_len, const uint8_t *src, size_t src_len)
1441
+ {
1442
+ size_t i, j;
1443
+ uint32_t c;
1444
+
1445
+ for (i = j = 0; i < src_len; i++) {
1446
+ c = src[i];
1447
+ if (c < 0x80) {
1448
+ if (j + 1 >= dest_len)
1449
+ goto overflow;
1450
+ dest[j++] = c;
1451
+ } else {
1452
+ if (j + 2 >= dest_len)
1453
+ goto overflow;
1454
+ dest[j++] = (c >> 6) | 0xC0;
1455
+ dest[j++] = (c & 0x3F) | 0x80;
1456
+ }
1457
+ }
1458
+ if (j < dest_len)
1459
+ dest[j] = '\0';
1460
+ return j;
1461
+
1462
+ overflow:
1463
+ if (j < dest_len)
1464
+ dest[j] = '\0';
1465
+ while (i < src_len)
1466
+ j += 1 + (src[i++] >= 0x80);
1467
+ return j;
1468
+ }
1469
+
1470
+ /* Encode a buffer of 16-bit code points as a UTF-8 encoded string
1471
+ `src` points to the source buffer.
1472
+ `src_len` is the length of the source buffer
1473
+ `dest` points to the destination array, it can be null if `dest_len` is `0`
1474
+ `dest_len` is the length in bytes of the destination array. A null
1475
+ terminator is stored at the end of the array unless `dest_len` is `0`.
1476
+ */
1477
+ static inline size_t utf8_encode_buf16(char *dest, size_t dest_len, const uint16_t *src, size_t src_len)
1478
+ {
1479
+ size_t i, j;
1480
+ uint32_t c;
1481
+
1482
+ for (i = j = 0; i < src_len;) {
1483
+ c = src[i++];
1484
+ if (c < 0x80) {
1485
+ if (j + 1 >= dest_len)
1486
+ goto overflow;
1487
+ dest[j++] = c;
1488
+ } else {
1489
+ if (is_hi_surrogate(c) && i < src_len && is_lo_surrogate(src[i]))
1490
+ c = from_surrogate(c, src[i++]);
1491
+ if (j + utf8_encode_len(c) >= dest_len)
1492
+ goto overflow;
1493
+ j += utf8_encode((uint8_t *)dest + j, c);
1494
+ }
1495
+ }
1496
+ if (j < dest_len)
1497
+ dest[j] = '\0';
1498
+ return j;
1499
+
1500
+ overflow:
1501
+ i -= 1 + (c > 0xFFFF);
1502
+ if (j < dest_len)
1503
+ dest[j] = '\0';
1504
+ while (i < src_len) {
1505
+ c = src[i++];
1506
+ if (c < 0x80) {
1507
+ j++;
1508
+ } else {
1509
+ if (is_hi_surrogate(c) && i < src_len && is_lo_surrogate(src[i]))
1510
+ c = from_surrogate(c, src[i++]);
1511
+ j += utf8_encode_len(c);
1512
+ }
1513
+ }
1514
+ return j;
1515
+ }
1516
+
1517
+ /*---- sorting with opaque argument ----*/
1518
+
1519
+ typedef void (*exchange_f)(void *a, void *b, size_t size);
1520
+ typedef int (*cmp_f)(const void *, const void *, void *opaque);
1521
+
1522
+ static void exchange_bytes(void *a, void *b, size_t size) {
1523
+ uint8_t *ap = (uint8_t *)a;
1524
+ uint8_t *bp = (uint8_t *)b;
1525
+
1526
+ while (size-- != 0) {
1527
+ uint8_t t = *ap;
1528
+ *ap++ = *bp;
1529
+ *bp++ = t;
1530
+ }
1531
+ }
1532
+
1533
+ static void exchange_one_byte(void *a, void *b, size_t size) {
1534
+ uint8_t *ap = (uint8_t *)a;
1535
+ uint8_t *bp = (uint8_t *)b;
1536
+ uint8_t t = *ap;
1537
+ *ap = *bp;
1538
+ *bp = t;
1539
+ }
1540
+
1541
+ static void exchange_int16s(void *a, void *b, size_t size) {
1542
+ uint16_t *ap = (uint16_t *)a;
1543
+ uint16_t *bp = (uint16_t *)b;
1544
+
1545
+ for (size /= sizeof(uint16_t); size-- != 0;) {
1546
+ uint16_t t = *ap;
1547
+ *ap++ = *bp;
1548
+ *bp++ = t;
1549
+ }
1550
+ }
1551
+
1552
+ static void exchange_one_int16(void *a, void *b, size_t size) {
1553
+ uint16_t *ap = (uint16_t *)a;
1554
+ uint16_t *bp = (uint16_t *)b;
1555
+ uint16_t t = *ap;
1556
+ *ap = *bp;
1557
+ *bp = t;
1558
+ }
1559
+
1560
+ static void exchange_int32s(void *a, void *b, size_t size) {
1561
+ uint32_t *ap = (uint32_t *)a;
1562
+ uint32_t *bp = (uint32_t *)b;
1563
+
1564
+ for (size /= sizeof(uint32_t); size-- != 0;) {
1565
+ uint32_t t = *ap;
1566
+ *ap++ = *bp;
1567
+ *bp++ = t;
1568
+ }
1569
+ }
1570
+
1571
+ static void exchange_one_int32(void *a, void *b, size_t size) {
1572
+ uint32_t *ap = (uint32_t *)a;
1573
+ uint32_t *bp = (uint32_t *)b;
1574
+ uint32_t t = *ap;
1575
+ *ap = *bp;
1576
+ *bp = t;
1577
+ }
1578
+
1579
+ static void exchange_int64s(void *a, void *b, size_t size) {
1580
+ uint64_t *ap = (uint64_t *)a;
1581
+ uint64_t *bp = (uint64_t *)b;
1582
+
1583
+ for (size /= sizeof(uint64_t); size-- != 0;) {
1584
+ uint64_t t = *ap;
1585
+ *ap++ = *bp;
1586
+ *bp++ = t;
1587
+ }
1588
+ }
1589
+
1590
+ static void exchange_one_int64(void *a, void *b, size_t size) {
1591
+ uint64_t *ap = (uint64_t *)a;
1592
+ uint64_t *bp = (uint64_t *)b;
1593
+ uint64_t t = *ap;
1594
+ *ap = *bp;
1595
+ *bp = t;
1596
+ }
1597
+
1598
+ static void exchange_int128s(void *a, void *b, size_t size) {
1599
+ uint64_t *ap = (uint64_t *)a;
1600
+ uint64_t *bp = (uint64_t *)b;
1601
+
1602
+ for (size /= sizeof(uint64_t) * 2; size-- != 0; ap += 2, bp += 2) {
1603
+ uint64_t t = ap[0];
1604
+ uint64_t u = ap[1];
1605
+ ap[0] = bp[0];
1606
+ ap[1] = bp[1];
1607
+ bp[0] = t;
1608
+ bp[1] = u;
1609
+ }
1610
+ }
1611
+
1612
+ static void exchange_one_int128(void *a, void *b, size_t size) {
1613
+ uint64_t *ap = (uint64_t *)a;
1614
+ uint64_t *bp = (uint64_t *)b;
1615
+ uint64_t t = ap[0];
1616
+ uint64_t u = ap[1];
1617
+ ap[0] = bp[0];
1618
+ ap[1] = bp[1];
1619
+ bp[0] = t;
1620
+ bp[1] = u;
1621
+ }
1622
+
1623
+ static inline exchange_f exchange_func(const void *base, size_t size) {
1624
+ switch (((uintptr_t)base | (uintptr_t)size) & 15) {
1625
+ case 0:
1626
+ if (size == sizeof(uint64_t) * 2)
1627
+ return exchange_one_int128;
1628
+ else
1629
+ return exchange_int128s;
1630
+ case 8:
1631
+ if (size == sizeof(uint64_t))
1632
+ return exchange_one_int64;
1633
+ else
1634
+ return exchange_int64s;
1635
+ case 4:
1636
+ case 12:
1637
+ if (size == sizeof(uint32_t))
1638
+ return exchange_one_int32;
1639
+ else
1640
+ return exchange_int32s;
1641
+ case 2:
1642
+ case 6:
1643
+ case 10:
1644
+ case 14:
1645
+ if (size == sizeof(uint16_t))
1646
+ return exchange_one_int16;
1647
+ else
1648
+ return exchange_int16s;
1649
+ default:
1650
+ if (size == 1)
1651
+ return exchange_one_byte;
1652
+ else
1653
+ return exchange_bytes;
1654
+ }
1655
+ }
1656
+
1657
+ static void heapsortx(void *base, size_t nmemb, size_t size, cmp_f cmp, void *opaque)
1658
+ {
1659
+ uint8_t *basep = (uint8_t *)base;
1660
+ size_t i, n, c, r;
1661
+ exchange_f swap = exchange_func(base, size);
1662
+
1663
+ if (nmemb > 1) {
1664
+ i = (nmemb / 2) * size;
1665
+ n = nmemb * size;
1666
+
1667
+ while (i > 0) {
1668
+ i -= size;
1669
+ for (r = i; (c = r * 2 + size) < n; r = c) {
1670
+ if (c < n - size && cmp(basep + c, basep + c + size, opaque) <= 0)
1671
+ c += size;
1672
+ if (cmp(basep + r, basep + c, opaque) > 0)
1673
+ break;
1674
+ swap(basep + r, basep + c, size);
1675
+ }
1676
+ }
1677
+ for (i = n - size; i > 0; i -= size) {
1678
+ swap(basep, basep + i, size);
1679
+
1680
+ for (r = 0; (c = r * 2 + size) < i; r = c) {
1681
+ if (c < i - size && cmp(basep + c, basep + c + size, opaque) <= 0)
1682
+ c += size;
1683
+ if (cmp(basep + r, basep + c, opaque) > 0)
1684
+ break;
1685
+ swap(basep + r, basep + c, size);
1686
+ }
1687
+ }
1688
+ }
1689
+ }
1690
+
1691
+ static inline void *med3(void *a, void *b, void *c, cmp_f cmp, void *opaque)
1692
+ {
1693
+ return cmp(a, b, opaque) < 0 ?
1694
+ (cmp(b, c, opaque) < 0 ? b : (cmp(a, c, opaque) < 0 ? c : a )) :
1695
+ (cmp(b, c, opaque) > 0 ? b : (cmp(a, c, opaque) < 0 ? a : c ));
1696
+ }
1697
+
1698
+ /* pointer based version with local stack and insertion sort threshhold */
1699
+ static inline void rqsort(void *base, size_t nmemb, size_t size, cmp_f cmp, void *opaque)
1700
+ {
1701
+ struct { uint8_t *base; size_t count; int depth; } stack[50], *sp = stack;
1702
+ uint8_t *ptr, *pi, *pj, *plt, *pgt, *top, *m;
1703
+ size_t m4, i, lt, gt, span, span2;
1704
+ int c, depth;
1705
+ exchange_f swap = exchange_func(base, size);
1706
+ exchange_f swap_block = exchange_func(base, size | 128);
1707
+
1708
+ if (nmemb < 2 || size <= 0)
1709
+ return;
1710
+
1711
+ sp->base = (uint8_t *)base;
1712
+ sp->count = nmemb;
1713
+ sp->depth = 0;
1714
+ sp++;
1715
+
1716
+ while (sp > stack) {
1717
+ sp--;
1718
+ ptr = sp->base;
1719
+ nmemb = sp->count;
1720
+ depth = sp->depth;
1721
+
1722
+ while (nmemb > 6) {
1723
+ if (++depth > 50) {
1724
+ /* depth check to ensure worst case logarithmic time */
1725
+ heapsortx(ptr, nmemb, size, cmp, opaque);
1726
+ nmemb = 0;
1727
+ break;
1728
+ }
1729
+ /* select median of 3 from 1/4, 1/2, 3/4 positions */
1730
+ /* should use median of 5 or 9? */
1731
+ m4 = (nmemb >> 2) * size;
1732
+ m = med3(ptr + m4, ptr + 2 * m4, ptr + 3 * m4, cmp, opaque);
1733
+ swap(ptr, m, size); /* move the pivot to the start or the array */
1734
+ i = lt = 1;
1735
+ pi = plt = ptr + size;
1736
+ gt = nmemb;
1737
+ pj = pgt = top = ptr + nmemb * size;
1738
+ for (;;) {
1739
+ while (pi < pj && (c = cmp(ptr, pi, opaque)) >= 0) {
1740
+ if (c == 0) {
1741
+ swap(plt, pi, size);
1742
+ lt++;
1743
+ plt += size;
1744
+ }
1745
+ i++;
1746
+ pi += size;
1747
+ }
1748
+ while (pi < (pj -= size) && (c = cmp(ptr, pj, opaque)) <= 0) {
1749
+ if (c == 0) {
1750
+ gt--;
1751
+ pgt -= size;
1752
+ swap(pgt, pj, size);
1753
+ }
1754
+ }
1755
+ if (pi >= pj)
1756
+ break;
1757
+ swap(pi, pj, size);
1758
+ i++;
1759
+ pi += size;
1760
+ }
1761
+ /* array has 4 parts:
1762
+ * from 0 to lt excluded: elements identical to pivot
1763
+ * from lt to pi excluded: elements smaller than pivot
1764
+ * from pi to gt excluded: elements greater than pivot
1765
+ * from gt to n excluded: elements identical to pivot
1766
+ */
1767
+ /* move elements identical to pivot in the middle of the array: */
1768
+ /* swap values in ranges [0..lt[ and [i-lt..i[
1769
+ swapping the smallest span between lt and i-lt is sufficient
1770
+ */
1771
+ span = plt - ptr;
1772
+ span2 = pi - plt;
1773
+ lt = i - lt;
1774
+ if (span > span2)
1775
+ span = span2;
1776
+ swap_block(ptr, pi - span, span);
1777
+ /* swap values in ranges [gt..top[ and [i..top-(top-gt)[
1778
+ swapping the smallest span between top-gt and gt-i is sufficient
1779
+ */
1780
+ span = top - pgt;
1781
+ span2 = pgt - pi;
1782
+ pgt = top - span2;
1783
+ gt = nmemb - (gt - i);
1784
+ if (span > span2)
1785
+ span = span2;
1786
+ swap_block(pi, top - span, span);
1787
+
1788
+ /* now array has 3 parts:
1789
+ * from 0 to lt excluded: elements smaller than pivot
1790
+ * from lt to gt excluded: elements identical to pivot
1791
+ * from gt to n excluded: elements greater than pivot
1792
+ */
1793
+ /* stack the larger segment and keep processing the smaller one
1794
+ to minimize stack use for pathological distributions */
1795
+ if (lt > nmemb - gt) {
1796
+ sp->base = ptr;
1797
+ sp->count = lt;
1798
+ sp->depth = depth;
1799
+ sp++;
1800
+ ptr = pgt;
1801
+ nmemb -= gt;
1802
+ } else {
1803
+ sp->base = pgt;
1804
+ sp->count = nmemb - gt;
1805
+ sp->depth = depth;
1806
+ sp++;
1807
+ nmemb = lt;
1808
+ }
1809
+ }
1810
+ /* Use insertion sort for small fragments */
1811
+ for (pi = ptr + size, top = ptr + nmemb * size; pi < top; pi += size) {
1812
+ for (pj = pi; pj > ptr && cmp(pj - size, pj, opaque) > 0; pj -= size)
1813
+ swap(pj, pj - size, size);
1814
+ }
1815
+ }
1816
+ }
1817
+
1818
+ /*---- Portable time functions ----*/
1819
+
1820
+ #ifdef _WIN32
1821
+ // From: https://stackoverflow.com/a/26085827
1822
+ static int gettimeofday_msvc(struct timeval *tp)
1823
+ {
1824
+ static const uint64_t EPOCH = ((uint64_t)116444736000000000ULL);
1825
+
1826
+ SYSTEMTIME system_time;
1827
+ FILETIME file_time;
1828
+ uint64_t time;
1829
+
1830
+ GetSystemTime(&system_time);
1831
+ SystemTimeToFileTime(&system_time, &file_time);
1832
+ time = ((uint64_t)file_time.dwLowDateTime);
1833
+ time += ((uint64_t)file_time.dwHighDateTime) << 32;
1834
+
1835
+ tp->tv_sec = (long)((time - EPOCH) / 10000000L);
1836
+ tp->tv_usec = (long)(system_time.wMilliseconds * 1000);
1837
+
1838
+ return 0;
1839
+ }
1840
+
1841
+ static inline uint64_t js__hrtime_ns(void) {
1842
+ LARGE_INTEGER counter, frequency;
1843
+ double scaled_freq;
1844
+ double result;
1845
+
1846
+ if (!QueryPerformanceFrequency(&frequency))
1847
+ abort();
1848
+ assert(frequency.QuadPart != 0);
1849
+
1850
+ if (!QueryPerformanceCounter(&counter))
1851
+ abort();
1852
+ assert(counter.QuadPart != 0);
1853
+
1854
+ /* Because we have no guarantee about the order of magnitude of the
1855
+ * performance counter interval, integer math could cause this computation
1856
+ * to overflow. Therefore we resort to floating point math.
1857
+ */
1858
+ scaled_freq = (double) frequency.QuadPart / NANOSEC;
1859
+ result = (double) counter.QuadPart / scaled_freq;
1860
+ return (uint64_t) result;
1861
+ }
1862
+ #else
1863
+ static inline uint64_t js__hrtime_ns(void) {
1864
+ #ifdef __DJGPP
1865
+ struct timeval tv;
1866
+ if (gettimeofday(&tv, NULL))
1867
+ abort();
1868
+ return tv.tv_sec * NANOSEC + tv.tv_usec * 1000;
1869
+ #else
1870
+ struct timespec t;
1871
+
1872
+ if (clock_gettime(CLOCK_MONOTONIC, &t))
1873
+ abort();
1874
+
1875
+ return t.tv_sec * NANOSEC + t.tv_nsec;
1876
+ #endif
1877
+ }
1878
+ #endif
1879
+
1880
+ static inline int64_t js__gettimeofday_us(void) {
1881
+ struct timeval tv;
1882
+ #ifdef _WIN32
1883
+ gettimeofday_msvc(&tv);
1884
+ #else
1885
+ gettimeofday(&tv, NULL);
1886
+ #endif
1887
+ return ((int64_t)tv.tv_sec * 1000000) + tv.tv_usec;
1888
+ }
1889
+
1890
+ // Fills |buf| with |len| bytes from the entropy source of the operating
1891
+ // system. Returns 0 on success, -1 on failure.
1892
+ static inline int js__random_bytes(void *buf, size_t len) {
1893
+ uint8_t *p = buf;
1894
+ #if defined(_WIN32)
1895
+ typedef BOOLEAN NTAPI rtl_gen_random(PVOID buf, ULONG len);
1896
+ static void *volatile cached_fp;
1897
+ rtl_gen_random *fn;
1898
+ void *fp;
1899
+ HMODULE h;
1900
+ ULONG n;
1901
+
1902
+ // not using locks to avoid deadlocking; DllMain() and LoadLibraryA()
1903
+ // both take out the global loader lock
1904
+ fp = InterlockedCompareExchangePointer(&cached_fp, NULL, NULL);
1905
+ if (fp)
1906
+ goto gen;
1907
+ h = LoadLibraryA("advapi32.dll");
1908
+ if (!h)
1909
+ return -1;
1910
+ fp = (void *)GetProcAddress(h, "SystemFunction036"); // RtlGenRandom
1911
+ if (!fp) {
1912
+ FreeLibrary(h);
1913
+ return -1;
1914
+ }
1915
+ if (InterlockedCompareExchangePointer(&cached_fp, fp, NULL))
1916
+ FreeLibrary(h); // lost a benign race with another thread
1917
+ gen:
1918
+ fn = (rtl_gen_random *)fp;
1919
+ while (len > 0) {
1920
+ n = len > 0x40000000 ? 0x40000000 : (ULONG)len;
1921
+ if (!fn(p, n))
1922
+ return -1;
1923
+ p += n;
1924
+ len -= n;
1925
+ }
1926
+ return 0;
1927
+ #elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || \
1928
+ defined(__NetBSD__) || defined(__DragonFly__)
1929
+ arc4random_buf(p, len);
1930
+ return 0;
1931
+ #elif defined(__wasi__) || defined(EMSCRIPTEN) || defined(__EMSCRIPTEN__)
1932
+ while (len > 0) {
1933
+ size_t n = len > 256 ? 256 : len; // the most getentropy() gives
1934
+ if (getentropy(p, n))
1935
+ return -1;
1936
+ p += n;
1937
+ len -= n;
1938
+ }
1939
+ return 0;
1940
+ #elif defined(__DJGPP)
1941
+ // no entropy source, the clock has to do
1942
+ uint64_t x = js__gettimeofday_us() | 1;
1943
+ while (len > 0) {
1944
+ // xorshift64*
1945
+ x ^= x >> 12;
1946
+ x ^= x << 25;
1947
+ x ^= x >> 27;
1948
+ *p++ = (x * 0x2545F4914F6CDD1D) >> 56;
1949
+ len--;
1950
+ }
1951
+ return 0;
1952
+ #else
1953
+ ssize_t n;
1954
+ int fd;
1955
+ #if defined(__linux__) && defined(SYS_getrandom)
1956
+ // by system call number because older libcs lack the wrapper
1957
+ while (len > 0) {
1958
+ n = syscall(SYS_getrandom, p, len, 0);
1959
+ if (n < 0 && errno == EINTR)
1960
+ continue;
1961
+ if (n <= 0)
1962
+ break; // old kernel or seccomp filter, try /dev/urandom
1963
+ js__msan_unpoison(p, n);
1964
+ p += n;
1965
+ len -= n;
1966
+ }
1967
+ if (len == 0)
1968
+ return 0;
1969
+ #endif
1970
+ do
1971
+ fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
1972
+ while (fd < 0 && errno == EINTR);
1973
+ if (fd < 0)
1974
+ return -1;
1975
+ while (len > 0) {
1976
+ n = read(fd, p, len);
1977
+ if (n < 0 && errno == EINTR)
1978
+ continue;
1979
+ if (n <= 0)
1980
+ break;
1981
+ p += n;
1982
+ len -= n;
1983
+ }
1984
+ close(fd);
1985
+ return len == 0 ? 0 : -1;
1986
+ #endif
1987
+ }
1988
+
1989
+ #if defined(_WIN32)
1990
+ static inline int js_exepath(char *buffer, size_t *size_ptr) {
1991
+ int utf8_len, utf16_buffer_len, utf16_len;
1992
+ WCHAR* utf16_buffer;
1993
+
1994
+ if (buffer == NULL || size_ptr == NULL || *size_ptr == 0)
1995
+ return -1;
1996
+
1997
+ if (*size_ptr > 32768) {
1998
+ /* Windows paths can never be longer than this. */
1999
+ utf16_buffer_len = 32768;
2000
+ } else {
2001
+ utf16_buffer_len = (int)*size_ptr;
2002
+ }
2003
+
2004
+ utf16_buffer = malloc(sizeof(WCHAR) * utf16_buffer_len);
2005
+ if (!utf16_buffer)
2006
+ return -1;
2007
+
2008
+ /* Get the path as UTF-16. */
2009
+ utf16_len = GetModuleFileNameW(NULL, utf16_buffer, utf16_buffer_len);
2010
+ if (utf16_len <= 0)
2011
+ goto error;
2012
+
2013
+ /* Convert to UTF-8 */
2014
+ utf8_len = WideCharToMultiByte(CP_UTF8,
2015
+ 0,
2016
+ utf16_buffer,
2017
+ -1,
2018
+ buffer,
2019
+ (int)*size_ptr,
2020
+ NULL,
2021
+ NULL);
2022
+ if (utf8_len == 0)
2023
+ goto error;
2024
+
2025
+ free(utf16_buffer);
2026
+
2027
+ /* utf8_len *does* include the terminating null at this point, but the
2028
+ * returned size shouldn't. */
2029
+ *size_ptr = utf8_len - 1;
2030
+ return 0;
2031
+
2032
+ error:
2033
+ free(utf16_buffer);
2034
+ return -1;
2035
+ }
2036
+ #elif defined(__APPLE__)
2037
+ static inline int js_exepath(char *buffer, size_t *size) {
2038
+ /* realpath(exepath) may be > PATH_MAX so double it to be on the safe side. */
2039
+ char abspath[PATH_MAX * 2 + 1];
2040
+ char exepath[PATH_MAX + 1];
2041
+ uint32_t exepath_size;
2042
+ size_t abspath_size;
2043
+
2044
+ if (buffer == NULL || size == NULL || *size == 0)
2045
+ return -1;
2046
+
2047
+ exepath_size = sizeof(exepath);
2048
+ if (_NSGetExecutablePath(exepath, &exepath_size))
2049
+ return -1;
2050
+
2051
+ if (realpath(exepath, abspath) != abspath)
2052
+ return -1;
2053
+
2054
+ abspath_size = strlen(abspath);
2055
+ if (abspath_size == 0)
2056
+ return -1;
2057
+
2058
+ *size -= 1;
2059
+ if (*size > abspath_size)
2060
+ *size = abspath_size;
2061
+
2062
+ memcpy(buffer, abspath, *size);
2063
+ buffer[*size] = '\0';
2064
+
2065
+ return 0;
2066
+ }
2067
+ #elif defined(__linux__) || defined(__GNU__)
2068
+ static inline int js_exepath(char *buffer, size_t *size) {
2069
+ ssize_t n;
2070
+
2071
+ if (buffer == NULL || size == NULL || *size == 0)
2072
+ return -1;
2073
+
2074
+ n = *size - 1;
2075
+ if (n > 0)
2076
+ n = readlink("/proc/self/exe", buffer, n);
2077
+
2078
+ if (n == -1)
2079
+ return n;
2080
+
2081
+ buffer[n] = '\0';
2082
+ *size = n;
2083
+
2084
+ return 0;
2085
+ }
2086
+ #else
2087
+ static inline int js_exepath(char* buffer, size_t* size_ptr) {
2088
+ return -1;
2089
+ }
2090
+ #endif
2091
+
2092
+ /*--- Cross-platform threading APIs. ----*/
2093
+
2094
+ #if JS_HAVE_THREADS
2095
+ #if defined(_WIN32)
2096
+ typedef void (*js__once_cb)(void);
2097
+
2098
+ typedef struct {
2099
+ js__once_cb callback;
2100
+ } js__once_data_t;
2101
+
2102
+ static int WINAPI js__once_inner(INIT_ONCE *once, void *param, void **context) {
2103
+ js__once_data_t *data = param;
2104
+
2105
+ data->callback();
2106
+
2107
+ return 1;
2108
+ }
2109
+
2110
+ static inline void js_once(js_once_t *guard, js__once_cb callback) {
2111
+ js__once_data_t data = { .callback = callback };
2112
+ InitOnceExecuteOnce(guard, js__once_inner, (void*) &data, NULL);
2113
+ }
2114
+
2115
+ static inline void js_mutex_init(js_mutex_t *mutex) {
2116
+ InitializeCriticalSection(mutex);
2117
+ }
2118
+
2119
+ static inline void js_mutex_destroy(js_mutex_t *mutex) {
2120
+ DeleteCriticalSection(mutex);
2121
+ }
2122
+
2123
+ static inline void js_mutex_lock(js_mutex_t *mutex) {
2124
+ EnterCriticalSection(mutex);
2125
+ }
2126
+
2127
+ static inline void js_mutex_unlock(js_mutex_t *mutex) {
2128
+ LeaveCriticalSection(mutex);
2129
+ }
2130
+
2131
+ static inline void js_cond_init(js_cond_t *cond) {
2132
+ InitializeConditionVariable(cond);
2133
+ }
2134
+
2135
+ static inline void js_cond_destroy(js_cond_t *cond) {
2136
+ /* nothing to do */
2137
+ (void) cond;
2138
+ }
2139
+
2140
+ static inline void js_cond_signal(js_cond_t *cond) {
2141
+ WakeConditionVariable(cond);
2142
+ }
2143
+
2144
+ static inline void js_cond_broadcast(js_cond_t *cond) {
2145
+ WakeAllConditionVariable(cond);
2146
+ }
2147
+
2148
+ static inline void js_cond_wait(js_cond_t *cond, js_mutex_t *mutex) {
2149
+ if (!SleepConditionVariableCS(cond, mutex, INFINITE))
2150
+ abort();
2151
+ }
2152
+
2153
+ static inline int js_cond_timedwait(js_cond_t *cond, js_mutex_t *mutex, uint64_t timeout) {
2154
+ if (SleepConditionVariableCS(cond, mutex, (DWORD)(timeout / 1e6)))
2155
+ return 0;
2156
+ if (GetLastError() != ERROR_TIMEOUT)
2157
+ abort();
2158
+ return -1;
2159
+ }
2160
+
2161
+ static inline int js_thread_create(js_thread_t *thrd, void (*start)(void *), void *arg,
2162
+ int flags)
2163
+ {
2164
+ HANDLE h, cp;
2165
+
2166
+ *thrd = INVALID_HANDLE_VALUE;
2167
+ if (flags & ~JS_THREAD_CREATE_DETACHED)
2168
+ return -1;
2169
+ h = (HANDLE)_beginthread(start, /*stacksize*/2<<20, arg);
2170
+ if (!h)
2171
+ return -1;
2172
+ if (flags & JS_THREAD_CREATE_DETACHED)
2173
+ return 0;
2174
+ // _endthread() automatically closes the handle but we want to wait on
2175
+ // it so make a copy. Race-y for very short-lived threads. Can be solved
2176
+ // by switching to _beginthreadex(CREATE_SUSPENDED) but means changing
2177
+ // |start| from __cdecl to __stdcall.
2178
+ cp = GetCurrentProcess();
2179
+ if (DuplicateHandle(cp, h, cp, thrd, 0, FALSE, DUPLICATE_SAME_ACCESS))
2180
+ return 0;
2181
+ return -1;
2182
+ }
2183
+
2184
+ static inline int js_thread_join(js_thread_t thrd)
2185
+ {
2186
+ if (WaitForSingleObject(thrd, INFINITE))
2187
+ return -1;
2188
+ CloseHandle(thrd);
2189
+ return 0;
2190
+ }
2191
+
2192
+ #else /* !defined(_WIN32) */
2193
+
2194
+ static inline void js_once(js_once_t *guard, void (*callback)(void)) {
2195
+ if (pthread_once(guard, callback))
2196
+ abort();
2197
+ }
2198
+
2199
+ static inline void js_mutex_init(js_mutex_t *mutex) {
2200
+ if (pthread_mutex_init(mutex, NULL))
2201
+ abort();
2202
+ }
2203
+
2204
+ static inline void js_mutex_destroy(js_mutex_t *mutex) {
2205
+ if (pthread_mutex_destroy(mutex))
2206
+ abort();
2207
+ }
2208
+
2209
+ static inline void js_mutex_lock(js_mutex_t *mutex) {
2210
+ if (pthread_mutex_lock(mutex))
2211
+ abort();
2212
+ }
2213
+
2214
+ static inline void js_mutex_unlock(js_mutex_t *mutex) {
2215
+ if (pthread_mutex_unlock(mutex))
2216
+ abort();
2217
+ }
2218
+
2219
+ static inline void js_cond_init(js_cond_t *cond) {
2220
+ #if defined(__APPLE__) && defined(__MACH__)
2221
+ if (pthread_cond_init(cond, NULL))
2222
+ abort();
2223
+ #else
2224
+ pthread_condattr_t attr;
2225
+
2226
+ if (pthread_condattr_init(&attr))
2227
+ abort();
2228
+
2229
+ if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC))
2230
+ abort();
2231
+
2232
+ if (pthread_cond_init(cond, &attr))
2233
+ abort();
2234
+
2235
+ if (pthread_condattr_destroy(&attr))
2236
+ abort();
2237
+ #endif
2238
+ }
2239
+
2240
+ static inline void js_cond_destroy(js_cond_t *cond) {
2241
+ #if defined(__APPLE__) && defined(__MACH__)
2242
+ /* It has been reported that destroying condition variables that have been
2243
+ * signalled but not waited on can sometimes result in application crashes.
2244
+ * See https://codereview.chromium.org/1323293005.
2245
+ */
2246
+ pthread_mutex_t mutex;
2247
+ struct timespec ts;
2248
+ int err;
2249
+
2250
+ if (pthread_mutex_init(&mutex, NULL))
2251
+ abort();
2252
+
2253
+ if (pthread_mutex_lock(&mutex))
2254
+ abort();
2255
+
2256
+ ts.tv_sec = 0;
2257
+ ts.tv_nsec = 1;
2258
+
2259
+ err = pthread_cond_timedwait_relative_np(cond, &mutex, &ts);
2260
+ if (err != 0 && err != ETIMEDOUT)
2261
+ abort();
2262
+
2263
+ if (pthread_mutex_unlock(&mutex))
2264
+ abort();
2265
+
2266
+ if (pthread_mutex_destroy(&mutex))
2267
+ abort();
2268
+ #endif /* defined(__APPLE__) && defined(__MACH__) */
2269
+
2270
+ if (pthread_cond_destroy(cond))
2271
+ abort();
2272
+ }
2273
+
2274
+ static inline void js_cond_signal(js_cond_t *cond) {
2275
+ if (pthread_cond_signal(cond))
2276
+ abort();
2277
+ }
2278
+
2279
+ static inline void js_cond_broadcast(js_cond_t *cond) {
2280
+ if (pthread_cond_broadcast(cond))
2281
+ abort();
2282
+ }
2283
+
2284
+ static inline void js_cond_wait(js_cond_t *cond, js_mutex_t *mutex) {
2285
+ #if defined(__APPLE__) && defined(__MACH__)
2286
+ int r;
2287
+
2288
+ errno = 0;
2289
+ r = pthread_cond_wait(cond, mutex);
2290
+
2291
+ /* Workaround for a bug in OS X at least up to 13.6
2292
+ * See https://github.com/libuv/libuv/issues/4165
2293
+ */
2294
+ if (r == EINVAL && errno == EBUSY)
2295
+ return;
2296
+ if (r)
2297
+ abort();
2298
+ #else
2299
+ if (pthread_cond_wait(cond, mutex))
2300
+ abort();
2301
+ #endif
2302
+ }
2303
+
2304
+ static inline int js_cond_timedwait(js_cond_t *cond, js_mutex_t *mutex, uint64_t timeout) {
2305
+ int r;
2306
+ struct timespec ts;
2307
+
2308
+ #if !defined(__APPLE__)
2309
+ timeout += js__hrtime_ns();
2310
+ #endif
2311
+
2312
+ ts.tv_sec = timeout / NANOSEC;
2313
+ ts.tv_nsec = timeout % NANOSEC;
2314
+ #if defined(__APPLE__) && defined(__MACH__)
2315
+ r = pthread_cond_timedwait_relative_np(cond, mutex, &ts);
2316
+ #else
2317
+ r = pthread_cond_timedwait(cond, mutex, &ts);
2318
+ #endif
2319
+
2320
+ if (r == 0)
2321
+ return 0;
2322
+
2323
+ if (r == ETIMEDOUT)
2324
+ return -1;
2325
+
2326
+ abort();
2327
+
2328
+ /* Pacify some compilers. */
2329
+ return -1;
2330
+ }
2331
+
2332
+ static inline int js_thread_create(js_thread_t *thrd, void (*start)(void *), void *arg,
2333
+ int flags)
2334
+ {
2335
+ union {
2336
+ void (*x)(void *);
2337
+ void *(*f)(void *);
2338
+ } u = {start};
2339
+ pthread_attr_t attr;
2340
+ int ret;
2341
+
2342
+ if (flags & ~JS_THREAD_CREATE_DETACHED)
2343
+ return -1;
2344
+ if (pthread_attr_init(&attr))
2345
+ return -1;
2346
+ ret = -1;
2347
+ if (pthread_attr_setstacksize(&attr, 2<<20))
2348
+ goto fail;
2349
+ if (flags & JS_THREAD_CREATE_DETACHED)
2350
+ if (pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED))
2351
+ goto fail;
2352
+ if (pthread_create(thrd, &attr, u.f, arg))
2353
+ goto fail;
2354
+ ret = 0;
2355
+ fail:
2356
+ pthread_attr_destroy(&attr);
2357
+ return ret;
2358
+ }
2359
+
2360
+ static inline int js_thread_join(js_thread_t thrd)
2361
+ {
2362
+ if (pthread_join(thrd, NULL))
2363
+ return -1;
2364
+ return 0;
2365
+ }
2366
+
2367
+ #endif /* !defined(_WIN32) */
2368
+ #endif /* JS_HAVE_THREADS */
2369
+
2370
+ #ifdef __cplusplus
2371
+ } /* extern "C" { */
2372
+ #endif
2373
+
2374
+ #endif /* CUTILS_H */