Repository navigation
Add booleans and strings to JsValue in the C FFI - #5693
jasonmilad wants to merge 2 commits into
Conversation
Test262 conformance changes
Tested main commit: |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5693 +/- ##
===========================================
+ Coverage 47.24% 63.06% +15.82%
===========================================
Files 476 538 +62
Lines 46892 58600 +11708
===========================================
+ Hits 22154 36959 +14805
+ Misses 24738 21641 -3097 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
abdulhamid999
left a comment
There was a problem hiding this comment.
Reviewed ffi/boa_ffi/src/lib.rs, ffi/boa_ffi/README.md, generated headers in ffi/boa_ffi/bindings/c/, and the Rust/C tests in tests/c/ (static inspection; not compiled locally).
Findings
- Non-coercive boolean & string APIs:
from_boolean/as_booleanandfrom_string/as_string/as_string_lossycleanly avoid JS type coercion, preserve embedded NUL bytes via explicit slice length (DiplomatStr), reject invalid UTF-8 withNULL, and separateJsStringError::NotAStringfromJsStringError::InvalidEncoding(unpaired UTF-16 surrogates) while providing lossyU+FFFDreplacement inas_string_lossy. - Test coverage & memory/lifetime safety: Rust tests and C tests (
tests/c/value.c,tests/c/error.c) thoroughly exercise Latin1/UTF-16/emoji/NUL round trips, caller-buffer mutation afterfrom_string, NULL data with length 0, fixed vs growableDiplomatWritebuffers, and primitive lifetime afterBoaContextdestruction. - Docstring completeness nit (
ffi/boa_ffi/src/lib.rs):README.mdclearly documents checkingwrite.grow_failedon fixed stack buffers (diplomat_simple_write), andtests/c/value.cverifies thatsmall_res.is_okremainstruewhilesmall_write.grow_failedistrueandlen == 0. However, the doc comments onJsValue::as_stringandJsValue::as_string_lossyinsrc/lib.rsonly mention checkingis_ok(notwrite.grow_failed). Adding a brief note there aboutwrite.grow_failedfor fixed stack buffers would keep the Rust/header docs aligned withREADME.md.
Clean addition to the FFI surface overall!
|
Thanks for the review! Good catch, I added a note about checking |
This Pull Request is related to #332. It follows #5618 (and is independent of #5689) by adding boolean and UTF-8 string support to
JsValue, so C callers can construct and read those primitives without needing a context.It changes the following:
boa_JsValue_from_booleanandboa_JsValue_as_boolean.as_booleandoes not coerce truthy or falsy non-boolean values.boa_JsValue_from_string, which validates UTF-8 and copies the inputDiplomatStringViewusing its explicit byte length (including any'\0'bytes), returningNULLon malformed UTF-8.boa_JsValue_as_stringandboa_JsValue_as_string_lossy, encoding Boa strings as UTF-8 into a caller-providedDiplomatWritebuffer without coercion:as_stringreturns aJsStringError(NotAStringorInvalidEncodingfor unpaired UTF-16 surrogates) without writing to the buffer on failure.as_string_lossyreplaces unpaired surrogates withU+FFFDand only fails on non-strings.write_strcall so fixed buffers fail cleanly on overflow anddiplomat_buffer_write_creategrows at most once.ffi/boa_ffi/bindings/c/withdiplomat-gen.tests/c/value.candtests/c/error.c), and updatedffi/boa_ffi/README.md.