|
| 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 | + |
| 11 | +using namespace Nan; // NOLINT(build/namespaces) |
| 12 | + |
| 13 | +NAN_METHOD(NewNumber) { |
| 14 | + info.GetReturnValue().Set(New(0.5)); |
| 15 | +} |
| 16 | + |
| 17 | +NAN_METHOD(NewNegativeInteger) { |
| 18 | + info.GetReturnValue().Set(New(-1)); |
| 19 | +} |
| 20 | + |
| 21 | +NAN_METHOD(NewPositiveInteger) { |
| 22 | + info.GetReturnValue().Set(New(1)); |
| 23 | +} |
| 24 | + |
| 25 | +NAN_METHOD(NewUtf8String) { |
| 26 | + const char s[] = "strïng"; |
| 27 | + info.GetReturnValue().Set(New(s).ToLocalChecked()); |
| 28 | +} |
| 29 | + |
| 30 | +NAN_METHOD(NewLatin1String) { |
| 31 | + const uint8_t s[] = "str\xefng"; |
| 32 | + info.GetReturnValue().Set(NewOneByteString(s).ToLocalChecked()); |
| 33 | +} |
| 34 | + |
| 35 | +NAN_METHOD(NewUcs2String) { |
| 36 | + uint16_t s[] = {'s', 't', 'r', 0xef, 'n', 'g', '\0'}; |
| 37 | + info.GetReturnValue().Set(New(s).ToLocalChecked()); |
| 38 | +} |
| 39 | + |
| 40 | +static const uint16_t ws[] = {'s', 't', 'r', 0xef, 'n', 'g', '\0'}; |
| 41 | +static const char s[] = {'s', 't', 'r', 'i', 'n', 'g', '\0'}; |
| 42 | + |
| 43 | +class ExtString : public v8::String::ExternalStringResource { |
| 44 | + public: |
| 45 | + ~ExtString() { } |
| 46 | + const uint16_t *data() const { return ws; } |
| 47 | + size_t length() const { return sizeof (ws) / sizeof (*ws) - 1; } |
| 48 | +}; |
| 49 | + |
| 50 | + |
| 51 | +class ExtAsciiString : public ExternalOneByteStringResource { |
| 52 | + public: |
| 53 | + ~ExtAsciiString() { } |
| 54 | + const char *data() const { return s; } |
| 55 | + size_t length() const { return sizeof (s) / sizeof (*s) - 1; } |
| 56 | +}; |
| 57 | + |
| 58 | +NAN_METHOD(NewExternalStringResource) { |
| 59 | + v8::Local<v8::String> ext = New(new ExtString()).ToLocalChecked(); |
| 60 | + info.GetReturnValue().Set(ext); |
| 61 | +} |
| 62 | + |
| 63 | +NAN_METHOD(NewExternalAsciiStringResource) { |
| 64 | + v8::Local<v8::String> ext = New(new ExtAsciiString()).ToLocalChecked(); |
| 65 | + info.GetReturnValue().Set(ext); |
| 66 | +} |
| 67 | + |
| 68 | +NAN_MODULE_INIT(Init) { |
| 69 | + Set(target |
| 70 | + , New("newNumber").ToLocalChecked() |
| 71 | + , GetFunction(New<v8::FunctionTemplate>(NewNumber)).ToLocalChecked() |
| 72 | + ); |
| 73 | + Set(target |
| 74 | + , New("newNegativeInteger").ToLocalChecked() |
| 75 | + , GetFunction(New<v8::FunctionTemplate>(NewNegativeInteger)) |
| 76 | + .ToLocalChecked() |
| 77 | + ); |
| 78 | + Set(target |
| 79 | + , New("newPositiveInteger").ToLocalChecked() |
| 80 | + , GetFunction(New<v8::FunctionTemplate>(NewPositiveInteger)) |
| 81 | + .ToLocalChecked() |
| 82 | + ); |
| 83 | + Set(target |
| 84 | + , New("newUtf8String").ToLocalChecked() |
| 85 | + , GetFunction(New<v8::FunctionTemplate>(NewUtf8String)).ToLocalChecked() |
| 86 | + ); |
| 87 | + Set(target |
| 88 | + , New("newLatin1String").ToLocalChecked() |
| 89 | + , GetFunction(New<v8::FunctionTemplate>(NewLatin1String)).ToLocalChecked() |
| 90 | + ); |
| 91 | + Set(target |
| 92 | + , New("newUcs2String").ToLocalChecked() |
| 93 | + , GetFunction(New<v8::FunctionTemplate>(NewUcs2String)).ToLocalChecked() |
| 94 | + ); |
| 95 | + Set(target |
| 96 | + , New("newExternalStringResource").ToLocalChecked() |
| 97 | + , GetFunction(New<v8::FunctionTemplate>(NewExternalStringResource)) |
| 98 | + .ToLocalChecked() |
| 99 | + ); |
| 100 | + Set(target |
| 101 | + , New("newExternalAsciiStringResource").ToLocalChecked() |
| 102 | + , GetFunction(New<v8::FunctionTemplate>(NewExternalAsciiStringResource)) |
| 103 | + .ToLocalChecked() |
| 104 | + ); |
| 105 | +} |
| 106 | + |
| 107 | +NODE_MODULE(morenews, Init) |
0 commit comments