docs.sheetjs.com/docz/docs/03-demos/42-engines/09-hermes.md

877 lines
24 KiB
Markdown
Raw Permalink Normal View History

2023-05-30 06:41:09 +00:00
---
2023-08-03 02:49:32 +00:00
title: Sharing Sheets with Hermes
sidebar_label: C++ + Hermes
description: Process structured data in C++ programs. Seamlessly integrate spreadsheets into your program by pairing Hermes and SheetJS. Handle the most complex files without breaking a sweat.
2023-05-30 06:41:09 +00:00
pagination_prev: demos/bigdata/index
pagination_next: solutions/input
---
import current from '/version.js';
2024-01-29 03:29:45 +00:00
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
2023-05-30 06:41:09 +00:00
import CodeBlock from '@theme/CodeBlock';
2023-08-03 02:49:32 +00:00
[Hermes](https://hermesengine.dev/) is an embeddable JS engine written in C++.
2023-05-30 06:41:09 +00:00
2023-08-03 02:49:32 +00:00
[SheetJS](https://sheetjs.com) is a JavaScript library for reading and writing
data from spreadsheets.
2023-05-30 06:41:09 +00:00
2023-08-03 02:49:32 +00:00
This demo uses Hermes and SheetJS to pull data from a spreadsheet and print CSV
rows. We'll explore how to load SheetJS in a Hermes context and process
spreadsheets from a C++ program.
The ["Integration Example"](#integration-example) section includes a complete
command-line tool for reading data from files.
## Integration Details
:::info pass
Many Hermes functions are not documented. The explanation was verified against
2024-03-16 16:04:18 +00:00
commit `15b323d`.
2023-08-03 02:49:32 +00:00
:::
:::danger pass
2023-05-30 06:41:09 +00:00
The main target for Hermes is React Native. At the time of writing, there was
no official documentation for embedding the Hermes engine in C++ programs.
:::
2023-08-03 02:49:32 +00:00
### Initialize Hermes
2023-05-30 06:41:09 +00:00
2023-08-03 02:49:32 +00:00
A Hermes engine instance is created with `facebook::hermes::makeHermesRuntime`:
2023-05-30 06:41:09 +00:00
```cpp
std::unique_ptr<facebook::jsi::Runtime> rt(facebook::hermes::makeHermesRuntime());
```
2023-08-03 02:49:32 +00:00
_Essential Objects_
2023-06-20 01:21:34 +00:00
2023-08-03 02:49:32 +00:00
Hermes does not expose a `console` or `global` variable, but they can be
synthesized from JS code in the runtime:
2023-06-20 01:21:34 +00:00
2023-08-03 02:49:32 +00:00
- `global` can be obtained from a reference to `this` in an unbound function:
2023-06-20 01:21:34 +00:00
```js
/* create global object */
var global = (function(){ return this; }).call(null);
2023-08-03 02:49:32 +00:00
```
- `console.log` can be constructed from the builtin `print` function:
```js
2023-06-20 01:21:34 +00:00
/* create a fake `console` from the hermes `print` builtin */
var console = { log: function(x) { print(x); } };
```
2023-08-03 02:49:32 +00:00
The code can be stored in a C string and evaluated using `prepareJavascript` to
prepare code and `evaluatePreparedJavascript` to evaluate:
2023-05-30 06:41:09 +00:00
```cpp
2023-08-03 02:49:32 +00:00
const char *init_code =
2023-05-30 06:41:09 +00:00
/* create global object */
"var global = (function(){ return this; }).call(null);"
/* create a fake `console` from the hermes `print` builtin */
"var console = { log: function(x) { print(x); } };"
2023-08-03 02:49:32 +00:00
;
auto src = std::make_shared<facebook::jsi::StringBuffer>(init_code);
2023-05-30 06:41:09 +00:00
auto js = rt->prepareJavaScript(src, std::string("<eval>"));
rt->evaluatePreparedJavaScript(js);
```
2023-08-03 02:49:32 +00:00
:::info Exception handling
Standard C++ exception handling patterns are used in Hermes integration code.
The base class for Hermes exceptions is `facebook::jsi::JSIException`:
```cpp
try {
const char *init_code = "...";
auto src = std::make_shared<facebook::jsi::StringBuffer>(init_code);
auto js = rt->prepareJavaScript(src, std::string("<eval>"));
rt->evaluatePreparedJavaScript(js);
} catch (const facebook::jsi::JSIException &e) {
std::cerr << "JavaScript exception: " << e.what() << std::endl;
return 1;
}
```
:::
### Load SheetJS Scripts
[SheetJS Standalone scripts](/docs/getting-started/installation/standalone) can
be parsed and evaluated in a Hermes context.
2023-05-30 06:41:09 +00:00
The main library can be loaded by reading the script from the file system and
2023-08-03 02:49:32 +00:00
evaluating in the Hermes context.
2024-01-29 03:29:45 +00:00
:::note pass
2023-08-03 02:49:32 +00:00
There are nonstandard tricks to embed the entire script in the binary. There are
language proposals such as `#embed` (mirroring the same feature in C23).
For simplicity, the examples read the script file from the filesystem.
:::
_Reading scripts from the filesystem_
For the purposes of this demo, the standard C `<stdio.h>` methods are used:
2023-05-30 06:41:09 +00:00
```cpp
static char *read_file(const char *filename, size_t *sz) {
FILE *f = fopen(filename, "rb");
if(!f) return NULL;
long fsize; { fseek(f, 0, SEEK_END); fsize = ftell(f); fseek(f, 0, SEEK_SET); }
char *buf = (char *)malloc(fsize * sizeof(char));
*sz = fread((void *) buf, 1, fsize, f);
fclose(f);
return buf;
}
2023-08-03 02:49:32 +00:00
// ...
/* read SheetJS library from filesystem */
size_t sz; char *xlsx_full_min_js = read_file("xlsx.full.min.js", &sz);
```
2023-09-28 17:13:57 +00:00
:::caution pass
For Windows applications, the string must be null-terminated:
```cpp
/* Hermes-Windows requires the null terminator */
static char *read_file_null(const char *filename, size_t *sz) {
FILE *f = fopen(filename, "rb");
if(!f) return NULL;
long fsize; { fseek(f, 0, SEEK_END); fsize = ftell(f) + 1; fseek(f, 0, SEEK_SET); }
char *buf = (char *)malloc(fsize * sizeof(char));
*sz = fread((void *) buf, 1, fsize, f);
buf[fsize - 1] = 0;
fclose(f);
return buf;
}
// ...
/* read SheetJS library from filesystem */
size_t sz; char *xlsx_full_min_js = read_file_null("xlsx.full.min.js", &sz);
```
:::
2023-08-03 02:49:32 +00:00
_Hermes Wrapper_
Hermes does not provide a friendly way to prepare JavaScript code stored in a
standard heap-allocated C string. Fortunately a wrapper can be created:
```cpp
2023-05-30 06:41:09 +00:00
/* Unfortunately the library provides no C-friendly Buffer classes */
class CBuffer : public facebook::jsi::Buffer {
public:
CBuffer(const uint8_t *data, size_t size) : buf(data), sz(size) {}
size_t size() const override { return sz; }
const uint8_t *data() const override { return buf; }
private:
const uint8_t *buf;
size_t sz;
};
// ...
/* load SheetJS library */
auto src = std::make_shared<CBuffer>(CBuffer((uint8_t *)xlsx_full_min_js, sz));
2023-08-03 02:49:32 +00:00
```
_Evaluating SheetJS Library Code_
The code wrapper can be "prepared" with `prepareJavascript` and "evaluated" with
`evaluatePreparedJavascript`.
The second argument to `preparedJavascript` is a C++ `std::string` that holds
the source URL. Typically a name like `xlsx.full.min.js` helps distinguish
SheetJS library exceptions from other parts of the application.
```cpp
2023-05-30 06:41:09 +00:00
auto js = rt->prepareJavaScript(src, std::string("xlsx.full.min.js"));
rt->evaluatePreparedJavaScript(js);
```
2023-08-03 02:49:32 +00:00
_Testing_
If the library is loaded, `XLSX.version` will be a string. This string can be
pulled into the main C++ program.
The `evaluatePreparedJavascript` method returns a `facebook::jsi::Value` object
that represents the result:
2023-05-30 06:41:09 +00:00
```cpp
2023-08-03 02:49:32 +00:00
/* evaluate XLSX.version and capture the result */
auto src = std::make_shared<facebook::jsi::StringBuffer>("XLSX.version");
2023-05-30 06:41:09 +00:00
auto js = rt->prepareJavaScript(src, std::string("<eval>"));
2023-08-03 02:49:32 +00:00
facebook::jsi::Value jsver = rt->evaluatePreparedJavaScript(js);
```
The `getString` method extracts the string value and returns an internal string
object (`facebook::jsi::String`). Given that string object, the `utf8` method
returns a proper C++ `std::string` that can be printed:
```cpp
/* pull the version string into C++ code and print */
facebook::jsi::String jsstr = jsver.getString(*rt);
std::string cppver = jsstr.utf8(*rt);
std::cout << "SheetJS version " << cppver << std::endl;
2023-05-30 06:41:09 +00:00
```
### Reading Files
2023-08-03 02:49:32 +00:00
Typically C++ code will read files and Hermes will project the data in the JS
engine as an `ArrayBuffer`. SheetJS libraries can parse `ArrayBuffer` data.
Standard SheetJS operations can pick the first worksheet and generate CSV string
data from the worksheet. Hermes provides methods to convert the JS strings back
to `std::string` objects for further processing in C++.
2023-09-24 03:59:48 +00:00
:::note pass
2023-08-03 02:49:32 +00:00
It is strongly recommended to create a stub function to perform the entire
workflow in JS code and pass the final result back to C++.
:::
_Hermes Wrapper_
2023-05-30 06:41:09 +00:00
Hermes supports `ArrayBuffer` but has no simple helper to read raw memory.
Libraries are expected to implement `MutableBuffer`:
```cpp
2023-06-20 01:21:34 +00:00
/* ArrayBuffer constructor expects MutableBuffer */
2023-05-30 06:41:09 +00:00
class CMutableBuffer : public facebook::jsi::MutableBuffer {
public:
CMutableBuffer(uint8_t *data, size_t size) : buf(data), sz(size) {}
size_t size() const override { return sz; }
uint8_t *data() override { return buf; }
private:
uint8_t *buf;
size_t sz;
};
```
2023-08-03 02:49:32 +00:00
A `facebook::jsi::ArrayBuffer` object can be created using the wrapper:
```cpp
/* load payload as ArrayBuffer */
size_t sz; char *data = read_file("pres.xlsx", &sz);
auto payload = std::make_shared<CMutableBuffer>(CMutableBuffer((uint8_t *)data, sz));
auto ab = facebook::jsi::ArrayBuffer(*rt, payload);
```
_SheetJS Operations_
In this example, the goal is to pull the first worksheet and generate CSV rows.
`XLSX.read`[^1] parses the `ArrayBuffer` and returns a SheetJS workbook object:
```js
var wb = XLSX.read(buf);
```
The `SheetNames` property[^2] is an array of the sheet names in the workbook.
The first sheet name can be obtained with the following JS snippet:
```js
var first_sheet_name = wb.SheetNames[0];
```
The `Sheets` property[^3] is an object whose keys are sheet names and whose
corresponding values are worksheet objects.
```js
var first_sheet = wb.Sheets[first_sheet_name];
```
The `sheet_to_csv` utility function[^4] generates a CSV string from the sheet:
```js
var csv = XLSX.utils.sheet_to_csv(first_sheet);
```
_C++ integration code_
2023-05-30 06:41:09 +00:00
2023-06-20 01:21:34 +00:00
:::note pass
The stub function will be passed an `ArrayBuffer` object:
2023-05-30 06:41:09 +00:00
```js
function(buf) {
/* `buf` will be an ArrayBuffer */
var wb = XLSX.read(buf);
return XLSX.utils.sheet_to_csv(wb.Sheets[wb.SheetNames[0]]);
}
```
2023-06-20 01:21:34 +00:00
:::
2023-08-03 02:49:32 +00:00
The result after evaluating the stub is a `facebook::jsi::Value` object:
2023-05-30 06:41:09 +00:00
```cpp
2023-08-03 02:49:32 +00:00
/* define stub function to read and convert first sheet to CSV */
auto src = std::make_shared<facebook::jsi::StringBuffer>(
"(function(buf) {"
"var wb = XLSX.read(buf);"
"return XLSX.utils.sheet_to_csv(wb.Sheets[wb.SheetNames[0]]);"
"})"
);
auto js = rt->prepareJavaScript(src, std::string("<eval>"));
facebook::jsi::Value funcval = rt->evaluatePreparedJavaScript(js);
```
To call this function, the opaque `Value` must be converted to a `Function`:
```cpp
facebook::jsi::Function func = func.asObject(*rt).asFunction(*rt);
```
2023-05-30 06:41:09 +00:00
2023-08-03 02:49:32 +00:00
The `Function` exposes a `call` method to perform the function invocation. The
stub accepts an `ArrayBuffer` argument:
2023-05-30 06:41:09 +00:00
2023-08-03 02:49:32 +00:00
```cpp
/* call stub function and capture result */
facebook::jsi::Value csv = func.call(*rt, ab);
```
In the same way the library version string was pulled into C++ code, the CSV
data can be captured using `getString` and `utf8` methods:
```cpp
/* interpret as utf8 */
std::string str = csv.getString(*rt).utf8(*rt);
std::cout << str << std::endl;
2023-05-30 06:41:09 +00:00
```
## Complete Example
The "Integration Example" covers a traditional integration in a C++ application,
while the "CLI Test" demonstrates other concepts using the `hermes` CLI tool.
### Integration Example
2023-11-04 05:05:26 +00:00
:::note Tested Deployments
2023-05-30 06:41:09 +00:00
2023-06-05 20:12:53 +00:00
This demo was tested in the following deployments:
2023-05-30 06:41:09 +00:00
2023-06-05 20:12:53 +00:00
| Architecture | Git Commit | Date |
|:-------------|:-----------|:-----------|
2024-04-26 04:16:13 +00:00
| `darwin-x64` | `d070c74` | 2024-04-25 |
2024-05-24 08:24:50 +00:00
| `darwin-arm` | `d070c74` | 2024-05-23 |
2024-03-22 04:45:40 +00:00
| `linux-x64` | `d217af8` | 2024-03-21 |
2024-05-26 07:50:55 +00:00
| `linux-arm` | `d070c74` | 2024-05-25 |
2023-05-30 06:41:09 +00:00
2023-09-28 17:13:57 +00:00
The main Hermes source tree does not have Windows support. The `hermes-windows`
fork, which powers React Native for Windows, does have built-in support[^5]
| Architecture | Git Commit | Date |
|:-------------|:-----------|:-----------|
2024-03-25 04:13:01 +00:00
| `win10-x64` | `240573e` | 2024-03-24 |
2024-06-21 07:54:27 +00:00
| `win11-arm` | `240573e` | 2024-06-20 |
2023-09-28 17:13:57 +00:00
2023-11-04 05:05:26 +00:00
The ["Windows Example"](#windows-example) covers `hermes-windows`.
2023-05-30 06:41:09 +00:00
:::
2023-08-28 22:40:53 +00:00
0) Install [dependencies](https://hermesengine.dev/docs/building-and-running/#dependencies)
<details>
<summary><b>Installation Notes</b> (click to show)</summary>
2023-08-28 22:40:53 +00:00
2023-09-28 17:13:57 +00:00
The official guidance[^6] has been verified in macOS and HoloOS (Linux).
2023-08-28 22:40:53 +00:00
On macOS:
2023-06-05 20:12:53 +00:00
```bash
brew install icu4c cmake ninja
```
2023-08-28 22:40:53 +00:00
On HoloOS (and other Arch Linux distros):
```bash
sudo pacman -Syu cmake git ninja icu python zip readline
```
2023-08-31 22:09:08 +00:00
On Debian and Ubuntu:
```bash
sudo apt install cmake git ninja-build libicu-dev python zip libreadline-dev
```
2024-05-26 07:50:55 +00:00
:::note pass
When using virtual machines, Linux builds require at least 8 GB memory.
:::
2023-08-28 22:40:53 +00:00
</details>
2023-06-05 20:12:53 +00:00
1) Make a project directory:
2023-05-30 06:41:09 +00:00
```bash
mkdir sheetjs-hermes
cd sheetjs-hermes
```
2023-06-05 20:12:53 +00:00
2) Download the [`Makefile`](pathname:///hermes/Makefile):
2023-05-30 06:41:09 +00:00
```bash
curl -LO https://docs.sheetjs.com/hermes/Makefile
```
2023-06-05 20:12:53 +00:00
3) Download [`sheetjs-hermes.cpp`](pathname:///hermes/sheetjs-hermes.cpp):
2023-05-30 06:41:09 +00:00
```bash
curl -LO https://docs.sheetjs.com/hermes/sheetjs-hermes.cpp
```
2023-06-05 20:12:53 +00:00
4) Build the library (this is the `init` target):
2023-05-30 06:41:09 +00:00
```bash
make init
```
2023-11-16 04:20:57 +00:00
:::caution pass
2024-03-22 04:45:40 +00:00
In some test runs, the build failed due to Ninja issues:
2023-11-16 04:20:57 +00:00
```
CMake Error at CMakeLists.txt:64 (project):
Running
'/usr/local/lib/depot_tools/ninja' '--version'
failed with:
depot_tools/ninja.py: Could not find Ninja in the third_party of the current project, nor in your PATH.
```
This is due to a conflict with the Ninja version that ships with `depot_tools`.
Since `depot_tools` typically is added before other folders in the system `PATH`
variable, it is strongly recommended to rename the `ninja` binary, build the
Hermes libraries, and restore the `ninja` binary:
```bash
# Rename `ninja`
mv /usr/local/lib/depot_tools/ninja /usr/local/lib/depot_tools/ninja_tmp
# Build Hermes
make init
# Restore `ninja`
mv /usr/local/lib/depot_tools/ninja_tmp /usr/local/lib/depot_tools/ninja
```
:::
2024-03-22 04:45:40 +00:00
:::note pass
In some tests, the build failed with a message referencing a missing header:
```
hermes/API/hermes/inspector/chrome/tests/SerialExecutor.cpp:34:16: note: std::runtime_error is defined in header <stdexcept>; did you forget to #include <stdexcept>?
```
**This error affects the official Hermes releases!**
The fix is to manually add a `#include` statement in the corresponding header
file (`API/hermes/inspector/chrome/tests/SerialExecutor.h` in the repo):
```c title="hermes/API/hermes/inspector/chrome/tests/SerialExecutor.h (add highlighted line)"
#include <memory>
#include <mutex>
#if !defined(_WINDOWS) && !defined(__EMSCRIPTEN__)
// highlight-next-line
#include <stdexcept>
#include <pthread.h>
#else
#include <thread>
```
:::
2023-06-05 20:12:53 +00:00
5) Build the application:
2023-05-30 06:41:09 +00:00
```bash
make sheetjs-hermes
```
2023-09-22 06:32:55 +00:00
6) Download the SheetJS Standalone script and the test file. Save both files in
the project directory:
2023-05-30 06:41:09 +00:00
<ul>
<li><a href={`https://cdn.sheetjs.com/xlsx-${current}/package/dist/xlsx.full.min.js`}>xlsx.full.min.js</a></li>
2024-04-26 04:16:13 +00:00
<li><a href="https://docs.sheetjs.com/pres.numbers">pres.numbers</a></li>
2023-05-30 06:41:09 +00:00
</ul>
<CodeBlock language="bash">{`\
curl -LO https://cdn.sheetjs.com/xlsx-${current}/package/dist/xlsx.full.min.js
2024-04-26 04:16:13 +00:00
curl -LO https://docs.sheetjs.com/pres.numbers`}
2023-05-30 06:41:09 +00:00
</CodeBlock>
2024-01-29 03:29:45 +00:00
7) Copy the `libhermes` and `libjsi` libraries into the current folder:
<Tabs groupId="os">
<TabItem value="linux" label="Linux">
```bash
cp ./build_release/API/hermes/libhermes.so .
cp ./build_release/jsi/libjsi.so .
```
</TabItem>
<TabItem value="darwin" label="MacOS">
```bash
cp ./build_release/API/hermes/libhermes.dylib .
cp ./build_release/jsi/libjsi.dylib .
```
</TabItem>
</Tabs>
8) Run the application:
2023-05-30 06:41:09 +00:00
```bash
./sheetjs-hermes pres.numbers
```
If successful, the program will print the library version number and the
contents of the first sheet as CSV rows.
2023-09-28 17:13:57 +00:00
### Windows Example
2024-06-21 07:54:27 +00:00
:::info pass
On ARM64, the commands must be run in a "ARM64 Native Tools Command Prompt".
:::
2023-09-28 17:13:57 +00:00
0) Install dependencies.
<details>
<summary><b>Installation Notes</b> (click to show)</summary>
2023-09-28 17:13:57 +00:00
2024-03-25 04:13:01 +00:00
The build sequence requires Python, which can be installed from the official
Windows installer[^7].
2023-09-28 17:13:57 +00:00
2024-03-25 04:13:01 +00:00
Visual Studio with "Desktop development with C++" workload and Cmake must be
installed[^8]. In addition, the following Spectre-mitigated libs must be added:
- MSVC C++ x64/x86 Spectre-mitigated libs (Latest)
- C++ ATL for latest build tools with Spectre Mitigations (x86 & x64)
- C++ MFC for latest build tools with Spectre Mitigations (x86 & x64)
2023-09-28 17:13:57 +00:00
The easiest way to install is to select "Individual components" and search for
"spectre latest" (no quotation marks). Pick each option for the relevant CPU.
</details>
1) Set up `depot_tools`.
[`depot_tools.zip`](https://storage.googleapis.com/chrome-infra/depot_tools.zip)
must be downloaded and extracted to `c:\src\depot_tools\`.
:::note pass
This ZIP has a number of hidden files and folders (including `.git`) which
should be extracted along with the normal files.
:::
Add the path `c:\src\depot_tools\` to the User `PATH` environment variable
<details>
<summary><b>Environment Variable Setup</b> (click to show)</summary>
2023-09-28 17:13:57 +00:00
Type `env` in the search bar and select "Edit the system environment variables".
In the new window, click the "Environment Variables..." button.
In the new window, look for the "User variables" section. Select "Path" in the
list and click "Edit".
In the new window, click "New" and type `c:\src\depot_tools` and press Enter.
Select the row and repeatedly click "Move Up" until it is the first entry.
Click "OK" in each window (3 windows) and restart your computer.
</details>
2) Delete `c:\src\depot_tools\ninja` if it exists, then download the
[official Windows release](https://github.com/ninja-build/ninja/releases/download/v1.11.1/ninja-win.zip)
and move the `ninja.exe` into `c:\src\depot_tools`. If a `ninja.exe` exists in
the folder, replace the existing program.
3) Make a project directory:
```bash
mkdir sheetjs-hermes
cd sheetjs-hermes
```
4) Clone the `hermes-windows` repo:
```bash
git clone https://github.com/microsoft/hermes-windows
cd hermes-windows
2024-03-25 04:13:01 +00:00
git checkout 240573e
2023-09-28 17:13:57 +00:00
cd ..
```
:::note pass
If there are errors related to SSL or certificates or `CApath`, temporarily
disable SSL in Git:
```bash
git config --global http.sslVerify false
git clone https://github.com/microsoft/hermes-windows
git config --global http.sslVerify true
```
:::
5) Build the library:
2024-06-21 07:54:27 +00:00
<Tabs groupId="arch">
<TabItem value="x64" label="x64">
2023-09-28 17:13:57 +00:00
```bash
cd hermes-windows
.\.ado\scripts\cibuild.ps1 -AppPlatform win32 -Platform x64 -ToolsPlatform x64
cd ..
```
:::note pass
The script may fail with the message:
> cannot be loaded because running scripts is disabled on this system
In a "Run as Administrator" powershell window, run the following command:
```
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned
```
:::
2023-10-29 03:22:50 +00:00
:::info pass
2024-03-25 04:13:01 +00:00
In some test runs, the command failed when trying to copy `hermes.exe`:
2023-10-29 03:22:50 +00:00
```
Copy-Item: C:\Users\Me\Documents\hermes-windows\.ado\scripts\cibuild.ps1:331
Line |
331 | Copy-Item "$compilerAndToolsBuildPath\bin\hermes.exe" -Destinatio …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| Cannot find path 'C:\Users\Me\Documents\hermes-windows\workspace\build\tools\bin\hermes.exe'
| because it does not exist.
```
The libraries are built first and the standalone binary is not needed when
embedding Hermes, so the error message can be safely ignored.
:::
2024-06-21 07:54:27 +00:00
</TabItem>
<TabItem value="arm" label="ARM64">
```bash
cmake -S hermes-windows -B build -G "Visual Studio 17 2022" -A arm64
cmake --build ./build
```
</TabItem>
</Tabs>
2023-09-28 17:13:57 +00:00
6) Copy every generated `.lib` and `.dll` file into the main folder:
2024-06-21 07:54:27 +00:00
<Tabs groupId="arch">
<TabItem value="x64" label="x64">
2023-09-28 17:13:57 +00:00
```powershell
dir -r -Path .\hermes-windows\workspace\build\win32-x64-debug\ -Filter "*.dll" | Copy-Item -Destination .\
dir -r -Path .\hermes-windows\workspace\build\win32-x64-debug\ -Filter "*.lib" | Copy-Item -Destination .\
```
2024-06-21 07:54:27 +00:00
</TabItem>
<TabItem value="arm" label="ARM64">
Run the following commands in a PowerShell session:
```powershell
dir -r -Path .\build -Filter "*.dll" | Copy-Item -Destination .\
dir -r -Path .\build -Filter "*.lib" | Copy-Item -Destination .\
```
</TabItem>
</Tabs>
2023-09-28 17:13:57 +00:00
7) Download [`sheetjs-hermes.cpp`](pathname:///hermes/sheetjs-hermesw.cpp):
```bash
2024-03-25 04:13:01 +00:00
curl -o sheetjs-hermesw.cpp https://docs.sheetjs.com/hermes/sheetjs-hermesw.cpp
2023-09-28 17:13:57 +00:00
```
8) Build the application:
```powershell
cl /MDd sheetjs-hermesw.cpp DbgHelp.lib *.lib /I hermes-windows\API /I hermes-windows\include /I hermes-windows\public\ /I hermes-windows\API\jsi
```
:::caution pass
If `cl` is not found, run the command in the "Native Tools Command Prompt"
:::
9) Download the SheetJS Standalone script and the test file. Save both files in
the project directory:
<ul>
<li><a href={`https://cdn.sheetjs.com/xlsx-${current}/package/dist/xlsx.full.min.js`}>xlsx.full.min.js</a></li>
2024-04-26 04:16:13 +00:00
<li><a href="https://docs.sheetjs.com/pres.numbers">pres.numbers</a></li>
2023-09-28 17:13:57 +00:00
</ul>
<CodeBlock language="bash">{`\
2024-03-25 04:13:01 +00:00
curl -o xlsx.full.min.js https://cdn.sheetjs.com/xlsx-${current}/package/dist/xlsx.full.min.js
2024-04-26 04:16:13 +00:00
curl -o pres.numbers https://docs.sheetjs.com/pres.numbers`}
2023-09-28 17:13:57 +00:00
</CodeBlock>
10) Run the application:
```bash
.\sheetjs-hermesw.exe pres.numbers
```
If successful, the program will print the library version number and the
contents of the first sheet as CSV rows.
2023-05-30 06:41:09 +00:00
### CLI Test
2023-11-04 05:05:26 +00:00
:::note Tested Deployments
2023-05-30 06:41:09 +00:00
2024-03-14 08:25:08 +00:00
This demo was tested in the following deployments:
| Architecture | Hermes | Date |
|:-------------|:---------|:-----------|
| `darwin-x64` | `0.12.0` | 2024-03-13 |
2023-05-30 06:41:09 +00:00
:::
Due to limitations of the standalone binary, this demo will encode a test file
as a Base64 string and directly add it to an amalgamated script.
2023-11-04 05:05:26 +00:00
#### Install CLI
0) Install the Hermes command line tools:
```bash
2024-01-29 03:29:45 +00:00
npx jsvu hermes@0.12.0
2023-11-04 05:05:26 +00:00
```
When prompted, select the appropriate operating system.
1) Inspect the output of the installer. Look for "Installing binary" lines:
```text pass
Extracting…
// highlight-next-line
2024-01-29 03:29:45 +00:00
Installing binary to ~/.jsvu/engines/hermes-0.12.0/hermes-0.12.0…
Installing symlink at ~/.jsvu/bin/hermes-0.12.0 pointing to ~/.jsvu/engines/hermes-0.12.0/hermes-0.12.0…
Installing binary to ~/.jsvu/engines/hermes-0.12.0/hermes-0.12.0-compiler…
Installing symlink at ~/.jsvu/bin/hermes-0.12.0-compiler pointing to ~/.jsvu/engines/hermes-0.12.0/hermes-0.12.0-compiler…
2023-11-04 05:05:26 +00:00
```
The first "Installing binary" line mentions the path to the `hermes` tool.
#### Setup Project
2023-05-30 06:41:09 +00:00
2023-11-04 05:05:26 +00:00
2) Create a new project folder:
```bash
mkdir sheetjs-hermes-cli
cd sheetjs-hermes-cli
```
3) Copy the binary from Step 1 into the current folder. For example, on macOS:
```bash
2024-01-29 03:29:45 +00:00
cp ~/.jsvu/engines/hermes-0.12.0/hermes-0.12.0 .
2023-11-04 05:05:26 +00:00
```
#### Create Script
4) Download the SheetJS Standalone script and the test file. Save both files in
2023-09-22 06:32:55 +00:00
the project directory:
2023-05-30 06:41:09 +00:00
<ul>
<li><a href={`https://cdn.sheetjs.com/xlsx-${current}/package/dist/xlsx.full.min.js`}>xlsx.full.min.js</a></li>
2024-04-26 04:16:13 +00:00
<li><a href="https://docs.sheetjs.com/pres.numbers">pres.numbers</a></li>
2023-05-30 06:41:09 +00:00
</ul>
<CodeBlock language="bash">{`\
curl -LO https://cdn.sheetjs.com/xlsx-${current}/package/dist/xlsx.full.min.js
2024-04-26 04:16:13 +00:00
curl -LO https://docs.sheetjs.com/pres.numbers`}
2023-05-30 06:41:09 +00:00
</CodeBlock>
2023-11-04 05:05:26 +00:00
5) Bundle the test file and create `payload.js`:
2023-05-30 06:41:09 +00:00
```bash
node -e "fs.writeFileSync('payload.js', 'var payload = \"' + fs.readFileSync('pres.numbers').toString('base64') + '\";')"
```
2023-11-04 05:05:26 +00:00
6) Create support scripts:
2023-05-30 06:41:09 +00:00
- `global.js` creates a `global` variable and defines a fake `console`:
```js title="global.js"
var global = (function(){ return this; }).call(null);
var console = { log: function(x) { print(x); } };
```
- `hermes.js` will call `XLSX.read` and `XLSX.utils.sheet_to_csv`:
```js title="hermes.js"
var wb = XLSX.read(payload, {type:'base64'});
console.log(XLSX.utils.sheet_to_csv(wb.Sheets[wb.SheetNames[0]]));
```
2023-11-04 05:05:26 +00:00
7) Create the amalgamation `sheetjs.hermes.js`:
2023-05-30 06:41:09 +00:00
```bash
2023-11-04 05:05:26 +00:00
cat global.js xlsx.full.min.js payload.js hermes.js > sheetjs.hermes.js
2023-05-30 06:41:09 +00:00
```
The final script defines `global` before loading the standalone library. Once
ready, it will read the bundled test data and print the contents as CSV.
2023-11-04 05:05:26 +00:00
#### Testing
8) Run the script using the Hermes standalone binary:
2023-05-30 06:41:09 +00:00
```bash
2024-01-29 03:29:45 +00:00
./hermes-0.12.0 sheetjs.hermes.js
2023-05-30 06:41:09 +00:00
```
2023-08-03 02:49:32 +00:00
If successful, the script will print CSV data from the test file.
[^1]: See [`read` in "Reading Files"](/docs/api/parse-options)
[^2]: See ["Workbook Object"](/docs/csf/book)
[^3]: See ["Workbook Object"](/docs/csf/book)
[^4]: See [`sheet_to_csv` in "Utilities"](/docs/api/utilities/csv#csv-output)
2023-09-28 17:13:57 +00:00
[^5]: See [`microsoft/hermes-windows`](https://github.com/microsoft/hermes-windows) on GitHub
2024-03-25 04:13:01 +00:00
[^6]: See ["Dependencies" in "Building and Running"](https://hermesengine.dev/docs/building-and-running/#dependencies) in the Hermes Documentation
[^7]: See ["Download Python"](https://www.python.org/downloads/) in the Python website.
[^8]: See [the Visual Studio website](https://visualstudio.microsoft.com/#vs-section) for download links.