@qalqi/headless-hermes 0.1.0 → 0.2.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.
package/README.md CHANGED
@@ -1,25 +1,172 @@
1
1
  # @qalqi/headless-hermes
2
2
 
3
- The [Hermes](https://github.com/facebook/hermes) JavaScript engine for headless use, without React Native: the
4
- `hermes` and `hermesc` tools, the JSI runtime library (`libhermesvm`) and the headers for JSI, Hermes and Node-API,
5
- prebuilt for Linux x64, Linux arm64 and macOS arm64. Built from a pinned commit of Hermes' `static_h` branch.
3
+ **Hermes, without the app.** The JavaScript engine behind React Native, as a plain command and a C++ library: no
4
+ Metro, no simulator, no device. One `npm i`, and Hermes runs on your laptop and in CI.
5
+
6
+ [![npm](https://img.shields.io/npm/v/@qalqi/headless-hermes)](https://www.npmjs.com/package/@qalqi/headless-hermes)
7
+ [![build](https://github.com/qalqi/headless-hermes-jsi-node-api/actions/workflows/build.yml/badge.svg)](https://github.com/qalqi/headless-hermes-jsi-node-api/actions/workflows/build.yml)
6
8
 
7
9
  ```sh
8
10
  npm i -D @qalqi/headless-hermes
9
- npx hermes script.js
10
11
  ```
11
12
 
13
+ ```js
14
+ // fib.js
15
+ const fib = n => n < 2 ? n : fib(n - 1) + fib(n - 2);
16
+ print(`fib(25) = ${fib(25)} on Hermes ${HermesInternal.getRuntimeProperties()["OSS Release Version"]}`);
17
+ ```
18
+
19
+ ```console
20
+ $ npx hermes fib.js
21
+ fib(25) = 75025 on Hermes 1.0.0
22
+ ```
23
+
24
+ ## Does my code run on Hermes?
25
+
26
+ Node and browsers are not Hermes. Find out on your machine, not on a phone:
27
+
28
+ ```js
29
+ // probe.js
30
+ const check = (name, f) => { try { f(); print(` ok ${name}`); } catch (e) { print(` no ${name}: ${e.message}`); } };
31
+ check("class private fields", () => { class A { #x = 1; get() { return this.#x; } } new A().get(); });
32
+ check("Intl.DateTimeFormat", () => new Intl.DateTimeFormat("en").format(0));
33
+ check("with statement", () => new Function("with ({}) {}")());
34
+ check("WeakRef", () => new WeakRef({}));
35
+ check("structuredClone", () => structuredClone({}));
36
+ ```
37
+
38
+ ```console
39
+ $ npx hermes probe.js
40
+ ok class private fields
41
+ no Intl.DateTimeFormat: Property 'Intl' doesn't exist
42
+ no with statement: 1:14:with statement is not supported
43
+ ok WeakRef
44
+ no structuredClone: Property 'structuredClone' doesn't exist
45
+ ```
46
+
47
+ The same check in GitHub Actions, no install step:
48
+
49
+ ```yaml
50
+ - run: npx -y -p @qalqi/headless-hermes hermes test/hermes-smoke.js
51
+ ```
52
+
53
+ ## Compile to bytecode
54
+
55
+ `hermesc` ships too. Precompile once, start instantly:
56
+
57
+ ```console
58
+ $ npx hermesc -emit-binary -out fib.hbc fib.js
59
+ $ npx hermes fib.hbc
60
+ fib(25) = 75025 on Hermes 1.0.0
61
+ ```
62
+
63
+ ## Embed Hermes in C++ in 13 lines
64
+
65
+ The package carries the JSI headers and the runtime library, so a native host is one compiler call away:
66
+
67
+ ```cpp
68
+ // host.cpp
69
+ #include <hermes/hermes.h>
70
+ #include <iostream>
71
+ using namespace facebook;
72
+ int main() {
73
+ auto rt = facebook::hermes::makeHermesRuntime();
74
+ rt->global().setProperty(*rt, "twice", jsi::Function::createFromHostFunction(
75
+ *rt, jsi::PropNameID::forAscii(*rt, "twice"), 1,
76
+ [](jsi::Runtime &, const jsi::Value &, const jsi::Value *args, size_t) {
77
+ return jsi::Value(args[0].getNumber() * 2);
78
+ }));
79
+ auto r = rt->evaluateJavaScript(std::make_shared<jsi::StringBuffer>("twice(21)"), "main.js");
80
+ std::cout << "twice(21) = " << r.getNumber() << std::endl;
81
+ }
82
+ ```
83
+
84
+ ```console
85
+ $ INC=$(node -p "require('@qalqi/headless-hermes').includeDir()")
86
+ $ LIB=$(node -p "require('@qalqi/headless-hermes').libDir()")
87
+ $ g++ -std=c++17 host.cpp -I"$INC" -L"$LIB" -lhermesvm -Wl,-rpath,"$LIB" -o host
88
+ $ ./host
89
+ twice(21) = 42
90
+ ```
91
+
92
+ That is how a JSI native module gets tested without React Native: register it on a headless runtime, run its
93
+ JavaScript, assert. The [repository](https://github.com/qalqi/headless-hermes-jsi-node-api) has more hosts to copy:
94
+ host functions loaded from shared libraries, an event loop with `setTimeout` and microtasks, and a Node-API host that
95
+ loads unmodified Node.js addons (C and [napi-rs](https://napi.rs/)) into Hermes.
96
+
97
+ ## Load your JSI library: no host code at all
98
+
99
+ `hermes-jsi-run` is that host, ready-made: `console`, timers, microtasks, and an event loop your library's threads post
100
+ results back to (`headless_hermes.h`, the job React Native's CallInvoker does in an app). Export one function:
101
+
102
+ ```cpp
103
+ extern "C" void headless_hermes_register(facebook::jsi::Runtime &rt, const headless_hermes_host *host);
104
+ ```
105
+
106
+ ```console
107
+ $ g++ -std=c++17 -shared -fPIC worker.cpp -I"$INC" -L"$LIB" -lhermesvm -o libworker.so
108
+ $ npx hermes-jsi-run demo.js ./libworker.so
109
+ add(2, 3) = 5
110
+ script done; the loop keeps running while work is pending
111
+ microtask before any timer
112
+ awaited { a: 2, b: 4 } [ 6 ] Uint8Array(2) [ 2, 4 ]
113
+ double(21) resolved on another thread: 42
114
+ timer after 50 ms
115
+ ```
116
+
117
+ `double()` resolves its Promise from a `std::thread`:
118
+ [worker.cpp](https://github.com/qalqi/headless-hermes-jsi-node-api/blob/main/jsi-run/example/worker.cpp),
119
+ [demo.js](https://github.com/qalqi/headless-hermes-jsi-node-api/blob/main/jsi-run/example/demo.js).
120
+
121
+ ## Node.js addons, on Hermes
122
+
123
+ Hermes implements Node-API too. `napi-host` loads `.node` addons, unmodified (C, C++,
124
+ [napi-rs](https://napi.rs/)), through a `require()` that resolves like Node's:
125
+
126
+ ```js
127
+ // app.js
128
+ const { hello, add } = require("../hello.node");
129
+ console.log(hello(), "| add(20, 22) =", add(20, 22));
130
+ ```
131
+
132
+ ```console
133
+ $ npx napi-host demo/app.js
134
+ hello from Node-API | add(20, 22) = 42
135
+ ```
136
+
137
+ Async work, thread-safe functions and Promises work: the host runs the event loop they need.
138
+
139
+ ## What you get
140
+
141
+ | Platform | Package (installed for you) |
142
+ |---|---|
143
+ | Linux x64 | [`@qalqi/headless-hermes-linux-x64`](https://www.npmjs.com/package/@qalqi/headless-hermes-linux-x64) |
144
+ | Linux arm64 | [`@qalqi/headless-hermes-linux-arm64`](https://www.npmjs.com/package/@qalqi/headless-hermes-linux-arm64) |
145
+ | macOS arm64 | [`@qalqi/headless-hermes-darwin-arm64`](https://www.npmjs.com/package/@qalqi/headless-hermes-darwin-arm64) |
146
+
147
+ Each has `bin/` (`hermes`, `hermesc`, `hermes-jsi-run`, `napi-host`), `lib/libhermesvm` and `include/` (JSI, Hermes,
148
+ Node-API, `headless_hermes.h`). npm picks the one for
149
+ your machine; you only ever install `@qalqi/headless-hermes`.
150
+
12
151
  ```js
13
152
  const hermes = require('@qalqi/headless-hermes');
14
- hermes.includeDir(); // headers for building your own host
15
- hermes.libDir(); // libhermesvm
16
- hermes.buildInfo(); // the exact Hermes commit
153
+ hermes.includeDir(); // headers
154
+ hermes.libDir(); // libhermesvm
155
+ hermes.bin('napi-host'); // path to a tool
156
+ hermes.buildInfo(); // the exact Hermes commit
17
157
  ```
18
158
 
19
- Only need `hermesc` to compile bytecode for a React Native build? That's Meta's
20
- [`hermes-compiler`](https://www.npmjs.com/package/hermes-compiler). This package is for running and embedding Hermes
21
- outside React Native.
159
+ ## Which Hermes?
160
+
161
+ A pinned commit of Hermes' `static_h` branch, the one with JSI and Node-API (it reports itself as Hermes 1.0.0).
162
+ `buildInfo()` prints the commit; the repository's README maps every release to its Hermes commit. Every version is
163
+ built and published by GitHub Actions with [npm provenance](https://docs.npmjs.com/generating-provenance-statements),
164
+ so each tarball links to the run and source that built it.
165
+
166
+ Only need `hermesc` to compile bytecode for a React Native release build? Use Meta's
167
+ [`hermes-compiler`](https://www.npmjs.com/package/hermes-compiler). This package is for running and embedding Hermes.
168
+
169
+ ## License
22
170
 
23
- Examples, the build scripts and the static libraries for linking your own Node-API host:
24
- [headless-hermes-jsi-node-api](https://github.com/qalqi/headless-hermes-jsi-node-api). Packaging is Apache-2.0;
25
- Hermes is MIT (LICENSE-hermes in each platform package).
171
+ Packaging: Apache-2.0. Hermes: MIT, Meta Platforms, Inc. and affiliates (`LICENSE-hermes` in each platform package).
172
+ Started from [tmikov/hermes-jsi-demos](https://github.com/tmikov/hermes-jsi-demos).
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+ const { spawnSync } = require('child_process');
4
+ const r = spawnSync(require('..').bin('hermes-jsi-run'), process.argv.slice(2), { stdio: 'inherit' });
5
+ if (r.error) { console.error(r.error.message); process.exit(1); }
6
+ process.exit(r.status === null ? 1 : r.status);
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+ 'use strict';
3
+ const { spawnSync } = require('child_process');
4
+ const r = spawnSync(require('..').bin('napi-host'), process.argv.slice(2), { stdio: 'inherit' });
5
+ if (r.error) { console.error(r.error.message); process.exit(1); }
6
+ process.exit(r.status === null ? 1 : r.status);
package/index.js CHANGED
@@ -19,11 +19,11 @@ function platformPackageDir() {
19
19
  const dir = () => platformPackageDir();
20
20
  module.exports = {
21
21
  platformPackageDir,
22
- /** Headers: include/jsi, include/hermes (hermes.h, Public/), include/hermes/napi (node_api.h), include/napi (hermes_napi.h). */
22
+ /** Headers: include/jsi, include/hermes (hermes.h, Public/), include/hermes/napi (node_api.h), include/napi (hermes_napi.h), include/headless_hermes.h. */
23
23
  includeDir: () => path.join(dir(), 'include'),
24
24
  /** The JSI runtime library (libhermesvm). */
25
25
  libDir: () => path.join(dir(), 'lib'),
26
- /** A bundled tool: 'hermes' or 'hermesc'. */
26
+ /** A bundled tool: 'hermes', 'hermesc', 'hermes-jsi-run' or 'napi-host'. */
27
27
  bin: (name) => path.join(dir(), 'bin', name),
28
28
  /** The Hermes commit and build details. */
29
29
  buildInfo: () => require('fs').readFileSync(path.join(dir(), 'BUILD-INFO.txt'), 'utf8'),
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@qalqi/headless-hermes",
3
- "version": "0.1.0",
4
- "description": "Hermes JavaScript engine for headless use (no React Native): the hermes and hermesc tools, the JSI runtime library and headers (JSI, Hermes, Node-API), prebuilt per platform",
3
+ "version": "0.2.0",
4
+ "description": "Hermes JavaScript engine for headless use (no React Native): hermes and hermesc, hermes-jsi-run (a JSI host for native libraries) and napi-host (Node-API addons on Hermes), the runtime library and headers, prebuilt per platform",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
7
7
  "type": "git",
@@ -20,7 +20,9 @@
20
20
  "main": "index.js",
21
21
  "bin": {
22
22
  "hermes": "bin/hermes.js",
23
- "hermesc": "bin/hermesc.js"
23
+ "hermesc": "bin/hermesc.js",
24
+ "hermes-jsi-run": "bin/hermes-jsi-run.js",
25
+ "napi-host": "bin/napi-host.js"
24
26
  },
25
27
  "files": [
26
28
  "index.js",
@@ -32,9 +34,9 @@
32
34
  "node": ">=18"
33
35
  },
34
36
  "optionalDependencies": {
35
- "@qalqi/headless-hermes-linux-x64": "0.1.0",
36
- "@qalqi/headless-hermes-linux-arm64": "0.1.0",
37
- "@qalqi/headless-hermes-darwin-arm64": "0.1.0"
37
+ "@qalqi/headless-hermes-linux-x64": "0.2.0",
38
+ "@qalqi/headless-hermes-linux-arm64": "0.2.0",
39
+ "@qalqi/headless-hermes-darwin-arm64": "0.2.0"
38
40
  },
39
41
  "hermes": {
40
42
  "commit": "d41288df5432666c832abc2b2d8d53a14c1ec3de"