Skip to main content

Rust + Boa

在生产应用中,强烈建议使用性能更高的引擎(如 v8)的绑定

¥In a production application, it is strongly recommended to use a binding for a more performant engine like v8

Boa 是一个用 Rust 编写的 JavaScript 引擎。

¥Boa is a JavaScript engine written in Rust.

SheetJS 是一个用于从电子表格读取和写入数据的 JavaScript 库。

¥SheetJS is a JavaScript library for reading and writing data from spreadsheets.

"完整示例" 部分创建一个命令行工具,用于从电子表格读取数据并生成 CSV 行。

¥The "Complete Example" section creates a command-line tool for reading data from spreadsheets and generating CSV rows.

集成详情

¥Integration Details

初始化 Boa

¥Initialize Boa

一个 JS 上下文可以用一行构建:

¥A JS context can be constructed in one line:

use boa_engine::Context;

/* initialize */
let context = &mut Context::default();

以下辅助函数将字符串计算为 JS 代码:

¥The following helper function evaluates strings as JS code:

use std::string::String;
use boa_engine::{Context, Source, JsError};

/* simple wrapper to evaluate code snippets */
fn eval_code(c: &mut Context, code: &str) -> Result<String, JsError> {
let src = Source::from_bytes(code);
match c.eval(src) {
Ok(res) => { return Ok(res.to_string(c).unwrap().to_std_string_escaped()); }
Err(e) => { return Err(e); }
};
}

加载 SheetJS 脚本

¥Load SheetJS Scripts

SheetJS 独立脚本 可以在 Boa 上下文中解析和评估。

¥The SheetJS Standalone scripts can be parsed and evaluated in a Boa context.

Boa 提供了一个特殊的辅助程序来从路径读取源代码:

¥Boa provides a special helper to read source code from a path:

use std::path::Path;
use std::string::String;
use boa_engine::{js_string, Context, Source, JsError};

/* simple wrapper to evaluate an entire script file */
fn eval_file(c: &mut Context, path: &str) -> Result<String, JsError> {
let src = Source::from_filepath(Path::new(path)).unwrap();
match c.eval(src) {
Ok(res) => { return Ok(res.to_string(c).unwrap().to_std_string_escaped()); }
Err(e) => { return Err(e); }
};
}

// ...
/* load library */
match eval_file(context, "./xlsx.full.min.js") {
Ok(_res) => {}
Err(e) => { return eprintln!("Uncaught {e}"); }
}

要确认库已加载,可以检查 XLSX.version

¥To confirm the library is loaded, XLSX.version can be inspected:

  /* get version string */
match eval_code(context, "XLSX.version") {
Ok(res) => { println!( "SheetJS library version {}", res); }
Err(e) => { return eprintln!("Uncaught {e}"); }
}

读取文件

¥Reading Files

Boa 原生支持 ArrayBuffer。此代码片段将文件中的数据读取到 Vec<u8> 中,并将数据作为 ArrayBuffer 存储在全局范围内:

¥Boa supports ArrayBuffer natively. This snippet reads data from a file into Vec<u8> and stores the data as an ArrayBuffer in global scope:

  /* read file */
let data: Vec<u8> = fs::read("pres.xlsx").unwrap();
let array: JsArrayBuffer = JsArrayBuffer::from_byte_block(data, context).unwrap();
let attrs = Attribute::WRITABLE | Attribute::ENUMERABLE | Attribute::CONFIGURABLE;
context.register_global_property(js_string!("buf"), array, attrs);

/* parse with SheetJS */
match eval_code(context, "void (globalThis.wb = XLSX.read(buf))") {
Ok(_res) => { }
Err(e) => { return eprintln!("Uncaught {e}"); }
}

wb 将是 JS 环境中的一个变量,可以使用各种 SheetJS API 函数进行检查。

¥wb will be a variable in the JS environment that can be inspected using the various SheetJS API functions.

完整示例

¥Complete Example

测试部署

该演示在以下部署中进行了测试:

¥This demo was tested in the following deployments:

架构Boa日期
darwin-x640.18.02024-04-25
darwin-arm0.18.02024-05-23
win10-x640.18.02024-04-25
win11-arm0.18.02024-05-25
linux-x640.18.02024-03-21
linux-arm0.18.02024-05-25
  1. 安装 Rust。

    ¥Install Rust.

Boa 0.18.0 需要 Rust 版本 1.67 或更高版本。

¥Boa 0.18.0 requires Rust version 1.67 or later.

Debian 12(Bullseye)附带 Rust 版本 1.63.0

¥Debian 12 (Bullseye) ships with Rust version 1.63.0.

强烈建议从官方发行版安装 Rust。

¥It is strongly recommended to install Rust from the official distribution.

  1. 创建一个新项目:

    ¥Create a new project:

cargo new sheetjs-boa
cd sheetjs-boa
cargo run
  1. 添加 boa_engine 箱子:

    ¥Add the boa_engine crate:

cargo add boa_engine
  1. 下载 SheetJS 独立脚本和测试文件。将这两个文件保存在项目目录中:

    ¥Download the SheetJS Standalone script and test file. Save both files in the project directory:

curl -LO https://cdn.sheetjs.com/xlsx-0.20.3/package/dist/xlsx.full.min.js
curl -LO https://xlsx.nodejs.cn/pres.xlsx
  1. 下载 main.rs 并替换 src/main.rs

    ¥Download main.rs and replace src/main.rs:

curl -L -o src/main.rs https://xlsx.nodejs.cn/boa/main.rs
  1. 在发布模式下构建并运行应用:

    ¥Build and run the app in release mode:

cargo run --release

稍等片刻后,内容将以 CSV 形式显示。

¥After a short wait, the contents will be displayed in CSV form.

默认调试版本未优化,可能会引发堆栈溢出错误。强烈建议尽可能使用 --release

¥The default debug build is not optimized and can elicit stack overflow errors. It is strongly encouraged to use --release when possible.