Skip to content

fix(engine): replace JsRegExp js_expect with proper error handling#4915

Open
tkshsbcue wants to merge 3 commits intoboa-dev:mainfrom
tkshsbcue:fix/jsregexp-expect-to-error
Open

fix(engine): replace JsRegExp js_expect with proper error handling#4915
tkshsbcue wants to merge 3 commits intoboa-dev:mainfrom
tkshsbcue:fix/jsregexp-expect-to-error

Conversation

@tkshsbcue
Copy link
Contributor

fix(engine): replace JsRegExp js_expect with proper error handling

Summary

Replaces all js_expect() and expect() calls in the JsRegExp Rust API wrapper with proper JsResult-based error handling. The JsRegExp type is used by applications that embed Boa; previously, invariant violations would panic and crash the host process instead of returning recoverable errors.

Motivation

When Boa is embedded in a Rust application (e.g. via boa_engine), the JsRegExp API is the primary way to create and interact with RegExp objects from Rust. Methods like JsRegExp::new(), .global(), .test(), .exec(), etc. used js_expect() on the assumption that internal RegExp getters always return the expected types (boolean, string, object). If an invariant is ever violated—due to a bug, corrupted state, or future refactor—the host process panics. For embedded use cases, this is unacceptable: the embedder expects to handle errors via Result and continue execution. Replacing panics with proper JsResult propagation ensures the API is robust and embedder-friendly.

Changes

Category Description
Removed JsExpect import from jsregexp.rs
Replaced js_expect("...") with `ok_or_else(
Replaced to_object(context).js_expect(...) and JsArray::from_object(...).js_expect(...) with ? for exec()
Added JsError import for explicit error construction

Methods updated:

  • new()as_object().js_expectok_or_else with JsError::from
  • has_indices(), global(), ignore_case(), multiline(), dot_all(), unicode(), sticky()as_boolean().js_expectok_or_else
  • flags(), source(), to_string()as_string().js_expectok_or_else
  • test()as_boolean().js_expectok_or_else
  • exec()to_object().js_expect and from_object().js_expect? (propagate JsResult)

Technical Details

  • File modified: core/engine/src/object/builtins/jsregexp.rs
  • Lines changed: +73, -32
  • Behavioral impact: Invariant violations that previously panicked now return Err(JsError) with a TypeError message. Normal operation is unchanged; all existing tests pass.

The ok_or_else pattern converts Option<T> to Result<T, JsError> by constructing a JsNativeError::typ() with a descriptive message when the value is None. For exec(), to_object() and JsArray::from_object() already return JsResult, so ? is used to propagate errors.

Testing

  • cargo test -p boa_engine --lib — all 930 tests pass
  • cargo clippy -p boa_engine — no warnings
  • cargo build — successful compilation

Replace js_expect() and expect() in the JsRegExp Rust API with proper
JsResult propagation. This prevents host process crashes when Boa is
embedded and invariant violations occur (e.g. corrupted internal state).

- new(): as_object() -> ok_or_else with TypeError
- has_indices, global, ignore_case, multiline, dot_all, unicode, sticky:
  as_boolean() -> ok_or_else with TypeError
- flags, source, to_string: as_string() -> ok_or_else with TypeError
- test(): as_boolean() -> ok_or_else with TypeError
- exec(): to_object() and from_object() use ? for error propagation

All methods now return JsResult and propagate errors instead of panicking.

Made-with: Cursor
@tkshsbcue tkshsbcue requested a review from a team as a code owner March 7, 2026 02:56
@tkshsbcue
Copy link
Contributor Author

@jedel1043 i hope you can have a look
thanks!

@github-actions
Copy link

github-actions bot commented Mar 7, 2026

Test262 conformance changes

Test result main count PR count difference
Total 52,963 52,963 0
Passed 49,666 49,666 0
Ignored 2,284 2,284 0
Failed 1,013 1,013 0
Panics 0 0 0
Conformance 93.77% 93.77% 0.00%

Tested main commit: 2ebc979e4492a12a5361b9aaeb727183e1d25462
Tested PR commit: 418b97766a86dcef853f76e232b60713500485f1
Compare commits: 2ebc979...418b977

@codecov
Copy link

codecov bot commented Mar 7, 2026

Codecov Report

❌ Patch coverage is 7.31707% with 38 lines in your changes missing coverage. Please review.
✅ Project coverage is 57.76%. Comparing base (6ddc2b4) to head (418b977).
⚠️ Report is 773 commits behind head on main.

Files with missing lines Patch % Lines
core/engine/src/object/builtins/jsregexp.rs 7.31% 38 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##             main    #4915       +/-   ##
===========================================
+ Coverage   47.24%   57.76%   +10.51%     
===========================================
  Files         476      556       +80     
  Lines       46892    60793    +13901     
===========================================
+ Hits        22154    35116    +12962     
- Misses      24738    25677      +939     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant