Skip to content

Commit

Permalink
style: change helloWorld to multiply
Browse files Browse the repository at this point in the history
  • Loading branch information
hubgan committed Jul 10, 2024
2 parents b8bd327 + 20420a1 commit 2239126
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 62 deletions.
12 changes: 6 additions & 6 deletions cpp/JSIExampleHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ namespace example {
std::vector<jsi::PropNameID> JSIExampleHostObject::getPropertyNames(jsi::Runtime &runtime)
{
std::vector<jsi::PropNameID> propertyNames;
propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "helloWorld"));
propertyNames.push_back(jsi::PropNameID::forAscii(runtime, "multiply"));
return propertyNames;
}

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

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

Expand All @@ -30,15 +30,15 @@ namespace example {
void JSIExampleHostObject::set(jsi::Runtime &runtime, const jsi::PropNameID &propNameId, const jsi::Value &value)
{
auto propName = propNameId.utf8(runtime);
if (propName == "helloWorld")
if (propName == "multiply")
{
// Do nothing
return;
}
throw std::runtime_error("Not yet implemented!");
}

jsi::Value JSIExampleHostObject::helloWorld(jsi::Runtime &runtime, const jsi::Value &value, const jsi::Value &value2) {
jsi::Value JSIExampleHostObject::multiply(jsi::Runtime &runtime, const jsi::Value &value, const jsi::Value &value2) {
if (value.isNumber() && value2.isNumber()) {
// Extract numbers and add them
double result = value.asNumber() + value2.asNumber();
Expand Down
2 changes: 1 addition & 1 deletion cpp/JSIExampleHostObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace example
jsi::Value get(jsi::Runtime &, const jsi::PropNameID &name) override;
void set(jsi::Runtime &, const jsi::PropNameID &name, const jsi::Value &value) override;
std::vector<jsi::PropNameID> getPropertyNames(jsi::Runtime &rt) override;
static jsi::Value helloWorld(jsi::Runtime &, const jsi::Value &value, const jsi::Value &value2);
static jsi::Value multiply(jsi::Runtime &, const jsi::Value &value, const jsi::Value &value2);
};

} // namespace margelo
Expand Down
37 changes: 18 additions & 19 deletions example/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
buildscript {
ext {
buildToolsVersion = "34.0.0"
minSdkVersion = 23
compileSdkVersion = 34
targetSdkVersion = 34
ndkVersion = "26.1.10909125"
kotlinVersion = "1.9.22"
kotlin_version = '2.0.0'
}
repositories {
google()
mavenCentral()
}
dependencies {
classpath("com.android.tools.build:gradle")
classpath("com.facebook.react:react-native-gradle-plugin")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin")
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
ext {
buildToolsVersion = "34.0.0"
minSdkVersion = 23
compileSdkVersion = 34
targetSdkVersion = 34
ndkVersion = "26.1.10909125"
kotlin_version = '2.0.0'
}
repositories {
google()
mavenCentral()
}
dependencies {
classpath("com.android.tools.build:gradle")
classpath("com.facebook.react:react-native-gradle-plugin")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin")
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

apply plugin: "com.facebook.react.rootproject"
9 changes: 4 additions & 5 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import React from 'react';
import { StyleSheet, View, Text } from 'react-native';
import { JSIExample } from '../../src/JSIExample';
import JSIExample from '../../src/JSIExample/JSIExample';

const App: React.FC = () => {
const sayHello = () => {
//JSIExample.helloWorld = 'Hello World';
return JSIExample.helloWorld(2, 3);
const multiply = () => {
return JSIExample.multiply(2, 3);
};

return (
<View style={styles.container}>
<Text>{sayHello()}</Text>
<Text>{multiply()}</Text>
</View>
);
};
Expand Down
73 changes: 43 additions & 30 deletions src/JSIExample.ts → src/JSIExample/JSIExample.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { NativeModules, Platform } from 'react-native';
import type { JSIExampleWrapper } from './types';

export interface JSIExampleWrapper {
helloWorld(num1: number, num2: number): string;
}

// global func declaration for JSI functions
declare global {
function nativeCallSyncHook(): unknown;
var __JSIExampleProxy: JSIExampleWrapper | undefined;
function verifyExpoGo() {
const ExpoConstants =
NativeModules.NativeUnimoduleProxy?.modulesConstants?.ExponentConstants;
if (ExpoConstants != null) {
if (ExpoConstants.appOwnership === 'expo') {
throw new Error(
'react-native-fast-crypto is not supported in Expo Go! Use EAS (`expo prebuild`) or eject to a bare workflow instead.'
);
} else {
throw new Error('\n* Make sure you ran `expo prebuild`.');
}
}
}

// Check if the constructor exists. If not, try installing the JSI bindings.
if (global.__JSIExampleProxy == null) {
// Get the native JSIExample ReactModule
function getJSIExample() {
const JSIExampleModule = NativeModules.JSIExample;
if (JSIExampleModule == null) {
let message =
Expand All @@ -25,44 +28,54 @@ if (global.__JSIExampleProxy == null) {
if (Platform.OS === 'android') {
message += '\n* Make sure gradle is synced.';
}
// check if Expo
const ExpoConstants =
NativeModules.NativeUnimoduleProxy?.modulesConstants?.ExponentConstants;
if (ExpoConstants != null) {
if (ExpoConstants.appOwnership === 'expo') {
// We're running Expo Go
throw new Error(
'react-native-fast-crypto is not supported in Expo Go! Use EAS (`expo prebuild`) or eject to a bare workflow instead.'
);
} else {
// We're running Expo bare / standalone
message += '\n* Make sure you ran `expo prebuild`.';
}
}

message += '\n* Make sure you rebuilt the app.';
throw new Error(message);
}
return JSIExampleModule;
}

// Check if we are running on-device (JSI)
function verifyOnDevice(JSIExampleModule: any) {
if (global.nativeCallSyncHook == null || JSIExampleModule.install == null) {
throw new Error(
'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.'
);
}
}

// Call the synchronous blocking install() function
function installModule(JSIExampleModule: any) {
const result = JSIExampleModule.install();
if (result !== true)
throw new Error(
`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}`
);
}

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

export const JSIExample: JSIExampleWrapper = global.__JSIExampleProxy;
function createJSIExampleProxy(): JSIExampleWrapper {
if (global.__JSIExampleProxy) {
return global.__JSIExampleProxy;
}

verifyExpoGo();

const JSIExampleModule = getJSIExample();

verifyOnDevice(JSIExampleModule);
installModule(JSIExampleModule);
verifyInstallation();

if (global.__JSIExampleProxy == null) {
throw new Error('Failed to initialize __JSIExampleProxy.');
}

return global.__JSIExampleProxy;
}

// Call the creator and export what it returns
export default createJSIExampleProxy();
9 changes: 9 additions & 0 deletions src/JSIExample/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface JSIExampleWrapper {
multiply(a: number, b: number): number;
}

// global func declaration for JSI functions
declare global {
function nativeCallSyncHook(): unknown;
var __JSIExampleProxy: JSIExampleWrapper | undefined;
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './JSIExample/JSIExample';
export * from './JSIExample/types';
1 change: 0 additions & 1 deletion src/index.tsx

This file was deleted.

0 comments on commit 2239126

Please sign in to comment.