Skip to content
Merged
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
14 changes: 14 additions & 0 deletions src/jsifier.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
compileTimeContext,
printErr,
readFile,
runInMacroContext,
warn,
warnOnce,
warningOccured,
Expand Down Expand Up @@ -79,6 +80,19 @@ function stringifyWithFunctions(obj) {
if (Array.isArray(obj)) {
return '[' + obj.map(stringifyWithFunctions).join(',') + ']';
}

// preserve the type of the object if it is one of [Map, Set, WeakMap, WeakSet].
const builtinContainers = runInMacroContext('[Map, Set, WeakMap, WeakSet]', {
filename: '<internal>',
});
for (const container of builtinContainers) {
if (obj instanceof container) {
const className = container.name;
assert(!obj.size, `cannot stringify ${className} with data`);
return `new ${className}`;
}
}

var rtn = '{\n';
for (const [key, value] of Object.entries(obj)) {
var str = stringifyWithFunctions(value);
Expand Down
28 changes: 28 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -4974,6 +4974,34 @@ def test_jslib_has_library(self):
''')
self.do_runf(test_file('hello_world.c'), emcc_args=['-L', '-lfoo.js'])

def test_jslib_new_objects_basic(self):
create_file('lib.js', '''
addToLibrary({
$obj: {
a: new Map(),
b: new Set(),
c: new WeakMap(),
d: new WeakSet()
}
});
''')
self.run_process([EMCC, test_file('hello_world.c'), '--js-library=lib.js', '-sEXPORTED_FUNCTIONS=obj,_main'])
self.assertContained("a:new Map,", read_file('a.out.js'))
self.assertContained("b:new Set,", read_file('a.out.js'))
self.assertContained("c:new WeakMap,", read_file('a.out.js'))
self.assertContained("d:new WeakSet,", read_file('a.out.js'))

def test_jslib_new_objects_non_empty(self):
create_file('lib.js', '''
addToLibrary({
$obj: {
bad: new Map([[1,2],[3,4]])
}
});
''')
err = self.expect_fail([EMCC, test_file('hello_world.c'), '--js-library=lib.js', '-sEXPORTED_FUNCTIONS=obj,_main'])
self.assertContained('cannot stringify Map with data', err)

def test_EMCC_BUILD_DIR(self):
# EMCC_BUILD_DIR was necessary in the past since we used to force the cwd to be src/ for
# technical reasons.
Expand Down