fix: preserve fast natives with legacy fallback by Copilot · Pull Request #1941 · NativeScript/android · GitHub
Skip to content
Closed
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
108 changes: 76 additions & 32 deletions test-app/runtime/src/main/cpp/com_tns_Runtime.cpp
63 changes: 50 additions & 13 deletions test-app/runtime/src/main/java/com/tns/Runtime.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tns;

import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
Expand Down Expand Up @@ -31,15 +32,15 @@
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.Collections;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.RunnableFuture;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.Collections;
import dalvik.annotation.optimization.CriticalNative;
import dalvik.annotation.optimization.FastNative;

public class Runtime {
private static final boolean USE_FAST_NATIVE_METHODS = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O;

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

Expand All @@ -53,26 +54,62 @@ private native void initNativeScript(int runtimeId, String filesPath, String nat

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

@CriticalNative
private static native int generateNewObjectId(int runtimeId);
private static int generateNewObjectId(int runtimeId) {
if (USE_FAST_NATIVE_METHODS) {
return RuntimeNativeFast.generateNewObjectId(runtimeId);
}

return generateNewObjectIdLegacy(runtimeId);
}

private static native int generateNewObjectIdLegacy(int runtimeId);

private boolean notifyGc(int runtimeId) {
if (USE_FAST_NATIVE_METHODS) {
return RuntimeNativeFast.notifyGc(this, runtimeId);
}

return notifyGcLegacy(runtimeId);
}

@FastNative
private native boolean notifyGc(int runtimeId);
private native boolean notifyGcLegacy(int runtimeId);

private native void lock(int runtimeId);

private native void unlock(int runtimeId);

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

@CriticalNative
private static native int getCurrentRuntimeId();
private static int getCurrentRuntimeId() {
if (USE_FAST_NATIVE_METHODS) {
return RuntimeNativeFast.getCurrentRuntimeId();
}

return getCurrentRuntimeIdLegacy();
}

private static native int getCurrentRuntimeIdLegacy();

public static int getPointerSize() {
if (USE_FAST_NATIVE_METHODS) {
return RuntimeNativeFast.getPointerSize();
}

return getPointerSizeLegacy();
}

private static native int getPointerSizeLegacy();

public static void SetManualInstrumentationMode(String mode) {
if (USE_FAST_NATIVE_METHODS) {
RuntimeNativeFast.SetManualInstrumentationMode(mode);
return;
}

@CriticalNative
public static native int getPointerSize();
SetManualInstrumentationModeLegacy(mode);
}

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

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

Expand Down
24 changes: 24 additions & 0 deletions test-app/runtime/src/main/java/com/tns/RuntimeNativeFast.java
Loading