Skip to content

Commit

Permalink
Add new simple export mechanism
Browse files Browse the repository at this point in the history
This change adds a new `-sEXPORT` setting that can by used to export
any type of symbols (see emscripten-core#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.
  • Loading branch information
sbc100 committed Nov 21, 2024
1 parent 319b423 commit 3558467
Show file tree
Hide file tree
Showing 281 changed files with 1,659 additions and 1,351 deletions.
6 changes: 1 addition & 5 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,6 @@ def apply_user_settings():

setattr(settings, user_key, value)

if key == 'EXPORTED_FUNCTIONS':
# used for warnings in emscripten.py
settings.USER_EXPORTS = settings.EXPORTED_FUNCTIONS.copy()

# TODO(sbc): Remove this legacy way.
if key == 'WASM_OBJECT_FILES':
settings.LTO = 0 if value else 'full'
Expand Down Expand Up @@ -1516,7 +1512,7 @@ def in_directory(root, child):

def parse_symbol_list_file(contents):
"""Parse contents of one-symbol-per-line response file. This format can by used
with, for example, -sEXPORTED_FUNCTIONS=@filename and avoids the need for any
with, for example, -sEXPORTS=@filename and avoids the need for any
kind of quoting or escaping.
"""
values = contents.splitlines()
Expand Down
2 changes: 1 addition & 1 deletion site/source/docs/getting_started/FAQ.rst
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ Here is an example of how to use it:
<script type="text/javascript">
var Module = {
onRuntimeInitialized: function() {
Module._foobar(); // foobar was exported
Module.foobar(); // foobar was exported
}
};
</script>
Expand Down
4 changes: 2 additions & 2 deletions site/source/docs/optimizing/Module-Splitting.rst
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ Here’s the function to write the profile and our new main function::

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

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

// Free the buffer.
_free(ptr);
free(ptr);
});

int main() {
Expand Down
2 changes: 1 addition & 1 deletion site/source/docs/porting/Debugging.rst
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ Note that you need to emit HTML as in that example, as the memory profiler
output is rendered onto the page. To view it, load ``page.html`` in your
browser (remember to use a :ref:`local webserver <faq-local-webserver>`). The display
auto-updates, so you can open the devtools console and run a command like
``_malloc(1024 * 1024)``. That will allocate 1MB of memory, which will then show
``malloc(1024 * 1024)``. That will allocate 1MB of memory, which will then show
up on the memory profiler display.

.. _debugging-autodebugger:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ The parameters you pass to and receive from functions need to be primitive value
- JavaScript string ``someString`` can be converted to a ``char *`` using ``ptr = stringToNewUTF8(someString)``.

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

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

.. code-block:: javascript
var buf = Module._malloc(myTypedArray.length*myTypedArray.BYTES_PER_ELEMENT);
var buf = Module.malloc(myTypedArray.length*myTypedArray.BYTES_PER_ELEMENT);
Module.HEAPU8.set(myTypedArray, buf);
Module.ccall('my_function', 'number', ['number'], [buf]);
Module._free(buf);
Module.free(buf);
Here ``my_function`` is a C function that receives a single integer parameter
(or a pointer, they are both just 32-bit integers for us) and returns an
Expand Down
8 changes: 4 additions & 4 deletions src/Fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ function fetchLoadCachedData(db, fetch, onsuccess, onerror) {
#endif
// The data pointer malloc()ed here has the same lifetime as the emscripten_fetch_t structure itself has, and is
// freed when emscripten_fetch_close() is called.
var ptr = _malloc(len);
var ptr = malloc(len);
HEAPU8.set(new Uint8Array(value), ptr);
{{{ makeSetValue('fetch', C_STRUCTS.emscripten_fetch_t.data, 'ptr', '*') }}};
writeI53ToI64(fetch + {{{ C_STRUCTS.emscripten_fetch_t.numBytes }}}, len);
Expand Down Expand Up @@ -329,7 +329,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
#endif
// The data pointer malloc()ed here has the same lifetime as the emscripten_fetch_t structure itself has, and is
// freed when emscripten_fetch_close() is called.
ptr = _malloc(ptrLen);
ptr = malloc(ptrLen);
HEAPU8.set(new Uint8Array(/** @type{Array<number>} */(xhr.response)), ptr);
}
{{{ makeSetValue('fetch', C_STRUCTS.emscripten_fetch_t.data, 'ptr', '*') }}}
Expand Down Expand Up @@ -401,7 +401,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
assert(onprogress, 'When doing a streaming fetch, you should have an onprogress handler registered to receive the chunks!');
#endif
// Allocate byte data in Emscripten heap for the streamed memory block (freed immediately after onprogress call)
ptr = _malloc(ptrLen);
ptr = malloc(ptrLen);
HEAPU8.set(new Uint8Array(/** @type{Array<number>} */(xhr.response)), ptr);
}
{{{ makeSetValue('fetch', C_STRUCTS.emscripten_fetch_t.data, 'ptr', '*') }}}
Expand All @@ -415,7 +415,7 @@ function fetchXHR(fetch, onsuccess, onerror, onprogress, onreadystatechange) {
if (xhr.statusText) stringToUTF8(xhr.statusText, fetch + {{{ C_STRUCTS.emscripten_fetch_t.statusText }}}, 64);
onprogress?.(fetch, xhr, e);
if (ptr) {
_free(ptr);
free(ptr);
}
};
xhr.onreadystatechange = (e) => {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ if (!ALL_INCOMING_MODULE_JS_API.length) {
ALL_INCOMING_MODULE_JS_API = INCOMING_MODULE_JS_API;
}

EXPORTED_FUNCTIONS = new Set(EXPORTED_FUNCTIONS);
EXPORTS = new Set(EXPORTS);
WASM_EXPORTS = new Set(WASM_EXPORTS);
SIDE_MODULE_EXPORTS = new Set(SIDE_MODULE_EXPORTS);
INCOMING_MODULE_JS_API = new Set(INCOMING_MODULE_JS_API);
Expand Down
2 changes: 1 addition & 1 deletion src/deterministic.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Module['thisProgram'] = 'thisProgram'; // for consistency between different buil

function hashMemory(id) {
var ret = 0;
var len = _sbrk(0);
var len = sbrk(0);
for (var i = 0; i < len; i++) {
ret = (ret*17 + HEAPU8[i])|0;
}
Expand Down
26 changes: 13 additions & 13 deletions src/embind/embind.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
/*global addToLibrary*/

/*global Module, asm*/
/*global _malloc, _free, _memcpy*/
/*global malloc, free, memcpy*/
/*global FUNCTION_TABLE, HEAP8, HEAPU8, HEAP16, HEAPU16, HEAP32, HEAPU32, HEAPF32, HEAPF64*/
/*global readLatin1String*/
/*global Emval, emval_handle_array, __emval_decref*/
/*global Emval, emval_handle_array, _emval_decref*/
/*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. */

// -- jshint doesn't understand library syntax, so we need to specifically tell it about the symbols we define
Expand Down Expand Up @@ -46,7 +46,7 @@ var LibraryEmbind = {
name: 'emscripten::val',
'fromWireType': (handle) => {
var rv = Emval.toValue(handle);
__emval_decref(handle);
_emval_decref(handle);
return rv;
},
'toWireType': (destructors, value) => Emval.toHandle(value),
Expand Down Expand Up @@ -536,7 +536,7 @@ var LibraryEmbind = {
str = a.join('');
}

_free(value);
free(value);

return str;
},
Expand All @@ -558,7 +558,7 @@ var LibraryEmbind = {
}

// assumes POINTER_SIZE alignment
var base = _malloc({{{ POINTER_SIZE }}} + length + 1);
var base = malloc({{{ POINTER_SIZE }}} + length + 1);
var ptr = base + {{{ POINTER_SIZE }}};
{{{ makeSetValue('base', '0', 'length', SIZE_TYPE) }}};
if (stdStringIsUTF8 && valueIsOfTypeString) {
Expand All @@ -568,7 +568,7 @@ var LibraryEmbind = {
for (var i = 0; i < length; ++i) {
var charCode = value.charCodeAt(i);
if (charCode > 255) {
_free(ptr);
free(ptr);
throwBindingError('String has UTF-16 code units that do not fit in 8 bits');
}
HEAPU8[ptr + i] = charCode;
Expand All @@ -581,14 +581,14 @@ var LibraryEmbind = {
}

if (destructors !== null) {
destructors.push(_free, base);
destructors.push(free, base);
}
return base;
},
argPackAdvance: GenericWireTypeSize,
'readValueFromPointer': readPointer,
destructorFunction(ptr) {
_free(ptr);
free(ptr);
},
});
},
Expand Down Expand Up @@ -636,7 +636,7 @@ var LibraryEmbind = {
}
}

_free(value);
free(value);

return str;
},
Expand All @@ -647,20 +647,20 @@ var LibraryEmbind = {

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

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

if (destructors !== null) {
destructors.push(_free, ptr);
destructors.push(free, ptr);
}
return ptr;
},
argPackAdvance: GenericWireTypeSize,
'readValueFromPointer': readPointer,
destructorFunction(ptr) {
_free(ptr);
free(ptr);
}
});
},
Expand All @@ -671,7 +671,7 @@ var LibraryEmbind = {

_embind_register_user_type__deps: ['_embind_register_emval'],
_embind_register_user_type: (rawType, name) => {
__embind_register_emval(rawType);
_embind_register_emval(rawType);
},

_embind_register_optional__deps: ['$registerType', '$EmValOptionalType'],
Expand Down
4 changes: 2 additions & 2 deletions src/embind/embind_shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ var LibraryEmbindShared = {
},
$getTypeName__deps: ['$readLatin1String', '__getTypeName', 'free'],
$getTypeName: (type) => {
var ptr = ___getTypeName(type);
var ptr = __getTypeName(type);
var rv = readLatin1String(ptr);
_free(ptr);
free(ptr);
return rv;
},
$getFunctionName__deps: [],
Expand Down
8 changes: 4 additions & 4 deletions src/embind/emval.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/*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. */

// -- jshint doesn't understand library syntax, so we need to mark the symbols exposed here
/*global getStringOrSymbol, emval_freelist, emval_handles, Emval, __emval_unregister, count_emval_handles, emval_symbols, __emval_decref*/
/*global getStringOrSymbol, emval_freelist, emval_handles, Emval, _emval_unregister, count_emval_handles, emval_symbols, _emval_decref*/
/*global emval_addMethodCaller, emval_methodCallers, addToLibrary, global, emval_lookupTypes, makeLegalFunctionName*/
/*global emval_get_global*/

Expand Down Expand Up @@ -116,7 +116,7 @@ var LibraryEmVal = {
_emval_run_destructors: (handle) => {
var destructors = Emval.toValue(handle);
runDestructors(destructors);
__emval_decref(handle);
_emval_decref(handle);
},

_emval_new_array__deps: ['$Emval'],
Expand Down Expand Up @@ -470,7 +470,7 @@ var LibraryEmVal = {
_emval_coro_suspend__deps: ['$Emval', '_emval_coro_resume'],
_emval_coro_suspend: (promiseHandle, awaiterPtr) => {
Emval.toValue(promiseHandle).then(result => {
__emval_coro_resume(awaiterPtr, Emval.toHandle(result));
_emval_coro_resume(awaiterPtr, Emval.toHandle(result));
});
},

Expand All @@ -483,7 +483,7 @@ var LibraryEmVal = {
// user-friendly error message and stacktrace from C++ exception
// if EXCEPTION_STACK_TRACES is enabled and numeric exception
// with metadata optimised out otherwise.
___cxa_rethrow();
__cxa_rethrow();
} catch (e) {
// But catch it so that it rejects the promise instead of throwing
// in an unpredictable place during async execution.
Expand Down
Loading

0 comments on commit 3558467

Please sign in to comment.