|
| 1 | +/* |
| 2 | + * Copyright 2013, Tim Branyen @tbranyen <tim@tabdeveloper.com> |
| 3 | + * @author Michael Robinson @codeofinterest <mike@pagesofinterest.net> |
| 4 | + * |
| 5 | + * Dual licensed under the MIT and GPL licenses. |
| 6 | + */ |
| 7 | + |
| 8 | +#include <v8.h> |
| 9 | +#include <node.h> |
| 10 | + |
| 11 | +#include "../vendor/libgit2/include/git2.h" |
| 12 | + |
| 13 | +#include "../include/repo.h" |
| 14 | +#include "../include/sig.h" |
| 15 | + |
| 16 | +using namespace v8; |
| 17 | +using namespace node; |
| 18 | + |
| 19 | +void GitSignature::Initialize (Handle<v8::Object> target) { |
| 20 | + Local<FunctionTemplate> tpl = FunctionTemplate::New(New); |
| 21 | + |
| 22 | + tpl->InstanceTemplate()->SetInternalFieldCount(1); |
| 23 | + tpl->SetClassName(String::NewSymbol("Signature")); |
| 24 | + |
| 25 | + NODE_SET_PROTOTYPE_METHOD(tpl, "duplicate", Duplicate); |
| 26 | + NODE_SET_PROTOTYPE_METHOD(tpl, "name", Name); |
| 27 | + NODE_SET_PROTOTYPE_METHOD(tpl, "email", Email); |
| 28 | + |
| 29 | + NODE_SET_PROTOTYPE_METHOD(tpl, "free", Free); |
| 30 | + |
| 31 | + constructor_template = Persistent<Function>::New(tpl->GetFunction()); |
| 32 | + target->Set(String::NewSymbol("Signature"), constructor_template); |
| 33 | +} |
| 34 | + |
| 35 | +git_signature* GitSignature::GetValue() { |
| 36 | + return this->sig; |
| 37 | +} |
| 38 | + |
| 39 | +void GitSignature::SetValue(git_signature* sig) { |
| 40 | + this->sig = sig; |
| 41 | + this->name = sig->name; |
| 42 | + this->email = sig->email; |
| 43 | +} |
| 44 | + |
| 45 | +void GitSignature::New(const char *name, const char *email, time_t time, int offset) { |
| 46 | + git_signature_new(&this->sig, name, email, time, offset); |
| 47 | + //this->sig = git_signature_new(name, email, time, offset); |
| 48 | +} |
| 49 | + |
| 50 | +char* GitSignature::Name() { |
| 51 | + return this->name; |
| 52 | +} |
| 53 | + |
| 54 | +char* GitSignature::Email() { |
| 55 | + return this->email; |
| 56 | +} |
| 57 | + |
| 58 | +Handle<Value> GitSignature::New(const Arguments& args) { |
| 59 | + HandleScope scope; |
| 60 | + |
| 61 | + GitSignature *signature = new GitSignature(); |
| 62 | + signature->Wrap(args.This()); |
| 63 | + |
| 64 | + return scope.Close(args.This()); |
| 65 | +} |
| 66 | + |
| 67 | +Handle<Value> GitSignature::Duplicate(const Arguments& args) { |
| 68 | + HandleScope scope; |
| 69 | + |
| 70 | + if(args.Length() == 0 || !args[0]->IsObject()) { |
| 71 | + return ThrowException(Exception::Error(String::New("GitSignature is required and must be an Object."))); |
| 72 | + } |
| 73 | + |
| 74 | + Local<Object> duplicateSignature = GitSignature::constructor_template->NewInstance(); |
| 75 | + GitSignature *duplicateSignatureInstance = ObjectWrap::Unwrap<GitSignature>(signature); |
| 76 | + |
| 77 | + GitSignature* signature = ObjectWrap::Unwrap<GitSignature>(args[0]->ToObject()); |
| 78 | + |
| 79 | + duplicateSignatureInstance->SetValue(git_signature_dup(signature->GetValue())); |
| 80 | + |
| 81 | + return duplicateSignature; |
| 82 | +} |
| 83 | + |
| 84 | +Handle<Value> GitSignature::Free(const Arguments& args) { |
| 85 | + HandleScope scope; |
| 86 | + |
| 87 | + GitSignature *signature = ObjectWrap::Unwrap<GitSignature>(args.This()); |
| 88 | + git_signature_free(this->signature); |
| 89 | + |
| 90 | + return Undefined(); |
| 91 | +} |
| 92 | + |
| 93 | +Handle<Value> GitSignature::Name(const Arguments& args) { |
| 94 | + HandleScope scope; |
| 95 | + |
| 96 | + GitSignature *signature = ObjectWrap::Unwrap<GitSignature>(args.This()); |
| 97 | + |
| 98 | + return String::New(signature->Name()); |
| 99 | +} |
| 100 | + |
| 101 | +Handle<Value> GitSignature::Email(const Arguments& args) { |
| 102 | + HandleScope scope; |
| 103 | + |
| 104 | + GitSignature *signature = ObjectWrap::Unwrap<GitSignature>(args.This()); |
| 105 | + |
| 106 | + return String::New(signature->Email()); |
| 107 | +} |
| 108 | + |
| 109 | +Persistent<Function> GitSignature::constructor_template; |
0 commit comments