Skip to content

Commit

Permalink
src: move IsolateData out of Environment
Browse files Browse the repository at this point in the history
A follow-up commit is going to make IsolateData creation explicit.
In order for that to work, it needs to move out of Environment.

PR-URL: #7082
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
  • Loading branch information
bnoordhuis committed Jun 1, 2016
1 parent e4b78c6 commit 0301ce9
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 55 deletions.
20 changes: 9 additions & 11 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@

namespace node {

inline Environment::IsolateData* Environment::IsolateData::Get(
v8::Isolate* isolate) {
inline IsolateData* IsolateData::Get(v8::Isolate* isolate) {
return static_cast<IsolateData*>(isolate->GetData(kIsolateSlot));
}

inline Environment::IsolateData* Environment::IsolateData::GetOrCreate(
v8::Isolate* isolate, uv_loop_t* loop) {
inline IsolateData* IsolateData::GetOrCreate(v8::Isolate* isolate,
uv_loop_t* loop) {
IsolateData* isolate_data = Get(isolate);
if (isolate_data == nullptr) {
isolate_data = new IsolateData(isolate, loop);
Expand All @@ -31,7 +30,7 @@ inline Environment::IsolateData* Environment::IsolateData::GetOrCreate(
return isolate_data;
}

inline void Environment::IsolateData::Put() {
inline void IsolateData::Put() {
if (--ref_count_ == 0) {
isolate()->SetData(kIsolateSlot, nullptr);
delete this;
Expand All @@ -47,8 +46,7 @@ inline void Environment::IsolateData::Put() {
//
// One byte because our strings are ASCII and we can safely skip V8's UTF-8
// decoding step. It's a one-time cost, but why pay it when you don't have to?
inline Environment::IsolateData::IsolateData(v8::Isolate* isolate,
uv_loop_t* loop)
inline IsolateData::IsolateData(v8::Isolate* isolate, uv_loop_t* loop)
: event_loop_(loop),
isolate_(isolate),
#define V(PropertyName, StringValue) \
Expand All @@ -75,11 +73,11 @@ inline Environment::IsolateData::IsolateData(v8::Isolate* isolate,
#undef V
ref_count_(0) {}

inline uv_loop_t* Environment::IsolateData::event_loop() const {
inline uv_loop_t* IsolateData::event_loop() const {
return event_loop_;
}

inline v8::Isolate* Environment::IsolateData::isolate() const {
inline v8::Isolate* IsolateData::isolate() const {
return isolate_;
}

Expand Down Expand Up @@ -432,7 +430,7 @@ inline ares_task_list* Environment::cares_task_list() {
return &cares_task_list_;
}

inline Environment::IsolateData* Environment::isolate_data() const {
inline IsolateData* Environment::isolate_data() const {
return isolate_data_;
}

Expand Down Expand Up @@ -543,7 +541,7 @@ inline v8::Local<v8::Object> Environment::NewInternalFieldObject() {
#define VS(PropertyName, StringValue) V(v8::String, PropertyName, StringValue)
#define V(TypeName, PropertyName, StringValue) \
inline \
v8::Local<TypeName> Environment::IsolateData::PropertyName() const { \
v8::Local<TypeName> IsolateData::PropertyName() const { \
/* Strings are immutable so casting away const-ness here is okay. */ \
return const_cast<IsolateData*>(this)->PropertyName ## _.Get(isolate()); \
}
Expand Down
85 changes: 41 additions & 44 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,47 @@ struct ares_task_t {

RB_HEAD(ares_task_list, ares_task_t);

class IsolateData {
public:
static inline IsolateData* GetOrCreate(v8::Isolate* isolate, uv_loop_t* loop);
inline void Put();
inline uv_loop_t* event_loop() const;

#define VP(PropertyName, StringValue) V(v8::Private, PropertyName, StringValue)
#define VS(PropertyName, StringValue) V(v8::String, PropertyName, StringValue)
#define V(TypeName, PropertyName, StringValue) \
inline v8::Local<TypeName> PropertyName() const;
PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
PER_ISOLATE_STRING_PROPERTIES(VS)
#undef V
#undef VS
#undef VP

private:
static const int kIsolateSlot = NODE_ISOLATE_SLOT;

inline static IsolateData* Get(v8::Isolate* isolate);
inline explicit IsolateData(v8::Isolate* isolate, uv_loop_t* loop);
inline v8::Isolate* isolate() const;

uv_loop_t* const event_loop_;
v8::Isolate* const isolate_;

#define VP(PropertyName, StringValue) V(v8::Private, PropertyName, StringValue)
#define VS(PropertyName, StringValue) V(v8::String, PropertyName, StringValue)
#define V(TypeName, PropertyName, StringValue) \
v8::Eternal<TypeName> PropertyName ## _;
PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
PER_ISOLATE_STRING_PROPERTIES(VS)
#undef V
#undef VS
#undef VP

unsigned int ref_count_;

DISALLOW_COPY_AND_ASSIGN(IsolateData);
};

class Environment {
public:
class AsyncHooks {
Expand Down Expand Up @@ -568,9 +609,6 @@ class Environment {
static const int kContextEmbedderDataIndex = NODE_CONTEXT_EMBEDDER_DATA_INDEX;

private:
static const int kIsolateSlot = NODE_ISOLATE_SLOT;

class IsolateData;
inline Environment(v8::Local<v8::Context> context, uv_loop_t* loop);
inline ~Environment();
inline IsolateData* isolate_data() const;
Expand Down Expand Up @@ -615,47 +653,6 @@ class Environment {
ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V)
#undef V

// Per-thread, reference-counted singleton.
class IsolateData {
public:
static inline IsolateData* GetOrCreate(v8::Isolate* isolate,
uv_loop_t* loop);
inline void Put();
inline uv_loop_t* event_loop() const;

#define VP(PropertyName, StringValue) V(v8::Private, PropertyName, StringValue)
#define VS(PropertyName, StringValue) V(v8::String, PropertyName, StringValue)
#define V(TypeName, PropertyName, StringValue) \
inline v8::Local<TypeName> PropertyName() const;
PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
PER_ISOLATE_STRING_PROPERTIES(VS)
#undef V
#undef VS
#undef VP

private:
inline static IsolateData* Get(v8::Isolate* isolate);
inline explicit IsolateData(v8::Isolate* isolate, uv_loop_t* loop);
inline v8::Isolate* isolate() const;

uv_loop_t* const event_loop_;
v8::Isolate* const isolate_;

#define VP(PropertyName, StringValue) V(v8::Private, PropertyName, StringValue)
#define VS(PropertyName, StringValue) V(v8::String, PropertyName, StringValue)
#define V(TypeName, PropertyName, StringValue) \
v8::Eternal<TypeName> PropertyName ## _;
PER_ISOLATE_PRIVATE_SYMBOL_PROPERTIES(VP)
PER_ISOLATE_STRING_PROPERTIES(VS)
#undef V
#undef VS
#undef VP

unsigned int ref_count_;

DISALLOW_COPY_AND_ASSIGN(IsolateData);
};

DISALLOW_COPY_AND_ASSIGN(Environment);
};

Expand Down

0 comments on commit 0301ce9

Please sign in to comment.