<commit_before>
import dynlib
import strutils
import typetraits
import macros
import os
import osproc

const jniHeader = "jni.h"

proc jFileOrDirExists(path: string): bool =
    when nimvm:
        let res = when defined(windows):
                staticExec("IF EXISTS \"" & path & "\" ( echo true ) ELSE ( echo false ) ")
            else:
                staticExec("if [ -e \"" & path & "\" ]; then echo true; else echo false; fi")
        result = res == "true"
    else:
        result = fileExists(path) or dirExists(path)

proc jExecProcess(path: string): string =
    when nimvm:
        result = staticExec(path)
    else:
        result = string(execProcess(path))

proc getJavaHome*(): string =
    if getEnv("JAVA_HOME").len > 0:
        result = getEnv("JAVA_HOME")
    elif jFileOrDirExists("/usr/libexec/java_home"):
        result = jExecProcess("/usr/libexec/java_home")
    elif jFileOrDirExists("/usr/lib/jvm/default-java"):
        result = "/usr/lib/jvm/default-java"

const JAVA_HOME = getJavaHome()
static: assert(JAVA_HOME.len > 0, "Java home not found")

type JavaVMPtr* {.header: jniHeader.} = pointer
type JNIEnv* {.header: jniHeader.} = object
type JNIEnvPtr* = ptr JNIEnv

var currentEnv* : JNIEnvPtr

const JNI_INCLUDE_DIR = JAVA_HOME & "/include"

{.passC: "-I" & JNI_INCLUDE_DIR.}

when defined macosx:
    {.emit: """
    #include <CoreFoundation/CoreFoundation.h>
    """.}
    {.passC: "-I" & JNI_INCLUDE_DIR & "/darwin".}
    {.passL: "-framework CoreFoundation".}

type JavaVM* = ref object of RootObj
    env*: JNIEnvPtr

type JavaVMOption* {.header: jniHeader.} = object
    optionString: cstring
    extraInfo: pointer

type JavaError* = object of Exception
    className*: string
    fullStackTrace*: string

type jint* {.header: jniHeader.} = cint
type jsize* {.header: jniHeader.} = jint
type jchar* {.header: jniHeader.} = uint16
type jlong* {.header: jniHeader.} = int64
type jshort* {.header: jniHeader.} = int16
type jbyte* {.header: jniHeader.} = int8
type jfloat* {.header: jniHeader.} = cfloat
type jdouble* {.header: jniHeader.} = cdouble
type jboolean* {.header: jniHeader.} = uint8
type jclass* {.header: jniHeader.} = distinct pointer
type jmethodID* {.header: jniHeader.} = pointer
type jobject* {.header: jniHeader.} = pointer
type jfieldID* {.header: jniHeader.} = pointer
type jstring* {.header: jniHeader.} = jobject
type jthrowable* {.header: jniHeader.} = jobject
type jarray* {.header: jniHeader.} = jobject
type jobjectArray* {.header: jniHeader.} = jarray

proc `isNil`* (x: jclass): bool {.borrow.}

{.warning[SmallLshouldNotBeUsed]: off.}
type jvalue* {.header: jniHeader, union.} = object
    z: jboolean
    b: jbyte
    c: jchar
    s: jshort
    i: jint
    j: jlong
    f: jfloat
    d: jdouble
    l: jobject

type JavaVMInitArgs* {.header: jniHeader.} = object
    version: jint

    nOptions: jint
    options: ptr JavaVMOption
    ignoreUnrecognized: jboolean

var JNI_VERSION_1_1* {.header: jniHeader.} : jint
var JNI_VERSION_1_2* {.header: jniHeader.} : jint
var JNI_VERSION_1_4* {.header: jniHeader.} : jint
var JNI_VERSION_1_6* {.header: jniHeader.} : jint
var JNI_VERSION_1_8* {.header: jniHeader.} : jint

var JNI_CreateJavaVM: proc (pvm: ptr JavaVMPtr, penv: ptr pointer, args: pointer): jint {.cdecl.}
var JNI_GetDefaultJavaVMInitArgs: proc(vm_args: ptr JavaVMInitArgs): jint {.cdecl.}
var JNI_GetCreatedJavaVMs: proc(vmBuf: ptr JavaVMPtr, bufLen: jsize, nVMs: ptr jsize): jint {.cdecl.}

when not defined(macosx):
    proc linkWithJVMModule(handle: LibHandle) =
        JNI_CreateJavaVM = cast[type(JNI_CreateJavaVM)](symAddr(handle, "JNI_CreateJavaVM"))
        JNI_GetDefaultJavaVMInitArgs = cast[type(JNI_GetDefaultJavaVMInitArgs)](symAddr(handle, "JNI_GetDefaultJavaVMInitArgs"))
        JNI_GetCreatedJavaVMs = cast[type(JNI_GetCreatedJavaVMs)](symAddr(handle, "JNI_GetCreatedJavaVMs"))

    proc findJVMLib(): string =
        let home = getJavaHome()
        when defined(windows):
            result = home & "\\jre\\lib\\jvm.dll"
            if fileExists(result): return
        else:
            result = home & "/jre/lib/libjvm.so"
            if fileExists(result): return
            result = home & "/jre/lib/libjvm.dylib"
            if fileExists(result): return
            when hostCpu == "amd64":
                # Ubuntu
                result = home & "/jre/lib/amd64/jamvm/libjvm.so"
                if fileExists(result): return
        # libjvm not found
        result = nil
<commit_msg>Added one more search path<commit_after>
import dynlib
import strutils
import typetraits
import macros
import os
import osproc

const jniHeader = "jni.h"

proc jFileOrDirExists(path: string): bool =
    when nimvm:
        let res = when defined(windows):
                staticExec("IF EXISTS \"" & path & "\" ( echo true ) ELSE ( echo false ) ")
            else:
                staticExec("if [ -e \"" & path & "\" ]; then echo true; else echo false; fi")
        result = res == "true"
    else:
        result = fileExists(path) or dirExists(path)

proc jExecProcess(path: string): string =
    when nimvm:
        result = staticExec(path)
    else:
        result = string(execProcess(path))

proc getJavaHome*(): string =
    if getEnv("JAVA_HOME").len > 0:
        result = getEnv("JAVA_HOME")
    elif jFileOrDirExists("/usr/libexec/java_home"):
        result = jExecProcess("/usr/libexec/java_home")
    elif jFileOrDirExists("/usr/lib/jvm/default-java"):
        result = "/usr/lib/jvm/default-java"

const JAVA_HOME = getJavaHome()
static: assert(JAVA_HOME.len > 0, "Java home not found")

type JavaVMPtr* {.header: jniHeader.} = pointer
type JNIEnv* {.header: jniHeader.} = object
type JNIEnvPtr* = ptr JNIEnv

var currentEnv* : JNIEnvPtr

const JNI_INCLUDE_DIR = JAVA_HOME & "/include"

{.passC: "-I" & JNI_INCLUDE_DIR.}

when defined macosx:
    {.emit: """
    #include <CoreFoundation/CoreFoundation.h>
    """.}
    {.passC: "-I" & JNI_INCLUDE_DIR & "/darwin".}
    {.passL: "-framework CoreFoundation".}

type JavaVM* = ref object of RootObj
    env*: JNIEnvPtr

type JavaVMOption* {.header: jniHeader.} = object
    optionString: cstring
    extraInfo: pointer

type JavaError* = object of Exception
    className*: string
    fullStackTrace*: string

type jint* {.header: jniHeader.} = cint
type jsize* {.header: jniHeader.} = jint
type jchar* {.header: jniHeader.} = uint16
type jlong* {.header: jniHeader.} = int64
type jshort* {.header: jniHeader.} = int16
type jbyte* {.header: jniHeader.} = int8
type jfloat* {.header: jniHeader.} = cfloat
type jdouble* {.header: jniHeader.} = cdouble
type jboolean* {.header: jniHeader.} = uint8
type jclass* {.header: jniHeader.} = distinct pointer
type jmethodID* {.header: jniHeader.} = pointer
type jobject* {.header: jniHeader.} = pointer
type jfieldID* {.header: jniHeader.} = pointer
type jstring* {.header: jniHeader.} = jobject
type jthrowable* {.header: jniHeader.} = jobject
type jarray* {.header: jniHeader.} = jobject
type jobjectArray* {.header: jniHeader.} = jarray

proc `isNil`* (x: jclass): bool {.borrow.}

{.warning[SmallLshouldNotBeUsed]: off.}
type jvalue* {.header: jniHeader, union.} = object
    z: jboolean
    b: jbyte
    c: jchar
    s: jshort
    i: jint
    j: jlong
    f: jfloat
    d: jdouble
    l: jobject

type JavaVMInitArgs* {.header: jniHeader.} = object
    version: jint

    nOptions: jint
    options: ptr JavaVMOption
    ignoreUnrecognized: jboolean

var JNI_VERSION_1_1* {.header: jniHeader.} : jint
var JNI_VERSION_1_2* {.header: jniHeader.} : jint
var JNI_VERSION_1_4* {.header: jniHeader.} : jint
var JNI_VERSION_1_6* {.header: jniHeader.} : jint
var JNI_VERSION_1_8* {.header: jniHeader.} : jint

var JNI_CreateJavaVM: proc (pvm: ptr JavaVMPtr, penv: ptr pointer, args: pointer): jint {.cdecl.}
var JNI_GetDefaultJavaVMInitArgs: proc(vm_args: ptr JavaVMInitArgs): jint {.cdecl.}
var JNI_GetCreatedJavaVMs: proc(vmBuf: ptr JavaVMPtr, bufLen: jsize, nVMs: ptr jsize): jint {.cdecl.}

when not defined(macosx):
    proc linkWithJVMModule(handle: LibHandle) =
        JNI_CreateJavaVM = cast[type(JNI_CreateJavaVM)](symAddr(handle, "JNI_CreateJavaVM"))
        JNI_GetDefaultJavaVMInitArgs = cast[type(JNI_GetDefaultJavaVMInitArgs)](symAddr(handle, "JNI_GetDefaultJavaVMInitArgs"))
        JNI_GetCreatedJavaVMs = cast[type(JNI_GetCreatedJavaVMs)](symAddr(handle, "JNI_GetCreatedJavaVMs"))

    proc findJVMLib(): string =
        let home = getJavaHome()
        when defined(windows):
            result = home & "\\jre\\lib\\jvm.dll"
            if fileExists(result): return
        else:
            result = home & "/jre/lib/libjvm.so"
            if fileExists(result): return
            result = home & "/jre/lib/libjvm.dylib"
            if fileExists(result): return
            when hostCpu == "amd64":
                # Ubuntu
                result = home & "/jre/lib/amd64/jamvm/libjvm.so"
                if fileExists(result): return
                result = home & "/jre/lib/amd64/server/libjvm.so"
                if fileExists(result): return
        # libjvm not found
        result = nil

{'fraction_non_alphanumeric': 0.1275359140256607, 'fraction_numerical': 0.00548305735277991, 'mean_word_length': 3.185406149609913, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7597857', 'n_tokens_mistral': 3202, 'n_tokens_neox': 3112, 'n_words': 808}