-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.cc
More file actions
83 lines (57 loc) · 2.43 KB
/
sample.cc
File metadata and controls
83 lines (57 loc) · 2.43 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <node.h>
#include <string>
#include "ArrayBuffer.h"
using namespace v8;
#if NODE_MINOR_VERSION > 11
void CreateArrayBuffer(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate *isolate = args.GetIsolate();
node::ArrayBuffer *buffer = node::ArrayBuffer::New(isolate, "Hello World!");
args.GetReturnValue().Set(buffer->ToArrayBuffer());
}
void PrintArrayBuffer(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate *isolate = args.GetIsolate();
node::ArrayBuffer *buffer = node::ArrayBuffer::New(isolate, args[0]);
printf("node::ArrayBuffer: '%s', %zu\n", buffer->ToUtf8(), buffer->Length());
}
void DisposeMemory(const v8::FunctionCallbackInfo<v8::Value>& args) {
Isolate *isolate = args.GetIsolate();
isolate->LowMemoryNotification();
}
void SampleInit(Handle<Object> exports) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
exports->Set(String::NewFromUtf8(isolate, "CreateArrayBuffer"),
FunctionTemplate::New(isolate, CreateArrayBuffer)->GetFunction());
exports->Set(String::NewFromUtf8(isolate, "PrintArrayBuffer"),
FunctionTemplate::New(isolate, PrintArrayBuffer)->GetFunction());
exports->Set(String::NewFromUtf8(isolate, "DisposeMemory"),
FunctionTemplate::New(isolate, DisposeMemory)->GetFunction());
}
#else
static Handle<Value> CreateArrayBuffer(const Arguments& args) {
HandleScope scope;
node::ArrayBuffer *buffer = node::ArrayBuffer::New("Hello World!");
return scope.Close(buffer->ToArrayBuffer());
}
static Handle<Value> PrintArrayBuffer(const Arguments& args) {
HandleScope scope;
node::ArrayBuffer *buffer = node::ArrayBuffer::New(args[0]);
printf("node::ArrayBuffer: '%s', %zu\n", buffer->ToUtf8(), buffer->Length());
return scope.Close(Undefined());
}
static Handle<Value> DisposeMemory(const Arguments& args) {
HandleScope scope;
V8::LowMemoryNotification();
return scope.Close(Undefined());
}
void SampleInit(Handle<Object> exports) {
HandleScope scope;
exports->Set(String::New("CreateArrayBuffer"),
FunctionTemplate::New(CreateArrayBuffer)->GetFunction());
exports->Set(String::New("PrintArrayBuffer"),
FunctionTemplate::New(PrintArrayBuffer)->GetFunction());
exports->Set(String::New("DisposeMemory"),
FunctionTemplate::New(DisposeMemory)->GetFunction());
}
#endif
NODE_MODULE(sample, SampleInit);