Skip to content

Commit f7d0cd4

Browse files
author
cawfree
authored
Merge pull request #5 from cawfree/cawfree/local-imports
@cawfree/local-imports
2 parents 68ca848 + 8eb34c2 commit f7d0cd4

File tree

6 files changed

+69
-4
lines changed

6 files changed

+69
-4
lines changed

example/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module '*.wasm' {
2+
const value: any;
3+
export default value;
4+
}

example/ios/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ PODS:
656656
- React-jsinspector (0.71.4)
657657
- React-logger (0.71.4):
658658
- glog
659-
- react-native-webassembly (0.2.3):
659+
- react-native-webassembly (0.2.4):
660660
- RCT-Folly
661661
- RCTRequired
662662
- RCTTypeSafety
@@ -961,7 +961,7 @@ SPEC CHECKSUMS:
961961
React-jsiexecutor: d6b7fa9260aa3cb40afee0507e3bc1d17ecaa6f2
962962
React-jsinspector: 1f51e775819199d3fe9410e69ee8d4c4161c7b06
963963
React-logger: 0d58569ec51d30d1792c5e86a8e3b78d24b582c6
964-
react-native-webassembly: 6a92b15260a85e3c3cfaf675c2b42700361ea460
964+
react-native-webassembly: c857530d3e827532d9835850251b56e1cdb68ef7
965965
React-perflogger: 0bb0522a12e058f6eb69d888bc16f40c16c4b907
966966
React-RCTActionSheet: bfd675a10f06a18728ea15d82082d48f228a213a
967967
React-RCTAnimation: 2fa220b2052ec75b733112aca39143d34546a941

example/metro.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const path = require('path');
22
const escape = require('escape-string-regexp');
33
const exclusionList = require('metro-config/src/defaults/exclusionList');
4+
const metroDefault = require('metro-config/src/defaults/defaults');
45
const pak = require('../package.json');
56

67
const root = path.resolve(__dirname, '..');
@@ -27,6 +28,8 @@ module.exports = {
2728
acc[name] = path.join(__dirname, 'node_modules', name);
2829
return acc;
2930
}, {}),
31+
32+
assetExts: metroDefault.assetExts.concat(['wasm']),
3033
},
3134

3235
transformer: {

example/src/App.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { Button, StyleSheet, View } from 'react-native';
33

44
import * as WebAssembly from 'react-native-webassembly';
55

6+
import Local from './Local.wasm';
7+
68
import { useWasmCircomRuntime, useWasmHelloWorld } from './hooks';
79
import { fetchWasm } from './utils';
810

@@ -32,6 +34,24 @@ export default function App() {
3234
if (result !== 305) throw new Error('Failed to add.');
3335
}, [helloWorldResult]);
3436

37+
React.useEffect(
38+
() =>
39+
void (async () => {
40+
try {
41+
const localModule = await WebAssembly.instantiate<{
42+
readonly add: (a: number, b: number) => number;
43+
}>(Local);
44+
45+
const result = localModule.instance.exports.add(1000, 2000);
46+
47+
if (result !== 3000) throw new Error('Failed to add. (Local)');
48+
} catch (e) {
49+
console.error(e);
50+
}
51+
})(),
52+
[]
53+
);
54+
3555
React.useEffect(
3656
() =>
3757
void (async () => {

example/src/Local.wasm

93 Bytes
Binary file not shown.

src/index.tsx

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { Image } = require('react-native');
12
const { Buffer } = require('buffer');
23
const { nanoid } = require('nanoid/non-secure');
34

@@ -63,9 +64,43 @@ const getScopedFunctions = (
6364
);
6465
};
6566

67+
const fetchRequireAsBase64 = async (moduleId: number): Promise<string> => {
68+
const maybeAssetSource = Image.resolveAssetSource(moduleId);
69+
70+
const maybeUri = maybeAssetSource?.uri;
71+
72+
if (typeof maybeUri !== 'string' || !maybeUri.length)
73+
throw new Error(
74+
`Expected non-empty string uri, encountered "${String(maybeUri)}".`
75+
);
76+
77+
const base64EncodedString = String(
78+
await new Promise(async (resolve, reject) => {
79+
const reader = new FileReader();
80+
reader.onload = () => resolve(reader.result);
81+
reader.onerror = reject;
82+
reader.readAsDataURL(await (await fetch(maybeUri)).blob());
83+
})
84+
);
85+
86+
const maybeBase64EncodedString = base64EncodedString
87+
.substring(base64EncodedString.indexOf(','))
88+
.slice(1);
89+
90+
if (
91+
typeof maybeBase64EncodedString !== 'string' ||
92+
!maybeBase64EncodedString.length
93+
)
94+
throw new Error(
95+
`Expected non-empty string base64EncodedString, encountered "${maybeBase64EncodedString}".`
96+
);
97+
98+
return maybeBase64EncodedString;
99+
};
100+
66101
// https://developer.mozilla.org/en-US/docs/WebAssembly/JavaScript_interface/instantiate
67102
export async function instantiate<Exports extends object>(
68-
bufferSource: Uint8Array | ArrayBuffer,
103+
bufferSource: Uint8Array | ArrayBuffer | number,
69104
maybeImportObject: WebAssemblyImportObject | undefined = undefined
70105
): Promise<WebassemblyInstantiateResult<Exports>> {
71106
const iid = nanoid();
@@ -84,7 +119,10 @@ export async function instantiate<Exports extends object>(
84119

85120
const instanceResult = Webassembly.instantiate({
86121
iid,
87-
bufferSource: Buffer.from(bufferSource).toString('base64'),
122+
bufferSource:
123+
typeof bufferSource === 'number'
124+
? await fetchRequireAsBase64(bufferSource)
125+
: Buffer.from(bufferSource).toString('base64'),
88126
stackSizeInBytes,
89127
rawFunctions,
90128
rawFunctionScopes,

0 commit comments

Comments
 (0)