Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: refactor binding data for deserialization support #37112

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions node.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,7 @@
'src/node_union_bytes.h',
'src/node_url.h',
'src/node_version.h',
'src/node_v8.h',
'src/node_v8_platform-inl.h',
'src/node_wasi.h',
'src/node_watchdog.h',
Expand Down
4 changes: 2 additions & 2 deletions src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ that state is through the use of `Environment::AddBindingData`, which gives
binding functions access to an object for storing such state.
That object is always a [`BaseObject`][].

Its class needs to have a static `binding_data_name` field based on a
Its class needs to have a static `type_name` field based on a
constant string, in order to disambiguate it from other classes of this type,
and which could e.g. match the binding’s name (in the example above, that would
be `cares_wrap`).
Expand All @@ -433,7 +433,7 @@ class BindingData : public BaseObject {
public:
BindingData(Environment* env, Local<Object> obj) : BaseObject(env, obj) {}

static constexpr FastStringKey binding_data_name { "http_parser" };
static constexpr FastStringKey type_name { "http_parser" };

std::vector<char> parser_buffer;
bool parser_buffer_in_use = false;
Expand Down
4 changes: 2 additions & 2 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ inline T* Environment::GetBindingData(v8::Local<v8::Context> context) {
context->GetAlignedPointerFromEmbedderData(
ContextEmbedderIndex::kBindingListIndex));
DCHECK_NOT_NULL(map);
auto it = map->find(T::binding_data_name);
auto it = map->find(T::type_name);
if (UNLIKELY(it == map->end())) return nullptr;
T* result = static_cast<T*>(it->second.get());
DCHECK_NOT_NULL(result);
Expand All @@ -377,7 +377,7 @@ inline T* Environment::AddBindingData(
context->GetAlignedPointerFromEmbedderData(
ContextEmbedderIndex::kBindingListIndex));
DCHECK_NOT_NULL(map);
auto result = map->emplace(T::binding_data_name, item);
auto result = map->emplace(T::type_name, item);
CHECK(result.second);
DCHECK_EQ(GetBindingData<T>(context), item.get());
return item.get();
Expand Down
2 changes: 1 addition & 1 deletion src/node_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2397,7 +2397,7 @@ void BindingData::MemoryInfo(MemoryTracker* tracker) const {
}

// TODO(addaleax): Remove once we're on C++17.
constexpr FastStringKey BindingData::binding_data_name;
constexpr FastStringKey BindingData::type_name;

void Initialize(Local<Object> target,
Local<Value> unused,
Expand Down
2 changes: 1 addition & 1 deletion src/node_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class BindingData : public BaseObject {
std::vector<BaseObjectPtr<FileHandleReadWrap>>
file_handle_read_wrap_freelist;

static constexpr FastStringKey binding_data_name { "fs" };
static constexpr FastStringKey type_name { "fs" };

void MemoryInfo(MemoryTracker* tracker) const override;
SET_SELF_SIZE(BindingData)
Expand Down
2 changes: 1 addition & 1 deletion src/node_http2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3001,7 +3001,7 @@ void Http2State::MemoryInfo(MemoryTracker* tracker) const {
}

// TODO(addaleax): Remove once we're on C++17.
constexpr FastStringKey Http2State::binding_data_name;
constexpr FastStringKey Http2State::type_name;

// Set up the process.binding('http2') binding.
void Initialize(Local<Object> target,
Expand Down
2 changes: 1 addition & 1 deletion src/node_http2_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class Http2State : public BaseObject {
SET_SELF_SIZE(Http2State)
SET_MEMORY_INFO_NAME(Http2State)

static constexpr FastStringKey binding_data_name { "http2" };
static constexpr FastStringKey type_name { "http2" };

private:
struct http2_state_internal {
Expand Down
4 changes: 2 additions & 2 deletions src/node_http_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class BindingData : public BaseObject {
BindingData(Environment* env, Local<Object> obj)
: BaseObject(env, obj) {}

static constexpr FastStringKey binding_data_name { "http_parser" };
static constexpr FastStringKey type_name { "http_parser" };

std::vector<char> parser_buffer;
bool parser_buffer_in_use = false;
Expand All @@ -101,7 +101,7 @@ class BindingData : public BaseObject {
};

// TODO(addaleax): Remove once we're on C++17.
constexpr FastStringKey BindingData::binding_data_name;
constexpr FastStringKey BindingData::type_name;

// helper class for the Parser
struct StringPtr {
Expand Down
102 changes: 41 additions & 61 deletions src/node_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

#include "node.h"
#include "node_v8.h"
#include "base_object-inl.h"
#include "env-inl.h"
#include "memory_tracker-inl.h"
#include "node.h"
#include "util-inl.h"
#include "v8.h"

namespace node {

namespace v8_utils {
using v8::Array;
using v8::Context;
using v8::FunctionCallbackInfo;
Expand All @@ -44,7 +45,6 @@ using v8::Uint32;
using v8::V8;
using v8::Value;


#define HEAP_STATISTICS_PROPERTIES(V) \
V(0, total_heap_size, kTotalHeapSizeIndex) \
V(1, total_heap_size_executable, kTotalHeapSizeExecutableIndex) \
Expand All @@ -63,7 +63,6 @@ static constexpr size_t kHeapStatisticsPropertiesCount =
HEAP_STATISTICS_PROPERTIES(V);
#undef V


#define HEAP_SPACE_STATISTICS_PROPERTIES(V) \
V(0, space_size, kSpaceSizeIndex) \
V(1, space_used_size, kSpaceUsedSizeIndex) \
Expand All @@ -85,35 +84,37 @@ static const size_t kHeapCodeStatisticsPropertiesCount =
HEAP_CODE_STATISTICS_PROPERTIES(V);
#undef V

class BindingData : public BaseObject {
public:
BindingData(Environment* env, Local<Object> obj)
: BaseObject(env, obj),
heap_statistics_buffer(env->isolate(), kHeapStatisticsPropertiesCount),
heap_space_statistics_buffer(env->isolate(),
kHeapSpaceStatisticsPropertiesCount),
heap_code_statistics_buffer(env->isolate(),
kHeapCodeStatisticsPropertiesCount) {}

static constexpr FastStringKey binding_data_name { "v8" };

AliasedFloat64Array heap_statistics_buffer;
AliasedFloat64Array heap_space_statistics_buffer;
AliasedFloat64Array heap_code_statistics_buffer;

void MemoryInfo(MemoryTracker* tracker) const override {
tracker->TrackField("heap_statistics_buffer", heap_statistics_buffer);
tracker->TrackField("heap_space_statistics_buffer",
heap_space_statistics_buffer);
tracker->TrackField("heap_code_statistics_buffer",
heap_code_statistics_buffer);
}
SET_SELF_SIZE(BindingData)
SET_MEMORY_INFO_NAME(BindingData)
};
BindingData::BindingData(Environment* env, Local<Object> obj)
: BaseObject(env, obj),
heap_statistics_buffer(env->isolate(), kHeapStatisticsPropertiesCount),
heap_space_statistics_buffer(env->isolate(),
kHeapSpaceStatisticsPropertiesCount),
heap_code_statistics_buffer(env->isolate(),
kHeapCodeStatisticsPropertiesCount) {
obj->Set(env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(), "heapStatisticsBuffer"),
heap_statistics_buffer.GetJSArray())
.Check();
obj->Set(env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(), "heapCodeStatisticsBuffer"),
heap_code_statistics_buffer.GetJSArray())
.Check();
obj->Set(env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(), "heapSpaceStatisticsBuffer"),
heap_space_statistics_buffer.GetJSArray())
.Check();
}

void BindingData::MemoryInfo(MemoryTracker* tracker) const {
tracker->TrackField("heap_statistics_buffer", heap_statistics_buffer);
tracker->TrackField("heap_space_statistics_buffer",
heap_space_statistics_buffer);
tracker->TrackField("heap_code_statistics_buffer",
heap_code_statistics_buffer);
}

// TODO(addaleax): Remove once we're on C++17.
constexpr FastStringKey BindingData::binding_data_name;
constexpr FastStringKey BindingData::type_name;

void CachedDataVersionTag(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Expand Down Expand Up @@ -179,36 +180,12 @@ void Initialize(Local<Object> target,

env->SetMethodNoSideEffect(target, "cachedDataVersionTag",
CachedDataVersionTag);

// Export symbols used by v8.getHeapStatistics()
env->SetMethod(
target, "updateHeapStatisticsBuffer", UpdateHeapStatisticsBuffer);

target
->Set(env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(), "heapStatisticsBuffer"),
binding_data->heap_statistics_buffer.GetJSArray())
.Check();

#define V(i, _, name) \
target->Set(env->context(), \
FIXED_ONE_BYTE_STRING(env->isolate(), #name), \
Uint32::NewFromUnsigned(env->isolate(), i)).Check();

HEAP_STATISTICS_PROPERTIES(V)

// Export symbols used by v8.getHeapCodeStatistics()
env->SetMethod(
target, "updateHeapCodeStatisticsBuffer", UpdateHeapCodeStatisticsBuffer);

target
->Set(env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(), "heapCodeStatisticsBuffer"),
binding_data->heap_code_statistics_buffer.GetJSArray())
.Check();

HEAP_CODE_STATISTICS_PROPERTIES(V)

size_t number_of_heap_spaces = env->isolate()->NumberOfHeapSpaces();

// Heap space names are extracted once and exposed to JavaScript to
Expand All @@ -230,20 +207,23 @@ void Initialize(Local<Object> target,
"updateHeapSpaceStatisticsBuffer",
UpdateHeapSpaceStatisticsBuffer);

target
->Set(env->context(),
FIXED_ONE_BYTE_STRING(env->isolate(),
"heapSpaceStatisticsBuffer"),
binding_data->heap_space_statistics_buffer.GetJSArray())
#define V(i, _, name) \
target \
->Set(env->context(), \
FIXED_ONE_BYTE_STRING(env->isolate(), #name), \
Uint32::NewFromUnsigned(env->isolate(), i)) \
.Check();

HEAP_STATISTICS_PROPERTIES(V)
HEAP_CODE_STATISTICS_PROPERTIES(V)
HEAP_SPACE_STATISTICS_PROPERTIES(V)
#undef V

// Export symbols used by v8.setFlagsFromString()
env->SetMethod(target, "setFlagsFromString", SetFlagsFromString);
}

} // namespace v8_utils
} // namespace node

NODE_MODULE_CONTEXT_AWARE_INTERNAL(v8, node::Initialize)
NODE_MODULE_CONTEXT_AWARE_INTERNAL(v8, node::v8_utils::Initialize)
36 changes: 36 additions & 0 deletions src/node_v8.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#ifndef SRC_NODE_V8_H_
#define SRC_NODE_V8_H_

#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#include "aliased_buffer.h"
#include "base_object.h"
#include "util.h"
#include "v8.h"

namespace node {
class Environment;

namespace v8_utils {
class BindingData : public BaseObject {
public:
BindingData(Environment* env, v8::Local<v8::Object> obj);

static constexpr FastStringKey type_name{"node::v8::BindingData"};

AliasedFloat64Array heap_statistics_buffer;
AliasedFloat64Array heap_space_statistics_buffer;
AliasedFloat64Array heap_code_statistics_buffer;

void MemoryInfo(MemoryTracker* tracker) const override;
SET_SELF_SIZE(BindingData)
SET_MEMORY_INFO_NAME(BindingData)
};

} // namespace v8_utils

} // namespace node

#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

#endif // SRC_NODE_V8_H_