fix(engine): replace JsRegExp js_expect with proper error handling#4915
Open
tkshsbcue wants to merge 3 commits intoboa-dev:mainfrom
Open
fix(engine): replace JsRegExp js_expect with proper error handling#4915tkshsbcue wants to merge 3 commits intoboa-dev:mainfrom
tkshsbcue wants to merge 3 commits intoboa-dev:mainfrom
Conversation
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
Contributor
Author
|
@jedel1043 i hope you can have a look |
Test262 conformance changes
Tested main commit: |
Codecov Report❌ Patch coverage is
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. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fix(engine): replace JsRegExp js_expect with proper error handling
Summary
Replaces all
js_expect()andexpect()calls in theJsRegExpRust API wrapper with properJsResult-based error handling. TheJsRegExptype 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), theJsRegExpAPI is the primary way to create and interact with RegExp objects from Rust. Methods likeJsRegExp::new(),.global(),.test(),.exec(), etc. usedjs_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 viaResultand continue execution. Replacing panics with properJsResultpropagation ensures the API is robust and embedder-friendly.Changes
JsExpectimport fromjsregexp.rsjs_expect("...")with `ok_or_else(to_object(context).js_expect(...)andJsArray::from_object(...).js_expect(...)with?forexec()JsErrorimport for explicit error constructionMethods updated:
new()—as_object().js_expect→ok_or_elsewithJsError::fromhas_indices(),global(),ignore_case(),multiline(),dot_all(),unicode(),sticky()—as_boolean().js_expect→ok_or_elseflags(),source(),to_string()—as_string().js_expect→ok_or_elsetest()—as_boolean().js_expect→ok_or_elseexec()—to_object().js_expectandfrom_object().js_expect→?(propagateJsResult)Technical Details
core/engine/src/object/builtins/jsregexp.rsErr(JsError)with aTypeErrormessage. Normal operation is unchanged; all existing tests pass.The
ok_or_elsepattern convertsOption<T>toResult<T, JsError>by constructing aJsNativeError::typ()with a descriptive message when the value isNone. Forexec(),to_object()andJsArray::from_object()already returnJsResult, so?is used to propagate errors.Testing
cargo test -p boa_engine --lib— all 930 tests passcargo clippy -p boa_engine— no warningscargo build— successful compilation