Skip to content

Commit a5c39f0

Browse files
Implement java.io.File in vm/JavaAPI (#4324)
* Implement java.io.File in vm/JavaAPI with native methods and integration test. Implemented `java.io.File` using native methods in `vm/ByteCodeTranslator/src/nativeMethods.m` utilizing `NSFileManager` for iOS compatibility. Added comprehensive `FileClassIntegrationTest` which verifies the implementation by compiling Java source to C, linking with a POSIX-based native mock (for Linux CI compatibility), and executing the resulting binary. The test ensures correct Java-to-Native binding and basic file system operations. * Implement java.io.File in JavaAPI with native support for ParparVM - Added full implementation of `java.io.File` in `vm/JavaAPI` delegating to native methods. - Added native implementation in `vm/ByteCodeTranslator/src/java_io_File.m` supporting both iOS (`NSFileManager`) and Linux/POSIX. - Updated `ByteCodeTranslator` to package the new native source file. - Patched `vm/ByteCodeTranslator/src/nativeMethods.m` and `cn1_globals.m` to support compilation on Linux (added guards and stubs). - Added `vm/tests/src/test/java/com/codename1/tools/translator/FileClassIntegrationTest.java` to verify the implementation. - Fixed string concatenation issues in `vm/JavaAPI` exception constructors to resolve C compilation errors. * Implement java.io.File in JavaAPI with native support for ParparVM - Added full implementation of `java.io.File` in `vm/JavaAPI` delegating to native methods. - Added native implementation in `vm/ByteCodeTranslator/src/java_io_File.m` supporting both iOS (`NSFileManager`) and Linux/POSIX. - Updated `ByteCodeTranslator` to package the new native source file. - Patched `vm/ByteCodeTranslator/src/nativeMethods.m` and `cn1_globals.m` to support compilation on Linux (added guards and stubs). - Added `vm/tests/src/test/java/com/codename1/tools/translator/FileClassIntegrationTest.java` to verify the implementation. - Fixed string concatenation issues in `vm/JavaAPI` exception constructors to resolve C compilation errors. - Added `vm/JavaAPI/src/java/util/Objects.java` to resolve missing dependency during integration tests. * Implement java.io.File in JavaAPI with native support for ParparVM - Added full implementation of `java.io.File` in `vm/JavaAPI` delegating to native methods. - Added native implementation in `vm/ByteCodeTranslator/src/java_io_File.m` supporting both iOS (`NSFileManager`) and Linux/POSIX. - Updated `ByteCodeTranslator` to package the new native source file. - Patched `vm/ByteCodeTranslator/src/nativeMethods.m` and `cn1_globals.m` to support compilation on Linux (added guards and stubs). - Added `vm/tests/src/test/java/com/codename1/tools/translator/FileClassIntegrationTest.java` to verify the implementation. - Fixed string concatenation issues in `vm/JavaAPI` exception constructors to resolve C compilation errors. - Added `vm/JavaAPI/src/java/util/Objects.java` to resolve missing dependency during integration tests. - Removed conflicting `java.util.Objects` stub generation from `LambdaIntegrationTest`. * Implement java.io.File in JavaAPI with native support for ParparVM - Added full implementation of `java.io.File` in `vm/JavaAPI` delegating to native methods. - Added native implementation in `vm/ByteCodeTranslator/src/java_io_File.m` supporting both iOS (`NSFileManager`) and Linux/POSIX. - Updated `ByteCodeTranslator` to package the new native source file. - Patched `vm/ByteCodeTranslator/src/nativeMethods.m` and `cn1_globals.m` to support compilation on Linux (added guards and stubs). - Refactored common native definitions (NSString helpers, NSLog) to `cn1_globals.h`. - Guarded privacy-sensitive file system APIs with `CN1_ENABLE_FILE_SYSTEM_STATS`. - Updated POSIX UTF-8 decoding in `nativeMethods.m` to use the DFA decoder. - Added `vm/tests/src/test/java/com/codename1/tools/translator/FileClassIntegrationTest.java` to verify the implementation. - Fixed string concatenation issues in `vm/JavaAPI` exception constructors to resolve C compilation errors. - Added `vm/JavaAPI/src/java/util/Objects.java` to resolve missing dependency during integration tests. - Removed conflicting `java.util.Objects` stub generation from `LambdaIntegrationTest`. --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent daa53ee commit a5c39f0

File tree

14 files changed

+1261
-153
lines changed

14 files changed

+1261
-153
lines changed

vm/ByteCodeTranslator/src/cn1_globals.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,9 +1024,14 @@ extern void initMethodStack(CODENAME_ONE_THREAD_STATE, JAVA_OBJECT __cn1ThisObje
10241024
int methodBlockOffset = threadStateData->tryBlockOffset;
10251025

10261026

1027-
#ifdef __OBJC__
1027+
#if defined(__APPLE__) && defined(__OBJC__)
10281028
extern JAVA_OBJECT fromNSString(CODENAME_ONE_THREAD_STATE, NSString* str);
10291029
extern NSString* toNSString(CODENAME_ONE_THREAD_STATE, JAVA_OBJECT o);
1030+
#else
1031+
#define NSLog(...) printf(__VA_ARGS__); printf("\n")
1032+
typedef int BOOL;
1033+
#define YES 1
1034+
#define NO 0
10301035
#endif
10311036

10321037
extern JAVA_OBJECT __NEW_ARRAY_JAVA_BOOLEAN(CODENAME_ONE_THREAD_STATE, JAVA_INT size);

vm/ByteCodeTranslator/src/cn1_globals.m

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@
1515
#include "java_lang_Runnable.h"
1616
#include "java_lang_System.h"
1717
#include "java_lang_ArrayIndexOutOfBoundsException.h"
18+
#if defined(__APPLE__) && defined(__OBJC__)
1819
#import <mach/mach.h>
1920
#import <mach/mach_host.h>
21+
#else
22+
#include <time.h>
23+
#define NSLog(...) printf(__VA_ARGS__); printf("\n")
24+
#endif
2025

2126
// The amount of memory allocated between GC cycle checks (generally 30 seconds)
2227
// that triggers "High-frequency" GC mode. When "High-frequency" mode is triggered,
@@ -70,8 +75,9 @@ static JAVA_BOOLEAN isEdt(long threadId) {
7075
}
7176

7277
// Gets the amount of free memory in the system.
73-
static natural_t get_free_memory(void)
78+
static long get_free_memory(void)
7479
{
80+
#if defined(__APPLE__) && defined(__OBJC__)
7581
mach_port_t host_port;
7682
mach_msg_type_number_t host_size;
7783
vm_size_t pagesize;
@@ -85,8 +91,11 @@ static natural_t get_free_memory(void)
8591
return 0;
8692
}
8793
/* Stats in bytes */
88-
natural_t mem_free = vm_stat.free_count * pagesize;
94+
long mem_free = vm_stat.free_count * pagesize;
8995
return mem_free;
96+
#else
97+
return 1024 * 1024 * 100; // Stub: 100MB
98+
#endif
9099
}
91100

92101
// Initializes the GC thresholds based on the free memory on the device.
@@ -674,7 +683,9 @@ void preSweepCount(CODENAME_ONE_THREAD_STATE) {
674683
}
675684

676685
void printObjectsPostSweep(CODENAME_ONE_THREAD_STATE) {
686+
#if defined(__APPLE__) && defined(__OBJC__)
677687
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
688+
#endif
678689

679690
// this should be the last class used
680691
int classTypeCount[cn1_array_3_id_java_util_Vector + 1];
@@ -706,10 +717,14 @@ void printObjectsPostSweep(CODENAME_ONE_THREAD_STATE) {
706717
if(classTypeCount[iter] > 0) {
707718
if(classTypeCountPreSweep[iter] - classTypeCount[iter] > 0) {
708719
if(iter > cn1_array_start_offset) {
720+
#if defined(__APPLE__) && defined(__OBJC__)
709721
NSLog(@"There are %i instances of %@ taking up %i bytes, %i were cleaned which saved %i bytes", classTypeCount[iter], [NSString stringWithUTF8String:arrayOfNames[iter]], sizeInHeapForType[iter], classTypeCountPreSweep[iter] - classTypeCount[iter], sizeInHeapForTypePreSweep[iter] - sizeInHeapForType[iter]);
722+
#endif
710723
} else {
711724
JAVA_OBJECT str = STRING_FROM_CONSTANT_POOL_OFFSET(classNameLookup[iter]);
725+
#if defined(__APPLE__) && defined(__OBJC__)
712726
NSLog(@"There are %i instances of %@ taking up %i bytes, %i were cleaned which saved %i bytes", classTypeCount[iter], toNSString(threadStateData, str), sizeInHeapForType[iter], classTypeCountPreSweep[iter] - classTypeCount[iter], sizeInHeapForTypePreSweep[iter] - sizeInHeapForType[iter]);
727+
#endif
713728
}
714729
}
715730
actualTotalMemory += sizeInHeapForType[iter];
@@ -719,11 +734,15 @@ void printObjectsPostSweep(CODENAME_ONE_THREAD_STATE) {
719734
NSLog(@"**** GC cycle complete ****");
720735

721736
free(arrayOfNames);
737+
#if defined(__APPLE__) && defined(__OBJC__)
722738
[pool release];
739+
#endif
723740
}
724741

725742
void printObjectTypesInHeap(CODENAME_ONE_THREAD_STATE) {
743+
#if defined(__APPLE__) && defined(__OBJC__)
726744
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
745+
#endif
727746

728747
// this should be the last class used
729748
int classTypeCount[cn1_array_3_id_java_util_Vector + 1];
@@ -756,18 +775,24 @@ void printObjectTypesInHeap(CODENAME_ONE_THREAD_STATE) {
756775
float f = ((float)classTypeCount[iter]) / ((float)t) * 100.0f;
757776
float f2 = ((float)sizeInHeapForType[iter]) / ((float)totalAllocatedHeap) * 100.0f;
758777
if(iter > cn1_array_start_offset) {
778+
#if defined(__APPLE__) && defined(__OBJC__)
759779
NSLog(@"There are %i instances of %@ which is %i percent its %i bytes which is %i mem percent", classTypeCount[iter], [NSString stringWithUTF8String:arrayOfNames[iter]], (int)f, sizeInHeapForType[iter], (int)f2);
780+
#endif
760781
} else {
761782
JAVA_OBJECT str = STRING_FROM_CONSTANT_POOL_OFFSET(classNameLookup[iter]);
783+
#if defined(__APPLE__) && defined(__OBJC__)
762784
NSLog(@"There are %i instances of %@ which is %i percent its %i bytes which is %i mem percent", classTypeCount[iter], toNSString(threadStateData, str), (int)f, sizeInHeapForType[iter], (int)f2);
785+
#endif
763786
}
764787
actualTotalMemory += sizeInHeapForType[iter];
765788
}
766789
}
767790
NSLog(@"Actual ram = %i vs total mallocs = %i", actualTotalMemory, totalAllocatedHeap);
768791

769792
free(arrayOfNames);
793+
#if defined(__APPLE__) && defined(__OBJC__)
770794
[pool release];
795+
#endif
771796
}
772797
#endif
773798

@@ -796,6 +821,7 @@ void codenameOneGCSweep() {
796821

797822
#ifdef DEBUG_GC_ALLOCATIONS
798823
int classId = o->className;
824+
#if defined(__APPLE__) && defined(__OBJC__)
799825
NSString* whereIs;
800826
if(classId > 0) {
801827
whereIs = (NSString*)((struct obj__java_lang_String*)STRING_FROM_CONSTANT_POOL_OFFSET(classId))->java_lang_String_nsString;
@@ -824,6 +850,7 @@ void codenameOneGCSweep() {
824850
}
825851
NSLog(@"Sweeping: %X, Mark: %i, Allocated: %@ %i , type: %@, toString: '%@'", (int)o, o->__codenameOneGcMark, whereIs, o->line, [NSString stringWithUTF8String:o->__codenameOneParentClsReference->clsName], ns);
826852
}
853+
#endif
827854
#endif
828855

829856
removeObjectFromHeapCollection(threadStateData, o);
@@ -1249,6 +1276,7 @@ void initConstantPool() {
12491276

12501277
JAVA_OBJECT utf8String = NULL;
12511278

1279+
#if defined(__APPLE__) && defined(__OBJC__)
12521280
JAVA_OBJECT fromNSString(CODENAME_ONE_THREAD_STATE, NSString* str) {
12531281
if (str == nil) {
12541282
return JAVA_NULL;
@@ -1274,6 +1302,7 @@ JAVA_OBJECT fromNSString(CODENAME_ONE_THREAD_STATE, NSString* str) {
12741302
finishedNativeAllocations();
12751303
return s;
12761304
}
1305+
#endif
12771306

12781307
const char* stringToUTF8(CODENAME_ONE_THREAD_STATE, JAVA_OBJECT str) {
12791308
if(str == NULL) {
@@ -1308,6 +1337,7 @@ JAVA_OBJECT fromNSString(CODENAME_ONE_THREAD_STATE, NSString* str) {
13081337
return cs;
13091338
}
13101339

1340+
#if defined(__APPLE__) && defined(__OBJC__)
13111341
NSString* toNSString(CODENAME_ONE_THREAD_STATE, JAVA_OBJECT o) {
13121342
if(o == JAVA_NULL) {
13131343
return 0;
@@ -1323,6 +1353,7 @@ JAVA_OBJECT fromNSString(CODENAME_ONE_THREAD_STATE, NSString* str) {
13231353
str->java_lang_String_nsString = (JAVA_LONG)x;
13241354
return st;
13251355
}
1356+
#endif
13261357

13271358
JAVA_OBJECT __NEW_ARRAY_JAVA_BOOLEAN(CODENAME_ONE_THREAD_STATE, JAVA_INT size) {
13281359
JAVA_OBJECT o = allocArray(threadStateData, size, &class_array1__JAVA_BOOLEAN, sizeof(JAVA_ARRAY_BOOLEAN), 1);

vm/ByteCodeTranslator/src/com/codename1/tools/translator/ByteCodeTranslator.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ private static void handleCleanOutput(ByteCodeTranslator b, File[] sources, File
244244
copy(ByteCodeTranslator.class.getResourceAsStream("/xmlvm.h"), new FileOutputStream(xmlvm));
245245
File nativeMethods = new File(srcRoot, "nativeMethods.m");
246246
copy(ByteCodeTranslator.class.getResourceAsStream("/nativeMethods.m"), new FileOutputStream(nativeMethods));
247+
File javaIoFileM = new File(srcRoot, "java_io_File.m");
248+
copy(ByteCodeTranslator.class.getResourceAsStream("/java_io_File.m"), new FileOutputStream(javaIoFileM));
247249

248250
Parser.writeOutput(srcRoot);
249251

@@ -299,6 +301,8 @@ private static void handleIosOutput(ByteCodeTranslator b, File[] sources, File d
299301
copy(ByteCodeTranslator.class.getResourceAsStream("/cn1_globals.m"), new FileOutputStream(cn1GlobalsM));
300302
File nativeMethods = new File(srcRoot, "nativeMethods.m");
301303
copy(ByteCodeTranslator.class.getResourceAsStream("/nativeMethods.m"), new FileOutputStream(nativeMethods));
304+
File javaIoFileM = new File(srcRoot, "java_io_File.m");
305+
copy(ByteCodeTranslator.class.getResourceAsStream("/java_io_File.m"), new FileOutputStream(javaIoFileM));
302306

303307
if (System.getProperty("USE_RPMALLOC", "false").equals("true")) {
304308
File malloc = new File(srcRoot, "malloc.c");

0 commit comments

Comments
 (0)