File: indexedinterceptors.cpp

package info (click to toggle)
node-nan 2.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 1,212 kB
  • ctags: 1,658
  • sloc: cpp: 6,307; python: 3,105; ansic: 1,314; makefile: 104; sh: 33
file content (116 lines) | stat: -rw-r--r-- 3,730 bytes parent folder | download
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*********************************************************************
 * NAN - Native Abstractions for Node.js
 *
 * Copyright (c) 2016 NAN contributors
 *
 * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
 ********************************************************************/

#include <nan.h>
#include <cstring>

using namespace Nan;  // NOLINT(build/namespaces)

class IndexedInterceptor : public ObjectWrap {
  char buf[256];

 public:
  IndexedInterceptor() { std::strncpy(this->buf, "foo", sizeof (this->buf)); }
  static NAN_MODULE_INIT(Init);
  static v8::Local<v8::Value> NewInstance ();
  static NAN_METHOD(New);

  static NAN_INDEX_GETTER(PropertyGetter);
  static NAN_INDEX_SETTER(PropertySetter);
  static NAN_INDEX_ENUMERATOR(PropertyEnumerator);
  static NAN_INDEX_DELETER(PropertyDeleter);
  static NAN_INDEX_QUERY(PropertyQuery);
};

static Persistent<v8::FunctionTemplate> indexedinterceptors_constructor;

NAN_METHOD(CreateNew) {
  info.GetReturnValue().Set(IndexedInterceptor::NewInstance());
}

NAN_MODULE_INIT(IndexedInterceptor::Init) {
  v8::Local<v8::FunctionTemplate> tpl =
    Nan::New<v8::FunctionTemplate>(IndexedInterceptor::New);
  indexedinterceptors_constructor.Reset(tpl);
  tpl->SetClassName(Nan::New("IndexedInterceptor").ToLocalChecked());
  tpl->InstanceTemplate()->SetInternalFieldCount(1);
  v8::Local<v8::ObjectTemplate> inst = tpl->InstanceTemplate();

  SetIndexedPropertyHandler(
      inst
    , IndexedInterceptor::PropertyGetter
    , IndexedInterceptor::PropertySetter
    , IndexedInterceptor::PropertyQuery
    , IndexedInterceptor::PropertyDeleter
    , IndexedInterceptor::PropertyEnumerator);

  v8::Local<v8::Function> createnew =
    Nan::New<v8::FunctionTemplate>(CreateNew)->GetFunction();
  Set(target, Nan::New("create").ToLocalChecked(), createnew);
}

v8::Local<v8::Value> IndexedInterceptor::NewInstance () {
  EscapableHandleScope scope;
  v8::Local<v8::FunctionTemplate> constructorHandle =
      Nan::New(indexedinterceptors_constructor);
  v8::Local<v8::Object> instance =
    constructorHandle->GetFunction()->NewInstance(0, NULL);
  return scope.Escape(instance);
}

NAN_METHOD(IndexedInterceptor::New) {
  IndexedInterceptor* interceptor = new IndexedInterceptor();
  interceptor->Wrap(info.This());
  info.GetReturnValue().Set(info.This());
}


NAN_INDEX_GETTER(IndexedInterceptor::PropertyGetter) {
  IndexedInterceptor* interceptor =
    ObjectWrap::Unwrap<IndexedInterceptor>(info.Holder());
  if (index == 0) {
    info.GetReturnValue().Set(Nan::New(interceptor->buf).ToLocalChecked());
  } else {
    info.GetReturnValue().Set(Nan::New("bar").ToLocalChecked());
  }
}

NAN_INDEX_SETTER(IndexedInterceptor::PropertySetter) {
  IndexedInterceptor* interceptor =
    ObjectWrap::Unwrap<IndexedInterceptor>(info.Holder());
  if (index == 0) {
    std::strncpy(
        interceptor->buf
      , *v8::String::Utf8Value(value)
      , sizeof (interceptor->buf));
    info.GetReturnValue().Set(info.This());
  } else {
    info.GetReturnValue().Set(info.This());
  }
}

NAN_INDEX_ENUMERATOR(IndexedInterceptor::PropertyEnumerator) {
  v8::Local<v8::Array> arr = Nan::New<v8::Array>();
  Set(arr, 0, Nan::New("whee").ToLocalChecked());
  info.GetReturnValue().Set(arr);
}

NAN_INDEX_DELETER(IndexedInterceptor::PropertyDeleter) {
  IndexedInterceptor* interceptor =
    ObjectWrap::Unwrap<IndexedInterceptor>(info.Holder());
  std::strncpy(interceptor->buf, "goober", sizeof (interceptor->buf));
  info.GetReturnValue().Set(True());
}

NAN_INDEX_QUERY(IndexedInterceptor::PropertyQuery) {
  if (index == 1) {
    info.GetReturnValue().Set(Nan::New<v8::Integer>(v8::DontEnum));
  }
}

NODE_MODULE(indexedinterceptors, IndexedInterceptor::Init)