Skip to content

Conversation

@nazar-pc
Copy link
Contributor

Fixes #5568

output = Popen([os.path.join(self.get_dir(), 'files.o.run')], stdin=open(os.path.join(self.get_dir(), 'stdin')), stdout=PIPE, stderr=PIPE).communicate()
self.assertContained('''size: 37
data: 119,97,107,97,32,119,97,107,97,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35,35
loop: 119 97 107 97 32 119 97 107 97 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35 35
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, that whitespace actually matters. i pushed a fix to incoming now so it doesn't look unnecessary to editors.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rebased against incoming

assert "module['exports'] = NotModule;" in src
output = Popen(NODE_JS + ['-e', 'var m = require("./a.out.js"); m();'], stdout=PIPE, stderr=PIPE).communicate()
assert output == ('hello, world!\n', ''), 'expected output, got\n===\nSTDOUT\n%s\n===\nSTDERR\n%s\n===\n' % output
Popen([PYTHON, EMCC, path_from_root('tests', 'hello_world.c'), '-s', 'MODULARIZE=1', '-s', 'NO_EXIT_RUNTIME=1']).communicate()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a comment saying this tests two calls to require

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment added


Module['arguments'] = process['argv'].slice(2);

#if MODULARIZE == 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a comment saying why this is not needed in that mode

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment added

@CMCDragonkai
Copy link

CMCDragonkai commented Sep 12, 2017

I met this issue as well, and my solution to it was not to use MODULARIZE=1 and instead use prejs and postjs function wrapper that defined a locally scoped module to be undefined.

var module = undefined;
// or let module = undefined; // works under no optimisation, but under optimisation this fails

In fact this was the only way to use ES6 modules. The MODURALIZE=1 wouldn't work if you wanted to use ES6 modules instead (because it just wraps the whole thing in a single function and prejs and postjs are no longer true headers and footers of the compiled js). A transition of MODULARIZE=1 plus perhaps ES6=1 to ES6 modules could be another PR.

src/shell.js Outdated

Module['arguments'] = process['argv'].slice(2);

// With MODULARIZE == 1 wrapper function will be exported instead of Module itself and re-exporting it second time will cause issues
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i still don't fully understand this even with the comment, sorry.

why is the second time special? and why does it matter we export the wrapper function and not the Module? I don't see a connection here that is important, I guess...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without this patch var m = require('module_name') initially exports wrapper function module.exports = wrapper_function, and we need to call this wrapper function to obtain Module itself.
However, when m() is called it re-exports module.exports = Module.
Thus successive require('module_name') doesn't return wrapper function anymore, but instead returns plain Module.

Just try to run added test on current incoming branch.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two Module variables. When you require it the first time you get the outer wrapper Module, and the second time you get the inner true Module.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see, so there are two separate places we export, one inside the module - in shell.js, which is disabled by this PR - and another outside the module, added by MODULARIZE, and is the proper place?

If that's correct then looks good to me. Let's add that to the comment, though, something like MODULARIZE will export the module in the proper place outside, we don't need to export here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is correct, updated comment accordingly

@kripken kripken merged commit 117b7a3 into emscripten-core:incoming Sep 13, 2017
@kripken
Copy link
Member

kripken commented Sep 13, 2017

Great, thanks.

@nazar-pc nazar-pc deleted the modularize-fix branch September 13, 2017 17:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants