The program is showing an issue to send 2000 rows of text to C++ code in Web Assembly I tried to send all the content together and failed. I decided to send line by line, it sent only up to 1400
- Maybe there is a better solution to check why it has delays and don't push another function to C++. Maybe it works like async await
- How to check why this error happened, still new in this area, and doesn't knows about debugging and error handling.
- Maybe I have to extend the heap/stack memory for 1 Txt file usage
- Maybe It is possible to transfer big Txt file data as binary.
The problematic code, sending line by line, javascript:
// content = (long txt file)
const rows = contents.split('\n');
// rows.length = 2000
for (let i = 0; i < row.length ; ++i)
{
Module.set_line(rows[i]);
}
My solution is to sleep every 1000 lines for 2ms. javascript:
const delay_ns = 2000;
const divide = 1000;
const row_length = rows.length;
const remained_sections = Math.ceil(row_length / divide) - 1;
let i = 0;
for (;i < divide && i < row_length ; ++i)
{
Module.set_line(rows[i]);
}
for (let j = 0; j < remained_sections; ++j)
{
const newmaxi = divides * j;
setTimeout(function() {
for (; i < newmaxi && i < row_lengths ; ++i)
{
Module.set_line(rows[i]);
}
}, delay_ns);
C++
static std::string text_total{};
void set_line(const std::string& line)
{
text_total+= line;
}
EMSCRIPTEN_BINDINGS(lines_data_functions)
{
emscripten::function("set_line", &set_line);
};
bash simplified code
emcc main.cpp -o main_wasm.js --emrun --std=c++17 --bind