Skip to content

Enforce JSON-serializable runtime function args and returns#7

Draft
V3RON wants to merge 2 commits into
mainfrom
feat/enforce-types
Draft

Enforce JSON-serializable runtime function args and returns#7
V3RON wants to merge 2 commits into
mainfrom
feat/enforce-types

Conversation

@V3RON
Copy link
Copy Markdown
Collaborator

@V3RON V3RON commented May 25, 2026

Summary

Runtime functions (runtimeFunction, ThreadedRuntime.run, call, etc.) now require arguments and return values to be JSON-serializable.

  • TypeScript: ThreadableFunction / RuntimeFunction constrain args to readonly JsonValue[] and returns to JsonValue | void. Passing functions, class instances, undefined in object fields, and other non-JSON values should fail at compile time. New helper types (ThreadableFunctionArgs, ThreadableFunctionAwaitedReturn) are exported from @react-native-runtimes/core.
  • Runtime (__DEV__ only): assertJson validates the args tuple before a cross-runtime call and the resolved return value before it is sent back (skipped when the handler returns undefined). Production builds skip these checks; invalid values may still slip through via JSON.stringify behavior.

Scope

This applies to runtime functions only. Headless tasks and other threaded APIs are unchanged.

Test plan

  • Typecheck example app and confirm invalid runtime function signatures are rejected
  • Run existing runtime function harness tests
  • In __DEV__, verify passing a function as an arg or return throws with a clear error message
  • Confirm void / undefined returns still work without triggering result validation

Made with Cursor

@V3RON V3RON marked this pull request as ready for review May 25, 2026 14:36
@V3RON
Copy link
Copy Markdown
Collaborator Author

V3RON commented May 25, 2026

There's a small catch: validation is now implemented on the JS side, but there's also a native way to run JS functions, and that path is not validated at the moment.

One option is to duplicate the validation logic. Another option is to expose the validation function so it can be called from native code. Alternatively, we could drop validation on the native side, assuming people will mainly use the JS API.

WDYT?

@V3RON V3RON marked this pull request as draft May 27, 2026 14:50
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