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

requested changes to java.c, configure.ac, and internal state to cobl… #150

Closed
wants to merge 9 commits into from
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
3 changes: 3 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
2024-06-12 Vedant Tewari <[email protected]>

* configure.ac: Add support for JNI (Java Native Interface)

2023-02-25 Ron Norman <[email protected]>

Expand Down
13 changes: 13 additions & 0 deletions DEPENDENCIES
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,19 @@ The following libraries ARE required WHEN :

JSON-C is distributed under Expat License.

5) JNI (Java Native Interface) support is used

BOTH runtime AND development components required.

One of the following:

o Java Development Kit (JDK) 8 or later
https://openjdk.java.net/

The JDK is distributed under various open-source licenses depending on the vendor and version. Common licenses include the GNU General Public License (GPL) and the Oracle Binary Code License Agreement.

To enable JNI support, ensure that the JDK is installed on your system, and set the appropriate environment variables (e.g., JAVA_HOME) to point to the JDK installation directory.


See HACKING if you wish to hack the GnuCOBOL source or build directly
from version control as this includes the list of additional tools
Expand Down
12 changes: 12 additions & 0 deletions DEPENDENCIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,15 @@ Support for GENERATE JSON is provided by *one* of the following:

JSON-C is distributed under Expat License.

JNI Support
------------

Support for JNI (Java Native Interface) is provided by:

* [Java Development Kit (JDK)](https://openjdk.java.net/) 8 or later.

The JDK is distributed under various open-source licenses depending on the vendor and version. Common licenses include the GNU General Public License (GPL) and the Oracle Binary Code License Agreement.

To enable JNI support, ensure that the JDK is installed on your system, and set the appropriate environment variables (e.g., JAVA_HOME) to point to the JDK installation directory.


85 changes: 66 additions & 19 deletions cobc/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ static struct literal_list *literal_cache = NULL;
static struct field_list *field_cache = NULL;
static struct field_list *local_field_cache = NULL;
static struct call_list *call_cache = NULL;
extern JavaVM *jvm;
extern JNIEnv *env;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure these are needed in the compiler (cobc) itself.

static struct call_list *func_call_cache = NULL;
static struct static_call_list *static_call_cache = NULL;
static struct base_list *base_cache = NULL;
Expand Down Expand Up @@ -387,6 +389,21 @@ lookup_source (const char *p)
return source_id++;
}

static void lookup_java_call(const char *p) {
struct call_list *clp;

for (clp = call_cache; clp; clp = clp->next) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's another list you need to manipulate in this function. Something like call_java_cache.

if (strcmp(p, clp->call_name) == 0) {
return;
}
}

clp = (struct call_list *)cob_malloc(sizeof(struct call_list));
clp->call_name = p;
clp->next = call_cache;
call_cache = clp;
}

static void
lookup_call (const char *p)
{
Expand Down Expand Up @@ -419,6 +436,24 @@ lookup_func_call (const char *p)
func_call_cache = clp;
}

static void
lookup_java_call (const char *p)
{
struct static_call_list *sclp;

for (sclp = static_call_cache; sclp; sclp = sclp->next) {
if (strcmp (p, sclp->call_name) == 0) {
return;
}
}
sclp = cobc_parse_malloc (sizeof (struct static_call_list));
sclp->call_name = p;
sclp->convention = convention;
sclp->return_type = return_type;
sclp->next = static_call_cache;
static_call_cache = sclp;
}

static void
lookup_static_call (const char *p, int convention, int return_type)
{
Expand Down Expand Up @@ -7070,7 +7105,7 @@ output_call (struct cb_call *p)
ret_ptr = 1;
}
system_call = NULL;

// check the name where it is translated, check if the string begins with java then encode the string differently
#ifdef _WIN32
if (p->convention & CB_CONV_STDCALL) {
convention = "_std";
Expand Down Expand Up @@ -7532,27 +7567,39 @@ output_call (struct cb_call *p)
if (name_is_literal_or_prototype) {
s = get_program_id_str (p->name);
name_str = cb_encode_program_id (s, 1, cb_fold_call);
lookup_call (name_str);
callname = s;
#ifdef HAVE_JNI
/* Distinguishing lookup from call*/
if(strncmp("Java.", name, 6) == 0) {
void static_java_method = lookup_static_call(s + 6, p->argv[0], COB_RETURN_NULL);
cob_call_java(static_java_method);
} else {
// rest
#endif
// we need to use lookup_java_call instead (implement)
lookup_java_call (name_str);
callname = s;

output_line ("if (call_%s.funcvoid == NULL || cob_glob_ptr->cob_physical_cancel)", name_str);
output_block_open ();
output_prefix ();
output_line ("if (call_%s.funcvoid == NULL || cob_glob_ptr->cob_physical_cancel)", name_str);
output_block_open ();
output_prefix ();

nlp = find_nested_prog_with_id (name_str);
if (nlp) {
output ("call_%s.funcint = %s_%d__;",
name_str, name_str,
nlp->nested_prog->toplev_count);
} else {
output ("call_%s.funcvoid = ", name_str);
output ("cob_resolve_cobol (");
output_string ((const unsigned char *)s,
(int)strlen (s), 0);
output (", %d, %d);", cb_fold_call, !p->stmt1);
nlp = find_nested_prog_with_id (name_str);
if (nlp) {
output ("call_%s.funcint = %s_%d__;",
name_str, name_str,
nlp->nested_prog->toplev_count);
} else {
output ("call_%s.funcvoid = ", name_str);
output ("cob_resolve_cobol (");
output_string ((const unsigned char *)s,
(int)strlen (s), 0);
output (", %d, %d);", cb_fold_call, !p->stmt1);
}
output_newline ();
output_block_close ();
#ifdef HAVE_JNI
}
output_newline ();
output_block_close ();
#endif
} else {
name_str = NULL;
needs_unifunc = 1;
Expand Down
43 changes: 42 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,22 @@ AC_DEFUN([AC_PROG_CXX], [])
AC_PROG_LN_S
AC_PROG_INSTALL

PRINT_VAR(JAVA_HOME)
PRINT_VAR(JAVA)
PRINT_VAR(JAVAC)
PRINT_VAR(JAVAH)
PRINT_VAR(JAR)
PRINT_VAR(JNI_INCLUDES)

AC_PATH_PROG(javahome,java, "/usr/bin")
javahome=`dirname ${javahome}`
echo "JAVAHOME=${javahome}"
for each in `ls ${javahome}/include` ;do
if [ test -d ${javahome}/include/${each} ] ;then
javaos=${each}
fi;
done;

AC_PROG_MAKE_SET
AC_LIB_RPATH

Expand All @@ -480,9 +496,34 @@ dnl AC_CHECK_HEADERS([stdint.h whcar.h malloc.h])
# mandatory:
AC_CHECK_HEADERS([sys/types.h signal.h stddef.h], [],
[AC_MSG_ERROR([mandatory header could not be found or included])])
AC_CHECK_HEADERS([jni.h], [have_jni=yes],
[AC_MSG_ERROR([jni.h is required but could not be found])])
AC_CHECK_HEADERS([jni_md.h], [],
[AC_MSG_ERROR([jni_md.h is required but could not be found])])
# optional:
AC_CHECK_HEADERS([sys/time.h locale.h fcntl.h dlfcn.h sys/wait.h sys/sysmacros.h])

AC_ARG_VAR([JAVA_HOME], [Java Runtime Environment (JRE) location])
AC_ARG_ENABLE([java],
[AC_HELP_STRING([--disable-java],
[disable Java Interoperatibility])])
case $target_cpu in
x86_64) JVM_ARCH=amd64 ;;
i?86) JVM_ARCH=i386 ;;
*) JVM_ARCH=$target_cpu ;;
esac
AC_SUBST([JVM_ARCH])
AS_IF([test X$enable_java_feature != Xno],
[AS_IF([test X$have_jni != Xyes],
[AC_MSG_FAILURE([The Java Native Interface is required for Java feature.])])
AS_IF([test -z "$JAVA_HOME"],
[AC_MSG_WARN([JAVA_HOME has not been set. JAVA_HOME must be set at run time to locate libjvm.])],
[save_LDFLAGS=$LDFLAGS
LDFLAGS="-L$JAVA_HOME/lib/$JVM_ARCH/client -L$JAVA_HOME/lib/$JVM_ARCH/server $LDFLAGS"
AC_CHECK_LIB([jvm], [JNI_CreateJavaVM], [LIBS=$LIBS],
[AC_MSG_WARN([no libjvm found at JAVA_HOME])])
AC_DEFINE([WITH_JNI], [1])
LDFLAGS=$save_LDFLAGS
])])

# Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST
Expand Down
5 changes: 5 additions & 0 deletions libcob/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
2024-06-12 Vedant Tewari <[email protected]>

* Makefile.am: Updated to include JNI-related files.
* coblocal.h: Added internal state for JNI support.
* java.c: Implemented JNI support for GnuCOBOL.

2023-06-02 Simon Sobisch <[email protected]>

Expand Down
6 changes: 6 additions & 0 deletions libcob/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
# You should have received a copy of the GNU General Public License
# along with GnuCOBOL. If not, see <https://www.gnu.org/licenses/>.

lib_LTLIBRARIES = libcob.la
libcob_la_SOURCES = common.c move.c numeric.c strings.c \
fileio.c call.c intrinsic.c termio.c screenio.c reportio.c cobgetopt.c \
java.c \
mlio.c coblocal.h cconv.c system.def profiling.c
Comment on lines +22 to +26
Copy link
Collaborator

@GitMensch GitMensch Jun 16, 2024

Choose a reason for hiding this comment

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

you likely just wanted to add java.c to the list below; later on you may include it conditional on a variable (similar but not identical to libodbci).

In any case that's an adjustment, so please add ,2024 to the copyright year of this file


if LOCAL_CJSON
nodist_libcob_la_SOURCES = cJSON.c
DISTCLEANFILES = cJSON.c cJSON.h
Expand Down
14 changes: 13 additions & 1 deletion libcob/coblocal.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
Copyright (C) 2007-2012, 2014-2022 Free Software Foundation, Inc.
Written by Roger While, Simon Sobisch, Ron Norman
Written by Roger While, Simon Sobisch, Ron Norman, Vedant Tewari

This file is part of GnuCOBOL.

Expand Down Expand Up @@ -449,6 +449,18 @@ COB_HIDDEN const char *cob_get_last_exception_name (void);
COB_EXPIMP void cob_field_to_string (const cob_field *, void *,
const size_t);
COB_HIDDEN void cob_parameter_check (const char *, const int);
COB_HIDDEN char* cob_get_strerror (void);

enum cob_case_modifier {
CCM_NONE,
CCM_LOWER,
CCM_UPPER,
CCM_LOWER_LOCALE,
CCM_UPPER_LOCALE
};

COB_HIDDEN int cob_field_to_string (const cob_field *, void *,
const size_t, const enum cob_case_modifier target_case);

COB_HIDDEN cob_settings *cob_get_settings_ptr (void);

Expand Down
11 changes: 11 additions & 0 deletions libcob/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,17 @@ struct cob_call_struct {
cob_call_union cob_cstr_cancel; /* Cancel entry */
};

#ifdef HAVE_JNI
typedef struct __cob_java_static_method {
jclass cls;
jmethodID mid;
} cob_java_handle;

COB_EXPIMP cob_java_handle* cob_resolve_java (const char* class_name, const char* method_name,
const char *type_signature);
COB_EXPIMP int cob_call_java (const cob_java_handle* nargs);
#endif

/* Screen structure */
typedef struct __cob_screen {
struct __cob_screen *next; /* Pointer to next */
Expand Down
Loading