Skip to content

Commit

Permalink
update to node v0.12; fix win installation
Browse files Browse the repository at this point in the history
  • Loading branch information
honzabrecka committed Jul 21, 2015
1 parent 9fe62e6 commit 2e2690e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 39 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
@@ -1,6 +1,6 @@
language: node
language: node_js
node_js:
- '0.10'
- "0.12"
before_script:
- sudo pip install cpp-coveralls
- npm install -g node-gyp
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -22,6 +22,6 @@
"url": "git@github.com:honzabrecka/sudoku-c.git"
},
"engines": {
"node": ">=0.10"
"node": ">=0.12"
}
}
75 changes: 41 additions & 34 deletions src/sudoku.cpp
Expand Up @@ -15,28 +15,29 @@ extern "C" {

using namespace v8;

Handle<Value> Solve(const Arguments& args) {
HandleScope scope;
void Solve(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
HandleScope scope(isolate);

if (args.Length() < 1) {
ThrowException(Exception::TypeError(String::New("Wrong number of arguments.")));
return scope.Close(Undefined());
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Wrong number of arguments.")));
return;
}

if (!args[0]->IsArray()) {
ThrowException(Exception::TypeError(String::New("Expected array.")));
return scope.Close(Undefined());
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Expected array.")));
return;
}

Handle<Array> grid = Handle<Array>::Cast(args[0]);
Local<Array> grid = Local<Array>::Cast(args[0]);
int gridLength = grid->Length();

if (gridLength != N * N) {
ThrowException(Exception::TypeError(String::New("Wrong grid given - invalid length.")));
return scope.Close(Undefined());
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Wrong grid given - invalid length.")));
return;
}

int xgrid[gridLength];
int *xgrid = new int[gridLength];
int i;

for (i = 0; i < gridLength; i++) {
Expand All @@ -46,22 +47,25 @@ Handle<Value> Solve(const Arguments& args) {
int result = sudoku_solve(xgrid);

if (!result) {
ThrowException(Exception::TypeError(String::New("Invalid board given.")));
return scope.Close(Undefined());
delete[] xgrid;
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Invalid board given.")));
return;
}

for (i = 0; i < gridLength; i++) {
grid->Set(i, Uint32::New(*(xgrid + i)));
grid->Set(i, Uint32::New(isolate, *(xgrid + i)));
}

return scope.Close(grid);
delete[] xgrid;
args.GetReturnValue().Set(grid);
}

Handle<Value> Generate(const Arguments& args) {
HandleScope scope;
void Generate(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
HandleScope scope(isolate);
int gridLength = N * N;
Local<Array> grid = Array::New(gridLength);
int xgrid[gridLength];
Local<Array> grid = Array::New(isolate, gridLength);
int *xgrid = new int[gridLength];
int i;

for (i = 0; i < gridLength; i++) {
Expand All @@ -71,39 +75,41 @@ Handle<Value> Generate(const Arguments& args) {
sudoku_generate(xgrid);

for (i = 0; i < gridLength; i++) {
grid->Set(i, Uint32::New(*(xgrid + i)));
grid->Set(i, Uint32::New(isolate, *(xgrid + i)));
}

return scope.Close(grid);
delete[] xgrid;
args.GetReturnValue().Set(grid);
}

Handle<Value> Classic(const Arguments& args) {
HandleScope scope;
void Classic(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
HandleScope scope(isolate);

if (args.Length() < 2) {
ThrowException(Exception::TypeError(String::New("Wrong number of arguments.")));
return scope.Close(Undefined());
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Wrong number of arguments.")));
return;
}

if (!args[0]->IsArray()) {
ThrowException(Exception::TypeError(String::New("Expected array.")));
return scope.Close(Undefined());
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Expected array.")));
return;
}

if (!args[1]->IsNumber()) {
ThrowException(Exception::TypeError(String::New("Expected number.")));
return scope.Close(Undefined());
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Expected number.")));
return;
}

Handle<Array> grid = Handle<Array>::Cast(args[0]);
Local<Array> grid = Local<Array>::Cast(args[0]);
int gridLength = grid->Length();

if (gridLength != N * N) {
ThrowException(Exception::TypeError(String::New("Wrong grid given - invalid length.")));
return scope.Close(Undefined());
isolate->ThrowException(Exception::TypeError(String::NewFromUtf8(isolate, "Wrong grid given - invalid length.")));
return;
}

int xgrid[gridLength];
int *xgrid = new int[gridLength];
int i;

for (i = 0; i < gridLength; i++) {
Expand All @@ -113,10 +119,11 @@ Handle<Value> Classic(const Arguments& args) {
sudoku_classic(xgrid, args[1]->Uint32Value());

for (i = 0; i < gridLength; i++) {
grid->Set(i, Uint32::New(*(xgrid + i)));
grid->Set(i, Uint32::New(isolate, *(xgrid + i)));
}

return scope.Close(grid);
delete[] xgrid;
args.GetReturnValue().Set(grid);
}

void init(Handle<Object> target) {
Expand Down
5 changes: 3 additions & 2 deletions src/sudoku_c.c
Expand Up @@ -92,7 +92,7 @@ static void shuffle(int * array, int length)
{
int i, r;

srand(time(NULL));
srand((unsigned int)time(NULL));

for (i = length - 1; i > 0; i--) {
r = rand() % (i + 1);
Expand Down Expand Up @@ -197,7 +197,7 @@ int sudoku_solve(int * grid)
int sudoku_classic(int * grid, int empty)
{
int length = N * N;
int indicesToRemove[length];
int *indicesToRemove = (int *) malloc(length * sizeof(int));
int i;

fill(indicesToRemove, length, 0);
Expand All @@ -207,5 +207,6 @@ int sudoku_classic(int * grid, int empty)
grid[*(indicesToRemove + i)] = EMPTY;
}

free(indicesToRemove);
return 1;
}

0 comments on commit 2e2690e

Please sign in to comment.