forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSITest.cpp
43 lines (37 loc) · 1.47 KB
/
JSITest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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));
}