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

feat: added basic jsi example #15

Merged
merged 1 commit into from
Jul 10, 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
133 changes: 0 additions & 133 deletions CODE_OF_CONDUCT.md

This file was deleted.

18 changes: 14 additions & 4 deletions android/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
cmake_minimum_required(VERSION 3.4.1)
project(AudioContext)
cmake_minimum_required(VERSION 3.9.0)
project(react-native-audio-context)

set (CMAKE_VERBOSE_MAKEFILE ON)
set (CMAKE_CXX_STANDARD 14)

add_library(react-native-audio-context SHARED
../cpp/react-native-audio-context.cpp
add_library(react-native-audio-context
SHARED
../cpp/JSIExampleHostObject.cpp
cpp-adapter.cpp
)

# Specifies a path to native header files.
include_directories(
../cpp
../node_modules/react-native/ReactCommon/jsi
)

find_package(ReactAndroid REQUIRED CONFIG)

target_link_libraries(
react-native-audio-context
ReactAndroid::jsi
android
)
17 changes: 13 additions & 4 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import com.android.Version

buildscript {
repositories {
google()
mavenCentral()
}

dependencies {
classpath "com.android.tools.build:gradle:7.2.1"
classpath "com.android.tools.build:gradle:7.2.2"
}
}

Expand All @@ -19,6 +21,7 @@ def isNewArchitectureEnabled() {
}

apply plugin: "com.android.library"
apply plugin: 'org.jetbrains.kotlin.android'

if (isNewArchitectureEnabled()) {
apply plugin: "com.facebook.react"
Expand All @@ -32,8 +35,8 @@ def getExtOrIntegerDefault(name) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["AudioContext_" + name]).toInteger()
}

def supportsNamespace() {
def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
static def supportsNamespace() {
def parsed = Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
def major = parsed[0].toInteger()
def minor = parsed[1].toInteger()

Expand Down Expand Up @@ -64,6 +67,7 @@ android {
cmake {
cppFlags "-O2 -frtti -fexceptions -Wall -fstack-protector-all"
abiFilters (*reactNativeArchitectures())
arguments "-DANDROID_STL=c++_shared"
}
}
}
Expand All @@ -76,6 +80,7 @@ android {

buildFeatures {
buildConfig true
prefab true
}

buildTypes {
Expand Down Expand Up @@ -103,6 +108,9 @@ android {
}
}
}
kotlinOptions {
jvmTarget = '17'
}
}

repositories {
Expand All @@ -116,12 +124,13 @@ dependencies {
// For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+"
implementation 'androidx.core:core-ktx:1.13.1'
}

if (isNewArchitectureEnabled()) {
react {
jsRootDir = file("../src/")
libraryName = "AudioContext"
libraryName = "react-native-audio-context"
codegenJavaPackageName = "com.audiocontext"
}
}
20 changes: 16 additions & 4 deletions android/cpp-adapter.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
#include <jni.h>
#include "react-native-audio-context.h"
#include <jsi/jsi.h>
#include "JSIExampleHostObject.h"

using namespace facebook;

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

extern "C"
JNIEXPORT jdouble JNICALL
Java_com_audiocontext_AudioContextModule_nativeMultiply(JNIEnv *env, jclass type, jdouble a, jdouble b) {
return audiocontext::multiply(a, b);
JNIEXPORT void JNICALL
Java_com_audiocontext_jsi_JSIExampleModule_00024Companion_nativeInstall(JNIEnv *env, jobject clazz, jlong jsiPtr) {
auto runtime = reinterpret_cast<jsi::Runtime*>(jsiPtr);
if (runtime) {
install(*runtime);
}
}
34 changes: 0 additions & 34 deletions android/src/main/java/com/audiocontext/AudioContextModule.java

This file was deleted.

44 changes: 0 additions & 44 deletions android/src/main/java/com/audiocontext/AudioContextPackage.java

This file was deleted.

17 changes: 17 additions & 0 deletions android/src/main/java/com/audiocontext/JSIExamplePackage.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.audiocontext

import com.audiocontext.jsi.JSIExampleModule
import com.facebook.react.ReactPackage
import com.facebook.react.bridge.NativeModule
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.uimanager.ViewManager

class JSIExamplePackage : ReactPackage {
override fun createNativeModules(reactContext: ReactApplicationContext): List<NativeModule> {
return listOf<NativeModule>(JSIExampleModule(reactContext))
}

override fun createViewManagers(reactContext: ReactApplicationContext): List<ViewManager<*, *>> {
return emptyList()
}
}
36 changes: 36 additions & 0 deletions android/src/main/java/com/audiocontext/jsi/JSIExampleModule.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.audiocontext.jsi

import android.util.Log
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.module.annotations.ReactModule

@ReactModule(name = JSIExampleModule.NAME)
class JSIExampleModule(reactContext: ReactApplicationContext?) :
ReactContextBaseJavaModule(reactContext) {
override fun getName(): String {
return NAME
}

@ReactMethod(isBlockingSynchronousMethod = true)
fun install(): Boolean {
try {
System.loadLibrary("react-native-audio-context")

val jsContext = reactApplicationContext.javaScriptContextHolder

nativeInstall(jsContext!!.get())
return true
} catch (exception: Exception) {
Log.e(NAME, "Failed to install JSI Bindings for react-native-audio-context", exception)
return false
}
}

companion object {
const val NAME: String = "JSIExample"

private external fun nativeInstall(jsiPtr: Long)
}
}
Loading