Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Allow configuration of out/err in DynJSConfig through the DynThreadCo…

…ntext

and used by the console module for console logging.

Defaults to System.out and System.err.
  • Loading branch information...
commit e74f9e184bb6efe2ebf49963effe9e7c3fedc8db 1 parent 90b4fc0
@bobmcwhirter bobmcwhirter authored
View
2  src/main/java/org/dynjs/runtime/DynJS.java
@@ -113,6 +113,8 @@ public void eval(DynThreadContext context, InputStream is, String filename) {
context.setRuntime(this);
context.setClassLoader(config.getClassLoader());
+ context.setOutputStream(config.getOutputStream());
+ context.setErrorStream(config.getErrorStream());
Executor executor = new Executor(context);
walker.setExecutor(executor);
walker.program();
View
24 src/main/java/org/dynjs/runtime/DynJSConfig.java
@@ -15,16 +15,19 @@
*/
package org.dynjs.runtime;
-import org.dynjs.runtime.loader.Builtin;
-
+import java.io.PrintStream;
import java.util.LinkedHashSet;
import java.util.Set;
+import org.dynjs.runtime.loader.Builtin;
+
public class DynJSConfig {
private boolean debug;
private Set<Builtin> builtins = new LinkedHashSet<>();
private final DynamicClassLoader classLoader;
+ private PrintStream outputStream = System.out;
+ private PrintStream errorStream = System.err;
public DynJSConfig() {
this.classLoader = new DynamicClassLoader();
@@ -53,4 +56,21 @@ public void addBuiltin(String bindingName, Object boundObject) {
public DynamicClassLoader getClassLoader() {
return classLoader;
}
+
+ public void setOutput(PrintStream outputStream) {
+ this.outputStream = outputStream;
+ }
+
+ public PrintStream getOutputStream() {
+ return this.outputStream;
+ }
+
+ public void setErrorStream(PrintStream errorStream) {
+ this.errorStream = errorStream;
+ }
+
+ public PrintStream getErrorStream() {
+ return this.errorStream;
+ }
+
}
View
20 src/main/java/org/dynjs/runtime/DynThreadContext.java
@@ -15,6 +15,8 @@
*/
package org.dynjs.runtime;
+import java.io.OutputStream;
+import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
@@ -72,6 +74,8 @@
private DynamicClassLoader classLoader;
private List<String> loadPaths = Collections.synchronizedList(new ArrayList<String>());
private List<ModuleProvider> moduleProviders = Collections.synchronizedList(new ArrayList<ModuleProvider>());
+ private PrintStream outputStream = System.out;
+ private PrintStream errorStream = System.err;
public DynThreadContext() {
for (Map.Entry<String, Object> builin : BUILTINS.entrySet()) {
@@ -205,6 +209,22 @@ public String toString() {
}
}
+ public void setOutputStream(PrintStream outputStream) {
+ this.outputStream = outputStream;
+ }
+
+ public PrintStream getOutputStream() {
+ return this.outputStream;
+ }
+
+ public void setErrorStream(PrintStream errorStream) {
+ this.errorStream = errorStream;
+ }
+
+ public PrintStream getErrorStream() {
+ return this.errorStream;
+ }
+
public void addModuleProvider(ModuleProvider moduleProvider) {
moduleProviders.add( moduleProvider );
}
View
20 src/main/java/org/dynjs/runtime/modules/ConsoleModule.java
@@ -1,11 +1,27 @@
package org.dynjs.runtime.modules;
+import java.io.OutputStream;
+import java.io.PrintStream;
+
+import org.dynjs.runtime.DynThreadContext;
+
@Module(name = "console")
public class ConsoleModule {
@Export
- public void log(String message) {
- System.err.println( message );
+ public void log(Object self, DynThreadContext context, String message) {
+ OutputStream errStream = context.getErrorStream();
+
+ PrintStream err = null;
+
+ if ( errStream instanceof PrintStream ) {
+ err = (PrintStream) errStream;
+ } else {
+ err = new PrintStream( errStream );
+ }
+
+ err.println( message );
+
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.