@@ -11,6 +11,7 @@ extern "C" {
1111#include " ../include/lock_master.h"
1212#include " ../include/functions/copy.h"
1313#include " ../include/{{ filename }}.h"
14+ #include " nodegit_wrapper.cc"
1415
1516{% each dependencies as dependency %}
1617 #include " {{ dependency }}"
@@ -23,47 +24,7 @@ using namespace v8;
2324using namespace node ;
2425
2526{% if cType %}
26- {{ cppClassName }}::{{ cppClassName }}({{ cType }} *raw, bool selfFreeing, Local<v8::Object> owner) {
27- if (!owner.IsEmpty ()) {
28- // if we have an owner, there are two options - either we duplicate the raw object
29- // (so we own the duplicate, and can self-free it)
30- // or we keep a handle on the owner so it doesn't get garbage collected
31- // while this wrapper is accessible
32- {% if dupFunction %}
33- {% if shouldAlloc %}
34- this ->raw = ({{ cType }} *)malloc (sizeof ({{ cType }}));
35- {{ dupFunction }}(this ->raw , raw);
36- {% else %}
37- {{ dupFunction }}(&this ->raw , raw);
38- {% endif %}
39- selfFreeing = true ;
40- {% else %}
41- this ->owner .Reset (owner);
42- this ->raw = raw;
43- {% endif %}
44- } else {
45- this ->raw = raw;
46- }
47- this ->selfFreeing = selfFreeing;
48-
49- if (selfFreeing) {
50- SelfFreeingInstanceCount++;
51- } else {
52- NonSelfFreeingConstructedCount++;
53- }
54-
55- }
56-
5727 {{ cppClassName }}::~{{ cppClassName }}() {
58- {% if freeFunctionName %}
59- if (this ->selfFreeing ) {
60- {{ freeFunctionName }}(this ->raw );
61- SelfFreeingInstanceCount--;
62-
63- this ->raw = NULL ;
64- }
65- {% endif %}
66-
6728 // this will cause an error if you have a non-self-freeing object that also needs
6829 // to save values. Since the object that will eventually free the object has no
6930 // way of knowing to free these values.
@@ -104,61 +65,13 @@ using namespace node;
10465 {% endif %}
10566 {% endeach %}
10667
107- Nan::SetMethod (tpl, " getSelfFreeingInstanceCount" , GetSelfFreeingInstanceCount);
108- Nan::SetMethod (tpl, " getNonSelfFreeingConstructedCount" , GetNonSelfFreeingConstructedCount);
68+ InitializeTemplate (tpl);
10969
11070 Local<Function> _constructor_template = Nan::GetFunction (tpl).ToLocalChecked ();
11171 constructor_template.Reset (_constructor_template);
11272 Nan::Set (target, Nan::New (" {{ jsClassName }}" ).ToLocalChecked (), _constructor_template);
11373 }
11474
115- NAN_METHOD ({{ cppClassName }}::JSNewFunction) {
116-
117- if (info.Length () == 0 || !info[0 ]->IsExternal ()) {
118- {% if createFunctionName %}
119- return Nan::ThrowError (" A new {{ cppClassName }} cannot be instantiated. Use {{ jsCreateFunctionName }} instead." );
120- {% else %}
121- return Nan::ThrowError (" A new {{ cppClassName }} cannot be instantiated." );
122- {% endif %}
123- }
124-
125- {{ cppClassName }}* object = new {{ cppClassName }}(static_cast <{{ cType }} *>(
126- Local<External>::Cast (info[0 ])->Value ()),
127- Nan::To<bool >(info[1 ]).FromJust (),
128- info.Length () >= 3 && !info[2 ].IsEmpty () && info[2 ]->IsObject () ? info[2 ]->ToObject () : Local<v8::Object>()
129- );
130- object->Wrap (info.This ());
131-
132- info.GetReturnValue ().Set (info.This ());
133- }
134-
135- Local<v8::Value> {{ cppClassName }}::New(const {{ cType }} *raw, bool selfFreeing, Local<v8::Object> owner) {
136- Nan::EscapableHandleScope scope;
137- Local<v8::Value> argv[3 ] = { Nan::New<External>((void *)raw), Nan::New (selfFreeing), owner };
138- return scope.Escape (
139- Nan::NewInstance (
140- Nan::New ({{ cppClassName }}::constructor_template),
141- owner.IsEmpty () ? 2 : 3 , // passing an empty handle as part of the arguments causes a crash
142- argv
143- ).ToLocalChecked ());
144- }
145-
146- NAN_METHOD ({{ cppClassName }}::GetSelfFreeingInstanceCount) {
147- info.GetReturnValue ().Set (SelfFreeingInstanceCount);
148- }
149-
150- NAN_METHOD ({{ cppClassName }}::GetNonSelfFreeingConstructedCount) {
151- info.GetReturnValue ().Set (NonSelfFreeingConstructedCount);
152- }
153-
154- {{ cType }} *{{ cppClassName }}::GetValue() {
155- return this ->raw ;
156- }
157-
158- void {{ cppClassName }}::ClearValue() {
159- this ->raw = NULL ;
160- }
161-
16275{% else %}
16376
16477 void {{ cppClassName }}::InitializeComponent(Local<v8::Object> target) {
@@ -191,9 +104,8 @@ using namespace node;
191104
192105{% partial fields . %}
193106
194- {% if not cTypeIsUndefined %}
195- Nan::Persistent<Function> {{ cppClassName }}::constructor_template;
107+ {%if cType %}
108+ // force base class template instantiation, to make sure we get all the
109+ // methods, statics, etc.
110+ template class NodeGitWrapper <{{ cppClassName }}Traits>;
196111{% endif %}
197-
198- int {{ cppClassName }}::SelfFreeingInstanceCount;
199- int {{ cppClassName }}::NonSelfFreeingConstructedCount;
0 commit comments