krillplayer-plugin 0.5.0 → 0.7.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 (150) hide show
  1. package/LICENSE +2 -1
  2. package/README.md +2 -0
  3. package/dist/cli.js +397 -80
  4. package/dist/crypto.js +38 -11
  5. package/dist/engine/engine.c +6 -3
  6. package/kernel/Sources/CQuickJS/include/kernel_qjs.h +8 -1
  7. package/kernel/Sources/CQuickJS/kernel_internal.h +54 -0
  8. package/kernel/Sources/CQuickJS/kernel_qjs.c +17 -29
  9. package/kernel/Sources/CQuickJS/kernel_wasm.c +1155 -0
  10. package/kernel/Sources/CWAMR/include/kernel_wamr.h +151 -0
  11. package/kernel/Sources/CWAMR/include/kernel_wamr_config.h +66 -0
  12. package/kernel/Sources/CWAMR/vendor/LICENSE +219 -0
  13. package/kernel/Sources/CWAMR/vendor/core/config.h +727 -0
  14. package/kernel/Sources/CWAMR/vendor/core/iwasm/aot/aot_runtime.h +899 -0
  15. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/arch/invokeNative_general.c +123 -0
  16. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_application.c +933 -0
  17. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_blocking_op.c +92 -0
  18. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_c_api.c +5391 -0
  19. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_c_api_internal.h +246 -0
  20. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_exec_env.c +336 -0
  21. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_exec_env.h +328 -0
  22. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_loader_common.c +242 -0
  23. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_loader_common.h +62 -0
  24. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_memory.c +1999 -0
  25. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_memory.h +154 -0
  26. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_native.c +1532 -0
  27. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_native.h +117 -0
  28. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_runtime_common.c +8070 -0
  29. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_runtime_common.h +1396 -0
  30. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_shared_memory.c +477 -0
  31. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_shared_memory.h +63 -0
  32. package/kernel/Sources/CWAMR/vendor/core/iwasm/common/wasm_suspend_flags.h +52 -0
  33. package/kernel/Sources/CWAMR/vendor/core/iwasm/compilation/aot.h +366 -0
  34. package/kernel/Sources/CWAMR/vendor/core/iwasm/include/lib_export.h +60 -0
  35. package/kernel/Sources/CWAMR/vendor/core/iwasm/include/wasm_c_api.h +910 -0
  36. package/kernel/Sources/CWAMR/vendor/core/iwasm/include/wasm_export.h +2440 -0
  37. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm.h +1492 -0
  38. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_interp.h +122 -0
  39. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_interp_fast.c +7998 -0
  40. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_loader.c +16909 -0
  41. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_loader.h +80 -0
  42. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_opcode.h +1043 -0
  43. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_runtime.c +5174 -0
  44. package/kernel/Sources/CWAMR/vendor/core/iwasm/interpreter/wasm_runtime.h +912 -0
  45. package/kernel/Sources/CWAMR/vendor/core/iwasm/libraries/thread-mgr/thread_manager.c +1569 -0
  46. package/kernel/Sources/CWAMR/vendor/core/iwasm/libraries/thread-mgr/thread_manager.h +256 -0
  47. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_alloc.c +1169 -0
  48. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_gc.c +497 -0
  49. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_gc.h +350 -0
  50. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_gc_internal.h +384 -0
  51. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_hmu.c +95 -0
  52. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/ems/ems_kfc.c +547 -0
  53. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/mem_alloc.c +252 -0
  54. package/kernel/Sources/CWAMR/vendor/core/shared/mem-alloc/mem_alloc.h +96 -0
  55. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/memory/mremap.c +12 -0
  56. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_blocking_op.c +70 -0
  57. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_malloc.c +72 -0
  58. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_memmap.c +302 -0
  59. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_sleep.c +20 -0
  60. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_thread.c +784 -0
  61. package/kernel/Sources/CWAMR/vendor/core/shared/platform/common/posix/posix_time.c +28 -0
  62. package/kernel/Sources/CWAMR/vendor/core/shared/platform/darwin/platform_init.c +43 -0
  63. package/kernel/Sources/CWAMR/vendor/core/shared/platform/darwin/platform_internal.h +126 -0
  64. package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_api_extension.h +1676 -0
  65. package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_api_vmcore.h +192 -0
  66. package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_common.h +204 -0
  67. package/kernel/Sources/CWAMR/vendor/core/shared/platform/include/platform_wasi_types.h +610 -0
  68. package/kernel/Sources/CWAMR/vendor/core/shared/platform/linux/platform_init.c +43 -0
  69. package/kernel/Sources/CWAMR/vendor/core/shared/platform/linux/platform_internal.h +139 -0
  70. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_assert.c +25 -0
  71. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_assert.h +42 -0
  72. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_atomic.h +300 -0
  73. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_bitmap.c +27 -0
  74. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_bitmap.h +114 -0
  75. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_common.c +217 -0
  76. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_common.h +83 -0
  77. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_hashmap.c +339 -0
  78. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_hashmap.h +168 -0
  79. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_leb128.c +77 -0
  80. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_leb128.h +30 -0
  81. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_list.c +111 -0
  82. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_list.h +109 -0
  83. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_log.c +111 -0
  84. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_log.h +94 -0
  85. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_platform.h +38 -0
  86. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_queue.c +256 -0
  87. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_queue.h +80 -0
  88. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_vector.c +279 -0
  89. package/kernel/Sources/CWAMR/vendor/core/shared/utils/bh_vector.h +126 -0
  90. package/kernel/Sources/CWAMR/vendor/core/shared/utils/gnuc.h +14 -0
  91. package/kernel/Sources/CWAMR/vendor/core/shared/utils/runtime_timer.c +469 -0
  92. package/kernel/Sources/CWAMR/vendor/core/shared/utils/runtime_timer.h +51 -0
  93. package/kernel/Sources/CWAMR/vendor/core/version.h +24 -0
  94. package/kernel/Sources/CWAMR/wamr_bh_assert.c +98 -0
  95. package/kernel/Sources/CWAMR/wamr_bh_bitmap.c +98 -0
  96. package/kernel/Sources/CWAMR/wamr_bh_common.c +98 -0
  97. package/kernel/Sources/CWAMR/wamr_bh_hashmap.c +98 -0
  98. package/kernel/Sources/CWAMR/wamr_bh_leb128.c +98 -0
  99. package/kernel/Sources/CWAMR/wamr_bh_list.c +98 -0
  100. package/kernel/Sources/CWAMR/wamr_bh_log.c +98 -0
  101. package/kernel/Sources/CWAMR/wamr_bh_queue.c +98 -0
  102. package/kernel/Sources/CWAMR/wamr_bh_vector.c +98 -0
  103. package/kernel/Sources/CWAMR/wamr_ems_alloc.c +98 -0
  104. package/kernel/Sources/CWAMR/wamr_ems_gc.c +98 -0
  105. package/kernel/Sources/CWAMR/wamr_ems_hmu.c +98 -0
  106. package/kernel/Sources/CWAMR/wamr_ems_kfc.c +98 -0
  107. package/kernel/Sources/CWAMR/wamr_invokeNative_general.c +98 -0
  108. package/kernel/Sources/CWAMR/wamr_mem_alloc.c +98 -0
  109. package/kernel/Sources/CWAMR/wamr_mremap.c +98 -0
  110. package/kernel/Sources/CWAMR/wamr_platform_init_darwin.c +100 -0
  111. package/kernel/Sources/CWAMR/wamr_platform_init_linux.c +100 -0
  112. package/kernel/Sources/CWAMR/wamr_posix_blocking_op.c +98 -0
  113. package/kernel/Sources/CWAMR/wamr_posix_malloc.c +98 -0
  114. package/kernel/Sources/CWAMR/wamr_posix_memmap.c +98 -0
  115. package/kernel/Sources/CWAMR/wamr_posix_sleep.c +98 -0
  116. package/kernel/Sources/CWAMR/wamr_posix_thread.c +98 -0
  117. package/kernel/Sources/CWAMR/wamr_posix_time.c +98 -0
  118. package/kernel/Sources/CWAMR/wamr_runtime_timer.c +98 -0
  119. package/kernel/Sources/CWAMR/wamr_thread_manager.c +98 -0
  120. package/kernel/Sources/CWAMR/wamr_wasm_application.c +98 -0
  121. package/kernel/Sources/CWAMR/wamr_wasm_blocking_op.c +98 -0
  122. package/kernel/Sources/CWAMR/wamr_wasm_c_api.c +98 -0
  123. package/kernel/Sources/CWAMR/wamr_wasm_exec_env.c +98 -0
  124. package/kernel/Sources/CWAMR/wamr_wasm_interp_fast.c +98 -0
  125. package/kernel/Sources/CWAMR/wamr_wasm_loader.c +98 -0
  126. package/kernel/Sources/CWAMR/wamr_wasm_loader_common.c +98 -0
  127. package/kernel/Sources/CWAMR/wamr_wasm_memory.c +98 -0
  128. package/kernel/Sources/CWAMR/wamr_wasm_native.c +98 -0
  129. package/kernel/Sources/CWAMR/wamr_wasm_runtime.c +98 -0
  130. package/kernel/Sources/CWAMR/wamr_wasm_runtime_common.c +98 -0
  131. package/kernel/Sources/CWAMR/wamr_wasm_shared_memory.c +98 -0
  132. package/kernel/Sources/Kernel/PluginPrelude.js +5 -2
  133. package/kernel/Tests/KernelTests/CasePins.js +50 -0
  134. package/package.json +2 -2
  135. package/skill/krillplayer-plugin/SKILL.md +2 -0
  136. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-content-rejected.json +8 -4
  137. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-login-expired.json +8 -4
  138. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-muted.json +8 -4
  139. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-other.json +8 -4
  140. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-rate-limited.json +8 -4
  141. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/danmaku-send-sent.json +8 -4
  142. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live-logged-in.json +39 -0
  143. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live-selected-backup-line.json +46 -0
  144. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live-selected-line-gone.json +46 -0
  145. package/skill/krillplayer-plugin/assets/samples/live-video-platform/cases/play-live.json +21 -1
  146. package/skill/krillplayer-plugin/assets/samples/live-video-platform/plugin.ts +39 -4
  147. package/skill/krillplayer-plugin/references/cases.md +20 -2
  148. package/skill/krillplayer-plugin/references/contract.d.ts +114 -12
  149. package/skill/krillplayer-plugin/references/crypto-tools.md +104 -0
  150. package/skill/krillplayer-plugin/references/errors-and-fixes.md +5 -1
@@ -0,0 +1,217 @@
1
+ /*
2
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
+ */
5
+
6
+ #include "bh_common.h"
7
+
8
+ static char *
9
+ align_ptr(char *src, unsigned int b)
10
+ {
11
+ uintptr_t v = (uintptr_t)src;
12
+ uintptr_t m = b - 1;
13
+ return (char *)((v + m) & ~m);
14
+ }
15
+
16
+ /*
17
+ Memory copy, with word alignment
18
+ */
19
+ int
20
+ b_memcpy_wa(void *s1, unsigned int s1max, const void *s2, unsigned int n)
21
+ {
22
+ char *dest = (char *)s1;
23
+ char *src = (char *)s2;
24
+
25
+ char *pa = align_ptr(src, 4);
26
+ char *pb = align_ptr((src + n), 4);
27
+
28
+ unsigned int buff;
29
+ const char *p_byte_read;
30
+
31
+ unsigned int *p;
32
+ char *ps;
33
+
34
+ if (n == 0) {
35
+ return 0;
36
+ }
37
+
38
+ if (pa > src) {
39
+ pa -= 4;
40
+ }
41
+
42
+ for (p = (unsigned int *)pa; p < (unsigned int *)pb; p++) {
43
+ buff = *(p);
44
+ p_byte_read = ((char *)&buff);
45
+
46
+ /* read leading word */
47
+ if ((char *)p <= src) {
48
+ for (ps = src; ps < ((char *)p + 4); ps++) {
49
+ if (ps >= src + n) {
50
+ break;
51
+ }
52
+ p_byte_read = ((char *)&buff) + (ps - (char *)p);
53
+ *dest++ = *p_byte_read;
54
+ }
55
+ }
56
+ /* read trailing word */
57
+ else if ((char *)p >= pb - 4) {
58
+ for (ps = (char *)p; ps < src + n; ps++) {
59
+ *dest++ = *p_byte_read++;
60
+ }
61
+ }
62
+ /* read remaining word(s) */
63
+ else {
64
+ if ((char *)p + 4 >= src + n) {
65
+ for (ps = (char *)p; ps < src + n; ps++) {
66
+ *dest++ = *p_byte_read++;
67
+ }
68
+ }
69
+ else {
70
+ *(unsigned int *)dest = buff;
71
+ dest += 4;
72
+ }
73
+ }
74
+ }
75
+
76
+ return 0;
77
+ }
78
+
79
+ int
80
+ b_memcpy_s(void *s1, unsigned int s1max, const void *s2, unsigned int n)
81
+ {
82
+ char *dest = (char *)s1;
83
+ char *src = (char *)s2;
84
+ if (n == 0) {
85
+ return 0;
86
+ }
87
+
88
+ if (s1 == NULL) {
89
+ return -1;
90
+ }
91
+ if (s2 == NULL || n > s1max) {
92
+ memset(dest, 0, s1max);
93
+ return -1;
94
+ }
95
+ memcpy(dest, src, n);
96
+ return 0;
97
+ }
98
+
99
+ int
100
+ b_memmove_s(void *s1, unsigned int s1max, const void *s2, unsigned int n)
101
+ {
102
+ char *dest = (char *)s1;
103
+ char *src = (char *)s2;
104
+ if (n == 0) {
105
+ return 0;
106
+ }
107
+
108
+ if (s1 == NULL) {
109
+ return -1;
110
+ }
111
+ if (s2 == NULL || n > s1max) {
112
+ memset(dest, 0, s1max);
113
+ return -1;
114
+ }
115
+ memmove(dest, src, n);
116
+ return 0;
117
+ }
118
+
119
+ int
120
+ b_strcat_s(char *s1, unsigned int s1max, const char *s2)
121
+ {
122
+ if (NULL == s1 || NULL == s2 || s1max < (strlen(s1) + strlen(s2) + 1)) {
123
+ return -1;
124
+ }
125
+
126
+ memcpy(s1 + strlen(s1), s2, strlen(s2) + 1);
127
+ return 0;
128
+ }
129
+
130
+ int
131
+ b_strcpy_s(char *s1, unsigned int s1max, const char *s2)
132
+ {
133
+ if (NULL == s1 || NULL == s2 || s1max < (strlen(s2) + 1)) {
134
+ return -1;
135
+ }
136
+
137
+ memcpy(s1, s2, strlen(s2) + 1);
138
+ return 0;
139
+ }
140
+
141
+ char *
142
+ bh_strdup(const char *s)
143
+ {
144
+ uint32 size;
145
+ char *s1 = NULL;
146
+
147
+ if (s) {
148
+ size = (uint32)(strlen(s) + 1);
149
+ if ((s1 = BH_MALLOC(size)))
150
+ bh_memcpy_s(s1, size, s, size);
151
+ }
152
+ return s1;
153
+ }
154
+
155
+ char *
156
+ wa_strdup(const char *s)
157
+ {
158
+ uint32 size;
159
+ char *s1 = NULL;
160
+
161
+ if (s) {
162
+ size = (uint32)(strlen(s) + 1);
163
+ if ((s1 = WA_MALLOC(size)))
164
+ bh_memcpy_s(s1, size, s, size);
165
+ }
166
+ return s1;
167
+ }
168
+
169
+ #if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0
170
+ int
171
+ bh_system(const char *cmd)
172
+ {
173
+ int ret;
174
+
175
+ #if !(defined(_WIN32) || defined(_WIN32_))
176
+ ret = system(cmd);
177
+ #else
178
+ ret = _spawnlp(_P_WAIT, "cmd.exe", "/c", cmd, NULL);
179
+ #endif
180
+
181
+ return ret;
182
+ }
183
+
184
+ #if defined(_WIN32) || defined(_WIN32_)
185
+ errno_t
186
+ _mktemp_s(char *nameTemplate, size_t sizeInChars);
187
+ #endif
188
+
189
+ bool
190
+ bh_mkstemp(char *file_name, size_t name_len)
191
+ {
192
+ int fd;
193
+
194
+ #if !(defined(_WIN32) || defined(_WIN32_))
195
+ (void)name_len;
196
+ /* On Linux, it generates a unique temporary filename from template, creates
197
+ * and opens the file, and returns an open file descriptor for the file. */
198
+ if ((fd = mkstemp(file_name)) <= 0) {
199
+ goto fail;
200
+ }
201
+
202
+ /* close and remove temp file */
203
+ close(fd);
204
+ unlink(file_name);
205
+ #else
206
+ /* On Windows, it generates a unique temporary file name but does not create
207
+ * or open the file */
208
+ if (_mktemp_s(file_name, name_len) != 0) {
209
+ goto fail;
210
+ }
211
+ #endif
212
+
213
+ return true;
214
+ fail:
215
+ return false;
216
+ }
217
+ #endif /* End of WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0 */
@@ -0,0 +1,83 @@
1
+ /*
2
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
+ */
5
+
6
+ #ifndef _BH_COMMON_H
7
+ #define _BH_COMMON_H
8
+
9
+ #include "bh_platform.h"
10
+
11
+ #ifdef __cplusplus
12
+ extern "C" {
13
+ #endif
14
+
15
+ #define bh_memcpy_s(dest, dlen, src, slen) \
16
+ do { \
17
+ int _ret = b_memcpy_s(dest, dlen, src, slen); \
18
+ (void)_ret; \
19
+ bh_assert(_ret == 0); \
20
+ } while (0)
21
+
22
+ #define bh_memcpy_wa(dest, dlen, src, slen) \
23
+ do { \
24
+ int _ret = b_memcpy_wa(dest, dlen, src, slen); \
25
+ (void)_ret; \
26
+ bh_assert(_ret == 0); \
27
+ } while (0)
28
+
29
+ #define bh_memmove_s(dest, dlen, src, slen) \
30
+ do { \
31
+ int _ret = b_memmove_s(dest, dlen, src, slen); \
32
+ (void)_ret; \
33
+ bh_assert(_ret == 0); \
34
+ } while (0)
35
+
36
+ #define bh_strcat_s(dest, dlen, src) \
37
+ do { \
38
+ int _ret = b_strcat_s(dest, dlen, src); \
39
+ (void)_ret; \
40
+ bh_assert(_ret == 0); \
41
+ } while (0)
42
+
43
+ #define bh_strcpy_s(dest, dlen, src) \
44
+ do { \
45
+ int _ret = b_strcpy_s(dest, dlen, src); \
46
+ (void)_ret; \
47
+ bh_assert(_ret == 0); \
48
+ } while (0)
49
+
50
+ int
51
+ b_memcpy_s(void *s1, unsigned int s1max, const void *s2, unsigned int n);
52
+ int
53
+ b_memcpy_wa(void *s1, unsigned int s1max, const void *s2, unsigned int n);
54
+ int
55
+ b_memmove_s(void *s1, unsigned int s1max, const void *s2, unsigned int n);
56
+ int
57
+ b_strcat_s(char *s1, unsigned int s1max, const char *s2);
58
+ int
59
+ b_strcpy_s(char *s1, unsigned int s1max, const char *s2);
60
+
61
+ /* strdup with string allocated by BH_MALLOC */
62
+ char *
63
+ bh_strdup(const char *s);
64
+
65
+ /* strdup with string allocated by WA_MALLOC */
66
+ char *
67
+ wa_strdup(const char *s);
68
+
69
+ #if WASM_ENABLE_WAMR_COMPILER != 0 || WASM_ENABLE_JIT != 0
70
+ /* Executes a system command in bash/cmd.exe */
71
+ int
72
+ bh_system(const char *cmd);
73
+
74
+ /* Tests whether can create a temporary file with the given name */
75
+ bool
76
+ bh_mkstemp(char *filename, size_t name_len);
77
+ #endif
78
+
79
+ #ifdef __cplusplus
80
+ }
81
+ #endif
82
+
83
+ #endif
@@ -0,0 +1,339 @@
1
+ /*
2
+ * Copyright (C) 2019 Intel Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
+ */
5
+
6
+ #include "bh_hashmap.h"
7
+
8
+ typedef struct HashMapElem {
9
+ void *key;
10
+ void *value;
11
+ struct HashMapElem *next;
12
+ } HashMapElem;
13
+
14
+ struct HashMap {
15
+ /* size of element array */
16
+ uint32 size;
17
+ /* lock for elements */
18
+ korp_mutex *lock;
19
+ /* hash function of key */
20
+ HashFunc hash_func;
21
+ /* key equal function */
22
+ KeyEqualFunc key_equal_func;
23
+ KeyDestroyFunc key_destroy_func;
24
+ ValueDestroyFunc value_destroy_func;
25
+ HashMapElem *elements[1];
26
+ };
27
+
28
+ HashMap *
29
+ bh_hash_map_create(uint32 size, bool use_lock, HashFunc hash_func,
30
+ KeyEqualFunc key_equal_func, KeyDestroyFunc key_destroy_func,
31
+ ValueDestroyFunc value_destroy_func)
32
+ {
33
+ HashMap *map;
34
+ uint64 total_size;
35
+
36
+ if (size < HASH_MAP_MIN_SIZE)
37
+ size = HASH_MAP_MIN_SIZE;
38
+
39
+ if (size > HASH_MAP_MAX_SIZE) {
40
+ LOG_ERROR("HashMap create failed: size is too large.\n");
41
+ return NULL;
42
+ }
43
+
44
+ if (!hash_func || !key_equal_func) {
45
+ LOG_ERROR("HashMap create failed: hash function or key equal function "
46
+ " is NULL.\n");
47
+ return NULL;
48
+ }
49
+
50
+ total_size = offsetof(HashMap, elements)
51
+ + sizeof(HashMapElem *) * (uint64)size
52
+ + (use_lock ? sizeof(korp_mutex) : 0);
53
+
54
+ /* size <= HASH_MAP_MAX_SIZE, so total_size won't be larger than
55
+ UINT32_MAX, no need to check integer overflow */
56
+ if (!(map = BH_MALLOC((uint32)total_size))) {
57
+ LOG_ERROR("HashMap create failed: alloc memory failed.\n");
58
+ return NULL;
59
+ }
60
+
61
+ memset(map, 0, (uint32)total_size);
62
+
63
+ if (use_lock) {
64
+ map->lock = (korp_mutex *)((uint8 *)map + offsetof(HashMap, elements)
65
+ + sizeof(HashMapElem *) * size);
66
+ if (os_mutex_init(map->lock)) {
67
+ LOG_ERROR("HashMap create failed: init map lock failed.\n");
68
+ BH_FREE(map);
69
+ return NULL;
70
+ }
71
+ }
72
+
73
+ map->size = size;
74
+ map->hash_func = hash_func;
75
+ map->key_equal_func = key_equal_func;
76
+ map->key_destroy_func = key_destroy_func;
77
+ map->value_destroy_func = value_destroy_func;
78
+ return map;
79
+ }
80
+
81
+ bool
82
+ bh_hash_map_insert(HashMap *map, void *key, void *value)
83
+ {
84
+ uint32 index;
85
+ HashMapElem *elem;
86
+
87
+ if (!map || !key) {
88
+ LOG_ERROR("HashMap insert elem failed: map or key is NULL.\n");
89
+ return false;
90
+ }
91
+
92
+ if (map->lock) {
93
+ os_mutex_lock(map->lock);
94
+ }
95
+
96
+ index = map->hash_func(key) % map->size;
97
+ elem = map->elements[index];
98
+ while (elem) {
99
+ if (map->key_equal_func(elem->key, key)) {
100
+ LOG_ERROR("HashMap insert elem failed: duplicated key found.\n");
101
+ goto fail;
102
+ }
103
+ elem = elem->next;
104
+ }
105
+
106
+ if (!(elem = BH_MALLOC(sizeof(HashMapElem)))) {
107
+ LOG_ERROR("HashMap insert elem failed: alloc memory failed.\n");
108
+ goto fail;
109
+ }
110
+
111
+ elem->key = key;
112
+ elem->value = value;
113
+ elem->next = map->elements[index];
114
+ map->elements[index] = elem;
115
+
116
+ if (map->lock) {
117
+ os_mutex_unlock(map->lock);
118
+ }
119
+ return true;
120
+
121
+ fail:
122
+ if (map->lock) {
123
+ os_mutex_unlock(map->lock);
124
+ }
125
+ return false;
126
+ }
127
+
128
+ void *
129
+ bh_hash_map_find(HashMap *map, void *key)
130
+ {
131
+ uint32 index;
132
+ HashMapElem *elem;
133
+ void *value;
134
+
135
+ if (!map || !key) {
136
+ LOG_ERROR("HashMap find elem failed: map or key is NULL.\n");
137
+ return NULL;
138
+ }
139
+
140
+ if (map->lock) {
141
+ os_mutex_lock(map->lock);
142
+ }
143
+
144
+ index = map->hash_func(key) % map->size;
145
+ elem = map->elements[index];
146
+
147
+ while (elem) {
148
+ if (map->key_equal_func(elem->key, key)) {
149
+ value = elem->value;
150
+ if (map->lock) {
151
+ os_mutex_unlock(map->lock);
152
+ }
153
+ return value;
154
+ }
155
+ elem = elem->next;
156
+ }
157
+
158
+ if (map->lock) {
159
+ os_mutex_unlock(map->lock);
160
+ }
161
+ return NULL;
162
+ }
163
+
164
+ bool
165
+ bh_hash_map_update(HashMap *map, void *key, void *value, void **p_old_value)
166
+ {
167
+ uint32 index;
168
+ HashMapElem *elem;
169
+
170
+ if (!map || !key) {
171
+ LOG_ERROR("HashMap update elem failed: map or key is NULL.\n");
172
+ return false;
173
+ }
174
+
175
+ if (map->lock) {
176
+ os_mutex_lock(map->lock);
177
+ }
178
+
179
+ index = map->hash_func(key) % map->size;
180
+ elem = map->elements[index];
181
+
182
+ while (elem) {
183
+ if (map->key_equal_func(elem->key, key)) {
184
+ if (p_old_value)
185
+ *p_old_value = elem->value;
186
+ elem->value = value;
187
+ if (map->lock) {
188
+ os_mutex_unlock(map->lock);
189
+ }
190
+ return true;
191
+ }
192
+ elem = elem->next;
193
+ }
194
+
195
+ if (map->lock) {
196
+ os_mutex_unlock(map->lock);
197
+ }
198
+ return false;
199
+ }
200
+
201
+ bool
202
+ bh_hash_map_remove(HashMap *map, void *key, void **p_old_key,
203
+ void **p_old_value)
204
+ {
205
+ uint32 index;
206
+ HashMapElem *elem, *prev;
207
+
208
+ if (!map || !key) {
209
+ LOG_ERROR("HashMap remove elem failed: map or key is NULL.\n");
210
+ return false;
211
+ }
212
+
213
+ if (map->lock) {
214
+ os_mutex_lock(map->lock);
215
+ }
216
+
217
+ index = map->hash_func(key) % map->size;
218
+ prev = elem = map->elements[index];
219
+
220
+ while (elem) {
221
+ if (map->key_equal_func(elem->key, key)) {
222
+ if (p_old_key)
223
+ *p_old_key = elem->key;
224
+ if (p_old_value)
225
+ *p_old_value = elem->value;
226
+
227
+ if (elem == map->elements[index])
228
+ map->elements[index] = elem->next;
229
+ else
230
+ prev->next = elem->next;
231
+
232
+ BH_FREE(elem);
233
+
234
+ if (map->lock) {
235
+ os_mutex_unlock(map->lock);
236
+ }
237
+ return true;
238
+ }
239
+
240
+ prev = elem;
241
+ elem = elem->next;
242
+ }
243
+
244
+ if (map->lock) {
245
+ os_mutex_unlock(map->lock);
246
+ }
247
+ return false;
248
+ }
249
+
250
+ bool
251
+ bh_hash_map_destroy(HashMap *map)
252
+ {
253
+ uint32 index;
254
+ HashMapElem *elem, *next;
255
+
256
+ if (!map) {
257
+ LOG_ERROR("HashMap destroy failed: map is NULL.\n");
258
+ return false;
259
+ }
260
+
261
+ if (map->lock) {
262
+ os_mutex_lock(map->lock);
263
+ }
264
+
265
+ for (index = 0; index < map->size; index++) {
266
+ elem = map->elements[index];
267
+ while (elem) {
268
+ next = elem->next;
269
+
270
+ if (map->key_destroy_func) {
271
+ map->key_destroy_func(elem->key);
272
+ }
273
+ if (map->value_destroy_func) {
274
+ map->value_destroy_func(elem->value);
275
+ }
276
+ BH_FREE(elem);
277
+
278
+ elem = next;
279
+ }
280
+ }
281
+
282
+ if (map->lock) {
283
+ os_mutex_unlock(map->lock);
284
+ os_mutex_destroy(map->lock);
285
+ }
286
+ BH_FREE(map);
287
+ return true;
288
+ }
289
+
290
+ uint32
291
+ bh_hash_map_get_struct_size(HashMap *hashmap)
292
+ {
293
+ uint32 size = (uint32)(uintptr_t)offsetof(HashMap, elements)
294
+ + (uint32)sizeof(HashMapElem *) * hashmap->size;
295
+
296
+ if (hashmap->lock) {
297
+ size += (uint32)sizeof(korp_mutex);
298
+ }
299
+
300
+ return size;
301
+ }
302
+
303
+ uint32
304
+ bh_hash_map_get_elem_struct_size()
305
+ {
306
+ return (uint32)sizeof(HashMapElem);
307
+ }
308
+
309
+ bool
310
+ bh_hash_map_traverse(HashMap *map, TraverseCallbackFunc callback,
311
+ void *user_data)
312
+ {
313
+ uint32 index;
314
+ HashMapElem *elem, *next;
315
+
316
+ if (!map || !callback) {
317
+ LOG_ERROR("HashMap traverse failed: map or callback is NULL.\n");
318
+ return false;
319
+ }
320
+
321
+ if (map->lock) {
322
+ os_mutex_lock(map->lock);
323
+ }
324
+
325
+ for (index = 0; index < map->size; index++) {
326
+ elem = map->elements[index];
327
+ while (elem) {
328
+ next = elem->next;
329
+ callback(elem->key, elem->value, user_data);
330
+ elem = next;
331
+ }
332
+ }
333
+
334
+ if (map->lock) {
335
+ os_mutex_unlock(map->lock);
336
+ }
337
+
338
+ return true;
339
+ }