Skip to content

Commit

Permalink
ClassHandle TemplateSpecific and Unwrap refactor
Browse files Browse the repository at this point in the history
Removes a lot of repeated code
  • Loading branch information
laverdet committed Feb 28, 2018
1 parent 27d2616 commit 48cf991
Show file tree
Hide file tree
Showing 23 changed files with 54 additions and 109 deletions.
5 changes: 0 additions & 5 deletions src/context_handle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ ContextHandle::ContextHandle(
shared_ptr<RemoteHandle<Value>> global
) : isolate(std::move(isolate)), context(std::move(context)), global(std::move(global)) {}

IsolateEnvironment::IsolateSpecific<FunctionTemplate>& ContextHandle::TemplateSpecific() {
static IsolateEnvironment::IsolateSpecific<FunctionTemplate> tmpl;
return tmpl;
}

Local<FunctionTemplate> ContextHandle::Definition() {
return Inherit<TransferableHandle>(MakeClass(
"Context", nullptr,
Expand Down
1 change: 0 additions & 1 deletion src/context_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ class ContextHandle : public TransferableHandle {
std::shared_ptr<RemoteHandle<v8::Context>> context,
std::shared_ptr<RemoteHandle<v8::Value>> global
);
static IsolateEnvironment::IsolateSpecific<v8::FunctionTemplate>& TemplateSpecific();
static v8::Local<v8::FunctionTemplate> Definition();
std::unique_ptr<Transferable> TransferOut() final;
void CheckDisposed();
Expand Down
10 changes: 0 additions & 10 deletions src/external_copy_handle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ ExternalCopyHandle::~ExternalCopyHandle() {
}
}

IsolateEnvironment::IsolateSpecific<FunctionTemplate>& ExternalCopyHandle::TemplateSpecific() {
static IsolateEnvironment::IsolateSpecific<FunctionTemplate> tmpl;
return tmpl;
}

Local<FunctionTemplate> ExternalCopyHandle::Definition() {
return Inherit<TransferableHandle>(MakeClass(
"ExternalCopy", ParameterizeCtor<decltype(&New), &New>(),
Expand Down Expand Up @@ -128,11 +123,6 @@ Local<Value> ExternalCopyIntoHandle::ExternalCopyIntoTransferable::TransferIn()

ExternalCopyIntoHandle::ExternalCopyIntoHandle(shared_ptr<ExternalCopy> value, bool transfer_in) : value(std::move(value)), transfer_in(transfer_in) {}

IsolateEnvironment::IsolateSpecific<FunctionTemplate>& ExternalCopyIntoHandle::TemplateSpecific() {
static IsolateEnvironment::IsolateSpecific<FunctionTemplate> tmpl;
return tmpl;
}

Local<FunctionTemplate> ExternalCopyIntoHandle::Definition() {
return Inherit<TransferableHandle>(MakeClass("ExternalCopyInto", nullptr));
}
Expand Down
2 changes: 0 additions & 2 deletions src/external_copy_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ class ExternalCopyHandle : public TransferableHandle {
ExternalCopyHandle(const ExternalCopyHandle&) = delete;
ExternalCopyHandle& operator= (const ExternalCopyHandle&) = delete;
~ExternalCopyHandle() final;
static IsolateEnvironment::IsolateSpecific<v8::FunctionTemplate>& TemplateSpecific();
static v8::Local<v8::FunctionTemplate> Definition();
std::unique_ptr<Transferable> TransferOut() final;

Expand Down Expand Up @@ -57,7 +56,6 @@ class ExternalCopyIntoHandle : public TransferableHandle {

public:
explicit ExternalCopyIntoHandle(std::shared_ptr<ExternalCopy> value, bool transfer_in);
static IsolateEnvironment::IsolateSpecific<v8::FunctionTemplate>& TemplateSpecific();
static v8::Local<v8::FunctionTemplate> Definition();
std::unique_ptr<Transferable> TransferOut() final;
};
Expand Down
5 changes: 0 additions & 5 deletions src/isolate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ class LibraryHandle : public TransferableHandle {
};

public:
static IsolateEnvironment::IsolateSpecific<FunctionTemplate>& TemplateSpecific() {
static IsolateEnvironment::IsolateSpecific<FunctionTemplate> tmpl;
return tmpl;
}

static Local<FunctionTemplate> Definition() {
return Inherit<TransferableHandle>(MakeClass(
"isolated_vm", nullptr,
Expand Down
2 changes: 1 addition & 1 deletion src/isolate/class_handle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
namespace ivm {

ClassHandle* _ClassHandleUnwrap(v8::Local<v8::Object> handle) {
return ClassHandle::Unwrap(handle);
return ClassHandle::UnwrapClassHandle(handle);
}

} // namespace ivm
23 changes: 21 additions & 2 deletions src/isolate/class_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,15 @@ class ClassHandle {
PrivateConstructorError(info);
}

/**
* Returns a unique IsolateSpecific for each subclass
*/
template <typename T>
static IsolateEnvironment::IsolateSpecific<v8::FunctionTemplate>& TemplateSpecific() {
static IsolateEnvironment::IsolateSpecific<v8::FunctionTemplate> tmpl;
return tmpl;
}

protected:
/**
* Sets up this object's FunctionTemplate inside the current isolate
Expand Down Expand Up @@ -389,7 +398,7 @@ class ClassHandle {
*/
template <typename T>
static v8::Local<v8::FunctionTemplate> GetFunctionTemplate() {
IsolateEnvironment::IsolateSpecific<v8::FunctionTemplate>& specific = T::TemplateSpecific();
IsolateEnvironment::IsolateSpecific<v8::FunctionTemplate>& specific = TemplateSpecific<T>();
v8::MaybeLocal<v8::FunctionTemplate> maybe_tmpl = specific.Deref();
v8::Local<v8::FunctionTemplate> tmpl;
if (maybe_tmpl.ToLocal(&tmpl)) {
Expand All @@ -414,7 +423,17 @@ class ClassHandle {
/**
* Pull out native pointer from v8 handle
*/
static ClassHandle* Unwrap(v8::Local<v8::Object> handle) {
template <typename T>
static T* Unwrap(v8::Local<v8::Object> handle) {
assert(!handle.IsEmpty());
if (!ClassHandle::GetFunctionTemplate<T>()->HasInstance(handle)) {
return nullptr;
}
assert(handle->InternalFieldCount() > 0);
return dynamic_cast<T*>(static_cast<ClassHandle*>(handle->GetAlignedPointerFromInternalField(0)));
}

static ClassHandle* UnwrapClassHandle(v8::Local<v8::Object> handle) {
assert(!handle.IsEmpty());
assert(handle->InternalFieldCount() > 0);
return static_cast<ClassHandle*>(handle->GetAlignedPointerFromInternalField(0));
Expand Down
7 changes: 1 addition & 6 deletions src/isolate/stack_trace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Local<String> RenderErrorStack(Local<Value> data) {
);
} else {
// StackTraceHolder
StackTraceHolder& that = *dynamic_cast<StackTraceHolder*>(ClassHandle::Unwrap(data.As<Object>()));
StackTraceHolder& that = *ClassHandle::Unwrap<StackTraceHolder>(data.As<Object>());
Local<StackTrace> stack_trace = Deref(that.stack_trace);
std::stringstream ss;
int size = stack_trace->GetFrameCount();
Expand Down Expand Up @@ -126,11 +126,6 @@ void AttachStackGetter(Local<Object> error, Local<Value> data) {
*/
StackTraceHolder::StackTraceHolder(Local<StackTrace> stack_handle) : stack_trace(Isolate::GetCurrent(), stack_handle) {}

IsolateEnvironment::IsolateSpecific<FunctionTemplate>& StackTraceHolder::TemplateSpecific() {
static IsolateEnvironment::IsolateSpecific<FunctionTemplate> tmpl;
return tmpl;
}

Local<FunctionTemplate> StackTraceHolder::Definition() {
return MakeClass("StackTraceHolder", nullptr);
}
Expand Down
1 change: 0 additions & 1 deletion src/isolate/stack_trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class StackTraceHolder : public ClassHandle {
public:
v8::Persistent<v8::StackTrace, v8::CopyablePersistentTraits<v8::StackTrace>> stack_trace;
explicit StackTraceHolder(v8::Local<v8::StackTrace> stack_handle);
static IsolateEnvironment::IsolateSpecific<v8::FunctionTemplate>& TemplateSpecific();
static v8::Local<v8::FunctionTemplate> Definition();
static void AttachStack(v8::Local<v8::Object> error, v8::Local<v8::StackTrace> stack);
static void ChainStack(v8::Local<v8::Object> error, v8::Local<v8::StackTrace> stack);
Expand Down
58 changes: 28 additions & 30 deletions src/isolate_handle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,6 @@ Local<Value> IsolateHandle::IsolateHandleTransferable::TransferIn() {

IsolateHandle::IsolateHandle(shared_ptr<IsolateHolder> isolate) : isolate(std::move(isolate)) {}

IsolateEnvironment::IsolateSpecific<FunctionTemplate>& IsolateHandle::TemplateSpecific() {
static IsolateEnvironment::IsolateSpecific<FunctionTemplate> tmpl;
return tmpl;
}

Local<FunctionTemplate> IsolateHandle::Definition() {
return Inherit<TransferableHandle>(MakeClass(
"Isolate", ParameterizeCtor<decltype(&New), &New>(),
Expand Down Expand Up @@ -158,15 +153,14 @@ unique_ptr<ClassHandle> IsolateHandle::New(MaybeLocal<Object> maybe_options) {
// Set snapshot
Local<Value> snapshot_handle = Unmaybe(options->Get(context, v8_symbol("snapshot")));
if (!snapshot_handle->IsUndefined()) {
if (
snapshot_handle->IsObject() &&
ClassHandle::GetFunctionTemplate<ExternalCopyHandle>()->HasInstance(snapshot_handle.As<Object>())
) {
ExternalCopyHandle* copy_handle = dynamic_cast<ExternalCopyHandle*>(ClassHandle::Unwrap(snapshot_handle.As<Object>()));
ExternalCopyArrayBuffer* copy_ptr = dynamic_cast<ExternalCopyArrayBuffer*>(copy_handle->GetValue().get());
if (copy_ptr != nullptr) {
snapshot_blob = copy_ptr->GetSharedPointer();
snapshot_blob_length = copy_ptr->Length();
if (snapshot_handle->IsObject()) {
ExternalCopyHandle* copy_handle = ClassHandle::Unwrap<ExternalCopyHandle>(snapshot_handle.As<Object>());
if (copy_handle != nullptr) {
ExternalCopyArrayBuffer* copy_ptr = dynamic_cast<ExternalCopyArrayBuffer*>(copy_handle->GetValue().get());
if (copy_ptr != nullptr) {
snapshot_blob = copy_ptr->GetSharedPointer();
snapshot_blob_length = copy_ptr->Length();
}
}
}
if (!snapshot_blob) {
Expand Down Expand Up @@ -279,10 +273,12 @@ struct CompileScriptRunner : public ThreePhaseTask {
shared_ptr<IsolateHolder> isolate;
unique_ptr<ExternalCopyString> code_string;
unique_ptr<ScriptOriginHolder> script_origin_holder;
shared_ptr<ExternalCopyArrayBuffer> cached_data_blob; // also phase 3
shared_ptr<void> cached_data_in;
size_t cached_data_in_size = 0;
bool produce_cached_data { false };
// phase 3
shared_ptr<RemoteHandle<UnboundScript>> script;
shared_ptr<ExternalCopyArrayBuffer> cached_data_out;
bool supplied_cached_data { false };
bool cached_data_rejected { false };

Expand All @@ -297,15 +293,17 @@ struct CompileScriptRunner : public ThreePhaseTask {
// Get cached data blob
Local<Value> cached_data_handle = Unmaybe(options->Get(context, v8_symbol("cachedData")));
if (!cached_data_handle->IsUndefined()) {
if (
!cached_data_handle->IsObject() ||
!ClassHandle::GetFunctionTemplate<ExternalCopyHandle>()->HasInstance(cached_data_handle.As<Object>())
) {
throw js_type_error("`cachedData` must be an ExternalCopy to ArrayBuffer");
if (cached_data_handle->IsObject()) {
ExternalCopyHandle* copy_handle = ClassHandle::Unwrap<ExternalCopyHandle>(cached_data_handle.As<Object>());
if (copy_handle != nullptr) {
ExternalCopyArrayBuffer* copy_ptr = dynamic_cast<ExternalCopyArrayBuffer*>(copy_handle->GetValue().get());
if (copy_ptr != nullptr) {
cached_data_in = copy_ptr->GetSharedPointer();
cached_data_in_size = copy_ptr->Length();
}
}
}
ExternalCopyHandle& copy_handle = *dynamic_cast<ExternalCopyHandle*>(ClassHandle::Unwrap(cached_data_handle.As<Object>()));
cached_data_blob = std::dynamic_pointer_cast<ExternalCopyArrayBuffer>(copy_handle.GetValue());
if (!cached_data_blob) {
if (!cached_data_in) {
throw js_type_error("`cachedData` must be an ExternalCopy to ArrayBuffer");
}
}
Expand All @@ -326,9 +324,9 @@ struct CompileScriptRunner : public ThreePhaseTask {
ScriptOrigin script_origin = script_origin_holder->ToScriptOrigin();
ScriptCompiler::CompileOptions compile_options = ScriptCompiler::kNoCompileOptions;
unique_ptr<ScriptCompiler::CachedData> cached_data = nullptr;
if (cached_data_blob) {
if (cached_data_in) {
compile_options = ScriptCompiler::kConsumeCodeCache;
cached_data = std::make_unique<ScriptCompiler::CachedData>(reinterpret_cast<const uint8_t*>(cached_data_blob->Data()), cached_data_blob->Length());
cached_data = std::make_unique<ScriptCompiler::CachedData>(reinterpret_cast<const uint8_t*>(cached_data_in.get()), cached_data_in_size);
} else if (produce_cached_data) {
compile_options = ScriptCompiler::kProduceCodeCache;
}
Expand All @@ -338,14 +336,14 @@ struct CompileScriptRunner : public ThreePhaseTask {
));

// Check cached data flags
if (cached_data_blob) {
if (cached_data_in) {
supplied_cached_data = true;
cached_data_rejected = source.GetCachedData()->rejected;
cached_data_blob.reset();
cached_data_in.reset();
} else if (produce_cached_data) {
const ScriptCompiler::CachedData* cached_data = source.GetCachedData();
assert(cached_data != nullptr);
cached_data_blob = std::make_shared<ExternalCopyArrayBuffer>((void*)cached_data->data, cached_data->length);
cached_data_out = std::make_shared<ExternalCopyArrayBuffer>((void*)cached_data->data, cached_data->length);
}
}

Expand All @@ -356,8 +354,8 @@ struct CompileScriptRunner : public ThreePhaseTask {
Local<Context> context = isolate->GetCurrentContext();
if (supplied_cached_data) {
Unmaybe(value->Set(context, v8_symbol("cachedDataRejected"), Boolean::New(isolate, cached_data_rejected)));
} else if (cached_data_blob) {
Unmaybe(value->Set(context, v8_symbol("cachedData"), ClassHandle::NewInstance<ExternalCopyHandle>(cached_data_blob)));
} else if (cached_data_out) {
Unmaybe(value->Set(context, v8_symbol("cachedData"), ClassHandle::NewInstance<ExternalCopyHandle>(std::move(cached_data_out))));
}
return value;
}
Expand Down
1 change: 0 additions & 1 deletion src/isolate_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class IsolateHandle : public TransferableHandle {

public:
explicit IsolateHandle(std::shared_ptr<IsolateHolder> isolate);
static IsolateEnvironment::IsolateSpecific<v8::FunctionTemplate>& TemplateSpecific();
static v8::Local<v8::FunctionTemplate> Definition();
static std::unique_ptr<ClassHandle> New(v8::MaybeLocal<v8::Object> maybe_options);
std::unique_ptr<Transferable> TransferOut() final;
Expand Down
5 changes: 0 additions & 5 deletions src/lib_handle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ Local<Value> LibHandle::LibTransferable::TransferIn() {
/**
* ivm.lib API container
*/
IsolateEnvironment::IsolateSpecific<FunctionTemplate>& LibHandle::TemplateSpecific() {
static IsolateEnvironment::IsolateSpecific<FunctionTemplate> tmpl;
return tmpl;
}

Local<FunctionTemplate> LibHandle::Definition() {
return Inherit<TransferableHandle>(MakeClass(
"Lib", nullptr,
Expand Down
1 change: 0 additions & 1 deletion src/lib_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class LibHandle : public TransferableHandle {
v8::Local<v8::Value> Hrtime(v8::MaybeLocal<v8::Array> maybe_diff);

public:
static IsolateEnvironment::IsolateSpecific<v8::FunctionTemplate>& TemplateSpecific();
static v8::Local<v8::FunctionTemplate> Definition();
std::unique_ptr<Transferable> TransferOut() final;
};
Expand Down
5 changes: 0 additions & 5 deletions src/native_module_handle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ Local<Value> NativeModuleHandle::NativeModuleTransferable::TransferIn() {
*/
NativeModuleHandle::NativeModuleHandle(shared_ptr<NativeModule> module) : module(std::move(module)) {}

IsolateEnvironment::IsolateSpecific<FunctionTemplate>& NativeModuleHandle::TemplateSpecific() {
static IsolateEnvironment::IsolateSpecific<FunctionTemplate> tmpl;
return tmpl;
}

Local<FunctionTemplate> NativeModuleHandle::Definition() {
return Inherit<TransferableHandle>(MakeClass(
"NativeModule", ParameterizeCtor<decltype(&New), &New>(),
Expand Down
1 change: 0 additions & 1 deletion src/native_module_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class NativeModuleHandle : public TransferableHandle {

public:
explicit NativeModuleHandle(std::shared_ptr<NativeModule> module);
static IsolateEnvironment::IsolateSpecific<v8::FunctionTemplate>& TemplateSpecific();
static v8::Local<v8::FunctionTemplate> Definition();
static std::unique_ptr<NativeModuleHandle> New(v8::Local<v8::String> value);
std::unique_ptr<Transferable> TransferOut() final;
Expand Down
10 changes: 0 additions & 10 deletions src/reference_handle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ ReferenceHandle::ReferenceHandle(
TypeOf type_of
) : isolate(std::move(isolate)), reference(std::move(reference)), context(std::move(context)), type_of(type_of) {}

IsolateEnvironment::IsolateSpecific<FunctionTemplate>& ReferenceHandle::TemplateSpecific() {
static IsolateEnvironment::IsolateSpecific<FunctionTemplate> tmpl;
return tmpl;
}

Local<FunctionTemplate> ReferenceHandle::Definition() {
return Inherit<TransferableHandle>(MakeClass(
"Reference", ParameterizeCtor<decltype(&New), &New>(),
Expand Down Expand Up @@ -397,11 +392,6 @@ Local<Value> DereferenceHandle::DereferenceHandleTransferable::TransferIn() {
}
}

IsolateEnvironment::IsolateSpecific<FunctionTemplate>& DereferenceHandle::TemplateSpecific() {
static IsolateEnvironment::IsolateSpecific<FunctionTemplate> tmpl;
return tmpl;
}

Local<FunctionTemplate> DereferenceHandle::Definition() {
return Inherit<TransferableHandle>(MakeClass("Dereference", nullptr));
}
Expand Down
2 changes: 0 additions & 2 deletions src/reference_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ class ReferenceHandle : public TransferableHandle {
std::shared_ptr<RemoteHandle<v8::Context>> context,
TypeOf type_of
);
static IsolateEnvironment::IsolateSpecific<v8::FunctionTemplate>& TemplateSpecific();
static v8::Local<v8::FunctionTemplate> Definition();
std::unique_ptr<Transferable> TransferOut() final;
static std::unique_ptr<ReferenceHandle> New(v8::Local<v8::Value> var);
Expand Down Expand Up @@ -95,7 +94,6 @@ class DereferenceHandle : public TransferableHandle {
};

public:
static IsolateEnvironment::IsolateSpecific<v8::FunctionTemplate>& TemplateSpecific();
static v8::Local<v8::FunctionTemplate> Definition();
DereferenceHandle(std::shared_ptr<IsolateHolder> isolate, std::shared_ptr<RemoteHandle<v8::Value>> reference);
std::unique_ptr<Transferable> TransferOut() final;
Expand Down
5 changes: 0 additions & 5 deletions src/script_handle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ ScriptHandle::ScriptHandle(
shared_ptr<RemoteHandle<UnboundScript>> script
) : isolate(std::move(isolate)), script(std::move(script)) {}

IsolateEnvironment::IsolateSpecific<FunctionTemplate>& ScriptHandle::TemplateSpecific() {
static IsolateEnvironment::IsolateSpecific<FunctionTemplate> tmpl;
return tmpl;
}

Local<FunctionTemplate> ScriptHandle::Definition() {
return Inherit<TransferableHandle>(MakeClass(
"Script", nullptr,
Expand Down
1 change: 0 additions & 1 deletion src/script_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class ScriptHandle : public TransferableHandle {
std::shared_ptr<RemoteHandle<v8::UnboundScript>> script
);

static IsolateEnvironment::IsolateSpecific<v8::FunctionTemplate>& TemplateSpecific();
static v8::Local<v8::FunctionTemplate> Definition();
std::unique_ptr<Transferable> TransferOut() final;

Expand Down
5 changes: 0 additions & 5 deletions src/session_handle.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,6 @@ SessionHandle::SessionHandle(IsolateEnvironment& isolate) : session(std::make_sh
session->isolate = IsolateEnvironment::GetCurrentHolder();
}

IsolateEnvironment::IsolateSpecific<FunctionTemplate>& SessionHandle::TemplateSpecific() {
static IsolateEnvironment::IsolateSpecific<FunctionTemplate> tmpl;
return tmpl;
}

Local<FunctionTemplate> SessionHandle::Definition() {
return MakeClass(
"Session", nullptr,
Expand Down
1 change: 0 additions & 1 deletion src/session_handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class SessionHandle : public ClassHandle {

public:
explicit SessionHandle(IsolateEnvironment& isolate);
static IsolateEnvironment::IsolateSpecific<v8::FunctionTemplate>& TemplateSpecific();
static v8::Local<v8::FunctionTemplate> Definition();

void CheckDisposed();
Expand Down
Loading

0 comments on commit 48cf991

Please sign in to comment.