forked from bbondy/bloom-filter-cpp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
BloomFilterWrap.cpp
90 lines (71 loc) · 2.83 KB
/
BloomFilterWrap.cpp
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
84
85
86
87
88
89
90
/* Copyright (c) 2015 Brian R. Bondy. Distributed under the MPL2 license.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "BloomFilterWrap.h"
namespace BloomFilterWrap {
using v8::Context;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::FunctionTemplate;
using v8::Isolate;
using v8::Local;
using v8::Number;
using v8::Object;
using v8::Persistent;
using v8::String;
using v8::Boolean;
using v8::Value;
Persistent<Function> BloomFilterWrap::constructor;
BloomFilterWrap::BloomFilterWrap(unsigned int bitsPerElement,
unsigned int estimatedNumElements, HashFn hashFns[], int numHashFns)
: BloomFilter(bitsPerElement, estimatedNumElements, hashFns, numHashFns) {
}
BloomFilterWrap::~BloomFilterWrap() {
}
void BloomFilterWrap::Init(Local<Object> exports) {
Isolate* isolate = exports->GetIsolate();
// Prepare constructor template
Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
tpl->SetClassName(String::NewFromUtf8(isolate, "BloomFilter"));
tpl->InstanceTemplate()->SetInternalFieldCount(1);
// Prototype
NODE_SET_PROTOTYPE_METHOD(tpl, "add", BloomFilterWrap::Add);
NODE_SET_PROTOTYPE_METHOD(tpl, "exists", BloomFilterWrap::Exists);
constructor.Reset(isolate, tpl->GetFunction());
exports->Set(String::NewFromUtf8(isolate, "BloomFilter"),
tpl->GetFunction());
}
void BloomFilterWrap::New(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
if (args.IsConstructCall()) {
// Invoked as constructor: `new BloomFilter(...)`
BloomFilterWrap* obj = new BloomFilterWrap();
obj->Wrap(args.This());
args.GetReturnValue().Set(args.This());
} else {
// Invoked as plain function `BloomFilter(...)`, turn into construct call.
const int argc = 1;
Local<Value> argv[argc] = { args[0] };
Local<Context> context = isolate->GetCurrentContext();
Local<Function> cons = Local<Function>::New(isolate, constructor);
Local<Object> result =
cons->NewInstance(context, argc, argv).ToLocalChecked();
args.GetReturnValue().Set(result);
}
}
void BloomFilterWrap::Add(const FunctionCallbackInfo<Value>& args) {
String::Utf8Value str(args[0]->ToString());
const char * buffer = *str;
BloomFilterWrap* obj = ObjectWrap::Unwrap<BloomFilterWrap>(args.Holder());
obj->add(buffer);
}
void BloomFilterWrap::Exists(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = args.GetIsolate();
String::Utf8Value str(args[0]->ToString());
const char * buffer = *str;
BloomFilterWrap* obj = ObjectWrap::Unwrap<BloomFilterWrap>(args.Holder());
bool exists = obj->exists(buffer);
args.GetReturnValue().Set(Boolean::New(isolate, exists));
}
} // namespace BloomFilterWrap