Skip to content

Commit b0df148

Browse files
gahaaspthier
authored and
pthier
committed
[fastapi] Stop using deprecated FastApiTypedArray (nodejs#202)
* [fastapi] Stop using deprecated FastApiTypedArray * [fastapi] Stop using deprecated FastApiTypedArray This PR changes the signature of `FastCopy` and `WriteString` to not use `FastApiTypedArray` anymore. Instead the buffer is received as `Local<Value>`, and the type check is done in node instead of generated code. The data of the buffers is extracted the same as for slow calls. For the source of `FastCopy` it would also be possible to use the new V8 API `ArrayBufferView::GetContents()`, but I thought it's better for now to remain close to the regular implementation. For the other buffers this API cannot be used because it does not provide write access to the data, as it copies the data if it's stored on the heap.
1 parent 2c185ff commit b0df148

File tree

2 files changed

+36
-25
lines changed

2 files changed

+36
-25
lines changed

src/node_buffer.cc

+29-20
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,15 @@
4242
#include "nbytes.h"
4343

4444
#define THROW_AND_RETURN_UNLESS_BUFFER(env, obj) \
45-
THROW_AND_RETURN_IF_NOT_BUFFER(env, obj, "argument") \
45+
THROW_AND_RETURN_IF_NOT_BUFFER(env, obj, "argument")
46+
47+
#define THROW_AND_RETURN_VAL_UNLESS_BUFFER(isolate, val, prefix, retval) \
48+
do { \
49+
if (!Buffer::HasInstance(val)) { \
50+
node::THROW_ERR_INVALID_ARG_TYPE(isolate, prefix " must be a buffer"); \
51+
return retval; \
52+
} \
53+
} while (0)
4654

4755
#define THROW_AND_RETURN_IF_OOB(r) \
4856
do { \
@@ -61,7 +69,6 @@ using v8::BackingStore;
6169
using v8::BackingStoreInitializationMode;
6270
using v8::Context;
6371
using v8::EscapableHandleScope;
64-
using v8::FastApiTypedArray;
6572
using v8::FunctionCallbackInfo;
6673
using v8::Global;
6774
using v8::HandleScope;
@@ -582,19 +589,17 @@ void SlowCopy(const FunctionCallbackInfo<Value>& args) {
582589

583590
// Assume caller has properly validated args.
584591
uint32_t FastCopy(Local<Value> receiver,
585-
const v8::FastApiTypedArray<uint8_t>& source,
586-
const v8::FastApiTypedArray<uint8_t>& target,
592+
Local<Value> source_obj,
593+
Local<Value> target_obj,
587594
uint32_t target_start,
588595
uint32_t source_start,
589-
uint32_t to_copy) {
590-
uint8_t* source_data;
591-
CHECK(source.getStorageIfAligned(&source_data));
592-
593-
uint8_t* target_data;
594-
CHECK(target.getStorageIfAligned(&target_data));
595-
596-
memmove(target_data + target_start, source_data + source_start, to_copy);
596+
uint32_t to_copy,
597+
// NOLINTNEXTLINE(runtime/references) This is V8 api.
598+
v8::FastApiCallbackOptions& options) {
599+
ArrayBufferViewContents<char> source(source_obj);
600+
SPREAD_BUFFER_ARG(target_obj, target);
597601

602+
memmove(target_data + target_start, source.data() + source_start, to_copy);
598603
return to_copy;
599604
}
600605

@@ -1476,21 +1481,25 @@ void SlowWriteString(const FunctionCallbackInfo<Value>& args) {
14761481

14771482
template <encoding encoding>
14781483
uint32_t FastWriteString(Local<Value> receiver,
1479-
const v8::FastApiTypedArray<uint8_t>& dst,
1484+
Local<Value> dst,
14801485
const v8::FastOneByteString& src,
14811486
uint32_t offset,
1482-
uint32_t max_length) {
1483-
uint8_t* dst_data;
1484-
CHECK(dst.getStorageIfAligned(&dst_data));
1485-
CHECK(offset <= dst.length());
1486-
CHECK(dst.length() - offset <= std::numeric_limits<uint32_t>::max());
1487+
uint32_t max_length,
1488+
// NOLINTNEXTLINE(runtime/references) This is V8 api.
1489+
v8::FastApiCallbackOptions& options) {
1490+
THROW_AND_RETURN_VAL_UNLESS_BUFFER(options.isolate, dst, "dst", 0);
1491+
SPREAD_BUFFER_ARG(dst, dst_buffer);
1492+
CHECK(dst_buffer_length <=
1493+
static_cast<size_t>(std::numeric_limits<uint32_t>::max()));
1494+
uint32_t dst_size = static_cast<uint32_t>(dst_buffer_length);
1495+
CHECK(offset <= dst_size);
14871496
TRACK_V8_FAST_API_CALL("buffer.writeString");
14881497

14891498
return WriteOneByteString<encoding>(
14901499
src.data,
14911500
src.length,
1492-
reinterpret_cast<char*>(dst_data + offset),
1493-
std::min<uint32_t>(dst.length() - offset, max_length));
1501+
reinterpret_cast<char*>(dst_buffer_data + offset),
1502+
std::min<uint32_t>(dst_size - offset, max_length));
14941503
}
14951504

14961505
static v8::CFunction fast_write_string_ascii(

src/node_external_reference.h

+7-5
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,20 @@ using CFunctionWithBool = void (*)(v8::Local<v8::Value>,
5656

5757
using CFunctionWriteString =
5858
uint32_t (*)(v8::Local<v8::Value> receiver,
59-
const v8::FastApiTypedArray<uint8_t>& dst,
59+
v8::Local<v8::Value> dst,
6060
const v8::FastOneByteString& src,
6161
uint32_t offset,
62-
uint32_t max_length);
62+
uint32_t max_length,
63+
v8::FastApiCallbackOptions&);
6364

6465
using CFunctionBufferCopy =
6566
uint32_t (*)(v8::Local<v8::Value> receiver,
66-
const v8::FastApiTypedArray<uint8_t>& source,
67-
const v8::FastApiTypedArray<uint8_t>& target,
67+
v8::Local<v8::Value> source,
68+
v8::Local<v8::Value> target,
6869
uint32_t target_start,
6970
uint32_t source_start,
70-
uint32_t to_copy);
71+
uint32_t to_copy,
72+
v8::FastApiCallbackOptions&);
7173

7274
// This class manages the external references from the V8 heap
7375
// to the C++ addresses in Node.js.

0 commit comments

Comments
 (0)