|
| 1 | +// JNI shim for nodejs-mobile. Adapted from |
| 2 | +// JaneaSystems/nodejs-mobile-samples/android/native-gradle/.../native-lib.cpp |
| 3 | +// (MIT). Two responsibilities: |
| 4 | +// |
| 5 | +// 1. startNodeWithArguments(env, argv) → invokes node::Start on the calling |
| 6 | +// thread, with the args the Kotlin side built. Blocks until Node exits. |
| 7 | +// 2. Pipe stdout/stderr to logcat — otherwise Fastify's startup logs vanish. |
| 8 | + |
| 9 | +#include <jni.h> |
| 10 | +#include <android/log.h> |
| 11 | +#include <cstdlib> |
| 12 | +#include <cstring> |
| 13 | +#include <pthread.h> |
| 14 | +#include <unistd.h> |
| 15 | + |
| 16 | +// Forward-declared from libnode; the real signature lives in node.h. |
| 17 | +namespace node { |
| 18 | + int Start(int argc, char* argv[]); |
| 19 | +} |
| 20 | + |
| 21 | +#define LOG_TAG "jss-android" |
| 22 | +#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) |
| 23 | +#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) |
| 24 | + |
| 25 | +// ----- stdout/stderr → logcat ----------------------------------------------- |
| 26 | + |
| 27 | +static int s_pipe_stdout[2]; |
| 28 | +static int s_pipe_stderr[2]; |
| 29 | +static pthread_t s_thread_stdout; |
| 30 | +static pthread_t s_thread_stderr; |
| 31 | + |
| 32 | +static void* pump(void* arg) { |
| 33 | + int fd = *static_cast<int*>(arg); |
| 34 | + int prio = (fd == s_pipe_stdout[0]) ? ANDROID_LOG_INFO : ANDROID_LOG_ERROR; |
| 35 | + char buf[512]; |
| 36 | + ssize_t n; |
| 37 | + while ((n = read(fd, buf, sizeof(buf) - 1)) > 0) { |
| 38 | + if (buf[n - 1] == '\n') --n; |
| 39 | + buf[n] = 0; |
| 40 | + __android_log_write(prio, LOG_TAG, buf); |
| 41 | + } |
| 42 | + return nullptr; |
| 43 | +} |
| 44 | + |
| 45 | +static void redirect_stdio_to_logcat() { |
| 46 | + setvbuf(stdout, nullptr, _IOLBF, 0); |
| 47 | + setvbuf(stderr, nullptr, _IONBF, 0); |
| 48 | + pipe(s_pipe_stdout); pipe(s_pipe_stderr); |
| 49 | + dup2(s_pipe_stdout[1], STDOUT_FILENO); |
| 50 | + dup2(s_pipe_stderr[1], STDERR_FILENO); |
| 51 | + pthread_create(&s_thread_stdout, nullptr, pump, &s_pipe_stdout[0]); |
| 52 | + pthread_create(&s_thread_stderr, nullptr, pump, &s_pipe_stderr[0]); |
| 53 | + pthread_detach(s_thread_stdout); |
| 54 | + pthread_detach(s_thread_stderr); |
| 55 | +} |
| 56 | + |
| 57 | +// ----- JNI entry point ------------------------------------------------------- |
| 58 | + |
| 59 | +extern "C" JNIEXPORT jint JNICALL |
| 60 | +Java_live_jss_jss_1android_NodeBridge_startNodeWithArguments( |
| 61 | + JNIEnv* env, jclass /*clazz*/, jobjectArray jArgv) { |
| 62 | + |
| 63 | + static bool stdio_redirected = false; |
| 64 | + if (!stdio_redirected) { |
| 65 | + redirect_stdio_to_logcat(); |
| 66 | + stdio_redirected = true; |
| 67 | + } |
| 68 | + |
| 69 | + jsize argc = env->GetArrayLength(jArgv); |
| 70 | + if (argc <= 0) { |
| 71 | + LOGE("startNodeWithArguments called with empty argv"); |
| 72 | + return -1; |
| 73 | + } |
| 74 | + |
| 75 | + auto argv = static_cast<char**>(calloc(argc + 1, sizeof(char*))); |
| 76 | + for (jsize i = 0; i < argc; i++) { |
| 77 | + auto jArg = static_cast<jstring>(env->GetObjectArrayElement(jArgv, i)); |
| 78 | + const char* utf = env->GetStringUTFChars(jArg, nullptr); |
| 79 | + argv[i] = strdup(utf); |
| 80 | + env->ReleaseStringUTFChars(jArg, utf); |
| 81 | + env->DeleteLocalRef(jArg); |
| 82 | + } |
| 83 | + argv[argc] = nullptr; |
| 84 | + |
| 85 | + LOGI("node::Start with %d args, argv[0]=%s, argv[1]=%s", |
| 86 | + argc, argv[0], argc > 1 ? argv[1] : "(none)"); |
| 87 | + |
| 88 | + int rc = node::Start(argc, argv); |
| 89 | + |
| 90 | + LOGI("node::Start returned %d", rc); |
| 91 | + |
| 92 | + for (int i = 0; i < argc; i++) free(argv[i]); |
| 93 | + free(argv); |
| 94 | + return rc; |
| 95 | +} |
0 commit comments