Worklets without captured variables are rejected as "Regular javascript function" when transpiled by react-native-worklets' Babel plugin
Evaluación
- Dificultad
- 3/5
- Tiempo estimado
- 1-2 días
- Aptitud para principiantes
- 55/100
- Tipo de issue
- Error
- Claridad
- Bien especificado
- Estado de actividad
- Activo
- Stack tecnológico
- cpp, javascript, react-native
Línea de trabajo
Start with cpp/WKTJsiWorklet.h, especially isDecoratedAsWorklet() and createWorklet(), then trace how JsiObjectWrapper::setFunctionValue() handles a function without __closure. Reproduce with the supplied Babel output: a numeric __workletHash, string __initData.code, and no closure. Done when that no-capture worklet is recognized and invoked successfully while existing captured worklets still work.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Description
When a project uses the Babel plugin from react-native-worklets (the Reanimated 4 worklets package) together with react-native-worklets-core, any 'worklet' function that does not capture any outer variables is not recognized as a worklet by worklets-core.
Calling such a function from inside another worklet that runs on a worklets-core context throws:
Regular javascript function 't' cannot be shared. Try decorating the function with the 'worklet' keyword to allow the javascript function to be used as a worklet.
('t' is just the minified function name in a release bundle.)
We hit this through react-native-filament: EntitySelector's MaterialModifier worklet calls the module-level applyMaterialParameters worklet, which captures nothing, so every model with materialParameters fails on the worklet thread.
Environment
- react-native: 0.88.0 (New Architecture, Hermes)
- react-native-worklets-core: 1.6.3
- react-native-worklets: 0.13.0 (its Babel plugin is active)
- react-native-filament: 1.9.0 (where we first saw it; 1.11.0 has the same code)
- Babel plugins:
plugins: [ ['react-native-worklets-core/plugin', { processNestedWorklets: true }], 'react-native-worklets/plugin', ]
Minimal reproduction
import { Worklets } from 'react-native-worklets-core'
// Captures nothing from the outer scope
const double = (x: number) => {
'worklet'
return x * 2
}
const context = Worklets.createContext('repro')
// The worklet passed to runAsync captures `double`
context
.runAsync(() => {
'worklet'
return double(21) // <- throws "Regular javascript function '...' cannot be shared"
})
.then(console.log)
If double captures any outer variable (so it gets a __closure), the call works.
What the transpiled code looks like
With the react-native-worklets plugin (0.13.0, and the same in the 0.14 nightlies), a worklet that captures nothing is emitted without __closure, and the hash is a number literal:
var double = function double_Factory([_worklet_123_init_data]) {
const double = function (x) { return x * 2 }
double.__workletHash = 123 // number, not string
double.__initData = _worklet_123_init_data // { code: "(function ...)" }
return double // no __closure assignment
}([_worklet_123_init_data])
The plugin only emits __closure when closureVariables.length > 0 (see plugin/index.js), so leaving it out is intended behavior there, not a bug.
Root cause
In cpp/WKTJsiWorklet.h:
isDecoratedAsWorklet()returnstrueright away only when__workletHashis a string. Otherwise it requires__closureto be present, so a numeric hash with no__closureis treated as a regular function.createWorklet()returns early when__closureisundefined/null, so even if detection passed, the worklet would not be created.
JsiObjectWrapper::setFunctionValue() then wraps the function in a host function that throws the "cannot be shared" error when called.
Suggested fix
Treat a function as a worklet when __workletHash is a number and __initData.code is a string, and use an empty closure when __closure is missing. Code generated for a worklet with no captures never reads this.__closure, so an empty array is safe.
I have a small patch for this and can open a PR.
Possibly related
#258 reports release builds failing with "is not a Worklet" when the Reanimated plugin and the worklets-core plugin are used together. It might be the same root cause, but I haven't verified that.
- Lenguaje dominante
- C++
- Estrellas
- 791
- Forks
- 66
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Sin plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de margelo/react-native-worklets-core
-
Dificultad 4/5 3-5 días Aptitud para principiantes 45/100
-
Dificultad 5/5 Más de una semana Aptitud para principiantes 35/100
margelo/react-native-worklets-core#271 · 1 comentario ·
-
Dificultad 4/5 3-5 días Aptitud para principiantes 35/100
margelo/react-native-worklets-core#269 · 2 reacciones ·
-
Unable to download JS bundleAbierto
Dificultad 3/5 1-2 días Aptitud para principiantes 35/100
margelo/react-native-worklets-core#268 · 2 comentarios ·
-
Dificultad 3/5 1-2 días Aptitud para principiantes 72/100
margelo/react-native-worklets-core#267 · 1 comentario · 1 reacción ·
Todos los issues de margelo/react-native-worklets-core
Issues similares
-
[Docs] CONTRIBUTING still says FreeType 2.14.3 and TabLabel.h says ImGui 1.92.9b after the bumpsPosiblemente ocupada @mgradwohl la tomó hoy. Abiertoagent:WSL documentation LOW
Dificultad 1/5 1-3 horas Aptitud para principiantes 83/100
Los mantenedores suelen responder en 1 día
-
cudf-polars feature request
Dificultad 2/5 1-3 horas Aptitud para principiantes 77/100
Los mantenedores suelen responder en 2 días
-
(S1 - Need confirmation)
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
CleverRaven/Cataclysm-DDA#89212 ·
Los mantenedores suelen responder en 1 día
-
Status: Needs confirmation Status: Needs triage
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
Los mantenedores suelen responder en 1 día
-
a byte order mark at the start of a migration makes dbmate miss the up blockPosiblemente ocupada @Dem1241 la tomó hoy. Abierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
Los mantenedores suelen responder en 2 días