Browse files

[java] a classLoader for the Setting compiles ok but probably does no…

…t run
  • Loading branch information...
1 parent a829cc5 commit 3e281657d79e5e5acb1fd5c6942d7e8f289577c7 @mberends mberends committed Sep 22, 2010
Showing with 137 additions and 86 deletions.
  1. +35 −8 java/README.txt
  2. +60 −30 java/runtime/Rakudo/Init.java
  3. +42 −48 java/runtime/Rakudo/Runtime/Ops.java
View
43 java/README.txt
@@ -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.
+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
----------------------------
For clarity, or where there is doubt about the correctness of a
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.
-With apologies to jnthn++, a case convention for the initial letter of
-names is being gradually phased in. Class names, class member names and
-interfaces is start with an uppercase letter. Local variables begin in
-lowercase.
+With apologies to jnthn++, a widely used case convention for the initial
+letter of names is being gradually phased in. Class names, class member
+names and interfaces start with an uppercase letter. Local variables
+begin in lowercase.
6model specific translations
----------------------------
The RakudoObject interface defines properties STable and SC (meaning
SharedTable and SerializationContext). Java does not have properties,
-these become private data members _STable and _SC, and get accessor
-methods getSTable(), setSTable, getSC() and setSC().
+these become private data members and get accessor methods getSTable(),
+setSTable(), getSC() and setSC().
C# to Java translation guidelines (in case insensitive alphabetical order)
--------------------------------------------------------------------------
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# Console.Write becomes Java System.out.print.
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
versatile, because the value part must be a reference type, it cannot be
a value type. Therefore C# Dictionary<string, int> becomes Java
@@ -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# 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; ... .
Also the Java package hierarchy must match the file system directory
@@ -125,5 +139,18 @@ interface.
See: http://msdn.microsoft.com/en-us/library/bb549151.aspx and
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)
View
90 java/runtime/Rakudo/Init.java
@@ -1,5 +1,9 @@
package Rakudo;
+import java.lang.Class;
+import java.lang.ClassLoader;
+import java.lang.reflect.Method;
+
import Rakudo.Metamodel.KnowHOW.KnowHOWBootstrapper;
import Rakudo.Metamodel.KnowHOW.KnowHOWREPR;
import Rakudo.Metamodel.RakudoObject;
@@ -35,38 +39,31 @@
/// Handles the various bits of initialization that are needed.
/// Probably needs some don't-dupe-this work.
/// </summary>
- public static ThreadContext Initialize(String SettingName)
+ public static ThreadContext Initialize(String settingName)
{
// Bootstrap the meta-model.
RegisterRepresentations();
- RakudoObject KnowHOW = KnowHOWBootstrapper.Bootstrap();
+ RakudoObject knowHOW = KnowHOWBootstrapper.Bootstrap();
- // See if we're to load a setting or use the fake bootstrapping one.
- Context settingContext;
- if (SettingName == null)
- {
- settingContext = BootstrapSetting(KnowHOW);
- }
- else
- {
- settingContext = LoadSetting(SettingName, KnowHOW);
- }
+ // Either load a named setting or use the fake bootstrapping one.
+ Context settingContext =
+ (settingName != null) ? LoadSetting(settingName, knowHOW)
+ : BootstrapSetting(knowHOW);
// Cache native capture and LLCode type object.
CaptureHelper.CaptureTypeObject = settingContext.LexPad.GetByName("capture");
CodeObjectUtility.LLCodeTypeObject = (RakudoCodeRef.Instance)settingContext.LexPad.GetByName("LLCode");
// Create an execution domain and a thread context for it.
- ExecutionDomain execDom = new ExecutionDomain();
- ThreadContext thread = new ThreadContext();
- thread.Domain = execDom;
- thread.CurrentContext = settingContext;
- thread.DefaultBoolBoxType = settingContext.LexPad.GetByName("NQPInt");
- thread.DefaultIntBoxType = settingContext.LexPad.GetByName("NQPInt");
- thread.DefaultNumBoxType = settingContext.LexPad.GetByName("NQPNum");
- thread.DefaultStrBoxType = settingContext.LexPad.GetByName("NQPStr");
-
- return thread;
+ ExecutionDomain executionDomain = new ExecutionDomain();
+ ThreadContext threadContext = new ThreadContext();
+ threadContext.Domain = executionDomain;
+ threadContext.CurrentContext = settingContext;
+ threadContext.DefaultBoolBoxType = settingContext.LexPad.GetByName("NQPInt");
+ threadContext.DefaultIntBoxType = settingContext.LexPad.GetByName("NQPInt");
+ threadContext.DefaultNumBoxType = settingContext.LexPad.GetByName("NQPNum");
+ threadContext.DefaultStrBoxType = settingContext.LexPad.GetByName("NQPStr");
+ return threadContext;
}
/// <summary>
@@ -130,24 +127,57 @@ public RakudoObject Invoke(ThreadContext tc, RakudoObject self, RakudoObject cap
/// <param name="Name"></param>
/// <param name="KnowHOW"></param>
/// <returns></returns>
- public static Context LoadSetting(String Name, RakudoObject KnowHOW)
+ public static Context LoadSetting(String name, RakudoObject knowHOW)
{
// Load the assembly.
// 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.
-// TODO var Class = settingAssembly.GetType("NQPSetting");
-// TODO var Method = Class.GetMethod("LoadSetting", BindingFlags.NonPublic | BindingFlags.Static);
-
+ // var Class = settingAssembly.GetType("NQPSetting");
+ // 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.
- // TODO Context settingContext = (Context)Method.Invoke(null, new object[] { });
- Context settingContext = null; // TODO remove
+ // TODO Context settingContext = (Context)Method.Invoke(null, new Object[] { });
+ 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.
// XXX Should be able to toss all of these but KnowHOW.
settingContext.LexPad.Extend(new String[]
{ "KnowHOW", "print", "say", "capture", "LLCode" });
- settingContext.LexPad.SetByName("KnowHOW", KnowHOW);
+ settingContext.LexPad.SetByName("KnowHOW", knowHOW);
RakudoCodeRef.IFunc_Body funcPrint = new RakudoCodeRef.IFunc_Body()
{ // create an anonymous class
@@ -176,7 +206,7 @@ public RakudoObject Invoke(ThreadContext tc, RakudoObject self, RakudoObject cap
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("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;
}
View
90 java/runtime/Rakudo/Runtime/Ops.java
@@ -24,86 +24,86 @@
/// <param name="HOW"></param>
/// <param name="REPRName"></param>
/// <returns></returns>
- public static RakudoObject type_object_for(ThreadContext TC, RakudoObject HOW, String REPRName)
+ public static RakudoObject type_object_for(ThreadContext tc, RakudoObject HOW, String REPRName)
{
- return REPRRegistry.get_REPR_by_name(REPRName).type_object_for(TC, HOW);
+ return REPRRegistry.get_REPR_by_name(REPRName).type_object_for(tc, HOW);
}
/// <summary>
/// Create an instance of an object.
/// </summary>
/// <param name="WHAT"></param>
/// <returns></returns>
- public static RakudoObject instance_of(ThreadContext TC, RakudoObject WHAT)
+ public static RakudoObject instance_of(ThreadContext tc, RakudoObject WHAT)
{
- return WHAT.getSTable().REPR.instance_of(TC, WHAT);
+ return WHAT.getSTable().REPR.instance_of(tc, WHAT);
}
/// <summary>
/// Checks if the representation considers the object defined.
/// </summary>
- /// <param name="Obj"></param>
+ /// <param name="obj"></param>
/// <returns></returns>
- public static boolean repr_defined(ThreadContext TC, RakudoObject Obj)
+ public static boolean repr_defined(ThreadContext tc, RakudoObject obj)
{
- return Obj.getSTable().REPR.defined(TC, Obj);
+ return obj.getSTable().REPR.defined(tc, obj);
}
/// <summary>
/// Gets the value of an attribute.
/// </summary>
- /// <param name="Object"></param>
+ /// <param name="object"></param>
/// <param name="Class"></param>
- /// <param name="Name"></param>
+ /// <param name="name"></param>
/// <returns></returns>
- public static RakudoObject get_attr(ThreadContext TC, RakudoObject Object, RakudoObject Class, String Name)
+ public static RakudoObject get_attr(ThreadContext tc, RakudoObject object, RakudoObject Class, String name)
{
- return Object.getSTable().REPR.get_attribute(TC, Object, Class, Name);
+ return object.getSTable().REPR.get_attribute(tc, object, Class, name);
}
/// <summary>
/// Gets the value of an attribute, using the given hint.
/// </summary>
- /// <param name="Object"></param>
+ /// <param name="object"></param>
/// <param name="Class"></param>
- /// <param name="Name"></param>
+ /// <param name="name"></param>
/// <param name="Hint"></param>
/// <returns></returns>
- public static RakudoObject get_attr_with_hint(ThreadContext TC, RakudoObject Object, RakudoObject Class, String Name, int Hint)
+ public static RakudoObject get_attr_with_hint(ThreadContext tc, RakudoObject object, RakudoObject Class, String name, int Hint)
{
- return Object.getSTable().REPR.get_attribute_with_hint(TC, Object, Class, Name, Hint);
+ return object.getSTable().REPR.get_attribute_with_hint(tc, object, Class, name, Hint);
}
/// <summary>
/// Binds the value of an attribute to the given value.
/// </summary>
- /// <param name="Object"></param>
+ /// <param name="object"></param>
/// <param name="Class"></param>
- /// <param name="Name"></param>
+ /// <param name="name"></param>
/// <param name="Hint"></param>
- public static void bind_attr_with_hint(ThreadContext TC, RakudoObject Object, RakudoObject Class, String Name, RakudoObject Value)
+ public static void bind_attr_with_hint(ThreadContext tc, RakudoObject object, RakudoObject Class, String name, RakudoObject Value)
{
- Object.getSTable().REPR.bind_attribute(TC, Object, Class, Name, Value);
+ object.getSTable().REPR.bind_attribute(tc, object, Class, name, Value);
}
/// <summary>
/// Binds the value of an attribute to the given value, using the
/// given hint.
/// </summary>
- /// <param name="Object"></param>
+ /// <param name="object"></param>
/// <param name="Class"></param>
- /// <param name="Name"></param>
+ /// <param name="name"></param>
/// <param name="Hint"></param>
- public static void bind_attr_with_hint(ThreadContext TC, RakudoObject Object, RakudoObject Class, String Name, int Hint, RakudoObject Value)
+ public static void bind_attr_with_hint(ThreadContext tc, RakudoObject object, RakudoObject Class, String name, int Hint, RakudoObject Value)
{
- Object.getSTable().REPR.bind_attribute_with_hint(TC, Object, Class, Name, Hint, Value);
+ object.getSTable().REPR.bind_attribute_with_hint(tc, object, Class, name, Hint, Value);
}
/// <summary>
/// Finds a method to call by name.
/// </summary>
- /// <param name="Object"></param>
- /// <param name="Name"></param>
+ /// <param name="object"></param>
+ /// <param name="name"></param>
/// <returns></returns>
public static RakudoObject find_method(ThreadContext tc, RakudoObject object, String name)
{
@@ -113,8 +113,8 @@ public static RakudoObject find_method(ThreadContext tc, RakudoObject object, St
/// <summary>
/// Finds a method to call, using the hint if available.
/// </summary>
- /// <param name="Object"></param>
- /// <param name="Name"></param>
+ /// <param name="object"></param>
+ /// <param name="name"></param>
/// <param name="Hint"></param>
/// <returns></returns>
public static RakudoObject find_method_with_hint(ThreadContext tc, RakudoObject object, String name, int hint)
@@ -136,7 +136,7 @@ public static RakudoObject invoke(ThreadContext tc, RakudoObject invokee, Rakudo
/// <summary>
/// Gets the HOW (higher order workings, e.g. meta-package).
/// </summary>
- /// <param name="Obj"></param>
+ /// <param name="obj"></param>
/// <returns></returns>
public static RakudoObject get_how(ThreadContext tc, RakudoObject object)
{
@@ -146,7 +146,7 @@ public static RakudoObject get_how(ThreadContext tc, RakudoObject object)
/// <summary>
/// Gets the WHAT (type object).
/// </summary>
- /// <param name="Obj"></param>
+ /// <param name="obj"></param>
/// <returns></returns>
public static RakudoObject get_what(ThreadContext tc, RakudoObject object)
{
@@ -281,7 +281,7 @@ public static RakudoObject get_lex(ThreadContext tc, String name)
Context curContext = tc.CurrentContext;
while (curContext != null)
{
- // if (CurContext.LexPad.SlotMapping.TryGetValue(Name, out Index)) // the C# version
+ // if (CurContext.LexPad.SlotMapping.TryGetValue(name, out Index)) // the C# version
if (curContext.LexPad.SlotMapping.containsKey(name)) {
int index = curContext.LexPad.SlotMapping.get(name);
return curContext.LexPad.Storage[index];
@@ -302,7 +302,7 @@ public static RakudoObject bind_lex(ThreadContext tc, String name, RakudoObject
Context curContext = tc.CurrentContext;
while (curContext != null)
{
- // if (CurContext.LexPad.SlotMapping.TryGetValue(Name, out Index)) // the C# version
+ // if (CurContext.LexPad.SlotMapping.TryGetValue(name, out Index)) // the C# version
if (curContext.LexPad.SlotMapping.containsKey(name))
{
int index = curContext.LexPad.SlotMapping.get(name);
@@ -318,14 +318,14 @@ public static RakudoObject bind_lex(ThreadContext tc, String name, RakudoObject
/// Looks up a variable in the dynamic scope.
/// </summary>
/// <param name="C"></param>
- /// <param name="Name"></param>
+ /// <param name="name"></param>
/// <returns></returns>
public static RakudoObject get_dynamic(ThreadContext tc, String name)
{
Context curContext = tc.CurrentContext;
while (curContext != null)
{
- // if (CurContext.LexPad.SlotMapping.TryGetValue(Name, out Index)) // the C# version
+ // if (CurContext.LexPad.SlotMapping.TryGetValue(name, out Index)) // the C# version
if (curContext.LexPad.SlotMapping.containsKey(name)) {
int index = curContext.LexPad.SlotMapping.get(name);
return curContext.LexPad.Storage[index];
@@ -339,14 +339,14 @@ public static RakudoObject get_dynamic(ThreadContext tc, String name)
/// Binds the given value to a variable in the dynamic scope.
/// </summary>
/// <param name="C"></param>
- /// <param name="Name"></param>
+ /// <param name="name"></param>
/// <returns></returns>
public static RakudoObject bind_dynamic(ThreadContext tc, String name, RakudoObject value)
{
Context curContext = tc.CurrentContext;
while (curContext != null)
{
- // if (CurContext.LexPad.SlotMapping.TryGetValue(Name, out Index)) // the C# version
+ // if (CurContext.LexPad.SlotMapping.TryGetValue(name, out Index)) // the C# version
if (curContext.LexPad.SlotMapping.containsKey(name))
{
int index = curContext.LexPad.SlotMapping.get(name);
@@ -487,7 +487,7 @@ public static RakudoObject concat(ThreadContext tc, RakudoObject x, RakudoObject
/// Entry point to multi-dispatch over the candidates in the inner
/// dispatcher.
/// </summary>
- /// <param name="TC"></param>
+ /// <param name="tc"></param>
/// <returns></returns>
public static RakudoObject multi_dispatch_over_lexical_candidates(ThreadContext tc, RakudoObject name)
{
@@ -504,15 +504,13 @@ public static RakudoObject multi_dispatch_over_lexical_candidates(ThreadContext
/// Gets a value at a given positional index from a low level list
/// (something that uses the P6list representation).
/// </summary>
- /// <param name="TC"></param>
+ /// <param name="tc"></param>
/// <param name="LLList"></param>
/// <param name="Index"></param>
/// <returns></returns>
public static RakudoObject lllist_get_at_pos(ThreadContext tc, RakudoObject lowlevelList, RakudoObject index)
{
-// TODO correct syntax
-// if (lowlevelList.typeof(P6list.Instance))
- if ( true )
+ if (lowlevelList instanceof P6list.Instance)
{
return ((P6list.Instance)lowlevelList).Storage.get(Ops.unbox_int(tc, index));
}
@@ -526,15 +524,13 @@ public static RakudoObject lllist_get_at_pos(ThreadContext tc, RakudoObject lowl
/// Binds a value at a given positional index from a low level list
/// (something that uses the P6list representation).
/// </summary>
- /// <param name="TC"></param>
+ /// <param name="tc"></param>
/// <param name="LLList"></param>
/// <param name="Index"></param>
/// <returns></returns>
public static void lllist_bind_at_pos(ThreadContext tc, RakudoObject lowlevelList, RakudoObject indexObj, RakudoObject value)
{
-// TODO correct syntax
-// if (LLList is P6list.Instance)
- if ( true )
+ if (lowlevelList instanceof P6list.Instance)
{
ArrayList<RakudoObject> storage = ((P6list.Instance)lowlevelList).Storage;
Integer index = Ops.unbox_int(tc, indexObj);
@@ -561,15 +557,13 @@ public static void lllist_bind_at_pos(ThreadContext tc, RakudoObject lowlevelLis
/// Binds a value at a given positional index from a low level list
/// (something that uses the P6list representation).
/// </summary>
- /// <param name="TC"></param>
+ /// <param name="tc"></param>
/// <param name="LLList"></param>
/// <param name="Index"></param>
/// <returns></returns>
public static RakudoObject lllist_elems(ThreadContext tc, RakudoObject lowlevelList)
{
-// TODO correct syntax
-// if (LLList is P6list.Instance)
- if ( true )
+ if (lowlevelList instanceof P6list.Instance)
{
return Ops.box_int(tc, ((P6list.Instance)lowlevelList).Storage.size(), tc.DefaultIntBoxType);
}

0 comments on commit 3e28165

Please sign in to comment.