|
| 1 | +/********************************************************************* |
| 2 | + * NAN - Native Abstractions for Node.js |
| 3 | + * |
| 4 | + * Copyright (c) 2018 NAN contributors |
| 5 | + * |
| 6 | + * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md> |
| 7 | + ********************************************************************/ |
| 8 | + |
| 9 | +#include <nan.h> |
| 10 | +#include <cstring> |
| 11 | + |
| 12 | +using namespace Nan; // NOLINT(build/namespaces) |
| 13 | + |
| 14 | +class NamedInterceptor : public ObjectWrap { |
| 15 | + char buf[256]; |
| 16 | + |
| 17 | + public: |
| 18 | + NamedInterceptor() { std::strncpy(this->buf, "foo", sizeof (this->buf)); } |
| 19 | + static NAN_MODULE_INIT(Init); |
| 20 | + static v8::Local<v8::Value> NewInstance (); |
| 21 | + static NAN_METHOD(New); |
| 22 | + |
| 23 | + static NAN_PROPERTY_GETTER(PropertyGetter); |
| 24 | + static NAN_PROPERTY_SETTER(PropertySetter); |
| 25 | + static NAN_PROPERTY_ENUMERATOR(PropertyEnumerator); |
| 26 | + static NAN_PROPERTY_DELETER(PropertyDeleter); |
| 27 | + static NAN_PROPERTY_QUERY(PropertyQuery); |
| 28 | +}; |
| 29 | + |
| 30 | +static Persistent<v8::FunctionTemplate> namedinterceptors_constructor; |
| 31 | + |
| 32 | +NAN_METHOD(CreateNew) { |
| 33 | + info.GetReturnValue().Set(NamedInterceptor::NewInstance()); |
| 34 | +} |
| 35 | + |
| 36 | +NAN_MODULE_INIT(NamedInterceptor::Init) { |
| 37 | + v8::Local<v8::FunctionTemplate> tpl = |
| 38 | + Nan::New<v8::FunctionTemplate>(NamedInterceptor::New); |
| 39 | + namedinterceptors_constructor.Reset(tpl); |
| 40 | + tpl->SetClassName(Nan::New("NamedInterceptor").ToLocalChecked()); |
| 41 | + tpl->InstanceTemplate()->SetInternalFieldCount(1); |
| 42 | + v8::Local<v8::ObjectTemplate> inst = tpl->InstanceTemplate(); |
| 43 | + |
| 44 | + SetNamedPropertyHandler( |
| 45 | + inst |
| 46 | + , NamedInterceptor::PropertyGetter |
| 47 | + , NamedInterceptor::PropertySetter |
| 48 | + , NamedInterceptor::PropertyQuery |
| 49 | + , NamedInterceptor::PropertyDeleter |
| 50 | + , NamedInterceptor::PropertyEnumerator); |
| 51 | + |
| 52 | + v8::Local<v8::Function> createnew = |
| 53 | + Nan::GetFunction(Nan::New<v8::FunctionTemplate>(CreateNew)) |
| 54 | + .ToLocalChecked(); |
| 55 | + Set(target, Nan::New("create").ToLocalChecked(), createnew); |
| 56 | +} |
| 57 | + |
| 58 | +v8::Local<v8::Value> NamedInterceptor::NewInstance () { |
| 59 | + EscapableHandleScope scope; |
| 60 | + v8::Local<v8::FunctionTemplate> constructorHandle = |
| 61 | + Nan::New(namedinterceptors_constructor); |
| 62 | + v8::Local<v8::Object> instance = |
| 63 | + Nan::NewInstance(GetFunction(constructorHandle).ToLocalChecked()) |
| 64 | + .ToLocalChecked(); |
| 65 | + return scope.Escape(instance); |
| 66 | +} |
| 67 | + |
| 68 | +NAN_METHOD(NamedInterceptor::New) { |
| 69 | + NamedInterceptor* interceptor = new NamedInterceptor(); |
| 70 | + interceptor->Wrap(info.This()); |
| 71 | + info.GetReturnValue().Set(info.This()); |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +NAN_PROPERTY_GETTER(NamedInterceptor::PropertyGetter) { |
| 76 | + NamedInterceptor* interceptor = |
| 77 | + ObjectWrap::Unwrap<NamedInterceptor>(info.Holder()); |
| 78 | + if (!std::strcmp(*Nan::Utf8String(property), "prop")) { |
| 79 | + info.GetReturnValue().Set(Nan::New(interceptor->buf).ToLocalChecked()); |
| 80 | + } else { |
| 81 | + info.GetReturnValue().Set(Nan::New("bar").ToLocalChecked()); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +NAN_PROPERTY_SETTER(NamedInterceptor::PropertySetter) { |
| 86 | + NamedInterceptor* interceptor = |
| 87 | + ObjectWrap::Unwrap<NamedInterceptor>(info.Holder()); |
| 88 | + if (!std::strcmp(*Nan::Utf8String(property), "prop")) { |
| 89 | + std::strncpy( |
| 90 | + interceptor->buf |
| 91 | + , *Nan::Utf8String(value) |
| 92 | + , sizeof (interceptor->buf)); |
| 93 | + info.GetReturnValue().Set(info.This()); |
| 94 | + } else { |
| 95 | + info.GetReturnValue().Set(info.This()); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +NAN_PROPERTY_ENUMERATOR(NamedInterceptor::PropertyEnumerator) { |
| 100 | + v8::Local<v8::Array> arr = Nan::New<v8::Array>(); |
| 101 | + Set(arr, 0, Nan::New("value").ToLocalChecked()); |
| 102 | + info.GetReturnValue().Set(arr); |
| 103 | +} |
| 104 | + |
| 105 | +NAN_PROPERTY_DELETER(NamedInterceptor::PropertyDeleter) { |
| 106 | + NamedInterceptor* interceptor = |
| 107 | + ObjectWrap::Unwrap<NamedInterceptor>(info.Holder()); |
| 108 | + std::strncpy(interceptor->buf, "goober", sizeof (interceptor->buf)); |
| 109 | + info.GetReturnValue().Set(True()); |
| 110 | +} |
| 111 | + |
| 112 | +NAN_PROPERTY_QUERY(NamedInterceptor::PropertyQuery) { |
| 113 | + Nan::Utf8String s(property); |
| 114 | + if (!std::strcmp(*s, "thing")) { |
| 115 | + return info.GetReturnValue().Set(Nan::New<v8::Integer>(v8::DontEnum)); |
| 116 | + } |
| 117 | + if (!std::strcmp(*s, "value")) { |
| 118 | + return info.GetReturnValue().Set(Nan::New(0)); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +NODE_MODULE(namedinterceptors, NamedInterceptor::Init) |
0 commit comments