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

Initial Handling of Non-Void Java Method Calls in GnuCOBOL #174

Open
wants to merge 6 commits into
base: java-interop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
105 changes: 77 additions & 28 deletions cobc/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -7100,6 +7100,7 @@ output_java_call (struct cb_call *p)
char *last_dot;
char *method_name;
const char *class_name;
char return_type_signature[32];
char method_signature[2048] = "(";
Copy link
Collaborator

Choose a reason for hiding this comment

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

use COB_MINI_BUFF and COB_NORMAL_BUFF for those and down use strncat with the _MAX defines

char* mangled;
struct cb_tree_common *ptr;
Expand Down Expand Up @@ -7146,34 +7147,50 @@ output_java_call (struct cb_call *p)
strcat(method_signature, "Ljava/lang/String;");
}
break;
case CB_TAG_LIST:
{
struct cb_tree_common **list_elements = (struct cb_tree_common **) ptr;
for (int j = 0; list_elements[j] != NULL; j++) {
switch (CB_TREE_TAG(list_elements[j])) {
case CB_TAG_INTEGER:
strcat(method_signature, "[I");
break;
case CB_USAGE_FLOAT:
strcat(method_signature, "[F");
break;
case CB_USAGE_DOUBLE:
strcat(method_signature, "[D");
break;
case CB_CLASS_BOOLEAN:
strcat(method_signature, "[Z");
break;
case CB_TAG_STRING:
strcat(method_signature, "[Ljava/lang/String;");
break;
case CB_USAGE_OBJECT:
strcat(method_signature, "[Ljava/lang/Object;");
break;
default:
cobc_err_msg(_("Unsupported array type in Java method call"));
COBC_ABORT();
}
}
case CB_TAG_LIST:
{
struct cb_tree_common **list_elements = (struct cb_tree_common **) ptr;
int array_dimension = 1;

while (list_elements != NULL) {
struct cb_tree_common **inner_list = NULL;
for (int j = 0; list_elements[j] != NULL; j++) {
if (CB_TREE_TAG(list_elements[j]) == CB_TAG_LIST) {
array_dimension++;
inner_list = (struct cb_tree_common **) list_elements[j];
} else {
switch (CB_TREE_TAG(list_elements[j])) {
case CB_TAG_INTEGER:
strcat(method_signature, "[I");
break;
Copy link
Collaborator

@GitMensch GitMensch Sep 12, 2024

Choose a reason for hiding this comment

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

In case of integer we further have to check the type per usage

pic/type java type
PIC S9(n) COMP-5, where ≥ 1 n ≤ 4 short
PIC S9(n) COMP-5, where ≥ 5 n ≤ 9 int
PIC S9(n) COMP-5, where ≥ 10 n ≤ 18 long
usage COMP-3/PACKED-DECIMAL/DISPLAY numeric java.math.BigDecimal

Note that checking the TAGs and USAGEs and CLASSes in a single switch doesn't work (check with the debugger to get more details).

Also note that PIC X (=single byte) is a special case:

  • if it has two or one level88 below it with value x'01' / x'00' --> boolean
  • otherwise: byte

For all of the types you have (after addition of the above) there needs to be a check for the occurs attribute ("more than 1 dimension" will already be checked in the conformance checks) and if existing just add [].

As the valid types are identical for both arguments and returning items, their generation should be moved out to a static helper function called for each of the parameters (the loop here) and the returning item below.

case CB_USAGE_FLOAT:
strcat(method_signature, "[F");
break;
case CB_USAGE_DOUBLE:
strcat(method_signature, "[D");
break;
case CB_CLASS_BOOLEAN:
strcat(method_signature, "[Z");
break;
case CB_TAG_STRING:
strcat(method_signature, "[Ljava/lang/String;");
break;
case CB_USAGE_OBJECT:
strcat(method_signature, "[Ljava/lang/Object;");
break;
default:
cobc_err_msg(_("Unsupported array element type in Java method call"));
COBC_ABORT();
}
}
}
list_elements = inner_list;
}

if (array_dimension > 2) {
cobc_err_msg(_("Unsupported array dimension: %d"), array_dimension);
COBC_ABORT();
Copy link
Collaborator

@GitMensch GitMensch Sep 12, 2024

Choose a reason for hiding this comment

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

those two messages should be errors in cb_check_conformance as well as a check for "only field identifiers" (= no literals/functions/...) - which then also allows above to directly cast to a field pointer and operate on this; additional, for parameters passed BY REFERENCE that are a String/BigDecimal mapping item there should be a warning "immutable type implicit passed BY CONTENT".

}
}
break;
default:
Expand All @@ -7182,6 +7199,38 @@ output_java_call (struct cb_call *p)
}
}

if (p->call_returning == NULL) {
strcat(method_signature, ")V");
strcpy(return_type_signature, "void");
} else {
switch(CB_TREE_TAG(p->call_returning)) {
case CB_TAG_INTEGER:
strcat(method_signature, ")I");
strcpy(return_type_signature, "jint");
break;
case CB_TAG_STRING:
strcat(method_signature, ")Ljava/lang/String;");
strcpy(return_type_signature, "jstring");
break;
case CB_USAGE_FLOAT:
strcat(method_signature, ")F");
strcpy(return_type_signature, "jfloat");
break;
case CB_USAGE_DOUBLE:
strcat(method_signature, ")D");
strcpy(return_type_signature, "jdouble");
break;
case CB_CLASS_BOOLEAN:
strcat(method_signature, ")Z");
strcpy(return_type_signature, "jboolean");
break;
default:
strcat(method_signature, ")V");
strcpy(return_type_signature, "void");
break;
}
}

strcat(method_signature, ")V");

lookup_java_call(mangled, method_signature);
Expand Down
4 changes: 4 additions & 0 deletions libcob/java.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,15 @@ resolve_java (const char *class_name,
cls = (*env)->FindClass(env, jni_class_name);
free(jni_class_name);
if (!cls) {
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);
Comment on lines +82 to +83
Copy link
Collaborator

Choose a reason for hiding this comment

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

fine during development, needs to be adjusted later - see review on the other PR

return NULL;
}

mid = (*env)->GetStaticMethodID(env, cls, method_name, method_signature);
if (!mid) {
(*env)->ExceptionDescribe(env);
(*env)->ExceptionClear(env);
(*env)->DeleteLocalRef(env, cls);
return NULL;
}
Expand Down