File: global_object_get_property.cc

package info (click to toggle)
node-addon-api 8.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,248 kB
  • sloc: cpp: 15,431; javascript: 5,631; ansic: 157; makefile: 7
file content (40 lines) | stat: -rw-r--r-- 1,424 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
#include "napi.h"
#include "test_helper.h"

using namespace Napi;

Value GetPropertyWithNapiValueAsKey(const CallbackInfo& info) {
  Object globalObject = info.Env().Global();
  Name key = info[0].UnsafeAs<Name>();
  return MaybeUnwrap(globalObject.Get(key));
}

Value GetPropertyWithInt32AsKey(const CallbackInfo& info) {
  Object globalObject = info.Env().Global();
  Number key = info[0].UnsafeAs<Napi::Number>();
  return MaybeUnwrapOr(globalObject.Get(key.Uint32Value()), Value());
}

Value GetPropertyWithCStyleStringAsKey(const CallbackInfo& info) {
  Object globalObject = info.Env().Global();
  String cStrkey = info[0].UnsafeAs<String>();
  return MaybeUnwrapOr(globalObject.Get(cStrkey.Utf8Value().c_str()), Value());
}

Value GetPropertyWithCppStyleStringAsKey(const CallbackInfo& info) {
  Object globalObject = info.Env().Global();
  String cppStrKey = info[0].UnsafeAs<String>();
  return MaybeUnwrapOr(globalObject.Get(cppStrKey.Utf8Value()), Value());
}

void CreateMockTestObject(const CallbackInfo& info) {
  Object globalObject = info.Env().Global();
  Number napi_key = Number::New(info.Env(), 2);
  const char* CStringKey = "c_str_key";

  globalObject.Set(napi_key, "napi_attribute");
  globalObject[CStringKey] = "c_string_attribute";
  globalObject[std::string("cpp_string_key")] = "cpp_string_attribute";
  globalObject[std::string("circular")] = globalObject;
  globalObject[(uint32_t)15] = 15;
}