Skip to content

Commit 5c2f766

Browse files
authored
Merge pull request #6 from szymonWojdat/master
Fix build for OSX + Node 12
2 parents 66a3902 + 0c59bfe commit 5c2f766

File tree

2 files changed

+41
-76
lines changed

2 files changed

+41
-76
lines changed

srcs/semaphore.cc

Lines changed: 38 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,18 @@
11
#include "semaphore.h"
2+
#include "typeinfo"
23

34
Nan::Persistent<v8::Function> Semaphore::constructor;
45

56
Semaphore::Semaphore(char buf[], size_t buf_len, bool strict, bool debug, bool silent, bool retry_on_eintr, unsigned int value /*= 1*/)
67
{
7-
size_t i;
8-
9-
i = 0;
10-
while (i < buf_len)
11-
{
12-
this->sem_name[i] = buf[i];
13-
i++;
14-
}
15-
this->sem_name[buf_len] = 0;
8+
strcpy(this->sem_name, buf);
169
this->semaphore = sem_open(this->sem_name, O_CREAT, 0644, value);
17-
if(this->semaphore == SEM_FAILED)
10+
if (this->semaphore == SEM_FAILED)
1811
{
1912
this->closed = 1;
20-
Nan::ThrowError("could not create semaphore : sem_open failed");
13+
Nan::ThrowError("Could not create semaphore: sem_open failed");
2114
return ;
2215
}
23-
if (debug) {
24-
int sval;
25-
if (sem_getvalue(this->semaphore, &sval)) sval = -1;
26-
printf("[posix-semaphore] Opened semaphore (initial value %u, current value %d)\n", value, sval);
27-
}
2816
this->locked = false;
2917
this->closed = false;
3018
this->strict = strict;
@@ -39,6 +27,7 @@ Semaphore::~Semaphore()
3927

4028
void Semaphore::Init(v8::Local<v8::Object> exports)
4129
{
30+
v8::Local<v8::Context> context = exports->CreationContext();
4231
Nan::HandleScope scope;
4332

4433
// Prepare constructor template
@@ -51,18 +40,24 @@ void Semaphore::Init(v8::Local<v8::Object> exports)
5140
Nan::SetPrototypeMethod(tpl, "release", Release);
5241
Nan::SetPrototypeMethod(tpl, "close", Close);
5342

54-
constructor.Reset(tpl->GetFunction());
55-
exports->Set(Nan::New("Semaphore").ToLocalChecked(), tpl->GetFunction());
43+
constructor.Reset(tpl->GetFunction(context).ToLocalChecked());
44+
exports->Set(
45+
context,
46+
Nan::New("Semaphore").ToLocalChecked(),
47+
tpl->GetFunction(context).ToLocalChecked()
48+
);
5649
}
5750

5851
void Semaphore::New(const Nan::FunctionCallbackInfo<v8::Value>& info)
5952
{
60-
bool strict;
61-
bool debug;
62-
bool silent;
63-
bool retry_on_eintr;
64-
char *buf;
65-
unsigned int value;
53+
v8::Isolate* isolate = info.GetIsolate();
54+
v8::Local<v8::Context> context = isolate->GetCurrentContext();
55+
bool strict;
56+
bool debug;
57+
bool silent;
58+
bool retry_on_eintr;
59+
char buf[SEMSIZE];
60+
unsigned int value;
6661

6762
if (!info.IsConstructCall())
6863
return Nan::ThrowError("Must call Semaphore() with new");
@@ -80,40 +75,36 @@ void Semaphore::New(const Nan::FunctionCallbackInfo<v8::Value>& info)
8075
return Nan::ThrowError("Semaphore() expects a boolean as fifth argument");
8176
if (!info[5]->IsUndefined() && !info[5]->IsUint32())
8277
return Nan::ThrowError("Semaphore() expects an integer as sixth argument");
83-
strict = info[1]->BooleanValue();
84-
debug = info[2]->BooleanValue();
85-
silent = info[3]->BooleanValue();
86-
retry_on_eintr = info[4]->BooleanValue();
87-
value = !info[5]->IsUndefined()? info[5]->IntegerValue(): 1;
88-
v8::String::Utf8Value str(info[0]->ToString());
89-
if (str.length() >= 255 || str.length() <= 0)
78+
strict = Nan::To<bool>(info[1]).FromJust();
79+
debug = Nan::To<bool>(info[2]).FromJust();
80+
silent = Nan::To<bool>(info[3]).FromJust();
81+
retry_on_eintr = Nan::To<bool>(info[4]).FromJust();
82+
value = !info[5]->IsUndefined()? info[5]->IntegerValue(context).FromJust(): 1;
83+
v8::String::Utf8Value v8str(isolate, info[0]);
84+
std::string str(*v8str);
85+
86+
size_t str_len;
87+
str_len = str.length();
88+
strncpy(buf, str.c_str(), str_len);
89+
buf[str_len] = '\0';
90+
91+
if (str_len >= SEMSIZE - 1 || str_len <= 0)
9092
return Nan::ThrowError("Semaphore() : first argument's length must be < 255 && > 0");
91-
buf = (char*)(*str);
92-
Semaphore* obj = new Semaphore(buf, str.length(), strict, debug, silent, retry_on_eintr, value);
93+
94+
Semaphore* obj = new Semaphore(buf, str_len, strict, debug, silent, retry_on_eintr, value);
9395
obj->Wrap(info.This());
9496
info.GetReturnValue().Set(info.This());
9597
}
9698

9799
void Semaphore::Acquire(const Nan::FunctionCallbackInfo<v8::Value>& info)
98100
{
99-
int r;
101+
int r;
100102
Semaphore* obj = ObjectWrap::Unwrap<Semaphore>(info.Holder());
101103

102-
/*if (!obj->strict && (obj->closed || obj->locked))
103-
{
104-
if (obj->debug)
105-
printf("[posix-semaphore] 'acquire' called when semaphore was already acquired or closed, but strict mode deactivated, so not failing\n");
106-
return ;
107-
}*/
108104
if (obj->strict && obj->closed)
109105
return Nan::ThrowError("trying to do operation over semaphore, but already closed");
110106
if (obj->strict && obj->locked)
111107
return Nan::ThrowError("trying to acquire semaphore, but already acquired");
112-
if (obj->strict && obj->debug) {
113-
int sval;
114-
if (sem_getvalue(obj->semaphore, &sval)) sval = -1;
115-
printf("[posix-semaphore] Before sem_wait, value = %d\n", sval);
116-
}
117108
while ((r = sem_wait(obj->semaphore)) == -1 && errno == EINTR && obj->retry_on_eintr)
118109
{
119110
if (obj->debug)
@@ -139,59 +130,32 @@ void Semaphore::Acquire(const Nan::FunctionCallbackInfo<v8::Value>& info)
139130
return ;
140131
}
141132
obj->locked = true;
142-
if (obj->debug) {
143-
int sval;
144-
if (sem_getvalue(obj->semaphore, &sval)) sval = -1;
145-
printf("[posix-semaphore] After sem_wait, value = %d\n", sval);
146-
}
147133
}
148134

149135
void Semaphore::Release(const Nan::FunctionCallbackInfo<v8::Value>& info)
150136
{
151137
Semaphore* obj = ObjectWrap::Unwrap<Semaphore>(info.Holder());
152-
153-
/*if (!obj->strict && (obj->closed || !obj->locked))
154-
{
155-
if (obj->debug)
156-
printf("[posix-semaphore] 'release' called when semaphore was already released or closed, but strict mode deactivated, so not failing\n");
157-
return ;
158-
}*/
138+
159139
if (obj->strict && obj->closed)
160140
return Nan::ThrowError("trying to do operation over semaphore, but already closed");
161141
if (obj->strict && !obj->locked)
162142
return Nan::ThrowError("trying to release semaphore, but already released");
163-
if (obj->debug) {
164-
int sval;
165-
if (sem_getvalue(obj->semaphore, &sval)) sval = -1;
166-
printf("[posix-semaphore] Before sem_post, value = %d\n", sval);
167-
}
168143
if (sem_post(obj->semaphore) == -1)
169144
{
170145
if (obj->debug || !obj->silent)
171146
{
172-
printf("[posix-semaphore] sem_post failed, printing errno message ('man sem_post' for more details on possible errors) : \n");
147+
printf("[posix-semaphore] sem_post failed, printing errno message ('man sem_post' for more details on possible errors): \n");
173148
perror("[posix-semaphore] ");
174149
}
175150
return Nan::ThrowError("could not release semaphore, sem_post failed");
176151
}
177152
obj->locked = false;
178-
if (obj->debug) {
179-
int sval;
180-
if (sem_getvalue(obj->semaphore, &sval)) sval = -1;
181-
printf("[posix-semaphore] After sem_post, value = %d\n", sval);
182-
}
183153
}
184154

185155
void Semaphore::Close(const Nan::FunctionCallbackInfo<v8::Value>& info)
186156
{
187157
Semaphore* obj = ObjectWrap::Unwrap<Semaphore>(info.Holder());
188158

189-
/*if (!obj->strict && obj->closed)
190-
{
191-
if (obj->debug)
192-
printf("[posix-semaphore] 'close' called when semaphore was already closed, but strict mode deactivated, so not failing\n");
193-
return ;
194-
}*/
195159
if (obj->strict && obj->closed)
196160
return Nan::ThrowError("trying to close semaphore, but already closed");
197161
if (obj->debug)

srcs/semaphore.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#ifndef SEMAPHORE_H
22
#define SEMAPHORE_H
3+
#define SEMSIZE 256
34

45
#include <nan.h>
56
#include <fcntl.h>
@@ -19,9 +20,9 @@ class Semaphore : public Nan::ObjectWrap {
1920
static void Release(const Nan::FunctionCallbackInfo<v8::Value>& info);
2021
static void Close(const Nan::FunctionCallbackInfo<v8::Value>& info);
2122
static Nan::Persistent<v8::Function> constructor;
22-
23+
2324
sem_t *semaphore;
24-
char sem_name[256];
25+
char sem_name[SEMSIZE];
2526
bool locked;
2627
bool closed;
2728
bool strict;

0 commit comments

Comments
 (0)