Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 7 additions & 22 deletions generate/input/callbacks.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,6 @@
"error": -1
}
},
"git_blob_chunk_cb": {
"args": [
{
"name": "entry",
"cType": "const git_config_entry *"
},
{
"name": "payload",
"cType": "void *"
}
],
"return": {
"type": "int",
"noResults": 1,
"success": 0,
"error": -1
}
},
"git_checkout_notify_cb": {
"args": [
{
Expand Down Expand Up @@ -326,7 +308,8 @@
"args": [
{
"name": "diff_so_far",
"cType": "const git_diff *"
"cType": "const git_diff *",
"ignore": true
},
{
"name": "delta_to_add",
Expand All @@ -347,11 +330,13 @@
"success": 0,
"error": -1
}
},"git_diff_progress_cb": {
},
"git_diff_progress_cb": {
"args": [
{
"name": "diff_so_far",
"cType": "const git_diff *"
"cType": "const git_diff *",
"ignore": true
},
{
"name": "old_path",
Expand Down Expand Up @@ -551,7 +536,7 @@
"args": [
{
"name": "out",
"cType": "git_repository **",
"cType": "git_remote **",
"isReturn": true
},
{
Expand Down
22 changes: 22 additions & 0 deletions generate/input/libgit2-supplement.json
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,28 @@
},
"group": "filter_source"
},
"git_filter_source_repo": {
"args": [
{
"name": "out",
"type": "git_repository **"
},
{
"name": "src",
"type": "const git_filter_source *"
}
],
"isManual": true,
"cFile": "generate/templates/manual/filter_source/repo.cc",
"isAsync": true,
"isPrototypeMethod": true,
"type": "function",
"group": "filter_source",
"return": {
"type": "int",
"isErrorCode": true
}
},
"git_patch_convenient_from_diff": {
"args": [
{
Expand Down
2 changes: 2 additions & 0 deletions generate/scripts/generateNativeCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ module.exports = function generateNativeCode() {
argsInfo: require("../templates/filters/args_info"),
arrayTypeToPlainType: require("../templates/filters/array_type_to_plain_type"),
asElementPointer: require("../templates/filters/as_element_pointer"),
callbackArgsInfo: require("../templates/filters/callback_args_info"),
callbackArgsCount: require("../templates/filters/callback_args_count"),
cppToV8: require("../templates/filters/cpp_to_v8"),
defaultValue: require("../templates/filters/default_value"),
fieldsInfo: require("../templates/filters/fields_info"),
Expand Down
18 changes: 18 additions & 0 deletions generate/templates/filters/callback_args_count.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module.exports = function(args) {
if (!args) {
return 0;
}

return args.reduce(
function(count, arg) {
var shouldCount = !arg.isReturn &&
!arg.isSelf &&
arg.name !== "payload" &&
arg.name !== "self" &&
!arg.ignore;

return shouldCount ? count + 1 : count;
},
0
);
};
27 changes: 27 additions & 0 deletions generate/templates/filters/callback_args_info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module.exports = function(args) {
var result = args.reduce(
function(argList, arg) {
var useArg = !arg.isReturn &&
!arg.isSelf &&
arg.name !== "payload" &&
arg.name !== "self" &&
!arg.ignore;

if (!useArg) {
return argList;
}

arg.firstArg = argList.length === 0;
argList.push(arg);

return argList;
},
[]
);

if (result.length) {
result[result.length - 1].lastArg = true;
}

return result;
};
4 changes: 2 additions & 2 deletions generate/templates/filters/js_args_count.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ module.exports = function(args) {
if (!args) {
return 0;
}

for(cArg = 0, jsArg = 0; cArg < args.length; cArg++) {
var arg = args[cArg];

if (!arg.isReturn && !arg.isSelf && !arg.isPayload) {
if (!arg.isReturn && !arg.isSelf) {
jsArg++;
}
}
Expand Down
90 changes: 90 additions & 0 deletions generate/templates/manual/filter_source/repo.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// NOTE you may need to occasionally rebuild this method by calling the generators
// if major changes are made to the templates / generator.

// Due to some garbage collection issues related to submodules and git_filters, we need to clone the repository
// pointer before giving it to a user.

/*
* @param Repository callback
*/
NAN_METHOD(GitFilterSource::Repo) {
if (info.Length() == 0 || !info[0]->IsFunction()) {
return Nan::ThrowError("Callback is required and must be a Function.");
}

RepoBaton *baton = new RepoBaton;

baton->error_code = GIT_OK;
baton->error = NULL;
baton->src = Nan::ObjectWrap::Unwrap<GitFilterSource>(info.This())->GetValue();

Nan::Callback *callback = new Nan::Callback(v8::Local<Function>::Cast(info[0]));
RepoWorker *worker = new RepoWorker(baton, callback);

worker->SaveToPersistent("src", info.This());

AsyncLibgit2QueueWorker(worker);
return;
}

void GitFilterSource::RepoWorker::Execute() {
git_error_clear();

{
LockMaster lockMaster(true, baton->src);

git_repository *repo = git_filter_source_repo(baton->src);
baton->error_code = git_repository_open(&repo, git_repository_path(repo));

if (baton->error_code == GIT_OK) {
baton->out = repo;
} else if (git_error_last() != NULL) {
baton->error = git_error_dup(git_error_last());
}
}
}

void GitFilterSource::RepoWorker::HandleOKCallback() {
if (baton->error_code == GIT_OK) {
v8::Local<v8::Value> to;

if (baton->out != NULL) {
to = GitRepository::New(baton->out, true);
} else {
to = Nan::Null();
}

v8::Local<v8::Value> argv[2] = {Nan::Null(), to};
callback->Call(2, argv, async_resource);
} else {
if (baton->error) {
v8::Local<v8::Object> err;
if (baton->error->message) {
err = Nan::Error(baton->error->message)->ToObject();
} else {
err = Nan::Error("Method repo has thrown an error.")->ToObject();
}
err->Set(Nan::New("errno").ToLocalChecked(), Nan::New(baton->error_code));
err->Set(Nan::New("errorFunction").ToLocalChecked(),
Nan::New("FilterSource.repo").ToLocalChecked());
v8::Local<v8::Value> argv[1] = {err};
callback->Call(1, argv, async_resource);
if (baton->error->message)
free((void *)baton->error->message);
free((void *)baton->error);
} else if (baton->error_code < 0) {
v8::Local<v8::Object> err =
Nan::Error("Method repo has thrown an error.")->ToObject();
err->Set(Nan::New("errno").ToLocalChecked(),
Nan::New(baton->error_code));
err->Set(Nan::New("errorFunction").ToLocalChecked(),
Nan::New("FilterSource.repo").ToLocalChecked());
v8::Local<v8::Value> argv[1] = {err};
callback->Call(1, argv, async_resource);
} else {
callback->Call(0, NULL, async_resource);
}
}

delete baton;
}
35 changes: 15 additions & 20 deletions generate/templates/partials/callback_helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,27 @@ void {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_async(void
{% endif %}
{% endeach %}

v8::Local<Value> argv[{{ cbFunction.args|jsArgsCount }}] = {
{% each cbFunction.args|argsInfo as arg %}
{% if arg | isPayload %}
{%-- payload is always the last arg --%}
// payload is null because we can use closure scope in javascript
Nan::Undefined()
{% elsif arg.isJsArg %}
{% if arg.isEnum %}
Nan::New((int)baton->{{ arg.name }}),
{% elsif arg.isLibgitType %}
{{ arg.cppClassName }}::New(baton->{{ arg.name }}, false),
{% elsif arg.cType == "size_t" %}
// HACK: NAN should really have an overload for Nan::New to support size_t
Nan::New((unsigned int)baton->{{ arg.name }}),
{% elsif arg.cppClassName == 'String' %}
Nan::New(baton->{{ arg.name }}).ToLocalChecked(),
{% else %}
Nan::New(baton->{{ arg.name }}),
{% endif %}
v8::Local<Value> argv[{{ cbFunction.args|callbackArgsCount }}] = {
{% each cbFunction.args|callbackArgsInfo as arg %}
{% if not arg.firstArg %}, {% endif %}
{% if arg.isEnum %}
Nan::New((int)baton->{{ arg.name }})
{% elsif arg.isLibgitType %}
{{ arg.cppClassName }}::New(baton->{{ arg.name }}, false)
{% elsif arg.cType == "size_t" %}
// HACK: NAN should really have an overload for Nan::New to support size_t
Nan::New((unsigned int)baton->{{ arg.name }})
{% elsif arg.cppClassName == 'String' %}
Nan::New(baton->{{ arg.name }}).ToLocalChecked()
{% else %}
Nan::New(baton->{{ arg.name }})
{% endif %}
{% endeach %}
};

Nan::TryCatch tryCatch;
// TODO This should take an async_resource, but we will need to figure out how to pipe the correct context into this
Nan::MaybeLocal<v8::Value> maybeResult = Nan::Call(*callback, {{ cbFunction.args|jsArgsCount }}, argv);
Nan::MaybeLocal<v8::Value> maybeResult = Nan::Call(*callback, {{ cbFunction.args|callbackArgsCount }}, argv);
v8::Local<v8::Value> result;
if (!maybeResult.IsEmpty()) {
result = maybeResult.ToLocalChecked();
Expand Down
Loading