Skip to content

Commit 2bf45b3

Browse files
John Haleytbranyen
authored andcommitted
Objects can now store enums/structs and opaque structs
1 parent 1c563e7 commit 2bf45b3

6 files changed

Lines changed: 46 additions & 10 deletions

File tree

generate/descriptor.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@
162162
"clone_options": {
163163
"cType": "git_clone_options",
164164
"isStruct": true,
165-
"isPublicStruct": true,
166165
"dependencies": [
167166
"../include/checkout_options.h",
168167
"../include/remote_callbacks.h"

generate/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ fse.remove(path.resolve(__dirname, "../src")).then(function() {
9292

9393
// Write out all the classes.
9494
enabled.forEach(function(idef) {
95-
if (idef.isPublicStruct) {
95+
if (idef.hasConstructor) {
9696
file.write("../src/" + idef.name + ".cc", templates.struct_content.render(idef));
9797
file.write("../include/" + idef.name + ".h", templates.struct_header.render(idef));
9898
}

generate/partials/field_accessors.cc

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ NAN_GETTER({{ cppClassName }}::Get{{ field.cppFunctionName }}) {
66

77
{{ cType }} *raw = ObjectWrap::Unwrap<{{ cppClassName }}>(args.This())->GetValue();
88

9-
{%if field.cppClassName == 'String' %}
9+
{%if field.hasConstructor %}
10+
NanReturnValue(NanNew(new {{ field.cppClassName }}(&raw->{{ field.name }})));
11+
{%elsif field.cppClassName == 'String' %}
1012
NanReturnValue(NanNew<String>(raw->{{ field.name }}));
1113
{%elsif field.cppClassName|isV8Value %}
1214
NanReturnValue(NanNew<{{ field.cppClassName }}>(raw->{{ field.name }}));
@@ -19,7 +21,10 @@ NAN_SETTER({{ cppClassName }}::Set{{ field.cppFunctionName }}) {
1921

2022
{{ cType }} *raw = ObjectWrap::Unwrap<{{ cppClassName }}>(args.This())->GetValue();
2123

22-
{%if field.cppClassName == 'String' %}
24+
{%if field.hasConstructor %}
25+
{{ field.cType }}* wrappedStruct = ObjectWrap::Unwrap<{{ field.cppClassName }}>(value->ToObject())->GetValue();
26+
memcpy(&raw->{{ field.name }}, wrappedStruct, sizeof({{ field.cType }}));
27+
{%elsif field.cppClassName == 'String' %}
2328
if (raw->{{ field.name }}) {
2429
//free(raw->{{ field.name }});
2530
}
@@ -28,14 +33,12 @@ NAN_SETTER({{ cppClassName }}::Set{{ field.cppFunctionName }}) {
2833
raw->{{ field.name }} = strdup(*str);
2934
{%elsif field.isCppClassIntType%}
3035
if (value->IsNumber()) {
31-
raw->{{ field.name }} = value->field.cppClassName}}Value();
36+
raw->{{ field.name }} = value->{{field.cppClassName}}Value();
3237
}
33-
{%elsif field.cppClassName == 'Number' %}
38+
{%else%}
3439
if (value->IsNumber()) {
35-
raw->{{ field.name }} = value->Int32Value();
40+
raw->{{ field.name }} = ({{ field.cType }}) value->Int32Value();
3641
}
37-
{%else%}
38-
raw->{{ field.name }} = ({{ field.cType }}) value;
3942
{%endif%}
4043

4144
}

generate/setup.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,18 @@ fileNames.forEach(function(fileName, index) {
124124
cFile.fields = descriptor[fileName].fields || structFile.fields || [];
125125
}
126126

127+
// This should turn 'git_clone_options' into 'git_clone_init_options'
128+
var initFnName = ["git"].concat(fileName.split('_'));
129+
130+
initFnName.splice(-1, 0, "init");
131+
initFnName = initFnName.join('_');
132+
133+
// Does this struct contain an init function? If so then we can create one publicly
134+
// to pass into functions and other structs
135+
file.hasConstructor = structFile.used && structFile.used.needs.some(function (fnName) {
136+
return fnName === initFnName;
137+
});
138+
127139
// Doesn't actually exist.
128140
if (cFile.functions.indexOf(file.freeFunctionName) === -1) {
129141
delete file.freeFunctionName;
@@ -190,6 +202,22 @@ fileNames.forEach(function(fileName, index) {
190202
field.jsFunctionName = camelCase(field.name);
191203
field.cppClassName = typeMap[field.cType].cpp;
192204
field.jsClassName = typeMap[field.cType].js;
205+
206+
var struct = structs[field.cType];
207+
208+
// This should turn 'git_clone_options' into 'git_clone_init_options'
209+
var initFnName = field.cType.split('_');
210+
211+
initFnName.splice(-1, 0, "init");
212+
initFnName = initFnName.join('_');
213+
214+
// Does this struct contain an init function? If so then we can create one publicly
215+
// to pass into functions and other structs
216+
field.hasConstructor = struct && struct.used && struct.used.needs.some(function (fnName) {
217+
return fnName === initFnName;
218+
});
219+
220+
field.isEnum = struct && struct.type === "enum";
193221
} catch (ex) {
194222
console.error(file.filename, field.cType + " is missing a definition.");
195223
}

generate/templates/struct_content.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ using namespace node;
2222
memcpy(this->raw, &wrappedValue, sizeof({{ cType }}));
2323
}
2424

25+
{{ cppClassName }}::{{ cppClassName }}({{ cType }}* raw) {
26+
{{ cType }} wrappedValue = *raw;
27+
this->raw = ({{ cType }}*) malloc(sizeof({{ cType }}));
28+
memcpy(this->raw, &wrappedValue, sizeof({{ cType }}));
29+
}
30+
2531
{{ cppClassName }}::~{{ cppClassName }}() {
2632
// This is going to cause memory leaks. We'll have to solve that later
2733
// TODO: Clean up memory better

generate/templates/struct_header.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ using namespace v8;
1717

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

0 commit comments

Comments
 (0)