Skip to content

Commit fbebe2c

Browse files
authored
Add missing runtime exports (#6007)
The recent runtime refactoring did not add support for exporting the methods that were moved out of Runtime.. This fixes that, and refactors that exporting code to a single convenient location. It also adds a warning if an invalid item is asked to be exported.
1 parent 56b02ec commit fbebe2c

File tree

8 files changed

+133
-89
lines changed

8 files changed

+133
-89
lines changed

src/arrayUtils.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// All functions here should be maybeExported from jsifier.js
2-
31
/** @type {function(string, boolean=, number=)} */
42
function intArrayFromString(stringy, dontAddNull, length) {
53
var len = length > 0 ? length : lengthBytesUTF8(stringy)+1;

src/base64Utils.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// All functions here should be maybeExported from jsifier.js
2-
31
// Copied from https://github.com/strophe/strophejs/blob/e06d027/src/polyfills.js#L149
42

53
// This code was written by Tyler Akins and has been placed in the

src/jsifier.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -541,15 +541,9 @@ function JSify(data, functionsOnly) {
541541
print('var ASSERTIONS = ' + !!ASSERTIONS + ';\n');
542542

543543
print(preprocess(read('arrayUtils.js')));
544-
// Export all arrayUtils.js functions
545-
print(maybeExport('intArrayFromString'));
546-
print(maybeExport('intArrayToString'));
547544

548545
if (SUPPORT_BASE64_EMBEDDING) {
549546
print(preprocess(read('base64Utils.js')));
550-
// Export all base64Utils.js functions
551-
print(maybeExport('intArrayFromBase64'));
552-
print(maybeExport('tryParseAsDataURI'));
553547
}
554548

555549
if (asmLibraryFunctions.length > 0) {

src/modules.js

Lines changed: 121 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -290,42 +290,132 @@ function isExportedByForceFilesystem(name) {
290290
name === 'removeRunDependency';
291291
}
292292

293-
// optionally export something.
294-
// in ASSERTIONS mode we show a useful error if it is used without
295-
// being exported. how we show the message depends on whether it's
296-
// a function (almost all of them) or a number.
297-
function maybeExport(name, isNumber) {
298-
// if requested to be exported, export it
299-
if (name in EXPORTED_RUNTIME_METHODS_SET) {
300-
var exported = name;
301-
if (isFSPrefixed(exported)) {
302-
// this is a filesystem value, FS.x exported as FS_x
303-
exported = 'FS.' + exported.substr(3);
293+
// export parts of the JS runtime that the user asked for
294+
function exportRuntime() {
295+
// optionally export something.
296+
// in ASSERTIONS mode we show a useful error if it is used without
297+
// being exported. how we show the message depends on whether it's
298+
// a function (almost all of them) or a number.
299+
function maybeExport(name, isNumber) {
300+
// if requested to be exported, export it
301+
if (name in EXPORTED_RUNTIME_METHODS_SET) {
302+
var exported = name;
303+
if (isFSPrefixed(exported)) {
304+
// this is a filesystem value, FS.x exported as FS_x
305+
exported = 'FS.' + exported.substr(3);
306+
}
307+
return 'Module["' + name + '"] = ' + exported + ';';
304308
}
305-
return 'Module["' + name + '"] = ' + exported + ';';
309+
// do not export it. but if ASSERTIONS, emit a
310+
// stub with an error, so the user gets a message
311+
// if it is used, that they should export it
312+
if (ASSERTIONS) {
313+
// check if it already exists, to support EXPORT_ALL and other cases
314+
// (we could optimize this, but in ASSERTIONS mode code size doesn't
315+
// matter anyhow)
316+
var extra = '';
317+
if (isExportedByForceFilesystem(name)) {
318+
extra = '. Alternatively, forcing filesystem support (-s FORCE_FILESYSTEM=1) can export this for you';
319+
}
320+
if (!isNumber) {
321+
return 'if (!Module["' + name + '"]) Module["' + name + '"] = function() { abort("\'' + name + '\' was not exported. add it to EXTRA_EXPORTED_RUNTIME_METHODS (see the FAQ)' + extra + '") };';
322+
} else {
323+
return 'if (!Module["' + name + '"]) Object.defineProperty(Module, "' + name + '", { get: function() { abort("\'' + name + '\' was not exported. add it to EXTRA_EXPORTED_RUNTIME_METHODS (see the FAQ)' + extra + '") } });';
324+
}
325+
}
326+
return '';
327+
}
328+
329+
function maybeExportNumber(name) {
330+
return maybeExport(name, true);
306331
}
307-
// do not export it. but if ASSERTIONS, emit a
308-
// stub with an error, so the user gets a message
309-
// if it is used, that they should export it
332+
333+
// All possible runtime elements to export
334+
var runtimeElements = [
335+
'intArrayFromString',
336+
'intArrayToString',
337+
'ccall',
338+
'cwrap',
339+
'setValue',
340+
'getValue',
341+
'allocate',
342+
'getMemory',
343+
'Pointer_stringify',
344+
'AsciiToString',
345+
'stringToAscii',
346+
'UTF8ArrayToString',
347+
'UTF8ToString',
348+
'stringToUTF8Array',
349+
'stringToUTF8',
350+
'UTF16ToString',
351+
'stringToUTF16',
352+
'lengthBytesUTF16',
353+
'UTF32ToString',
354+
'stringToUTF32',
355+
'lengthBytesUTF32',
356+
'stackTrace',
357+
'addOnPreRun',
358+
'addOnInit',
359+
'addOnPreMain',
360+
'addOnExit',
361+
'addOnPostRun',
362+
'writeStringToMemory',
363+
'writeArrayToMemory',
364+
'writeAsciiToMemory',
365+
'addRunDependency',
366+
'removeRunDependency',
367+
'FS',
368+
'FS_createFolder',
369+
'FS_createPath',
370+
'FS_createDataFile',
371+
'FS_createPreloadedFile',
372+
'FS_createLazyFile',
373+
'FS_createLink',
374+
'FS_createDevice',
375+
'FS_unlink',
376+
'GL',
377+
'staticAlloc',
378+
'dynamicAlloc',
379+
'warnOnce',
380+
'loadDynamicLibrary',
381+
'loadWebAssemblyModule',
382+
'getLEB',
383+
'getFunctionTables',
384+
'alignFunctionTables',
385+
'registerFunctions',
386+
'addFunction',
387+
'removeFunction',
388+
'getFuncWrapper',
389+
'prettyPrint',
390+
'makeBigInt',
391+
'dynCall',
392+
'getCompilerSetting',
393+
];
394+
if (SUPPORT_BASE64_EMBEDDING) {
395+
runtimeElements.push('intArrayFromBase64');
396+
runtimeElements.push('tryParseAsDataURI');
397+
}
398+
var runtimeNumbers = [
399+
'ALLOC_NORMAL',
400+
'ALLOC_STACK',
401+
'ALLOC_STATIC',
402+
'ALLOC_DYNAMIC',
403+
'ALLOC_NONE',
404+
];
310405
if (ASSERTIONS) {
311-
// check if it already exists, to support EXPORT_ALL and other cases
312-
// (we could optimize this, but in ASSERTIONS mode code size doesn't
313-
// matter anyhow)
314-
var extra = '';
315-
if (isExportedByForceFilesystem(name)) {
316-
extra = '. Alternatively, forcing filesystem support (-s FORCE_FILESYSTEM=1) can export this for you';
317-
}
318-
if (!isNumber) {
319-
return 'if (!Module["' + name + '"]) Module["' + name + '"] = function() { abort("\'' + name + '\' was not exported. add it to EXTRA_EXPORTED_RUNTIME_METHODS (see the FAQ)' + extra + '") };';
320-
} else {
321-
return 'if (!Module["' + name + '"]) Object.defineProperty(Module, "' + name + '", { get: function() { abort("\'' + name + '\' was not exported. add it to EXTRA_EXPORTED_RUNTIME_METHODS (see the FAQ)' + extra + '") } });';
406+
// check all exported things exist, warn about typos
407+
for (var name in EXPORTED_RUNTIME_METHODS_SET) {
408+
if (runtimeElements.indexOf(name) < 0 &&
409+
runtimeNumbers.indexOf(name) < 0) {
410+
printErr('warning: invalid item (maybe a typo?) in EXPORTED_RUNTIME_METHODS: ' + name);
411+
}
322412
}
323413
}
324-
return '';
325-
}
326-
327-
function maybeExportNumber(name) {
328-
return maybeExport(name, true);
414+
return runtimeElements.map(function(name) {
415+
return maybeExport(name);
416+
}).join('\n') + runtimeNumbers.map(function(name) {
417+
return maybeExportNumber(name);
418+
}).join('\n');
329419
}
330420

331421
var PassManager = {

src/postamble.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,7 @@
33

44
Module['asm'] = asm;
55

6-
{{{ maybeExport('FS') }}}
7-
{{{ maybeExport('FS_createFolder') }}}
8-
{{{ maybeExport('FS_createPath') }}}
9-
{{{ maybeExport('FS_createDataFile') }}}
10-
{{{ maybeExport('FS_createPreloadedFile') }}}
11-
{{{ maybeExport('FS_createLazyFile') }}}
12-
{{{ maybeExport('FS_createLink') }}}
13-
{{{ maybeExport('FS_createDevice') }}}
14-
{{{ maybeExport('FS_unlink') }}}
15-
16-
{{{ maybeExport('GL') }}}
6+
{{{ exportRuntime() }}}
177

188
#if MEM_INIT_IN_WASM == 0
199
#if MEM_INIT_METHOD == 2

0 commit comments

Comments
 (0)