Skip to content

Commit

Permalink
add basic support for TypedArray
Browse files Browse the repository at this point in the history
  • Loading branch information
ADopeReader committed Jul 26, 2022
1 parent 77d2e5a commit 891405d
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ add_library( # Sets the name of the library.
src/main/cpp/v8/JNIV8Function.cpp
src/main/cpp/v8/JNIV8Promise.cpp
src/main/cpp/v8/JNIV8ArrayBuffer.cpp
src/main/cpp/v8/JNIV8TypedArray.cpp
src/main/cpp/v8/JNIV8Symbol.cpp
)

Expand Down
3 changes: 3 additions & 0 deletions src/main/cpp/v8/JNIV8Marshalling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "JNIV8Promise.h"
#include "JNIV8Array.h"
#include "JNIV8ArrayBuffer.h"
#include "JNIV8TypedArray.h"
#include "JNIV8Symbol.h"
#include "JNIV8GenericObject.h"

Expand Down Expand Up @@ -500,6 +501,8 @@ jobject JNIV8Marshalling::v8value2jobject(v8::Local<v8::Value> valueRef) {
return JNIV8Wrapper::wrapObject<JNIV8Promise>(objectRef)->getJObject();
} else if(valueRef->IsArrayBuffer()) {
return JNIV8Wrapper::wrapObject<JNIV8ArrayBuffer>(objectRef)->getJObject();
} else if(valueRef->IsTypedArray()) {
return JNIV8Wrapper::wrapObject<JNIV8TypedArray>(objectRef)->getJObject();
}
auto ptr = JNIV8Wrapper::wrapObject<JNIV8Object>(objectRef);
if (ptr) {
Expand Down
33 changes: 33 additions & 0 deletions src/main/cpp/v8/JNIV8TypedArray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//
// Created by Dominik Seifert on 25.07.22
//

#include "JNIV8TypedArray.h"

#include <stdlib.h>

BGJS_JNI_LINK(JNIV8TypedArray, "ag/boersego/bgjs/JNIV8TypedArray");



bool JNIV8TypedArray::isWrappableV8Object(v8::Local<v8::Object> object) {
return object->IsTypedArray() || (object->IsProxy() && object.As<v8::Proxy>()->GetTarget()->IsTypedArray());
}

void JNIV8TypedArray::initializeJNIBindings(JNIClassInfo *info, bool isReload) {
info->registerNativeMethod("getV8Length", "()I", (void *) JNIV8TypedArray::jniGetV8Length);
}

/**
* returns the length of the TypedArray
*/
jint JNIV8TypedArray::jniGetV8Length(JNIEnv *env, jobject obj) {
JNIV8Object_PrepareJNICall(JNIV8TypedArray, v8::TypedArray, 0);
return localRef->Length();
}

/**
* cache JNI class references
*/
void JNIV8TypedArray::initJNICache() {
}
30 changes: 30 additions & 0 deletions src/main/cpp/v8/JNIV8TypedArray.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//
// Created by Dominik Seifert on 25.07.22
//

#ifndef ANDROID_TRADINGLIB_SAMPLE_JNIV8TYPEDARRAY_H
#define ANDROID_TRADINGLIB_SAMPLE_JNIV8TYPEDARRAY_H

#include "JNIV8Wrapper.h"

class JNIV8TypedArray : public JNIScope<JNIV8TypedArray, JNIV8Object> {
public:
JNIV8TypedArray(jobject obj, JNIClassInfo *info) : JNIScope(obj, info) {};

static bool isWrappableV8Object(v8::Local<v8::Object> object);
static void initializeJNIBindings(JNIClassInfo *info, bool isReload);

/**
* returns the length of the TypedArray
*/
static jint jniGetV8Length(JNIEnv *env, jobject obj);

/**
* cache JNI class references
*/
static void initJNICache();
};

BGJS_JNI_LINK_DEF(JNIV8TypedArray)

#endif //ANDROID_TRADINGLIB_SAMPLE_JNIV8TYPEDARRAY_H
2 changes: 2 additions & 0 deletions src/main/cpp/v8/JNIV8Wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ using namespace v8;
#include "JNIV8Promise.h"
#include "JNIV8Symbol.h"
#include "JNIV8ArrayBuffer.h"
#include "JNIV8TypedArray.h"
#include "v8.h"

#include <string>
Expand Down Expand Up @@ -45,6 +46,7 @@ void JNIV8Wrapper::init() {
JNIV8Wrapper::registerObject<JNIV8Symbol>(JNIV8ObjectType::kWrapper);
JNIV8Wrapper::registerObject<JNIV8PromiseResolver>(JNIV8ObjectType::kWrapper);
JNIV8Wrapper::registerObject<JNIV8ArrayBuffer>(JNIV8ObjectType::kWrapper);
JNIV8Wrapper::registerObject<JNIV8TypedArray>(JNIV8ObjectType::kWrapper);

JNIEnv *env = JNIWrapper::getEnvironment();

Expand Down
17 changes: 17 additions & 0 deletions src/main/java/ag/boersego/bgjs/JNIV8TypedArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ag.boersego.bgjs;

import androidx.annotation.Keep;

final public class JNIV8TypedArray extends JNIV8Object {
//------------------------------------------------------------------------
// internal fields & methods
@Keep
JNIV8TypedArray(V8Engine engine, long jsObjPtr, Object[] arguments) {
super(engine, jsObjPtr, arguments);
}

/**
* returns the length of the array
*/
public native int getV8Length();
}

0 comments on commit 891405d

Please sign in to comment.