File: morenews.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 (104 lines) | stat: -rw-r--r-- 2,944 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
/*********************************************************************
 * 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>

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

NAN_METHOD(NewNumber) {
  info.GetReturnValue().Set(New(0.5));
}

NAN_METHOD(NewNegativeInteger) {
  info.GetReturnValue().Set(New(-1));
}

NAN_METHOD(NewPositiveInteger) {
  info.GetReturnValue().Set(New(1));
}

NAN_METHOD(NewUtf8String) {
  const char s[] = "strĂ¯ng";
  info.GetReturnValue().Set(New(s).ToLocalChecked());
}

NAN_METHOD(NewLatin1String) {
  const uint8_t s[] = "str\xefng";
  info.GetReturnValue().Set(NewOneByteString(s).ToLocalChecked());
}

NAN_METHOD(NewUcs2String) {
  uint16_t s[] = {'s', 't', 'r', 0xef, 'n', 'g', '\0'};
  info.GetReturnValue().Set(New(s).ToLocalChecked());
}

static const uint16_t ws[] = {'s', 't', 'r', 0xef, 'n', 'g', '\0'};
static const char s[] = {'s', 't', 'r', 'i', 'n', 'g', '\0'};

class ExtString : public v8::String::ExternalStringResource {
 public:
  ~ExtString() { }
  const uint16_t *data() const { return ws; }
  size_t length() const { return sizeof (ws) / sizeof (*ws) - 1; }
};


class ExtAsciiString : public ExternalOneByteStringResource {
 public:
  ~ExtAsciiString() { }
  const char *data() const { return s; }
  size_t length() const { return sizeof (s) / sizeof (*s) - 1; }
};

NAN_METHOD(NewExternalStringResource) {
  v8::Local<v8::String> ext = New(new ExtString()).ToLocalChecked();
  info.GetReturnValue().Set(ext);
}

NAN_METHOD(NewExternalAsciiStringResource) {
  v8::Local<v8::String> ext = New(new ExtAsciiString()).ToLocalChecked();
  info.GetReturnValue().Set(ext);
}

NAN_MODULE_INIT(Init) {
  Set(target
    , New("newNumber").ToLocalChecked()
    , New<v8::FunctionTemplate>(NewNumber)->GetFunction()
  );
  Set(target
    , New("newNegativeInteger").ToLocalChecked()
    , New<v8::FunctionTemplate>(NewNegativeInteger)->GetFunction()
  );
  Set(target
    , New("newPositiveInteger").ToLocalChecked()
    , New<v8::FunctionTemplate>(NewPositiveInteger)->GetFunction()
  );
  Set(target
    , New("newUtf8String").ToLocalChecked()
    , New<v8::FunctionTemplate>(NewUtf8String)->GetFunction()
  );
  Set(target
    , New("newLatin1String").ToLocalChecked()
    , New<v8::FunctionTemplate>(NewLatin1String)->GetFunction()
  );
  Set(target
    , New("newUcs2String").ToLocalChecked()
    , New<v8::FunctionTemplate>(NewUcs2String)->GetFunction()
  );
  Set(target
    , New("newExternalStringResource").ToLocalChecked()
    , New<v8::FunctionTemplate>(NewExternalStringResource)->GetFunction()
  );
  Set(target
    , New("newExternalAsciiStringResource").ToLocalChecked()
    , New<v8::FunctionTemplate>(NewExternalAsciiStringResource)
    ->GetFunction()
  );
}

NODE_MODULE(morenews, Init)