emnapi 0.31.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/CMakeLists.txt +160 -0
- package/LICENSE +21 -0
- package/README.md +875 -0
- package/cmake/wasm32.cmake +32 -0
- package/dist/library_napi.js +4784 -0
- package/include/common.h +26 -0
- package/include/emnapi.h +81 -0
- package/include/js_native_api.h +560 -0
- package/include/js_native_api_types.h +159 -0
- package/include/napi-inl.deprecated.h +186 -0
- package/include/napi-inl.h +6299 -0
- package/include/napi.h +3127 -0
- package/include/node_api.h +226 -0
- package/include/node_api_types.h +56 -0
- package/include/uv/threadpool.h +41 -0
- package/include/uv/unix.h +21 -0
- package/include/uv.h +134 -0
- package/index.d.ts +4 -0
- package/index.js +22 -0
- package/lib/wasm32/libdlmalloc.a +0 -0
- package/lib/wasm32/libemmalloc.a +0 -0
- package/lib/wasm32/libemnapi.a +0 -0
- package/lib/wasm32-emscripten/libemnapi-mt.a +0 -0
- package/lib/wasm32-emscripten/libemnapi.a +0 -0
- package/lib/wasm32-emscripten.txt +5 -0
- package/lib/wasm32-wasi/libemnapi.a +0 -0
- package/lib/wasm32-wasi.txt +4 -0
- package/lib/wasm32.txt +4 -0
- package/package.json +43 -0
- package/src/emnapi.c +1344 -0
- package/src/malloc/dlmalloc/dlmalloc.c +92 -0
- package/src/malloc/dlmalloc/malloc.c +6395 -0
- package/src/malloc/emmalloc/emmalloc.c +1551 -0
- package/src/malloc/memcpy.c +136 -0
- package/src/malloc/memset.c +98 -0
- package/src/malloc/sbrk.c +29 -0
- package/src/uv/queue.h +108 -0
- package/src/uv/threadpool.c +408 -0
- package/src/uv/unix/async.c +206 -0
- package/src/uv/unix/core.c +35 -0
- package/src/uv/unix/loop.c +36 -0
- package/src/uv/unix/thread.c +118 -0
- package/src/uv/uv-common.c +51 -0
- package/src/uv/uv-common.h +68 -0
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
#include <stddef.h>
|
|
2
|
+
#include <stdint.h>
|
|
3
|
+
|
|
4
|
+
#ifdef __GNUC__
|
|
5
|
+
#define __BYTE_ORDER __BYTE_ORDER__
|
|
6
|
+
#define __LITTLE_ENDIAN 1234
|
|
7
|
+
#endif
|
|
8
|
+
|
|
9
|
+
#ifndef BULK_MEMORY_THRESHOLD
|
|
10
|
+
#define BULK_MEMORY_THRESHOLD 32
|
|
11
|
+
#endif
|
|
12
|
+
|
|
13
|
+
void *memcpy(void *restrict dest, const void *restrict src, size_t n)
|
|
14
|
+
{
|
|
15
|
+
#if defined(__wasm_bulk_memory__)
|
|
16
|
+
if (n > BULK_MEMORY_THRESHOLD)
|
|
17
|
+
return __builtin_memcpy(dest, src, n);
|
|
18
|
+
#endif
|
|
19
|
+
unsigned char *d = dest;
|
|
20
|
+
const unsigned char *s = src;
|
|
21
|
+
|
|
22
|
+
#ifdef __GNUC__
|
|
23
|
+
|
|
24
|
+
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
|
25
|
+
#define LS >>
|
|
26
|
+
#define RS <<
|
|
27
|
+
#else
|
|
28
|
+
#define LS <<
|
|
29
|
+
#define RS >>
|
|
30
|
+
#endif
|
|
31
|
+
|
|
32
|
+
typedef uint32_t __attribute__((__may_alias__)) u32;
|
|
33
|
+
uint32_t w, x;
|
|
34
|
+
|
|
35
|
+
for (; (uintptr_t)s % 4 && n; n--) *d++ = *s++;
|
|
36
|
+
|
|
37
|
+
if ((uintptr_t)d % 4 == 0) {
|
|
38
|
+
for (; n>=16; s+=16, d+=16, n-=16) {
|
|
39
|
+
*(u32 *)(d+0) = *(u32 *)(s+0);
|
|
40
|
+
*(u32 *)(d+4) = *(u32 *)(s+4);
|
|
41
|
+
*(u32 *)(d+8) = *(u32 *)(s+8);
|
|
42
|
+
*(u32 *)(d+12) = *(u32 *)(s+12);
|
|
43
|
+
}
|
|
44
|
+
if (n&8) {
|
|
45
|
+
*(u32 *)(d+0) = *(u32 *)(s+0);
|
|
46
|
+
*(u32 *)(d+4) = *(u32 *)(s+4);
|
|
47
|
+
d += 8; s += 8;
|
|
48
|
+
}
|
|
49
|
+
if (n&4) {
|
|
50
|
+
*(u32 *)(d+0) = *(u32 *)(s+0);
|
|
51
|
+
d += 4; s += 4;
|
|
52
|
+
}
|
|
53
|
+
if (n&2) {
|
|
54
|
+
*d++ = *s++; *d++ = *s++;
|
|
55
|
+
}
|
|
56
|
+
if (n&1) {
|
|
57
|
+
*d = *s;
|
|
58
|
+
}
|
|
59
|
+
return dest;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (n >= 32) switch ((uintptr_t)d % 4) {
|
|
63
|
+
case 1:
|
|
64
|
+
w = *(u32 *)s;
|
|
65
|
+
*d++ = *s++;
|
|
66
|
+
*d++ = *s++;
|
|
67
|
+
*d++ = *s++;
|
|
68
|
+
n -= 3;
|
|
69
|
+
for (; n>=17; s+=16, d+=16, n-=16) {
|
|
70
|
+
x = *(u32 *)(s+1);
|
|
71
|
+
*(u32 *)(d+0) = (w LS 24) | (x RS 8);
|
|
72
|
+
w = *(u32 *)(s+5);
|
|
73
|
+
*(u32 *)(d+4) = (x LS 24) | (w RS 8);
|
|
74
|
+
x = *(u32 *)(s+9);
|
|
75
|
+
*(u32 *)(d+8) = (w LS 24) | (x RS 8);
|
|
76
|
+
w = *(u32 *)(s+13);
|
|
77
|
+
*(u32 *)(d+12) = (x LS 24) | (w RS 8);
|
|
78
|
+
}
|
|
79
|
+
break;
|
|
80
|
+
case 2:
|
|
81
|
+
w = *(u32 *)s;
|
|
82
|
+
*d++ = *s++;
|
|
83
|
+
*d++ = *s++;
|
|
84
|
+
n -= 2;
|
|
85
|
+
for (; n>=18; s+=16, d+=16, n-=16) {
|
|
86
|
+
x = *(u32 *)(s+2);
|
|
87
|
+
*(u32 *)(d+0) = (w LS 16) | (x RS 16);
|
|
88
|
+
w = *(u32 *)(s+6);
|
|
89
|
+
*(u32 *)(d+4) = (x LS 16) | (w RS 16);
|
|
90
|
+
x = *(u32 *)(s+10);
|
|
91
|
+
*(u32 *)(d+8) = (w LS 16) | (x RS 16);
|
|
92
|
+
w = *(u32 *)(s+14);
|
|
93
|
+
*(u32 *)(d+12) = (x LS 16) | (w RS 16);
|
|
94
|
+
}
|
|
95
|
+
break;
|
|
96
|
+
case 3:
|
|
97
|
+
w = *(u32 *)s;
|
|
98
|
+
*d++ = *s++;
|
|
99
|
+
n -= 1;
|
|
100
|
+
for (; n>=19; s+=16, d+=16, n-=16) {
|
|
101
|
+
x = *(u32 *)(s+3);
|
|
102
|
+
*(u32 *)(d+0) = (w LS 8) | (x RS 24);
|
|
103
|
+
w = *(u32 *)(s+7);
|
|
104
|
+
*(u32 *)(d+4) = (x LS 8) | (w RS 24);
|
|
105
|
+
x = *(u32 *)(s+11);
|
|
106
|
+
*(u32 *)(d+8) = (w LS 8) | (x RS 24);
|
|
107
|
+
w = *(u32 *)(s+15);
|
|
108
|
+
*(u32 *)(d+12) = (x LS 8) | (w RS 24);
|
|
109
|
+
}
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
if (n&16) {
|
|
113
|
+
*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++;
|
|
114
|
+
*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++;
|
|
115
|
+
*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++;
|
|
116
|
+
*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++;
|
|
117
|
+
}
|
|
118
|
+
if (n&8) {
|
|
119
|
+
*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++;
|
|
120
|
+
*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++;
|
|
121
|
+
}
|
|
122
|
+
if (n&4) {
|
|
123
|
+
*d++ = *s++; *d++ = *s++; *d++ = *s++; *d++ = *s++;
|
|
124
|
+
}
|
|
125
|
+
if (n&2) {
|
|
126
|
+
*d++ = *s++; *d++ = *s++;
|
|
127
|
+
}
|
|
128
|
+
if (n&1) {
|
|
129
|
+
*d = *s;
|
|
130
|
+
}
|
|
131
|
+
return dest;
|
|
132
|
+
#endif
|
|
133
|
+
|
|
134
|
+
for (; n; n--) *d++ = *s++;
|
|
135
|
+
return dest;
|
|
136
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
#include <stddef.h>
|
|
2
|
+
#include <stdint.h>
|
|
3
|
+
|
|
4
|
+
#ifndef BULK_MEMORY_THRESHOLD
|
|
5
|
+
#define BULK_MEMORY_THRESHOLD 32
|
|
6
|
+
#endif
|
|
7
|
+
|
|
8
|
+
void *memset(void *dest, int c, size_t n)
|
|
9
|
+
{
|
|
10
|
+
#if defined(__wasm_bulk_memory__)
|
|
11
|
+
if (n > BULK_MEMORY_THRESHOLD)
|
|
12
|
+
return __builtin_memset(dest, c, n);
|
|
13
|
+
#endif
|
|
14
|
+
unsigned char *s = dest;
|
|
15
|
+
size_t k;
|
|
16
|
+
|
|
17
|
+
/* Fill head and tail with minimal branching. Each
|
|
18
|
+
* conditional ensures that all the subsequently used
|
|
19
|
+
* offsets are well-defined and in the dest region. */
|
|
20
|
+
|
|
21
|
+
if (!n) return dest;
|
|
22
|
+
s[0] = c;
|
|
23
|
+
s[n-1] = c;
|
|
24
|
+
if (n <= 2) return dest;
|
|
25
|
+
s[1] = c;
|
|
26
|
+
s[2] = c;
|
|
27
|
+
s[n-2] = c;
|
|
28
|
+
s[n-3] = c;
|
|
29
|
+
if (n <= 6) return dest;
|
|
30
|
+
s[3] = c;
|
|
31
|
+
s[n-4] = c;
|
|
32
|
+
if (n <= 8) return dest;
|
|
33
|
+
|
|
34
|
+
/* Advance pointer to align it at a 4-byte boundary,
|
|
35
|
+
* and truncate n to a multiple of 4. The previous code
|
|
36
|
+
* already took care of any head/tail that get cut off
|
|
37
|
+
* by the alignment. */
|
|
38
|
+
|
|
39
|
+
k = -(uintptr_t)s & 3;
|
|
40
|
+
s += k;
|
|
41
|
+
n -= k;
|
|
42
|
+
n &= -4;
|
|
43
|
+
|
|
44
|
+
#ifdef __GNUC__
|
|
45
|
+
typedef uint32_t __attribute__((__may_alias__)) u32;
|
|
46
|
+
typedef uint64_t __attribute__((__may_alias__)) u64;
|
|
47
|
+
|
|
48
|
+
u32 c32 = ((u32)-1)/255 * (unsigned char)c;
|
|
49
|
+
|
|
50
|
+
/* In preparation to copy 32 bytes at a time, aligned on
|
|
51
|
+
* an 8-byte bounary, fill head/tail up to 28 bytes each.
|
|
52
|
+
* As in the initial byte-based head/tail fill, each
|
|
53
|
+
* conditional below ensures that the subsequent offsets
|
|
54
|
+
* are valid (e.g. !(n<=24) implies n>=28). */
|
|
55
|
+
|
|
56
|
+
*(u32 *)(s+0) = c32;
|
|
57
|
+
*(u32 *)(s+n-4) = c32;
|
|
58
|
+
if (n <= 8) return dest;
|
|
59
|
+
*(u32 *)(s+4) = c32;
|
|
60
|
+
*(u32 *)(s+8) = c32;
|
|
61
|
+
*(u32 *)(s+n-12) = c32;
|
|
62
|
+
*(u32 *)(s+n-8) = c32;
|
|
63
|
+
if (n <= 24) return dest;
|
|
64
|
+
*(u32 *)(s+12) = c32;
|
|
65
|
+
*(u32 *)(s+16) = c32;
|
|
66
|
+
*(u32 *)(s+20) = c32;
|
|
67
|
+
*(u32 *)(s+24) = c32;
|
|
68
|
+
*(u32 *)(s+n-28) = c32;
|
|
69
|
+
*(u32 *)(s+n-24) = c32;
|
|
70
|
+
*(u32 *)(s+n-20) = c32;
|
|
71
|
+
*(u32 *)(s+n-16) = c32;
|
|
72
|
+
|
|
73
|
+
/* Align to a multiple of 8 so we can fill 64 bits at a time,
|
|
74
|
+
* and avoid writing the same bytes twice as much as is
|
|
75
|
+
* practical without introducing additional branching. */
|
|
76
|
+
|
|
77
|
+
k = 24 + ((uintptr_t)s & 4);
|
|
78
|
+
s += k;
|
|
79
|
+
n -= k;
|
|
80
|
+
|
|
81
|
+
/* If this loop is reached, 28 tail bytes have already been
|
|
82
|
+
* filled, so any remainder when n drops below 32 can be
|
|
83
|
+
* safely ignored. */
|
|
84
|
+
|
|
85
|
+
u64 c64 = c32 | ((u64)c32 << 32);
|
|
86
|
+
for (; n >= 32; n-=32, s+=32) {
|
|
87
|
+
*(u64 *)(s+0) = c64;
|
|
88
|
+
*(u64 *)(s+8) = c64;
|
|
89
|
+
*(u64 *)(s+16) = c64;
|
|
90
|
+
*(u64 *)(s+24) = c64;
|
|
91
|
+
}
|
|
92
|
+
#else
|
|
93
|
+
/* Pure C fallback with no aliasing violations. */
|
|
94
|
+
for (; n; n--, s++) *s = c;
|
|
95
|
+
#endif
|
|
96
|
+
|
|
97
|
+
return dest;
|
|
98
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
#include <stddef.h>
|
|
2
|
+
|
|
3
|
+
#define SIZE_MAX -1
|
|
4
|
+
|
|
5
|
+
void *sbrk(ptrdiff_t increment) {
|
|
6
|
+
// sbrk(0) returns the current memory size.
|
|
7
|
+
if (increment == 0) {
|
|
8
|
+
// The wasm spec doesn't guarantee that memory.grow of 0 always succeeds.
|
|
9
|
+
return (void *)(__builtin_wasm_memory_size(0) * PAGESIZE);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// We only support page-size increments.
|
|
13
|
+
if (increment % PAGESIZE != 0) {
|
|
14
|
+
__builtin_trap();
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// WebAssembly doesn't support shrinking linear memory.
|
|
18
|
+
if (increment < 0) {
|
|
19
|
+
__builtin_trap();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
ptrdiff_t old = __builtin_wasm_memory_grow(0, (ptrdiff_t)increment / PAGESIZE);
|
|
23
|
+
|
|
24
|
+
if (old == SIZE_MAX) {
|
|
25
|
+
return (void *)-1;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return (void *)(old * PAGESIZE);
|
|
29
|
+
}
|
package/src/uv/queue.h
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/* Copyright (c) 2013, Ben Noordhuis <info@bnoordhuis.nl>
|
|
2
|
+
*
|
|
3
|
+
* Permission to use, copy, modify, and/or distribute this software for any
|
|
4
|
+
* purpose with or without fee is hereby granted, provided that the above
|
|
5
|
+
* copyright notice and this permission notice appear in all copies.
|
|
6
|
+
*
|
|
7
|
+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
8
|
+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
9
|
+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
10
|
+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
11
|
+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
12
|
+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
13
|
+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
#ifndef QUEUE_H_
|
|
17
|
+
#define QUEUE_H_
|
|
18
|
+
|
|
19
|
+
#include <stddef.h>
|
|
20
|
+
|
|
21
|
+
typedef void *QUEUE[2];
|
|
22
|
+
|
|
23
|
+
/* Private macros. */
|
|
24
|
+
#define QUEUE_NEXT(q) (*(QUEUE **) &((*(q))[0]))
|
|
25
|
+
#define QUEUE_PREV(q) (*(QUEUE **) &((*(q))[1]))
|
|
26
|
+
#define QUEUE_PREV_NEXT(q) (QUEUE_NEXT(QUEUE_PREV(q)))
|
|
27
|
+
#define QUEUE_NEXT_PREV(q) (QUEUE_PREV(QUEUE_NEXT(q)))
|
|
28
|
+
|
|
29
|
+
/* Public macros. */
|
|
30
|
+
#define QUEUE_DATA(ptr, type, field) \
|
|
31
|
+
((type *) ((char *) (ptr) - offsetof(type, field)))
|
|
32
|
+
|
|
33
|
+
/* Important note: mutating the list while QUEUE_FOREACH is
|
|
34
|
+
* iterating over its elements results in undefined behavior.
|
|
35
|
+
*/
|
|
36
|
+
#define QUEUE_FOREACH(q, h) \
|
|
37
|
+
for ((q) = QUEUE_NEXT(h); (q) != (h); (q) = QUEUE_NEXT(q))
|
|
38
|
+
|
|
39
|
+
#define QUEUE_EMPTY(q) \
|
|
40
|
+
((const QUEUE *) (q) == (const QUEUE *) QUEUE_NEXT(q))
|
|
41
|
+
|
|
42
|
+
#define QUEUE_HEAD(q) \
|
|
43
|
+
(QUEUE_NEXT(q))
|
|
44
|
+
|
|
45
|
+
#define QUEUE_INIT(q) \
|
|
46
|
+
do { \
|
|
47
|
+
QUEUE_NEXT(q) = (q); \
|
|
48
|
+
QUEUE_PREV(q) = (q); \
|
|
49
|
+
} \
|
|
50
|
+
while (0)
|
|
51
|
+
|
|
52
|
+
#define QUEUE_ADD(h, n) \
|
|
53
|
+
do { \
|
|
54
|
+
QUEUE_PREV_NEXT(h) = QUEUE_NEXT(n); \
|
|
55
|
+
QUEUE_NEXT_PREV(n) = QUEUE_PREV(h); \
|
|
56
|
+
QUEUE_PREV(h) = QUEUE_PREV(n); \
|
|
57
|
+
QUEUE_PREV_NEXT(h) = (h); \
|
|
58
|
+
} \
|
|
59
|
+
while (0)
|
|
60
|
+
|
|
61
|
+
#define QUEUE_SPLIT(h, q, n) \
|
|
62
|
+
do { \
|
|
63
|
+
QUEUE_PREV(n) = QUEUE_PREV(h); \
|
|
64
|
+
QUEUE_PREV_NEXT(n) = (n); \
|
|
65
|
+
QUEUE_NEXT(n) = (q); \
|
|
66
|
+
QUEUE_PREV(h) = QUEUE_PREV(q); \
|
|
67
|
+
QUEUE_PREV_NEXT(h) = (h); \
|
|
68
|
+
QUEUE_PREV(q) = (n); \
|
|
69
|
+
} \
|
|
70
|
+
while (0)
|
|
71
|
+
|
|
72
|
+
#define QUEUE_MOVE(h, n) \
|
|
73
|
+
do { \
|
|
74
|
+
if (QUEUE_EMPTY(h)) \
|
|
75
|
+
QUEUE_INIT(n); \
|
|
76
|
+
else { \
|
|
77
|
+
QUEUE* q = QUEUE_HEAD(h); \
|
|
78
|
+
QUEUE_SPLIT(h, q, n); \
|
|
79
|
+
} \
|
|
80
|
+
} \
|
|
81
|
+
while (0)
|
|
82
|
+
|
|
83
|
+
#define QUEUE_INSERT_HEAD(h, q) \
|
|
84
|
+
do { \
|
|
85
|
+
QUEUE_NEXT(q) = QUEUE_NEXT(h); \
|
|
86
|
+
QUEUE_PREV(q) = (h); \
|
|
87
|
+
QUEUE_NEXT_PREV(q) = (q); \
|
|
88
|
+
QUEUE_NEXT(h) = (q); \
|
|
89
|
+
} \
|
|
90
|
+
while (0)
|
|
91
|
+
|
|
92
|
+
#define QUEUE_INSERT_TAIL(h, q) \
|
|
93
|
+
do { \
|
|
94
|
+
QUEUE_NEXT(q) = (h); \
|
|
95
|
+
QUEUE_PREV(q) = QUEUE_PREV(h); \
|
|
96
|
+
QUEUE_PREV_NEXT(q) = (q); \
|
|
97
|
+
QUEUE_PREV(h) = (q); \
|
|
98
|
+
} \
|
|
99
|
+
while (0)
|
|
100
|
+
|
|
101
|
+
#define QUEUE_REMOVE(q) \
|
|
102
|
+
do { \
|
|
103
|
+
QUEUE_PREV_NEXT(q) = QUEUE_NEXT(q); \
|
|
104
|
+
QUEUE_NEXT_PREV(q) = QUEUE_PREV(q); \
|
|
105
|
+
} \
|
|
106
|
+
while (0)
|
|
107
|
+
|
|
108
|
+
#endif /* QUEUE_H_ */
|