File indexing completed on 2024-11-24 04:54:34
0001 /* 0002 SPDX-License-Identifier: MPL-2.0 0003 */ 0004 0005 /* Copyright (c) 2015 Brian R. Bondy. Distributed under the MPL2 license. 0006 * This Source Code Form is subject to the terms of the Mozilla Public 0007 * License, v. 2.0. If a copy of the MPL was not distributed with this 0008 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 0009 0010 #include "BloomFilterWrap.h" 0011 0012 namespace BloomFilterWrap { 0013 0014 using v8::Context; 0015 using v8::Function; 0016 using v8::FunctionCallbackInfo; 0017 using v8::FunctionTemplate; 0018 using v8::Isolate; 0019 using v8::Local; 0020 using v8::Number; 0021 using v8::Object; 0022 using v8::Persistent; 0023 using v8::String; 0024 using v8::Boolean; 0025 using v8::Value; 0026 0027 Persistent<Function> BloomFilterWrap::constructor; 0028 0029 BloomFilterWrap::BloomFilterWrap(unsigned int bitsPerElement, 0030 unsigned int estimatedNumElements, HashFn hashFns[], int numHashFns) 0031 : BloomFilter(bitsPerElement, estimatedNumElements, hashFns, numHashFns) { 0032 } 0033 0034 BloomFilterWrap::~BloomFilterWrap() { 0035 } 0036 0037 void BloomFilterWrap::Init(Local<Object> exports) { 0038 Isolate* isolate = exports->GetIsolate(); 0039 0040 // Prepare constructor template 0041 Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New); 0042 tpl->SetClassName(String::NewFromUtf8(isolate, "BloomFilter")); 0043 tpl->InstanceTemplate()->SetInternalFieldCount(1); 0044 0045 // Prototype 0046 NODE_SET_PROTOTYPE_METHOD(tpl, "add", BloomFilterWrap::Add); 0047 NODE_SET_PROTOTYPE_METHOD(tpl, "exists", BloomFilterWrap::Exists); 0048 0049 constructor.Reset(isolate, tpl->GetFunction()); 0050 exports->Set(String::NewFromUtf8(isolate, "BloomFilter"), 0051 tpl->GetFunction()); 0052 } 0053 0054 void BloomFilterWrap::New(const FunctionCallbackInfo<Value>& args) { 0055 Isolate* isolate = args.GetIsolate(); 0056 0057 if (args.IsConstructCall()) { 0058 // Invoked as constructor: `new BloomFilter(...)` 0059 BloomFilterWrap* obj = new BloomFilterWrap(); 0060 obj->Wrap(args.This()); 0061 args.GetReturnValue().Set(args.This()); 0062 } else { 0063 // Invoked as plain function `BloomFilter(...)`, turn into construct call. 0064 const int argc = 1; 0065 Local<Value> argv[argc] = { args[0] }; 0066 Local<Context> context = isolate->GetCurrentContext(); 0067 Local<Function> cons = Local<Function>::New(isolate, constructor); 0068 Local<Object> result = 0069 cons->NewInstance(context, argc, argv).ToLocalChecked(); 0070 args.GetReturnValue().Set(result); 0071 } 0072 } 0073 0074 void BloomFilterWrap::Add(const FunctionCallbackInfo<Value>& args) { 0075 Isolate* isolate = args.GetIsolate(); 0076 String::Utf8Value str(isolate, args[0]->ToString()); 0077 const char * buffer = *str; 0078 0079 BloomFilterWrap* obj = ObjectWrap::Unwrap<BloomFilterWrap>(args.Holder()); 0080 obj->add(buffer); 0081 } 0082 0083 void BloomFilterWrap::Exists(const FunctionCallbackInfo<Value>& args) { 0084 Isolate* isolate = args.GetIsolate(); 0085 String::Utf8Value str(isolate, args[0]->ToString()); 0086 const char * buffer = *str; 0087 0088 BloomFilterWrap* obj = ObjectWrap::Unwrap<BloomFilterWrap>(args.Holder()); 0089 bool exists = obj->exists(buffer); 0090 0091 args.GetReturnValue().Set(Boolean::New(isolate, exists)); 0092 } 0093 0094 0095 } // namespace BloomFilterWrap