Skip to content

Commit

Permalink
fix: Avoid TypeError raised by new Buffer() (#303)
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshihitoh committed Jul 25, 2024
1 parent b254593 commit be24437
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 42 deletions.
21 changes: 8 additions & 13 deletions cpp/src/binding/emscripten/zstd-binding.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <emscripten/bind.h>
#include <emscripten/val.h>

#include "../../zstd-codec.h"
#include "../../zstd-dict.h"
Expand Down Expand Up @@ -59,13 +60,13 @@ static val heap_buffer()
template<typename T>
static size_t copy_to_vector(Vec<T>& dest, const val& src)
{
const auto length = src["length"].as<unsigned int>();

val memory = heap_buffer();
val memory_view = src["constructor"].new_(memory, reinterpret_cast<uintptr_t>(dest.data()), length);
// TODO: use convertJSArrayToNumberVector function after upgrading Emscripten.
// The function is available since 2.0.7
const size_t length = src["length"].as<size_t>();
dest.resize(length);

dest.reserve(length);
memory_view.call<void>("set", src);
val memoryView{ typed_memory_view(length, dest.data()) };
memoryView.call<void>("set", src);

return length;
}
Expand Down Expand Up @@ -118,13 +119,7 @@ void Dummy()

void CloneToVector(Vec<u8>& dest, val src)
{
const auto length = src["length"].as<unsigned int>();
dest.resize(length);

val memory = heap_buffer();
val memory_view = src["constructor"].new_(memory, reinterpret_cast<uintptr_t>(dest.data()), length);

memory_view.call<void>("set", src);
copy_to_vector(dest, src);
}


Expand Down
2 changes: 1 addition & 1 deletion js/lib/zstd-codec-binding-wasm.js

Large diffs are not rendered by default.

64 changes: 36 additions & 28 deletions js/lib/zstd-codec-binding.js

Large diffs are not rendered by default.

0 comments on commit be24437

Please sign in to comment.