Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setOutputStream now changes output of print statements #108

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/main/java/org/dynjs/runtime/ExecutionContext.java
@@ -1,5 +1,6 @@
package org.dynjs.runtime;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
Expand All @@ -23,9 +24,10 @@ public static ExecutionContext createGlobalExecutionContext(DynJS runtime) {
context.clock = runtime.getConfig().getClock();
context.timeZone = runtime.getConfig().getTimeZone();
context.locale = runtime.getConfig().getLocale();
context.outputStream = runtime.getConfig().getOutputStream();
return context;
}

public static ExecutionContext createGlobalExecutionContext(DynJS runtime, InitializationListener listener) {
ExecutionContext context = ExecutionContext.createEvalExecutionContext(runtime);
listener.initialize(context);
Expand All @@ -51,6 +53,7 @@ public static ExecutionContext createEvalExecutionContext(DynJS runtime) {
private TimeZone timeZone;
private Locale locale;
private Object functionReference;
private PrintStream outputStream;

public ExecutionContext(ExecutionContext parent, LexicalEnvironment lexicalEnvironment, LexicalEnvironment variableEnvironment, Object thisBinding, boolean strict) {
this.parent = parent;
Expand All @@ -60,6 +63,18 @@ public ExecutionContext(ExecutionContext parent, LexicalEnvironment lexicalEnvir
this.strict = strict;
}

public PrintStream getOutputStream() {
if (this.outputStream == null) {
if (this.getParent() == null) {
return System.out;
} else {
return this.getParent().getOutputStream();
}
} else {
return this.outputStream;
}
}

public Object getFunctionReference() {
return this.functionReference;
}
Expand Down Expand Up @@ -558,7 +573,7 @@ public void collectStackElements(List<StackElement> elements) {
public JSObject getPrototypeFor(String type) {
return getGlobalObject().getPrototypeFor(type);
}

public String toString() {
return "ExecutionContext: " + System.identityHashCode( this ) + "; parent=" + this.parent;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/dynjs/runtime/builtins/Print.java
Expand Up @@ -28,10 +28,10 @@ public Print(GlobalObject globalObject) {

@Override
public Object call(ExecutionContext context, Object self, Object... args) {
System.out.println(Types.toString(context, args[0]));
context.getOutputStream().println(Types.toString(context, args[0]));
return Types.UNDEFINED;
}

@Override
public void setFileName() {
this.filename = "org/dynjs/runtime/builtins/Print.java";
Expand Down
31 changes: 31 additions & 0 deletions src/test/java/org/dynjs/runtime/OutputStreamTest.java
@@ -0,0 +1,31 @@
package org.dynjs.runtime;

import static org.fest.assertions.Assertions.*;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.io.IOException;

import org.dynjs.Config;
import org.junit.Test;

public class OutputStreamTest {
@Test
public void testOutputStream() throws UnsupportedEncodingException, IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(bos);

Config config = new Config();
config.setOutputStream(ps);
DynJS dynjs = new DynJS(config);

dynjs.evaluate("print(\"hello world\");");

ps.flush();
bos.flush();
String result = bos.toString("UTF-8");

assertThat(result).isEqualTo("hello world\n");

}
}