Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/src/lib/libaddfunction.js
4150 views
1
/**
2
* @license
3
* Copyright 2020 The Emscripten Authors
4
* SPDX-License-Identifier: MIT
5
*/
6
7
addToLibrary({
8
// This gives correct answers for everything less than 2^{14} = 16384
9
// I hope nobody is contemplating functions with 16384 arguments...
10
$uleb128EncodeWithLen__internal: true,
11
$uleb128EncodeWithLen: (arr) => {
12
const n = arr.length;
13
#if ASSERTIONS
14
assert(n < 16384);
15
#endif
16
// Note: this LEB128 length encoding produces extra byte for n < 128,
17
// but we don't care as it's only used in a temporary representation.
18
return [(n % 128) | 128, n >> 7, ...arr];
19
},
20
#if WASM_JS_TYPES
21
// Converts a signature like 'vii' into a description of the wasm types, like
22
// { parameters: ['i32', 'i32'], results: [] }.
23
$sigToWasmTypes__internal: true,
24
$sigToWasmTypes: (sig) => {
25
#if ASSERTIONS && !WASM_BIGINT
26
assert(!sig.includes('j'), 'i64 not permitted in function signatures when WASM_BIGINT is disabled');
27
#endif
28
var typeNames = {
29
'i': 'i32',
30
'j': 'i64',
31
'f': 'f32',
32
'd': 'f64',
33
'e': 'externref',
34
#if MEMORY64
35
'p': 'i64',
36
#else
37
'p': 'i32',
38
#endif
39
};
40
var type = {
41
parameters: [],
42
results: sig[0] == 'v' ? [] : [typeNames[sig[0]]]
43
};
44
for (var i = 1; i < sig.length; ++i) {
45
#if ASSERTIONS
46
assert(sig[i] in typeNames, 'invalid signature char: ' + sig[i]);
47
#endif
48
type.parameters.push(typeNames[sig[i]]);
49
}
50
return type;
51
},
52
#endif
53
$wasmTypeCodes__internal: true,
54
// Note: using template literal here instead of plain object
55
// because jsify serializes objects w/o quotes and Closure will then
56
// incorrectly mangle the properties.
57
$wasmTypeCodes: `{
58
'i': 0x7f, // i32
59
#if MEMORY64
60
'p': 0x7e, // i64
61
#else
62
'p': 0x7f, // i32
63
#endif
64
'j': 0x7e, // i64
65
'f': 0x7d, // f32
66
'd': 0x7c, // f64
67
'e': 0x6f, // externref
68
}`,
69
70
$generateTypePack__internal: true,
71
$generateTypePack__deps: ['$uleb128EncodeWithLen', '$wasmTypeCodes'],
72
$generateTypePack: (types) => uleb128EncodeWithLen(Array.from(types, (type) => {
73
var code = wasmTypeCodes[type];
74
#if ASSERTIONS
75
assert(code, `invalid signature char: ${type}`);
76
#endif
77
return code;
78
})),
79
80
// Wraps a JS function as a wasm function with a given signature.
81
#if !WASM2JS
82
$convertJsFunctionToWasm__deps: [
83
'$uleb128EncodeWithLen',
84
#if WASM_JS_TYPES
85
'$sigToWasmTypes',
86
#endif
87
'$generateTypePack'
88
],
89
#endif
90
$convertJsFunctionToWasm: (func, sig) => {
91
#if WASM2JS
92
// return func;
93
#else // WASM2JS
94
95
#if ASSERTIONS && !WASM_BIGINT
96
assert(!sig.includes('j'), 'i64 not permitted in function signatures when WASM_BIGINT is disabled');
97
#endif
98
#if WASM_JS_TYPES
99
// If the type reflection proposal is available, use the new
100
// "WebAssembly.Function" constructor.
101
// Otherwise, construct a minimal wasm module importing the JS function and
102
// re-exporting it.
103
if (typeof WebAssembly.Function == "function") {
104
return new WebAssembly.Function(sigToWasmTypes(sig), func);
105
}
106
#endif
107
108
// Rest of the module is static
109
var bytes = Uint8Array.of(
110
0x00, 0x61, 0x73, 0x6d, // magic ("\0asm")
111
0x01, 0x00, 0x00, 0x00, // version: 1
112
0x01, // Type section code
113
// The module is static, with the exception of the type section, which is
114
// generated based on the signature passed in.
115
...uleb128EncodeWithLen([
116
0x01, // count: 1
117
0x60 /* form: func */,
118
// param types
119
...generateTypePack(sig.slice(1)),
120
// return types (for now only supporting [] if `void` and single [T] otherwise)
121
...generateTypePack(sig[0] === 'v' ? '' : sig[0])
122
]),
123
// The rest of the module is static
124
0x02, 0x07, // import section
125
// (import "e" "f" (func 0 (type 0)))
126
0x01, 0x01, 0x65, 0x01, 0x66, 0x00, 0x00,
127
0x07, 0x05, // export section
128
// (export "f" (func 0 (type 0)))
129
0x01, 0x01, 0x66, 0x00, 0x00,
130
);
131
132
// We can compile this wasm module synchronously because it is very small.
133
// This accepts an import (at "e.f"), that it reroutes to an export (at "f")
134
var module = new WebAssembly.Module(bytes);
135
var instance = new WebAssembly.Instance(module, { 'e': { 'f': func } });
136
var wrappedFunc = instance.exports['f'];
137
return wrappedFunc;
138
#endif // WASM2JS
139
},
140
141
$freeTableIndexes: [],
142
143
// Weak map of functions in the table to their indexes, created on first use.
144
$functionsInTableMap: undefined,
145
146
$getEmptyTableSlot__deps: ['$freeTableIndexes', '$wasmTable'],
147
$getEmptyTableSlot: () => {
148
// Reuse a free index if there is one, otherwise grow.
149
if (freeTableIndexes.length) {
150
return freeTableIndexes.pop();
151
}
152
#if ASSERTIONS
153
try {
154
#endif
155
// Grow the table
156
return wasmTable['grow']({{{ toIndexType('1') }}});
157
#if ASSERTIONS
158
} catch (err) {
159
if (!(err instanceof RangeError)) {
160
throw err;
161
}
162
throw 'Unable to grow wasm table. Set ALLOW_TABLE_GROWTH.';
163
}
164
#endif
165
},
166
167
$updateTableMap__deps: ['$getWasmTableEntry'],
168
$updateTableMap: (offset, count) => {
169
if (functionsInTableMap) {
170
for (var i = offset; i < offset + count; i++) {
171
var item = getWasmTableEntry(i);
172
// Ignore null values.
173
if (item) {
174
functionsInTableMap.set(item, i);
175
}
176
}
177
}
178
},
179
180
$getFunctionAddress__deps: ['$updateTableMap', '$functionsInTableMap', '$wasmTable'],
181
$getFunctionAddress: (func) => {
182
// First, create the map if this is the first use.
183
if (!functionsInTableMap) {
184
functionsInTableMap = new WeakMap();
185
updateTableMap(0, {{{ from64Expr('wasmTable.length') }}});
186
}
187
return functionsInTableMap.get(func) || 0;
188
},
189
190
/**
191
* Add a function to the table.
192
* 'sig' parameter is required if the function being added is a JS function.
193
*/
194
$addFunction__docs: '/** @param {string=} sig */',
195
$addFunction__deps: ['$convertJsFunctionToWasm', '$getFunctionAddress',
196
'$functionsInTableMap', '$getEmptyTableSlot',
197
'$setWasmTableEntry',
198
#if ASSERTIONS >= 2
199
'$getWasmTableEntry', '$wasmTable',
200
#endif
201
],
202
203
$addFunction: (func, sig) => {
204
#if ASSERTIONS
205
assert(typeof func != 'undefined');
206
#endif // ASSERTIONS
207
// Check if the function is already in the table, to ensure each function
208
// gets a unique index.
209
var rtn = getFunctionAddress(func);
210
if (rtn) {
211
return rtn;
212
}
213
214
// It's not in the table, add it now.
215
216
#if ASSERTIONS >= 2
217
// Make sure functionsInTableMap is actually up to date, that is, that this
218
// function is not actually in the wasm Table despite not being tracked in
219
// functionsInTableMap.
220
for (var i = 0; i < wasmTable.length; i++) {
221
assert(getWasmTableEntry(i) != func, 'function in Table but not functionsInTableMap');
222
}
223
#endif
224
225
var ret = getEmptyTableSlot();
226
227
// Set the new value.
228
try {
229
// Attempting to call this with JS function will cause of table.set() to fail
230
setWasmTableEntry(ret, func);
231
} catch (err) {
232
if (!(err instanceof TypeError)) {
233
throw err;
234
}
235
#if ASSERTIONS
236
assert(typeof sig != 'undefined', 'Missing signature argument to addFunction: ' + func);
237
#endif
238
var wrapped = convertJsFunctionToWasm(func, sig);
239
setWasmTableEntry(ret, wrapped);
240
}
241
242
functionsInTableMap.set(func, ret);
243
244
return ret;
245
},
246
247
$removeFunction__deps: ['$functionsInTableMap', '$freeTableIndexes',
248
'$getWasmTableEntry', '$setWasmTableEntry'],
249
$removeFunction: (index) => {
250
functionsInTableMap.delete(getWasmTableEntry(index));
251
setWasmTableEntry(index, null);
252
freeTableIndexes.push(index);
253
},
254
});
255
256