Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions devtools/devtools-integration/native/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ target_include_directories(${PROJECT_NAME} PUBLIC include)
target_compile_options(${PROJECT_NAME} PRIVATE ${COMPILE_OPTIONS})

# region vm
if ("${JS_ENGINE}" STREQUAL "V8")
if ("${JS_ENGINE}" STREQUAL "V8" OR "${JS_ENGINE}" STREQUAL "HERMES_V8")
if (NOT TARGET v8)
message(FATAL_ERROR "The V8 target not found, peer dependency not satisfied")
endif()
Expand All @@ -45,9 +45,11 @@ if ("${JS_ENGINE}" STREQUAL "V8")
if (V8_WITHOUT_INSPECTOR)
target_compile_definitions(${PROJECT_NAME} PRIVATE "V8_WITHOUT_INSPECTOR")
endif ()
elseif ("${JS_ENGINE}" STREQUAL "JSC")
endif ()

if ("${JS_ENGINE}" STREQUAL "JSC")
target_compile_definitions(${PROJECT_NAME} PRIVATE "JS_JSC")
elseif ("${JS_ENGINE}" STREQUAL "HERMES")
elseif ("${JS_ENGINE}" STREQUAL "HERMES_ONLY" OR "${JS_ENGINE}" STREQUAL "HERMES_V8")
target_compile_definitions(${PROJECT_NAME} PRIVATE "JS_HERMES")
endif ()
# endregion
Expand Down
41 changes: 39 additions & 2 deletions docs/development/use-hermes-engine.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,46 @@ Hippy 从 `3.4.0` 版本开始支持 Hermes 引擎。本文档将指导你如何
>
> Tips 2: `HippyLaunchOptions` 中 `useHermesEngine` 的默认值为 `NO`,因此如果需要启用 Hermes 引擎,则必须显式地将其设置为 `YES`,否则项目将继续使用 JSC 引擎。

### Android 平台 (Alpha版本)
### Android 平台

由于 Android 平台的 Hermes 引擎切换目前处于 Alpha 阶段,我们将在未来版本中提供详细指引,请关注后续更新。
#### 1. 编译时选择引擎

**修改 `gradle.properties`**:

在项目根目录的 `gradle.properties` 文件中配置 JavaScript 引擎:

```properties
# 可选值:V8, HERMES_ONLY
CPP_JS_ENGINE=HERMES_ONLY

# 是否启用调试功能
CPP_ENABLE_INSPECTOR=true
```

**引擎配置说明**:
- `V8`:仅使用 V8 引擎(默认)
- `HERMES_ONLY`:仅使用 Hermes 引擎
- `HERMES_V8`:同时支持 V8 和 Hermes 引擎(并行构建,该模式目前暂不支持)

#### 2. 运行时动态切换

在创建 HippyEngine 实例时,可以通过 `EngineInitParams` 动态选择引擎:

```java
EngineInitParams params = new EngineInitParams();
params.context = context;
params.debugMode = false;

// 选择使用 Hermes 引擎
params.useHermesEngine = true;
params.jsEngineType = "hermes";

// 或者选择使用 V8 引擎
// params.useHermesEngine = false;
// params.jsEngineType = "v8";

HippyEngine engine = HippyEngine.create(params);
```

## 前端切换步骤

Expand Down
69 changes: 67 additions & 2 deletions driver/js/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,71 @@ endif ()

if ("${JS_ENGINE}" STREQUAL "HERMES_ONLY" OR "${JS_ENGINE}" STREQUAL "HERMES_V8")
if ("${CMAKE_SYSTEM_NAME}" STREQUAL "Android")
set(HERMES_REMOTE_FILENAME "android.tgz")
set(HERMES_REMOTE_FILENAME "hermes-runtime-android-v0.12.30302.3.tar.gz")
else ()
message(FATAL_ERROR "Unsupported system ${CMAKE_SYSTEM_NAME}")
endif ()
InfraPackage_Add(Hermes
REMOTE "global_packages/hermes/${HERMES_COMPONENT}/${HERMES_REMOTE_FILENAME}"
LOCAL "${HERMES_COMPONENT}")
target_link_libraries(${PROJECT_NAME} PUBLIC hermes)

# Standard Hermes package structure
set(HERMES_INCLUDE_DIR "${hermes_SOURCE_DIR}/include")
set(HERMES_LIB_DIR "${hermes_SOURCE_DIR}/unstripped-debug/lib")

# Set Hermes include directories
if(EXISTS "${HERMES_INCLUDE_DIR}/hermes")
target_include_directories(${PROJECT_NAME} PUBLIC "${HERMES_INCLUDE_DIR}")
message(STATUS "Added hermes include directory: ${HERMES_INCLUDE_DIR}")
else()
message(FATAL_ERROR "Hermes include directory not found: ${HERMES_INCLUDE_DIR}")
endif()

# Set Hermes library directory for current ABI
set(HERMES_ABI_LIB_DIR "${HERMES_LIB_DIR}/${ANDROID_ABI}")

# Debug: Print the hermes configuration
message(STATUS "Hermes configuration:")
message(STATUS " Source dir: ${hermes_SOURCE_DIR}")
message(STATUS " Include dir: ${HERMES_INCLUDE_DIR}")
message(STATUS " Library dir: ${HERMES_ABI_LIB_DIR}")

# Check if ABI library directory exists
if(NOT EXISTS "${HERMES_ABI_LIB_DIR}")
message(FATAL_ERROR "Hermes library directory not found: ${HERMES_ABI_LIB_DIR}")
endif()

# Link all Hermes libraries (using full paths for static linking effect)
set(HERMES_LIBRARIES
"${HERMES_ABI_LIB_DIR}/libhermes.so"
"${HERMES_ABI_LIB_DIR}/libjsi.so"
"${HERMES_ABI_LIB_DIR}/libfbjni.so"
"${HERMES_ABI_LIB_DIR}/libc++_shared.so"
)

# Verify all libraries exist before linking
foreach(LIB ${HERMES_LIBRARIES})
if(NOT EXISTS "${LIB}")
message(FATAL_ERROR "Hermes library not found: ${LIB}")
endif()
endforeach()

# Link all libraries at once
target_link_libraries(${PROJECT_NAME} PUBLIC ${HERMES_LIBRARIES})
message(STATUS "Linked Hermes libraries: libhermes.so, libjsi.so, libfbjni.so, libc++_shared.so")

target_compile_definitions(${PROJECT_NAME} PUBLIC "JS_HERMES")

# Enable RTTI and exceptions for specific source files to ensure ABI compatibility
# This is necessary for HERMES_V8 mode to work correctly
set_source_files_properties(
# Hermes-specific files
src/napi/hermes/hermes_ctx.cc
src/napi/hermes/hermes_ctx_value.cc
src/napi/hermes/hermes_try_catch.cc
src/vm/hermes/hermes_vm.cc
PROPERTIES COMPILE_FLAGS "-frtti -fexceptions"
)
elseif ("${JS_ENGINE}" STREQUAL "JSH")
target_compile_definitions(${PROJECT_NAME} PUBLIC "JS_JSH")
endif ()
Expand Down Expand Up @@ -233,6 +289,15 @@ if("${JS_ENGINE}" STREQUAL "HERMES_ONLY" OR "${JS_ENGINE}" STREQUAL "HERMES_V8")
else()
message(FATAL_ERROR "Unsupported system ${CMAKE_SYSTEM_NAME} by hermes engine")
endif()

# 为Hermes专用文件添加RTTI和异常支持
set_source_files_properties(
src/napi/hermes/hermes_ctx.cc
src/napi/hermes/hermes_ctx_value.cc
src/napi/hermes/hermes_try_catch.cc
src/vm/hermes/hermes_vm.cc
PROPERTIES COMPILE_FLAGS "-frtti -fexceptions"
)
endif ()

set(SOURCE_SET_STANDALONE
Expand Down
4 changes: 4 additions & 0 deletions driver/js/include/driver/js_driver_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ class JsDriverUtils {
string_view,
bool,
byte_string)>& callback);

// 引擎专用的工具函数,用于从slot中获取ScopeWrapper
static ScopeWrapper* GetScopeWrapperFromSlot(const std::any& slot);

static void LoadInstance(const std::shared_ptr<Scope>& scope, byte_string&& buffer_data);
static void UnloadInstance(const std::shared_ptr<Scope>& scope, byte_string&& buffer_data);

Expand Down
1 change: 1 addition & 0 deletions driver/js/include/driver/vm/js_vm.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class VM {
static inline const std::string kJSEngineV8 = "V8";
static inline const std::string kJSEngineJSC = "JSC";
static inline const std::string kJSEngineHermes = "Hermes";
static inline const std::string kJSEngineJSH = "JSH";

using string_view = footstone::string_view;
using Ctx = hippy::napi::Ctx;
Expand Down
Loading
Loading