Skip to content

Commit

Permalink
[java] a classLoader for the Setting compiles ok but probably does no…
Browse files Browse the repository at this point in the history
…t run
  • Loading branch information
mberends committed Sep 22, 2010
1 parent a829cc5 commit 3e28165
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 86 deletions.
43 changes: 35 additions & 8 deletions java/README.txt
Expand Up @@ -6,41 +6,53 @@ of the C# files in the dotnet tree. C# is the "Microsoft Java" and this
subproject shows how close they are. subproject shows how close they are.




Status
------

The Metamodel is fully translated. The setting loader (in Init.java)
needs to be rewritten in terms of ClassLoader. The compiler needs to be
translated.


Source formatting guidelines Source formatting guidelines
---------------------------- ----------------------------


For clarity, or where there is doubt about the correctness of a For clarity, or where there is doubt about the correctness of a
translation, include the original line afterwards in a comment. translation, include the original line afterwards in a comment.


Add horizontal spacing to maximize correlation of text in consecutive Use horizontal spacing to maximize correlation of text in consecutive
lines. lines.


With apologies to jnthn++, a case convention for the initial letter of With apologies to jnthn++, a widely used case convention for the initial
names is being gradually phased in. Class names, class member names and letter of names is being gradually phased in. Class names, class member
interfaces is start with an uppercase letter. Local variables begin in names and interfaces start with an uppercase letter. Local variables
lowercase. begin in lowercase.




6model specific translations 6model specific translations
---------------------------- ----------------------------


The RakudoObject interface defines properties STable and SC (meaning The RakudoObject interface defines properties STable and SC (meaning
SharedTable and SerializationContext). Java does not have properties, SharedTable and SerializationContext). Java does not have properties,
these become private data members _STable and _SC, and get accessor these become private data members and get accessor methods getSTable(),
methods getSTable(), setSTable, getSC() and setSC(). setSTable(), getSC() and setSC().




C# to Java translation guidelines (in case insensitive alphabetical order) C# to Java translation guidelines (in case insensitive alphabetical order)
-------------------------------------------------------------------------- --------------------------------------------------------------------------


C# abstract class becomes Java interface. C# abstract class becomes Java interface.


C# Add(item) (to a List) becomes Java add(item) (to an ArrayList).

C# bool becomes Java boolean. C# bool becomes Java boolean.


C# Console.Write becomes Java System.out.print. C# Console.Write becomes Java System.out.print.


C# const becomes Java static final. C# const becomes Java static final.


C# Count (of a List) becomes Java size() (of an ArrayList).

C# Dictionary becomes Java HashMap. The HashMap is not quite as C# Dictionary becomes Java HashMap. The HashMap is not quite as
versatile, because the value part must be a reference type, it cannot be versatile, because the value part must be a reference type, it cannot be
a value type. Therefore C# Dictionary<string, int> becomes Java a value type. Therefore C# Dictionary<string, int> becomes Java
Expand All @@ -67,7 +79,9 @@ C# Length (of an array) becomes Java length (of an array).


C# Length (of a List) becomes Java size() (of an ArrayList). C# Length (of a List) becomes Java size() (of an ArrayList).


C# List becomes Java ArrayList. C# List (in System.Collections.Generic) becomes Java ArrayList (in java.util).

C# Min(a,b) (in System.Math) becomes Java min(a,b) in java.lang.Math.


C# namespace yada { ... } becomes Java package yada; ... . C# namespace yada { ... } becomes Java package yada; ... .
Also the Java package hierarchy must match the file system directory Also the Java package hierarchy must match the file system directory
Expand Down Expand Up @@ -125,5 +139,18 @@ interface.
See: http://msdn.microsoft.com/en-us/library/bb549151.aspx and See: http://msdn.microsoft.com/en-us/library/bb549151.aspx and
http://dotnetperls.com/func http://dotnetperls.com/func



See Also
--------

IKVM (http://sourceforge.net/apps/mediawiki/ikvm/index.php?title=FAQ)
runs Java code on Mono and .NET CLR.

Compare Java and C#: http://discuss.joelonsoftware.com/default.asp?joel.3.456095.37

Compare JVM and CLR: http://benjismith.net/index.php/2006/06/23/biz-idea-08-dotjnet-bytecode-translator/

Compare JVM and CLR code security: http://onjava.com/pub/a/onjava/2003/11/26/javavsdotnet.html?page=2&x-maxdepth=0

Created by: Martin Berends (mberends in #perl6 on irc.freenode.net) Created by: Martin Berends (mberends in #perl6 on irc.freenode.net)


90 changes: 60 additions & 30 deletions java/runtime/Rakudo/Init.java
@@ -1,5 +1,9 @@
package Rakudo; package Rakudo;


import java.lang.Class;
import java.lang.ClassLoader;
import java.lang.reflect.Method;

import Rakudo.Metamodel.KnowHOW.KnowHOWBootstrapper; import Rakudo.Metamodel.KnowHOW.KnowHOWBootstrapper;
import Rakudo.Metamodel.KnowHOW.KnowHOWREPR; import Rakudo.Metamodel.KnowHOW.KnowHOWREPR;
import Rakudo.Metamodel.RakudoObject; import Rakudo.Metamodel.RakudoObject;
Expand Down Expand Up @@ -35,38 +39,31 @@ public class Init
/// Handles the various bits of initialization that are needed. /// Handles the various bits of initialization that are needed.
/// Probably needs some don't-dupe-this work. /// Probably needs some don't-dupe-this work.
/// </summary> /// </summary>
public static ThreadContext Initialize(String SettingName) public static ThreadContext Initialize(String settingName)
{ {
// Bootstrap the meta-model. // Bootstrap the meta-model.
RegisterRepresentations(); RegisterRepresentations();
RakudoObject KnowHOW = KnowHOWBootstrapper.Bootstrap(); RakudoObject knowHOW = KnowHOWBootstrapper.Bootstrap();


// See if we're to load a setting or use the fake bootstrapping one. // Either load a named setting or use the fake bootstrapping one.
Context settingContext; Context settingContext =
if (SettingName == null) (settingName != null) ? LoadSetting(settingName, knowHOW)
{ : BootstrapSetting(knowHOW);
settingContext = BootstrapSetting(KnowHOW);
}
else
{
settingContext = LoadSetting(SettingName, KnowHOW);
}


// Cache native capture and LLCode type object. // Cache native capture and LLCode type object.
CaptureHelper.CaptureTypeObject = settingContext.LexPad.GetByName("capture"); CaptureHelper.CaptureTypeObject = settingContext.LexPad.GetByName("capture");
CodeObjectUtility.LLCodeTypeObject = (RakudoCodeRef.Instance)settingContext.LexPad.GetByName("LLCode"); CodeObjectUtility.LLCodeTypeObject = (RakudoCodeRef.Instance)settingContext.LexPad.GetByName("LLCode");


// Create an execution domain and a thread context for it. // Create an execution domain and a thread context for it.
ExecutionDomain execDom = new ExecutionDomain(); ExecutionDomain executionDomain = new ExecutionDomain();
ThreadContext thread = new ThreadContext(); ThreadContext threadContext = new ThreadContext();
thread.Domain = execDom; threadContext.Domain = executionDomain;
thread.CurrentContext = settingContext; threadContext.CurrentContext = settingContext;
thread.DefaultBoolBoxType = settingContext.LexPad.GetByName("NQPInt"); threadContext.DefaultBoolBoxType = settingContext.LexPad.GetByName("NQPInt");
thread.DefaultIntBoxType = settingContext.LexPad.GetByName("NQPInt"); threadContext.DefaultIntBoxType = settingContext.LexPad.GetByName("NQPInt");
thread.DefaultNumBoxType = settingContext.LexPad.GetByName("NQPNum"); threadContext.DefaultNumBoxType = settingContext.LexPad.GetByName("NQPNum");
thread.DefaultStrBoxType = settingContext.LexPad.GetByName("NQPStr"); threadContext.DefaultStrBoxType = settingContext.LexPad.GetByName("NQPStr");

return threadContext;
return thread;
} }


/// <summary> /// <summary>
Expand Down Expand Up @@ -130,24 +127,57 @@ public RakudoObject Invoke(ThreadContext tc, RakudoObject self, RakudoObject cap
/// <param name="Name"></param> /// <param name="Name"></param>
/// <param name="KnowHOW"></param> /// <param name="KnowHOW"></param>
/// <returns></returns> /// <returns></returns>
public static Context LoadSetting(String Name, RakudoObject KnowHOW) public static Context LoadSetting(String name, RakudoObject knowHOW)
{ {
// Load the assembly. // Load the assembly.
// TODO var settingAssembly = AppDomain.CurrentDomain.Load(Name); // TODO var settingAssembly = AppDomain.CurrentDomain.Load(Name);
ClassLoader loader = ClassLoader.getSystemClassLoader();
Class<?> classNQPSetting; // grrr, a wildcard type :-(
try {
classNQPSetting = loader.loadClass(name);
}
catch (ClassNotFoundException ex) {
classNQPSetting = null;
System.err.println("Class " + name + " not found: " + ex.getMessage());
System.exit(1);
}


// Find the setting type and its LoadSetting method. // Find the setting type and its LoadSetting method.
// TODO var Class = settingAssembly.GetType("NQPSetting"); // var Class = settingAssembly.GetType("NQPSetting");
// TODO var Method = Class.GetMethod("LoadSetting", BindingFlags.NonPublic | BindingFlags.Static); // var Method = Class.GetMethod("LoadSetting", BindingFlags.NonPublic | BindingFlags.Static);

String s = new String();
Class stringClass = s.getClass();
Method methodLoadSetting;
try {
methodLoadSetting = classNQPSetting.getMethod("LoadSetting", stringClass);
}
catch ( NoSuchMethodException ex) {
methodLoadSetting = null;
System.err.println("Method LoadSetting not found: " + ex.getMessage());
System.exit(1);
}

// Run it to get the context we want. // Run it to get the context we want.
// TODO Context settingContext = (Context)Method.Invoke(null, new object[] { }); // TODO Context settingContext = (Context)Method.Invoke(null, new Object[] { });
Context settingContext = null; // TODO remove Context settingContext = null;
try {
settingContext = (Context)methodLoadSetting.invoke( null, s );
}
catch (IllegalAccessException ex) {
System.err.println("Illegal access: " + ex.getMessage());
System.exit(1);
}
catch (java.lang.reflect.InvocationTargetException ex) {
System.err.println("Invocation target exception: " + ex.getMessage());
System.exit(1);
}



// Fudge a few more things in. // Fudge a few more things in.
// XXX Should be able to toss all of these but KnowHOW. // XXX Should be able to toss all of these but KnowHOW.
settingContext.LexPad.Extend(new String[] settingContext.LexPad.Extend(new String[]
{ "KnowHOW", "print", "say", "capture", "LLCode" }); { "KnowHOW", "print", "say", "capture", "LLCode" });
settingContext.LexPad.SetByName("KnowHOW", KnowHOW); settingContext.LexPad.SetByName("KnowHOW", knowHOW);


RakudoCodeRef.IFunc_Body funcPrint = new RakudoCodeRef.IFunc_Body() RakudoCodeRef.IFunc_Body funcPrint = new RakudoCodeRef.IFunc_Body()
{ // create an anonymous class { // create an anonymous class
Expand Down Expand Up @@ -176,7 +206,7 @@ public RakudoObject Invoke(ThreadContext tc, RakudoObject self, RakudoObject cap
settingContext.LexPad.SetByName("say", CodeObjectUtility.WrapNativeMethod(funcSay)); settingContext.LexPad.SetByName("say", CodeObjectUtility.WrapNativeMethod(funcSay));


settingContext.LexPad.SetByName("capture", REPRRegistry.get_REPR_by_name("P6capture").type_object_for(null,null)); settingContext.LexPad.SetByName("capture", REPRRegistry.get_REPR_by_name("P6capture").type_object_for(null,null));
settingContext.LexPad.SetByName("LLCode", REPRRegistry.get_REPR_by_name("RakudoCodeRef").type_object_for(null,KnowHOW.getSTable().REPR.instance_of(null,KnowHOW))); settingContext.LexPad.SetByName("LLCode", REPRRegistry.get_REPR_by_name("RakudoCodeRef").type_object_for(null, knowHOW.getSTable().REPR.instance_of(null, knowHOW)));


return settingContext; return settingContext;
} }
Expand Down

0 comments on commit 3e28165

Please sign in to comment.