forked from nodejs/node-v0.x-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinding.cc
More file actions
55 lines (36 loc) · 1.05 KB
/
Copy pathbinding.cc
File metadata and controls
55 lines (36 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <node.h>
#include <v8.h>
#include <uv.h>
using namespace v8;
extern "C" {
void init(Handle<Object> target);
}
#define BUFSIZE 1024
static uint8_t buf[BUFSIZE];
static uv_mutex_t lock;
Handle<Value> Get(const Arguments& args) {
HandleScope scope;
int index = args[0]->Uint32Value();
if (index < 0 || BUFSIZE <= index) {
return ThrowException(Exception::Error(String::New("out of bounds")));
}
return scope.Close(Integer::New(buf[index]));
}
Handle<Value> Set(const Arguments& args) {
uv_mutex_lock(&lock);
HandleScope scope;
int index = args[0]->Uint32Value();
if (index < 0 || BUFSIZE <= index) {
return ThrowException(Exception::Error(String::New("out of bounds")));
}
buf[index] = args[1]->Uint32Value();
Local<Integer> val = Integer::New(buf[index]);
uv_mutex_unlock(&lock);
return scope.Close(val);
}
void init(Handle<Object> target) {
NODE_SET_METHOD(target, "get", Get);
NODE_SET_METHOD(target, "set", Set);
target->Set(String::New("length"), Integer::New(BUFSIZE));
uv_mutex_init(&lock);
}