Skip to content

Commit adb04db

Browse files
committed
make totalMemory work again
Signed-off-by: Stefan Niederhauser <[email protected]>
1 parent 128a3d8 commit adb04db

File tree

5 files changed

+18
-11
lines changed

5 files changed

+18
-11
lines changed

graphviz-java/src/main/java/guru/nidi/graphviz/engine/AbstractJsGraphvizEngine.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import java.util.AbstractMap.SimpleEntry;
2121
import java.util.Map.Entry;
2222

23-
import static guru.nidi.graphviz.engine.IoUtils.*;
23+
import static guru.nidi.graphviz.engine.IoUtils.readStream;
2424

2525
public abstract class AbstractJsGraphvizEngine extends AbstractGraphvizEngine {
2626
public AbstractJsGraphvizEngine(boolean sync) {
@@ -36,9 +36,9 @@ public String execute(String src, Options options) {
3636

3737
protected String jsVizExec(String src, Options options) {
3838
final Entry<String, Options> srcAndOptions = preprocessCode(src, options);
39-
return src.startsWith("render")
40-
? src
41-
: ("render('" + srcAndOptions.getKey() + "'," + srcAndOptions.getValue().toJson(false) + ");");
39+
final String memory = options.totalMemory == null ? "" : "totalMemory=" + options.totalMemory + ";";
40+
final String render = "render('" + srcAndOptions.getKey() + "'," + srcAndOptions.getValue().toJson(false) + ");";
41+
return src.startsWith("render") ? src : (memory + render);
4242
}
4343

4444
protected Entry<String, Options> preprocessCode(String src, Options options) {
@@ -68,12 +68,19 @@ protected String jsVizCode() throws IOException {
6868
}
6969

7070
protected String jsInitEnv() {
71-
return "var viz = new Viz();"
71+
return "var viz; var totalMemory = 16777216;"
72+
+ "function initViz(force){"
73+
+ " if (force || !viz || viz.totalMemory !== totalMemory){"
74+
+ " viz = new Viz({Module: function(){ return Viz.Module({TOTAL_MEMORY: totalMemory}); }, render: Viz.render});"
75+
+ " viz.totalMemory = totalMemory;"
76+
+ " }"
77+
+ " return viz;"
78+
+ "}"
7279
+ "function render(src, options){"
7380
+ " try {"
74-
+ " viz.renderString(src, options)"
81+
+ " initViz().renderString(src, options)"
7582
+ " .then(function(res) { result(res); })"
76-
+ " .catch(function(err) { viz = new Viz(); error(err.toString()); });"
83+
+ " .catch(function(err) { initViz(true); error(err.toString()); });"
7784
+ " } catch(e) { error(e.toString()); }"
7885
+ "}";
7986
}

graphviz-java/src/main/java/guru/nidi/graphviz/engine/GraphvizGraalEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
import java.io.InputStream;
2222

23-
import static guru.nidi.graphviz.engine.IoUtils.*;
23+
import static guru.nidi.graphviz.engine.IoUtils.readStream;
2424

2525
class GraphvizGraalEngine extends AbstractJsGraphvizEngine {
2626
private static final Context CTX = Context.newBuilder("js").allowAllAccess(true).build();

graphviz-java/src/main/java/guru/nidi/graphviz/engine/GraphvizNashornEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import java.util.regex.Matcher;
2121
import java.util.regex.Pattern;
2222

23-
import static guru.nidi.graphviz.engine.IoUtils.*;
23+
import static guru.nidi.graphviz.engine.IoUtils.readStream;
2424

2525
class GraphvizNashornEngine extends AbstractJsGraphvizEngine {
2626
private static final ScriptEngine ENGINE = new ScriptEngineManager().getEngineByExtension("js");

graphviz-java/src/main/java/guru/nidi/graphviz/engine/Rasterizer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import java.awt.image.BufferedImage;
2121
import java.util.function.Consumer;
2222

23-
import static guru.nidi.graphviz.engine.IoUtils.*;
23+
import static guru.nidi.graphviz.engine.IoUtils.isOnClasspath;
2424

2525
public interface Rasterizer {
2626
@Nullable

graphviz-java/src/test/java/guru/nidi/graphviz/model/ExampleTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ void ex1b() throws IOException {
7474
final Node
7575
printf = node("printf"),
7676
make_string = node("make_string");
77-
final Graph g = graph().graphAttr().with("dpi",300).directed().with(
77+
final Graph g = graph().graphAttr().with("dpi", 300).directed().with(
7878
node("main").with(Color.rgb("ffcc00"), Style.FILLED).link(
7979
node("parse").link(node("execute")
8080
.link(make_string, printf, node("compare"))),

0 commit comments

Comments
 (0)