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

Framework target: using Java classes as native ObjC #457

Merged
merged 6 commits into from
Feb 26, 2020

Conversation

dkimitsa
Copy link
Contributor

This PR improves the way Java code might be used in ObjC/Swift project by exposing java @CustomClass object to native side, so these can be instantiated directly as simple as [[MyClass alloc] init] and JVM is started automatically.
For this:

  • compiler generates and exports OBJC_CLASS_$_${CLASSNAME} symbols that is needed at native part during linkage;
  • compiler doesn't produce proper structure of it but only placeholder;
  • valid data is being copied into OBJC_CLASS_$_${CLASSNAME} once backing ObjC class is dynamically created runtime once Java class is loaded;
  • compiler generates a list of @CustomClass files and pre-loads there once framework is loaded and JVM started

…tomClass java object natively from objc/swift.

It was not possible as to use class its metadata have to be available as OBJC_CLASS_$_{class_name}. Otherwise compilation fails on linkage stage.
RoboVM doesn't generate static metadata. Instead it creates ObjC classes dynamically runtime.

To achieve following was done:
- for each CustomClass OBJC_CLASS_$_{class_name} structure is allocated compilation time;
- OBJC_CLASS_$_{class_name} structs being filled with data once Java class is loaded and corresponding ObjC class is created;
- framework support is modified to preload all CustomClasses exposed by Framework when framework loads

ObjCMemberPlugin:
- <cinit> modified to copy ObjCClass.getHandle into OBJC_CLASS_$_{class_name}
- also it replaces the handle in ObjCClass. with pointer to OBJC_CLASS_$_{class_name} (so both Java and native side will have same Class pointer)

FrameworkTarget:
- all  OBJC_CLASS_$_{class_name} added to exported symbols

ClassCompiler:
- added compiler for $publishObjClass in CustomClass

Linker:
- added functionality to embed global symbols. In particular this is required to export list of classes to preload in _bcFrameworkPreloadClasses

Frameworksupport:
- now it starts VM once framework is loaded
- also it preload required custom classes
- old functionality maintained to allow old code that was using rvmInstantiateFramework to work
…k.name and framework.executable are different broken framework was produced
…k.name and framework.executable are different broken framework was produced
@dkimitsa
Copy link
Contributor Author

long read and tutorial

@Tom-Ski
Copy link
Member

Tom-Ski commented Feb 24, 2020

Very cool stuff!

@Tom-Ski Tom-Ski merged commit 7d58af8 into MobiVM:jdk12 Feb 26, 2020
@dkimitsa dkimitsa deleted the jdk12-frm-with-objc branch March 8, 2020 12:45
dkimitsa added a commit to dkimitsa/robovm that referenced this pull request Apr 9, 2020
* * FrameworkTarget: exporting OBJC_CLASS to allow instantiation of CustomClass java object natively from objc/swift.
It was not possible as to use class its metadata have to be available as OBJC_CLASS_$_{class_name}. Otherwise compilation fails on linkage stage.
RoboVM doesn't generate static metadata. Instead it creates ObjC classes dynamically runtime.

To achieve following was done:
- for each CustomClass OBJC_CLASS_$_{class_name} structure is allocated compilation time;
- OBJC_CLASS_$_{class_name} structs being filled with data once Java class is loaded and corresponding ObjC class is created;
- framework support is modified to preload all CustomClasses exposed by Framework when framework loads

ObjCMemberPlugin:
- <cinit> modified to copy ObjCClass.getHandle into OBJC_CLASS_$_{class_name}
- also it replaces the handle in ObjCClass. with pointer to OBJC_CLASS_$_{class_name} (so both Java and native side will have same Class pointer)

FrameworkTarget:
- all  OBJC_CLASS_$_{class_name} added to exported symbols

ClassCompiler:
- added compiler for $publishObjClass in CustomClass

Linker:
- added functionality to embed global symbols. In particular this is required to export list of classes to preload in _bcFrameworkPreloadClasses

Frameworksupport:
- now it starts VM once framework is loaded
- also it preload required custom classes
- old functionality maintained to allow old code that was using rvmInstantiateFramework to work

* * FrameworkTarget: template updated to match ObjC changes

* * FrameworkTarget: idea -- removed main class from wizard as there is no such item anymore in framework target

* * FrameworkTarget: template -- fixed imageName, otherwise if framework.name and  framework.executable are different broken framework was produced

* * FrameworkTarget: template -- fixed imageName, otherwise if framework.name and  framework.executable are different broken framework was produced

* * fixed typos
dkimitsa added a commit to dkimitsa/robovm that referenced this pull request Apr 9, 2020
* * FrameworkTarget: exporting OBJC_CLASS to allow instantiation of CustomClass java object natively from objc/swift.
It was not possible as to use class its metadata have to be available as OBJC_CLASS_$_{class_name}. Otherwise compilation fails on linkage stage.
RoboVM doesn't generate static metadata. Instead it creates ObjC classes dynamically runtime.

To achieve following was done:
- for each CustomClass OBJC_CLASS_$_{class_name} structure is allocated compilation time;
- OBJC_CLASS_$_{class_name} structs being filled with data once Java class is loaded and corresponding ObjC class is created;
- framework support is modified to preload all CustomClasses exposed by Framework when framework loads

ObjCMemberPlugin:
- <cinit> modified to copy ObjCClass.getHandle into OBJC_CLASS_$_{class_name}
- also it replaces the handle in ObjCClass. with pointer to OBJC_CLASS_$_{class_name} (so both Java and native side will have same Class pointer)

FrameworkTarget:
- all  OBJC_CLASS_$_{class_name} added to exported symbols

ClassCompiler:
- added compiler for $publishObjClass in CustomClass

Linker:
- added functionality to embed global symbols. In particular this is required to export list of classes to preload in _bcFrameworkPreloadClasses

Frameworksupport:
- now it starts VM once framework is loaded
- also it preload required custom classes
- old functionality maintained to allow old code that was using rvmInstantiateFramework to work

* * FrameworkTarget: template updated to match ObjC changes

* * FrameworkTarget: idea -- removed main class from wizard as there is no such item anymore in framework target

* * FrameworkTarget: template -- fixed imageName, otherwise if framework.name and  framework.executable are different broken framework was produced

* * FrameworkTarget: template -- fixed imageName, otherwise if framework.name and  framework.executable are different broken framework was produced

* * fixed typos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants