Skip to content

Commit 2239126

Browse files
committed
style: change helloWorld to multiply
2 parents b8bd327 + 20420a1 commit 2239126

File tree

8 files changed

+83
-62
lines changed

8 files changed

+83
-62
lines changed

cpp/JSIExampleHostObject.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,20 @@ namespace example {
77
std::vector<jsi::PropNameID> JSIExampleHostObject::getPropertyNames(jsi::Runtime &runtime)
88
{
99
std::vector<jsi::PropNameID> propertyNames;
10-
propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "helloWorld"));
10+
propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "multiply"));
1111
return propertyNames;
1212
}
1313

1414
jsi::Value JSIExampleHostObject::get(jsi::Runtime &runtime, const jsi::PropNameID &propNameId) {
1515
auto propName = propNameId.utf8(runtime);
1616

17-
if (propName == "helloWorld") {
17+
if (propName == "multiply") {
1818
return jsi::Function::createFromHostFunction(runtime, propNameId, 2,
1919
[this](jsi::Runtime &rt, const jsi::Value &, const jsi::Value *args, size_t count) {
2020
if (count != 2) {
21-
throw std::invalid_argument("helloWorld expects exactly two arguments");
21+
throw std::invalid_argument("multiply expects exactly two arguments");
2222
}
23-
return this->helloWorld(rt, args[0], args[1]);
23+
return this->multiply(rt, args[0], args[1]);
2424
});
2525
}
2626

@@ -30,15 +30,15 @@ namespace example {
3030
void JSIExampleHostObject::set(jsi::Runtime &runtime, const jsi::PropNameID &propNameId, const jsi::Value &value)
3131
{
3232
auto propName = propNameId.utf8(runtime);
33-
if (propName == "helloWorld")
33+
if (propName == "multiply")
3434
{
3535
// Do nothing
3636
return;
3737
}
3838
throw std::runtime_error("Not yet implemented!");
3939
}
4040

41-
jsi::Value JSIExampleHostObject::helloWorld(jsi::Runtime &runtime, const jsi::Value &value, const jsi::Value &value2) {
41+
jsi::Value JSIExampleHostObject::multiply(jsi::Runtime &runtime, const jsi::Value &value, const jsi::Value &value2) {
4242
if (value.isNumber() && value2.isNumber()) {
4343
// Extract numbers and add them
4444
double result = value.asNumber() + value2.asNumber();

cpp/JSIExampleHostObject.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace example
1717
jsi::Value get(jsi::Runtime &, const jsi::PropNameID &name) override;
1818
void set(jsi::Runtime &, const jsi::PropNameID &name, const jsi::Value &value) override;
1919
std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt) override;
20-
static jsi::Value helloWorld(jsi::Runtime &, const jsi::Value &value, const jsi::Value &value2);
20+
static jsi::Value multiply(jsi::Runtime &, const jsi::Value &value, const jsi::Value &value2);
2121
};
2222

2323
} // namespace margelo

example/android/build.gradle

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
11
buildscript {
2-
ext {
3-
buildToolsVersion = "34.0.0"
4-
minSdkVersion = 23
5-
compileSdkVersion = 34
6-
targetSdkVersion = 34
7-
ndkVersion = "26.1.10909125"
8-
kotlinVersion = "1.9.22"
9-
kotlin_version = '2.0.0'
10-
}
11-
repositories {
12-
google()
13-
mavenCentral()
14-
}
15-
dependencies {
16-
classpath("com.android.tools.build:gradle")
17-
classpath("com.facebook.react:react-native-gradle-plugin")
18-
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin")
19-
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
20-
}
2+
ext {
3+
buildToolsVersion = "34.0.0"
4+
minSdkVersion = 23
5+
compileSdkVersion = 34
6+
targetSdkVersion = 34
7+
ndkVersion = "26.1.10909125"
8+
kotlin_version = '2.0.0'
9+
}
10+
repositories {
11+
google()
12+
mavenCentral()
13+
}
14+
dependencies {
15+
classpath("com.android.tools.build:gradle")
16+
classpath("com.facebook.react:react-native-gradle-plugin")
17+
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin")
18+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
19+
}
2120
}
2221

2322
apply plugin: "com.facebook.react.rootproject"

example/src/App.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
import React from 'react';
22
import { StyleSheet, View, Text } from 'react-native';
3-
import { JSIExample } from '../../src/JSIExample';
3+
import JSIExample from '../../src/JSIExample/JSIExample';
44

55
const App: React.FC = () => {
6-
const sayHello = () => {
7-
//JSIExample.helloWorld = 'Hello World';
8-
return JSIExample.helloWorld(2, 3);
6+
const multiply = () => {
7+
return JSIExample.multiply(2, 3);
98
};
109

1110
return (
1211
<View style={styles.container}>
13-
<Text>{sayHello()}</Text>
12+
<Text>{multiply()}</Text>
1413
</View>
1514
);
1615
};
Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
import { NativeModules, Platform } from 'react-native';
2+
import type { JSIExampleWrapper } from './types';
23

3-
export interface JSIExampleWrapper {
4-
helloWorld(num1: number, num2: number): string;
5-
}
6-
7-
// global func declaration for JSI functions
8-
declare global {
9-
function nativeCallSyncHook(): unknown;
10-
var __JSIExampleProxy: JSIExampleWrapper | undefined;
4+
function verifyExpoGo() {
5+
const ExpoConstants =
6+
NativeModules.NativeUnimoduleProxy?.modulesConstants?.ExponentConstants;
7+
if (ExpoConstants != null) {
8+
if (ExpoConstants.appOwnership === 'expo') {
9+
throw new Error(
10+
'react-native-fast-crypto is not supported in Expo Go! Use EAS (`expo prebuild`) or eject to a bare workflow instead.'
11+
);
12+
} else {
13+
throw new Error('\n* Make sure you ran `expo prebuild`.');
14+
}
15+
}
1116
}
1217

13-
// Check if the constructor exists. If not, try installing the JSI bindings.
14-
if (global.__JSIExampleProxy == null) {
15-
// Get the native JSIExample ReactModule
18+
function getJSIExample() {
1619
const JSIExampleModule = NativeModules.JSIExample;
1720
if (JSIExampleModule == null) {
1821
let message =
@@ -25,44 +28,54 @@ if (global.__JSIExampleProxy == null) {
2528
if (Platform.OS === 'android') {
2629
message += '\n* Make sure gradle is synced.';
2730
}
28-
// check if Expo
29-
const ExpoConstants =
30-
NativeModules.NativeUnimoduleProxy?.modulesConstants?.ExponentConstants;
31-
if (ExpoConstants != null) {
32-
if (ExpoConstants.appOwnership === 'expo') {
33-
// We're running Expo Go
34-
throw new Error(
35-
'react-native-fast-crypto is not supported in Expo Go! Use EAS (`expo prebuild`) or eject to a bare workflow instead.'
36-
);
37-
} else {
38-
// We're running Expo bare / standalone
39-
message += '\n* Make sure you ran `expo prebuild`.';
40-
}
41-
}
42-
4331
message += '\n* Make sure you rebuilt the app.';
4432
throw new Error(message);
4533
}
34+
return JSIExampleModule;
35+
}
4636

47-
// Check if we are running on-device (JSI)
37+
function verifyOnDevice(JSIExampleModule: any) {
4838
if (global.nativeCallSyncHook == null || JSIExampleModule.install == null) {
4939
throw new Error(
5040
'Failed to install react-native-fast-crypto: React Native is not running on-device. JSIExample can only be used when synchronous method invocations (JSI) are possible. If you are using a remote debugger (e.g. Chrome), switch to an on-device debugger (e.g. Flipper) instead.'
5141
);
5242
}
43+
}
5344

54-
// Call the synchronous blocking install() function
45+
function installModule(JSIExampleModule: any) {
5546
const result = JSIExampleModule.install();
5647
if (result !== true)
5748
throw new Error(
5849
`Failed to install react-native-fast-crypto: The native JSIExample Module could not be installed! Looks like something went wrong when installing JSI bindings: ${result}`
5950
);
51+
}
6052

61-
// Check again if the constructor now exists. If not, throw an error.
53+
function verifyInstallation() {
6254
if (global.__JSIExampleProxy == null)
6355
throw new Error(
6456
'Failed to install react-native-fast-crypto, the native initializer function does not exist. Are you trying to use JSIExample from different JS Runtimes?'
6557
);
6658
}
6759

68-
export const JSIExample: JSIExampleWrapper = global.__JSIExampleProxy;
60+
function createJSIExampleProxy(): JSIExampleWrapper {
61+
if (global.__JSIExampleProxy) {
62+
return global.__JSIExampleProxy;
63+
}
64+
65+
verifyExpoGo();
66+
67+
const JSIExampleModule = getJSIExample();
68+
69+
verifyOnDevice(JSIExampleModule);
70+
installModule(JSIExampleModule);
71+
verifyInstallation();
72+
73+
if (global.__JSIExampleProxy == null) {
74+
throw new Error('Failed to initialize __JSIExampleProxy.');
75+
}
76+
77+
return global.__JSIExampleProxy;
78+
}
79+
80+
// Call the creator and export what it returns
81+
export default createJSIExampleProxy();

src/JSIExample/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export interface JSIExampleWrapper {
2+
multiply(a: number, b: number): number;
3+
}
4+
5+
// global func declaration for JSI functions
6+
declare global {
7+
function nativeCallSyncHook(): unknown;
8+
var __JSIExampleProxy: JSIExampleWrapper | undefined;
9+
}

src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './JSIExample/JSIExample';
2+
export * from './JSIExample/types';

src/index.tsx

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)