Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit dedf7af

Browse files
committed
Copy constructor and copy assignement
1 parent 3bd1ccc commit dedf7af

3 files changed

Lines changed: 53 additions & 1 deletion

File tree

src/callback_bridge.h

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ template <typename T, typename L = void*>
1212
class CallbackBridge {
1313
public:
1414
CallbackBridge(v8::Local<v8::Function>, bool);
15+
CallbackBridge(const CallbackBridge<T,L> &);
16+
CallbackBridge<T,L>& operator=(const CallbackBridge<T,L> &);
1517
virtual ~CallbackBridge();
1618

1719
// Executes the callback
@@ -21,6 +23,8 @@ class CallbackBridge {
2123
// We will expose a bridge object to the JS callback that wraps this instance so we don't loose context.
2224
// This is the V8 constructor for such objects.
2325
static Nan::MaybeLocal<v8::Function> get_wrapper_constructor();
26+
void init_uv(void);
27+
void init_wrapper(void);
2428
static void async_gone(uv_handle_t *handle);
2529
static NAN_METHOD(New);
2630
static NAN_METHOD(ReturnCallback);
@@ -59,15 +63,61 @@ CallbackBridge<T, L>::CallbackBridge(v8::Local<v8::Function> callback, bool is_s
5963
* This is invoked from the main JavaScript thread.
6064
* V8 context is available.
6165
*/
62-
Nan::HandleScope scope;
66+
init_uv();
67+
init_wrapper();
68+
}
69+
70+
template <typename T, typename L>
71+
CallbackBridge<T, L>::CallbackBridge(const CallbackBridge<T,L>& other) : callback(new Nan::Callback(other.callback->GetFunction())), is_sync(other.is_sync) {
72+
/*
73+
* This is invoked from the main JavaScript thread.
74+
* V8 context is available.
75+
*/
76+
init_uv();
77+
init_wrapper();
78+
}
79+
80+
template <typename T, typename L>
81+
CallbackBridge<T, L>&
82+
CallbackBridge<T, L>::operator= (const CallbackBridge<T,L>& other)
83+
{
84+
/*
85+
* This is invoked from the main JavaScript thread.
86+
* V8 context is available.
87+
*/
88+
if (other != *this) {
89+
delete this->callback;
90+
this->wrapper.Reset();
91+
uv_cond_destroy(&this->condition_variable);
92+
uv_mutex_destroy(&this->cv_mutex);
93+
if (!is_sync) {
94+
uv_close(this->async, &async_gone);
95+
}
96+
97+
this->callback = new Nan::Callback(other.callback->GetFunction());
98+
this->is_sync = other.is_sync;
99+
init_uv();
100+
init_wrapper();
101+
}
102+
return *this;
103+
}
104+
105+
template <typename T, typename L>
106+
void
107+
CallbackBridge<T, L>::init_uv(void) {
63108
uv_mutex_init(&this->cv_mutex);
64109
uv_cond_init(&this->condition_variable);
65110
if (!is_sync) {
66111
this->async = new uv_async_t;
67112
this->async->data = (void*) this;
68113
uv_async_init(uv_default_loop(), this->async, (uv_async_cb) dispatched_async_uv_callback);
69114
}
115+
}
70116

117+
template <typename T, typename L>
118+
void
119+
CallbackBridge<T, L>::init_wrapper(void) {
120+
Nan::HandleScope scope;
71121
v8::Local<v8::Function> func = CallbackBridge<T, L>::get_wrapper_constructor().ToLocalChecked();
72122
wrapper.Reset(Nan::NewInstance(func).ToLocalChecked());
73123
Nan::SetInternalFieldPointer(Nan::New(wrapper), 0, this);

src/custom_function_bridge.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
class CustomFunctionBridge : public CallbackBridge<Sass_Value*> {
1010
public:
1111
CustomFunctionBridge(v8::Local<v8::Function> cb, bool is_sync) : CallbackBridge<Sass_Value*>(cb, is_sync) {}
12+
CustomFunctionBridge(const CustomFunctionBridge& other) : CallbackBridge<Sass_Value*>(other) {}
1213

1314
private:
1415
Sass_Value* post_process_return_value(v8::Local<v8::Value>) const;

src/custom_importer_bridge.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ typedef Sass_Import_List SassImportList;
1111
class CustomImporterBridge : public CallbackBridge<SassImportList> {
1212
public:
1313
CustomImporterBridge(v8::Local<v8::Function> cb, bool is_sync) : CallbackBridge<SassImportList>(cb, is_sync) {}
14+
CustomImporterBridge(const CustomImporterBridge& other) : CallbackBridge<SassImportList>(other) {}
1415

1516
private:
1617
SassImportList post_process_return_value(v8::Local<v8::Value>) const;

0 commit comments

Comments
 (0)