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

Some modest contributions : use of c++ inheritance to match Java inheritance, use of class caches to shrink code... #1

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
*.obj
*.class
*.bak
*.bak
*.o
*.lib
*~
*.jar
java/cppjvm/java_*.cpp
java/cppjvm/include/java/*
bin/*
14 changes: 11 additions & 3 deletions buildscripts/config.darwin.makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@

thirdparty_platform = linux
thirdparty_platform = macosx

compiler = g++
compiler_global_flags = -c -DPOSIX
compiler_object_name_prefix = -o
#compiler_global_flags += -g
compiler_global_flags += -I/System/Library/Frameworks/JavaVM.framework/Versions/Current/Headers/
compiler_global_flags += -O3 -Os
compiler_global_flags += -fno-stack-protector -funsafe-loop-optimizations
compiler_global_flags += -U_FORTIFY_SOURCE

#compiler_global_flags += -arch x86_64

compiler_object_name_prefix = -o
compiler_object_suffix = .o

staticlib_suffix = .lib
Expand All @@ -12,7 +20,7 @@ static_linker_global_flags = -static
static_linker_output_name_prefix = -o

executable_linker = g++
executable_linker_global_flags =
executable_linker_global_flags = -framework JavaVM
executable_linker_output_name_prefix = -o
executable_linker_library_path_prefix = -L
executable_linker_library_name_prefix =
Expand Down
22 changes: 16 additions & 6 deletions java/cppwrap/src/com/earwicker/cppjvm/cppwrap/CppWrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,28 +27,38 @@ public class CppWrap {
}


public static String cppType(Class<?> j) throws Exception {
public static String cppType(Class<?> j) {//throws Exception {
if (j == null)
//j = Object.class;
return "jobject";

if (j.isPrimitive()) {
String g = primitives.get(j);
return g == null ? "jobject" : g;
if (g != null)
return g;
//return g == null ? "jobject" : g;
}

if (j.isArray())
return "::jvm::array< " + cppType(j.getComponentType()) + " >";

// very poor support for nested classes!
if (j.getDeclaringClass() != null)
return "jobject";
if (!shouldGenerate(j))//j.getDeclaringClass() != null)
j = Object.class;
//return "jobject";

return "::" + j.getName().replaceAll("\\.", "::");
}

public static boolean shouldGenerate(Class<?> cls) {
String n = cls.getName();
if (n.startsWith("sun."))
return false;
return cls.getDeclaringClass() == null;
}
public static boolean isWrapped(Class<?> cls) {
// Can't wrap nested classes yet...
return !cls.isPrimitive() && cls.getDeclaringClass() == null;
return !cls.isPrimitive();// && cls.getDeclaringClass() == null;
}

public static String fixName(String name) {
Expand Down Expand Up @@ -88,7 +98,7 @@ public static int saveIfDifferent(String path, String content) throws Exception
public static int generate(Class<?> cls, File out) throws Exception
{
int generated = 0;
if (!isWrapped(cls))
if (!CppWrap.shouldGenerate(cls) || !isWrapped(cls))
return generated;

char sl = File.separatorChar;
Expand Down
137 changes: 96 additions & 41 deletions java/cppwrap/src/com/earwicker/cppjvm/cppwrap/HeaderGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import java.io.*;

public class HeaderGenerator extends SourceGenerator {
public void generate() throws Exception {
public void generate() throws Exception {
beginIncludeGuard();
globalIncludes();
includeParentType();
forwardDeclareRequiredTypes();
beginNamespace(cls());
beginClass();
Expand All @@ -22,7 +23,7 @@ public void generate() throws Exception {
}

protected void forwardDeclare(Class<?> cls) {
if (!CppWrap.isWrapped(cls))
if (!CppWrap.shouldGenerate(cls) || !CppWrap.isWrapped(cls))
return;

beginNamespace(cls);
Expand All @@ -45,6 +46,16 @@ protected void globalIncludes() {
out().println("#include <jvm/virtual_machine.hpp>");
out().println("#include <jvm/object.hpp>");
out().println("#include <jvm/array.hpp>");
if (!putDefinitionsInHeaders)
out().println("#include <impl/class_cache.hpp>");
}

protected void includeParentType() {
Class superclass = cls().getSuperclass();
if (superclass == null)
return;

out().println("#include <" + superclass.getName().replace('.', '/') + ".hpp>");
}

protected void forwardDeclareRequiredTypes() throws Exception {
Expand All @@ -55,12 +66,21 @@ protected void forwardDeclareRequiredTypes() throws Exception {

protected void beginClass() {
out().println();
out().println("class " + cls().getSimpleName() + " : public ::jvm::object");
Class superclass = cls().getSuperclass();
String superclassCppName;
if (superclass == null) {
superclassCppName = "::jvm::object";
} else {
superclassCppName = CppWrap.cppType(superclass);//"::" + superclass.getName().replaceAll("\\.", "::");
}
out().println("class " + cls().getSimpleName() + " : public " + superclassCppName);
out().println("{");
if (putDefinitionsInHeaders)
out().println(" static cppjvm::impl::class_cache s_impl_cache;");
out().println("public:");

// Can construct from a jobject, but not implicitly to avoid accidental unsafe conversion
out().println(" explicit " + cls().getSimpleName() + "(jobject jobj) : object(jobj) {}");
out().println(" explicit " + cls().getSimpleName() + "(jobject jobj) : " + superclassCppName + "(jobj) {}");

out().println(" static jclass get_class();");

Expand All @@ -69,58 +89,93 @@ protected void beginClass() {

// Copy constructor: copy reference
out().println(" " + cls().getSimpleName() + "(const " + cls().getSimpleName() + " &other)");
out().println(" : object(other.get_impl()) {}");
out().println(" : " + superclassCppName + "(other.get_impl()) {}");
}

protected void endClass() {
out().println("};");
}

protected void declareConstructors() throws Exception {
for (Constructor<?> ctor : cls().getConstructors()) {
Class<?>[] params = ctor.getParameterTypes();

// void new_(params...);
out().print(" void new_(");
listParameters(params, DECLARE_TYPES);
out().println(");");

// For non-default and non-copy constructors only:
if ((params.length > 1) ||
((params.length == 1) && !params[0].equals(cls()))) {

// Make an actual C++ constructor
out().print(" explicit " + cls().getSimpleName() + "(");
listParameters(params, DECLARE_TYPES);
out().println(")");
out().println(" {");
out().print(" new_(");
listParameters(params, CALL_WRAPPED);
out().println(");");
out().println(" }");
}
}
if (putDefinitionsInHeaders) {
out().println(indent(new ImplementationGenerator() {
@Override
public void generate() throws Exception {
defineConstructors();
}
@Override
protected boolean isInHeader() {
return true;
}
@Override
protected void editWarning() {}
}.toString(cls())));
} else {
for (Constructor<?> ctor : cls().getConstructors()) {
Class<?>[] params = ctor.getParameterTypes();

// void new_(params...);
out().print(" void new_(");
listParameters(params, DECLARE_TYPES);
out().println(");");

// For non-default and non-copy constructors only:
if ((params.length > 1) ||
((params.length == 1) && !params[0].equals(cls()))) {

// Make an actual C++ constructor
out().print(" explicit " + cls().getSimpleName() + "(");
listParameters(params, DECLARE_TYPES);
out().println(")");
out().println(" {");
out().print(" new_(");
listParameters(params, CALL_WRAPPED);
out().println(");");
out().println(" }");
}
}
}
}

protected void declareConversions() throws Exception {
String dummy = CppWrap.cppType(Object.class);
for (Class<?> st : CppWrap.getSuperTypes(cls())) {
out().println(" operator " + CppWrap.cppType(st) + "() const;");
String conv = CppWrap.cppType(st);
if (conv.equals(dummy))
continue;

out().println(" operator " + CppWrap.cppType(st) + "() const;");
}
}

protected void declareMethods() throws Exception {
for (Method m : cls().getMethods()) {
if (m.isSynthetic())
continue;

// [static] return-type methodName(params...) [const];
out().print(" " +
(Modifier.isStatic(m.getModifiers()) ? "static " : "") +
CppWrap.cppType(m.getReturnType()) + " " +
CppWrap.fixName(m.getName()) + "(");
listParameters(m.getParameterTypes(), DECLARE_TYPES);
out().println(Modifier.isStatic(m.getModifiers()) ? ");" : ") const;");
}
if (putDefinitionsInHeaders)
out().println(indent(new ImplementationGenerator() {
@Override
public void generate() throws Exception {
defineMethods();
}
@Override
protected boolean isInHeader() {
return true;
}
@Override
protected void editWarning() {}
}.toString(cls())));
else {
for (Method m : methods()) {
if (m.isSynthetic())
continue;

// [static] return-type methodName(params...) [const];
out().print(" " +
(Modifier.isStatic(m.getModifiers()) ? "static " : ""/*"virtual "*/) +
CppWrap.cppType(m.getReturnType()) + " " +
CppWrap.fixName(m.getName()) + "(");
listParameters(m.getParameterTypes(), DECLARE_TYPES);
out().println(Modifier.isStatic(m.getModifiers()) ? ");" : ") const;");
}
}
}

protected void declareSpecialStringFeatures() {
Expand Down
Loading