Skip to content

Commit f7a1903

Browse files
committed
change the way classes free themselves
1 parent ed966eb commit f7a1903

6 files changed

Lines changed: 26 additions & 31 deletions

File tree

generate/partials/convert_to_v8.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// start convert_to_v8 block
12
{%if cppClassName == 'String' %}
23
{%if size %}
34
to = NanNew<String>({{= parsedName =}}, {{ size }});
@@ -39,9 +40,15 @@ to = tmpArray;
3940
{%endif%}
4041

4142
if ({{= parsedName =}} != NULL) {
43+
// {{= cppClassName }} {{= parsedName }}
44+
{%if cppClassName == 'Wrapper' %}
4245
to = {{ cppClassName }}::New((void *){{= parsedName =}});
46+
{%else%}
47+
to = {{ cppClassName }}::New((void *){{= parsedName =}}, false);
48+
{%endif%}
4349
} else {
4450
to = NanNull();
4551
}
4652

4753
{%endif%}
54+
// end convert_to_v8 block

generate/partials/sync_function.cc

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

22
{%partial doc .%}
33
NAN_METHOD({{ cppClassName }}::{{ cppFunctionName }}) {
4-
//NanScope();
54
{%partial guardArguments .%}
65

76
{%each .|returnsInfo 'true' as _return %}
@@ -36,17 +35,6 @@ from_{{ arg.name }}
3635
{%if not arg.lastArg %},{%endif%}
3736
{%endeach%}
3837
);
39-
/*
40-
{%each args|argsInfo as arg %}
41-
{%if arg.isCppClassStringOrArray %}
42-
{%if arg.freeFunctionName %}
43-
{{ arg.freeFunctionName }}(from_{{ arg.name }});
44-
{%else%}
45-
free((void *)from_{{ arg.name }});
46-
{%endif%}
47-
{%endif%}
48-
{%endeach%}
49-
*/
5038
{%if return.isErrorCode %}
5139
if (result != GIT_OK) {
5240
{%each args|argsInfo as arg %}
@@ -77,9 +65,9 @@ from_{{ arg.name }}
7765
{%endif%}
7866
{%endeach%}
7967
{%if .|returnsCount == 1 %}
80-
NanReturnValue(to);
68+
return to;
8169
{%else%}
82-
NanReturnValue(toReturn);
70+
return toReturn;
8371
{%endif%}
8472
{%endif%}
8573
}

generate/templates/class_content.cc

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ using namespace v8;
1717
using namespace node;
1818

1919
{%if cType%}
20-
{{ cppClassName }}::{{ cppClassName }}({{ cType }} *raw) {
20+
{{ cppClassName }}::{{ cppClassName }}({{ cType }} *raw, bool selfFreeing) {
2121
this->raw = raw;
22-
this->selfFreeing = true;
22+
this->selfFreeing = selfFreeing;
2323
}
2424

2525
{{ cppClassName }}::~{{ cppClassName }}() {
@@ -65,16 +65,17 @@ NAN_METHOD({{ cppClassName }}::New) {
6565
if (args.Length() == 0 || !args[0]->IsExternal()) {
6666
return NanThrowError("{{ cType }} is required.");
6767
}
68-
{{ cppClassName }}* object = new {{ cppClassName }}(static_cast<{{ cType }} *>(Handle<External>::Cast(args[0])->Value()));
68+
69+
{{ cppClassName }}* object = new {{ cppClassName }}(static_cast<{{ cType }} *>(Handle<External>::Cast(args[0])->Value()), args[1]->BooleanValue());
6970
object->Wrap(args.This());
7071

7172
NanReturnValue(args.This());
7273
}
7374

74-
Handle<Value> {{ cppClassName }}::New(void *raw) {
75+
Handle<Value> {{ cppClassName }}::New(void *raw, bool selfFreeing) {
7576
NanEscapableScope();
76-
Handle<Value> argv[1] = { NanNew<External>((void *)raw) };
77-
return NanEscapeScope(NanNew<Function>({{ cppClassName }}::constructor_template)->NewInstance(1, argv));
77+
Handle<Value> argv[2] = { NanNew<External>((void *)raw), Boolean::New(selfFreeing) };
78+
return NanEscapeScope(NanNew<Function>({{ cppClassName }}::constructor_template)->NewInstance(2, argv));
7879
}
7980

8081
{{ cType }} *{{ cppClassName }}::GetValue() {

generate/templates/class_header.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#ifndef {{ cppClassName|upper }}_H
22
#define {{ cppClassName|upper }}_H
3-
3+
// generated from class_header.h
44
#include <nan.h>
55
#include <string>
66

@@ -36,14 +36,13 @@ class {{ cppClassName }} : public ObjectWrap {
3636
{{ cType }} *GetValue();
3737
{{ cType }} **GetRefValue();
3838

39-
static Handle<Value> New(void *raw);
40-
41-
bool selfFreeing;
39+
static Handle<Value> New(void *raw, bool selfFreeing);
4240
{%endif%}
41+
bool selfFreeing;
4342

4443
private:
4544
{%if cType%}
46-
{{ cppClassName }}({{ cType }} *raw);
45+
{{ cppClassName }}({{ cType }} *raw, bool selfFreeing);
4746
~{{ cppClassName }}();
4847
{%endif%}
4948

generate/templates/struct_content.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ extern "C" {
1818
using namespace v8;
1919
using namespace node;
2020
using namespace std;
21-
21+
// generated from struct_content.cc
2222
{{ cppClassName }}::{{ cppClassName }}() {
2323
{{ cType }} wrappedValue = {{ cType|upper }}_INIT;
2424
this->raw = ({{ cType }}*) malloc(sizeof({{ cType }}));
@@ -28,10 +28,10 @@ using namespace std;
2828
this->selfFreeing = true;
2929
}
3030

31-
{{ cppClassName }}::{{ cppClassName }}({{ cType }}* raw) {
31+
{{ cppClassName }}::{{ cppClassName }}({{ cType }}* raw, bool selfFreeing) {
3232
this->raw = raw;
3333
this->ConstructFields();
34-
this->selfFreeing = true;
34+
this->selfFreeing = selfFreeing;
3535
}
3636

3737
{{ cppClassName }}::~{{ cppClassName }}() {
@@ -90,7 +90,7 @@ NAN_METHOD({{ cppClassName }}::New) {
9090
instance = new {{ cppClassName }}();
9191
}
9292
else {
93-
instance = new {{ cppClassName }}(static_cast<{{ cType }}*>(Handle<External>::Cast(args[0])->Value()));
93+
instance = new {{ cppClassName }}(static_cast<{{ cType }}*>(Handle<External>::Cast(args[0])->Value()), true);
9494
}
9595

9696
instance->Wrap(args.This());

generate/templates/struct_header.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#ifndef {{ cppClassName|upper }}_H
22
#define {{ cppClassName|upper }}_H
3-
3+
// generated from struct_header.h
44
#include <nan.h>
55
#include <string>
66

@@ -17,7 +17,7 @@ using namespace v8;
1717

1818
class {{ cppClassName }} : public ObjectWrap {
1919
public:
20-
{{ cppClassName }}({{ cType }}* raw);
20+
{{ cppClassName }}({{ cType }}* raw, bool selfFreeing);
2121
static Persistent<Function> constructor_template;
2222
static void Initialize (Handle<v8::Object> target);
2323

0 commit comments

Comments
 (0)