Skip to content

Commit 62d843f

Browse files
committed
refactor(android): unify static hermes backend
Switches Android Hermes over to the same Static Hermes header/library surface used by Apple and keeps SHERMES only as a compatibility selector. This absorbs the earlier transient Hermes adapter/header alignment work so reviewers see one Hermes unification step.
1 parent 3601a86 commit 62d843f

159 files changed

Lines changed: 2529 additions & 16903 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

NativeScript/napi/hermes/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Hermes Node-API adapter
2+
=======================
3+
4+
`include/` is the single vendored Static Hermes header surface used by both
5+
Apple and Android builds. It comes from `DjDeveloperr/build-hermes` and must
6+
stay in sync with the Hermes binaries under `Frameworks/` and
7+
`platforms/android/test-app/runtime/src/main/libs/hermes/`.
8+
9+
Android still accepts the historical `SHERMES` engine selector so existing
10+
test/build scripts keep working, but it is now only an alias for `HERMES`.
11+
Both selectors compile the same adapter and link the same Static Hermes
12+
artifact.

NativeScript/napi/hermes/include/hermes/AsyncDebuggerAPI.h

Lines changed: 23 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
#ifndef HERMES_ASYNCDEBUGGERAPI_H
9-
#define HERMES_ASYNCDEBUGGERAPI_H
8+
#pragma once
109

1110
#ifdef HERMES_ENABLE_DEBUGGER
1211

@@ -68,8 +67,6 @@ using DebuggerEventCallback = std::function<void(
6867
HermesRuntime &runtime,
6968
AsyncDebuggerAPI &asyncDebugger,
7069
DebuggerEventType event)>;
71-
using DebuggerEventCallbackID = uint32_t;
72-
constexpr const uint32_t kInvalidDebuggerEventCallbackID = 0;
7370
using InterruptCallback = std::function<void(HermesRuntime &runtime)>;
7471
using EvalCompleteCallback = std::function<
7572
void(HermesRuntime &runtime, const debugger::EvalResult &result)>;
@@ -82,36 +79,34 @@ using EvalCompleteCallback = std::function<
8279
/// functions that are safe to call on any thread. All other functions must be
8380
/// called on the runtime thread.
8481
class HERMES_EXPORT AsyncDebuggerAPI : private debugger::EventObserver {
85-
/// Hide the constructor so users can only construct via static create
86-
/// methods.
87-
AsyncDebuggerAPI(HermesRuntime &runtime);
88-
8982
public:
90-
/// Creates an AsyncDebuggerAPI for use with the provided HermesRuntime. This
91-
/// should be called and created at the same time as creating HermesRuntime.
92-
static std::unique_ptr<AsyncDebuggerAPI> create(HermesRuntime &runtime);
83+
/// Constructs an AsyncDebuggerAPI for use with the provided HermesRuntime.
84+
/// This should be called and created at the same time as creating
85+
/// HermesRuntime.
86+
explicit AsyncDebuggerAPI(HermesRuntime &runtime);
9387

9488
/// Must be destroyed on the runtime thread or when you're sure nothing is
9589
/// interacting with the runtime. Must be destroyed before destroying
9690
/// HermesRuntime.
9791
~AsyncDebuggerAPI() override;
9892

99-
/// Add a callback function to invoke when the runtime pauses due to various
93+
/// Set a callback function to invoke when the runtime pauses due to various
10094
/// conditions such as hitting a "debugger;" statement. Can be called from any
101-
/// thread. If there are no DebuggerEventCallback, then any reason that might
95+
/// thread. If there is no DebuggerEventCallback, then any reason that might
10296
/// trigger a pause, such as a "debugger;" statement or breakpoints, will not
103-
/// actually pause and will simply continue execution. Any caller that adds an
97+
/// actually pause and will simply continue execution. Any caller that sets an
10498
/// event callback cannot just be observing events and never call
10599
/// \p resumeFromPaused in any of its code paths. The caller must either
106100
/// expose UI enabling human action for controlling the debugger, or it must
107101
/// have programmatic logic that controls the debugger via
108102
/// \p resumeFromPaused.
109-
DebuggerEventCallbackID addDebuggerEventCallback_TS(
110-
DebuggerEventCallback callback);
103+
///
104+
/// The provided callback must be non-empty. Use \p
105+
/// clearDebuggerEventCallback_TS to clear the callback.
106+
void setDebuggerEventCallback_TS(DebuggerEventCallback callback);
111107

112-
/// Remove a previously added callback function. If there is no callback
113-
/// registered using the provided \p id, the function does nothing.
114-
void removeDebuggerEventCallback_TS(DebuggerEventCallbackID id);
108+
/// Clear the debugger event callback. Can be called from any thread.
109+
void clearDebuggerEventCallback_TS();
115110

116111
/// Whether the runtime is currently paused waiting for the next action.
117112
/// Should only be called from the runtime thread.
@@ -120,7 +115,7 @@ class HERMES_EXPORT AsyncDebuggerAPI : private debugger::EventObserver {
120115
/// Whether the runtime is currently paused for any reason (e.g. script
121116
/// parsed, running interrupts, or waiting for a command).
122117
/// Should only be called from the runtime thread.
123-
bool isPaused();
118+
bool isPaused() const;
124119

125120
/// Provide the next action to perform. Should only be called from the runtime
126121
/// thread and only if the next command is expected to be set.
@@ -145,11 +140,6 @@ class HERMES_EXPORT AsyncDebuggerAPI : private debugger::EventObserver {
145140
debugger::Command didPause(debugger::Debugger &debugger) override;
146141

147142
private:
148-
struct EventCallbackEntry {
149-
DebuggerEventCallbackID id;
150-
DebuggerEventCallback callback;
151-
};
152-
153143
/// This function infinite loops and uses \p signal_ to block the runtime
154144
/// thread. It gets woken up if new InterruptCallback is queued or if
155145
/// DebuggerEventCallback changes.
@@ -163,11 +153,8 @@ class HERMES_EXPORT AsyncDebuggerAPI : private debugger::EventObserver {
163153
/// to run all interrupts, but will stop if any interrupt sets a next command.
164154
void runInterrupts(bool ignoreNextCommand = true);
165155

166-
/// Returns the next DebuggerEventCallback to execute if any.
167-
std::optional<DebuggerEventCallback> takeNextEventCallback();
168-
169-
/// Runs every DebuggerEventCallback that has been registered.
170-
void runEventCallbacks(DebuggerEventType event);
156+
/// Runs the DebuggerEventCallback that has been registered (if any).
157+
void runEventCallback(DebuggerEventType event);
171158

172159
HermesRuntime &runtime_;
173160

@@ -186,19 +173,8 @@ class HERMES_EXPORT AsyncDebuggerAPI : private debugger::EventObserver {
186173
/// calls to didPause.
187174
bool inDidPause_ = false;
188175

189-
/// Next ID to use when adding a DebuggerEventCallback.
190-
uint32_t nextEventCallbackID_ TSA_GUARDED_BY(mutex_);
191-
192-
/// Callback functions to invoke to notify events in \p didPause. Using
193-
/// std::list which requires O(N) search when removing an element, but removal
194-
/// should be a rare event. So the choice of using std::list is to optimize
195-
/// for typical usage.
196-
std::list<EventCallbackEntry> eventCallbacks_ TSA_GUARDED_BY(mutex_){};
197-
198-
/// Iterator for eventCallbacks_. Used to traverse through the list when
199-
/// running the callbacks.
200-
std::list<EventCallbackEntry>::iterator eventCallbackIterator_
201-
TSA_GUARDED_BY(mutex_);
176+
/// The debugger event callback to invoke when the runtime pauses.
177+
DebuggerEventCallback eventCallback_ TSA_GUARDED_BY(mutex_){};
202178

203179
/// Queue of interrupt callback functions to invoke.
204180
std::queue<InterruptCallback> interruptCallbacks_ TSA_GUARDED_BY(mutex_){};
@@ -257,32 +233,25 @@ using DebuggerEventCallback = std::function<void(
257233
HermesRuntime &runtime,
258234
AsyncDebuggerAPI &asyncDebugger,
259235
DebuggerEventType event)>;
260-
using DebuggerEventCallbackID = uint32_t;
261-
constexpr const uint32_t kInvalidDebuggerEventCallbackID = 0;
262236
using InterruptCallback = std::function<void(HermesRuntime &runtime)>;
263237
using EvalCompleteCallback = std::function<
264238
void(HermesRuntime &runtime, const debugger::EvalResult &result)>;
265239

266240
class HERMES_EXPORT AsyncDebuggerAPI {
267241
public:
268-
static std::unique_ptr<AsyncDebuggerAPI> create(HermesRuntime &runtime) {
269-
return nullptr;
270-
}
242+
explicit AsyncDebuggerAPI(HermesRuntime &) {}
271243

272244
~AsyncDebuggerAPI() {}
273245

274-
DebuggerEventCallbackID addDebuggerEventCallback_TS(
275-
DebuggerEventCallback callback) {
276-
return kInvalidDebuggerEventCallbackID;
277-
}
246+
void setDebuggerEventCallback_TS(DebuggerEventCallback callback) {}
278247

279-
void removeDebuggerEventCallback_TS(DebuggerEventCallbackID id) {}
248+
void clearDebuggerEventCallback_TS() {}
280249

281250
bool isWaitingForCommand() {
282251
return false;
283252
}
284253

285-
bool isPaused() {
254+
bool isPaused() const {
286255
return false;
287256
}
288257

@@ -305,5 +274,3 @@ class HERMES_EXPORT AsyncDebuggerAPI {
305274
} // namespace facebook
306275

307276
#endif // !HERMES_ENABLE_DEBUGGER
308-
309-
#endif // HERMES_ASYNCDEBUGGERAPI_H

NativeScript/napi/hermes/include/hermes/CompileJS.h

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
#ifndef HERMES_COMPILEJS_H
9-
#define HERMES_COMPILEJS_H
8+
#pragma once
109

1110
#include <optional>
1211
#include <string>
@@ -68,6 +67,31 @@ bool compileJS(
6867
std::string &bytecode,
6968
bool optimize = true);
7069

71-
} // namespace hermes
70+
/// Options for overload of compileJS that accepts CompileJSOptions.
71+
struct CompileJSOptions {
72+
/// If true, the bytecode will be optimized.
73+
bool optimize{true};
74+
/// Maximum number of instructions (in addition to parameter handling)
75+
/// that is allowed for inlining of small functions.
76+
unsigned inlineMaxSize{50};
77+
/// If true, the bytecode will be interruptable.
78+
bool emitAsyncBreakCheck{false};
79+
/// If true, debugging information will be generated in the bytecode.
80+
bool debug{false};
81+
/// Enable ES6 block scoping support.
82+
bool enableES6BlockScoping{false};
83+
/// Enable async generators support.
84+
bool enableAsyncGenerators{false};
85+
};
7286

73-
#endif
87+
/// Like the other compileJS overloads, but takes a struct of options with some
88+
/// additional configurability.
89+
bool compileJS(
90+
const std::string &str,
91+
const std::string &sourceURL,
92+
std::string &bytecode,
93+
const CompileJSOptions &options,
94+
DiagnosticHandler *diagHandler,
95+
std::optional<std::string_view> sourceMapBuf = std::nullopt);
96+
97+
} // namespace hermes

NativeScript/napi/hermes/include/hermes/DebuggerAPI.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
#ifndef HERMES_DEBUGGERAPI_H
9-
#define HERMES_DEBUGGERAPI_H
8+
#pragma once
109

1110
#ifdef HERMES_ENABLE_DEBUGGER
1211

@@ -31,6 +30,9 @@ class HermesValue;
3130
namespace facebook {
3231
namespace hermes {
3332
class HermesRuntime;
33+
// Forward declaration of the internal Root API class, which is marked as a
34+
// friend of the Debugger.
35+
class HermesRootAPI;
3436

3537
namespace debugger {
3638

@@ -269,6 +271,7 @@ class HERMES_EXPORT Debugger {
269271
::facebook::jsi::Value getThrownValue();
270272

271273
private:
274+
friend HermesRootAPI;
272275
friend std::unique_ptr<HermesRuntime> hermes::makeHermesRuntime(
273276
const ::hermes::vm::RuntimeConfig &);
274277
friend std::unique_ptr<jsi::ThreadSafeRuntime>
@@ -497,5 +500,3 @@ class EventObserver {
497500
} // namespace facebook
498501

499502
#endif // !HERMES_ENABLE_DEBUGGER
500-
501-
#endif // HERMES_DEBUGGERAPI_H

NativeScript/napi/hermes/include/hermes/Public/Buffer.h

Lines changed: 0 additions & 42 deletions
This file was deleted.

NativeScript/napi/hermes/include/hermes/Public/CtorConfig.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
#ifndef HERMES_PUBLIC_CTORCONFIG_H
9-
#define HERMES_PUBLIC_CTORCONFIG_H
8+
#pragma once
109

1110
#include <utility>
1211

@@ -128,11 +127,11 @@
128127
return TypeAsSingleToken{__VA_ARGS__}; \
129128
}
130129

131-
#define _HERMES_CTORCONFIG_SETTER(CX, TYPE, NAME, ...) \
132-
inline auto with##NAME(TYPE NAME)->decltype(*this) { \
133-
config_.NAME##_ = std::move(NAME); \
134-
NAME##Explicit_ = true; \
135-
return *this; \
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; \
136135
}
137136

138137
#define _HERMES_CTORCONFIG_BUILDER_GETTER(CX, TYPE, NAME, ...) \
@@ -144,5 +143,3 @@
144143
if (newConfig.has##NAME()) { \
145144
with##NAME(newConfig.config_.get##NAME()); \
146145
}
147-
148-
#endif // HERMES_PUBLIC_CTORCONFIG_H

NativeScript/napi/hermes/include/hermes/Public/DebuggerTypes.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,12 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
#ifndef HERMES_PUBLIC_DEBUGGERTYPES_H
9-
#define HERMES_PUBLIC_DEBUGGERTYPES_H
8+
#pragma once
109

1110
#include <cstdint>
1211
#include <string>
1312
#include <vector>
14-
#pragma GCC diagnostic push
1513

16-
#ifdef HERMES_COMPILER_SUPPORTS_WSHORTEN_64_TO_32
17-
#pragma GCC diagnostic ignored "-Wshorten-64-to-32"
18-
#endif
1914
namespace hermes {
2015
namespace vm {
2116
class Debugger;
@@ -88,7 +83,7 @@ struct StackTrace {
8883

8984
private:
9085
explicit StackTrace(std::vector<CallFrameInfo> frames)
91-
: frames_(std::move(frames)){};
86+
: frames_(std::move(frames)) {};
9287
friend ProgramState;
9388
friend ::hermes::vm::Debugger;
9489
std::vector<CallFrameInfo> frames_;
@@ -196,5 +191,3 @@ struct BreakpointInfo {
196191
} // namespace debugger
197192
} // namespace hermes
198193
} // namespace facebook
199-
200-
#endif

0 commit comments

Comments
 (0)