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
24 changes: 24 additions & 0 deletions examples/status.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var nodegit = require("../"),
path = require("path");

// This code shows working directory changes similar to git status

nodegit.Repository.open(path.resolve(__dirname, "../.git"))
.then(function(repo) {
repo.getStatus().then(function(statuses) {
function statusToText(status) {
var words = [];
if (status.isNew()) { words.push("NEW"); }
if (status.isModified()) { words.push("MODIFIED"); }
if (status.isTypechange()) { words.push("TYPECHANGE"); }
if (status.isRenamed()) { words.push("RENAMED"); }
if (status.isIgnored()) { words.push("IGNORED"); }

return words.join(" ");
}

statuses.forEach(function(file) {
console.log(file.path() + " " + statusToText(file));
});
});
});
148 changes: 148 additions & 0 deletions generate/combyne/partials/callback_helpers.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
{%each args as cbFunction %}
{%if cbFunction.isCallbackFunction %}

{{ cbFunction.return.type }} {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_cppCallback (
{% each cbFunction.args|argsInfo as arg %}
{{ arg.cType }} {{ arg.name}}{% if not arg.lastArg %},{% endif %}
{% endeach %}
) {
{{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton* baton = new {{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton();

{% each cbFunction.args|argsInfo as arg %}
baton->{{ arg.name }} = {{ arg.name }};
{% endeach %}

baton->req.data = baton;
baton->done = false;

uv_queue_work(uv_default_loop(), &baton->req, {{ cppFunctionName }}_{{ cbFunction.name }}_asyncWork, {{ cppFunctionName }}_{{ cbFunction.name }}_asyncAfter);

while(!baton->done) {
this_thread::sleep_for(chrono::milliseconds(1));
}

{% each cbFunction|returnsInfo true false as _return %}
*{{ _return.name }} = *baton->{{ _return.name }};
{% endeach %}

return baton->result;
}

void {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_asyncWork(uv_work_t* req) {
// We aren't doing any work on a seperate thread, just need to
// access the main node thread in the async after method.
// However, this worker method is still needed
}

void {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_asyncAfter(uv_work_t* req, int status) {
NanScope();

{{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton* baton = static_cast<{{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton*>(req->data);

NanCallback* callback = (NanCallback *)baton->payload;

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

TryCatch tryCatch;
Handle<Value> result = callback->Call({{ cbFunction.args|jsArgsCount }}, argv);

if (result->IsObject() && result->ToObject()->Has(NanNew("then"))) {
Handle<Value> thenProp = result->ToObject()->Get(NanNew("then"));

if (thenProp->IsFunction()) {
// we can be reasonbly certain that the result is a promise
Local<Object> promise = result->ToObject();

NanAssignPersistent(baton->promise, promise);

uv_queue_work(uv_default_loop(), &baton->req, {{ cppFunctionName }}_{{ cbFunction.name }}_asyncWork, {{ cppFunctionName }}_{{ cbFunction.name }}_asyncPromisePolling);
return;
}
}

{{ cbFunction.return.type }} resultStatus;

{% each cbFunction|returnsInfo true false as _return %}
if (result.IsEmpty() || result->IsNativeError()) {
baton->result = {{ cbFunction.return.error }};
}
else if (!result->IsNull() && !result->IsUndefined()) {
{{ _return.cppClassName }}* wrapper = ObjectWrap::Unwrap<{{ _return.cppClassName }}>(result->ToObject());
wrapper->selfFreeing = false;

baton->{{ _return.name }} = wrapper->GetRefValue();
baton->result = {{ cbFunction.return.success }};
}
else {
baton->result = {{ cbFunction.return.noResults }};
}
{% endeach %}
baton->done = true;
}

void {{ cppClassName }}::{{ cppFunctionName }}_{{ cbFunction.name }}_asyncPromisePolling(uv_work_t* req, int status) {
NanScope();

{{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton* baton = static_cast<{{ cppFunctionName }}_{{ cbFunction.name|titleCase }}Baton*>(req->data);
Local<Object> promise = NanNew<Object>(baton->promise);
NanCallback* isPendingFn = new NanCallback(promise->Get(NanNew("isPending")).As<Function>());
Local<Value> argv[1]; // MSBUILD won't assign an array of length 0
Local<Boolean> isPending = isPendingFn->Call(0, argv)->ToBoolean();

if (isPending->Value()) {
uv_queue_work(uv_default_loop(), &baton->req, {{ cppFunctionName }}_{{ cbFunction.name }}_asyncWork, {{ cppFunctionName }}_{{ cbFunction.name }}_asyncPromisePolling);
return;
}

NanCallback* isFulfilledFn = new NanCallback(promise->Get(NanNew("isFulfilled")).As<Function>());
Local<Boolean> isFulfilled = isFulfilledFn->Call(0, argv)->ToBoolean();

if (isFulfilled->Value()) {
NanCallback* resultFn = new NanCallback(promise->Get(NanNew("value")).As<Function>());
Handle<Value> result = resultFn->Call(0, argv);
{{ cbFunction.return.type }} resultStatus;

{% each cbFunction|returnsInfo true false as _return %}
if (result.IsEmpty() || result->IsNativeError()) {
baton->result = {{ cbFunction.return.error }};
}
else if (!result->IsNull() && !result->IsUndefined()) {
{{ _return.cppClassName }}* wrapper = ObjectWrap::Unwrap<{{ _return.cppClassName }}>(result->ToObject());
wrapper->selfFreeing = false;

baton->{{ _return.name }} = wrapper->GetRefValue();
baton->result = {{ cbFunction.return.success }};
}
else {
baton->result = {{ cbFunction.return.noResults }};
}
{% endeach %}
baton->done = true;
}
else {
// promise was rejected
baton->result = {{ cbFunction.return.error }};
baton->done = false;
}
}
{%endif%}
{%endeach%}
Loading