@nativue/core 0.1.0 → 0.3.0

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.
Files changed (42) hide show
  1. package/Package.swift +26 -2
  2. package/android/build.gradle.kts +2 -2
  3. package/android/consumer-rules.pro +3 -0
  4. package/android/nativue.settings.gradle.kts +56 -0
  5. package/android/src/main/java/dev/nativue/Bridge.kt +51 -7
  6. package/android/src/main/java/dev/nativue/ModalHost.kt +2 -2
  7. package/android/src/main/java/dev/nativue/NativueActivity.kt +8 -0
  8. package/android/src/main/java/dev/nativue/Navigation.kt +32 -24
  9. package/android/src/main/java/dev/nativue/Plugins.kt +75 -0
  10. package/android/src/main/java/dev/nativue/Style.kt +5 -1
  11. package/android/src/main/java/dev/nativue/Theme.kt +61 -0
  12. package/android/src/main/java/dev/nativue/Views.kt +14 -2
  13. package/cli/config.d.mts +19 -0
  14. package/cli/config.mjs +4 -0
  15. package/cli/link.mjs +129 -0
  16. package/cli/nativue.mjs +67 -20
  17. package/cli/symbolicate.mjs +3 -5
  18. package/ios/Bridge.swift +207 -102
  19. package/ios/JavaScriptRuntime/JavaScriptRuntime.mm +231 -0
  20. package/ios/JavaScriptRuntime/include/NativueJavaScriptRuntime.h +29 -0
  21. package/ios/JavaScriptRuntime/vendor/hermes/LICENSE +21 -0
  22. package/ios/JavaScriptRuntime/vendor/hermes/Public/CrashManager.h +107 -0
  23. package/ios/JavaScriptRuntime/vendor/hermes/Public/CtorConfig.h +145 -0
  24. package/ios/JavaScriptRuntime/vendor/hermes/Public/GCConfig.h +211 -0
  25. package/ios/JavaScriptRuntime/vendor/hermes/Public/GCTripwireContext.h +40 -0
  26. package/ios/JavaScriptRuntime/vendor/hermes/Public/HermesExport.h +14 -0
  27. package/ios/JavaScriptRuntime/vendor/hermes/Public/RuntimeConfig.h +141 -0
  28. package/ios/JavaScriptRuntime/vendor/hermes/Public/SamplingProfiler.h +270 -0
  29. package/ios/JavaScriptRuntime/vendor/hermes/hermes.h +242 -0
  30. package/ios/NativeNode.swift +1 -0
  31. package/ios/NativueView.swift +10 -3
  32. package/ios/Plugins.swift +45 -0
  33. package/ios/Style.swift +14 -1
  34. package/package.json +7 -2
  35. package/src/appearance.ts +49 -0
  36. package/src/components.ts +3 -1
  37. package/src/entry.ts +2 -1
  38. package/src/globals.ts +3 -0
  39. package/src/native-functions.ts +41 -0
  40. package/src/renderer.ts +58 -1
  41. package/src/types.ts +2 -0
  42. package/src/virtual-modules.d.ts +2 -0
@@ -0,0 +1,231 @@
1
+ // The bridge between Bridge.swift and Hermes, through JSI: the iOS side of android/src/main/cpp/bridge.cpp.
2
+ // Values cross as Foundation objects. Every call happens on the JS queue.
3
+ #import "NativueJavaScriptRuntime.h"
4
+
5
+ #include <hermes/hermes.h>
6
+ #include <jsi/jsi.h>
7
+
8
+ #include <functional>
9
+ #include <memory>
10
+ #include <string>
11
+ #include <vector>
12
+
13
+ using namespace facebook;
14
+
15
+ namespace {
16
+
17
+ // ---------- Strings (UTF-16 both ways, so emoji survive) ----------
18
+
19
+ NSString *toObjectiveCString(jsi::Runtime &runtime, const jsi::String &string) {
20
+ std::u16string text = string.utf16(runtime);
21
+
22
+ return [NSString stringWithCharacters:reinterpret_cast<const unichar *>(text.data()) length:text.size()];
23
+ }
24
+
25
+ jsi::String toJavaScriptString(jsi::Runtime &runtime, NSString *string) {
26
+ std::vector<unichar> characters(string.length);
27
+ [string getCharacters:characters.data() range:NSMakeRange(0, string.length)];
28
+
29
+ return jsi::String::createFromUtf16(
30
+ runtime, reinterpret_cast<const char16_t *>(characters.data()), characters.size());
31
+ }
32
+
33
+ std::string toStdString(NSString *string) {
34
+ return string.UTF8String ?: "";
35
+ }
36
+
37
+ NSString *toObjectiveCString(const std::string &string) {
38
+ return [NSString stringWithUTF8String:string.c_str()] ?: @"";
39
+ }
40
+
41
+ // ---------- Values ----------
42
+
43
+ id toObjectiveC(jsi::Runtime &runtime, const jsi::Value &value);
44
+
45
+ NSArray *arrayToObjectiveC(jsi::Runtime &runtime, const jsi::Array &array) {
46
+ size_t length = array.size(runtime);
47
+ NSMutableArray *elements = [NSMutableArray arrayWithCapacity:length];
48
+
49
+ for (size_t index = 0; index < length; index++) {
50
+ [elements addObject:toObjectiveC(runtime, array.getValueAtIndex(runtime, index))];
51
+ }
52
+
53
+ return elements;
54
+ }
55
+
56
+ NSDictionary *objectToObjectiveC(jsi::Runtime &runtime, const jsi::Object &object) {
57
+ jsi::Array names = object.getPropertyNames(runtime);
58
+ size_t count = names.size(runtime);
59
+ NSMutableDictionary *properties = [NSMutableDictionary dictionaryWithCapacity:count];
60
+
61
+ for (size_t index = 0; index < count; index++) {
62
+ jsi::String name = names.getValueAtIndex(runtime, index).getString(runtime);
63
+ jsi::Value value = object.getProperty(runtime, jsi::PropNameID::forString(runtime, name));
64
+ if (value.isUndefined()) continue; // `{ badge: undefined }` reads as no badge, not as null
65
+
66
+ properties[toObjectiveCString(runtime, name)] = toObjectiveC(runtime, value);
67
+ }
68
+
69
+ return properties;
70
+ }
71
+
72
+ // null, undefined, functions and other non-data values become NSNull.
73
+ id toObjectiveC(jsi::Runtime &runtime, const jsi::Value &value) {
74
+ if (value.isBool()) return value.getBool() ? @YES : @NO;
75
+ if (value.isNumber()) return @(value.getNumber());
76
+ if (value.isString()) return toObjectiveCString(runtime, value.getString(runtime));
77
+ if (!value.isObject()) return NSNull.null;
78
+
79
+ jsi::Object object = value.getObject(runtime);
80
+ if (object.isFunction(runtime)) return NSNull.null;
81
+ if (object.isArray(runtime)) return arrayToObjectiveC(runtime, object.getArray(runtime));
82
+
83
+ return objectToObjectiveC(runtime, object);
84
+ }
85
+
86
+ jsi::Value toJavaScript(jsi::Runtime &runtime, id value);
87
+
88
+ jsi::Value arrayToJavaScript(jsi::Runtime &runtime, NSArray *elements) {
89
+ jsi::Array array(runtime, elements.count);
90
+
91
+ for (NSUInteger index = 0; index < elements.count; index++) {
92
+ array.setValueAtIndex(runtime, index, toJavaScript(runtime, elements[index]));
93
+ }
94
+
95
+ return array;
96
+ }
97
+
98
+ // Keys that aren't strings (HTTP header fields can be anything) use their description.
99
+ jsi::Value dictionaryToJavaScript(jsi::Runtime &runtime, NSDictionary *properties) {
100
+ jsi::Object object(runtime);
101
+
102
+ for (id key in properties) {
103
+ object.setProperty(runtime, toJavaScriptString(runtime, [key description]), toJavaScript(runtime, properties[key]));
104
+ }
105
+
106
+ return object;
107
+ }
108
+
109
+ jsi::Value toJavaScript(jsi::Runtime &runtime, id value) {
110
+ if (value == nil || value == NSNull.null) return jsi::Value::null();
111
+ if ([value isKindOfClass:NSNumber.class]) {
112
+ if (CFGetTypeID((__bridge CFTypeRef)value) == CFBooleanGetTypeID()) return jsi::Value([value boolValue]);
113
+
114
+ return jsi::Value([value doubleValue]);
115
+ }
116
+ if ([value isKindOfClass:NSString.class]) return toJavaScriptString(runtime, value);
117
+ if ([value isKindOfClass:NSArray.class]) return arrayToJavaScript(runtime, value);
118
+ if ([value isKindOfClass:NSDictionary.class]) return dictionaryToJavaScript(runtime, value);
119
+
120
+ return jsi::Value::undefined();
121
+ }
122
+
123
+ // ---------- Bundle ----------
124
+
125
+ // Hermes runs bytecode straight from the buffer, so it keeps the data alive as long as it needs it.
126
+ class DataBuffer : public jsi::Buffer {
127
+ public:
128
+ explicit DataBuffer(NSData *data) : data_(data) {}
129
+
130
+ size_t size() const override {
131
+ return data_.length;
132
+ }
133
+
134
+ const uint8_t *data() const override {
135
+ return static_cast<const uint8_t *>(data_.bytes);
136
+ }
137
+
138
+ private:
139
+ NSData *data_;
140
+ };
141
+
142
+ } // namespace
143
+
144
+ @implementation NativueJavaScriptRuntime {
145
+ std::unique_ptr<facebook::hermes::HermesRuntime> _hermes;
146
+ void (^_onError)(NSString *, NSString *);
147
+ }
148
+
149
+ - (instancetype)initWithFunctions:(NSDictionary<NSString *, id> *)functions
150
+ onError:(void (^)(NSString *, NSString *))onError {
151
+ self = [super init];
152
+ if (self == nil) return nil;
153
+
154
+ auto config = ::hermes::vm::RuntimeConfig::Builder().withMicrotaskQueue(true).build();
155
+ _hermes = facebook::hermes::makeHermesRuntime(config);
156
+ _onError = onError;
157
+ [self installNativeModule:functions];
158
+
159
+ return self;
160
+ }
161
+
162
+ - (void)installNativeModule:(NSDictionary<NSString *, id> *)functions {
163
+ jsi::Runtime &runtime = *_hermes;
164
+ jsi::Object nativeModule(runtime);
165
+
166
+ for (NSString *name in functions) {
167
+ NativueNativeFunction function = functions[name];
168
+ auto call = [function](jsi::Runtime &runtime, const jsi::Value &, const jsi::Value *arguments, size_t count) {
169
+ NSMutableArray *objectiveCArguments = [NSMutableArray arrayWithCapacity:count];
170
+ for (size_t position = 0; position < count; position++) {
171
+ [objectiveCArguments addObject:toObjectiveC(runtime, arguments[position])];
172
+ }
173
+
174
+ return toJavaScript(runtime, function(objectiveCArguments));
175
+ };
176
+
177
+ std::string functionName = toStdString(name);
178
+ nativeModule.setProperty(
179
+ runtime,
180
+ functionName.c_str(),
181
+ jsi::Function::createFromHostFunction(runtime, jsi::PropNameID::forUtf8(runtime, functionName), 0, call));
182
+ }
183
+
184
+ runtime.global().setProperty(runtime, "__native", nativeModule);
185
+ }
186
+
187
+ - (void)evaluate:(NSData *)bundle sourceURL:(NSString *)sourceURL {
188
+ auto buffer = std::make_shared<DataBuffer>(bundle);
189
+ std::string url = toStdString(sourceURL);
190
+
191
+ [self runReportingErrors:[&] { _hermes->evaluateJavaScript(buffer, url); }];
192
+ }
193
+
194
+ - (void)call:(NSString *)functionName arguments:(NSArray *)arguments {
195
+ jsi::Runtime &runtime = *_hermes;
196
+ std::string name = toStdString(functionName);
197
+
198
+ [self runReportingErrors:[&] {
199
+ jsi::Value function = runtime.global().getProperty(runtime, name.c_str());
200
+ if (!function.isObject() || !function.getObject(runtime).isFunction(runtime)) return;
201
+
202
+ std::vector<jsi::Value> values;
203
+ for (id argument in arguments) values.push_back(toJavaScript(runtime, argument));
204
+
205
+ function.getObject(runtime).getFunction(runtime).call(
206
+ runtime, static_cast<const jsi::Value *>(values.data()), values.size());
207
+ }];
208
+ }
209
+
210
+ // Runs `work` and the promise jobs it queued, reporting errors instead of throwing them. A failing job
211
+ // doesn't stop the others.
212
+ - (void)runReportingErrors:(const std::function<void()> &)work {
213
+ auto attempt = [self](const std::function<void()> &step) {
214
+ try {
215
+ step();
216
+ return true;
217
+ } catch (const jsi::JSError &error) {
218
+ _onError(toObjectiveCString(error.getMessage()), toObjectiveCString(error.getStack()));
219
+ } catch (const std::exception &error) {
220
+ _onError(toObjectiveCString(error.what()), @"");
221
+ }
222
+
223
+ return false;
224
+ };
225
+
226
+ attempt(work);
227
+ while (!attempt([self] { _hermes->drainMicrotasks(); })) {
228
+ }
229
+ }
230
+
231
+ @end
@@ -0,0 +1,29 @@
1
+ #import <Foundation/Foundation.h>
2
+
3
+ NS_ASSUME_NONNULL_BEGIN
4
+
5
+ typedef id _Nullable (^NativueNativeFunction)(NSArray *arguments);
6
+
7
+ /**
8
+ * A Hermes runtime (JavaScriptRuntime.mm) with `functions` (NativueNativeFunction blocks: Swift closures
9
+ * converted with `as @convention(block)`) installed as `globalThis.__native`. Values
10
+ * cross as NSNull, NSNumber (booleans as kCFBooleanTrue/False), NSString, NSArray and NSDictionary. Use
11
+ * it from one thread at a time.
12
+ */
13
+ NS_SWIFT_NAME(JavaScriptRuntime)
14
+ @interface NativueJavaScriptRuntime : NSObject
15
+
16
+ - (instancetype)initWithFunctions:(NSDictionary<NSString *, id> *)functions
17
+ onError:(void (^)(NSString *message, NSString *stack))onError NS_DESIGNATED_INITIALIZER;
18
+
19
+ - (instancetype)init NS_UNAVAILABLE;
20
+
21
+ /// JavaScript (UTF-8) or Hermes bytecode: Hermes tells them apart.
22
+ - (void)evaluate:(NSData *)bundle sourceURL:(NSString *)sourceURL;
23
+
24
+ /// Calls a global function, if the bundle defined it.
25
+ - (void)call:(NSString *)functionName arguments:(NSArray *)arguments;
26
+
27
+ @end
28
+
29
+ NS_ASSUME_NONNULL_END
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) Meta Platforms, Inc. and affiliates.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,107 @@
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ #ifndef HERMES_PUBLIC_CRASHMANAGER_H
9
+ #define HERMES_PUBLIC_CRASHMANAGER_H
10
+
11
+ #include <hermes/Public/HermesExport.h>
12
+
13
+ #include <cstddef>
14
+ #include <functional>
15
+
16
+ namespace hermes {
17
+ namespace vm {
18
+
19
+ /// A CrashManager provides functions that determine what memory and data is
20
+ /// included in dumps in case of crashes.
21
+ class HERMES_EXPORT CrashManager {
22
+ public:
23
+ /// CallbackKey is the type of an identifier for a callback supplied to the
24
+ /// CrashManager.
25
+ using CallbackKey = int;
26
+ /// Type for the callback function invoked on crash. The fd supplied is a raw
27
+ /// file stream an implementation should write a JSON object to.
28
+ using CallbackFunc = std::function<void(int fd)>;
29
+
30
+ /// Registers some memory to be included in any crash dump that occurs.
31
+ /// \param mem A pointer to allocated memory. It must be unregistered
32
+ /// before being freed.
33
+ /// \param length The number of bytes the memory controls.
34
+ virtual void registerMemory(void *mem, size_t length) = 0;
35
+
36
+ /// Unregisters some memory from being included in any crash dump that occurs.
37
+ virtual void unregisterMemory(void *mem) = 0;
38
+
39
+ /// Registers custom data to be included in any crash dump that occurs.
40
+ /// Calling \c setCustomData on the same key twice will overwrite the previous
41
+ /// value.
42
+ /// \param key A tag to look for in the custom data output. Distinguishes
43
+ /// between multiple values.
44
+ /// \param val The value to store for the given key.
45
+ virtual void setCustomData(const char *key, const char *val) = 0;
46
+
47
+ /// If the given \p key has an associated custom data string, remove the
48
+ /// association. If the key hasn't been set before, is a no-op.
49
+ virtual void removeCustomData(const char *key) = 0;
50
+
51
+ /// Same as \c setCustomData, except it is only set for the current thread.
52
+ virtual void setContextualCustomData(const char *key, const char *val) = 0;
53
+
54
+ /// Same as \c removeCustomData, except it is for keys set with \c
55
+ /// setContextualCustomData.
56
+ virtual void removeContextualCustomData(const char *key) = 0;
57
+
58
+ /// Registers a function to be called after a crash has occurred. This
59
+ /// function can examine memory and serialize this to a JSON output stream.
60
+ /// Implmentations decide where the stream is routed to.
61
+ /// \param callback A function to called after a crash.
62
+ /// \return A CallbackKey representing the function you provided. Pass this
63
+ /// key into unregisterCallback when it that callback is no longer needed.
64
+ virtual CallbackKey registerCallback(CallbackFunc callback) = 0;
65
+
66
+ /// Unregisters a previously registered callback. After this function returns,
67
+ /// the previously registered function will not be executed by this
68
+ /// CrashManager during a crash.
69
+ virtual void unregisterCallback(CallbackKey key) = 0;
70
+
71
+ /// the heap information.
72
+ struct HeapInformation {
73
+ /// The amount of memory that is currently in use
74
+ size_t used_{0};
75
+ /// The amount of memory that can currently be allocated
76
+ /// before a full GC is triggered.
77
+ size_t size_{0};
78
+ };
79
+
80
+ /// Record the heap information.
81
+ /// \param heapInfo The current heap information
82
+ virtual void setHeapInfo(const HeapInformation &heapInfo) = 0;
83
+
84
+ virtual ~CrashManager();
85
+ };
86
+
87
+ /// A CrashManager that does nothing.
88
+ class HERMES_EXPORT NopCrashManager final : public CrashManager {
89
+ public:
90
+ void registerMemory(void *, size_t) override {}
91
+ void unregisterMemory(void *) override {}
92
+ void setCustomData(const char *, const char *) override {}
93
+ void removeCustomData(const char *) override {}
94
+ void setContextualCustomData(const char *, const char *) override {}
95
+ void removeContextualCustomData(const char *) override {}
96
+ CallbackKey registerCallback(CallbackFunc /*callback*/) override {
97
+ return 0;
98
+ }
99
+ void unregisterCallback(CallbackKey /*key*/) override {}
100
+ void setHeapInfo(const HeapInformation & /*heapInfo*/) override {}
101
+
102
+ ~NopCrashManager() override;
103
+ };
104
+
105
+ } // namespace vm
106
+ } // namespace hermes
107
+ #endif
@@ -0,0 +1,145 @@
1
+ /*
2
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ #pragma once
9
+
10
+ #include <utility>
11
+
12
+ /// Defines a new class, called \p NAME representing a constructor config, and
13
+ /// an associated builder class.
14
+ ///
15
+ /// The fields of the class (along with their types and default values) are
16
+ /// encoded in the \p FIELDS parameter, and any logic to be run whilst building
17
+ /// the config can be passed as a code block in \p BUILD_BODY.
18
+ ///
19
+ /// Example:
20
+ ///
21
+ /// Suppose we wish to define a configuration class called Foo, with the
22
+ /// following fields and default values:
23
+ ///
24
+ /// int A = 0;
25
+ /// int B = 42;
26
+ /// std::string C = "hello";
27
+ ///
28
+ /// Such that the value in A is at most the length of \c C.
29
+ ///
30
+ /// We can do so with the following declaration:
31
+ ///
32
+ /// " #define FIELDS(F) \ "
33
+ /// " F(int, A) \ "
34
+ /// " F(int, B, 42) \ "
35
+ /// " F(std::string, C, "hello") "
36
+ /// " "
37
+ /// " _HERMES_CTORCONFIG_STRUCT(Foo, FIELDS, { "
38
+ /// " A_ = std::min(A_, C_.length()); "
39
+ /// " }); "
40
+ ///
41
+ /// N.B.
42
+ /// - The definition of A does not mention any value -- meaning it is
43
+ /// default initialised.
44
+ /// - References to the fields in the validation logic have a trailling
45
+ /// underscore.
46
+ ///
47
+ #define _HERMES_CTORCONFIG_STRUCT(NAME, FIELDS, BUILD_BODY) \
48
+ class NAME { \
49
+ FIELDS(_HERMES_CTORCONFIG_FIELD_DECL) \
50
+ \
51
+ public: \
52
+ class Builder; \
53
+ friend Builder; \
54
+ FIELDS(_HERMES_CTORCONFIG_GETTER) \
55
+ \
56
+ /* returns a Builder that starts with the current config. */ \
57
+ inline Builder rebuild() const; \
58
+ \
59
+ private: \
60
+ inline void doBuild(const Builder &builder); \
61
+ }; \
62
+ \
63
+ class NAME::Builder { \
64
+ NAME config_; \
65
+ \
66
+ FIELDS(_HERMES_CTORCONFIG_FIELD_EXPLICIT_BOOL_DECL) \
67
+ \
68
+ public: \
69
+ Builder() = default; \
70
+ \
71
+ explicit Builder(const NAME &config) : config_(config) {} \
72
+ \
73
+ inline const NAME build() { \
74
+ config_.doBuild(*this); \
75
+ return config_; \
76
+ } \
77
+ \
78
+ /* The explicitly set fields of \p newconfig update \
79
+ * the corresponding fields of \p this. */ \
80
+ inline Builder update(const NAME::Builder &newConfig); \
81
+ \
82
+ FIELDS(_HERMES_CTORCONFIG_SETTER) \
83
+ FIELDS(_HERMES_CTORCONFIG_FIELD_EXPLICIT_BOOL_ACCESSOR) \
84
+ }; \
85
+ \
86
+ NAME::Builder NAME::rebuild() const { \
87
+ return Builder(*this); \
88
+ } \
89
+ \
90
+ NAME::Builder NAME::Builder::update(const NAME::Builder &newConfig) { \
91
+ FIELDS(_HERMES_CTORCONFIG_UPDATE) \
92
+ return *this; \
93
+ } \
94
+ \
95
+ void NAME::doBuild(const NAME::Builder &builder) { \
96
+ (void)builder; \
97
+ BUILD_BODY \
98
+ }
99
+
100
+ /// Helper Macros
101
+
102
+ #define _HERMES_CTORCONFIG_FIELD_DECL(CX, TYPE, NAME, ...) \
103
+ TYPE NAME##_{__VA_ARGS__};
104
+
105
+ /// This ignores the first and trailing arguments, and defines a member
106
+ /// indicating whether field NAME was set explicitly.
107
+ #define _HERMES_CTORCONFIG_FIELD_EXPLICIT_BOOL_DECL(CX, TYPE, NAME, ...) \
108
+ bool NAME##Explicit_{false};
109
+
110
+ /// This defines an accessor for the "Explicit_" fields defined above.
111
+ #define _HERMES_CTORCONFIG_FIELD_EXPLICIT_BOOL_ACCESSOR(CX, TYPE, NAME, ...) \
112
+ bool has##NAME() const { \
113
+ return NAME##Explicit_; \
114
+ }
115
+
116
+ /// Placeholder token for fields whose defaults are not constexpr, to make the
117
+ /// listings more readable.
118
+ #define HERMES_NON_CONSTEXPR
119
+
120
+ #define _HERMES_CTORCONFIG_GETTER(CX, TYPE, NAME, ...) \
121
+ inline TYPE get##NAME() const { \
122
+ return NAME##_; \
123
+ } \
124
+ static CX TYPE getDefault##NAME() { \
125
+ /* Instead of parens around TYPE (non-standard) */ \
126
+ using TypeAsSingleToken = TYPE; \
127
+ return TypeAsSingleToken{__VA_ARGS__}; \
128
+ }
129
+
130
+ #define _HERMES_CTORCONFIG_SETTER(CX, TYPE, NAME, ...) \
131
+ inline auto with##NAME(TYPE NAME) -> decltype(*this) { \
132
+ config_.NAME##_ = std::move(NAME); \
133
+ NAME##Explicit_ = true; \
134
+ return *this; \
135
+ }
136
+
137
+ #define _HERMES_CTORCONFIG_BUILDER_GETTER(CX, TYPE, NAME, ...) \
138
+ TYPE get##NAME() const { \
139
+ return config_.NAME##_; \
140
+ }
141
+
142
+ #define _HERMES_CTORCONFIG_UPDATE(CX, TYPE, NAME, ...) \
143
+ if (newConfig.has##NAME()) { \
144
+ with##NAME(newConfig.config_.get##NAME()); \
145
+ }