Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix EXPORT_ES6 + USE_PTHREADS #8306

Merged
merged 187 commits into from
May 24, 2019
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
187 commits
Select commit Hold shift + click to select a range
b78e914
Fix ENVIRONMENT_IS_PTHREAD being undefined
VirtualTim Mar 19, 2019
0a3915a
Fix document.currentScript in ES6 modules
VirtualTim Mar 19, 2019
2849d36
Fix undefined PThread, HEAPU32
VirtualTim Mar 19, 2019
0edabe6
Replace importScripts with import for ES Modules
VirtualTim Mar 19, 2019
90a4b18
Add an EXPORT_ES6 ifdef
VirtualTim Mar 19, 2019
97b0dd2
Fix emcc.py build issue
VirtualTim Mar 20, 2019
90e2e59
Fix typo
VirtualTim Mar 20, 2019
f9e04a3
Fix indentation
VirtualTim Mar 20, 2019
5a933df
Fix another typo
VirtualTim Mar 20, 2019
03c96b8
Better way to determine script path inside module
VirtualTim Mar 21, 2019
5404d76
Fix ENVIRONMENT_IS_PTHREAD check
VirtualTim Mar 25, 2019
054f4ca
Remove old attempt at fixing ENVIRONMENT_IS_PTHREAD var hoisting
VirtualTim Mar 25, 2019
23bd473
Don't declare functions in a thread that have already been declared i…
VirtualTim Mar 25, 2019
e0125dd
Remove line with just whitespace
VirtualTim Mar 25, 2019
48d2d77
Put a space after my '#' to keep flake8 happy
VirtualTim Mar 25, 2019
4148743
Explicitly pass in ENVIRONMENT_IS_PTHREAD when creating a thread
VirtualTim Apr 2, 2019
dfd8440
Attach ENVIRONMENT_IS_PTHREAD to the module, instead of a global
VirtualTim Apr 2, 2019
a503811
Changed GETSCRIPT to script_url
VirtualTim Apr 2, 2019
de25547
Only remove runtime checks in the specific case they don't work
VirtualTim Apr 9, 2019
208daf5
Changed check to assertion.
VirtualTim May 17, 2019
8b86f6e
Prevent discard of value names for -g3 or above (#8263)
milizhang Mar 19, 2019
9c53450
testing: Prefer `do_run_in_out_file_test`. NFC. (#8305)
sbc100 Mar 19, 2019
c25c5dd
Fix SAFE_HEAP_LOG + WASM (#8309)
sbc100 Mar 19, 2019
970a22c
Cleanup of emcc -s argument parsing (#8312)
sbc100 Mar 20, 2019
0583f79
Rename Atomics.wake to Atomics.notify (#8320)
VirtualTim Mar 21, 2019
e5584ad
1.38.30
kripken Mar 21, 2019
c184ae3
Always call EMAR with PYTHON as arg0 (#8342)
sbc100 Mar 25, 2019
3715670
Add test_dylink_function_pointer_equality test (#8330)
sbc100 Mar 25, 2019
36e9a41
Create pull request template (#8345)
kripken Mar 26, 2019
a1e7f6f
Fix pthreads + LIBRARY_DEBUG (#8303)
kripken Mar 26, 2019
261c291
Convert test_printf to do_run_in_out_file_test (#8343)
sbc100 Mar 26, 2019
d1a9fc3
Fix llvm backend debug info with opts (#8298)
kripken Mar 26, 2019
6af55a6
Report browser exceptions (#8347)
kripken Mar 26, 2019
d3fd009
Add more fine-grained gl test reporting (#8346)
VirtualTim Mar 26, 2019
1a22f4c
Reduce the default verbosity of JS library error messages (#8339)
sbc100 Mar 26, 2019
f54f621
Enable SDL2 threads (#8301)
jakogut Mar 27, 2019
e6b1f3e
Support tar.gz file (#8355)
msabramo Mar 27, 2019
3c178bf
Print more compiler stages (makes -v more useful) (#8350)
sbc100 Mar 28, 2019
3bd09f6
Add bzip2 port (#8353)
msabramo Mar 28, 2019
745c31b
Add .editorconfig (#8359)
msabramo Mar 28, 2019
69c2e90
Document site update process (#8358)
kripken Mar 28, 2019
9c0fccb
Add comment about shutil.unpack_archive (#8360)
msabramo Mar 29, 2019
03c2739
Add libjpeg port (#8361)
msabramo Mar 29, 2019
65544da
Docs: remove ./ - do not assume emcc is in current directory (#8334)
DanielRuf Mar 29, 2019
2b2ab6b
Build libjpeg with -DBUILD_SHARED_LIBS=OFF in test code. NFC. (#8363)
sbc100 Mar 29, 2019
08105ed
other.test_minimal_runtime_code_size: apply slop to code sizes (#8364)
kripken Apr 1, 2019
b8717e4
Added --no_private_browsing emrun argument (#8319)
titrindl Apr 1, 2019
36b94e4
Pass --disable-shared when configuring building bullet (#8381)
sbc100 Apr 1, 2019
bcb5474
Add failing test of rtti (dynamic_cast) combined with dynamic librari…
sbc100 Apr 1, 2019
009c8f6
Set TARGET_SUPPORTS_SHARED_LIBS in cmake config (#8362)
sbc100 Apr 1, 2019
04a83ff
Delete unused build_project method and move build_library to test/run…
sbc100 Apr 1, 2019
fabdd2a
give a clear error when TOTAL_MEMORY is not big enough (#8369)
kripken Apr 2, 2019
c7323da
Fix warning in test_dylink_zlib (#8383)
sbc100 Apr 2, 2019
b56a8c7
Restore testing accidentally removed in #7499 (#8382)
sbc100 Apr 2, 2019
420b5e9
Move get_library helpers into test classes (#8385)
sbc100 Apr 2, 2019
29a7afa
Add chdir to File System API Docs (#8374)
mmore500 Apr 2, 2019
dd29b3b
remove duplicate settings entry for EMIT_EMSCRIPTEN_METADATA (#8387)
kripken Apr 3, 2019
f4d4d8b
test wasm3.test_llvm_used (#8388)
kripken Apr 3, 2019
18ebc84
Disallow EMULATED_FUNCTION_POINTERS with wasm backend (#8386)
sbc100 Apr 3, 2019
8204c0e
Mention that closure errors may be clearer with -g1, since then it is…
kripken Apr 3, 2019
9e837aa
iprintf/small_printf opts using Emscripten OS triple in LLVM backend …
kripken Apr 4, 2019
a291091
Get MODULARIZE_INSTANCE working with USE_PTHREADS (#8372)
kripken Apr 4, 2019
1423e30
wasm-backend: Handle namedGlobals in metadaa of wasm-emscripten-final…
sbc100 Apr 4, 2019
c4b5e3d
Add --disable-shared to freetype build used in testing (#8396)
sbc100 Apr 4, 2019
5c25fc5
Remove special compiler method for libcompiler_rt_wasm (#8391)
sbc100 Apr 4, 2019
15f323a
Benchmark improvements (#8398)
kripken Apr 5, 2019
586dd66
Errno fixes (#8402)
kripken Apr 5, 2019
c3ba604
Update Binaryen to 75 (#8408)
kripken Apr 5, 2019
4772328
update wasm docs (#8407)
kripken Apr 6, 2019
9e2efb0
Allow using wasm features (#8403)
kripken Apr 6, 2019
fc3dbcb
Make linpack test more robust (#8404)
kripken Apr 8, 2019
f666f9f
Remove old TODO (#8412)
sbc100 Apr 8, 2019
dc90308
Dynamic linking: export aliasee if not export all and if alias is exp…
waterlike86 Apr 8, 2019
a8164ac
Working (but slow) support for pthreads + memory growth (#8365)
kripken Apr 8, 2019
d0a43bf
Fix race conditions in browser testing (#8405)
kripken Apr 8, 2019
8504599
restore some ./'s that should not have been removed in #8334 (#8368)
kripken Apr 9, 2019
bcf4f5f
Disable NODERAWFS mode in unistd_unlink, which fails on the mac bots …
kripken Apr 9, 2019
4309b0c
emar: Only use minimal name mangling (#8419)
sbc100 Apr 9, 2019
c2bb415
Separate STRICT from MINIMAL_RUNTIME (#8411)
sbc100 Apr 9, 2019
8623572
pass in the feature flags to wasm-opt (#8418)
kripken Apr 9, 2019
178174f
Remove unused JS globals for stdin/stdout/stderr (#8410)
sbc100 Apr 9, 2019
9b79ed1
Another NODEFS symlink test part which we should disable on MacOS (#8…
kripken Apr 10, 2019
3a2c80b
add note on heap pointers and wasm memory growth (#8423)
ericmandel Apr 10, 2019
e60da12
tests: Add assertions to verify correct decorator use. NFC. (#8424)
sbc100 Apr 10, 2019
69a35b4
Don't refer to object files are bitcode. NFC. (#8425)
sbc100 Apr 10, 2019
7ebca1b
DISABLE_DEPRECATED_FIND_EVENT_TARGET_BEHAVIOR in STRICT mode (#8413)
sbc100 Apr 10, 2019
7e73471
Fix test_dlfcn_self (#8427)
sbc100 Apr 10, 2019
f05c3d0
Apply --whole-archive to groups of libraries, not just individual one…
sbc100 Apr 10, 2019
fb9b1dd
Move all js library decisions to a single location (#8428)
kripken Apr 10, 2019
829a4db
benchmark improvements (#8414)
kripken Apr 10, 2019
8725504
Remove duplication between libc.a and libc_rt_wasm.a. NFC. (#8426)
sbc100 Apr 10, 2019
12a5ae5
Format NAMED_GLOBALS in generated JS. NFC. (#8432)
sbc100 Apr 11, 2019
dd50abc
yet more macos NODEFS disabling (#8435)
kripken Apr 11, 2019
a13f12d
Reorder when we render the pre js code in jsifier.js. (#8436)
kripken Apr 11, 2019
0439bc1
Remove some closure compiler hacks (#8437)
kripken Apr 11, 2019
a365dba
Fix STACKTOP/STACK_MAX variables for relocatable (#8434)
rianhunter Apr 12, 2019
629390f
emrun stability improvements (#8456)
kripken Apr 17, 2019
645bdcc
Fix filesystem/syscall optimization (#8438)
kripken Apr 19, 2019
6ceb773
Add everything but bulk-memory to wasm-as invocation (#8459)
tlively Apr 19, 2019
28606af
update changelog [ci skip]
kripken Apr 19, 2019
32d409f
Consistent handling `-l` and `-L` flags in `-Wl,` and `-Wl,@rsp` (#8462)
sbc100 Apr 19, 2019
cd07644
emscripten.py: optimize check_all_implemented. (#8453)
waterlike86 Apr 20, 2019
45427af
Enable ERROR_ON_MISSING_LIBRARIES by default (#8461)
sbc100 Apr 20, 2019
6ace2e9
[docs] Remove unnecessary comments (#8463)
waseemahmad31 Apr 20, 2019
375e65a
added closing brace for extern "C" in websocket.h (#8468)
arasouli91 Apr 21, 2019
44b7f4a
WebGL2 wait sync fixes (#8460)
coopersimon Apr 22, 2019
be1e99f
Accept -msimd128 instead of -s SIMD=1 and add feature test modes (#8478)
tlively Apr 24, 2019
7bb4aff
Add support for MAIN_MODULE/SIDE_MODULE with the wasm backend (#8431)
sbc100 Apr 24, 2019
38b0ea3
Change ino_t/off_t to 64-bits (#8467)
rianhunter Apr 24, 2019
32d5af6
Cleanup add_emscripten_metadata. NFC. (#8482)
sbc100 Apr 24, 2019
9d58245
update changelog [ci skip]
kripken Apr 24, 2019
34cebc7
Handle the get​Supported​Extensions() === null case (#8483)
hpohl Apr 24, 2019
6f080ae
Followup to #8438 - fix dependencies problems that PR created (#8464)
kripken Apr 25, 2019
2f6e69f
Fix conditional #include in js preprocessor (#8471)
kripken Apr 25, 2019
a795155
Rename C++ tests files to cpp extension. NFC. (#8479)
sbc100 Apr 25, 2019
60210f7
Add PYTHON to emmake and emconfigure commands when building sdl2-mixe…
sbc100 Apr 25, 2019
c805f33
Add pthread_setcanceltype to pthread stubs (#8480)
kripken Apr 25, 2019
57395a9
AUTODEBUG support for wasm files (#8472)
kripken Apr 25, 2019
10ba2cb
Remove assertion regarding exports['table'] (#8496)
sbc100 Apr 26, 2019
e92a55f
emrun stability fix (#8504)
kripken Apr 26, 2019
a89389d
Update binaryen and fix tests (#8499)
kripken Apr 26, 2019
d6b1d39
Skip test of wasm backend + relocatable + emu-fp-casts (#8508)
sbc100 Apr 26, 2019
38882fa
Improve dylink test cases (#8493)
sbc100 Apr 27, 2019
8d3b178
Fix WASM=0 warning in gen_struct_info (#8497)
sbc100 Apr 27, 2019
5e27ed1
Update emdump.py to be able to collapse templates under one, and show…
juj Apr 29, 2019
4a9a69a
Remove debug logging. NFC. (#8515)
sbc100 Apr 29, 2019
da96266
Enable rtti + dylib tests with wasm backend (#8510)
sbc100 Apr 29, 2019
4a1ab9e
If the user explicitly sets `FILESYSTEM` to 0, mark `SYSCALLS_REQUIRE…
kripken Apr 30, 2019
db9d9dc
build system gl libs with `-s USE_WEBGL2=1` only when necessary (#8512)
hujiajie Apr 30, 2019
a4eea28
test_other: Add helper function to verify expected failing commands. …
sbc100 Apr 30, 2019
117ae30
Fix broken telldir/seekdir testing (#8527)
sbc100 May 1, 2019
c25f8bc
Flesh out emscripten metadata (#8519)
rianhunter May 2, 2019
c814f17
Fix emscripten_GetProcAddress with various suffixes (#8430)
paraboul May 2, 2019
618caaa
Cleanup test_export_aliasee (#8537)
sbc100 May 2, 2019
4099f5b
testing: Add support for C source files in runner.do_run_in_out_file_…
sbc100 May 2, 2019
aed6acd
Don't export all when building with MAIN_MODULE=2 and wasm backend (#…
sbc100 May 3, 2019
7ca7823
Build gl separately for OFFSCREEN_FRAMEBUFFER (#8530)
kripken May 3, 2019
567697e
Cleanup `build_library` test helper (#8543)
sbc100 May 3, 2019
7d65c0c
Simplify handling of ll file generations in test code (#8378)
sbc100 May 4, 2019
f6a0888
Rename legacy GL emulation related sloppy GL extension lookup functio…
juj May 6, 2019
6cce904
SDL2: rebuild all projects that install SDL2 include files (#8546)
Beuc May 6, 2019
2dd1b7b
Fix getdents syscall to handle seekdir/telldir (#8540)
rianhunter May 7, 2019
cd4f62e
Prefer cwd argument to run_process over chdir. NFC. (#8556)
sbc100 May 7, 2019
5d7247b
Show a clear error on -o pointing to an invalid directory. fixes #854…
kripken May 8, 2019
b8d2bf1
Make STRICT=1 equivalent to EMCC_STRICT=1 in terms of legacy settings…
sbc100 May 8, 2019
3d856e2
LLVM wasm backend JS output using Binaryen wasm2js (#8501)
kripken May 8, 2019
fa0074e
Remove OUTLINING_LIMIT (#8562)
kripken May 8, 2019
0d0a3dd
Show a clear assertion failure if thread initialization fails (#8481)
VirtualTim May 9, 2019
c51f715
Don't run dlfcn tests on wasm2js (#8575)
kripken May 9, 2019
50b2dfe
Fix SDL2 LTO (#8580)
kripken May 10, 2019
2641ea8
Update expectations for test_binaryen_metadce_hello under wasm backen…
sbc100 May 10, 2019
dc54a7e
Update binaryen to version_84 (#8585)
sbc100 May 10, 2019
b9a594b
Add support for more detailed metadce expectations (#8583)
sbc100 May 10, 2019
3c17301
Update AUTHORS for #8591 (#8592)
nico May 13, 2019
5a3d982
Fix a typo in a comment (#8591)
nico May 13, 2019
83bca46
[docs] fix broken link in Filesystem-API.rst (#8593) [ci skip]
bryangingechen May 13, 2019
2398daa
Fix a typo in a PATH_FS dependency (#8587)
kripken May 13, 2019
f054894
style followups for #8501 (#8563)
kripken May 13, 2019
91b6972
Fix wasmlto* test modes on libraries (#8550)
kripken May 13, 2019
ef686fb
Tidy up js wrapper function generation. NFC. (#8603)
sbc100 May 14, 2019
0fd80a5
Add python3 version check (#8602)
sbc100 May 14, 2019
1a85480
Fix test_dlfcn_self test under wasm and wasm backend (#8531)
sbc100 May 14, 2019
ae9ddc4
Fix requestAnimationFrame in workers (#8586)
kripken May 14, 2019
5521ad9
OffscreenCanvas: do not automatically try to return contexts from a w…
kripken May 14, 2019
87770d1
OffscreenCanvas: if OFFSCREEN_FRAMEBUFFER is off, we don't provide co…
kripken May 14, 2019
02f7e58
Remove .commit() from OffscreenCanvas support (#8606)
kripken May 14, 2019
10ae6e2
Support minifyLocals in the JS optimizer even without asm.js. This wi…
kripken May 14, 2019
311ec74
Strip out trailing zeros from wasm mem init files. (#8607)
kripken May 15, 2019
d1aa9d0
fix a missed #if for supporting OFFSCREEN_FRAMEBUFFER being off while…
walkingeyerobot May 15, 2019
0f9ccc5
[docs] Mention default directories in Filesystem-API.rst (#8595)
bryangingechen May 15, 2019
507d879
Fix pthreads without OFFSCREEN_FRAMEBUFFER use of emscripten_webgl_ma…
kripken May 15, 2019
9fe43d6
Rename BINARYEN_ASYNC_COMPILATION -> WASM_ASYNC_COMPILATION (#8601)
sbc100 May 15, 2019
4aa04c5
Allow wasm object files as input, even when WASM_OBJECT_FILES=0 is se…
sbc100 May 16, 2019
f8cee72
Update fetch API docs as streaming downloads are Firefox Only (#8618)
VirtualTim May 16, 2019
80f8c93
Improve sanity.test_llvm. NFC. (#8626)
sbc100 May 16, 2019
e1d3dfe
Improve error messages around missing llvm install (#8625)
sbc100 May 17, 2019
23cdcfc
Adding proper fsync errors signaling for pipes. (#8631)
abujalski May 17, 2019
15cda75
Fix the dodgy call_sync_on_main_thread tests (#8621)
VirtualTim May 17, 2019
79a1955
Revert "Adding proper fsync errors signaling for pipes. (#8631)"
sbc100 May 17, 2019
82292b5
Allow emscripten to use the minimal ubsan runtime (#8617)
quantum5 May 17, 2019
3c9e831
1.38.32
kripken May 20, 2019
eb37140
Return detached threads to the pool (#8286)
VirtualTim May 20, 2019
ad74b35
Fix temp file handling in js_optimizer (#8642)
kripken May 20, 2019
a8c8706
Don't try to handle complex things in dynamicAlloc, abort() on errors…
kripken May 21, 2019
939695a
Merge branch 'incoming' into patch-3
VirtualTim May 23, 2019
79659e6
Remove accidentally added whitespace
VirtualTim May 23, 2019
6ab9a00
Changed comment to reflect change in behaviour
VirtualTim May 24, 2019
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
9 changes: 7 additions & 2 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2771,14 +2771,19 @@ def modularize():
# after document.currentScript is gone, so we save it.
# (when MODULARIZE_INSTANCE, an instance is created
# immediately anyhow, like in non-modularize mode)
if shared.Settings.EXPORT_ES6:
script_url = "import.meta.url"
VirtualTim marked this conversation as resolved.
Show resolved Hide resolved
else:
script_url = "typeof document !== 'undefined' && document.currentScript ? document.currentScript.src : undefined"
src = '''
var %(EXPORT_NAME)s = (function() {
var _scriptDir = typeof document !== 'undefined' && document.currentScript ? document.currentScript.src : undefined;
var _scriptDir = %(script_url)s;
return (%(src)s);
})();
''' % {
'EXPORT_NAME': shared.Settings.EXPORT_NAME,
'src': src
'src': src,
'script_url': script_url
}
else:
# Create the MODULARIZE_INSTANCE instance
Expand Down
4 changes: 4 additions & 0 deletions emscripten.py
Original file line number Diff line number Diff line change
Expand Up @@ -1666,6 +1666,10 @@ def create_receiving(function_table_data, function_tables_defs, exported_impleme

shared.Settings.MODULE_EXPORTS = module_exports = exported_implemented_functions + function_tables(function_table_data)

# Should only need to do this in the main thread.
Copy link
Member

Choose a reason for hiding this comment

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

Please clarify this in the comment. I believe the issue is that when assertions are on, we emit code that is incompatible with ES6 currently? Can add a TODO in the comment to fix that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good point, I missed the comment what I changed the behaviour.

if shared.Settings.USE_PTHREADS:
receiving = "if (!ENVIRONMENT_IS_PTHREAD) {\n" + receiving + "}\n"
Copy link
Collaborator

Choose a reason for hiding this comment

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

But the stuff added to receiving below we do want on non-main-threads? Can you explain, perhaps write more in the comment here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

In my testing it seems like these functions were already shared with the non-main-threads by the main thread. Calling these from a non-main-thread attempted to redefine them (as the same thing). No idea if that's how it's supposed to work.

Copy link
Member

Choose a reason for hiding this comment

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

Based on where this line is, it just puts the runtime assertions stuff behind this check. What's special about them? As @sbc100 said, I think this should all be needed in pthreads too - do you have a test that fails without this change?

Copy link
Collaborator Author

@VirtualTim VirtualTim Apr 4, 2019

Choose a reason for hiding this comment

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

When I tried to run the code before this change I was getting errors about the vars in this block being redefined. It seems like when initialising a thread these functions have already been defined (not sure how). Normally var redefinition isn't an issue as everything gets hoisted, so I assumed it was an ES6 module thing. I'll have to do some more testing, I can't remember exactly what error I was trying to avoid.

Although looking at that again I should add a check for if receiving is an empty string.

Edit: The actual error I get is:
Uncaught (in promise) TypeError: Cannot assign to read only property '__ZSt18uncaught_exceptionv' of object '[object Object]'
__ZSt18uncaught_exceptionv just happens to be the first in the list, it seems to error on all of them, causing initialising pthreads to fail.
I had assumed that these were getting defined twice, once in the main thread, once in workers, but it looks like you can't just redefine things like that in an ES6 module. I don't know why. I'll change the settings check to if shared.Settings.USE_PTHREADS and shared.Settings.EXPORT_ES6 and receiving: if I can't figure out how to get this working.

Copy link
Member

Choose a reason for hiding this comment

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

That's a very odd error - I don't think we use read-only properties anywhere?

Copy link
Member

@kripken kripken May 14, 2019

Choose a reason for hiding this comment

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

Thanks, and sorry for my mistake earlier!

Ok, what I think is happening here is: when loaded as an ES6 module, the wasm exports object is an ES6 module export object, or behaves like one, and those are more "static" than normal JS objects. And we try to replace them with wrappers to add extra runtime error handling, and that fails.

But that doesn't explain why this is a problem only on workers but not on the main thread. It sounds like that could be a browser bug, in fact. That asm object is a wasm export object on both locations, so it should behave the same? Unless somehow only the worker is an ES6 context - I'm not too familiar with how those are created?

Copy link
Member

Choose a reason for hiding this comment

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

Regardless, we can split this out from the rest of this PR, if it's separable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah I don't really know too much about all that stuff either.
The change on that line allows EXPORT_ES6 + USE_PTHREADS to work with ASSERTIONS != 0. I think the best thing is to merge this PR as is, since by default ASSERTIONS=1.

Perhaps after this is merged another bug can be opened to fix those assertions?\

If you'd prefer I'm happy to remove that line, and just say that currently EXPORT_ES6 + USE_PTHREADS only work with ASSERTIONS=0.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, let's separate it out - a comment that says something like your last line there sounds good enough for now.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

OK, I changed it to an assert, since the next line would just throw anyway. At least this way the developer gets a clear message about this not working. Once this is merged I'll open another issue to fix USE_PTHREADS + EXPORT_ES6 +ASSERTIONS.


if not shared.Settings.SWAPPABLE_ASM_MODULE:
if shared.Settings.DECLARE_ASM_MODULE_EXPORTS:
imported_exports = [s for s in module_exports if s not in initializers]
Expand Down
7 changes: 3 additions & 4 deletions src/shell_pthreads.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
// 3) We could be an application pthread running in a worker. (ENVIRONMENT_IS_WORKER == true and ENVIRONMENT_IS_PTHREAD == true)
#if USE_PTHREADS

if (typeof ENVIRONMENT_IS_PTHREAD === 'undefined') {
// ENVIRONMENT_IS_PTHREAD=true will have been preset in worker.js. Make it false in the main runtime thread.
// N.B. this line needs to appear without 'var' keyword to avoid 'var hoisting' from occurring. (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var)
ENVIRONMENT_IS_PTHREAD = false;
// ENVIRONMENT_IS_PTHREAD=true will have been preset in worker.js. Make it false in the main runtime thread.
var ENVIRONMENT_IS_PTHREAD = Module.ENVIRONMENT_IS_PTHREAD || false;
if (!ENVIRONMENT_IS_PTHREAD) {
var PthreadWorkerInit = {}; // Collects together variables that are needed at initialization time for the web workers that host pthreads.
}
#if MODULARIZE
Expand Down
19 changes: 17 additions & 2 deletions src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ var buffer; // All pthreads share the same Emscripten HEAP as SharedArrayBuffer
var DYNAMICTOP_PTR = 0;
var DYNAMIC_BASE = 0;

var ENVIRONMENT_IS_PTHREAD = true;
VirtualTim marked this conversation as resolved.
Show resolved Hide resolved
var PthreadWorkerInit = {};

// performance.now() is specced to return a wallclock time in msecs since that Web Worker/main thread launched. However for pthreads this can cause
Expand All @@ -35,6 +34,10 @@ var __performance_now_clock_drift = 0;
// Cannot use console.log or console.error in a web worker, since that would risk a browser deadlock! https://bugzilla.mozilla.org/show_bug.cgi?id=1049091
// Therefore implement custom logging facility for threads running in a worker, which queue the messages to main thread to print.
var Module = {};
#if EXPORT_ES6
Copy link
Member

Choose a reason for hiding this comment

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

i think we should also emit these lines if MODULARIZE as we do assign to them there as well (line 144/158), and for the same reason?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The difference is that in normal (aka sloppy) JS you can define things without var/const/let. So defining PThread like PThread = Module['PThread']; is perfectly valid. However this is not valid inside an ES6 module as that enforces strict mode. That's why I had to declare them here.
I can change #if EXPORT_ES6 to #if MODULARIZE if you want, but there may be a reason that it was initially done like that and I didn't want to break existing modularize code.

var PThread;
var HEAPU32;
#endif

#if ASSERTIONS
function assert(condition, text) {
Expand Down Expand Up @@ -128,15 +131,26 @@ this.onmessage = function(e) {
#endif

{{{ makeAsmExportAndGlobalAssignTargetInPthread('PthreadWorkerInit') }}} = e.data.PthreadWorkerInit;
Module['ENVIRONMENT_IS_PTHREAD'] = true;

#if MODULARIZE && EXPORT_ES6
import(e.data.urlOrBlob).then(function({{{ EXPORT_NAME }}}) {
Module = {{{ EXPORT_NAME }}}.default(Module);
PThread = Module['PThread'];
HEAPU32 = Module['HEAPU32'];
#if !ASMFS
if (typeof FS !== 'undefined' && typeof FS.createStandardStreams === 'function') FS.createStandardStreams();
#endif
postMessage({ cmd: 'loaded' });
});
#else
if (typeof e.data.urlOrBlob === 'string') {
importScripts(e.data.urlOrBlob);
} else {
var objectUrl = URL.createObjectURL(e.data.urlOrBlob);
importScripts(objectUrl);
URL.revokeObjectURL(objectUrl);
}

#if MODULARIZE
Module = {{{ EXPORT_NAME }}}(Module);
PThread = Module['PThread'];
Expand All @@ -147,6 +161,7 @@ this.onmessage = function(e) {
if (typeof FS !== 'undefined' && typeof FS.createStandardStreams === 'function') FS.createStandardStreams();
#endif
postMessage({ cmd: 'loaded' });
#endif
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 // MODULARIZE && EXPORT_ES6 so it's easier to match up the if and its end

} else if (e.data.cmd === 'objectTransfer') {
PThread.receiveObjectTransfer(e.data);
} else if (e.data.cmd === 'run') { // This worker was idle, and now should start executing its pthread entry point.
Expand Down