Last active
August 29, 2015 14:00
-
-
Save skalee/11318312 to your computer and use it in GitHub Desktop.
In Emscripten, writing to `std::cout` in some cases fails with "TypeError: Cannot read property 'fd' of null".
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mergeInto(LibraryManager.library, { | |
try_from_js: function() { | |
Module.ccall('print_it', 'number', ['string'], ["From `try_from_js` it works."]); | |
setTimeout(function() { | |
// It could be also nodejs' readline's `on('line', …)` callback. | |
Module.ccall('print_it', 'number', ['string'], ["From `setTimeout` callback it fails."]); | |
}, 0); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TARGET = why-not-works.js | |
OBJS = src.bc | |
HLPS = helper.js | |
CXX = emcc | |
LDFLAGS = \ | |
-s EXPORTED_FUNCTIONS="['_print_it', '_main']" \ | |
--js-library helper.js | |
.PHONY: run clean default | |
$(TARGET): $(OBJS) $(HLPS) | |
$(CXX) -o $@ $(OBJS) $(LDFLAGS) | |
%.bc: %.cpp | |
$(CXX) $< -o $@ | |
default: $(TARGET) | |
clean: | |
- rm $(TARGET) *.bc | |
run: $(TARGET) | |
node $(TARGET) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ make clean run | |
rm why-not-works.js *.bc | |
emcc src.cpp -o src.bc | |
emcc -o why-not-works.js src.bc -s EXPORTED_FUNCTIONS="['_print_it', '_main']" --js-library helper.js | |
node why-not-works.js | |
* From `main` it works. | |
* From `try_from_js` it works. | |
/Users/skale/Projekty/cannot-read-fd/why-not-works.js:7959 | |
$67,$90,$93,$102,$147) } catch(e) { if (typeof e != "number") throw e; if (ABO | |
^ | |
TypeError: Cannot read property 'fd' of null | |
at _fileno (/Users/skale/Projekty/cannot-read-fd/why-not-works.js:5597:41) | |
at _fwrite (/Users/skale/Projekty/cannot-read-fd/why-not-works.js:5603:16) | |
at Array.__ZNSt3__111__stdoutbufIcE6xsputnEPKci [as 700] (/Users/skale/Projekty/cannot-read-fd/why-not-works.js:9590:9) | |
at __ZNSt3__116__pad_and_outputIcNS_11char_traitsIcEEEENS_19ostreambuf_iteratorIT_T0_EES6_PKS4_S8_S8_RNS_8ios_baseES4_ (/Users/skale/Projekty/cannot-read-fd/why-not-works.js:8519:31) | |
at /Users/skale/Projekty/cannot-read-fd/why-not-works.js:7959:44 | |
at __ZNSt3__1lsINS_11char_traitsIcEEEERNS_13basic_ostreamIcT_EES6_PKc (/Users/skale/Projekty/cannot-read-fd/why-not-works.js:7959:290) | |
at _print_it (/Users/skale/Projekty/cannot-read-fd/why-not-works.js:7742:9) | |
at ccallFunc (/Users/skale/Projekty/cannot-read-fd/why-not-works.js:612:24) | |
at Object.ccall (/Users/skale/Projekty/cannot-read-fd/why-not-works.js:569:10) | |
at null._onTimeout (/Users/skale/Projekty/cannot-read-fd/why-not-works.js:2302:16) | |
make: *** [run] Error 8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <iostream> | |
extern "C" void try_from_js(); | |
extern "C" int print_it(const char* str) { | |
std::cout << " * " << str << std::endl; | |
return 0; | |
} | |
int main(int argc, char* argv[]) { | |
print_it("From `main` it works."); | |
try_from_js(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment