Skip to content

Commit

Permalink
Add some temp code to test array buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
ryantrem committed May 25, 2020
1 parent 98de687 commit 28152ce
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 2 deletions.
16 changes: 16 additions & 0 deletions RNTester/android/app/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
cmake_minimum_required(VERSION 3.13.2)

project(JSITest)

add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/../../../ReactCommon/jsi/jsi ${CMAKE_CURRENT_BINARY_DIR}/jsi)
include_directories(
${CMAKE_CURRENT_LIST_DIR}/../../../ReactCommon/jsi
)

add_library(JSITest SHARED src/main/cpp/JSITest.cpp)

target_link_libraries(JSITest
# link with these libraries:
android
log
jsi)
6 changes: 6 additions & 0 deletions RNTester/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ android {
testBuildType System.getProperty('testBuildType', 'debug') // This will later be used to control the test apk build type
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}
externalNativeBuild {
cmake {
version '3.13.2+'
path 'CMakeLists.txt'
}
}
signingConfigs {
release {
storeFile file(MYAPP_RELEASE_STORE_FILE)
Expand Down
43 changes: 43 additions & 0 deletions RNTester/android/app/src/main/cpp/JSITest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <jni.h>
#include <jsi/jsi.h>
#include <android/log.h>
#include <sstream>

using namespace facebook;

namespace
{
void log(const char *str)
{
__android_log_print(ANDROID_LOG_VERBOSE, "JSITest", "%s", str);
}
}

extern "C" JNIEXPORT void JNICALL Java_com_facebook_react_uiapp_JSITestModule_initialize(JNIEnv* env, jclass clazz, jlong jsiPtr)
{
auto& jsiRuntime = *reinterpret_cast<facebook::jsi::Runtime*>(jsiPtr);

auto testFunction = jsi::Function::createFromHostFunction(jsiRuntime, jsi::PropNameID::forAscii(jsiRuntime, "testArrayBuffer"), 1, [](jsi::Runtime& runtime, const jsi::Value& thisValue, const jsi::Value* arguments, size_t count) -> jsi::Value
{
auto arrayBufferArg = arguments[0].asObject(runtime);
bool isArrayBuffer = arrayBufferArg.isArrayBuffer(runtime);
if (isArrayBuffer)
{
jsi::ArrayBuffer arrayBuffer = arrayBufferArg.getArrayBuffer(runtime);
uint8_t* arrayBufferData = arrayBuffer.data(runtime);
size_t arrayBufferSize = arrayBuffer.size(runtime);

for (int i = 0; i < arrayBufferSize; i++)
{
uint8_t element = arrayBufferData[i];
std::ostringstream message;
message << "Element at index " << i << ": " << std::to_string(element);
log(message.str().c_str());
arrayBufferData[i] = element * 2;
}
}

return nullptr;
});
jsiRuntime.global().setProperty(jsiRuntime, "testArrayBuffer", std::move(testFunction));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.facebook.react.uiapp;

import androidx.annotation.NonNull;

import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.uimanager.ViewManager;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

class JSITestModule extends ReactContextBaseJavaModule {
static {
System.loadLibrary("JSITest");
}

private static native void initialize(long jsiPtr);

public JSITestModule(ReactApplicationContext reactContext) {
super(reactContext);
}

@NonNull
@Override
public String getName() {
return "JSITestModule";
}

@Override
public void initialize() {
super.initialize();

JSITestModule.initialize(this.getReactApplicationContext().getJavaScriptContextHolder().get());
}
}

class JSITestPackage implements ReactPackage {
@NonNull
@Override
public List<NativeModule> createNativeModules(@NonNull ReactApplicationContext reactContext) {
return Arrays.<NativeModule>asList(new JSITestModule(reactContext));
}

@NonNull
@Override
public List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public boolean getUseDeveloperSupport() {

@Override
public List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(new MainReactPackage());
return Arrays.<ReactPackage>asList(new MainReactPackage(), new JSITestPackage());
}
};

Expand Down
7 changes: 7 additions & 0 deletions RNTester/js/RNTesterApp.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@

'use strict';

const testArray = new Uint8Array(2);
testArray[0] = 42;
testArray[1] = 101;
testArrayBuffer(testArray.buffer);
console.log(`Mutated element at index 0: ${testArray[0]}`);
console.log(`Mutated element at index 1: ${testArray[1]}`);

const RNTesterActions = require('./utils/RNTesterActions');
const RNTesterExampleContainer = require('./components/RNTesterExampleContainer');
const RNTesterExampleList = require('./components/RNTesterExampleList');
Expand Down
2 changes: 1 addition & 1 deletion ReactCommon/jsi/jsi/jsi.h
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ class ArrayBuffer : public Object {
ArrayBuffer(ArrayBuffer&&) = default;
ArrayBuffer& operator=(ArrayBuffer&&) = default;

/// \return the size of the ArrayBuffer, according to its byteLength property.
/// \return the size of the ArrayBuffer, according to its property.
/// (C++ naming convention)
size_t size(Runtime& runtime) const {
return runtime.size(*this);
Expand Down

0 comments on commit 28152ce

Please sign in to comment.