Skip to main content

JavaScript 引擎

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

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

JavaScript 代码无法在大多数现代计算机上直接执行。软件组件 ("JavaScript 引擎") 执行代码。嵌入 JS 引擎后,程序可以利用 SheetJS 库来处理电子表格和数据。

¥JavaScript code cannot be directly executed on most modern computers. A software component ("JavaScript engine") executes code. After embedding a JS engine, programs can leverage SheetJS libraries to process spreadsheets and data.

本节中的演示展示了许多 JS 引擎和语言绑定。在每种情况下,我们都将构建一个示例应用,该应用嵌入 JS 引擎、加载 SheetJS 库脚本以及读取和写入电子表格文件。

¥The demos in this section showcase a number of JS engines and language bindings. In each case, we will build a sample application that embeds a JS engine, loads SheetJS library scripts, and reads and writes spreadsheet files.

一般注意事项

¥General Caveats

有许多具有不同设计目标的 JS 引擎。有些是专为低功耗或低内存环境而设计的。其他目标是与特定编程语言或环境的互操作性。通常它们支持 ES3 并且能够运行 SheetJS 代码。

¥There are many JS engines with different design goals. Some are designed for low-power or low-memory environments. Others aim for interoperability with specific programming languages or environments. Typically they support ES3 and are capable of running SheetJS code.

轻量级 JS 引擎通常缺少通用浏览器和 NodeJS API。

¥Common browser and NodeJS APIs are often missing from light-weight JS engines.

全局的

¥Global

有些发动机不提供 globalThisglobalwindowglobal 变量可以在应在 JS 引擎中运行的一行中公开:

¥Some engines do not provide globalThis or global or window. A global variable can be exposed in one line that should be run in the JS engine:

var global = (function(){ return this; }).call(null);

控制台

¥Console

某些引擎不提供 console 对象,但提供其他方式打印到标准输出。例如,Hermes[^1] 提供 print()。应使用引擎打印函数创建 console 对象:

¥Some engines do not provide a console object but offer other ways to print to standard output. For example, Hermes[^1] provides print(). A console object should be created using the engine print function:

var console = { log: function(x) { print(x); } };

二进制数据

¥Binary Data

某些引擎不提供交换二进制数据的简单方法。例如,某些库传递以 null 结尾的数组,这会截断 XLSX、XLS 和其他导出。应避免接受无长度指针的 API。

¥Some engines do not provide easy ways to exchange binary data. For example, some libraries pass null-terminated arrays, which would truncate XLSX, XLS, and other exports. APIs that accept pointers without length should be avoided.

Base64 字符串是安全的,因为它们不使用空字符,但仅应在没有安全方法传递 ArrayBufferUint8Array 对象时使用。SheetJS read[^2] 和 write[^3] 方法直接支持 Base64 字符串。

¥Base64 strings are safe, as they do not use null characters, but should only be used when there is no safe way to pass ArrayBuffer or Uint8Array objects. The SheetJS read[^2] and write[^3] methods directly support Base64 strings.

字节约定

¥Byte Conventions

Java 本身没有无符号字节的概念。byte[] 中的值限制在 -128 .. 127 范围内。它们需要在 JS 引擎中修复。

¥Java has no native concept of unsigned bytes. Values in a byte[] are limited to the range -128 .. 127. They need to be fixed within the JS engine.

某些引擎支持类型化数组。Uint8Array 构造函数将修复值:

¥Some engines support typed arrays. The Uint8Array constructor will fix values:

var signed_data = [-48, -49, 17, -32, /* ... */]; // 0xD0 0xCF 0x11 0xE0 ...
var fixed_data = new Uint8Array(signed_data);

当不支持 Uint8Array 时,可以通过按位运算固定值:

¥When Uint8Array is not supported, values can be fixed with bitwise operations:

var signed_data = [-48, -49, 17, -32, /* ... */]; // 0xD0 0xCF 0x11 0xE0 ...
var fixed_data = new Array(signed_data.length);
for(var i = 0; i < signed_data.length; ++i) fixed_data[i] = signed_data[i] & 0xFF;

引擎

¥Engines

演示在多个操作系统(Windows、MacOS 和 Linux)和多种架构(x64 和 ARM64)上进行了测试。

¥Demos are tested across multiple operating systems (Windows, MacOS and Linux) across multiple architectures (x64 and ARM64).

The following engines have been tested in their native languages:

The following bindings have been tested:

Asterisks (✱) in the Windows columns mark tests that were run in Windows Subsystem for Linux (WSL)

Boa

Boa 是一个用 Rust 编写的嵌入式 JS 引擎。

¥Boa is an embeddable JS engine written in Rust.

该演示已移至 到专用页面

¥This demo has been moved to a dedicated page.

ChakraCore

ChakraCore 是一个用 C++ 编写的嵌入式 JS 引擎。

¥ChakraCore is an embeddable JS engine written in C++.

该演示已移至 到专用页面

¥This demo has been moved to a dedicated page.

Duktape

Duktape 是一个用 C 编写的嵌入式 JS 引擎。它已被移植到许多奇异的体系结构和操作系统。

¥Duktape is an embeddable JS engine written in C. It has been ported to a number of exotic architectures and operating systems.

该演示已移至 到专用页面。该演示包括 C、Perl、PHP、Python 和 Zig 语言的示例。

¥This demo has been moved to a dedicated page. The demo includes examples in C, Perl, PHP, Python and Zig.

Goja

Goja 是 ECMAScript 5 的纯 Go 实现。

¥Goja is a pure Go implementation of ECMAScript 5.

该演示已移至 到专用页面

¥This demo has been moved to a dedicated page.

Hermes

Hermes 是一个用 C++ 编写的嵌入式 JS 引擎。

¥Hermes is an embeddable JS engine written in C++.

该演示已移至 到专用页面

¥This demo has been moved to a dedicated page.

JavaScript 核心

¥JavaScriptCore

iOS 和 MacOS 附带了 JavaScriptCore 框架,用于从 Swift 和 Objective-C 运行 JS 代码。

¥iOS and MacOS ship with the JavaScriptCore framework for running JS code from Swift and Objective-C.

该演示已移至 到专用页面

¥This demo has been moved to a dedicated page.

JerryScript

JerryScript 是一种轻量级 JavaScript 引擎,设计用于包括微控制器在内的低内存环境。

¥JerryScript is a lightweight JavaScript engine designed for use in low-memory environments including microcontrollers.

该演示已移至 到专用页面

¥This demo has been moved to a dedicated page.

Jint

Jint 是一个用 C# 编写的 .NET 嵌入式 JS 引擎。

¥Jint is an embeddable JS engine for .NET written in C#.

该演示已移至 到专用页面

¥This demo has been moved to a dedicated page.

Nashorn

Nashorn 随某些版本的 Java 一起提供。它现在是一个独立的库。

¥Nashorn shipped with some versions of Java. It is now a standalone library.

该演示已移至 到专用页面

¥This demo has been moved to a dedicated page.

QuickJS

QuickJS 是一个用 C 编写的嵌入式 JS 引擎。它提供了一组单独的函数用于与文件系统和全局对象交互。它可以运行独立的浏览器脚本。

¥QuickJS is an embeddable JS engine written in C. It provides a separate set of functions for interacting with the filesystem and the global object. It can run the standalone browser scripts.

该演示已移至 到专用页面

¥This demo has been moved to a dedicated page.

Rhino

Rhino 是 Java 中的 ES3+ 引擎。

¥Rhino is an ES3+ engine in Java.

该演示已移至 到专用页面

¥This demo has been moved to a dedicated page.

V8

V8 是一个用 C++ 编写的嵌入式 JS 引擎。它为 Chromium 和 Chrome、NodeJS 和 Deno、Adobe UXP 和其他平台提供支持。

¥V8 is an embeddable JS engine written in C++. It powers Chromium and Chrome, NodeJS and Deno, Adobe UXP and other platforms.

该演示已移至 到专用页面。该演示包括 C++ 和 Rust 的示例。

¥This demo has been moved to a dedicated page. The demo includes examples in C++ and Rust.

"Python + 熊猫" 演示 使用 V8 和 Python。

¥The "Python + Pandas" demo uses V8 with Python.

[^1]: 请参阅 Hermes 演示中的 "初始化 Hermes"

¥See "Initialize Hermes" in the Hermes demo.

[^2]: 见 read 于 "读取文件"

¥See read in "Reading Files"

[^3]: 见 write 于 "写入文件"

¥See write in "Writing Files"