Skip to content

fix(jsi): complete V8Runtime NativeState, MutableBuffer ArrayBuffer, and ExternalMemoryAccounter support - #510

Closed
NathanWalker wants to merge 1 commit into
mainfrom
feat/jsi-nitromodules-runtime
Closed

NathanWalker wants to merge 1 commit into
mainfrom
feat/jsi-nitromodules-runtime

Conversation

@NathanWalker

@NathanWalker NathanWalker commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Completes key facebook::jsi::Runtime capabilities in rnv8::V8Runtime required by JSI consumers and NitroModules (HybridObject / Prototype / zero-copy ArrayBuffer / NativeState):

  1. Fix V8Runtime::setNativeState weak handle lifecycle & prototype preservation

    • weakV8Object in V8Runtime::setNativeState was previously stack-allocated (v8::Global<v8::Object> weakV8Object(isolate_, v8Object)), so its destructor ran when setNativeState returned, immediately resetting the global handle and cancelling the weak finalizer callback (leaking the heap-allocated std::shared_ptr<jsi::NativeState>).
    • Introduces NativeStateHolder on the heap with v8::WeakCallbackType::kParameter (matching HostObjectProxy::Finalizer) so std::shared_ptr<jsi::NativeState> is deterministically released when the V8 object is collected.
    • Preserves the object's prototype (v8ObjectOriginal->GetPrototype() -> v8Object->SetPrototype(context, prototype)) when replacing a 0-internal-field v8::Object with a 2-internal-field kNativeState instance, and updates NativeStateHolder::state in place when setNativeState is called again on an object that already holds kNativeState.
    • Guards GetInternalFieldType when internal field 0 is not a Uint32.
  2. Direct V8 prototype operations (createObjectWithPrototype, setPrototypeOf, getPrototypeOf)

    • Overrides createObjectWithPrototype, setPrototypeOf, and getPrototypeOf on V8Runtime using v8::Object::SetPrototype / GetPrototype directly.
    • Objects created via createObjectWithPrototype pre-allocate 2 internal fields initialized to InternalFieldType::kInvalid so subsequent setNativeState calls populate internal field 1 in place without replacing V8 object identity or invoking Object.assign.
  3. Implement V8Runtime::createArrayBuffer(std::shared_ptr<jsi::MutableBuffer>)

    • Replaces the std::logic_error("Not implemented") stub with a zero-copy v8::ArrayBuffer::NewBackingStore backed by the std::shared_ptr<jsi::MutableBuffer>, releasing the shared pointer in the BackingStore deleter callback when V8 collects or detaches the ArrayBuffer.
  4. Implement V8Runtime::setExternalMemoryPressure

    • Tracks per-object external memory pressure via v8::ExternalMemoryAccounter and a private key (v8::Private::ForApi), supporting repeated updates on the same object and decrementing the accounted bytes when the object is garbage-collected.

Summary by CodeRabbit

  • New Features
    • Added support for creating objects with a specified prototype and getting or setting an object’s prototype.
    • Improved handling of mutable buffers used to create ArrayBuffers, keeping their data available for as long as needed.
  • Bug Fixes
    • Improved validation of native state and internal-field values, preventing invalid values from causing assertion failures.
    • Updated external-memory accounting as tracked memory changes and is released.

@coderabbitai

coderabbitai Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f585502b-10b0-4580-b4bc-b685b4ed3f4f
📥 Commits

Reviewing files that changed from the base of the PR and between cb1d444 and 93eb39a.

📒 Files selected for processing (2)
  • NativeScript/v8runtime/V8Runtime.cpp
  • NativeScript/v8runtime/V8Runtime.h

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 1 remain after this review.

📝 Walkthrough

Walkthrough

V8Runtime adds object prototype operations, weakly finalized native-state holders, ArrayBuffer backing stores that retain mutable buffers, and external-memory tracking.

Changes

V8 runtime support

Layer / File(s) Summary
Native-state holder lifecycle
NativeScript/v8runtime/V8Runtime.cpp
Native state now uses weakly finalized holders. Queries validate the holder and its state. Replacement objects copy properties and prototypes, and transfer any external-memory tracker.
Object prototype operations
NativeScript/v8runtime/V8Runtime.h, NativeScript/v8runtime/V8Runtime.cpp
V8Runtime now creates objects with a supplied prototype and implements prototype get and set operations.
Buffer and external-memory accounting
NativeScript/v8runtime/V8Runtime.cpp
ArrayBuffer creation uses a backing store that retains the mutable buffer. External-memory pressure updates or creates a tracker that adjusts V8 accounting.

Priority: ➖ Normal

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Feature

Merge Risk: ⚪ Minimal · up to 93eb3

No actionable issue was established that needs to be fixed before merging. Complete the normal build and runtime checks.

Pre-merge checks · Passed 4 · Failed 1

Failed Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 4.76% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 21 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks (4 passed)
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely summarizes the main changes: completing V8Runtime NativeState, MutableBuffer ArrayBuffer, and ExternalMemoryAccounter support.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

I’m a rabbit, hopping by,
With V8 buffers held nearby.
Weak state holders keep their place,
Prototypes find their proper space.
Memory counts with careful care,
I twitch my nose and bound elsewhere!

Comment @coderabbitai help to get the list of available commands.

@NathanWalker

Copy link
Copy Markdown
Contributor Author

Moving to optional external package @nativescript/ios-jsi here: NativeScript/runtimes#76

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