Skip to content
Open
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
16 changes: 4 additions & 12 deletions NativeScript/ffi/jni/jsi/conversion/ArrayBufferHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace {
}

ArrayBufferHelper::ArrayBufferHelper()
: m_objectManager(nullptr), m_ByteBufferClass(nullptr), m_isDirectMethodID(nullptr),
: m_objectManager(nullptr), m_ByteBufferClass(nullptr),
m_remainingMethodID(nullptr), m_getMethodID(nullptr) {
}

Expand Down Expand Up @@ -103,19 +103,11 @@ JsValue ArrayBufferHelper::CreateFromCallbackImpl(JsRuntime &rt, const JsValue *
throw NativeScriptException("Wrong type of argument (ByteBuffer expected)");
}

if (m_isDirectMethodID == nullptr) {
m_isDirectMethodID = jEnv.GetMethodID(m_ByteBufferClass, "isDirect", "()Z");
assert(m_isDirectMethodID != nullptr);
}

auto ret = jEnv.CallBooleanMethod(obj, m_isDirectMethodID);

auto isDirectBuffer = ret == JNI_TRUE;

std::shared_ptr<engine::MutableBuffer> buffer;

if (isDirectBuffer) {
auto data = jEnv.GetDirectBufferAddress(obj);
auto data = jEnv.GetDirectBufferAddress(obj);

if (data != nullptr) {
auto size = jEnv.GetDirectBufferCapacity(obj);

buffer = std::make_shared<BorrowedBuffer>(static_cast<uint8_t *>(data), (size_t) size);
Expand Down
1 change: 0 additions & 1 deletion NativeScript/ffi/jni/jsi/conversion/ArrayBufferHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace tns {
ObjectManager* m_objectManager;

jclass m_ByteBufferClass;
jmethodID m_isDirectMethodID;
jmethodID m_remainingMethodID;
jmethodID m_getMethodID;
};
Expand Down
26 changes: 8 additions & 18 deletions NativeScript/ffi/jni/napi/conversion/ArrayBufferHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using namespace tns;

ArrayBufferHelper::ArrayBufferHelper()
: m_objectManager(nullptr), m_ByteBufferClass(nullptr), m_isDirectMethodID(nullptr),
: m_objectManager(nullptr), m_ByteBufferClass(nullptr),
m_remainingMethodID(nullptr), m_getMethodID(nullptr) {
}

Expand Down Expand Up @@ -80,19 +80,11 @@ napi_value ArrayBufferHelper::CreateFromCallbackImpl(napi_env env, size_t argc,
throw NativeScriptException("Wrong type of argument (ByteBuffer expected)");
}

if (m_isDirectMethodID == nullptr) {
m_isDirectMethodID = jEnv.GetMethodID(m_ByteBufferClass, "isDirect", "()Z");
assert(m_isDirectMethodID != nullptr);
}

auto ret = jEnv.CallBooleanMethod(obj, m_isDirectMethodID);

auto isDirectBuffer = ret == JNI_TRUE;

napi_value arrayBuffer;

if (isDirectBuffer) {
auto data = jEnv.GetDirectBufferAddress(obj);
auto data = jEnv.GetDirectBufferAddress(obj);

if (data != nullptr) {
auto size = jEnv.GetDirectBufferCapacity(obj);

void* externalData = data;
Expand All @@ -117,19 +109,17 @@ napi_value ArrayBufferHelper::CreateFromCallbackImpl(napi_env env, size_t argc,

auto byteArrayElements = jEnv.GetByteArrayElements(byteArray, 0);

jbyte* data = new jbyte[bufferRemainingSize];
memcpy(data, byteArrayElements, bufferRemainingSize);
jbyte* copy = new jbyte[bufferRemainingSize];
memcpy(copy, byteArrayElements, bufferRemainingSize);

NAPI_GUARD(napi_create_external_arraybuffer(env, data, bufferRemainingSize, [](napi_env env, void* finalize_data, void* finalize_hint) {
NAPI_GUARD(napi_create_external_arraybuffer(env, copy, bufferRemainingSize, [](napi_env env, void* finalize_data, void* finalize_hint) {
delete[] static_cast<jbyte*>(finalize_data);
}, nullptr, &arrayBuffer)) {}

jEnv.ReleaseByteArrayElements(byteArray, byteArrayElements, 0);
}

napi_value nativeObjectKey;
NAPI_GUARD(napi_create_string_utf8(env, "nativeObject", NAPI_AUTO_LENGTH, &nativeObjectKey)) {}
NAPI_GUARD(napi_set_property(env, arrayBuffer, nativeObjectKey, argObj)) {}
NAPI_GUARD(napi_set_named_property(env, arrayBuffer, "nativeObject", argObj)) {}

return arrayBuffer;
}
1 change: 0 additions & 1 deletion NativeScript/ffi/jni/napi/conversion/ArrayBufferHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ namespace tns {
ObjectManager* m_objectManager;

jclass m_ByteBufferClass;
jmethodID m_isDirectMethodID;
jmethodID m_remainingMethodID;
jmethodID m_getMethodID;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ function buildSuite() {
var cb = new org.nativescript.BenchmarkCallback({ onString: function (value) {} });
for (var i = 0; i < 1e4; i++) Benchmarks.invokeStringCallback(cb);
} },
{ name: "ArrayBuffer.from direct ByteBuffer", iterations: 1e5, action: function () { var b = Benchmarks.returnDirectBuffer(); for (var i = 0; i < 1e5; i++) ArrayBuffer.from(b); } },
{ name: "ArrayBuffer.from heap ByteBuffer", iterations: 1e5, action: function () { var b = Benchmarks.returnHeapBuffer(); for (var i = 0; i < 1e5; i++) ArrayBuffer.from(b); } },
{ name: "Invoke callback that throws", iterations: 5e4, action: function () {
var cb = new org.nativescript.BenchmarkCallback({ onCallback: function () { throw new Error("benchmark"); } });
Benchmarks.invokeThrowingCallback(cb, 5e4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ public static void invokeStringCallback(BenchmarkCallback callback) {
callback.onString("hello");
}

private static final java.nio.ByteBuffer DIRECT_BUFFER = java.nio.ByteBuffer.allocateDirect(1024);

private static final java.nio.ByteBuffer HEAP_BUFFER = java.nio.ByteBuffer.allocate(1024);

public static java.nio.ByteBuffer returnDirectBuffer() {
return DIRECT_BUFFER;
}

public static java.nio.ByteBuffer returnHeapBuffer() {
return HEAP_BUFFER;
}

public static int invokeThrowingCallback(BenchmarkCallback callback, int count) {
int failures = 0;
for (int i = 0; i < count; i++) {
Expand Down
Loading