Skip to content

Commit

Permalink
new client network connection; better handling of arrays in calls to …
Browse files Browse the repository at this point in the history
…java
  • Loading branch information
conanite committed Oct 10, 2009
1 parent bac41fb commit a02f3ca
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 45 deletions.
14 changes: 0 additions & 14 deletions rainbow.sh

This file was deleted.

2 changes: 2 additions & 0 deletions src/arc/rainbow.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
CP=`echo rainbow/java-lib/* | sed 's/ /\:/g'`
java -server -cp rainbow.jar:$CP rainbow.Console $*
6 changes: 6 additions & 0 deletions src/arc/rainbow/rainbow.arc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
(is type.method 'sym) `(java-static-invoke ,,class ',method ,@args)
`(java-static-invoke ,,class ,method ,@args))))

(mac java-imports (pkg . classes)
`(do ,@(map (fn (_) `(java-import ,(string pkg "." _))) classes)))

(def java-accessor (dir name)
(let prop-chars (coerce (string name) 'cons)
(sym+ dir (upcase-initial name))))
Expand Down Expand Up @@ -115,3 +118,6 @@
car.toks
`(string ,@toks))))




5 changes: 3 additions & 2 deletions src/arc/rainbow/swing.arc
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@
(each (name value) (pair name-value-pairs)
(it 'addAttribute (style-constant name) (if (is value t) t (awt-color value))))))

(mac courier (font-size)
`(Font new "Courier" font-plain* ,font-size))
(def make-font (name size) (Font new name font-plain* size))

(def courier (font-size) (make-font "Courier" font-size))

(mac dim (x y)
`(java-new "java.awt.Dimension" ,x ,y))
Expand Down
18 changes: 9 additions & 9 deletions src/arc/rainbow/welder.arc
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@

(assign night-colour-scheme (obj
background (awt-color 'black)
caret (awt-color 'white)
default (swing-style 'foreground 'gray 'background 'black)
caret (awt-color 'yellow)
default (swing-style 'foreground 'white 'background 'black)
syntax (swing-style 'foreground 'gray)
unmatched-syntax (swing-style 'foreground 'black 'background 'red 'bold t )
paren-match (swing-style 'foreground 'gray 'background 'blue)
search-highlight (swing-style 'background 'yellow)
sym (swing-style 'foreground "#80D080")
sym-string (swing-style 'foreground "#80D080")
sym-fn (swing-style 'foreground "#C0D0C0" 'bold t)
sym-mac (swing-style 'foreground "#9090B0" 'bold t)
string (swing-style 'foreground "#C0D0D0")
sym (swing-style 'foreground "#D0F0A0")
sym-string (swing-style 'foreground "#DDEEEE")
sym-fn (swing-style 'foreground 'white 'bold t)
sym-mac (swing-style 'foreground "#9090F0" 'bold t)
string (swing-style 'foreground "#DDEEEE")
int (swing-style 'foreground "#808040")
char (swing-style 'foreground "#706090")
comment (swing-style 'foreground "#604060" 'italic t)
Expand Down Expand Up @@ -546,7 +546,7 @@
(when (and editor!dirty (> (- (msec) editor!dirty) 400))
(wipe editor!dirty)
(welder-save-editor-content editor)
(time (welder-reindex editor))
(welder-reindex editor)
(later (colourise editor)
(editor!frame 'setTitle (welder-window-title editor))))
(sleep 0.1)
Expand Down Expand Up @@ -655,7 +655,7 @@
(welder-init (editor)
(configure-bean editor!pane
'caretColor colour-scheme!caret
'font (courier font-size)
'font (make-font "Courier" font-size)
'selectionColor colour-scheme!selection
'background colour-scheme!background))

Expand Down
2 changes: 1 addition & 1 deletion src/java/rainbow/Console.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import rainbow.parser.ParseException;
import rainbow.types.*;
import rainbow.util.Argv;
import rainbow.vm.VM;
import rainbow.vm.Instruction;
import rainbow.vm.VM;
import rainbow.vm.interpreter.visitor.Visitor;

import java.io.*;
Expand Down
2 changes: 1 addition & 1 deletion src/java/rainbow/LexicalClosure.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public LexicalClosure(int length, LexicalClosure parent) {

public void add(ArcObject value) {
if (count >= bindings.length) {
throw new Error("Can't add " + value + " to bindings: already full (" + count + ") " + Pair.buildFrom(bindings));
throw new ArcError("Can't add " + value + " to bindings: already full (" + count + ") " + Pair.buildFrom(bindings));
}
bindings[count] = value;
count++;
Expand Down
2 changes: 2 additions & 0 deletions src/java/rainbow/functions/Environment.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import rainbow.functions.lists.*;
import rainbow.functions.maths.*;
import rainbow.functions.network.ClientIp;
import rainbow.functions.network.Connect;
import rainbow.functions.network.OpenSocket;
import rainbow.functions.network.SocketAccept;
import rainbow.functions.predicates.*;
Expand Down Expand Up @@ -160,6 +161,7 @@ public static void init() {
new OpenSocket();
new ClientIp();
new SocketAccept();
new Connect();

new OutFile();
new InFile();
Expand Down
14 changes: 8 additions & 6 deletions src/java/rainbow/functions/IO.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,26 @@ public static void close(ArcObject port) {
((Output) port).close();
} else if (port instanceof ArcSocket) {
((ArcSocket) port).close();
} else if (port instanceof JavaObject) {
((JavaObject) port).close();
} else {
throw new ArcError("close: expected Input or Output object; got " + port);
}
}

public static Output chooseOutputPort(ArcObject port, Object caller) {
if (!(port instanceof Nil)) {
return Output.cast(port, caller);
} else {
if (port instanceof Nil) {
return stdOut();
} else {
return Output.cast(port, caller);
}
}

public static Input chooseInputPort(ArcObject port, Object caller) {
if (!(port instanceof Nil)) {
return Input.cast(port, caller);
} else {
if (port instanceof Nil) {
return stdIn();
} else {
return Input.cast(port, caller);
}
}

Expand Down
40 changes: 40 additions & 0 deletions src/java/rainbow/functions/network/Connect.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package rainbow.functions.network;

import rainbow.ArcError;
import rainbow.functions.Builtin;
import rainbow.types.*;
import rainbow.vm.VM;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class Connect extends Builtin {
public Connect() {
super("client-socket");
}

public void invokef(VM vm, ArcObject arg1, ArcObject arg2) {
String host = ((ArcString)arg1).value();
int port = (int) ((ArcNumber)arg2).toInt();
Socket socket;
try {
socket = new Socket(host, port);
InputStream in = socket.getInputStream();
OutputStream out = socket.getOutputStream();
vm.pushA(Pair.buildFrom(
new SocketInputPort(in, (ArcString) arg1),
new SocketOutputPort(out)));
} catch (IOException e) {
throw new ArcError("Couldn't connect to " + arg1 + ":" + arg2 + ", " + e, e);
}

}

public ArcObject invoke(Pair args) {
checkExactArgsCount(args, 2, getClass());
ArcNumber n = ArcNumber.cast(args.car(), this);
return new ArcSocket((int) n.toInt());
}
}
2 changes: 1 addition & 1 deletion src/java/rainbow/functions/system/ShellInvoke.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public ArcObject invoke(Pair args) {
SystemFunctions.copyStream(SystemFunctions.createProcess(ArcString.cast(args.car(), this)), IO.stdOut());
return NIL;
} catch (IOException e) {
throw new ArcError("system: failed to run " + args.car());
throw new ArcError("system: failed to run " + args.car() + ": " + e, e);
}
}
}
2 changes: 1 addition & 1 deletion src/java/rainbow/types/ArcSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public Pair accept() {
ArcString address = ArcString.make(socket.getInetAddress().toString());
return Pair.buildFrom(
new SocketInputPort(in, address),
new SocketOutputPort(out, address),
new SocketOutputPort(out),
address);
} catch (IOException e) {
throw new ArcError("socket-accept: " + ss.getLocalPort() + " failed : " + e.getMessage(), e);
Expand Down
49 changes: 47 additions & 2 deletions src/java/rainbow/types/JavaObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
import rainbow.functions.Closure;
import rainbow.functions.interpreted.InterpretedFunction;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.IOException;
import java.lang.reflect.*;
import java.util.*;

Expand Down Expand Up @@ -280,6 +284,10 @@ private static boolean match(Class parameterType, ArcObject arcObject) {
return true;
} else if (Character.class.isAssignableFrom(parameterType) && arcObject instanceof ArcCharacter) {
return true;
} else if (OutputStream.class.isAssignableFrom(parameterType) && arcObject instanceof Output) {
return true;
} else if (InputStream.class.isAssignableFrom(parameterType) && arcObject instanceof Input) {
return true;
} else if (parameterType == String.class && (arcObject instanceof ArcString || arcObject instanceof Symbol)) {
return true;
} else if (parameterType == List.class && arcObject instanceof Pair) {
Expand All @@ -291,13 +299,26 @@ private static boolean match(Class parameterType, ArcObject arcObject) {
} else if (parameterType.isInterface() && (arcObject instanceof Hash || arcObject instanceof InterpretedFunction || arcObject instanceof Closure)) {
return true;
} else if (parameterType.isArray()) {
return arcObject instanceof Pair;
return matchArrayType(parameterType, arcObject);
} else if (parameterType.isAssignableFrom(arcObject.unwrap().getClass())) {
return true;
}
return false;
}

private static boolean matchArrayType(Class parameterType, ArcObject arcObject) {
Class componentType = parameterType.getComponentType();
if (componentType.isPrimitive()) {
if (!(arcObject instanceof JavaObject)) {
return false;
}
Object wrapped = ((JavaObject)arcObject).unwrap();
return (wrapped.getClass().isArray() && wrapped.getClass().getComponentType() == componentType);
} else {
return arcObject instanceof Pair;
}
}

private static boolean isPrimitiveNumber(Class p) {
return p == Integer.TYPE || p == Long.TYPE || p == Double.TYPE || p == Float.TYPE;
}
Expand All @@ -316,9 +337,19 @@ public static ArcObject wrap(Object o) {
} else if (o instanceof Character) {
return ArcCharacter.make((Character) o);
} else if (o.getClass().isArray()) {
return wrapList((Object[]) o);
Class arrayClass = o.getClass();
Class componentClass = arrayClass.getComponentType();
if (componentClass.isPrimitive()) {
return new JavaObject(o);
} else {
return wrapList((Object[]) o);
}
} else if (o instanceof List) {
return wrapList((List) o);
} else if (o instanceof InputStream) {
return new Input((InputStream) o);
} else if (o instanceof OutputStream) {
return new Output(new PrintStream((OutputStream) o));
} else if (o instanceof Map) {
return wrapMap((Map) o);
} else if (o instanceof Boolean) {
Expand Down Expand Up @@ -357,4 +388,18 @@ private static ArcObject wrapMap(Map map) {
}
return hash;
}

public void close() {
try {
if (object instanceof InputStream) {
((InputStream)object).close();
} else if (object instanceof OutputStream) {
((OutputStream)object).close();
} else {
throw new ArcError("close: unexpected object: " + this);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
8 changes: 1 addition & 7 deletions src/java/rainbow/types/SocketOutputPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,9 @@
import java.io.PrintStream;

public class SocketOutputPort extends Output {
private ArcString client;

public SocketOutputPort(OutputStream out, ArcString client) {
public SocketOutputPort(OutputStream out) {
super(new PrintStream(out));
this.client = client;
}

public ArcString clientAddress() {
return client;
}

public static SocketInputPort cast(ArcObject argument, ArcObject caller) {
Expand Down
2 changes: 1 addition & 1 deletion src/java/rainbow/vm/instructions/invoke/Invoke_1.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import rainbow.types.ArcObject;
import rainbow.types.Pair;
import rainbow.types.Symbol;
import rainbow.vm.VM;
import rainbow.vm.Instruction;
import rainbow.vm.VM;
import rainbow.vm.interpreter.BoundSymbol;

import java.util.List;
Expand Down

0 comments on commit a02f3ca

Please sign in to comment.