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: dynamically register jni methods and use @FastNative & @CriticalNative #1792

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Using the new annotations to improve the runtime for our jni calls
triniwiz committed Nov 8, 2023
commit 04216ae454f0254ca214fdedcbb1a3493117a24e
7 changes: 7 additions & 0 deletions test-app/app/src/main/java/com/tns/AndroidJsV8Inspector.java
Original file line number Diff line number Diff line change
@@ -19,6 +19,8 @@
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;

import dalvik.annotation.optimization.CriticalNative;
import dalvik.annotation.optimization.FastNative;
import fi.iki.elonen.NanoHTTPD;
import fi.iki.elonen.NanoWSD;

@@ -27,14 +29,19 @@ class AndroidJsV8Inspector {
private static String ApplicationDir;
private String packageName;

@CriticalNative
protected native final void init();

@FastNative
protected native final void connect(Object connection);

@CriticalNative
private native void scheduleBreak();

@CriticalNative
protected static native void disconnect();

@FastNative
protected native final void dispatchMessage(String message);

private Handler mainHandler;
28 changes: 21 additions & 7 deletions test-app/runtime/src/main/cpp/com_tns_AndroidJsV8Inspector.cpp
Original file line number Diff line number Diff line change
@@ -3,28 +3,42 @@
#include <sstream>
#include "JsV8InspectorClient.h"
#include "ArgConverter.h"
#include "com_tns_AndroidJsV8Inspector.h"

using namespace tns;
using namespace std;

JNIEXPORT extern "C" void Java_com_tns_AndroidJsV8Inspector_init(JNIEnv* env, jobject object) {
JsV8InspectorClient::GetInstance()->init();
}

JNIEXPORT extern "C" void Java_com_tns_AndroidJsV8Inspector_connect(JNIEnv* env, jobject instance, jobject connection) {
JNIEXPORT JNICALL void connect(JNIEnv *env, jobject instance, jobject connection) {
JsV8InspectorClient::GetInstance()->disconnect();
JsV8InspectorClient::GetInstance()->connect(connection);
}

JNIEXPORT extern "C" void Java_com_tns_AndroidJsV8Inspector_scheduleBreak(JNIEnv* env, jobject instance) {
JNIEXPORT JNICALL void scheduleBreak(JNIEnv *env, jobject instance) {
JsV8InspectorClient::GetInstance()->scheduleBreak();
}

JNIEXPORT extern "C" void Java_com_tns_AndroidJsV8Inspector_disconnect(JNIEnv* env, jobject instance) {
JNIEXPORT JNICALL void scheduleBreakCritical() {
JsV8InspectorClient::GetInstance()->scheduleBreak();
}

JNIEXPORT JNICALL void disconnect(JNIEnv *env, jobject instance) {
JsV8InspectorClient::GetInstance()->disconnect();
}

JNIEXPORT JNICALL void disconnectCritical() {
JsV8InspectorClient::GetInstance()->disconnect();
}

JNIEXPORT extern "C" void Java_com_tns_AndroidJsV8Inspector_dispatchMessage(JNIEnv* env, jobject instance, jstring jMessage) {
JNIEXPORT JNICALL void dispatchMessage(JNIEnv *env, jobject instance, jstring jMessage) {
std::string message = ArgConverter::jstringToString(jMessage);
JsV8InspectorClient::GetInstance()->dispatchMessage(message);
}

JNIEXPORT JNICALL void init(JNIEnv *env, jobject object) {
JsV8InspectorClient::GetInstance()->init();
}

JNIEXPORT JNICALL void initCritical() {
JsV8InspectorClient::GetInstance()->init();
}
35 changes: 35 additions & 0 deletions test-app/runtime/src/main/cpp/com_tns_AndroidJsV8Inspector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// Created by Osei Fortune on 07/11/2023.
//
#include "jni.h"
#include "JEnv.h"
#include "NativeScriptException.h"
#include <sstream>
#include "JsV8InspectorClient.h"
#include "ArgConverter.h"


using namespace tns;
using namespace std;

#ifndef COM_TNS_ANDROIDJSV8INSPECTOR_H
#define COM_TNS_ANDROIDJSV8INSPECTOR_H

void init(JNIEnv *env, jobject object);

void initCritical();

void connect(JNIEnv *env, jobject instance, jobject connection);

void scheduleBreak(JNIEnv *env, jobject instance);

void scheduleBreakCritical();

void disconnect(JNIEnv *env, jobject instance);

void disconnectCritical();

void dispatchMessage(JNIEnv *env, jobject instance, jstring jMessage);


#endif //COM_TNS_ANDROIDJSV8INSPECTOR_H
2 changes: 1 addition & 1 deletion test-app/runtime/src/main/cpp/com_tns_AssetExtractor.cpp
Original file line number Diff line number Diff line change
@@ -7,7 +7,7 @@
using namespace tns;
using namespace std;

extern "C" JNIEXPORT void Java_com_tns_AssetExtractor_extractAssets(JNIEnv* env, jobject obj, jstring apk, jstring inputDir, jstring outputDir, jboolean _forceOverwrite) {
JNIEXPORT JNICALL void extractAssets(JNIEnv* env, jobject obj, jstring apk, jstring inputDir, jstring outputDir, jboolean _forceOverwrite) {
try {
AssetExtractor::ExtractAssets(env, obj, apk, inputDir, outputDir, _forceOverwrite);
} catch (NativeScriptException& e) {
19 changes: 19 additions & 0 deletions test-app/runtime/src/main/cpp/com_tns_AssetExtractor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Created by Osei Fortune on 07/11/2023.
//

#include "jni.h"
#include "AssetExtractor.h"
#include "NativeScriptException.h"

#include <sstream>

#ifndef COM_TNS_ASSETEXTRACTOR_H
#define COM_TNS_ASSETEXTRACTOR_H

using namespace std;
using namespace tns;

void extractAssets(JNIEnv* env, jobject obj, jstring apk, jstring inputDir, jstring outputDir, jboolean _forceOverwrite);

#endif //COM_TNS_ASSETEXTRACTOR_H
268 changes: 233 additions & 35 deletions test-app/runtime/src/main/cpp/com_tns_Runtime.cpp

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions test-app/runtime/src/main/java/com/tns/AssetExtractor.java
Original file line number Diff line number Diff line change
@@ -6,7 +6,10 @@
import android.content.Context;
import android.util.Log;

import dalvik.annotation.optimization.FastNative;

public class AssetExtractor {
@FastNative
private native void extractAssets(String apkPath, String input, String outputDir, boolean checkForNewerFiles);
private final Logger logger;

23 changes: 23 additions & 0 deletions test-app/runtime/src/main/java/com/tns/Runtime.java
Original file line number Diff line number Diff line change
@@ -36,46 +36,69 @@
import java.util.concurrent.RunnableFuture;
import java.util.concurrent.atomic.AtomicInteger;

import dalvik.annotation.optimization.CriticalNative;
import dalvik.annotation.optimization.FastNative;

public class Runtime {
@FastNative
private native void initNativeScript(int runtimeId, String filesPath, String nativeLibDir, boolean verboseLoggingEnabled, boolean isDebuggable, String packageName,
Object[] v8Options, String callingDir, int maxLogcatObjectSize, boolean forceLog);

@FastNative
private native void runModule(int runtimeId, String filePath) throws NativeScriptException;

@FastNative
private native void runWorker(int runtimeId, String filePath) throws NativeScriptException;

@FastNative
private native Object runScript(int runtimeId, String filePath) throws NativeScriptException;

@FastNative
private native Object callJSMethodNative(int runtimeId, int javaObjectID, String methodName, int retType, boolean isConstructor, Object... packagedArgs) throws NativeScriptException;
Comment on lines +47 to 57
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if these should be FastNative. FastNative completely disables GC while it's running, and these could likely run for a good while.

callJSMethodNative is even trickier because we can go java->native->java->native multiple times in long running methods

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the fastest we can go for those would be !


@FastNative
private native void createJSInstanceNative(int runtimeId, Object javaObject, int javaObjectID, String canonicalName);

@FastNative
private native int generateNewObjectId(int runtimeId);

@FastNative
private native boolean notifyGc(int runtimeId);

@FastNative
private native void lock(int runtimeId);

@FastNative
private native void unlock(int runtimeId);

@FastNative
private native void passExceptionToJsNative(int runtimeId, Throwable ex, String message, String fullStackTrace, String jsStackTrace, boolean isDiscarded);

@FastNative
private static native int getCurrentRuntimeId();

@FastNative
public static native int getPointerSize();

@FastNative
public static native void SetManualInstrumentationMode(String mode);

@FastNative
private static native void WorkerGlobalOnMessageCallback(int runtimeId, String message);

@FastNative
private static native void WorkerObjectOnMessageCallback(int runtimeId, int workerId, String message);

@FastNative
private static native void TerminateWorkerCallback(int runtimeId);

@FastNative
private static native void ClearWorkerPersistent(int runtimeId, int workerId);

@FastNative
private static native void CallWorkerObjectOnErrorHandleMain(int runtimeId, int workerId, String message, String stackTrace, String filename, int lineno, String threadName) throws NativeScriptException;

@FastNative
private static native void ResetDateTimeConfigurationCache(int runtimeId);

void passUncaughtExceptionToJs(Throwable ex, String message, String fullStackTrace, String jsStackTrace) {
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dalvik.annotation.optimization;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.CLASS) // Save memory, don't instantiate as an object at runtime.
@Target(ElementType.METHOD)
public @interface CriticalNative {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dalvik.annotation.optimization;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.CLASS)
@Target({ElementType.METHOD})
public @interface FastNative {
}