Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions src/jsifier.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,14 @@ import {
timer,
} from './utility.mjs';
import {
addedLibraryItems,
extraExports,
LibraryManager,
librarySymbols,
nativeAliases,
shouldExportLibrarySymbol,
} from './modules.mjs';

const addedLibraryItems = {};

// Debug-only mode (`EMCC_CHECK_DEPS=1` in the environment) that performs a full
// AST parse on JS library items and reports an error if any local binding
// shadows a library symbol (which could cause false positives in the
Expand Down Expand Up @@ -606,7 +605,7 @@ export async function runJSify(outputFile, symbolsOnly) {
}

for (const key of Object.keys(LibraryManager.library)) {
if (!isDecorator(key)) {
if (!isDecorator(key) && !LibraryManager.library[key + '__const']) {
if (INCLUDE_FULL_LIBRARY || EXPORTED_FUNCTIONS.has(mangleCSymbolName(key))) {
symbolsNeeded.push(key);
}
Expand Down Expand Up @@ -886,7 +885,10 @@ function(${args}) {
snippet = new Function('...args', stubFunctionBody);
}

librarySymbols.push(mangled);
const isConst = LibraryManager.library[symbol + '__const'];
if (!isConst) {
librarySymbols.push(mangled);
}

if (!isStub && LibraryManager.library[symbol + '__export']) {
extraExports.add(mangled);
Expand Down Expand Up @@ -957,7 +959,7 @@ function(${args}) {
// Check for dependencies on `__internal` symbols from user libraries.
if (isUserSymbol) {
for (const dep of deps) {
if (LibraryManager.library[dep + '__internal']) {
if (LibraryManager.library[dep + '__internal'] && !LibraryManager.library[dep + '__user']) {
warn(`user library symbol '${symbol}' depends on internal symbol '${dep}'`);
}
}
Expand All @@ -979,6 +981,9 @@ function(${args}) {
}
let contentText;
if (isFunction) {
if (isConst) {
error(`JS library error: '__const' decorator cannot be used with functions: '${symbol}'`);
}
// Emit the body of a JS library function.
if ((USE_ASAN || USE_LSAN) && LibraryManager.library[symbol + '__noleakcheck']) {
contentText = modifyJSFunction(
Expand All @@ -1003,6 +1008,9 @@ function(${args}) {
contentText = contentText.replace(/function(?:\s+([^(]+))?\s*\(/, `function ${mangled}(`);
}
} else if (typeof snippet === 'string' && snippet.startsWith(';')) {
if (isConst) {
error(`JS library error: '__const' decorator cannot be used with ';'-prefixed snippets: '${symbol}'`);
}
// In JS libraries
// foo: ';[code here verbatim]'
// emits
Expand All @@ -1012,6 +1020,9 @@ function(${args}) {
contentText += ';';
}
} else if (typeof snippet === 'undefined') {
if (isConst) {
error(`JS library error: '__const' decorator cannot be used with undefined symbols: '${symbol}'`);
}
// For JS library functions that are simply aliases of native symbols,
// we don't need to generate anything here. Instead these get included
// and exported alongside native symbols.
Expand All @@ -1029,14 +1040,15 @@ function(${args}) {
if (typeof snippet === 'string' && snippet[0] == '=') {
snippet = snippet.slice(1);
}
contentText = `var ${mangled} = ${snippet};`;
const declType = isConst ? 'const' : 'var';
contentText = `${declType} ${mangled} = ${snippet};`;
}

if (CHECK_DEPS && (STRICT || !isUserSymbol) && !isStub && contentText) {
checkShadowing(symbol, mangled, contentText);
}

if (contentText && MODULARIZE == 'instance' && shouldExportLibrarySymbol(mangled) && !isStub) {
if (contentText && MODULARIZE == 'instance' && shouldExportLibrarySymbol(mangled) && !isStub && !isConst) {
// In MODULARIZE=instance mode mark JS library symbols are exported at
// the point of declaration.
contentText = 'export ' + contentText;
Expand Down
21 changes: 12 additions & 9 deletions src/lib/libcore.js
Original file line number Diff line number Diff line change
Expand Up @@ -1478,13 +1478,14 @@ addToLibrary({

// Represents whether emscripten_get_now is guaranteed monotonic; the Date.now
// implementation is not :(
$nowIsMonotonic__internal: true,
...defineInternalConsts({
#if AUDIO_WORKLET // // https://github.com/WebAudio/web-audio-api/issues/2413
$nowIsMonotonic: `!!globalThis.performance?.now;`,
$nowIsMonotonic: `!!globalThis.performance?.now;`,
#else
// Modern environment where performance.now() is supported
$nowIsMonotonic: 1,
// Modern environment where performance.now() is supported
$nowIsMonotonic: 1,
#endif
}),

_emscripten_get_now_is_monotonic__internal: true,
_emscripten_get_now_is_monotonic: () => nowIsMonotonic,
Expand Down Expand Up @@ -2410,15 +2411,17 @@ addToLibrary({
// We used to define these globals unconditionally in support code.
// Instead, we now define them here so folks can pull it in explicitly, on
// demand.
$STACK_SIZE: {{{ STACK_SIZE }}},
$STACK_ALIGN: {{{ STACK_ALIGN }}},
$POINTER_SIZE: {{{ POINTER_SIZE }}},
$ASSERTIONS: {{{ ASSERTIONS }}},
...defineConsts({
$STACK_SIZE: {{{ STACK_SIZE }}},
$STACK_ALIGN: {{{ STACK_ALIGN }}},
$POINTER_SIZE: {{{ POINTER_SIZE }}},
$ASSERTIONS: {{{ ASSERTIONS }}},
}),
});

function autoAddDeps(lib, name) {
for (const item of Object.keys(lib)) {
if (!isDecorator(item)) {
if (!isDecorator(item) && item != name && !lib[item + '__const']) {
lib[item + '__deps'] ??= [];
lib[item + '__deps'].push(name);
}
Expand Down
13 changes: 6 additions & 7 deletions src/lib/libdylink.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@
#error "library_dylink.js requires MAIN_MODULE"
#endif

{{{
const UNDEFINED_ADDR = to64(-1);
}}}

var LibraryDylink = {
...defineInternalConsts({
$UNDEFINED_ADDR: to64(-1),
Comment thread
sbc100 marked this conversation as resolved.
}),
#if FILESYSTEM
$registerWasmPlugin: () => {
// Use string keys here for public methods to avoid minification since the
Expand Down Expand Up @@ -157,7 +156,7 @@ var LibraryDylink = {
#if DYLINK_DEBUG == 2
dbg(`new GOT entry: ${symName}`);
#endif
rtn = GOT[symName] = new WebAssembly.Global({'value': '{{{ POINTER_WASM_TYPE }}}', 'mutable': true}, {{{ UNDEFINED_ADDR }}});
rtn = GOT[symName] = new WebAssembly.Global({'value': '{{{ POINTER_WASM_TYPE }}}', 'mutable': true}, UNDEFINED_ADDR);
}
if (!weakImports.has(symName)) {
// Any non-weak reference to a symbol marks it as `required`, which
Expand Down Expand Up @@ -219,7 +218,7 @@ var LibraryDylink = {
}
#endif

var existingEntry = GOT[symName] && GOT[symName].value != {{{ UNDEFINED_ADDR }}};
var existingEntry = GOT[symName] && GOT[symName].value != UNDEFINED_ADDR;
if (replace || !existingEntry) {
#if DYLINK_DEBUG == 2
dbg(`updateGOT: before: ${symName} : ${GOT[symName]?.value}`);
Expand Down Expand Up @@ -303,7 +302,7 @@ var LibraryDylink = {
dbg('reportUndefinedSymbols');
#endif
for (var [symName, entry] of Object.entries(GOT)) {
if (entry.value == {{{ UNDEFINED_ADDR }}}) {
if (entry.value == UNDEFINED_ADDR) {
#if DYLINK_DEBUG
dbg(`undef GOT entry: ${symName}`);
#endif
Expand Down
Loading
Loading