Skip to content

Commit 0cb2571

Browse files
committed
Add new simple export mechanism
This change adds a new `-sEXPORT` setting that can by used to export any type of symbols (see #8380) It also includes a new `-sMANGLED_SYMBOLS` setting which default to true in order to not break backwards compatibility. Both these new settings are currently experimental and using `-sEXPORT` currently disables `-sMANGLED_SYMBOLS` by default.
1 parent 319b423 commit 0cb2571

File tree

281 files changed

+1657
-1351
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

281 files changed

+1657
-1351
lines changed

emcc.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,6 @@ def apply_user_settings():
293293

294294
setattr(settings, user_key, value)
295295

296-
if key == 'EXPORTED_FUNCTIONS':
297-
# used for warnings in emscripten.py
298-
settings.USER_EXPORTS = settings.EXPORTED_FUNCTIONS.copy()
299-
300296
# TODO(sbc): Remove this legacy way.
301297
if key == 'WASM_OBJECT_FILES':
302298
settings.LTO = 0 if value else 'full'
@@ -1516,7 +1512,7 @@ def in_directory(root, child):
15161512

15171513
def parse_symbol_list_file(contents):
15181514
"""Parse contents of one-symbol-per-line response file. This format can by used
1519-
with, for example, -sEXPORTED_FUNCTIONS=@filename and avoids the need for any
1515+
with, for example, -sEXPORTS=@filename and avoids the need for any
15201516
kind of quoting or escaping.
15211517
"""
15221518
values = contents.splitlines()

site/source/docs/getting_started/FAQ.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ Here is an example of how to use it:
350350
<script type="text/javascript">
351351
var Module = {
352352
onRuntimeInitialized: function() {
353-
Module._foobar(); // foobar was exported
353+
Module.foobar(); // foobar was exported
354354
}
355355
};
356356
</script>

site/source/docs/optimizing/Module-Splitting.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ Here’s the function to write the profile and our new main function::
131131

132132
// Get the size of the profile and allocate a buffer for it.
133133
var len = __write_profile(0, 0);
134-
var ptr = _malloc(len);
134+
var ptr = malloc(len);
135135

136136
// Write the profile data to the buffer.
137137
__write_profile(ptr, len);
@@ -142,7 +142,7 @@ Here’s the function to write the profile and our new main function::
142142
fs.writeFileSync('profile.data', profile_data);
143143

144144
// Free the buffer.
145-
_free(ptr);
145+
free(ptr);
146146
});
147147

148148
int main() {

site/source/docs/porting/Debugging.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ Note that you need to emit HTML as in that example, as the memory profiler
287287
output is rendered onto the page. To view it, load ``page.html`` in your
288288
browser (remember to use a :ref:`local webserver <faq-local-webserver>`). The display
289289
auto-updates, so you can open the devtools console and run a command like
290-
``_malloc(1024 * 1024)``. That will allocate 1MB of memory, which will then show
290+
``malloc(1024 * 1024)``. That will allocate 1MB of memory, which will then show
291291
up on the memory profiler display.
292292

293293
.. _debugging-autodebugger:

site/source/docs/porting/connecting_cpp_and_javascript/Interacting-with-code.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ The parameters you pass to and receive from functions need to be primitive value
238238
- JavaScript string ``someString`` can be converted to a ``char *`` using ``ptr = stringToNewUTF8(someString)``.
239239

240240
.. note:: The conversion to a pointer allocates memory, which needs to be
241-
freed up via a call to ``free(ptr)`` afterwards (``_free`` in JavaScript side) -
241+
freed up via a call to ``free(ptr)`` afterwards -
242242
- ``char *`` received from C/C++ can be converted to a JavaScript string using :js:func:`UTF8ToString`.
243243

244244
There are other convenience functions for converting strings and encodings
@@ -702,10 +702,10 @@ to process the data, and finally frees the buffer.
702702

703703
.. code-block:: javascript
704704
705-
var buf = Module._malloc(myTypedArray.length*myTypedArray.BYTES_PER_ELEMENT);
705+
var buf = Module.malloc(myTypedArray.length*myTypedArray.BYTES_PER_ELEMENT);
706706
Module.HEAPU8.set(myTypedArray, buf);
707707
Module.ccall('my_function', 'number', ['number'], [buf]);
708-
Module._free(buf);
708+
Module.free(buf);
709709
710710
Here ``my_function`` is a C function that receives a single integer parameter
711711
(or a pointer, they are both just 32-bit integers for us) and returns an

src/Fetch.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ function fetchLoadCachedData(db, fetch, onsuccess, onerror) {
152152
#endif
153153
// The data pointer malloc()ed here has the same lifetime as the emscripten_fetch_t structure itself has, and is
154154
// freed when emscripten_fetch_close() is called.
155-
var ptr = _malloc(len);
155+
var ptr = malloc(len);
156156
HEAPU8.set(new Uint8Array(value), ptr);
157157
{{{ makeSetValue('fetch', C_STRUCTS.emscripten_fetch_t.data, 'ptr', '*') }}};
158158
writeI53ToI64(fetch + {{{ C_STRUCTS.emscripten_fetch_t.numBytes }}}, len);
@@ -329,7 +329,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
329329
#endif
330330
// The data pointer malloc()ed here has the same lifetime as the emscripten_fetch_t structure itself has, and is
331331
// freed when emscripten_fetch_close() is called.
332-
ptr = _malloc(ptrLen);
332+
ptr = malloc(ptrLen);
333333
HEAPU8.set(new Uint8Array(/** @type{Array<number>} */(xhr.response)), ptr);
334334
}
335335
{{{ makeSetValue('fetch', C_STRUCTS.emscripten_fetch_t.data, 'ptr', '*') }}}
@@ -401,7 +401,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
401401
assert(onprogress, 'When doing a streaming fetch, you should have an onprogress handler registered to receive the chunks!');
402402
#endif
403403
// Allocate byte data in Emscripten heap for the streamed memory block (freed immediately after onprogress call)
404-
ptr = _malloc(ptrLen);
404+
ptr = malloc(ptrLen);
405405
HEAPU8.set(new Uint8Array(/** @type{Array<number>} */(xhr.response)), ptr);
406406
}
407407
{{{ makeSetValue('fetch', C_STRUCTS.emscripten_fetch_t.data, 'ptr', '*') }}}
@@ -415,7 +415,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
415415
if (xhr.statusText) stringToUTF8(xhr.statusText, fetch + {{{ C_STRUCTS.emscripten_fetch_t.statusText }}}, 64);
416416
onprogress?.(fetch, xhr, e);
417417
if (ptr) {
418-
_free(ptr);
418+
free(ptr);
419419
}
420420
};
421421
xhr.onreadystatechange = (e) => {

src/compiler.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ if (!ALL_INCOMING_MODULE_JS_API.length) {
5050
ALL_INCOMING_MODULE_JS_API = INCOMING_MODULE_JS_API;
5151
}
5252

53-
EXPORTED_FUNCTIONS = new Set(EXPORTED_FUNCTIONS);
53+
EXPORTS = new Set(EXPORTS);
5454
WASM_EXPORTS = new Set(WASM_EXPORTS);
5555
SIDE_MODULE_EXPORTS = new Set(SIDE_MODULE_EXPORTS);
5656
INCOMING_MODULE_JS_API = new Set(INCOMING_MODULE_JS_API);

src/deterministic.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Module['thisProgram'] = 'thisProgram'; // for consistency between different buil
2626

2727
function hashMemory(id) {
2828
var ret = 0;
29-
var len = _sbrk(0);
29+
var len = sbrk(0);
3030
for (var i = 0; i < len; i++) {
3131
ret = (ret*17 + HEAPU8[i])|0;
3232
}

src/embind/embind.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
/*global addToLibrary*/
77

88
/*global Module, asm*/
9-
/*global _malloc, _free, _memcpy*/
9+
/*global malloc, free, memcpy*/
1010
/*global FUNCTION_TABLE, HEAP8, HEAPU8, HEAP16, HEAPU16, HEAP32, HEAPU32, HEAPF32, HEAPF64*/
1111
/*global readLatin1String*/
12-
/*global Emval, emval_handle_array, __emval_decref*/
12+
/*global Emval, emval_handle_array, _emval_decref*/
1313
/*jslint sub:true*/ /* The symbols 'fromWireType' and 'toWireType' must be accessed via array notation to be closure-safe since craftInvokerFunction crafts functions as strings that can't be closured. */
1414

1515
// -- jshint doesn't understand library syntax, so we need to specifically tell it about the symbols we define
@@ -46,7 +46,7 @@ var LibraryEmbind = {
4646
name: 'emscripten::val',
4747
'fromWireType': (handle) => {
4848
var rv = Emval.toValue(handle);
49-
__emval_decref(handle);
49+
_emval_decref(handle);
5050
return rv;
5151
},
5252
'toWireType': (destructors, value) => Emval.toHandle(value),
@@ -536,7 +536,7 @@ var LibraryEmbind = {
536536
str = a.join('');
537537
}
538538

539-
_free(value);
539+
free(value);
540540

541541
return str;
542542
},
@@ -558,7 +558,7 @@ var LibraryEmbind = {
558558
}
559559

560560
// assumes POINTER_SIZE alignment
561-
var base = _malloc({{{ POINTER_SIZE }}} + length + 1);
561+
var base = malloc({{{ POINTER_SIZE }}} + length + 1);
562562
var ptr = base + {{{ POINTER_SIZE }}};
563563
{{{ makeSetValue('base', '0', 'length', SIZE_TYPE) }}};
564564
if (stdStringIsUTF8 && valueIsOfTypeString) {
@@ -568,7 +568,7 @@ var LibraryEmbind = {
568568
for (var i = 0; i < length; ++i) {
569569
var charCode = value.charCodeAt(i);
570570
if (charCode > 255) {
571-
_free(ptr);
571+
free(ptr);
572572
throwBindingError('String has UTF-16 code units that do not fit in 8 bits');
573573
}
574574
HEAPU8[ptr + i] = charCode;
@@ -581,14 +581,14 @@ var LibraryEmbind = {
581581
}
582582

583583
if (destructors !== null) {
584-
destructors.push(_free, base);
584+
destructors.push(free, base);
585585
}
586586
return base;
587587
},
588588
argPackAdvance: GenericWireTypeSize,
589589
'readValueFromPointer': readPointer,
590590
destructorFunction(ptr) {
591-
_free(ptr);
591+
free(ptr);
592592
},
593593
});
594594
},
@@ -636,7 +636,7 @@ var LibraryEmbind = {
636636
}
637637
}
638638

639-
_free(value);
639+
free(value);
640640

641641
return str;
642642
},
@@ -647,20 +647,20 @@ var LibraryEmbind = {
647647

648648
// assumes POINTER_SIZE alignment
649649
var length = lengthBytesUTF(value);
650-
var ptr = _malloc({{{ POINTER_SIZE }}} + length + charSize);
650+
var ptr = malloc({{{ POINTER_SIZE }}} + length + charSize);
651651
{{{ makeSetValue('ptr', '0', 'length / charSize', SIZE_TYPE) }}};
652652

653653
encodeString(value, ptr + {{{ POINTER_SIZE }}}, length + charSize);
654654

655655
if (destructors !== null) {
656-
destructors.push(_free, ptr);
656+
destructors.push(free, ptr);
657657
}
658658
return ptr;
659659
},
660660
argPackAdvance: GenericWireTypeSize,
661661
'readValueFromPointer': readPointer,
662662
destructorFunction(ptr) {
663-
_free(ptr);
663+
free(ptr);
664664
}
665665
});
666666
},
@@ -671,7 +671,7 @@ var LibraryEmbind = {
671671

672672
_embind_register_user_type__deps: ['_embind_register_emval'],
673673
_embind_register_user_type: (rawType, name) => {
674-
__embind_register_emval(rawType);
674+
_embind_register_emval(rawType);
675675
},
676676

677677
_embind_register_optional__deps: ['$registerType', '$EmValOptionalType'],

src/embind/embind_shared.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@ var LibraryEmbindShared = {
117117
},
118118
$getTypeName__deps: ['$readLatin1String', '__getTypeName', 'free'],
119119
$getTypeName: (type) => {
120-
var ptr = ___getTypeName(type);
120+
var ptr = __getTypeName(type);
121121
var rv = readLatin1String(ptr);
122-
_free(ptr);
122+
free(ptr);
123123
return rv;
124124
},
125125
$getFunctionName__deps: [],

0 commit comments

Comments
 (0)