Skip to content
This repository has been archived by the owner on Aug 31, 2018. It is now read-only.

Commit

Permalink
[squash] deglobalize platform
Browse files Browse the repository at this point in the history
  • Loading branch information
addaleax committed Oct 4, 2017
1 parent 619e4a2 commit 2c9cb94
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 43 deletions.
4 changes: 4 additions & 0 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ inline uint32_t* IsolateData::zero_fill_field() const {
return zero_fill_field_;
}

inline WorkerSupportingPlatform* IsolateData::platform() const {
return platform_;
}

inline Environment::AsyncHooks::AsyncHooks(v8::Isolate* isolate)
: isolate_(isolate),
fields_(isolate, kFieldsCount),
Expand Down
17 changes: 9 additions & 8 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ using v8::StackFrame;
using v8::StackTrace;
using v8::String;

IsolateData::IsolateData(Isolate* isolate, uv_loop_t* event_loop,
IsolateData::IsolateData(Isolate* isolate,
uv_loop_t* event_loop,
WorkerSupportingPlatform* platform,
uint32_t* zero_fill_field) :

// Create string and private symbol properties as internalized one byte strings.
Expand Down Expand Up @@ -62,16 +64,15 @@ IsolateData::IsolateData(Isolate* isolate, uv_loop_t* event_loop,
#undef V
isolate_(isolate),
event_loop_(event_loop),
zero_fill_field_(zero_fill_field) {
#ifdef NODE_USE_V8_PLATFORM
NodePlatform::platform->RegisterIsolate(this, event_loop);
#endif
zero_fill_field_(zero_fill_field),
platform_(platform) {
if (platform_ != nullptr)
platform_->RegisterIsolate(this, event_loop);
}

IsolateData::~IsolateData() {
#ifdef NODE_USE_V8_PLATFORM
NodePlatform::platform->UnregisterIsolate(this);
#endif
if (platform_ != nullptr)
platform_->UnregisterIsolate(this);
}

void Environment::Start(int argc,
Expand Down
3 changes: 3 additions & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,12 @@ struct node_async_ids {
class IsolateData {
public:
IsolateData(v8::Isolate* isolate, uv_loop_t* event_loop,
WorkerSupportingPlatform* platform = nullptr,
uint32_t* zero_fill_field = nullptr);
~IsolateData();
inline uv_loop_t* event_loop() const;
inline uint32_t* zero_fill_field() const;
inline WorkerSupportingPlatform* platform() const;

#define VP(PropertyName, StringValue) V(v8::Private, PropertyName)
#define VS(PropertyName, StringValue) V(v8::String, PropertyName)
Expand Down Expand Up @@ -371,6 +373,7 @@ class IsolateData {
v8::Isolate* const isolate_;
uv_loop_t* const event_loop_;
uint32_t* const zero_fill_field_;
WorkerSupportingPlatform* platform_;

DISALLOW_COPY_AND_ASSIGN(IsolateData);
};
Expand Down
41 changes: 26 additions & 15 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -259,32 +259,28 @@ static v8::Isolate* node_isolate;

node::DebugOptions debug_options;

#ifdef NODE_USE_V8_PLATFORM
NodePlatform* NodePlatform::platform = nullptr;
#endif

static struct {
#if NODE_USE_V8_PLATFORM
void Initialize(int thread_pool_size) {
tracing_agent_ =
trace_enabled ? new tracing::Agent() : nullptr;
NodePlatform::platform = new NodePlatform(thread_pool_size,
platform_ = new NodePlatform(thread_pool_size,
trace_enabled ? tracing_agent_->GetTracingController() : nullptr);
V8::InitializePlatform(NodePlatform::platform);
V8::InitializePlatform(platform_);
tracing::TraceEventHelper::SetTracingController(
trace_enabled ? tracing_agent_->GetTracingController() : nullptr);
}

void Dispose() {
NodePlatform::platform->Shutdown();
delete NodePlatform::platform;
NodePlatform::platform = nullptr;
platform_->Shutdown();
delete platform_;
platform_ = nullptr;
delete tracing_agent_;
tracing_agent_ = nullptr;
}

void DrainVMTasks(Isolate* isolate) {
NodePlatform::platform->DrainBackgroundTasks(isolate);
platform_->DrainBackgroundTasks(isolate);
}

#if HAVE_INSPECTOR
Expand All @@ -293,8 +289,7 @@ static struct {
// Inspector agent can't fail to start, but if it was configured to listen
// right away on the websocket port and fails to bind/etc, this will return
// false.
return env->inspector_agent()->Start(NodePlatform::platform,
script_path, options);
return env->inspector_agent()->Start(platform_, script_path, options);
}

bool InspectorStarted(Environment *env) {
Expand All @@ -310,7 +305,12 @@ static struct {
tracing_agent_->Stop();
}

NodePlatform* Platform() {
return platform_;
}

tracing::Agent* tracing_agent_;
NodePlatform* platform_;
#else // !NODE_USE_V8_PLATFORM
void Initialize(int thread_pool_size) {}
void Dispose() {}
Expand All @@ -326,6 +326,10 @@ static struct {
"so event tracing is not available.\n");
}
void StopTracingAgent() {}

NodePlatform* Platform() {
return nullptr;
}
#endif // !NODE_USE_V8_PLATFORM

#if !NODE_USE_V8_PLATFORM || !HAVE_INSPECTOR
Expand Down Expand Up @@ -4675,8 +4679,11 @@ int EmitExit(Environment* env) {
}


IsolateData* CreateIsolateData(Isolate* isolate, uv_loop_t* loop) {
return new IsolateData(isolate, loop);
IsolateData* CreateIsolateData(
Isolate* isolate,
uv_loop_t* loop,
WorkerSupportingPlatform* platform) {
return new IsolateData(isolate, loop, platform);
}


Expand Down Expand Up @@ -4824,7 +4831,11 @@ inline int Start(uv_loop_t* event_loop,
Locker locker(isolate);
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
IsolateData isolate_data(isolate, event_loop, allocator.zero_fill_field());
IsolateData isolate_data(
isolate,
event_loop,
v8_platform.Platform(),
allocator.zero_fill_field());
exit_code = Start(isolate, &isolate_data, argc, argv, exec_argc, exec_argv);
}

Expand Down
20 changes: 18 additions & 2 deletions src/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#endif

#include "v8.h" // NOLINT(build/include_order)
#include "v8-platform.h" // NOLINT(build/include_order)
#include "node_version.h" // NODE_MODULE_VERSION

#define NODE_MAKE_VERSION(major, minor, patch) \
Expand Down Expand Up @@ -209,8 +210,23 @@ NODE_EXTERN void Init(int* argc,
class IsolateData;
class Environment;

NODE_EXTERN IsolateData* CreateIsolateData(v8::Isolate* isolate,
struct uv_loop_s* loop);
class WorkerSupportingPlatform : public v8::Platform {
public:
virtual ~WorkerSupportingPlatform() { }
virtual void DrainBackgroundTasks(v8::Isolate* isolate) = 0;

// These will be called by the `IsolateData` creation/destruction functions.
virtual void RegisterIsolate(IsolateData* isolate_data,
struct uv_loop_s* loop) = 0;
virtual void UnregisterIsolate(IsolateData* isolate_data) = 0;
};

// If `platform` is passed, it will be used to register new worker instances.
// It can be `nullptr`, in which case creating new workers will not work.
NODE_EXTERN IsolateData* CreateIsolateData(
v8::Isolate* isolate,
struct uv_loop_s* loop,
WorkerSupportingPlatform* platform = nullptr);
NODE_EXTERN void FreeIsolateData(IsolateData* isolate_data);

NODE_EXTERN Environment* CreateEnvironment(IsolateData* isolate_data,
Expand Down
11 changes: 5 additions & 6 deletions src/node_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <unordered_map>

#include "libplatform/libplatform.h"
#include "node.h"
#include "node_mutex.h"
#include "uv.h"

Expand Down Expand Up @@ -62,12 +63,12 @@ class PerIsolatePlatformData {
TaskQueue<std::pair<v8::Task*, double>> foreground_delayed_tasks_;
};

class NodePlatform : public v8::Platform {
class NodePlatform : public WorkerSupportingPlatform {
public:
NodePlatform(int thread_pool_size, v8::TracingController* tracing_controller);
virtual ~NodePlatform() {}

void DrainBackgroundTasks(v8::Isolate* isolate);
void DrainBackgroundTasks(v8::Isolate* isolate) override;
void Shutdown();

// v8::Platform implementation.
Expand All @@ -83,10 +84,8 @@ class NodePlatform : public v8::Platform {

void FlushForegroundTasks(v8::Isolate* isolate);

void RegisterIsolate(IsolateData* isolate_data, uv_loop_t* loop);
void UnregisterIsolate(IsolateData* isolate_data);

static NodePlatform* platform;
void RegisterIsolate(IsolateData* isolate_data, uv_loop_t* loop) override;
void UnregisterIsolate(IsolateData* isolate_data) override;

private:
PerIsolatePlatformData* ForIsolate(v8::Isolate* isolate);
Expand Down
17 changes: 11 additions & 6 deletions test/cctest/node_test_fixture.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ class NodeTestFixture : public ::testing::Test {
public:
static uv_loop_t* CurrentLoop() { return &current_loop; }

node::WorkerSupportingPlatform* Platform() const { return platform_; }

protected:
v8::Isolate::CreateParams params_;
ArrayBufferAllocator allocator_;
Expand All @@ -84,24 +86,27 @@ class NodeTestFixture : public ::testing::Test {

virtual void SetUp() {
CHECK_EQ(0, uv_loop_init(&current_loop));
node::NodePlatform::platform = new node::NodePlatform(8, nullptr);
v8::V8::InitializePlatform(node::NodePlatform::platform);
platform_ = new node::NodePlatform(8, nullptr);
v8::V8::InitializePlatform(platform_);
v8::V8::Initialize();
params_.array_buffer_allocator = &allocator_;
isolate_ = v8::Isolate::New(params_);
}

virtual void TearDown() {
if (node::NodePlatform::platform == nullptr) return;
node::NodePlatform::platform->Shutdown();
if (platform_ == nullptr) return;
platform_->Shutdown();
while (uv_loop_alive(&current_loop)) {
uv_run(&current_loop, UV_RUN_ONCE);
}
v8::V8::ShutdownPlatform();
delete node::NodePlatform::platform;
node::NodePlatform::platform = nullptr;
delete platform_;
platform_ = nullptr;
CHECK_EQ(0, uv_loop_close(&current_loop));
}

private:
node::NodePlatform* platform_ = nullptr;
};

#endif // TEST_CCTEST_NODE_TEST_FIXTURE_H_
14 changes: 8 additions & 6 deletions test/cctest/test_environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ class EnvironmentTest : public NodeTestFixture {
public:
Env(const v8::HandleScope& handle_scope,
v8::Isolate* isolate,
const Argv& argv) {
const Argv& argv,
NodeTestFixture* test_fixture) {
context_ = v8::Context::New(isolate);
CHECK(!context_.IsEmpty());
isolate_data_ = CreateIsolateData(isolate,
NodeTestFixture::CurrentLoop());
NodeTestFixture::CurrentLoop(),
test_fixture->Platform());
CHECK_NE(nullptr, isolate_data_);
environment_ = CreateEnvironment(isolate_data_,
context_,
Expand Down Expand Up @@ -66,7 +68,7 @@ class EnvironmentTest : public NodeTestFixture {
TEST_F(EnvironmentTest, AtExitWithEnvironment) {
const v8::HandleScope handle_scope(isolate_);
const Argv argv;
Env env {handle_scope, isolate_, argv};
Env env {handle_scope, isolate_, argv, this};

AtExit(*env, at_exit_callback1);
RunAtExit(*env);
Expand All @@ -76,7 +78,7 @@ TEST_F(EnvironmentTest, AtExitWithEnvironment) {
TEST_F(EnvironmentTest, AtExitWithArgument) {
const v8::HandleScope handle_scope(isolate_);
const Argv argv;
Env env {handle_scope, isolate_, argv};
Env env {handle_scope, isolate_, argv, this};

std::string arg{"some args"};
AtExit(*env, at_exit_callback1, static_cast<void*>(&arg));
Expand All @@ -87,8 +89,8 @@ TEST_F(EnvironmentTest, AtExitWithArgument) {
TEST_F(EnvironmentTest, MultipleEnvironmentsPerIsolate) {
const v8::HandleScope handle_scope(isolate_);
const Argv argv;
Env env1 {handle_scope, isolate_, argv};
Env env2 {handle_scope, isolate_, argv};
Env env1 {handle_scope, isolate_, argv, this};
Env env2 {handle_scope, isolate_, argv, this};

AtExit(*env1, at_exit_callback1);
AtExit(*env2, at_exit_callback2);
Expand Down

0 comments on commit 2c9cb94

Please sign in to comment.