Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore/ios jsi setup #13

Merged
merged 3 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 20 additions & 14 deletions cpp/JSIExampleHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ 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)
{
jsi::Value JSIExampleHostObject::get(jsi::Runtime &runtime, const jsi::PropNameID &propNameId) {
auto propName = propNameId.utf8(runtime);

if (propName == "helloWorld")
{
return jsi::Function::createFromHostFunction(runtime, propNameId, 0,
[this](jsi::Runtime &rt, const jsi::Value &, const jsi::Value *, size_t)
{
return this->helloWorld(rt);
});
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("multiply expects exactly two arguments");
}
return this->multiply(rt, args[0], args[1]);
});
}

throw std::runtime_error("Not yet implemented!");
Expand All @@ -30,16 +30,22 @@ 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)
{
return jsi::String::createFromUtf8(runtime, "Hello World using jsi::HostObject!");
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();
return jsi::Value(result);
} else {
// Handle other cases (e.g., one is a number and the other is a string)
return jsi::Value::undefined();
}
}
}
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 &);
static jsi::Value multiply(jsi::Runtime &, const jsi::Value &value, const jsi::Value &value2);
};

} // namespace margelo
Expand Down
10 changes: 8 additions & 2 deletions example/ios/AudioContextExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,10 @@
"-DFOLLY_CFG_NO_COROUTINES=1",
"-DFOLLY_HAVE_CLOCK_GETTIME=1",
);
OTHER_LDFLAGS = "$(inherited) ";
OTHER_LDFLAGS = (
"$(inherited)",
" ",
);
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
USE_HERMES = true;
Expand Down Expand Up @@ -664,7 +667,10 @@
"-DFOLLY_CFG_NO_COROUTINES=1",
"-DFOLLY_HAVE_CLOCK_GETTIME=1",
);
OTHER_LDFLAGS = "$(inherited) ";
OTHER_LDFLAGS = (
"$(inherited)",
" ",
);
REACT_NATIVE_PATH = "${PODS_ROOT}/../../node_modules/react-native";
SDKROOT = iphoneos;
USE_HERMES = true;
Expand Down
2 changes: 0 additions & 2 deletions example/ios/Podfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
ENV['RCT_NEW_ARCH_ENABLED'] = '1'

# Resolve react_native_pods.rb with node to allow for hoisting
require Pod::Executable.execute_command('node', ['-p',
'require.resolve(
Expand Down
8 changes: 4 additions & 4 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1395,15 +1395,15 @@ SPEC CHECKSUMS:
React-jsitracing: 6b3c8c98313642140530f93c46f5a6ca4530b446
React-logger: fa92ba4d3a5d39ac450f59be2a3cec7b099f0304
React-Mapbuffer: 9f68550e7c6839d01411ac8896aea5c868eff63a
react-native-audio-context: 11ae1a36b7d91d7cbd04cf5d5e27a6a9fb145eb8
react-native-audio-context: 4184c3164b1911b9a2632be8dfeecc0cb3d41c9b
React-nativeconfig: fa5de9d8f4dbd5917358f8ad3ad1e08762f01dcb
React-NativeModulesApple: 585d1b78e0597de364d259cb56007052d0bda5e5
React-perflogger: 7bb9ba49435ff66b666e7966ee10082508a203e8
React-RCTActionSheet: a2816ae2b5c8523c2bc18a8f874a724a096e6d97
React-RCTAnimation: e78f52d7422bac13e1213e25e9bcbf99be872e1a
React-RCTAppDelegate: 4843f73d1089552a7d7f4ec6d29e9942c8f5e161
React-RCTAppDelegate: 24f46de486cfa3a9f46e4b0786eaf17d92e1e0c6
React-RCTBlob: 9f9d6599d1b00690704dadc4a4bc33a7e76938be
React-RCTFabric: 56eb7973b13cd9d7be03ca06f621ed0edd124b81
React-RCTFabric: 609e66bb0371b9082c62ed677ee0614efe711bf2
React-RCTImage: 39dd5aee6b92213845e1e7a7c41865801dc33493
React-RCTLinking: 35d742a982f901f9ea416d772763e2da65c2dc7d
React-RCTNetwork: b078576c0c896c71905f841716b9f9f5922111dc
Expand All @@ -1422,6 +1422,6 @@ SPEC CHECKSUMS:
SocketRocket: abac6f5de4d4d62d24e11868d7a2f427e0ef940d
Yoga: 88480008ccacea6301ff7bf58726e27a72931c8d

PODFILE CHECKSUM: 1b2da41d5518b36ddce0d4e029682759a82955cc
PODFILE CHECKSUM: 122f19e943aa833c1ad02dd5fd0e0364e65fe9ce

COCOAPODS: 1.14.3
14 changes: 8 additions & 6 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import React from 'react';
import { StyleSheet, View, Text } from 'react-native';
import JSIExample from '../../src/JSIExample/JSIExample';

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

return (
<View style={styles.container}>
<Text>{sayHello()}</Text>
<Text>{multiply()}</Text>
</View>
);
}
};

export default App;

const styles = StyleSheet.create({
container: {
Expand Down
15 changes: 0 additions & 15 deletions ios/AudioContext.h

This file was deleted.

21 changes: 0 additions & 21 deletions ios/AudioContext.mm

This file was deleted.

5 changes: 5 additions & 0 deletions ios/JSIExampleModule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#import <React/RCTBridgeModule.h>

@interface JSIExampleModule : NSObject <RCTBridgeModule>

@end
38 changes: 38 additions & 0 deletions ios/JSIExampleModule.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#import "JSIExampleModule.h"

#import <React/RCTBridge+Private.h>
#import <React/RCTUtils.h>
#import <jsi/jsi.h>

#import "../cpp/JSIExampleHostObject.h"

@implementation JSIExampleModule

RCT_EXPORT_MODULE(JSIExample)

RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(install)
{
NSLog(@"Installing JSI bindings for react-native-audio-context...");
RCTBridge* bridge = [RCTBridge currentBridge];
RCTCxxBridge* cxxBridge = (RCTCxxBridge*)bridge;
if (cxxBridge == nil) {
return @false;
}

using namespace facebook;

auto jsiRuntime = (jsi::Runtime*) cxxBridge.runtime;
if (jsiRuntime == nil) {
return @false;
}
auto& runtime = *jsiRuntime;

auto hostObject = std::make_shared<example::JSIExampleHostObject>();
auto object = jsi::Object::createFromHostObject(runtime, hostObject);
runtime.global().setProperty(runtime, "__JSIExampleProxy", std::move(object));

NSLog(@"Successfully installed JSI bindings for react-native-audio-context!");
return @true;
}

@end
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@
"jsSrcsDir": "src"
},
"create-react-native-library": {
"type": "module-new",
"type": "module-legacy",
"languages": "cpp",
"version": "0.37.1"
}
Expand Down
2 changes: 1 addition & 1 deletion src/JSIExample/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface JSIExampleWrapper {
helloWorld(): string;
multiply(a: number, b: number): number;
}

// global func declaration for JSI functions
Expand Down