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
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
89 changes: 88 additions & 1 deletion cobc/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -7098,7 +7098,9 @@ output_java_call (struct cb_call *p)
char *last_dot;
char *method_name;
const char *class_name;
char method_signature[256] = "(";
Copy link
Contributor

Choose a reason for hiding this comment

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

For internal buffer sizes you have COB_*_BUFF definitions in libcob/common.h that you can use here as well. Typical signature length could exceed 256 without much effort I think (with long-enough Java package names). I'd go for COB_NORMAL_BUFF (2048) for now.

char* mangled;
struct cb_tree *ptr;

mangled = strdup(class_and_method_name);
for (size_t i = 0; i < strlen(mangled) + 1; i++) {
Expand All @@ -7115,18 +7117,103 @@ output_java_call (struct cb_call *p)
method_name = last_dot + 1;
class_name = class_and_method_name;

for(ptr = p->args; ptr != NULL; ptr = ptr->next) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Note: indentation is strange here. I think you need to adjust that in your settings for this project.

switch(CB_TREE_TAG(ptr)) {
case CB_TAG_INTEGER:
strcat(method_signature, "I");
break;
case CB_TAG_LONG:
strcat(method_signature, "J");
break;
case CB_TAG_FLOAT:
strcat(method_signature, "F");
break;
case CB_TAG_DOUBLE:
strcat(method_signature, "D");
break;
case CB_TAG_BOOLEAN:
strcat(method_signature, "Z");
break;
case CB_TAG_BYTE:
strcat(method_signature, "B");
break;
case CB_TAG_SHORT:
strcat(method_signature, "S");
break;
case CB_TAG_CHAR:
strcat(method_signature, "C");
break;
case CB_TAG_STRING:
strcat(method_signature, "Ljava/lang/String;");
break;
case CB_TAG_OBJECT:
strcat(method_signature, "Ljava/lang/Object;");
break;
case CB_TAG_LITERAL:
if(CB_TREE_CATEGORY(ptr) == CB_CATEGORY_NUMERIC) {
strcat(method_signature, "I");
} else if(CB_TREE_CATEGORY(ptr) == CB_CATEGORY_ALPHANUMERIC) {
strcat(method_signature, "Ljava/lang/String;");
}
break;
case CB_TAG_ARRAY:
switch(CB_TREE_TAG(CB_TREE(ptr)->next)) {
case CB_TAG_INTEGER:
strcat(method_signature, "[I");
break;
case CB_TAG_LONG:
strcat(method_signature, "[J");
break;
case CB_TAG_FLOAT:
strcat(method_signature, "[F");
break;
case CB_TAG_DOUBLE:
strcat(method_signature, "[D");
break;
case CB_TAG_BOOLEAN:
strcat(method_signature, "[Z");
break;
case CB_TAG_BYTE:
strcat(method_signature, "[B");
break;
case CB_TAG_SHORT:
strcat(method_signature, "[S");
break;
case CB_TAG_CHAR:
strcat(method_signature, "[C");
break;
case CB_TAG_STRING:
strcat(method_signature, "[Ljava/lang/String;");
break;
case CB_TAG_OBJECT:
strcat(method_signature, "[Ljava/lang/Object;");
break;
default:
cobc_err_msg(_("Unsupported array type in Java method call"));
COBC_ABORT();
}
break;
default:
cobc_err_msg(_("Unsupported argument type in Java method call"));
COBC_ABORT();
}
}

strcat(method_signature, ")V");

lookup_java_call(mangled);
output_line("if (call_java_%s == NULL)", mangled);
output_block_open();

output_prefix();
output_line("call_java_%s = ", mangled);
output("cob_resolve_java(\"%s\", \"%s\", \"()V\");", class_name, method_name);
output("cob_resolve_java(\"%s\", \"%s\", \"()V\");", class_name, method_name, method_signature);
Copy link
Contributor

Choose a reason for hiding this comment

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

Here you should probable replace ()V with %s so the method signature is included.

output_newline ();
output_prefix ();
output_line("cob_call_java(call_java_%s);\n", mangled);
output_newline();
output_block_close();
cobc_free(mangled);
Copy link
Contributor

Choose a reason for hiding this comment

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

This will likely cause a use-after-free since mangled is referenced in the list created by lookup_java_call.

}

static void
Expand Down