emnapi 0.41.0 → 0.43.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.
package/include/emnapi.h CHANGED
@@ -1,7 +1,8 @@
1
1
  #ifndef EMNAPI_INCLUDE_EMNAPI_H_
2
2
  #define EMNAPI_INCLUDE_EMNAPI_H_
3
3
 
4
- #include "js_native_api.h"
4
+ #include <stdbool.h>
5
+ #include "js_native_api_types.h"
5
6
  #include "emnapi_common.h"
6
7
 
7
8
  typedef enum {
@@ -523,6 +523,16 @@ NAPI_EXTERN napi_status NAPI_CDECL napi_add_finalizer(napi_env env,
523
523
 
524
524
  #endif // NAPI_VERSION >= 5
525
525
 
526
+ #ifdef NAPI_EXPERIMENTAL
527
+
528
+ NAPI_EXTERN napi_status NAPI_CDECL
529
+ node_api_post_finalizer(napi_env env,
530
+ napi_finalize finalize_cb,
531
+ void* finalize_data,
532
+ void* finalize_hint);
533
+
534
+ #endif // NAPI_EXPERIMENTAL
535
+
526
536
  #if NAPI_VERSION >= 6
527
537
 
528
538
  // BigInt
@@ -40,7 +40,7 @@ struct uv_loop_s; // Forward declaration.
40
40
 
41
41
  typedef napi_value(NAPI_CDECL* napi_addon_register_func)(napi_env env,
42
42
  napi_value exports);
43
- typedef int32_t(NAPI_CDECL* node_api_addon_get_api_version_func)();
43
+ typedef int32_t(NAPI_CDECL* node_api_addon_get_api_version_func)(void);
44
44
 
45
45
  // Used by deprecated registration method napi_module_register.
46
46
  typedef struct napi_module {
@@ -84,7 +84,7 @@ typedef struct napi_module {
84
84
 
85
85
  #define NAPI_MODULE_INIT() \
86
86
  EXTERN_C_START \
87
- NAPI_MODULE_EXPORT int32_t NODE_API_MODULE_GET_API_VERSION() { \
87
+ NAPI_MODULE_EXPORT int32_t NODE_API_MODULE_GET_API_VERSION(void) { \
88
88
  return NAPI_VERSION; \
89
89
  } \
90
90
  NAPI_MODULE_EXPORT napi_value NAPI_MODULE_INITIALIZER(napi_env env, \
package/include/uv.h CHANGED
@@ -84,6 +84,23 @@ typedef void (*uv_thread_cb)(void* arg);
84
84
  UV_EXTERN int uv_thread_create(uv_thread_t* tid, uv_thread_cb entry, void* arg);
85
85
  UV_EXTERN int uv_thread_join(uv_thread_t *tid);
86
86
 
87
+ typedef enum {
88
+ UV_THREAD_NO_FLAGS = 0x00,
89
+ UV_THREAD_HAS_STACK_SIZE = 0x01
90
+ } uv_thread_create_flags;
91
+
92
+ struct uv_thread_options_s {
93
+ unsigned int flags;
94
+ size_t stack_size;
95
+ };
96
+
97
+ typedef struct uv_thread_options_s uv_thread_options_t;
98
+
99
+ UV_EXTERN int uv_thread_create_ex(uv_thread_t* tid,
100
+ const uv_thread_options_t* params,
101
+ uv_thread_cb entry,
102
+ void* arg);
103
+
87
104
  typedef void (*uv_close_cb)(uv_handle_t* handle);
88
105
  typedef void (*uv_async_cb)(uv_async_t* handle);
89
106
 
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "emnapi",
3
- "version": "0.41.0",
3
+ "version": "0.43.0",
4
4
  "description": "Node-API implementation for Emscripten",
5
5
  "main": "index.js",
6
6
  "peerDependencies": {
@@ -1,5 +1,5 @@
1
- #include "emnapi_internal.h"
2
1
  #include "node_api.h"
2
+ #include "emnapi_internal.h"
3
3
 
4
4
  #if NAPI_VERSION >= 8
5
5
 
@@ -21,6 +21,7 @@ napi_async_init(napi_env env,
21
21
  napi_value async_resource_name,
22
22
  napi_async_context* result) {
23
23
  CHECK_ENV(env);
24
+ _emnapi_env_check_gc_access(env);
24
25
  CHECK_ARG(env, async_resource_name);
25
26
  CHECK_ARG(env, result);
26
27
 
@@ -39,6 +40,7 @@ napi_async_init(napi_env env,
39
40
  napi_status napi_async_destroy(napi_env env,
40
41
  napi_async_context async_context) {
41
42
  CHECK_ENV(env);
43
+ _emnapi_env_check_gc_access(env);
42
44
  CHECK_ARG(env, async_context);
43
45
 
44
46
  napi_status status = _emnapi_async_destroy_js(async_context);
package/src/async_work.c CHANGED
@@ -149,6 +149,7 @@ napi_status napi_create_async_work(napi_env env,
149
149
  napi_async_work* result) {
150
150
  #if EMNAPI_HAVE_THREADS
151
151
  CHECK_ENV(env);
152
+ _emnapi_env_check_gc_access(env);
152
153
  CHECK_ARG(env, execute);
153
154
  CHECK_ARG(env, result);
154
155
 
@@ -188,6 +189,7 @@ napi_status napi_create_async_work(napi_env env,
188
189
  napi_status napi_delete_async_work(napi_env env, napi_async_work work) {
189
190
  #if EMNAPI_HAVE_THREADS
190
191
  CHECK_ENV(env);
192
+ _emnapi_env_check_gc_access(env);
191
193
  CHECK_ARG(env, work);
192
194
 
193
195
  async_work_delete(work);
@@ -156,6 +156,9 @@ napi_status _emnapi_node_make_callback(napi_env env,
156
156
  double trigger_async_id,
157
157
  napi_value* result);
158
158
 
159
+ EMNAPI_INTERNAL_EXTERN
160
+ void _emnapi_env_check_gc_access(napi_env env);
161
+
159
162
  #define EMNAPI_ASYNC_RESOURCE_CTOR(env, res, name, ar) \
160
163
  do { \
161
164
  EMNAPI_ASSERT_CALL(napi_create_reference((env), (res), 1, &(ar)->resource_)); \
@@ -4619,14 +4619,14 @@ void* dlmalloc(size_t bytes) {
4619
4619
  ensure_initialization(); /* initialize in sys_alloc if not using locks */
4620
4620
  #endif
4621
4621
 
4622
+ if (!PREACTION(gm)) {
4622
4623
  #if __wasilibc_unmodified_upstream // Try to initialize the allocator.
4623
4624
  #else
4624
- if (!is_initialized(gm)) {
4625
- try_init_allocator();
4626
- }
4625
+ if (!is_initialized(gm)) {
4626
+ try_init_allocator();
4627
+ }
4627
4628
  #endif
4628
4629
 
4629
- if (!PREACTION(gm)) {
4630
4630
  void* mem;
4631
4631
  size_t nb;
4632
4632
  if (bytes <= MAX_SMALL_REQUEST) {
@@ -5241,7 +5241,7 @@ static void internal_inspect_all(mstate m,
5241
5241
 
5242
5242
  /* Symbol marking the end of data, bss and explicit stack, provided by wasm-ld. */
5243
5243
  extern char __heap_base;
5244
- extern char __heap_end __attribute__((__weak__));
5244
+ extern char __heap_end;
5245
5245
 
5246
5246
  /* Initialize the initial state of dlmalloc to be able to use free memory between __heap_base and initial. */
5247
5247
  static void try_init_allocator(void) {
@@ -5253,23 +5253,18 @@ static void try_init_allocator(void) {
5253
5253
 
5254
5254
  char *base = &__heap_base;
5255
5255
  // Try to use the linker pseudo-symbol `__heap_end` for the initial size of
5256
- // the heap, but if that's not defined due to LLVM being too old perhaps then
5257
- // round up `base` to the nearest `PAGESIZE`. The initial size of linear
5258
- // memory will be at least the heap base to this page boundary, and it's then
5259
- // assumed that the initial linear memory image was truncated at that point.
5260
- // While this reflects the default behavior of `wasm-ld` it is also possible
5261
- // for users to craft larger linear memories by passing options to extend
5262
- // beyond this threshold. In this situation the memory will not be used for
5263
- // dlmalloc.
5264
- //
5265
- // Note that `sbrk(0)`, or in dlmalloc-ese `CALL_MORECORE(0)`, is specifically
5266
- // not used here. That captures the current size of the heap but is only
5267
- // correct if the we're the first to try to grow the heap. If the heap has
5268
- // grown elsewhere, such as a different allocator in place, then this would
5269
- // incorrectly claim such memroy as our own.
5256
+ // the heap.
5270
5257
  char *end = &__heap_end;
5271
- if (end == NULL)
5272
- end = (char*) page_align((size_t) base);
5258
+ if (end < base) {
5259
+ // "end" can be NULL when 1. you are using an old wasm-ld which doesn't
5260
+ // provide `__heap_end` (< 15.0.7) and 2. something (other libraries
5261
+ // or maybe your app?) includes a weak reference to `__heap_end` and
5262
+ // 3. the weak reference is found by the linker before this strong
5263
+ // reference.
5264
+ //
5265
+ // Note: This is a linker bug: https://github.com/llvm/llvm-project/issues/60829
5266
+ __builtin_trap();
5267
+ }
5273
5268
  size_t initial_heap_size = end - base;
5274
5269
 
5275
5270
  /* Check that initial heap is long enough to serve a minimal allocation request. */
package/src/node_api.c CHANGED
@@ -1,5 +1,5 @@
1
- #include "emnapi_internal.h"
2
1
  #include "node_api.h"
2
+ #include "emnapi_internal.h"
3
3
 
4
4
  #if EMNAPI_HAVE_THREADS
5
5
  #include "uv.h"
@@ -409,6 +409,7 @@ napi_create_threadsafe_function(napi_env env,
409
409
  napi_threadsafe_function* result) {
410
410
  #if EMNAPI_HAVE_THREADS
411
411
  CHECK_ENV(env);
412
+ _emnapi_env_check_gc_access(env);
412
413
  CHECK_ARG(env, async_resource_name);
413
414
  RETURN_STATUS_IF_FALSE(env, initial_thread_count > 0, napi_invalid_arg);
414
415
  CHECK_ARG(env, result);
@@ -209,9 +209,10 @@ void uv__threadpool_cleanup(void) {
209
209
  nthreads = 0;
210
210
  }
211
211
 
212
- EMNAPI_EXTERN int _emnapi_async_work_pool_size();
212
+ EMNAPI_INTERNAL_EXTERN int _emnapi_async_work_pool_size();
213
213
 
214
214
  static void init_threads(void) {
215
+ uv_thread_options_t config;
215
216
  unsigned int i;
216
217
  #if !defined(EMNAPI_WORKER_POOL_SIZE) || !(EMNAPI_WORKER_POOL_SIZE > 0)
217
218
  const char* val;
@@ -262,11 +263,17 @@ static void init_threads(void) {
262
263
  abort();
263
264
  #endif
264
265
 
266
+ // config.flags = UV_THREAD_HAS_STACK_SIZE;
267
+ // config.stack_size = 8u << 20; /* 8 MB */
268
+
269
+ // use DEFAULT_PTHREAD_STACK_SIZE
270
+ config.flags = UV_THREAD_NO_FLAGS;
271
+
265
272
  for (i = 0; i < nthreads; i++)
266
273
  #ifndef __EMNAPI_WASI_THREADS__
267
- if (uv_thread_create(threads + i, (uv_thread_cb) worker, &sem))
274
+ if (uv_thread_create_ex(threads + i, &config, (uv_thread_cb) worker, &sem))
268
275
  #else
269
- if (uv_thread_create(threads + i, (uv_thread_cb) worker, NULL))
276
+ if (uv_thread_create_ex(threads + i, &config, (uv_thread_cb) worker, NULL))
270
277
  #endif
271
278
  abort();
272
279
 
@@ -15,6 +15,31 @@
15
15
  // } \
16
16
  // } while (0)
17
17
 
18
+ static size_t uv__min_stack_size(void) {
19
+ static const size_t min = 8192;
20
+
21
+ #ifdef PTHREAD_STACK_MIN /* Not defined on NetBSD. */
22
+ if (min < (size_t) PTHREAD_STACK_MIN)
23
+ return PTHREAD_STACK_MIN;
24
+ #endif /* PTHREAD_STACK_MIN */
25
+
26
+ return min;
27
+ }
28
+
29
+ static size_t uv__default_stack_size(void) {
30
+ #if !defined(__linux__)
31
+ return 0;
32
+ #elif defined(__PPC__) || defined(__ppc__) || defined(__powerpc__)
33
+ return 4 << 20; /* glibc default. */
34
+ #else
35
+ return 2 << 20; /* glibc default. */
36
+ #endif
37
+ }
38
+
39
+ size_t uv__thread_stack_size(void) {
40
+ return uv__default_stack_size();
41
+ }
42
+
18
43
  void uv_sem_post(sem_t* sem) {
19
44
  if (sem_post(sem))
20
45
  abort();
@@ -104,10 +129,15 @@ void uv_cond_destroy(uv_cond_t* cond) {
104
129
  }
105
130
 
106
131
  int uv_thread_create_ex(uv_thread_t* tid,
107
- void* params,
132
+ const uv_thread_options_t* params,
108
133
  void (*entry)(void *arg),
109
134
  void *arg) {
110
135
  int err;
136
+ pthread_attr_t* attr;
137
+ pthread_attr_t attr_storage;
138
+ size_t pagesize;
139
+ size_t stack_size;
140
+ size_t min_stack_size;
111
141
 
112
142
  /* Used to squelch a -Wcast-function-type warning. */
113
143
  union {
@@ -115,14 +145,43 @@ int uv_thread_create_ex(uv_thread_t* tid,
115
145
  void* (*out)(void*);
116
146
  } f;
117
147
 
148
+ stack_size =
149
+ params->flags & UV_THREAD_HAS_STACK_SIZE ? params->stack_size : 0;
150
+
151
+ attr = NULL;
152
+ if (stack_size == 0) {
153
+ stack_size = uv__thread_stack_size();
154
+ } else {
155
+ pagesize = 65536;
156
+ stack_size = (stack_size + pagesize - 1) &~ (pagesize - 1);
157
+ min_stack_size = uv__min_stack_size();
158
+ if (stack_size < min_stack_size)
159
+ stack_size = min_stack_size;
160
+ }
161
+
162
+ if (stack_size > 0) {
163
+ attr = &attr_storage;
164
+
165
+ if (pthread_attr_init(attr))
166
+ abort();
167
+
168
+ if (pthread_attr_setstacksize(attr, stack_size))
169
+ abort();
170
+ }
171
+
118
172
  f.in = entry;
119
- err = pthread_create(tid, NULL, f.out, arg);
173
+ err = pthread_create(tid, attr, f.out, arg);
174
+
175
+ if (attr != NULL)
176
+ pthread_attr_destroy(attr);
120
177
 
121
178
  return err;
122
179
  }
123
180
 
124
181
  int uv_thread_create(uv_thread_t *tid, void (*entry)(void *arg), void *arg) {
125
- return uv_thread_create_ex(tid, NULL, entry, arg);
182
+ uv_thread_options_t params;
183
+ params.flags = UV_THREAD_NO_FLAGS;
184
+ return uv_thread_create_ex(tid, &params, entry, arg);
126
185
  }
127
186
 
128
187
  int uv_thread_join(uv_thread_t *tid) {