@qalqi/headless-hermes-linux-x64 0.0.0-stage → 0.1.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.
@@ -0,0 +1,59 @@
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 <jsi/jsi.h>
11
+
12
+ namespace facebook {
13
+ namespace jsi {
14
+
15
+ class FileBuffer : public Buffer {
16
+ public:
17
+ FileBuffer(const std::string& path);
18
+ ~FileBuffer() override;
19
+
20
+ size_t size() const override {
21
+ return size_;
22
+ }
23
+
24
+ const uint8_t* data() const override {
25
+ return data_;
26
+ }
27
+
28
+ private:
29
+ size_t size_;
30
+ uint8_t* data_;
31
+ };
32
+
33
+ // A trivial implementation of PreparedJavaScript that simply stores the source
34
+ // buffer and URL.
35
+ class SourceJavaScriptPreparation final : public jsi::PreparedJavaScript,
36
+ public jsi::Buffer {
37
+ std::shared_ptr<const jsi::Buffer> buf_;
38
+ std::string sourceURL_;
39
+
40
+ public:
41
+ SourceJavaScriptPreparation(
42
+ std::shared_ptr<const jsi::Buffer> buf,
43
+ std::string sourceURL)
44
+ : buf_(std::move(buf)), sourceURL_(std::move(sourceURL)) {}
45
+
46
+ const std::string& sourceURL() const {
47
+ return sourceURL_;
48
+ }
49
+
50
+ size_t size() const override {
51
+ return buf_->size();
52
+ }
53
+ const uint8_t* data() const override {
54
+ return buf_->data();
55
+ }
56
+ };
57
+
58
+ } // namespace jsi
59
+ } // namespace facebook
@@ -0,0 +1,79 @@
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 <mutex>
11
+
12
+ #include <jsi/decorator.h>
13
+ #include <jsi/jsi.h>
14
+
15
+ namespace facebook {
16
+ namespace jsi {
17
+
18
+ class ThreadSafeRuntime : public Runtime {
19
+ public:
20
+ virtual void lock() const = 0;
21
+ virtual void unlock() const = 0;
22
+ virtual Runtime& getUnsafeRuntime() = 0;
23
+ };
24
+
25
+ namespace detail {
26
+
27
+ template <typename R, typename L>
28
+ struct WithLock {
29
+ L lock;
30
+ WithLock(R& r) : lock(r) {}
31
+ void before() {
32
+ lock.lock();
33
+ }
34
+ void after() {
35
+ lock.unlock();
36
+ }
37
+ };
38
+
39
+ // The actual implementation of a given ThreadSafeRuntime. It's parameterized
40
+ // by:
41
+ //
42
+ // - R: The actual Runtime type that this wraps
43
+ // - L: A lock type that has three members:
44
+ // - L(R& r) // ctor
45
+ // - void lock()
46
+ // - void unlock()
47
+ template <typename R, typename L>
48
+ class ThreadSafeRuntimeImpl final
49
+ : public WithRuntimeDecorator<WithLock<R, L>, R, ThreadSafeRuntime> {
50
+ public:
51
+ template <typename... Args>
52
+ ThreadSafeRuntimeImpl(Args&&... args)
53
+ : WithRuntimeDecorator<WithLock<R, L>, R, ThreadSafeRuntime>(
54
+ unsafe_,
55
+ lock_),
56
+ unsafe_(std::forward<Args>(args)...),
57
+ lock_(unsafe_) {}
58
+
59
+ R& getUnsafeRuntime() override {
60
+ return WithRuntimeDecorator<WithLock<R, L>, R, ThreadSafeRuntime>::plain();
61
+ }
62
+
63
+ void lock() const override {
64
+ lock_.before();
65
+ }
66
+
67
+ void unlock() const override {
68
+ lock_.after();
69
+ }
70
+
71
+ private:
72
+ R unsafe_;
73
+ mutable WithLock<R, L> lock_;
74
+ };
75
+
76
+ } // namespace detail
77
+
78
+ } // namespace jsi
79
+ } // namespace facebook
@@ -0,0 +1,277 @@
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_NAPI_HERMES_NAPI_H
9
+ #define HERMES_NAPI_HERMES_NAPI_H
10
+
11
+ #include "hermes/napi/node_api.h"
12
+
13
+ #ifdef __cplusplus
14
+ extern "C" {
15
+ #endif
16
+
17
+ //===========================================================================
18
+ // Host integration interface
19
+ //===========================================================================
20
+
21
+ /// Abstract interface that the host application can provide to integrate
22
+ /// with its runtime environment. This covers async work scheduling
23
+ /// (napi_create_async_work / napi_queue_async_work), thread-safe
24
+ /// function dispatch, libuv event loop access, and fatal exception
25
+ /// handling. Fields left as nullptr preserve default behavior — in
26
+ /// particular, async work and thread-safe function APIs will return
27
+ /// napi_generic_failure if the relevant callbacks are not provided.
28
+ ///
29
+ /// The host is responsible for the lifetime of this struct — it must
30
+ /// outlive the napi_env that references it.
31
+ struct hermes_napi_host {
32
+ /// Schedule \p execute to run on a worker thread. When execute
33
+ /// completes, schedule \p complete to run on the main (JS) thread.
34
+ /// \p data is passed through to both callbacks.
35
+ /// \p complete receives \p status == 0 on success, or
36
+ /// napi_cancelled if the work was cancelled.
37
+ void (*post_work)(
38
+ void *loop_data,
39
+ void *work_data,
40
+ void (*execute)(void *work_data),
41
+ void (*complete)(void *work_data, napi_status status));
42
+
43
+ /// Cancel a previously posted work item. The event loop should
44
+ /// attempt to cancel the work. If successful, the complete callback
45
+ /// from post_work should be called with status == napi_cancelled.
46
+ /// Returns true if cancellation was initiated, false if the work
47
+ /// has already started or completed.
48
+ bool (*cancel_work)(void *loop_data, void *work_data);
49
+
50
+ /// Schedule \p callback to run on the main (JS) thread.
51
+ /// This is used by thread-safe functions to dispatch queued calls.
52
+ /// \p data is passed through to the callback.
53
+ void (*post_task)(
54
+ void *loop_data,
55
+ void *task_data,
56
+ void (*callback)(void *task_data));
57
+
58
+ /// Opaque data pointer passed as the first argument to post_work,
59
+ /// cancel_work, and post_task. Typically the host's event loop
60
+ /// instance.
61
+ void *data;
62
+
63
+ /// If non-null, napi_get_uv_event_loop() returns this pointer.
64
+ /// The host is responsible for the lifetime — it must outlive the
65
+ /// napi_env. Embedders without libuv leave this nullptr (the
66
+ /// default), in which case napi_get_uv_event_loop() returns
67
+ /// napi_generic_failure.
68
+ struct uv_loop_s *uv_loop;
69
+
70
+ /// If non-null, called by napi_fatal_exception() instead of aborting.
71
+ /// The host typically routes this to
72
+ /// process.emit('uncaughtException', err).
73
+ /// Receives the host data pointer, the napi_env, and the error value.
74
+ /// When null (the default), napi_fatal_exception() prints the error
75
+ /// and calls abort() via hermes_fatal().
76
+ void (*fatal_exception)(void *data, napi_env env, napi_value err);
77
+
78
+ /// Hold a long-lived reference on the event loop. The host should
79
+ /// keep the loop alive until a matching unref_loop call. Used by
80
+ /// threadsafe functions to model libuv-style "ref" semantics: while
81
+ /// any tsfn in an env is referenced, the env holds one ref so
82
+ /// producer threads can dispatch into JS without racing the loop's
83
+ /// shutdown.
84
+ ///
85
+ /// Contract: within a single napi_env, ref_loop / unref_loop calls
86
+ /// are paired and NEVER nested — every ref_loop is followed by a
87
+ /// matching unref_loop before the next ref_loop from that env. The
88
+ /// env coalesces all referenced tsfns into one ref/unref pair, so an
89
+ /// env contributes at most one outstanding ref at a time.
90
+ ///
91
+ /// Across distinct envs sharing the same host, multiple unmatched
92
+ /// refs may be in flight concurrently — one per env that has any
93
+ /// referenced tsfn — so the host implementation must accept
94
+ /// concurrent unmatched ref_loop calls (typically via a refcount
95
+ /// or its existing source-tracking mechanism).
96
+ ///
97
+ /// Optional — when null, TSFN ref/unref are tracked for API
98
+ /// compatibility only and have no effect on loop lifetime.
99
+ /// Must be paired with unref_loop.
100
+ void (*ref_loop)(void *loop_data);
101
+
102
+ /// Release a long-lived loop reference previously taken by ref_loop.
103
+ /// Optional; null only if ref_loop is also null.
104
+ void (*unref_loop)(void *loop_data);
105
+ };
106
+
107
+ typedef hermes_napi_host hermes_napi_event_loop;
108
+
109
+ //===========================================================================
110
+ // Environment lifecycle (public API)
111
+ //===========================================================================
112
+
113
+ /// Create a new NAPI environment backed by a Hermes Runtime.
114
+ /// The \p hermes_runtime parameter is an opaque pointer to a
115
+ /// hermes::vm::Runtime instance.
116
+ ///
117
+ /// The env is owned by the Runtime and is automatically torn down and
118
+ /// freed when the Runtime is destroyed. The caller must keep the
119
+ /// Runtime alive for as long as it uses the returned pointer, and must
120
+ /// not free the env itself.
121
+ ///
122
+ /// Finalizer semantics at Runtime teardown:
123
+ ///
124
+ /// Finalizers registered via napi_wrap, napi_add_finalizer,
125
+ /// napi_create_external, etc. are guaranteed to be invoked exactly
126
+ /// once with the live env that owns the wrapped object. We never
127
+ /// invoke user finalizers with env == nullptr.
128
+ ///
129
+ /// Callbacks fire in one of two contexts, both with a non-null env:
130
+ ///
131
+ /// - Unrestricted: the wrapped object was collected while the GC
132
+ /// heap was fully functional — either during normal operation
133
+ /// (collection happens, callback is drained at the next
134
+ /// NAPI_PREAMBLE / drainJobs) or during the env's shutdown
135
+ /// phase (cleanup hooks, persistent-reference finalizers, the
136
+ /// instance-data finalizer, or thread-safe-function cleanup
137
+ /// transitively triggered a GC; the callback is drained at the
138
+ /// end of shutdown). The callback may freely allocate, call
139
+ /// into JS, and use the env as in any other context.
140
+ ///
141
+ /// - Restricted: the wrapped object was still reachable from JS
142
+ /// roots at teardown and was finalized by
143
+ /// getHeap().finalizeAll() itself. The callback is drained
144
+ /// after finalizeAll, with the GC heap in a post-mortem state.
145
+ /// Any NAPI call that would allocate or run JavaScript returns
146
+ /// napi_cannot_run_js instead of executing. A spec-conformant
147
+ /// finalizer (which only frees native resources — free(),
148
+ /// close(), delete, refcount decrement) is unaffected. A
149
+ /// finalizer that tries to call back into JS receives the
150
+ /// error code and may choose to log it.
151
+ ///
152
+ /// The optional \p host provides host integration callbacks (async work,
153
+ /// thread-safe functions, etc.); if nullptr, those APIs return
154
+ /// napi_generic_failure.
155
+ NAPI_EXTERN napi_env NAPI_CDECL
156
+ hermes_napi_create_env(void *hermes_runtime, hermes_napi_host *host = nullptr);
157
+
158
+ /// Get the last module registered via the deprecated napi_module_register().
159
+ /// Returns nullptr if no module has been registered.
160
+ NAPI_EXTERN const napi_module *NAPI_CDECL
161
+ hermes_napi_get_last_registered_module();
162
+
163
+ /// Load a NAPI addon from a shared library at \p path.
164
+ ///
165
+ /// This function:
166
+ /// 1. Opens the shared library via dlopen().
167
+ /// 2. Looks up the `napi_register_module_v1` symbol (modern approach).
168
+ /// If not found, falls back to the deprecated `napi_module_register()`
169
+ /// approach (checking `hermes_napi_get_last_registered_module()`).
170
+ /// 3. Creates an empty `exports` object in the caller's env.
171
+ /// 4. Calls the module's init function with (env, exports).
172
+ /// 5. Stores the resulting exports object in \p result.
173
+ ///
174
+ /// The caller must provide a valid \p env with an open handle scope.
175
+ /// The module's CallbackBundles are owned by the caller's env.
176
+ ///
177
+ /// Returns napi_ok on success, or an error status on failure (with a
178
+ /// pending exception set on the env).
179
+ NAPI_EXTERN napi_status NAPI_CDECL
180
+ hermes_napi_load_module(napi_env env, const char *path, napi_value *result);
181
+
182
+ //===========================================================================
183
+ // Script execution
184
+ //===========================================================================
185
+
186
+ /// Flags for hermes_run_script(). The first field is the struct
187
+ /// size for ABI-stable extensibility — newer versions can append
188
+ /// fields without breaking older callers.
189
+ struct hermes_run_script_flags {
190
+ /// Size of this struct in bytes. Must be set to
191
+ /// sizeof(hermes_run_script_flags) by the caller.
192
+ size_t struct_size;
193
+ /// Run in strict mode.
194
+ bool strict;
195
+ /// Enable TypeScript support.
196
+ bool enable_ts;
197
+ /// Keep the compiled bytecode alive for the lifetime of the Runtime,
198
+ /// even after all references to it are gone. This lets the engine
199
+ /// avoid copying certain parts of the buffer, resulting in faster
200
+ /// load times. Do not set this for short-lived modules (e.g. REPL
201
+ /// input) — they would accumulate and never be freed.
202
+ bool persistent;
203
+ };
204
+
205
+ /// Compile and run JavaScript source in a single call.
206
+ ///
207
+ /// \p data and \p size describe a UTF-8 source buffer. If the last
208
+ /// byte is '\0', the source is used zero-copy (actual source length
209
+ /// is size-1). Otherwise, an internal copy is made to add the null
210
+ /// terminator required by the compiler.
211
+ ///
212
+ /// The runtime takes ownership of the buffer: \p finalize_cb is
213
+ /// called with (\p data, \p size, \p finalize_hint) when the buffer
214
+ /// is no longer needed. Pass NULL for \p finalize_cb if the buffer
215
+ /// is static or externally managed.
216
+ ///
217
+ /// \p source_url appears in stack traces (NULL → empty string).
218
+ /// \p flags controls compilation options (NULL → all defaults).
219
+ ///
220
+ /// Returns napi_ok on success, napi_pending_exception on JS or
221
+ /// compile error.
222
+ NAPI_EXTERN napi_status NAPI_CDECL hermes_run_script(
223
+ napi_env env,
224
+ const uint8_t *data,
225
+ size_t size,
226
+ void (*finalize_cb)(const uint8_t *data, size_t size, void *hint),
227
+ void *finalize_hint,
228
+ const char *source_url,
229
+ const hermes_run_script_flags *flags,
230
+ napi_value *result);
231
+
232
+ //===========================================================================
233
+ // Pre-compiled bytecode
234
+ //===========================================================================
235
+
236
+ /// Flags for hermes_run_bytecode(). The first field is the struct
237
+ /// size for ABI-stable extensibility — newer versions can append
238
+ /// fields without breaking older callers.
239
+ struct hermes_bytecode_flags {
240
+ size_t struct_size;
241
+ /// Keep the bytecode alive for the lifetime of the Runtime, even
242
+ /// after all references to it are gone. This lets the engine avoid
243
+ /// copying certain parts of the buffer, resulting in faster load
244
+ /// times. Do not set this for short-lived modules (e.g. REPL
245
+ /// input) — they would accumulate and never be freed.
246
+ bool persistent;
247
+ bool hides_epilogue;
248
+ bool funcs_are_builtins;
249
+ };
250
+
251
+ /// Run pre-compiled Hermes bytecode.
252
+ ///
253
+ /// \p data and \p size describe the bytecode buffer. The runtime
254
+ /// takes ownership: \p finalize_cb is called with (\p data, \p size,
255
+ /// \p finalize_hint) when the buffer is no longer needed. Pass NULL
256
+ /// for \p finalize_cb if the buffer is static or externally managed.
257
+ ///
258
+ /// \p source_url appears in stack traces (NULL → empty string).
259
+ /// \p flags controls runtime module behavior (NULL → all defaults).
260
+ ///
261
+ /// Returns napi_ok on success, napi_pending_exception on JS error,
262
+ /// or napi_generic_failure on bytecode validation failure.
263
+ NAPI_EXTERN napi_status NAPI_CDECL hermes_run_bytecode(
264
+ napi_env env,
265
+ const uint8_t *data,
266
+ size_t size,
267
+ void (*finalize_cb)(const uint8_t *data, size_t size, void *hint),
268
+ void *finalize_hint,
269
+ const char *source_url,
270
+ const hermes_bytecode_flags *flags,
271
+ napi_value *result);
272
+
273
+ #ifdef __cplusplus
274
+ }
275
+ #endif
276
+
277
+ #endif // HERMES_NAPI_HERMES_NAPI_H
@@ -0,0 +1,61 @@
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_NAPI_HERMES_NAPI_COMPILE_H
9
+ #define HERMES_NAPI_HERMES_NAPI_COMPILE_H
10
+
11
+ #include "hermes/napi/node_api.h"
12
+
13
+ #include <stddef.h>
14
+
15
+ /// Flags for hermes_compile_to_bytecode(). The first field is the struct
16
+ /// size for ABI-stable extensibility — newer versions can append
17
+ /// fields without breaking older callers.
18
+ struct hermes_compile_flags {
19
+ size_t struct_size;
20
+ /// Parse in strict mode.
21
+ bool strict;
22
+ /// Run the full optimization pipeline on the compiled IR.
23
+ bool optimize;
24
+ /// Emit async break check instructions, e.g. for enforcing execution
25
+ /// time limits.
26
+ bool emit_async_break_check;
27
+ /// Enable TypeScript support.
28
+ bool enable_ts;
29
+ };
30
+
31
+ /// Compile JavaScript source to serialized Hermes bytecode.
32
+ ///
33
+ /// \p source and \p source_size describe the UTF-8 source buffer.
34
+ /// For best performance, include the null terminator in the buffer
35
+ /// (i.e. source[source_size - 1] == '\0'); the actual source text is
36
+ /// then source_size - 1 bytes. If the buffer does not end with '\0',
37
+ /// the function makes an internal copy to add one.
38
+ ///
39
+ /// \p source_url appears in error messages and stack traces
40
+ /// (NULL -> empty string).
41
+ /// \p flags controls compilation behavior (NULL -> all defaults).
42
+ ///
43
+ /// On success, *bytecode_out and *bytecode_size_out receive a newly
44
+ /// allocated buffer containing the serialized bytecode. Free it with
45
+ /// hermes_free_bytecode().
46
+ ///
47
+ /// Returns napi_ok on success, napi_pending_exception on parse/compile
48
+ /// error (the exception describes the error).
49
+ napi_status hermes_compile_to_bytecode(
50
+ napi_env env,
51
+ const uint8_t *source,
52
+ size_t source_size,
53
+ const char *source_url,
54
+ const hermes_compile_flags *flags,
55
+ uint8_t **bytecode_out,
56
+ size_t *bytecode_size_out);
57
+
58
+ /// Free bytecode allocated by hermes_compile_to_bytecode().
59
+ void hermes_free_bytecode(uint8_t *bytecode);
60
+
61
+ #endif // HERMES_NAPI_HERMES_NAPI_COMPILE_H
Binary file
package/package.json CHANGED
@@ -1,6 +1,11 @@
1
1
  {
2
2
  "name": "@qalqi/headless-hermes-linux-x64",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Prebuilt Hermes (linux-x64) for @qalqi/headless-hermes: libhermesvm, hermes, hermesc, headers. Hermes commit d41288df5432666c832abc2b2d8d53a14c1ec3de.",
5
+ "license": "Apache-2.0 AND MIT",
6
+ "repository": { "type": "git", "url": "git+https://github.com/qalqi/headless-hermes-jsi-node-api.git" },
7
+ "os": ["linux"],
8
+ "cpu": ["x64"],
9
+ "hermes": { "commit": "d41288df5432666c832abc2b2d8d53a14c1ec3de" },
10
+ "files": ["include/", "lib/", "bin/", "LICENSE", "LICENSE-hermes", "BUILD-INFO.txt"]
11
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.