Skip to content

Commit 4113536

Browse files
committed
Merge pull request #250 from nodegit/refactor-classes
Refactor classes
2 parents 5e70611 + 86a41a0 commit 4113536

File tree

16 files changed

+104
-54
lines changed

16 files changed

+104
-54
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ git:
2222
branches:
2323
only:
2424
- master
25-
25+
script: npm --expose-gc test

example/add-and-commit.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var git = require('../'),
66
;
77

88
/**
9-
* This example creates a certain file `newfile.txt`, adds it to the git index and
9+
* This example creates a certain file `newfile.txt`, adds it to the git index and
1010
* commits it to head. Similar to a `git add newfile.txt` followed by a `git commit`
1111
**/
1212

@@ -56,4 +56,4 @@ git.Repo.open(path.resolve(__dirname, '../.git'), function(openReporError, repo)
5656
});
5757
});
5858
});
59-
});
59+
});

generate/descriptor.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@
196196
"functions": {
197197
"git_commit_create": {
198198
"ignore": false,
199-
"jsFunctionName": "createCommit",
200-
"cppFunctionName": "CreateCommit",
199+
"jsFunctionName": "create",
200+
"cppFunctionName": "Create",
201201
"isConstructorMethod": true,
202202
"isAsync": true,
203203
"return": {

generate/manual/include/macros.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef NODEGIT_PSUEDONAN_H_
2+
#if (NODE_MODULE_VERSION > 0x000B)
3+
// Node 0.11+ (0.11.3 and below won't compile with these)
4+
# define NodeGitPsueodoNanReturnEscapingValue(val) NanReturnValue(NanEscapeScope(val))
5+
6+
#else
7+
// Node 0.8 and 0.10
8+
# define NodeGitPsueodoNanReturnEscapingValue(val) NanReturnValue(val)
9+
10+
#endif
11+
12+
#endif //NODEGIT_PSUEDONAN_H_

generate/nkallen.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12344,8 +12344,8 @@
1234412344
"isAsync": true,
1234512345
"isConstructorMethod": false,
1234612346
"isPrototypeMethod": true,
12347-
"jsFunctionName": "createCommit",
12348-
"cppFunctionName": "CreateCommit",
12347+
"jsFunctionName": "create",
12348+
"cppFunctionName": "Create",
1234912349
"return": {
1235012350
"cType": "int",
1235112351
"cppClassName": "Int32",

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: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
{%partial doc .%}
33
NAN_METHOD({{ cppClassName }}::{{ cppFunctionName }}) {
4-
NanScope();
4+
NanEscapableScope();
55
{%partial guardArguments .%}
66

77
{%each .|returnsInfo 'true' as _return %}
@@ -36,17 +36,6 @@ from_{{ arg.name }}
3636
{%if not arg.lastArg %},{%endif%}
3737
{%endeach%}
3838
);
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-
5039
{%if return.isErrorCode %}
5140
if (result != GIT_OK) {
5241
{%each args|argsInfo as arg %}
@@ -77,9 +66,9 @@ from_{{ arg.name }}
7766
{%endif%}
7867
{%endeach%}
7968
{%if .|returnsCount == 1 %}
80-
NanReturnValue(to);
69+
NodeGitPsueodoNanReturnEscapingValue(to);
8170
{%else%}
82-
NanReturnValue(toReturn);
71+
NodeGitPsueodoNanReturnEscapingValue(toReturn);
8372
{%endif%}
8473
{%endif%}
8574
}

generate/setup.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ fileNames.forEach(function(fileName, index) {
114114
}
115115

116116
file.freeFunctionName = "git_" + fileName + "_free";
117+
file.createFunctionName = "git_" + fileName + "_create";
117118
}
118119

119120
// TODO Obsolete this.
@@ -136,12 +137,14 @@ fileNames.forEach(function(fileName, index) {
136137
return fnName === initFnName;
137138
});
138139

139-
// Doesn't actually exist.
140+
// Free function doesn't actually exist.
140141
if (cFile.functions.indexOf(file.freeFunctionName) === -1) {
141142
delete file.freeFunctionName;
142143
}
143144

144-
145+
if (cFile.functions.indexOf(file.createFunctionName) === -1) {
146+
delete file.createFunctionName;
147+
}
145148
var legacyFile = {};
146149

147150
if (file.jsClassName.indexOf("Git") === 0) {
@@ -394,6 +397,12 @@ fileNames.forEach(function(fileName, index) {
394397
}
395398
});
396399

400+
if (file.createFunctionName) {
401+
file.cppCreateFunctionName = typeMap[file.createFunctionName].cpp;
402+
file.jsCreateFunctionName = typeMap[file.createFunctionName].js;
403+
delete file.createFunctionName;
404+
}
405+
397406
files.push(file);
398407
});
399408

generate/templates/class_content.cc

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ extern "C" {
77
}
88

99
#include "../include/functions/copy.h"
10+
#include "../include/macros.h"
1011
#include "../include/{{ filename }}"
1112

1213
{%each dependencies as dependency%}
@@ -17,9 +18,9 @@ using namespace v8;
1718
using namespace node;
1819

1920
{%if cType%}
20-
{{ cppClassName }}::{{ cppClassName }}({{ cType }} *raw) {
21+
{{ cppClassName }}::{{ cppClassName }}({{ cType }} *raw, bool selfFreeing) {
2122
this->raw = raw;
22-
this->selfFreeing = true;
23+
this->selfFreeing = selfFreeing;
2324
}
2425

2526
{{ cppClassName }}::~{{ cppClassName }}() {
@@ -63,18 +64,23 @@ NAN_METHOD({{ cppClassName }}::New) {
6364
NanScope();
6465

6566
if (args.Length() == 0 || !args[0]->IsExternal()) {
66-
return NanThrowError("{{ cType }} is required.");
67+
{%if createFunctionName%}
68+
return NanThrowError("A new {{ cppClassName }} cannot be instantiated. Use {{ jsCreateFunctionName }} instead.");
69+
{%else%}
70+
return NanThrowError("A new {{ cppClassName }} cannot be instantiated.");
71+
{%endif%}
6772
}
68-
{{ cppClassName }}* object = new {{ cppClassName }}(static_cast<{{ cType }} *>(Handle<External>::Cast(args[0])->Value()));
73+
74+
{{ cppClassName }}* object = new {{ cppClassName }}(static_cast<{{ cType }} *>(Handle<External>::Cast(args[0])->Value()), args[1]->BooleanValue());
6975
object->Wrap(args.This());
7076

7177
NanReturnValue(args.This());
7278
}
7379

74-
Handle<Value> {{ cppClassName }}::New(void *raw) {
80+
Handle<Value> {{ cppClassName }}::New(void *raw, bool selfFreeing) {
7581
NanEscapableScope();
76-
Handle<Value> argv[1] = { NanNew<External>((void *)raw) };
77-
return NanEscapeScope(NanNew<Function>({{ cppClassName }}::constructor_template)->NewInstance(1, argv));
82+
Handle<Value> argv[2] = { NanNew<External>((void *)raw), NanNew<Boolean>(selfFreeing) };
83+
return NanEscapeScope(NanNew<Function>({{ cppClassName }}::constructor_template)->NewInstance(2, argv));
7884
}
7985

8086
{{ 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

0 commit comments

Comments
 (0)