Skip to content

Commit

Permalink
Merge pull request #9 from Frege/dev/1.0.2-snapshot
Browse files Browse the repository at this point in the history
Upgrade Frege, support for sandboxed stdout
  • Loading branch information
mmhelloworld committed Mar 9, 2014
2 parents 55ad718 + c55a902 commit 203015e
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 44 deletions.
2 changes: 1 addition & 1 deletion frege-interpreter-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>frege</groupId>
<artifactId>frege-interpreter</artifactId>
<version>1.0.2-SNAPSHOT</version>
<version>1.0.2</version>
</parent>
<artifactId>frege-interpreter-core</artifactId>
<packaging>jar</packaging>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Compiler.Scanner hiding (main, is, interpret)
import Compiler.Main(stdOptions, format,
make, parsePass, postTrue, getOperators)
import Compiler.GUtil as GUtil()
import frege.compiler.types.Flags as Compilerflags(Flag, Flags)
import Lib.PP(TEXT)
import Data.List
import Data.Maybe
Expand Down Expand Up @@ -102,7 +103,8 @@ standardOptions classLoader = do
thisPos = Position.null,
nsPos = Tree.empty,
thisPack = Pack.new "",
errors = 0,
numErrors = 0,
resErrors = 0,
messages = [],
cancelled = return false,
nextPass = 1,
Expand Down Expand Up @@ -314,7 +316,7 @@ runpass (pass,description,post) = do
postconditionHolds <- if state.errors == 0 then post else stio true
unless (postconditionHolds) do
doio (state.stderr.println "FATAL: post condition violated")
changeST Global.{sub <- SubSt.{errors <- (1+)}}
changeST Global.{sub <- SubSt.{numErrors <- (1+)}}
stio ()

derive Show Severity
Expand Down Expand Up @@ -628,16 +630,16 @@ showThrowableCause' depth t = maybe (showNative t) (showThrowableCause' (pred de
-- Native Definitions

data MemoryJavaCompiler = native frege.interpreter.javasupport.MemoryJavaCompiler where
native new :: Mutable s (JMap String ByteArr) -> STMutable s MemoryJavaCompiler
native new :: Mutable s (JMap String ByteArray) -> STMutable s MemoryJavaCompiler
| Mutable s InterpreterClassLoader -> STMutable s MemoryJavaCompiler
native compile :: Mutable s MemoryJavaCompiler -> String -> String -> STMutable s JavaCompilationInfo
native classLoader :: Mutable s MemoryJavaCompiler -> STMutable s InterpreterClassLoader

data InterpreterClassLoader = native frege.interpreter.javasupport.InterpreterClassLoader where
native new :: () -> STMutable s InterpreterClassLoader
| ClassLoader -> IOMutable InterpreterClassLoader
| Mutable s (JMap String ByteArr) -> STMutable s InterpreterClassLoader
pure native classes :: InterpreterClassLoader -> JMap String ByteArr
| Mutable s (JMap String ByteArray) -> STMutable s InterpreterClassLoader
pure native classes :: InterpreterClassLoader -> JMap String ByteArray

private defaultClassLoaderST = do
loader <- InterpreterClassLoader.new ()
Expand All @@ -652,9 +654,13 @@ data JavaCompilationInfo = native frege.interpreter.javasupport.CompilationInfo
native isSuccess :: Mutable s JavaCompilationInfo -> ST s Bool
native errorsAsString :: Mutable s JavaCompilationInfo -> ST s String
native classLoader :: MutableIO JavaCompilationInfo -> IO ClassLoader
native classes :: Mutable s JavaCompilationInfo -> STMutable s (JMap String ByteArr)
native classes :: Mutable s JavaCompilationInfo -> STMutable s (JMap String ByteArray)

data ByteArr = mutable native "byte[]"

data Byte = pure native byte
derive ArrayElement Byte

type ByteArray = JArray Byte

data JMap k v = native java.util.Map where
native put :: Mutable s (JMap k v) -> k -> v -> ST s ()
Expand All @@ -671,43 +677,12 @@ native fieldValue frege.interpreter.javasupport.JavaUtils.fieldValue ::
String -> String -> InterpreterClassLoader -> ST s (Maybe a)

native sandboxFieldValue frege.interpreter.javasupport.JavaUtils.sandboxFieldValue ::
String -> String -> InterpreterClassLoader -> ST s (Maybe a)
String -> String -> String -> StringWriter -> StringWriter -> InterpreterClassLoader -> IO (Maybe a)

pure native longToString Long.toString :: Long -> Int -> String

pure native showNative String.valueOf :: a -> String
pure native throwableCause getCause :: Throwable -> Maybe Throwable


data ObjectArr = native "java.lang.Object[]" where
--- make a new mutable Object array
native new "java.lang.Object[]"
:: Int -> STMutable s ObjectArr
native getAt "frege.runtime.Array.<java.lang.Object>arrayGet"
:: Mutable s ObjectArr -> Int -> ST s (Maybe Object)
native setAt "frege.runtime.Array.<java.lang.Object>arraySet"
:: Mutable s ObjectArr -> Int -> Object -> ST s ()
pure native frozenGetAt "frege.runtime.Array.<java.lang.Object>arrayGet"
:: ObjectArr -> Int -> Maybe Object
--- use this only if it is absolutely sure that there are no nulls in the array
pure native elemAt "frege.runtime.Array.<java.lang.Object>arrayGet"
:: ObjectArr -> Int -> Object
--- the length of the array
pure native length "frege.runtime.Array.<java.lang.Object>arrayLen"
:: ObjectArr -> Int
toList (a::ObjectArr) = elems a 0
where
elems (a::ObjectArr) i
| i < a.length = case frozenGetAt a i of
Just s -> s:elems a (i+1)
Nothing -> elems a (i+1)
| otherwise = []

fromListST :: [Object] -> STMutable u ObjectArr
fromListST objects = (ObjectArr.new objects.length >>= loop 0 objects) where
loop j (x:xs) arr = do ObjectArr.setAt arr j x; loop (j+1) xs arr
loop j [] arr = return arr
fromList objects = ST.run (fromListST objects >>= readonly id)

data Method = pure native java.lang.reflect.Method where
pure native getName :: Method -> String
2 changes: 1 addition & 1 deletion frege-interpreter-java-support/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>frege</groupId>
<artifactId>frege-interpreter</artifactId>
<version>1.0.2-SNAPSHOT</version>
<version>1.0.2</version>
</parent>
<artifactId>frege-interpreter-java-support</artifactId>
<packaging>jar</packaging>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package frege.interpreter.javasupport;

import frege.runtime.Runtime;

import java.io.BufferedReader;
import java.io.FilePermission;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.security.Permission;
import java.util.Map;
Expand All @@ -24,15 +31,28 @@ public static Object fieldValue(final String className,

public static Object sandboxFieldValue(final String className,
final String variableName,
final String stdinStr,
final StringWriter outWriter,
final StringWriter errWriter,
final InterpreterClassLoader loader) {
return sandbox(new FutureTask<>(new Callable<Object>() {
@Override
public Object call() throws Exception {
prepareRuntime(stdinStr, outWriter, errWriter);
return fieldValue(className, variableName, loader);
}
}), 5, TimeUnit.SECONDS);
}

private static void prepareRuntime(final String stdinStr, final StringWriter outWriter, final StringWriter errWriter) {
final BufferedReader stdin = new BufferedReader(new StringReader(stdinStr + "\n"));
final PrintWriter stdout = new PrintWriter(outWriter);
final PrintWriter stderr = new PrintWriter(errWriter);
Runtime.stdin.set(stdin);
Runtime.stdout.set(stdout);
Runtime.stderr.set(stderr);
}

public static <V> V sandbox(final FutureTask<V> task,
final long timeout,
final TimeUnit unit
Expand Down
2 changes: 1 addition & 1 deletion frege-script-engine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>frege</groupId>
<artifactId>frege-interpreter</artifactId>
<version>1.0.2-SNAPSHOT</version>
<version>1.0.2</version>
</parent>
<groupId>frege</groupId>
<artifactId>frege-script-engine</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>frege</groupId>
<artifactId>frege-interpreter</artifactId>
<packaging>pom</packaging>
<version>1.0.2-SNAPSHOT</version>
<version>1.0.2</version>
<name>frege-interpreter</name>
<url>https://github.com/Frege/frege-interpreter</url>
<modules>
Expand All @@ -15,7 +15,7 @@
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<frege.version>3.21.402-g0b89544</frege.version>
<frege.version>3.21.440-gd7ba043</frege.version>
<frege.plugin.version>1.0.5</frege.plugin.version>
<ecj.version>4.2.2</ecj.version>
</properties>
Expand Down

0 comments on commit 203015e

Please sign in to comment.