From 7cd60ce74a00290014db41cf342769cf3fde8d4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20S=C5=82uszniak?= Date: Thu, 5 Nov 2026 17:39:13 +0100 Subject: [PATCH 1/2] fix(cv): name the opencv lib when an app opted out of it With OpenCV off the native installer never adds the cv namespace, so every CV task failed with "Cannot read property 'resize' of undefined". Route the calls through cvJsi(), which throws INVALID_STATE naming the lib to add, and check it when the image preprocessor is created so tasks fail at construction. Closes #1618 --- .../__tests__/tasks/classification.test.ts | 15 +++++++++++++ .../src/extensions/cv/ops/box.ts | 6 +++--- .../src/extensions/cv/ops/image.ts | 16 +++++++------- .../src/extensions/cv/ops/quad.ts | 4 ++-- .../extensions/cv/utils/imagePreprocessor.ts | 3 +++ .../src/extensions/cv/utils/paddleOcrUtils.ts | 4 ++-- .../src/native/bridge.ts | 21 +++++++++++++++++++ 7 files changed, 54 insertions(+), 15 deletions(-) diff --git a/packages/react-native-executorch/__tests__/tasks/classification.test.ts b/packages/react-native-executorch/__tests__/tasks/classification.test.ts index 20f48bc510..74d4be0ad3 100644 --- a/packages/react-native-executorch/__tests__/tasks/classification.test.ts +++ b/packages/react-native-executorch/__tests__/tasks/classification.test.ts @@ -54,6 +54,21 @@ describe('createClassifier — model acceptance', () => { ); }); + it('names the OpenCV lib when the app opted out of it (#1618)', async () => { + registerBatched(); + const jsi = (globalThis as Record).__rnexecutorch_jsi__; + const { cv } = jsi; + delete jsi.cv; + try { + await expect(createClassifier(config())).rejects.toMatchObject({ + code: 'INVALID_STATE', + message: expect.stringContaining("Add the 'opencv' lib"), + }); + } finally { + jsi.cv = cv; + } + }); + it('surfaces a load failure', async () => { await expect( createClassifier({ ...config(), modelPath: '/models/absent.pte' }) diff --git a/packages/react-native-executorch/src/extensions/cv/ops/box.ts b/packages/react-native-executorch/src/extensions/cv/ops/box.ts index 61da7dae1e..8086da9e78 100644 --- a/packages/react-native-executorch/src/extensions/cv/ops/box.ts +++ b/packages/react-native-executorch/src/extensions/cv/ops/box.ts @@ -3,7 +3,7 @@ * Suppression (NMS). */ -import { rnexecutorchJsi } from '../../../native/bridge'; +import { cvJsi } from '../../../native/bridge'; import type { Tensor } from '../../../core/tensor'; import type { ResizeMode } from './image'; import { scalePoint } from './point'; @@ -187,7 +187,7 @@ export function nms( ): number[][]; export function nms(boxes: Tensor, scores: Tensor, options: NmsOptions): number[] | number[][] { 'worklet'; - return rnexecutorchJsi.cv.nms(boxes, scores, options); + return cvJsi().nms(boxes, scores, options); } /** @@ -223,5 +223,5 @@ export function restrictToBox(src: Tensor, dst: Tensor, box: BoundingBox [p.x, p.y]); - return rnexecutorchJsi.cv.rectifyQuad(src, dst, flat, { + return cvJsi().rectifyQuad(src, dst, flat, { contentWidth: options.contentWidth, align: options.align ?? 'left', padValue: options.padValue ?? 0, diff --git a/packages/react-native-executorch/src/extensions/cv/utils/imagePreprocessor.ts b/packages/react-native-executorch/src/extensions/cv/utils/imagePreprocessor.ts index e2a5a75a80..dc7a01d890 100644 --- a/packages/react-native-executorch/src/extensions/cv/utils/imagePreprocessor.ts +++ b/packages/react-native-executorch/src/extensions/cv/utils/imagePreprocessor.ts @@ -17,6 +17,7 @@ import { normalize, } from '../ops/image'; import { RnExecuTorchError } from '../../../core/error'; +import { cvJsi } from '../../../native/bridge'; /** * Options for configuring the image preprocessor pipeline. @@ -79,6 +80,8 @@ export function createImagePreprocessor( outputShape: number[] ): ImagePreprocessor { 'worklet'; + // Fail when the task is created, not on its first frame. + cvJsi(); const numRgbChannels = 3; const isRank3 = outputShape.length === 3 && outputShape[0] === numRgbChannels; const isRank4 = outputShape.length === 4 && outputShape[1] === numRgbChannels; diff --git a/packages/react-native-executorch/src/extensions/cv/utils/paddleOcrUtils.ts b/packages/react-native-executorch/src/extensions/cv/utils/paddleOcrUtils.ts index 388cf065dd..cfb51df2fc 100644 --- a/packages/react-native-executorch/src/extensions/cv/utils/paddleOcrUtils.ts +++ b/packages/react-native-executorch/src/extensions/cv/utils/paddleOcrUtils.ts @@ -2,7 +2,7 @@ * DBNet contour tracing and text quad extraction utilities for PP-OCRv6. */ -import { rnexecutorchJsi } from '../../../native/bridge'; +import { cvJsi } from '../../../native/bridge'; import type { Tensor } from '../../../core/tensor'; import { RnExecuTorchError } from '../../../core/error'; import type { Quad } from '../ops/quad'; @@ -60,6 +60,6 @@ export type DbnetDecodeOptions = { */ export function extractDbnetTextQuads(probabilityMap: Tensor, options: DbnetDecodeOptions): Quad[] { 'worklet'; - const flat = rnexecutorchJsi.cv.extractDbnetTextQuads(probabilityMap, options) as Float32Array; + const flat = cvJsi().extractDbnetTextQuads(probabilityMap, options) as Float32Array; return quadsFromFlat(flat); } diff --git a/packages/react-native-executorch/src/native/bridge.ts b/packages/react-native-executorch/src/native/bridge.ts index 3d17ab8fc0..153006b0ab 100644 --- a/packages/react-native-executorch/src/native/bridge.ts +++ b/packages/react-native-executorch/src/native/bridge.ts @@ -21,3 +21,24 @@ export const rnexecutorchJsi = globalObj.__rnexecutorch_jsi__; if (!rnexecutorchJsi) { throw RnExecuTorchError('UNKNOWN', "JSI global object '__rnexecutorch_jsi__' is not registered."); } + +/** + * The OpenCV-backed `cv` namespace of {@link rnexecutorchJsi}. Native code only + * installs it when the app builds with OpenCV, so an app that opted out gets an + * actionable error here instead of a `TypeError` on `undefined`. + * @returns The native `cv` namespace. + * @throws {RnExecuTorchError} With code `INVALID_STATE` if the native build + * has no OpenCV. + * @internal + */ +export function cvJsi() { + 'worklet'; + if (!rnexecutorchJsi.cv) { + throw RnExecuTorchError( + 'INVALID_STATE', + "The native build has no OpenCV. Add the 'opencv' lib (or a feature that uses it, such as " + + "'classification') to the app's react-native-executorch config and rebuild." + ); + } + return rnexecutorchJsi.cv; +} From 4266ac137fc11096b258065d96dc1aa2dda4a7da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20S=C5=82uszniak?= Date: Tue, 17 Nov 2026 10:41:13 +0100 Subject: [PATCH 2/2] refactor(cv): rename cvJsi to tryInitJsiCv --- .../src/extensions/cv/ops/box.ts | 6 +++--- .../src/extensions/cv/ops/image.ts | 16 ++++++++-------- .../src/extensions/cv/ops/quad.ts | 4 ++-- .../src/extensions/cv/utils/imagePreprocessor.ts | 4 ++-- .../src/extensions/cv/utils/paddleOcrUtils.ts | 4 ++-- .../react-native-executorch/src/native/bridge.ts | 2 +- 6 files changed, 18 insertions(+), 18 deletions(-) diff --git a/packages/react-native-executorch/src/extensions/cv/ops/box.ts b/packages/react-native-executorch/src/extensions/cv/ops/box.ts index 8086da9e78..9979e134e6 100644 --- a/packages/react-native-executorch/src/extensions/cv/ops/box.ts +++ b/packages/react-native-executorch/src/extensions/cv/ops/box.ts @@ -3,7 +3,7 @@ * Suppression (NMS). */ -import { cvJsi } from '../../../native/bridge'; +import { tryInitJsiCv } from '../../../native/bridge'; import type { Tensor } from '../../../core/tensor'; import type { ResizeMode } from './image'; import { scalePoint } from './point'; @@ -187,7 +187,7 @@ export function nms( ): number[][]; export function nms(boxes: Tensor, scores: Tensor, options: NmsOptions): number[] | number[][] { 'worklet'; - return cvJsi().nms(boxes, scores, options); + return tryInitJsiCv().nms(boxes, scores, options); } /** @@ -223,5 +223,5 @@ export function restrictToBox(src: Tensor, dst: Tensor, box: BoundingBox [p.x, p.y]); - return cvJsi().rectifyQuad(src, dst, flat, { + return tryInitJsiCv().rectifyQuad(src, dst, flat, { contentWidth: options.contentWidth, align: options.align ?? 'left', padValue: options.padValue ?? 0, diff --git a/packages/react-native-executorch/src/extensions/cv/utils/imagePreprocessor.ts b/packages/react-native-executorch/src/extensions/cv/utils/imagePreprocessor.ts index dc7a01d890..0b6e8a7410 100644 --- a/packages/react-native-executorch/src/extensions/cv/utils/imagePreprocessor.ts +++ b/packages/react-native-executorch/src/extensions/cv/utils/imagePreprocessor.ts @@ -17,7 +17,7 @@ import { normalize, } from '../ops/image'; import { RnExecuTorchError } from '../../../core/error'; -import { cvJsi } from '../../../native/bridge'; +import { tryInitJsiCv } from '../../../native/bridge'; /** * Options for configuring the image preprocessor pipeline. @@ -81,7 +81,7 @@ export function createImagePreprocessor( ): ImagePreprocessor { 'worklet'; // Fail when the task is created, not on its first frame. - cvJsi(); + tryInitJsiCv(); const numRgbChannels = 3; const isRank3 = outputShape.length === 3 && outputShape[0] === numRgbChannels; const isRank4 = outputShape.length === 4 && outputShape[1] === numRgbChannels; diff --git a/packages/react-native-executorch/src/extensions/cv/utils/paddleOcrUtils.ts b/packages/react-native-executorch/src/extensions/cv/utils/paddleOcrUtils.ts index cfb51df2fc..94c82ce6a2 100644 --- a/packages/react-native-executorch/src/extensions/cv/utils/paddleOcrUtils.ts +++ b/packages/react-native-executorch/src/extensions/cv/utils/paddleOcrUtils.ts @@ -2,7 +2,7 @@ * DBNet contour tracing and text quad extraction utilities for PP-OCRv6. */ -import { cvJsi } from '../../../native/bridge'; +import { tryInitJsiCv } from '../../../native/bridge'; import type { Tensor } from '../../../core/tensor'; import { RnExecuTorchError } from '../../../core/error'; import type { Quad } from '../ops/quad'; @@ -60,6 +60,6 @@ export type DbnetDecodeOptions = { */ export function extractDbnetTextQuads(probabilityMap: Tensor, options: DbnetDecodeOptions): Quad[] { 'worklet'; - const flat = cvJsi().extractDbnetTextQuads(probabilityMap, options) as Float32Array; + const flat = tryInitJsiCv().extractDbnetTextQuads(probabilityMap, options) as Float32Array; return quadsFromFlat(flat); } diff --git a/packages/react-native-executorch/src/native/bridge.ts b/packages/react-native-executorch/src/native/bridge.ts index 153006b0ab..00a9c0820e 100644 --- a/packages/react-native-executorch/src/native/bridge.ts +++ b/packages/react-native-executorch/src/native/bridge.ts @@ -31,7 +31,7 @@ if (!rnexecutorchJsi) { * has no OpenCV. * @internal */ -export function cvJsi() { +export function tryInitJsiCv() { 'worklet'; if (!rnexecutorchJsi.cv) { throw RnExecuTorchError(