Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add support for imports, static calls, and constructors.

  • Loading branch information...
commit b0379404a1948200c8b0783ac91324a6acd99c77 1 parent 9675167
@headius authored
View
12 lib/surinx/ast.rb
@@ -59,8 +59,13 @@ def build(body)
def compile(compiler, expr)
compiler.compile(receiver_node)
- compiler.compile(args_node)
- compiler.call name, args_node.size + 1, expr
+
+ if args_node
+ compiler.compile(args_node)
+ compiler.call name, args_node.size + 1, expr
+ else
+ compiler.call name, 1, expr
+ end
end
end
@@ -93,6 +98,9 @@ def compile(compiler, expr)
elsif name == "print"
compiler.compile(args_node)
compiler.print expr
+ elsif name == "import"
+ compiler.import args_node.get(0).value
+ compiler.this if expr
else
compiler.this
compiler.compile(args_node)
View
12 lib/surinx/compiler.rb
@@ -131,7 +131,17 @@ def set_constant(name, body)
end
def get_constant(name)
- compile(instance_variable_get "@#{name}")
+ value = instance_variable_get "@#{name}"
+ if Module === value
+ @mb.ldc(value)
+ else
+ compile(value)
+ end
+ end
+
+ def import(name)
+ short_name = name.split(/\./)[-1]
+ instance_variable_set "@#{short_name}", eval(name)
end
def branch(condition, then_body, else_body, expr)
View
140 src/com/headius/surinx/SimpleJavaBootstrap.java
@@ -10,6 +10,8 @@
import java.dyn.MethodHandle;
import java.dyn.MethodHandles;
import java.dyn.MethodType;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
@@ -135,23 +137,53 @@ public static Object fallback(CallSite site, Object receiver, Object[] args) thr
}
Class rClass = null;
- if (receiver == null) {
- rClass = site.callerClass();
- try {
- rMethod = rClass.getMethod(site.name(), argTypes);
- target = MethodHandles.lookup().unreflect(rMethod);
- target = MethodHandles.dropArguments(target, 0, Object.class);
- target = MethodHandles.convertArguments(target, site.type());
- } catch (NoSuchMethodException nsme) {
- // hacky...try with all Object
- for (int i = 0; i < argTypes.length; i++) {
- argTypes[i] = Object.class;
- }
- rMethod = rClass.getMethod(site.name(), argTypes);
- target = MethodHandles.lookup().unreflect(rMethod);
- target = MethodHandles.dropArguments(target, 0, Object.class);
- target = MethodHandles.convertArguments(target, site.type());
+ if (receiver == null || receiver.getClass() == Class.class) {
+ if (receiver == null) {
+ rClass = site.callerClass();
+ } else {
+ rClass = (Class)receiver;
}
+
+ if (site.name().equals("new")) {
+ // constructor calls not working in MLVM right now
+ // try {
+ // Constructor rConstructor = rClass.getConstructor(argTypes);
+ // target = MethodHandles.lookup().unreflectConstructor(rConstructor);
+ // } catch (NoSuchMethodException nsme) {
+ // // hacky...try with all Object
+ // for (int i = 0; i < argTypes.length; i++) {
+ // argTypes[i] = Object.class;
+ // }
+ // Constructor rConstructor = rClass.getConstructor(argTypes);
+ // target = MethodHandles.lookup().unreflectConstructor(rConstructor);
+ // }
+
+ argTypes = new Class[args.length + 1];
+ argTypes[0] = Class.class;
+ for (int i = 0; i < args.length; i++) {
+ argTypes[i + 1] = Object.class;
+ }
+ rMethod = SimpleJavaBootstrap.class.getMethod("__new__", argTypes);
+ target = MethodHandles.lookup().unreflect(rMethod);
+ target = MethodHandles.convertArguments(target, site.type());
+
+ site.setTarget(target);
+ return __new__(rClass, args);
+ } else {
+ try {
+ rMethod = rClass.getMethod(site.name(), argTypes);
+ target = MethodHandles.lookup().unreflect(rMethod);
+ } catch (NoSuchMethodException nsme) {
+ // hacky...try with all Object
+ for (int i = 0; i < argTypes.length; i++) {
+ argTypes[i] = Object.class;
+ }
+ rMethod = rClass.getMethod(site.name(), argTypes);
+ target = MethodHandles.lookup().unreflect(rMethod);
+ }
+ target = MethodHandles.dropArguments(target, 0, Object.class);
+ target = MethodHandles.convertArguments(target, site.type());
+ }
} else {
rClass = receiver.getClass();
rMethod = rClass.getMethod(site.name(), argTypes);
@@ -305,4 +337,80 @@ public static final Boolean equals(Double a, Integer b) {
public static final Boolean equals(Boolean a, Boolean b) {
return a == b;
}
+
+ public static final Object __new__(Class target) {
+ try {
+ return target.newInstance();
+ } catch (InstantiationException ie) {
+ throw new RuntimeException(ie);
+ } catch (IllegalAccessException iae) {
+ throw new RuntimeException(iae);
+ }
+ }
+
+ public static final Object __new__(Class target, Object arg0) {
+ Constructor c;
+ try {
+ c = target.getConstructor(arg0.getClass());
+ return c.newInstance(arg0);
+ } catch (NoSuchMethodException nsme) {
+ throw new RuntimeException(nsme);
+ } catch (InstantiationException ie) {
+ throw new RuntimeException(ie);
+ } catch (IllegalAccessException iae) {
+ throw new RuntimeException(iae);
+ } catch (InvocationTargetException ite) {
+ throw new RuntimeException(ite);
+ }
+ }
+
+ public static final Object __new__(Class target, Object arg0, Object arg1) {
+ Constructor c;
+ try {
+ c = target.getConstructor(arg0.getClass(), arg1.getClass());
+ return c.newInstance(arg0, arg1);
+ } catch (NoSuchMethodException nsme) {
+ throw new RuntimeException(nsme);
+ } catch (InstantiationException ie) {
+ throw new RuntimeException(ie);
+ } catch (IllegalAccessException iae) {
+ throw new RuntimeException(iae);
+ } catch (InvocationTargetException ite) {
+ throw new RuntimeException(ite);
+ }
+ }
+
+ public static final Object __new__(Class target, Object arg0, Object arg1, Object arg2) {
+ Constructor c;
+ try {
+ c = target.getConstructor(arg0.getClass(), arg1.getClass(), arg2.getClass());
+ return c.newInstance(arg0, arg1, arg2);
+ } catch (NoSuchMethodException nsme) {
+ throw new RuntimeException(nsme);
+ } catch (InstantiationException ie) {
+ throw new RuntimeException(ie);
+ } catch (IllegalAccessException iae) {
+ throw new RuntimeException(iae);
+ } catch (InvocationTargetException ite) {
+ throw new RuntimeException(ite);
+ }
+ }
+
+ public static final Object __new__(Class target, Object[] args) {
+ Class[] argTypes = new Class[args.length];
+ for (int i = 0; i < args.length; i++) argTypes[i] = args[i].getClass();
+ Constructor c;
+ try {
+ c = target.getConstructor(argTypes);
+ return c.newInstance(args);
+ } catch (NoSuchMethodException nsme) {
+ throw new RuntimeException(nsme);
+ } catch (InstantiationException ie) {
+ throw new RuntimeException(ie);
+ } catch (IllegalAccessException iae) {
+ throw new RuntimeException(iae);
+ } catch (InvocationTargetException ite) {
+ throw new RuntimeException(ite);
+ }
+ }
}
View
3  test/test_constructor.sx
@@ -0,0 +1,3 @@
+import 'java.lang.String'
+
+puts String.new('foo') == 'foo'
View
2  test/test_import.sx
@@ -0,0 +1,2 @@
+import 'java.lang.String'
+puts String == 'foo'.getClass
View
4 test/test_static.sx
@@ -0,0 +1,4 @@
+import 'java.lang.System'
+
+System.setProperty 'foo', 'bar'
+puts System.getProperty('foo') == 'bar'
Please sign in to comment.
Something went wrong with that request. Please try again.