asherah 3.0.16 → 4.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,115 +0,0 @@
1
- #include "logging_napi.h"
2
- #include "napi_utils.h"
3
-
4
- LoggerNapi::LoggerNapi(Napi::Env &env, const std::string &system_name)
5
- : StdErrLogger(system_name), log_hook(Napi::FunctionReference()), env(env) {
6
- }
7
-
8
- [[maybe_unused]] LoggerNapi::LoggerNapi(Napi::Env &env,
9
- const std::string &system_name,
10
- Napi::Function new_log_hook)
11
- : StdErrLogger(system_name), env(env) {
12
- if (unlikely(new_log_hook.IsEmpty())) {
13
- NapiUtils::ThrowException(env,
14
- system_name + ": new_log_hook cannot be nullptr");
15
- }
16
- log_hook = Napi::Persistent(new_log_hook);
17
- }
18
-
19
- LoggerNapi::~LoggerNapi() {
20
- auto old_log_hook = std::exchange(log_hook, Napi::FunctionReference());
21
- if (!old_log_hook.IsEmpty()) {
22
- old_log_hook.Unref();
23
- }
24
- }
25
-
26
- void LoggerNapi::set_log_hook(Napi::Function new_log_hook) {
27
- if (unlikely(new_log_hook.IsUndefined() || new_log_hook.IsEmpty())) {
28
- NapiUtils::ThrowException(env,
29
- system_name + ": new_log_hook cannot be empty");
30
- }
31
- auto old_log_hook = std::exchange(log_hook, Napi::Persistent(new_log_hook));
32
- if (!old_log_hook.IsEmpty()) {
33
- old_log_hook.Unref();
34
- }
35
- }
36
-
37
- void LoggerNapi::call_log_hook(int level, const std::string &message) const {
38
- if (unlikely(log_hook.IsEmpty())) {
39
- NapiUtils::ThrowException(env, system_name + ": log_hook cannot be empty");
40
- }
41
- Napi::HandleScope scope(env);
42
- Napi::Function log_hook_function = log_hook.Value();
43
- log_hook_function.Call(
44
- {Napi::Number::New(env, level),
45
- Napi::String::New(env, system_name + ": " + message)});
46
- }
47
-
48
- void LoggerNapi::debug_log(const char *function_name,
49
- const char *message) const {
50
- if (unlikely(verbose_flag)) {
51
- if (unlikely(log_hook.IsEmpty())) {
52
- StdErrLogger::debug_log(function_name, message);
53
- } else {
54
- call_log_hook(posix_log_level_debug,
55
- system_name + ": " + function_name + ": " + message);
56
- }
57
- }
58
- }
59
-
60
- void LoggerNapi::debug_log(const char *function_name,
61
- const std::string &message) const {
62
- if (unlikely(verbose_flag)) {
63
- if (unlikely(log_hook.IsEmpty())) {
64
- StdErrLogger::debug_log(function_name, message);
65
- } else {
66
- call_log_hook(posix_log_level_debug,
67
- system_name + ": " + function_name + ": " + message);
68
- }
69
- }
70
- }
71
-
72
- void LoggerNapi::debug_log_alloca(const char *function_name,
73
- const char *variable_name,
74
- size_t length) const {
75
- if (unlikely(verbose_flag)) {
76
- if (unlikely(log_hook.IsEmpty())) {
77
- StdErrLogger::debug_log_alloca(function_name, variable_name, length);
78
- } else {
79
- call_log_hook(posix_log_level_debug,
80
- system_name + ": " + function_name + ": Calling alloca(" +
81
- std::to_string(length) + ") (stack) for " +
82
- variable_name);
83
- }
84
- }
85
- }
86
-
87
- void LoggerNapi::debug_log_new(const char *function_name,
88
- const char *variable_name, size_t length) const {
89
- if (unlikely(verbose_flag)) {
90
- if (unlikely(log_hook.IsEmpty())) {
91
- StdErrLogger::debug_log_new(function_name, variable_name, length);
92
- } else {
93
- call_log_hook(posix_log_level_debug, system_name + ": " + function_name +
94
- ": Calling new[" +
95
- std::to_string(length) +
96
- "] (heap) for " + variable_name);
97
- }
98
- }
99
- }
100
-
101
- void LoggerNapi::error_log(const char *function_name,
102
- const char *message) const {
103
- if (likely(!log_hook.IsEmpty())) {
104
- call_log_hook(posix_log_level_error,
105
- system_name + ": " + function_name + ": " + message);
106
- }
107
- }
108
-
109
- void LoggerNapi::error_log(const char *function_name,
110
- const std::string &message) const {
111
- if (likely(!log_hook.IsEmpty())) {
112
- call_log_hook(posix_log_level_error,
113
- system_name + ": " + function_name + ": " + message);
114
- }
115
- }
@@ -1,41 +0,0 @@
1
- #ifndef LOGGING_NAPI_H
2
- #define LOGGING_NAPI_H
3
-
4
- #include "hints.h"
5
- #include "logging.h"
6
- #include "logging_stderr.h"
7
- #include <napi.h>
8
-
9
- class LoggerNapi : public StdErrLogger {
10
- public:
11
- LoggerNapi(Napi::Env &env, const std::string &system_name);
12
-
13
- [[maybe_unused]] explicit LoggerNapi(Napi::Env &env,
14
- const std::string &system_name,
15
- Napi::Function new_log_hook);
16
- ~LoggerNapi();
17
-
18
- void set_log_hook(Napi::Function new_log_hook);
19
-
20
- void debug_log(const char *function_name, const char *message) const override;
21
- void debug_log(const char *function_name,
22
- const std::string &message) const override;
23
- void debug_log_alloca(const char *function_name, const char *variable_name,
24
- size_t length) const override;
25
-
26
- void debug_log_new(const char *function_name, const char *variable_name,
27
- size_t length) const override;
28
-
29
- void error_log(const char *function_name, const char *message) const override;
30
- void error_log(const char *function_name,
31
- const std::string &message) const override;
32
-
33
- private:
34
- void call_log_hook(int level, const std::string &message) const;
35
- Napi::FunctionReference log_hook;
36
- Napi::Env env;
37
- const int posix_log_level_error = 3;
38
- const int posix_log_level_debug = 7;
39
- };
40
-
41
- #endif // LOGGING_NAPI_H
@@ -1,60 +0,0 @@
1
- #include "logging_stderr.h"
2
- #include "hints.h"
3
- #include <iostream>
4
-
5
- StdErrLogger::StdErrLogger(const std::string &system_name)
6
- : Logger(system_name) {}
7
-
8
- void StdErrLogger::debug_log(const char *function_name,
9
- const char *message) const {
10
- if (unlikely(verbose_flag)) {
11
- std::cerr << system_name << ": [DEBUG] " << function_name << ": " << message
12
- << std::endl
13
- << std::flush;
14
- }
15
- }
16
-
17
- void StdErrLogger::debug_log(const char *function_name,
18
- const std::string &message) const {
19
- if (unlikely(verbose_flag)) {
20
- std::cerr << system_name << ": [DEBUG] " << function_name << ": " << message
21
- << std::endl
22
- << std::flush;
23
- }
24
- }
25
-
26
- void StdErrLogger::debug_log_alloca(const char *function_name,
27
- const char *variable_name,
28
- size_t length) const {
29
- if (unlikely(verbose_flag)) {
30
- std::cerr << system_name << ": [DEBUG] " << function_name
31
- << ": Calling alloca(" << length << ") (stack) for "
32
- << variable_name << std::endl
33
- << std::flush;
34
- }
35
- }
36
-
37
- void StdErrLogger::debug_log_new(const char *function_name,
38
- const char *variable_name,
39
- size_t length) const {
40
- if (unlikely(verbose_flag)) {
41
- std::cerr << system_name << ": [DEBUG] " << function_name
42
- << ": Calling new[" << length << "] (heap) for " << variable_name
43
- << std::endl
44
- << std::flush;
45
- }
46
- }
47
-
48
- void StdErrLogger::error_log(const char *function_name,
49
- const char *message) const {
50
- std::cerr << system_name << ": [ERROR] " << function_name << ": " << message
51
- << std::endl
52
- << std::flush;
53
- }
54
-
55
- void StdErrLogger::error_log(const char *function_name,
56
- const std::string &message) const {
57
- std::cerr << system_name << ": [ERROR] " << function_name << ": " << message
58
- << std::endl
59
- << std::flush;
60
- }
@@ -1,23 +0,0 @@
1
- #ifndef STDERR_LOGGER_H
2
- #define STDERR_LOGGER_H
3
-
4
- #include "logging.h"
5
-
6
- class StdErrLogger : public Logger {
7
- public:
8
- StdErrLogger() = delete;
9
- explicit StdErrLogger(const std::string &system_name);
10
-
11
- void debug_log(const char *function_name, const char *message) const override;
12
- void debug_log(const char *function_name,
13
- const std::string &message) const override;
14
- void debug_log_alloca(const char *function_name, const char *variable_name,
15
- size_t length) const override;
16
- void debug_log_new(const char *function_name, const char *variable_name,
17
- size_t length) const override;
18
- void error_log(const char *function_name, const char *message) const override;
19
- void error_log(const char *function_name,
20
- const std::string &message) const override;
21
- };
22
-
23
- #endif // STDERR_LOGGER_H
package/src/napi_utils.h DELETED
@@ -1,164 +0,0 @@
1
- #ifndef NAPI_UTILS_H
2
- #define NAPI_UTILS_H
3
-
4
- #include "hints.h"
5
- #include <napi.h>
6
- #include <stdexcept>
7
- #include <string>
8
-
9
- class NapiUtils {
10
- public:
11
- // This gets the length of the utf-8 string without allocating / copying
12
- static size_t GetUtf8StringLength(const Napi::Env &env,
13
- const Napi::String &napiString) {
14
- size_t result;
15
- napi_status status =
16
- napi_get_value_string_utf8(env, napiString, nullptr, 0, &result);
17
- if (status != napi_ok) {
18
- ThrowException(env, "Failed to get UTF-8 string length. Status: " +
19
- std::to_string(status));
20
- }
21
- return result;
22
- }
23
-
24
- [[noreturn]] static void ThrowException(const Napi::Env &env,
25
- const std::string &message) {
26
- // throw std::runtime_error(message);
27
- throw Napi::Error::New(env, message);
28
- }
29
-
30
- static void AsJsonObjectAndString(const Napi::Env &env,
31
- const Napi::Value &value,
32
- Napi::String &jsonString,
33
- Napi::Object &jsonObject) {
34
- auto json = env.Global().Get("JSON").As<Napi::Object>();
35
- auto jsonStringify = json.Get("stringify").As<Napi::Function>();
36
- auto jsonParse = json.Get("parse").As<Napi::Function>();
37
- if (unlikely(value.IsUndefined())) {
38
- ThrowException(env, "Input value is undefined");
39
- } else if (value.IsString()) {
40
- // Convert string to object using JSON.parse
41
- auto str = value.As<Napi::String>();
42
- jsonString = str;
43
- jsonObject = jsonParse.Call({str}).As<Napi::Object>();
44
- } else if (value.IsObject()) {
45
- // Convert object to string using JSON.stringify
46
- auto obj = value.As<Napi::Object>();
47
- if (jsonStringify.IsUndefined()) {
48
- ThrowException(env, "jsonStringify is undefined");
49
- } else if (jsonStringify.IsEmpty()) {
50
- ThrowException(env, "jsonStringify is empty");
51
- }
52
- jsonString = jsonStringify.Call({obj}).As<Napi::String>();
53
- jsonObject = obj;
54
- } else {
55
- ThrowException(env, "Input value must be a Napi::Object or Napi::String");
56
- }
57
- }
58
-
59
- #pragma region Object Properties
60
-
61
- static void GetStringProperty(const Napi::Object &obj,
62
- const char *propertyName,
63
- Napi::String &result) {
64
- auto maybeValue = obj.Get(propertyName);
65
-
66
- if (likely(!maybeValue.IsUndefined() && !maybeValue.IsNull() &&
67
- maybeValue.IsString())) {
68
- result = maybeValue.As<Napi::String>();
69
- } else {
70
- ThrowException(obj.Env(), "Property '" + std::string(propertyName) +
71
- "' is not a Napi::String or is missing.");
72
- }
73
- }
74
-
75
- static void GetBooleanProperty(const Napi::Object &obj,
76
- const char *propertyName, bool &result,
77
- bool defaultValue = false) {
78
- auto maybeValue = obj.Get(propertyName);
79
-
80
- if (likely(!maybeValue.IsEmpty())) {
81
- Napi::Value value = maybeValue;
82
-
83
- if (value.IsBoolean()) {
84
- result = value.As<Napi::Boolean>();
85
- } else {
86
- // Coerce to boolean
87
- result = value.ToBoolean();
88
- }
89
- } else {
90
- result = defaultValue;
91
- }
92
- }
93
-
94
- #pragma endregion Object Properties
95
-
96
- #pragma region Parameter Support
97
-
98
- static void RequireParameterCount(const Napi::CallbackInfo &info,
99
- size_t expected) {
100
- if (unlikely(info.Length() != expected)) {
101
- std::string error_msg = "Expected " + std::to_string(expected) +
102
- " arguments, but got " +
103
- std::to_string(info.Length());
104
- ThrowException(info.Env(), error_msg);
105
- }
106
- }
107
-
108
- static Napi::String RequireParameterString(const Napi::Env &env,
109
- const char *func_name,
110
- Napi::Value value) {
111
- if (likely(value.IsString())) {
112
- return value.As<Napi::String>();
113
- } else if (unlikely(value.IsUndefined())) {
114
- ThrowException(env, std::string(func_name) +
115
- ": Expected String but received undefined");
116
- } else if (unlikely(value.IsNull())) {
117
- ThrowException(env, std::string(func_name) +
118
- ": Expected String but received null");
119
- } else {
120
- ThrowException(env, std::string(func_name) +
121
- ": Expected String but received unknown type");
122
- }
123
- }
124
-
125
- __attribute__((unused)) static Napi::Buffer<unsigned char>
126
- RequireParameterBuffer(const Napi::Env &env, const char *func_name,
127
- Napi::Value value) {
128
- if (likely(value.IsBuffer())) {
129
- return value.As<Napi::Buffer<unsigned char>>();
130
- } else if (unlikely(value.IsUndefined())) {
131
- ThrowException(env, std::string(func_name) +
132
- ": Expected String but received undefined");
133
- } else if (unlikely(value.IsNull())) {
134
- ThrowException(env, std::string(func_name) +
135
- ": Expected String but received null");
136
- } else {
137
- ThrowException(env, std::string(func_name) +
138
- ": Expected String but received unknown type");
139
- }
140
- }
141
-
142
- static Napi::Value RequireParameterStringOrBuffer(const Napi::Env &env,
143
- const char *func_name,
144
- Napi::Value value) {
145
- if (value.IsString()) {
146
- return value.As<Napi::String>();
147
- } else if (likely(value.IsBuffer())) {
148
- return value.As<Napi::Buffer<unsigned char>>(); // NOLINT(*-slicing)
149
- } else if (unlikely(value.IsUndefined())) {
150
- ThrowException(env, std::string(func_name) +
151
- ": Expected String but received undefined");
152
- } else if (unlikely(value.IsNull())) {
153
- ThrowException(env, std::string(func_name) +
154
- ": Expected String but received null");
155
- } else {
156
- ThrowException(env, std::string(func_name) +
157
- ": Expected String but received unknown type");
158
- }
159
- }
160
-
161
- #pragma endregion Parameter Support
162
- };
163
-
164
- #endif // NAPI_UTILS_H
@@ -1,50 +0,0 @@
1
- #ifndef SCOPED_ALLOCATE_H
2
- #define SCOPED_ALLOCATE_H
3
-
4
- #ifdef USE_SCOPED_ALLOCATE_BUFFER
5
-
6
- /*
7
- This macro allows us to allocate a buffer either on the stack or on the heap.
8
- If the requested buffer size is less than max_stack_alloc_size, we create the
9
- buffer on the stack using alloca(). Buffers allocated with alloca() are
10
- automatically freed when the function exits, since they're on the call stack.
11
- We use alloca() rather than VLAs because VLAs are limited by scope brackets
12
- rather than by the function call, making it hard to create a VLA in an if
13
- statement. If the buffer is larger than what we're willing to risk allocating
14
- on the stack, we instead allocate it on the heap and use a unique_ptr which
15
- will automatically free the allocated buffer when it goes out of scope.
16
- */
17
-
18
- #define SCOPED_ALLOCATE_BUFFER_UNIQUE_PTR(buffer, buffer_size, unique_ptr, \
19
- max_stack_alloc_size, function_name) \
20
- do { \
21
- buffer = nullptr; \
22
- if (buffer_size < max_stack_alloc_size) { \
23
- /* If the buffer is small enough, allocate it on the stack */ \
24
- buffer = (char *)alloca(buffer_size); \
25
- if (unlikely(buffer == nullptr)) { \
26
- throw std::runtime_error(std::string(function_name) + " alloca(" + \
27
- std::to_string(buffer_size) + \
28
- ") returned null"); \
29
- } \
30
- } else { \
31
- /* Otherwise, allocate it on the heap */ \
32
- buffer = new (std::nothrow) char[buffer_size]; \
33
- if (unlikely(buffer == nullptr)) { \
34
- throw std::runtime_error(std::string(function_name) + " new[" + \
35
- std::to_string(buffer_size) + \
36
- "] returned null"); \
37
- } \
38
- unique_ptr.reset(buffer); \
39
- } \
40
- } while (0)
41
-
42
- #define SCOPED_ALLOCATE_BUFFER(buffer, buffer_size, max_stack_alloc_size, \
43
- function_name) \
44
- std::unique_ptr<char[]> buffer##_unique_ptr; \
45
- SCOPED_ALLOCATE_BUFFER_UNIQUE_PTR(buffer, buffer_size, buffer##_unique_ptr, \
46
- max_stack_alloc_size, function_name)
47
-
48
- #endif // USE_SCOPED_ALLOCATE_BUFFER
49
-
50
- #endif // SCOPED_ALLOCATE_H