Skip to content

Commit

Permalink
fixed codestyle
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiago Oliveira committed Nov 11, 2016
1 parent a14c01e commit eed897f
Show file tree
Hide file tree
Showing 8 changed files with 1,961 additions and 1,795 deletions.
90 changes: 90 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
---
Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlinesLeft: false
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
ColumnLimit: 80
CommentPragmas: '^ IWYU pragma:'
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
- Regex: '^(<|"(gtest|isl|json)/)'
Priority: 3
- Regex: '.*'
Priority: 1
IndentCaseLabels: false
IndentWidth: 2
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments: true
SortIncludes: true
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 8
UseTab: Never
...

169 changes: 90 additions & 79 deletions src/addon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,86 +6,97 @@
* MIT License <https://github.com/nevermnd/anitomy-js/blob/master/LICENSE>
********************************************************************/

#include <nan.h>
#include "worker.h"
#include <nan.h>

namespace anitomyJs {

bool ValidateInput(v8::Local<v8::Value> value, v8::Isolate* isolate) {
bool valid = value->IsString() || value->IsArray();
if (!valid) isolate->ThrowException(v8::Exception::TypeError(v8::String::NewFromUtf8(isolate, "Wrong data type")));
return valid;
}

bool ValidateOptions(v8::Local<v8::Value> options, v8::Isolate* isolate) {
if (!options->IsObject()) {
isolate->ThrowException(v8::Exception::TypeError(v8::String::NewFromUtf8(isolate, "Options must be an object")));
return false;
}
return true;
}

void ParseSync(const Nan::FunctionCallbackInfo<v8::Value>& args) {
v8::Isolate* isolate = args.GetIsolate();
int args_length = args.Length();

if (args_length < 1) {
isolate->ThrowException(v8::Exception::TypeError(v8::String::NewFromUtf8(isolate, "Wrong number of arguments")));
return;
}

v8::Local<v8::Value> input = args[0];
if (!ValidateInput(input, isolate)) return;

anitomyJs::AnitomyJs anitomy;
if (args_length >= 2) {
v8::Local<v8::Value> options = args[1];
if (!ValidateOptions(options, isolate) || !anitomy.SetOptions(options->ToObject(), isolate)) return;
}

anitomy.SetInput(input);
anitomy.Parse();

args.GetReturnValue().Set(anitomy.ParsedResult(isolate));
}

void ParseAsync(const Nan::FunctionCallbackInfo<v8::Value>& args) {
v8::Isolate* isolate = args.GetIsolate();
int args_length = args.Length();

if (args_length < 2) {
isolate->ThrowException(v8::Exception::TypeError(v8::String::NewFromUtf8(isolate, "Wrong number of arguments")));
return;
}

v8::Local<v8::Value> input = args[0];
if (!ValidateInput(input, isolate)) return;
if (!args[1]->IsFunction()) {
isolate->ThrowException(v8::Exception::TypeError(
v8::String::NewFromUtf8(isolate, "Second parameter must be a callback")));
return;
}

Nan::Callback* callback = new Nan::Callback(args[1].As<v8::Function>());
anitomyJs::Worker* worker = new anitomyJs::Worker(callback);

if (args_length >= 3) {
v8::Local<v8::Value> options = args[2];
if (!ValidateOptions(options, isolate) || !worker->GetAnitomy()->SetOptions(options->ToObject(), isolate)) return;
}

worker->GetAnitomy()->SetInput(input);
Nan::AsyncQueueWorker(worker);
args.GetReturnValue().Set(Nan::Undefined());
}

void Init(v8::Local<v8::Object> exports) {
exports->Set(Nan::New("parseSync").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(ParseSync)->GetFunction());

exports->Set(Nan::New("parseAsync").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(ParseAsync)->GetFunction());
}

NODE_MODULE(anitomy, Init)

bool ValidateInput(v8::Local<v8::Value> value, v8::Isolate *isolate) {
bool valid = value->IsString() || value->IsArray();
if (!valid)
isolate->ThrowException(v8::Exception::TypeError(
v8::String::NewFromUtf8(isolate, "Wrong data type")));
return valid;
}

bool ValidateOptions(v8::Local<v8::Value> options, v8::Isolate *isolate) {
if (!options->IsObject()) {
isolate->ThrowException(v8::Exception::TypeError(
v8::String::NewFromUtf8(isolate, "Options must be an object")));
return false;
}
return true;
}

void ParseSync(const Nan::FunctionCallbackInfo<v8::Value> &args) {
v8::Isolate *isolate = args.GetIsolate();
int args_length = args.Length();

if (args_length < 1) {
isolate->ThrowException(v8::Exception::TypeError(
v8::String::NewFromUtf8(isolate, "Wrong number of arguments")));
return;
}

v8::Local<v8::Value> input = args[0];
if (!ValidateInput(input, isolate))
return;

anitomyJs::AnitomyJs anitomy;
if (args_length >= 2) {
v8::Local<v8::Value> options = args[1];
if (!ValidateOptions(options, isolate) ||
!anitomy.SetOptions(options->ToObject(), isolate))
return;
}

anitomy.SetInput(input);
anitomy.Parse();

args.GetReturnValue().Set(anitomy.ParsedResult(isolate));
}

void ParseAsync(const Nan::FunctionCallbackInfo<v8::Value> &args) {
v8::Isolate *isolate = args.GetIsolate();
int args_length = args.Length();

if (args_length < 2) {
isolate->ThrowException(v8::Exception::TypeError(
v8::String::NewFromUtf8(isolate, "Wrong number of arguments")));
return;
}

v8::Local<v8::Value> input = args[0];
if (!ValidateInput(input, isolate))
return;
if (!args[1]->IsFunction()) {
isolate->ThrowException(v8::Exception::TypeError(v8::String::NewFromUtf8(
isolate, "Second parameter must be a callback")));
return;
}

Nan::Callback *callback = new Nan::Callback(args[1].As<v8::Function>());
anitomyJs::Worker *worker = new anitomyJs::Worker(callback);

if (args_length >= 3) {
v8::Local<v8::Value> options = args[2];
if (!ValidateOptions(options, isolate) ||
!worker->GetAnitomy()->SetOptions(options->ToObject(), isolate))
return;
}

worker->GetAnitomy()->SetInput(input);
Nan::AsyncQueueWorker(worker);
args.GetReturnValue().Set(Nan::Undefined());
}

void Init(v8::Local<v8::Object> exports) {
exports->Set(Nan::New("parseSync").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(ParseSync)->GetFunction());

exports->Set(Nan::New("parseAsync").ToLocalChecked(),
Nan::New<v8::FunctionTemplate>(ParseAsync)->GetFunction());
}

NODE_MODULE(anitomy, Init)
}
Loading

0 comments on commit eed897f

Please sign in to comment.