目录

JSVM-API Overview

Introduction

HarmonyOS JSVM-API provides a set of stable APIs based on the standard JavaScript (JS) engine. It supports key functionalities such as creating and destroying engines, executing JS code, and implementing interaction between JS and C/C++ modules.

HarmonyOS JSVM-API provides a set of APIs written in C programming language that complies with C99.

HarmonyOS JSVM-API is not thread-safe. You need to ensure the thread safety of JSVM-API.

JSVM-API allows dynamically loaded JS code to be directly run during application runtime. With JSVM-API, you can also use C/C++ to implement core functionalities that demand high performance or closely rely on underlying system invocation, register C++ methods in JS code, and directly call them in JS code to improve the execution speed.

Unless otherwise specified, JSVM-API in this document refers to HarmonyOS JSVM-API.

JSVM-API only supports interaction between JS and C/C++. To implement interaction between ArkTS and C/C++, see Node-API Overview.

JSVM-API Architecture

Figure 1 JSVM-API architecture

JSVM-API Interaction Process

Figure 2 Key interaction process of JSVM-API

The interaction between JSVM-API and a native module is as follows:

  1. Initialization: Initialize the JSVM and JS context on the native module and register native functions in JS code. The native functions are hooked to GlobalThis, which is the global context of the JS execution environment.

  2. Invocation: When the JS side calls the methods registered in the JS global context via JSVM-API, the JS engine locates and invokes the corresponding C/C++ methods.