summaryrefslogtreecommitdiff
path: root/src/binaryen-c.cpp
diff options
context:
space:
mode:
authorMax Graey <maxgraey@gmail.com>2022-07-29 07:15:35 +0300
committerGitHub <noreply@github.com>2022-07-29 04:15:35 +0000
commitd02c260619e5d068b6893d4948de0487d0f1f66d (patch)
treebeba5a15fb0ace5bd34a4135efecc54deb754bc6 /src/binaryen-c.cpp
parent0cd9fb599fc5a44df7774d5f180d912ccab8c941 (diff)
downloadbinaryen-d02c260619e5d068b6893d4948de0487d0f1f66d.tar.gz
binaryen-d02c260619e5d068b6893d4948de0487d0f1f66d.tar.bz2
binaryen-d02c260619e5d068b6893d4948de0487d0f1f66d.zip
[JS Api] Reuse C-Api for emitText and emitStackIR (#4832)
Make the C API match the JS API and fix an old bug where extra newlines were emitted.
Diffstat (limited to 'src/binaryen-c.cpp')
-rw-r--r--src/binaryen-c.cpp45
1 files changed, 24 insertions, 21 deletions
diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp
index 682272201..68158cdf7 100644
--- a/src/binaryen-c.cpp
+++ b/src/binaryen-c.cpp
@@ -4009,8 +4009,8 @@ void BinaryenModulePrint(BinaryenModuleRef module) {
std::cout << *(Module*)module;
}
-void BinaryenModulePrintStackIR(BinaryenModuleRef module) {
- wasm::printStackIR(std::cout, (Module*)module);
+void BinaryenModulePrintStackIR(BinaryenModuleRef module, bool optimize) {
+ wasm::printStackIR(std::cout, (Module*)module, optimize);
}
void BinaryenModulePrintAsmjs(BinaryenModuleRef module) {
@@ -4197,11 +4197,12 @@ size_t BinaryenModuleWriteText(BinaryenModuleRef module,
size_t BinaryenModuleWriteStackIR(BinaryenModuleRef module,
char* output,
- size_t outputSize) {
+ size_t outputSize,
+ bool optimize) {
// use a stringstream as an std::ostream. Extract the std::string
// representation, and then store in the output.
std::stringstream ss;
- wasm::printStackIR(ss, (Module*)module);
+ wasm::printStackIR(ss, (Module*)module, optimize);
const auto temp = ss.str();
const auto ctemp = temp.c_str();
@@ -4238,40 +4239,42 @@ BinaryenModuleAllocateAndWrite(BinaryenModuleRef module,
char* sourceMap = nullptr;
if (sourceMapUrl) {
auto str = os.str();
- sourceMap = (char*)malloc(str.length() + 1);
- std::copy_n(str.c_str(), str.length() + 1, sourceMap);
+ const size_t len = str.length() + 1;
+ sourceMap = (char*)malloc(len);
+ std::copy_n(str.c_str(), len, sourceMap);
}
return {binary, buffer.size(), sourceMap};
}
char* BinaryenModuleAllocateAndWriteText(BinaryenModuleRef module) {
- std::stringstream ss;
+ std::ostringstream os;
bool colors = Colors::isEnabled();
Colors::setEnabled(false); // do not use colors for writing
- ss << *(Module*)module;
+ os << *(Module*)module;
Colors::setEnabled(colors); // restore colors state
- const std::string out = ss.str();
- const int len = out.length() + 1;
- char* cout = (char*)malloc(len);
- strncpy(cout, out.c_str(), len);
- return cout;
+ auto str = os.str();
+ const size_t len = str.length() + 1;
+ char* output = (char*)malloc(len);
+ std::copy_n(str.c_str(), len, output);
+ return output;
}
-char* BinaryenModuleAllocateAndWriteStackIR(BinaryenModuleRef module) {
- std::stringstream ss;
+char* BinaryenModuleAllocateAndWriteStackIR(BinaryenModuleRef module,
+ bool optimize) {
+ std::ostringstream os;
bool colors = Colors::isEnabled();
Colors::setEnabled(false); // do not use colors for writing
- wasm::printStackIR(ss, (Module*)module);
+ wasm::printStackIR(os, (Module*)module, optimize);
Colors::setEnabled(colors); // restore colors state
- const std::string out = ss.str();
- const int len = out.length() + 1;
- char* cout = (char*)malloc(len);
- strncpy(cout, out.c_str(), len);
- return cout;
+ auto str = os.str();
+ const size_t len = str.length() + 1;
+ char* output = (char*)malloc(len);
+ std::copy_n(str.c_str(), len, output);
+ return output;
}
BinaryenModuleRef BinaryenModuleRead(char* input, size_t inputSize) {