diff options
Diffstat (limited to 'core/libdeno/binding.cc')
-rw-r--r-- | core/libdeno/binding.cc | 85 |
1 files changed, 30 insertions, 55 deletions
diff --git a/core/libdeno/binding.cc b/core/libdeno/binding.cc index 80f733b4f..8001534ff 100644 --- a/core/libdeno/binding.cc +++ b/core/libdeno/binding.cc @@ -167,57 +167,33 @@ v8::Local<v8::Uint8Array> ImportBuf(DenoIsolate* d, deno_buf buf) { return v8::Local<v8::Uint8Array>(); } - if (buf.alloc_ptr == nullptr) { - // If alloc_ptr isn't set, we memcpy. - // This is currently used for flatbuffers created in Rust. - - // To avoid excessively allocating new ArrayBuffers, we try to reuse a - // single global ArrayBuffer. The caveat is that users must extract data - // from it before the next tick. We only do this for ArrayBuffers less than - // 1024 bytes. - v8::Local<v8::ArrayBuffer> ab; - void* data; - if (buf.data_len > GLOBAL_IMPORT_BUF_SIZE) { - // Simple case. We allocate a new ArrayBuffer for this. - ab = v8::ArrayBuffer::New(d->isolate_, buf.data_len); - data = ab->GetContents().Data(); + // To avoid excessively allocating new ArrayBuffers, we try to reuse a single + // global ArrayBuffer. The caveat is that users must extract data from it + // before the next tick. We only do this for ArrayBuffers less than 1024 + // bytes. + v8::Local<v8::ArrayBuffer> ab; + void* data; + if (buf.data_len > GLOBAL_IMPORT_BUF_SIZE) { + // Simple case. We allocate a new ArrayBuffer for this. + ab = v8::ArrayBuffer::New(d->isolate_, buf.data_len); + data = ab->GetContents().Data(); + } else { + // Fast case. We reuse the global ArrayBuffer. + if (d->global_import_buf_.IsEmpty()) { + // Lazily initialize it. + DCHECK_NULL(d->global_import_buf_ptr_); + ab = v8::ArrayBuffer::New(d->isolate_, GLOBAL_IMPORT_BUF_SIZE); + d->global_import_buf_.Reset(d->isolate_, ab); + d->global_import_buf_ptr_ = ab->GetContents().Data(); } else { - // Fast case. We reuse the global ArrayBuffer. - if (d->global_import_buf_.IsEmpty()) { - // Lazily initialize it. - DCHECK_NULL(d->global_import_buf_ptr_); - ab = v8::ArrayBuffer::New(d->isolate_, GLOBAL_IMPORT_BUF_SIZE); - d->global_import_buf_.Reset(d->isolate_, ab); - d->global_import_buf_ptr_ = ab->GetContents().Data(); - } else { - DCHECK(d->global_import_buf_ptr_); - ab = d->global_import_buf_.Get(d->isolate_); - } - data = d->global_import_buf_ptr_; + DCHECK(d->global_import_buf_ptr_); + ab = d->global_import_buf_.Get(d->isolate_); } - memcpy(data, buf.data_ptr, buf.data_len); - auto view = v8::Uint8Array::New(ab, 0, buf.data_len); - return view; - } else { - auto ab = v8::ArrayBuffer::New( - d->isolate_, reinterpret_cast<void*>(buf.alloc_ptr), buf.alloc_len, - v8::ArrayBufferCreationMode::kInternalized); - auto view = - v8::Uint8Array::New(ab, buf.data_ptr - buf.alloc_ptr, buf.data_len); - return view; + data = d->global_import_buf_ptr_; } -} - -static deno_buf GetContents(v8::Isolate* isolate, - v8::Local<v8::ArrayBufferView> view) { - auto ab = view->Buffer(); - auto contents = ab->GetContents(); - deno_buf buf; - buf.alloc_ptr = reinterpret_cast<uint8_t*>(contents.Data()); - buf.alloc_len = contents.ByteLength(); - buf.data_ptr = buf.alloc_ptr + view->ByteOffset(); - buf.data_len = view->ByteLength(); - return buf; + memcpy(data, buf.data_ptr, buf.data_len); + auto view = v8::Uint8Array::New(ab, 0, buf.data_len); + return view; } // Sets the recv_ callback. @@ -247,13 +223,12 @@ void Send(const v8::FunctionCallbackInfo<v8::Value>& args) { v8::HandleScope handle_scope(isolate); - deno_buf control = {nullptr, 0u, nullptr, 0u}; - if (args.Length() > 0) { - v8::Local<v8::Value> control_v = args[0]; - if (control_v->IsArrayBufferView()) { - control = - GetContents(isolate, v8::Local<v8::ArrayBufferView>::Cast(control_v)); - } + deno_buf control = {nullptr, 0}; + if (args[0]->IsArrayBufferView()) { + auto view = v8::Local<v8::ArrayBufferView>::Cast(args[0]); + auto data = + reinterpret_cast<uint8_t*>(view->Buffer()->GetContents().Data()); + control = {data + view->ByteOffset(), view->ByteLength()}; } PinnedBuf zero_copy = |