Skip to content

Commit

Permalink
when adding from js libraries, if we see something was already implem…
Browse files Browse the repository at this point in the history
…ented in compiled code, do not add from the library - it may be duplicate code if we do. instead, make sure to export the compiled version so js libraries can reach it. fixes #5288 (#5299)
  • Loading branch information
kripken authored Jun 17, 2017
1 parent 663fd42 commit 7a5744d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/jsifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ function JSify(data, functionsOnly) {
if (mainPass) {
// Add additional necessary items for the main pass. We can now do this since types are parsed (types can be used through
// generateStructInfo in library.js)
//B.start('jsifier-libload');

LibraryManager.load();
//B.stop('jsifier-libload');

var libFuncsToInclude;
if (INCLUDE_FULL_LIBRARY) {
Expand All @@ -68,10 +67,6 @@ function JSify(data, functionsOnly) {
libFuncsToInclude.push(key);
}
}
// mark implemented functions as already added (so if memcpy is in the forced full JS library, but also done in C, we just need the C)
for (var added in IMPLEMENTED_FUNCTIONS) {
addedLibraryItems[added.substr(1)] = true;
}
} else {
libFuncsToInclude = DEFAULT_LIBRARY_FUNCS_TO_INCLUDE;
}
Expand Down Expand Up @@ -134,6 +129,13 @@ function JSify(data, functionsOnly) {
var finalName = '_' + ident;
}

// if the function was implemented in compiled code, we just need to export it so we can reach it from JS
if (finalName in IMPLEMENTED_FUNCTIONS) {
EXPORTED_FUNCTIONS[finalName] = 1;
// stop here: we don't need to add anything from our js libraries, not even deps, compiled code is on it
return '';
}

// Don't replace implemented functions with library ones (which can happen when we add dependencies).
// Note: We don't return the dependencies here. Be careful not to end up where this matters
if (finalName in Functions.implementedFunctions) return '';
Expand Down
32 changes: 32 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -4751,6 +4751,38 @@ def test_unicode_js_library(self):
self.emcc_args += ['--js-library', path_from_root('tests', 'unicode_library.js')]
self.do_run(open(os.path.join(self.get_dir(), 'main.cpp'), 'r').read(), u'Unicode snowman \u2603 says hello!')

def test_js_lib_dep_memset(self):
open('lib.js', 'w').write(r'''
mergeInto(LibraryManager.library, {
depper__deps: ['memset'],
depper: function(ptr) {
_memset(ptr, 'd'.charCodeAt(0), 10);
},
});
''')
src = r'''
#include <string.h>
#include <stdio.h>
extern "C" {
extern void depper(char*);
}
int main(int argc, char** argv) {
char buffer[11];
buffer[10] = '\0';
// call by a pointer, to force linking of memset, no llvm intrinsic here
volatile auto ptr = memset;
(*ptr)(buffer, 'a', 10);
depper(buffer);
puts(buffer);
}
'''
self.emcc_args += ['--js-library', 'lib.js']
self.do_run(src, 'dddddddddd')
Settings.INCLUDE_FULL_LIBRARY = 1
self.do_run(src, 'dddddddddd')

def test_funcptr_import_type(self):
self.emcc_args += ['--js-library', path_from_root('tests', 'core', 'test_funcptr_import_type.js'), '-std=c++11']
self.do_run_in_out_file_test('tests', 'core', 'test_funcptr_import_type')
Expand Down

0 comments on commit 7a5744d

Please sign in to comment.