Redirect calls to printf and fprintf #315
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
When the interpreted code calls an external function, LLVM will use
dlsym()
to find the target in the current process. This works wellfor functions from libc which are always loaded.
However this also outputs the result of
printf()
andfprintf()
to theterminal and does not forward it to the user. This is very unintuitive
because values piped into
std::cout
andstd::cerr
correctly appear inthe browser.
As a solution, use
llvm::sys::DynamicLibrary::AddSymbol()
to injectcustom implementations into the process of finding external functions.
Once hooked up, the implementations format the passed arguments into
a C++ string and forward the result to the C++ streams that correctly
forward to the user.
Fixes #112