GeezerVison is showing github.com — src: migrate to new V8 ArrayBuffer API · e7dal/node@4f523c2
File treeExpand file treeCollapse file tree Open diff view settings addons/worker-buffer-callback js-native-api/test_typedarray node-api/test_worker_buffer_callback
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: src/env-inl.h +13-3Lines changed: 13 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -983,10 +983,20 @@ inline v8::MaybeLocal<v8::Object> AllocatedBuffer::ToBuffer() { | 983 | 983 | inline v8::Local<v8::ArrayBuffer> AllocatedBuffer::ToArrayBuffer() { | 984 | 984 | CHECK_NOT_NULL(env_); | 985 | 985 | uv_buf_t buf = release(); | | 986 | + auto callback = [](void* data, size_t length, void* deleter_data){ | | 987 | + CHECK_NOT_NULL(deleter_data); | | 988 | + | | 989 | + static_cast<v8::ArrayBuffer::Allocator*>(deleter_data) | | 990 | + ->Free(data, length); | | 991 | + }; | | 992 | + std::unique_ptr<v8::BackingStore> backing = | | 993 | + v8::ArrayBuffer::NewBackingStore(buf.base, | | 994 | + buf.len, | | 995 | + callback, | | 996 | + env_->isolate() | | 997 | + ->GetArrayBufferAllocator()); | 986 | 998 | return v8::ArrayBuffer::New(env_->isolate(), | 987 | | - buf.base, | 988 | | - buf.len, | 989 | | - v8::ArrayBufferCreationMode::kInternalized); | | 999 | + std::move(backing)); | 990 | 1000 | } | 991 | 1001 |
| 992 | 1002 | inline void Environment::ThrowError(const char* errmsg) { | |
Collapse file Copy file name to clipboardExpand all lines: src/js_native_api_v8.cc +11-1Lines changed: 11 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -2612,8 +2612,18 @@ napi_status napi_create_external_arraybuffer(napi_env env, | 2612 | 2612 | CHECK_ARG(env, result); | 2613 | 2613 |
| 2614 | 2614 | v8::Isolate* isolate = env->isolate; | | 2615 | + // The buffer will be freed with v8impl::ArrayBufferReference::New() | | 2616 | + // below, hence this BackingStore does not need to free the buffer. | | 2617 | + std::unique_ptr<v8::BackingStore> backing = | | 2618 | + v8::ArrayBuffer::NewBackingStore(external_data, | | 2619 | + byte_length, | | 2620 | + [](void*, size_t, void*){}, | | 2621 | + nullptr); | 2615 | 2622 | v8::Local<v8::ArrayBuffer> buffer = | 2616 | | - v8::ArrayBuffer::New(isolate, external_data, byte_length); | | 2623 | + v8::ArrayBuffer::New(isolate, std::move(backing)); | | 2624 | + // TODO(thangktran): drop this check when V8 is pumped to 8.0 . | | 2625 | + if (!buffer->IsExternal()) | | 2626 | + buffer->Externalize(buffer->GetBackingStore()); | 2617 | 2627 | v8::Maybe<bool> marked = env->mark_arraybuffer_as_untransferable(buffer); | 2618 | 2628 | CHECK_MAYBE_NOTHING(env, marked, napi_generic_failure); | 2619 | 2629 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/node_buffer.cc +41-11Lines changed: 41 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -47,8 +47,8 @@ namespace node { | 47 | 47 | namespace Buffer { | 48 | 48 |
| 49 | 49 | using v8::ArrayBuffer; | 50 | | -using v8::ArrayBufferCreationMode; | 51 | 50 | using v8::ArrayBufferView; | | 51 | +using v8::BackingStore; | 52 | 52 | using v8::Context; | 53 | 53 | using v8::EscapableHandleScope; | 54 | 54 | using v8::FunctionCallbackInfo; | @@ -127,8 +127,8 @@ CallbackInfo::CallbackInfo(Environment* env, | 127 | 127 | data_(data), | 128 | 128 | hint_(hint), | 129 | 129 | env_(env) { | 130 | | - ArrayBuffer::Contents obj_c = object->GetContents(); | 131 | | - CHECK_EQ(data_, static_cast<char*>(obj_c.Data())); | | 130 | + std::shared_ptr<BackingStore> obj_backing = object->GetBackingStore(); | | 131 | + CHECK_EQ(data_, static_cast<char*>(obj_backing->Data())); | 132 | 132 | if (object->ByteLength() != 0) | 133 | 133 | CHECK_NOT_NULL(data_); | 134 | 134 |
| @@ -406,7 +406,19 @@ MaybeLocal<Object> New(Environment* env, | 406 | 406 | return Local<Object>(); | 407 | 407 | } | 408 | 408 |
| 409 | | - Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(), data, length); | | 409 | + | | 410 | + // The buffer will be released by a CallbackInfo::New() below, | | 411 | + // hence this BackingStore callback is empty. | | 412 | + std::unique_ptr<BackingStore> backing = | | 413 | + ArrayBuffer::NewBackingStore(data, | | 414 | + length, | | 415 | + [](void*, size_t, void*){}, | | 416 | + nullptr); | | 417 | + Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(), | | 418 | + std::move(backing)); | | 419 | + // TODO(thangktran): drop this check when V8 is pumped to 8.0 . | | 420 | + if (!ab->IsExternal()) | | 421 | + ab->Externalize(ab->GetBackingStore()); | 410 | 422 | if (ab->SetPrivate(env->context(), | 411 | 423 | env->arraybuffer_untransferable_private_symbol(), | 412 | 424 | True(env->isolate())).IsNothing()) { | @@ -465,11 +477,21 @@ MaybeLocal<Object> New(Environment* env, | 465 | 477 | } | 466 | 478 | } | 467 | 479 |
| 468 | | - Local<ArrayBuffer> ab = | 469 | | - ArrayBuffer::New(env->isolate(), | 470 | | - data, | 471 | | - length, | 472 | | - ArrayBufferCreationMode::kInternalized); | | 480 | + auto callback = [](void* data, size_t length, void* deleter_data){ | | 481 | + CHECK_NOT_NULL(deleter_data); | | 482 | + | | 483 | + static_cast<v8::ArrayBuffer::Allocator*>(deleter_data) | | 484 | + ->Free(data, length); | | 485 | + }; | | 486 | + std::unique_ptr<v8::BackingStore> backing = | | 487 | + v8::ArrayBuffer::NewBackingStore(data, | | 488 | + length, | | 489 | + callback, | | 490 | + env->isolate() | | 491 | + ->GetArrayBufferAllocator()); | | 492 | + Local<ArrayBuffer> ab = ArrayBuffer::New(env->isolate(), | | 493 | + std::move(backing)); | | 494 | + | 473 | 495 | return Buffer::New(env, ab, 0, length).FromMaybe(Local<Object>()); | 474 | 496 | } | 475 | 497 |
| @@ -1181,8 +1203,16 @@ void Initialize(Local<Object> target, | 1181 | 1203 | if (NodeArrayBufferAllocator* allocator = | 1182 | 1204 | env->isolate_data()->node_allocator()) { | 1183 | 1205 | uint32_t* zero_fill_field = allocator->zero_fill_field(); | 1184 | | - Local<ArrayBuffer> array_buffer = ArrayBuffer::New( | 1185 | | - env->isolate(), zero_fill_field, sizeof(*zero_fill_field)); | | 1206 | + std::unique_ptr<BackingStore> backing = | | 1207 | + ArrayBuffer::NewBackingStore(zero_fill_field, | | 1208 | + sizeof(*zero_fill_field), | | 1209 | + [](void*, size_t, void*){}, | | 1210 | + nullptr); | | 1211 | + Local<ArrayBuffer> array_buffer = | | 1212 | + ArrayBuffer::New(env->isolate(), std::move(backing)); | | 1213 | + // TODO(thangktran): drop this check when V8 is pumped to 8.0 . | | 1214 | + if (!array_buffer->IsExternal()) | | 1215 | + array_buffer->Externalize(array_buffer->GetBackingStore()); | 1186 | 1216 | CHECK(target | 1187 | 1217 | ->Set(env->context(), | 1188 | 1218 | FIXED_ONE_BYTE_STRING(env->isolate(), "zeroFill"), | |
Collapse file Copy file name to clipboardExpand all lines: src/node_http2.cc +20-3Lines changed: 20 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,6 +16,7 @@ namespace node { | 16 | 16 |
| 17 | 17 | using v8::ArrayBuffer; | 18 | 18 | using v8::ArrayBufferView; | | 19 | +using v8::BackingStore; | 19 | 20 | using v8::Boolean; | 20 | 21 | using v8::Context; | 21 | 22 | using v8::Float64Array; | @@ -566,10 +567,18 @@ Http2Session::Http2Session(Environment* env, | 566 | 567 |
| 567 | 568 | { | 568 | 569 | // Make the js_fields_ property accessible to JS land. | | 570 | + std::unique_ptr<BackingStore> backing = | | 571 | + ArrayBuffer::NewBackingStore( | | 572 | + reinterpret_cast<uint8_t*>(&js_fields_), | | 573 | + kSessionUint8FieldCount, | | 574 | + [](void*, size_t, void*){}, | | 575 | + nullptr); | 569 | 576 | Local<ArrayBuffer> ab = | 570 | | - ArrayBuffer::New(env->isolate(), | 571 | | - reinterpret_cast<uint8_t*>(&js_fields_), | 572 | | - kSessionUint8FieldCount); | | 577 | + ArrayBuffer::New(env->isolate(), std::move(backing)); | | 578 | + // TODO(thangktran): drop this check when V8 is pumped to 8.0 . | | 579 | + if (!ab->IsExternal()) | | 580 | + ab->Externalize(ab->GetBackingStore()); | | 581 | + js_fields_ab_.Reset(env->isolate(), ab); | 573 | 582 | Local<Uint8Array> uint8_arr = | 574 | 583 | Uint8Array::New(ab, 0, kSessionUint8FieldCount); | 575 | 584 | USE(wrap->Set(env->context(), env->fields_string(), uint8_arr)); | @@ -581,6 +590,14 @@ Http2Session::~Http2Session() { | 581 | 590 | Debug(this, "freeing nghttp2 session"); | 582 | 591 | nghttp2_session_del(session_); | 583 | 592 | CHECK_EQ(current_nghttp2_memory_, 0); | | 593 | + HandleScope handle_scope(env()->isolate()); | | 594 | + // Detach js_fields_ab_ to avoid having problem when new Http2Session | | 595 | + // instances are created on the same location of previous | | 596 | + // instances. This in turn will call ArrayBuffer::NewBackingStore() | | 597 | + // multiple times with the same buffer address and causing error. | | 598 | + // Ref: https://github.com/nodejs/node/pull/30782 | | 599 | + Local<ArrayBuffer> ab = js_fields_ab_.Get(env()->isolate()); | | 600 | + ab->Detach(); | 584 | 601 | } | 585 | 602 |
| 586 | 603 | std::string Http2Session::diagnostic_name() const { | |
Collapse file Copy file name to clipboardExpand all lines: src/node_http2.h +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -989,6 +989,7 @@ class Http2Session : public AsyncWrap, | 989 | 989 |
| 990 | 990 | // JS-accessible numeric fields, as indexed by SessionUint8Fields. | 991 | 991 | SessionJSFields js_fields_ = {}; | | 992 | + v8::Global<v8::ArrayBuffer> js_fields_ab_; | 992 | 993 |
| 993 | 994 | // The session type: client or server | 994 | 995 | nghttp2_session_type session_type_; | |
Collapse file Copy file name to clipboardExpand all lines: src/node_v8.cc +40-9Lines changed: 40 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,6 +28,7 @@ namespace node { | 28 | 28 |
| 29 | 29 | using v8::Array; | 30 | 30 | using v8::ArrayBuffer; | | 31 | +using v8::BackingStore; | 31 | 32 | using v8::Context; | 32 | 33 | using v8::FunctionCallbackInfo; | 33 | 34 | using v8::HeapCodeStatistics; | @@ -162,12 +163,22 @@ void Initialize(Local<Object> target, | 162 | 163 | const size_t heap_statistics_buffer_byte_length = | 163 | 164 | sizeof(*env->heap_statistics_buffer()) * kHeapStatisticsPropertiesCount; | 164 | 165 |
| | 166 | + std::unique_ptr<BackingStore> heap_statistics_backing = | | 167 | + ArrayBuffer::NewBackingStore(env->heap_statistics_buffer(), | | 168 | + heap_statistics_buffer_byte_length, | | 169 | + [](void*, size_t, void*){}, | | 170 | + nullptr); | | 171 | + Local<ArrayBuffer> heap_statistics_ab = | | 172 | + ArrayBuffer::New(env->isolate(), | | 173 | + std::move(heap_statistics_backing)); | | 174 | + // TODO(thangktran): drop this check when V8 is pumped to 8.0 . | | 175 | + if (!heap_statistics_ab->IsExternal()) | | 176 | + heap_statistics_ab->Externalize( | | 177 | + heap_statistics_ab->GetBackingStore()); | 165 | 178 | target->Set(env->context(), | 166 | 179 | FIXED_ONE_BYTE_STRING(env->isolate(), | 167 | 180 | "heapStatisticsArrayBuffer"), | 168 | | - ArrayBuffer::New(env->isolate(), | 169 | | - env->heap_statistics_buffer(), | 170 | | - heap_statistics_buffer_byte_length)).Check(); | | 181 | + heap_statistics_ab).Check(); | 171 | 182 |
| 172 | 183 | #define V(i, _, name) \ | 173 | 184 | target->Set(env->context(), \ | @@ -189,12 +200,22 @@ void Initialize(Local<Object> target, | 189 | 200 | sizeof(*env->heap_code_statistics_buffer()) | 190 | 201 | * kHeapCodeStatisticsPropertiesCount; | 191 | 202 |
| | 203 | + std::unique_ptr<BackingStore> heap_code_statistics_backing = | | 204 | + ArrayBuffer::NewBackingStore(env->heap_code_statistics_buffer(), | | 205 | + heap_code_statistics_buffer_byte_length, | | 206 | + [](void*, size_t, void*){}, | | 207 | + nullptr); | | 208 | + Local<ArrayBuffer> heap_code_statistics_ab = | | 209 | + ArrayBuffer::New(env->isolate(), | | 210 | + std::move(heap_code_statistics_backing)); | | 211 | + // TODO(thangktran): drop this check when V8 is pumped to 8.0 . | | 212 | + if (!heap_code_statistics_ab->IsExternal()) | | 213 | + heap_code_statistics_ab->Externalize( | | 214 | + heap_code_statistics_ab->GetBackingStore()); | 192 | 215 | target->Set(env->context(), | 193 | 216 | FIXED_ONE_BYTE_STRING(env->isolate(), | 194 | 217 | "heapCodeStatisticsArrayBuffer"), | 195 | | - ArrayBuffer::New(env->isolate(), | 196 | | - env->heap_code_statistics_buffer(), | 197 | | - heap_code_statistics_buffer_byte_length)) | | 218 | + heap_code_statistics_ab) | 198 | 219 | .Check(); | 199 | 220 |
| 200 | 221 | #define V(i, _, name) \ | @@ -244,12 +265,22 @@ void Initialize(Local<Object> target, | 244 | 265 | kHeapSpaceStatisticsPropertiesCount * | 245 | 266 | number_of_heap_spaces; | 246 | 267 |
| | 268 | + std::unique_ptr<BackingStore> heap_space_statistics_backing = | | 269 | + ArrayBuffer::NewBackingStore(env->heap_space_statistics_buffer(), | | 270 | + heap_space_statistics_buffer_byte_length, | | 271 | + [](void*, size_t, void*){}, | | 272 | + nullptr); | | 273 | + Local<ArrayBuffer> heap_space_statistics_ab = | | 274 | + ArrayBuffer::New(env->isolate(), | | 275 | + std::move(heap_space_statistics_backing)); | | 276 | + // TODO(thangktran): drop this check when V8 is pumped to 8.0 . | | 277 | + if (!heap_space_statistics_ab->IsExternal()) | | 278 | + heap_space_statistics_ab->Externalize( | | 279 | + heap_space_statistics_ab->GetBackingStore()); | 247 | 280 | target->Set(env->context(), | 248 | 281 | FIXED_ONE_BYTE_STRING(env->isolate(), | 249 | 282 | "heapSpaceStatisticsArrayBuffer"), | 250 | | - ArrayBuffer::New(env->isolate(), | 251 | | - env->heap_space_statistics_buffer(), | 252 | | - heap_space_statistics_buffer_byte_length)) | | 283 | + heap_space_statistics_ab) | 253 | 284 | .Check(); | 254 | 285 |
| 255 | 286 | #define V(i, _, name) \ | |
Collapse file Copy file name to clipboardExpand all lines: src/node_worker.cc +2Lines changed: 2 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 19 | 19 | using node::kDisallowedInEnvironment; | 20 | 20 | using v8::Array; | 21 | 21 | using v8::ArrayBuffer; | | 22 | +using v8::BackingStore; | 22 | 23 | using v8::Boolean; | 23 | 24 | using v8::Context; | 24 | 25 | using v8::Float64Array; | @@ -622,6 +623,7 @@ void Worker::GetResourceLimits(const FunctionCallbackInfo<Value>& args) { | 622 | 623 |
| 623 | 624 | Local<Float64Array> Worker::GetResourceLimits(Isolate* isolate) const { | 624 | 625 | Local<ArrayBuffer> ab = ArrayBuffer::New(isolate, sizeof(resource_limits_)); | | 626 | + | 625 | 627 | memcpy(ab->GetBackingStore()->Data(), | 626 | 628 | resource_limits_, | 627 | 629 | sizeof(resource_limits_)); | |
Collapse file Copy file name to clipboardExpand all lines: test/addons/worker-buffer-callback/binding.cc +4-1Lines changed: 4 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -10,7 +10,6 @@ using v8::Object; | 10 | 10 | using v8::Value; | 11 | 11 |
| 12 | 12 | uint32_t free_call_count = 0; | 13 | | -char data[] = "hello"; | 14 | 13 |
| 15 | 14 | void GetFreeCallCount(const FunctionCallbackInfo<Value>& args) { | 16 | 15 | args.GetReturnValue().Set(free_call_count); | @@ -21,6 +20,9 @@ void Initialize(Local<Object> exports, | 21 | 20 | Local<Context> context) { | 22 | 21 | Isolate* isolate = context->GetIsolate(); | 23 | 22 | NODE_SET_METHOD(exports, "getFreeCallCount", GetFreeCallCount); | | 23 | + | | 24 | + char* data = new char; | | 25 | + | 24 | 26 | exports->Set(context, | 25 | 27 | v8::String::NewFromUtf8( | 26 | 28 | isolate, "buffer", v8::NewStringType::kNormal) | @@ -30,6 +32,7 @@ void Initialize(Local<Object> exports, | 30 | 32 | data, | 31 | 33 | sizeof(data), | 32 | 34 | [](char* data, void* hint) { | | 35 | + delete data; | 33 | 36 | free_call_count++; | 34 | 37 | }, | 35 | 38 | nullptr).ToLocalChecked()).Check(); | |
Collapse file Copy file name to clipboardExpand all lines: test/js-native-api/test_typedarray/test_typedarray.c +16-4Lines changed: 16 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | #define NAPI_EXPERIMENTAL | 2 | 2 | #include <js_native_api.h> | 3 | 3 | #include <string.h> | | 4 | +#include <stdlib.h> | 4 | 5 | #include "../common.h" | 5 | 6 |
| 6 | 7 | static napi_value Multiply(napi_env env, napi_callback_info info) { | @@ -74,22 +75,33 @@ static napi_value Multiply(napi_env env, napi_callback_info info) { | 74 | 75 | return output_array; | 75 | 76 | } | 76 | 77 |
| | 78 | +static void FinalizeCallback(napi_env env, | | 79 | + void* finalize_data, | | 80 | + void* finalize_hint) | | 81 | +{ | | 82 | + free(finalize_data); | | 83 | +} | | 84 | + | 77 | 85 | static napi_value External(napi_env env, napi_callback_info info) { | 78 | | - static int8_t externalData[] = {0, 1, 2}; | | 86 | + const uint8_t nElem = 3; | | 87 | + int8_t* externalData = malloc(nElem*sizeof(int8_t)); | | 88 | + externalData[0] = 0; | | 89 | + externalData[1] = 1; | | 90 | + externalData[2] = 2; | 79 | 91 |
| 80 | 92 | napi_value output_buffer; | 81 | 93 | NAPI_CALL(env, napi_create_external_arraybuffer( | 82 | 94 | env, | 83 | 95 | externalData, | 84 | | - sizeof(externalData), | 85 | | - NULL, // finalize_callback | | 96 | + nElem*sizeof(int8_t), | | 97 | + FinalizeCallback, | 86 | 98 | NULL, // finalize_hint | 87 | 99 | &output_buffer)); | 88 | 100 |
| 89 | 101 | napi_value output_array; | 90 | 102 | NAPI_CALL(env, napi_create_typedarray(env, | 91 | 103 | napi_int8_array, | 92 | | - sizeof(externalData) / sizeof(int8_t), | | 104 | + nElem, | 93 | 105 | output_buffer, | 94 | 106 | 0, | 95 | 107 | &output_array)); | |
Collapse file Copy file name to clipboardExpand all lines: test/node-api/test_worker_buffer_callback/test_worker_buffer_callback.c +5-2Lines changed: 5 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | #include <stdio.h> | | 2 | +#include <stdlib.h> | 2 | 3 | #include <node_api.h> | 3 | 4 | #include <assert.h> | 4 | 5 | #include "../../js-native-api/common.h" | 5 | 6 |
| 6 | 7 | uint32_t free_call_count = 0; | 7 | | -char data[] = "hello"; | 8 | 8 |
| 9 | 9 | napi_value GetFreeCallCount(napi_env env, napi_callback_info info) { | 10 | 10 | napi_value value; | @@ -13,7 +13,7 @@ napi_value GetFreeCallCount(napi_env env, napi_callback_info info) { | 13 | 13 | } | 14 | 14 |
| 15 | 15 | static void finalize_cb(napi_env env, void* finalize_data, void* hint) { | 16 | | - assert(finalize_data == data); | | 16 | + free(finalize_data); | 17 | 17 | free_call_count++; | 18 | 18 | } | 19 | 19 |
| @@ -29,6 +29,9 @@ NAPI_MODULE_INIT() { | 29 | 29 | // rather than a Node.js Buffer, since testing the latter would only test | 30 | 30 | // the same code paths and not the ones specific to N-API. | 31 | 31 | napi_value buffer; | | 32 | + | | 33 | + char* data = malloc(sizeof(char)); | | 34 | + | 32 | 35 | NAPI_CALL(env, napi_create_external_arraybuffer( | 33 | 36 | env, | 34 | 37 | data, | |
|