Skip to content

feat: Create ObjectUtils for Object.freeze, Object.defineProperty, ...#967

Merged
mrousavy merged 13 commits into
mainfrom
feat/object-utils
Oct 17, 2025
Merged

feat: Create ObjectUtils for Object.freeze, Object.defineProperty, ...#967
mrousavy merged 13 commits into
mainfrom
feat/object-utils

Conversation

@mrousavy
Copy link
Copy Markdown
Owner

@mrousavy mrousavy commented Oct 17, 2025

This one PR does 2 things:

1. Return actual error messages when Nitro fails to initialize

It logs & returns the actual error that was thrown when Nitro failed to initialize. This can happen if something is cooked with the runtime, or when a global() property cannot be set (e.g. if it's frozen)

So now the error would propagate up to JS instead of just saying the module cannot be found.

2. Add ObjectUtils

Also this PR adds ObjectUtils, a static-class that provides native access to JS methods like Object.freeze, Object.create, Object.defineProperty and a helper method Object.defineGlobal.

Caching and better performance

ObjectUtils also uses caching internally for common JS functions (like Object.defineProperty), so this should speed things up as well.

Object.defineGlobal

Object.defineGlobal takes a KnownGlobalPropertyName argument which is one of three values: __nitroDispatcher, __nitroJsiCache or NitroModulesProxy - all three values that Nitro puts into a jsi::Runtime's global. As we add more to global, we also need to add them to the enum. This prevents typos as before it was just loose strings repeated everywhere.

Object.defineGlobal also adds DEBUG checks like freezing the global (prevent it from being written to) and throwing if it already exists. We had an issue where react-native-typegpu from @wcandillon also installed the same globals since he uses an old copy of Nitro, which causes duplicate code in the codebase. This causes undecypherable crashes later on, since we try to downcast our global types to our nitro::margelo::Dispatcher type (or other types), which obviously doesn't work since it is a different type (or more precisely: C++ symbol) - the one from webgpu.

@vercel
Copy link
Copy Markdown

vercel Bot commented Oct 17, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Preview Comments Updated (UTC)
nitro-docs Skipped Skipped Oct 17, 2025 4:21pm

💡 Enable Vercel Agent with $100 free credit for automated AI reviews

@wcandillon
Copy link
Copy Markdown

let me know if there is anything I can do on my side to quick fix the webgpu module (namespace renaming/global renaming)

we already do it in native.
@mrousavy
Copy link
Copy Markdown
Owner Author

I think renaming the global would be enough for a monkey-patch, but nevertheless we should stop trying to monkeypatch all the time - going forward properly switching to the actually official release of Nitro makes more sense for both of us - from a maintenance and performance perspective :)

@mrousavy
Copy link
Copy Markdown
Owner Author

Created a fix for WebGPU: wcandillon/react-native-webgpu#271
let's merge this and then do nitro refactor

@mrousavy mrousavy merged commit 229e7d8 into main Oct 17, 2025
11 checks passed
@wcandillon
Copy link
Copy Markdown

@mrousavy nice! please trust that I not interesting to monkey patch forever, new bindings are high priority now.

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.

2 participants