forked from emscripten-core/emscripten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.mjs
339 lines (296 loc) · 8.54 KB
/
utility.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/**
* @license
* Copyright 2010 The Emscripten Authors
* SPDX-License-Identifier: MIT
*/
// General JS utilities - things that might be useful in any JS project.
// Nothing specific to Emscripten appears here.
import * as url from 'node:url';
import * as path from 'node:path';
import * as fs from 'node:fs';
import * as vm from 'node:vm';
import assert from 'node:assert';
export function safeQuote(x) {
return x.replace(/"/g, '\\"').replace(/'/g, "\\'");
}
export function dump(item) {
let funcData;
try {
if (typeof item == 'object' && item != null && item.funcData) {
funcData = item.funcData;
item.funcData = null;
}
return '// ' + JSON.stringify(item, null, ' ').replace(/\n/g, '\n// ');
} catch {
const ret = [];
for (const [i, j] of Object.entries(item)) {
if (typeof j == 'string' || typeof j == 'number') {
ret.push(`${i}: ${j}`);
} else {
ret.push(`${i}: [?]`);
}
}
return ret.join(',\n');
} finally {
if (funcData) item.funcData = funcData;
}
}
let warnings = false;
export function warningOccured() {
return warnings;
}
let currentFile = [];
export function pushCurrentFile(f) {
currentFile.push(f);
}
export function popCurrentFile() {
currentFile.pop();
}
function errorPrefix(lineNo) {
if (!currentFile.length) return '';
const filename = currentFile[currentFile.length - 1];
if (lineNo) {
return `${filename}:${lineNo}: `;
} else {
return `${filename}: `;
}
}
export function warn(msg, lineNo) {
warnings = true;
printErr(`warning: ${errorPrefix(lineNo)}${msg}`);
}
const seenWarnings = new Set();
export function warnOnce(msg) {
if (!seenWarnings.has(msg)) {
seenWarnings.add(msg);
warn(msg);
}
}
let abortExecution = false;
export function errorOccured() {
return abortExecution;
}
export function error(msg, lineNo) {
abortExecution = true;
printErr(`error: ${errorPrefix(lineNo)}${msg}`);
}
function range(size) {
return Array.from(Array(size).keys());
}
export function mergeInto(obj, other, options = null) {
if (options) {
// check for unintended symbol redefinition
if (options.noOverride) {
for (const key of Object.keys(other)) {
if (obj.hasOwnProperty(key)) {
error(
`Symbol re-definition in JavaScript library: ${key}. Do not use noOverride if this is intended`,
);
return;
}
}
}
// check if sig is missing for added functions
if (options.checkSig) {
for (const [key, value] of Object.entries(other)) {
if (typeof value === 'function' && !other.hasOwnProperty(key + '__sig')) {
error(`__sig is missing for function: ${key}. Do not use checkSig if this is intended`);
return;
}
}
}
}
if (!options || !options.allowMissing) {
for (const ident of Object.keys(other)) {
if (isDecorator(ident)) {
const index = ident.lastIndexOf('__');
const basename = ident.slice(0, index);
if (!(basename in obj) && !(basename in other)) {
error(`Missing library element '${basename}' for library config '${ident}'`);
}
}
}
}
for (const key of Object.keys(other)) {
if (isDecorator(key)) {
if (key.endsWith('__sig')) {
if (obj.hasOwnProperty(key)) {
const oldsig = obj[key];
const newsig = other[key];
if (oldsig == newsig) {
warn(`signature redefinition for: ${key}`);
} else {
error(`signature redefinition for: ${key}. (old=${oldsig} vs new=${newsig})`);
}
}
}
const index = key.lastIndexOf('__');
const decoratorName = key.slice(index);
const type = typeof other[key];
// Specific type checking for `__deps` which is expected to be an array
// (not just any old `object`)
if (decoratorName === '__deps') {
const deps = other[key];
if (!Array.isArray(deps)) {
error(
`JS library directive ${key}=${deps.toString()} is of type '${type}', but it should be an array`,
);
}
for (let dep of deps) {
if (dep && typeof dep !== 'string' && typeof dep !== 'function') {
error(
`__deps entries must be of type 'string' or 'function' not '${typeof dep}': ${key}`,
);
}
}
} else {
// General type checking for all other decorators
const decoratorTypes = {
__sig: 'string',
__proxy: 'string',
__asm: 'boolean',
__inline: 'boolean',
__postset: ['string', 'function'],
__docs: 'string',
__nothrow: 'boolean',
__noleakcheck: 'boolean',
__internal: 'boolean',
__user: 'boolean',
__async: 'boolean',
__i53abi: 'boolean',
};
const expected = decoratorTypes[decoratorName];
if (type !== expected && !expected.includes(type)) {
error(`Decorator (${key}} has wrong type. Expected '${expected}' not '${type}'`);
}
}
}
}
return Object.assign(obj, other);
}
// Symbols that start with '$' are not exported to the wasm module.
// They are intended to be called exclusively by JS code.
export function isJsOnlySymbol(symbol) {
return symbol[0] == '$';
}
export function isDecorator(ident) {
const suffixes = [
'__sig',
'__proxy',
'__asm',
'__inline',
'__deps',
'__postset',
'__docs',
'__nothrow',
'__noleakcheck',
'__internal',
'__user',
'__async',
'__i53abi',
];
return suffixes.some((suffix) => ident.endsWith(suffix));
}
export function readFile(filename) {
return fs.readFileSync(filename, 'utf8');
}
// Use import.meta.dirname here once we drop support for node v18.
const __dirname = url.fileURLToPath(new URL('.', import.meta.url));
export const srcDir = __dirname;
// Returns an absolute path for a file, resolving it relative to this script
// (i.e. relative to the src/ directory).
export function localFile(filename) {
assert(!path.isAbsolute(filename));
return path.join(srcDir, filename);
}
// Helper function for JS library files that can be used to read files
// relative to the src/ directory.
function read(filename) {
if (!path.isAbsolute(filename)) {
filename = localFile(filename);
}
return readFile(filename);
}
export function printErr(x) {
process.stderr.write(x + '\n');
}
export class Benchmarker {
totals = {};
ids = [];
lastTime = 0;
start(id) {
const now = Date.now();
if (this.ids.length > 0) {
this.totals[this.ids[this.ids.length - 1]] += now - this.lastTime;
}
this.lastTime = now;
this.ids.push(id);
this.totals[id] ||= 0;
}
stop(id) {
const now = Date.now();
assert(id === this.ids[this.ids.length - 1]);
this.totals[id] += now - this.lastTime;
this.lastTime = now;
this.ids.pop();
}
print(text) {
const ids = Object.keys(this.totals);
if (ids.length > 0) {
ids.sort((a, b) => this.totals[b] - this.totals[a]);
printErr(
text + ' times: \n' + ids.map((id) => id + ' : ' + this.totals[id] + ' ms').join('\n'),
);
}
}
}
/**
* Context in which JS library code is evaluated. This is distinct from the
* global scope of the compiler itself which avoids exposing all of the compiler
* internals to user JS library code.
*/
export const compileTimeContext = vm.createContext({
process,
console,
});
/**
* A symbols to the macro context.
* This will makes the symbols available to JS library code at build time.
*/
export function addToCompileTimeContext(object) {
Object.assign(compileTimeContext, object);
}
export function applySettings(obj) {
// Make settings available both in the current / global context
// and also in the macro execution contexted.
Object.assign(globalThis, obj);
addToCompileTimeContext(obj);
}
export function loadSettingsFile(f) {
const settings = {};
vm.runInNewContext(readFile(f), settings, {filename: f});
applySettings(settings);
return settings;
}
export function loadDefaultSettings() {
const rtn = loadSettingsFile(localFile('settings.js'));
Object.assign(rtn, loadSettingsFile(localFile('settings_internal.js')));
return rtn;
}
export function runInMacroContext(code, options) {
compileTimeContext['__filename'] = options.filename;
compileTimeContext['__dirname'] = path.dirname(options.filename);
return vm.runInContext(code, compileTimeContext, options);
}
addToCompileTimeContext({
assert,
error,
isDecorator,
isJsOnlySymbol,
mergeInto,
read,
warn,
warnOnce,
printErr,
range,
});