Skip to content

Commit

Permalink
Fix that a previous error would cause an exception to be thrown on va…
Browse files Browse the repository at this point in the history
…lid input.

This addresses #49. Thanks to @caseywatts for reporting this.
  • Loading branch information
mdaines committed Oct 30, 2015
1 parent ba12882 commit f595d85
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
13 changes: 9 additions & 4 deletions src/post.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
var graphviz;
var errors;

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

function Viz(src) {
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
Expand All @@ -9,14 +14,14 @@
graphviz = Module();
}

errors = "";

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

var lastError = graphviz["ccall"]("aglasterr", "string", [], []);

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

return resultString;
Expand Down
12 changes: 9 additions & 3 deletions src/viz.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#include <gvc.h>
#include <emscripten.h>

extern gvplugin_library_t gvplugin_dot_layout_LTX_library;
extern gvplugin_library_t gvplugin_neato_layout_LTX_library;
extern gvplugin_library_t gvplugin_core_LTX_library;

GVC_t *context = NULL;

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

char* vizRenderFromString(const char *src, const char *format, const char *engine) {

Agraph_t *graph;
Expand All @@ -18,9 +24,9 @@ char* vizRenderFromString(const char *src, const char *format, const char *engin
gvAddLibrary(context, &gvplugin_dot_layout_LTX_library);
gvAddLibrary(context, &gvplugin_neato_layout_LTX_library);
}
// Don't immediately report errors. Instead, we will ask aglasterr in post.js.
agseterr(AGMAX);

agseterr(AGERR);
agseterrf(vizErrorf);

// Reset line numbers.
agreadline(1);
Expand Down
11 changes: 9 additions & 2 deletions tests/input.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
QUnit.module("input");

QUnit.test("result from first graph in input is returned for multiple invocations", function(assert) {
var result = Viz("digraph A {} digraph B {}", { format: "xdot" });
var result;

result = Viz("digraph A {} digraph B {}", { format: "xdot" });
assert.ok(result.match(/digraph A/), "Result should contain \"digraph A\"");
assert.notOk(result.match(/digraph B/), "Result should not contain \"digraph B\"");

var result = Viz("digraph B {} digraph A {}", { format: "xdot" });
result = Viz("digraph B {} digraph A {}", { format: "xdot" });
assert.ok(result.match(/digraph B/), "Result should contain \"digraph B\"");
assert.notOk(result.match(/digraph A/), "Result should not contain \"digraph A\"");
});

QUnit.test("after throwing an exception on invalid input, do not throw one on valid input", function(assert) {
assert.throws(function() { Viz("digraph { \n ->"); });
Viz("digraph { a -> b;}");
});

QUnit.test("syntax error in graph throws exception", function(assert) {
assert.throws(function() {
Viz("digraph { \n ->");
Expand Down

0 comments on commit f595d85

Please sign in to comment.