File indexing completed on 2024-11-24 04:54:36

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 "./hash_set_wrap.h"
0011 
0012 namespace HashSetWrap {
0013 
0014 using v8::Function;
0015 using v8::FunctionCallbackInfo;
0016 using v8::FunctionTemplate;
0017 using v8::Isolate;
0018 using v8::Local;
0019 using v8::Number;
0020 using v8::Object;
0021 using v8::Persistent;
0022 using v8::String;
0023 using v8::Boolean;
0024 using v8::Value;
0025 
0026 Persistent<Function> HashSetWrap::constructor;
0027 
0028 HashSetWrap::HashSetWrap(uint32_t bucket_count, bool multi_set)
0029   : HashSet(bucket_count, multi_set) {
0030 }
0031 
0032 HashSetWrap::~HashSetWrap() {
0033 }
0034 
0035 void HashSetWrap::Init(Local<Object> exports) {
0036   Isolate* isolate = exports->GetIsolate();
0037 
0038   // Prepare constructor template
0039   Local<FunctionTemplate> tpl = FunctionTemplate::New(isolate, New);
0040   tpl->SetClassName(String::NewFromUtf8(isolate, "HashSet"));
0041   tpl->InstanceTemplate()->SetInternalFieldCount(1);
0042 
0043   // Prototype
0044   NODE_SET_PROTOTYPE_METHOD(tpl, "add", HashSetWrap::AddItem);
0045   NODE_SET_PROTOTYPE_METHOD(tpl, "exists", HashSetWrap::ItemExists);
0046 
0047   constructor.Reset(isolate, tpl->GetFunction());
0048   exports->Set(String::NewFromUtf8(isolate, "HashSet"),
0049                tpl->GetFunction());
0050 }
0051 
0052 void HashSetWrap::New(const FunctionCallbackInfo<Value>& args) {
0053   Isolate* isolate = args.GetIsolate();
0054 
0055   if (args.IsConstructCall()) {
0056     // Invoked as constructor: `new HashSet(...)`
0057     HashSetWrap* obj = new HashSetWrap(1024, false);
0058     obj->Wrap(args.This());
0059     args.GetReturnValue().Set(args.This());
0060   } else {
0061     // Invoked as plain function `HashSet(...)`, turn into construct call.
0062     const int argc = 1;
0063     Local<Value> argv[argc] = { args[0] };
0064     Local<Function> cons = Local<Function>::New(isolate, constructor);
0065     args.GetReturnValue().Set(
0066         cons->NewInstance(isolate->GetCurrentContext(), argc, argv)
0067             .ToLocalChecked());
0068   }
0069 }
0070 
0071 void HashSetWrap::AddItem(const FunctionCallbackInfo<Value>& args) {
0072   Isolate* isolate = args.GetIsolate();
0073   String::Utf8Value str(isolate, args[0]->ToString());
0074   const char * buffer = *str;
0075 
0076   HashSetWrap* obj = ObjectWrap::Unwrap<HashSetWrap>(args.Holder());
0077   obj->Add(ExampleData(buffer));
0078 }
0079 
0080 void HashSetWrap::ItemExists(const FunctionCallbackInfo<Value>& args) {
0081   Isolate* isolate = args.GetIsolate();
0082   String::Utf8Value str(isolate, args[0]->ToString());
0083   const char * buffer = *str;
0084 
0085   HashSetWrap* obj = ObjectWrap::Unwrap<HashSetWrap>(args.Holder());
0086   bool exists = obj->Exists(ExampleData(buffer));
0087 
0088   args.GetReturnValue().Set(Boolean::New(isolate, exists));
0089 }
0090 
0091 
0092 }  // namespace HashSetWrap