Skip to content

Commit

Permalink
Create a new instance of Module for every call of Viz. Access errors …
Browse files Browse the repository at this point in the history
…via a new vizLastErrorMessage C API.
  • Loading branch information
mdaines committed Jan 13, 2017
1 parent 435220d commit 2257a40
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ viz.js: src/pre.js module.js src/post.js
cat $^ > $@

module.js: src/viz.c
emcc -v -Os --closure 1 --memory-init-file 0 -s USE_ZLIB=1 -s MODULARIZE=1 -s EXPORTED_FUNCTIONS="['_vizRenderFromString', '_aglasterr', '_dtextract', '_Dtqueue']" -s EXPORTED_RUNTIME_METHODS="['Pointer_stringify', 'ccall', 'UTF8ToString']" -o $@ $< -I$(PREFIX)/include -I$(PREFIX)/include/graphviz -L$(PREFIX)/lib -L$(PREFIX)/lib/graphviz -lgvplugin_core -lgvplugin_dot_layout -lgvplugin_neato_layout -lcdt -lcgraph -lgvc -lgvpr -lpathplan -lexpat -lxdot -lz
emcc -v -Os --closure 1 --memory-init-file 0 -s USE_ZLIB=1 -s MODULARIZE=1 -s EXPORTED_FUNCTIONS="['_vizRenderFromString', '_vizLastErrorMessage', '_dtextract', '_Dtqueue']" -s EXPORTED_RUNTIME_METHODS="['Pointer_stringify', 'ccall', 'UTF8ToString']" -o $@ $< -I$(PREFIX)/include -I$(PREFIX)/include/graphviz -L$(PREFIX)/lib -L$(PREFIX)/lib/graphviz -lgvplugin_core -lgvplugin_dot_layout -lgvplugin_neato_layout -lcdt -lcgraph -lgvc -lgvpr -lpathplan -lexpat -lxdot -lz


$(PREFIX):
Expand Down
25 changes: 6 additions & 19 deletions src/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,18 @@
return render(src, format, engine);
}
}

var graphviz;
var errors;

function appendError(buf) {
errors += graphviz["Pointer_stringify"](buf);
}

function render(src, format, engine) {
if (typeof graphviz === "undefined") {
graphviz = Module();
}

errors = "";
var graphviz = Module();

var resultPointer = graphviz["ccall"]("vizRenderFromString", "number", ["string", "string", "string"], [src, format, engine]);
var resultString = graphviz["Pointer_stringify"](resultPointer);
graphviz["_free"](resultPointer);

var errorMessagePointer = graphviz["ccall"]("vizLastErrorMessage", "number", [], []);
var errorMessageString = graphviz["Pointer_stringify"](errorMessagePointer);

if (errors != "") {
throw errors;
if (errorMessageString != "") {
throw errorMessageString;
}

return resultString;
Expand Down Expand Up @@ -105,10 +96,6 @@
}
}

Viz.setMemorySize = function(size) {
graphviz = Module({ TOTAL_MEMORY: size });
}

Viz.svgXmlToPngBase64 = function(svgXml, scale, callback) {
Viz.svgXmlToPngImageElement(svgXml, scale, function(err, image) {
if (err) {
Expand Down
25 changes: 9 additions & 16 deletions src/viz.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@ extern gvplugin_library_t gvplugin_neato_layout_LTX_library;
extern gvplugin_library_t gvplugin_core_LTX_library;

GVC_t *context = NULL;
char *errorMessage = NULL;

int vizErrorf(char *buf) {
EM_ASM_({ appendError($0); }, buf);
errorMessage = buf;
return 0;
}

char* vizRenderFromString(const char *src, const char *format, const char *engine) {
char* vizLastErrorMessage() {
return errorMessage;
}

Agraph_t *graph;
char *result;
char* vizRenderFromString(const char *src, const char *format, const char *engine) {
Agraph_t *graph = NULL;
char *result = NULL;
unsigned int length;

if (context == NULL) {
Expand All @@ -28,25 +32,14 @@ char* vizRenderFromString(const char *src, const char *format, const char *engin
agseterr(AGERR);
agseterrf(vizErrorf);

// Reset line numbers.
agreadline(1);

while ((graph = agmemread(src))) {
if (result == NULL) {
gvLayout(context, graph, engine);

// result is freed in post.js.
gvRenderData(context, graph, format, &result, &length);

gvFreeLayout(context, graph);
}

agclose(graph);

// agmemread will continue to process graphs from the initial input. Set its *new* input to the empty string so that we don't go into an infinite loop.
src = "";
}

return result;

return result;
}

0 comments on commit 2257a40

Please sign in to comment.