|
| 1 | +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Igor Sheludko <isheludko@gmail.com> |
| 3 | +Date: Tue, 30 Apr 2024 15:22:06 +0200 |
| 4 | +Subject: src: use new V8 API to define stream accessor |
| 5 | +MIME-Version: 1.0 |
| 6 | +Content-Type: text/plain; charset=UTF-8 |
| 7 | +Content-Transfer-Encoding: 8bit |
| 8 | + |
| 9 | +Define XxxStream.prototype.onread as an accessor in JavaScript sense. |
| 10 | + |
| 11 | +Previously it was defined via soon-to-be-deprecated |
| 12 | +`v8::ObjectTemplate::SetAccessor(..)` which used to call setter even |
| 13 | +for property stores via stream object. |
| 14 | + |
| 15 | +The replacement V8 API `v8::ObjectTemplate::SetNativeDataProperty(..)` |
| 16 | +defines a properly behaving data property and thus a store to a stream |
| 17 | +object will not trigger the "onread" setter callback. |
| 18 | + |
| 19 | +In order to preserve the desired behavior of storing the value in the |
| 20 | +receiver's internal field the "onread" property should be defined as |
| 21 | +a proper JavaScript accessor. |
| 22 | + |
| 23 | +PR-URL: https://github.com/nodejs/node/pull/53084 |
| 24 | +Refs: https://github.com/v8/v8/commit/46c241eb99557fe8205acac5c526650c3847d180 |
| 25 | +Refs: https://github.com/v8/v8/commit/6ec883986bd417e2a42ddb960bd9449deb7e4639 |
| 26 | +Reviewed-By: Luigi Pinca <luigipinca@gmail.com> |
| 27 | +Reviewed-By: Tobias Nießen <tniessen@tnie.de> |
| 28 | +Reviewed-By: James M Snell <jasnell@gmail.com> |
| 29 | +Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> |
| 30 | +(cherry picked from commit bd151552ef35b0eed415eb1c50d30dafd341cee8) |
| 31 | + |
| 32 | +diff --git a/src/base_object-inl.h b/src/base_object-inl.h |
| 33 | +index 518b22dabef0974c2e7ecb466669925338524059..61f30b3cfbdb0f3d21fe8e93dc97c55162b69a69 100644 |
| 34 | +--- a/src/base_object-inl.h |
| 35 | ++++ b/src/base_object-inl.h |
| 36 | +@@ -132,19 +132,18 @@ v8::EmbedderGraph::Node::Detachedness BaseObject::GetDetachedness() const { |
| 37 | + |
| 38 | + template <int Field> |
| 39 | + void BaseObject::InternalFieldGet( |
| 40 | +- v8::Local<v8::Name> property, |
| 41 | +- const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 42 | +- info.GetReturnValue().Set( |
| 43 | +- info.This()->GetInternalField(Field).As<v8::Value>()); |
| 44 | ++ const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 45 | ++ args.GetReturnValue().Set( |
| 46 | ++ args.This()->GetInternalField(Field).As<v8::Value>()); |
| 47 | + } |
| 48 | + |
| 49 | + template <int Field, bool (v8::Value::*typecheck)() const> |
| 50 | +-void BaseObject::InternalFieldSet(v8::Local<v8::Name> property, |
| 51 | +- v8::Local<v8::Value> value, |
| 52 | +- const v8::PropertyCallbackInfo<void>& info) { |
| 53 | ++void BaseObject::InternalFieldSet( |
| 54 | ++ const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 55 | ++ v8::Local<v8::Value> value = args[0]; |
| 56 | + // This could be e.g. value->IsFunction(). |
| 57 | + CHECK(((*value)->*typecheck)()); |
| 58 | +- info.This()->SetInternalField(Field, value); |
| 59 | ++ args.This()->SetInternalField(Field, value); |
| 60 | + } |
| 61 | + |
| 62 | + bool BaseObject::has_pointer_data() const { |
| 63 | +diff --git a/src/base_object.h b/src/base_object.h |
| 64 | +index 5c16d0d1b32e2d056f4fcfa0e01781292932a0fa..ce6277dec5a2b9313ecd3699b39ec17585d5adc5 100644 |
| 65 | +--- a/src/base_object.h |
| 66 | ++++ b/src/base_object.h |
| 67 | +@@ -111,12 +111,9 @@ class BaseObject : public MemoryRetainer { |
| 68 | + |
| 69 | + // Setter/Getter pair for internal fields that can be passed to SetAccessor. |
| 70 | + template <int Field> |
| 71 | +- static void InternalFieldGet(v8::Local<v8::Name> property, |
| 72 | +- const v8::PropertyCallbackInfo<v8::Value>& info); |
| 73 | ++ static void InternalFieldGet(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 74 | + template <int Field, bool (v8::Value::*typecheck)() const> |
| 75 | +- static void InternalFieldSet(v8::Local<v8::Name> property, |
| 76 | +- v8::Local<v8::Value> value, |
| 77 | +- const v8::PropertyCallbackInfo<void>& info); |
| 78 | ++ static void InternalFieldSet(const v8::FunctionCallbackInfo<v8::Value>& args); |
| 79 | + |
| 80 | + // This is a bit of a hack. See the override in async_wrap.cc for details. |
| 81 | + virtual bool IsDoneInitializing() const; |
| 82 | +diff --git a/src/stream_base.cc b/src/stream_base.cc |
| 83 | +index af714734f49b836de79ebb25e61f1376b757d5ed..a8f9f98d413c8573eb22960a4cefb47408215469 100644 |
| 84 | +--- a/src/stream_base.cc |
| 85 | ++++ b/src/stream_base.cc |
| 86 | +@@ -492,6 +492,29 @@ Local<Object> StreamBase::GetObject() { |
| 87 | + return GetAsyncWrap()->object(); |
| 88 | + } |
| 89 | + |
| 90 | ++void StreamBase::AddAccessor(v8::Isolate* isolate, |
| 91 | ++ v8::Local<v8::Signature> signature, |
| 92 | ++ enum v8::PropertyAttribute attributes, |
| 93 | ++ v8::Local<v8::FunctionTemplate> t, |
| 94 | ++ JSMethodFunction* getter, |
| 95 | ++ JSMethodFunction* setter, |
| 96 | ++ v8::Local<v8::String> string) { |
| 97 | ++ Local<FunctionTemplate> getter_templ = |
| 98 | ++ NewFunctionTemplate(isolate, |
| 99 | ++ getter, |
| 100 | ++ signature, |
| 101 | ++ ConstructorBehavior::kThrow, |
| 102 | ++ SideEffectType::kHasNoSideEffect); |
| 103 | ++ Local<FunctionTemplate> setter_templ = |
| 104 | ++ NewFunctionTemplate(isolate, |
| 105 | ++ setter, |
| 106 | ++ signature, |
| 107 | ++ ConstructorBehavior::kThrow, |
| 108 | ++ SideEffectType::kHasSideEffect); |
| 109 | ++ t->PrototypeTemplate()->SetAccessorProperty( |
| 110 | ++ string, getter_templ, setter_templ, attributes); |
| 111 | ++} |
| 112 | ++ |
| 113 | + void StreamBase::AddMethod(Isolate* isolate, |
| 114 | + Local<Signature> signature, |
| 115 | + enum PropertyAttribute attributes, |
| 116 | +@@ -561,11 +584,14 @@ void StreamBase::AddMethods(IsolateData* isolate_data, |
| 117 | + JSMethod<&StreamBase::WriteString<LATIN1>>); |
| 118 | + t->PrototypeTemplate()->Set(FIXED_ONE_BYTE_STRING(isolate, "isStreamBase"), |
| 119 | + True(isolate)); |
| 120 | +- t->PrototypeTemplate()->SetAccessor( |
| 121 | +- FIXED_ONE_BYTE_STRING(isolate, "onread"), |
| 122 | +- BaseObject::InternalFieldGet<StreamBase::kOnReadFunctionField>, |
| 123 | +- BaseObject::InternalFieldSet<StreamBase::kOnReadFunctionField, |
| 124 | +- &Value::IsFunction>); |
| 125 | ++ AddAccessor(isolate, |
| 126 | ++ sig, |
| 127 | ++ static_cast<PropertyAttribute>(DontDelete | DontEnum), |
| 128 | ++ t, |
| 129 | ++ BaseObject::InternalFieldGet<StreamBase::kOnReadFunctionField>, |
| 130 | ++ BaseObject::InternalFieldSet<StreamBase::kOnReadFunctionField, |
| 131 | ++ &Value::IsFunction>, |
| 132 | ++ FIXED_ONE_BYTE_STRING(isolate, "onread")); |
| 133 | + } |
| 134 | + |
| 135 | + void StreamBase::RegisterExternalReferences( |
| 136 | +diff --git a/src/stream_base.h b/src/stream_base.h |
| 137 | +index 62a8928e144ad6aa67eeccdbc46d44da22887fb8..ccbd769ceaf3c1e024defb3104e601d037c98b6a 100644 |
| 138 | +--- a/src/stream_base.h |
| 139 | ++++ b/src/stream_base.h |
| 140 | +@@ -413,6 +413,13 @@ class StreamBase : public StreamResource { |
| 141 | + EmitToJSStreamListener default_listener_; |
| 142 | + |
| 143 | + void SetWriteResult(const StreamWriteResult& res); |
| 144 | ++ static void AddAccessor(v8::Isolate* isolate, |
| 145 | ++ v8::Local<v8::Signature> sig, |
| 146 | ++ enum v8::PropertyAttribute attributes, |
| 147 | ++ v8::Local<v8::FunctionTemplate> t, |
| 148 | ++ JSMethodFunction* getter, |
| 149 | ++ JSMethodFunction* setter, |
| 150 | ++ v8::Local<v8::String> str); |
| 151 | + static void AddMethod(v8::Isolate* isolate, |
| 152 | + v8::Local<v8::Signature> sig, |
| 153 | + enum v8::PropertyAttribute attributes, |
0 commit comments