Skip to content

Commit

Permalink
updated sources to be compileable with emcc
Browse files Browse the repository at this point in the history
added prerun.js
  • Loading branch information
MGmotors committed Nov 7, 2019
1 parent 6bf559b commit 23890a4
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/minisat/core/SolverTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ struct Lit {
int x;

// Use this as a constructor:
friend Lit mkLit(Var var, bool sign = false);
friend Lit mkLit(Var var, bool sign);

bool operator == (Lit p) const { return x == p.x; }
bool operator != (Lit p) const { return x != p.x; }
bool operator < (Lit p) const { return x < p.x; } // '<' makes p, ~p adjacent in the ordering.
};


inline Lit mkLit (Var var, bool sign) { Lit p; p.x = var + var + (int)sign; return p; }
inline Lit mkLit (Var var, bool sign = false) { Lit p; p.x = var + var + (int)sign; return p; }
inline Lit operator ~(Lit p) { Lit q; q.x = p.x ^ 1; return q; }
inline Lit operator ^(Lit p, bool b) { Lit q; q.x = p.x ^ (unsigned int)b; return q; }
inline bool sign (Lit p) { return p.x & 1; }
Expand Down
2 changes: 1 addition & 1 deletion src/minisat/utils/System.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWA

#include "minisat/utils/System.h"

#if defined(__linux__)
#if defined(__linux__) || defined(__EMSCRIPTEN__)

#include <stdlib.h>

Expand Down
92 changes: 92 additions & 0 deletions src_js/prerun.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* This file adds some Methods to the Module generated by emscripten.
* The file should be included to the buildprocess by adding the --pre-js option to emcc.
*/

/**
* Adds MyStdout to the Module-Object.
* MyStdout is used to buffer the output (stdOut) of the wasm.
*/
let setupMyStdOut = function () {

let MyStdout = {}

// buffer contains the string since the last flush
MyStdout._buffer = "";

// contents contains the flushed string
MyStdout._contents = "";

// We setup emscripten later to call this function if stdout is called from c-Code
MyStdout.print = function (charCode) {
// is this a flush? see https://emscripten.org/docs/api_reference/Filesystem-API.html#FS.init
if (charCode == null) {
MyStdout._contents += MyStdout._buffer;
MyStdout._buffer = "";
return;
}
// this is not a flush
MyStdout._buffer += String.fromCharCode(charCode);
}

// returns the contents
MyStdout.getContents = function () {
return MyStdout._contents;
}

// clears the content
MyStdout.clear = function () {
MyStdout._contents = "";
}

// flushes the buffer to content
MyStdout.flush = function () {
MyStdout.print(null);
}

Module.MyStdout = MyStdout;

FS.init((charcode) => { Module.MyStdout.print(charcode) }, (charcode) => { Module.MyStdout.print(charcode) }, (charcode) => { Module.MyStdout.print(charcode) });
}

/**
* Add a function that wraps the main Method call
*/
let addRunMiniSATMethod = function () {
/**
* Run miniSats's Main-Method. options can be used to pass commandline arguments.
* Data should contain the contents of the file to be solved.
*/
Module.runMiniSAT = function (data, options) {
args = options || [];
data = data || "";

// clear what MiniSAT has printed (hopefully nothing) so far.
Module.MyStdout.clear();
// Load the data to Memory using emscriptens emulated filesystem
FS.writeFile('input', data);
// create a File to write the output to
FS.writeFile('output', "");
// the last argument for MiniSAT is the location of the file to be solved
args.push("/input");
args.push("/output");
// now we call main. All prints go to MyStdout
callMain(args);
let outputFileContents = FS.readFile("output", { encoding: 'utf8' });
// flush the buffer
Module.MyStdout.flush();
// we do not need the virtual file anymore. So we release it.
FS.unlink("input");
FS.unlink("output");
// Return what MiniSAT has printed during its run.
return [Module.MyStdout.getContents(), outputFileContents];
}
}

// Module.prerun should be an array of functions called before the module is initalized
if (!Module.preRun) {
Module.preRun = [];
}
Module.preRun.push(setupMyStdOut);
Module.preRun.push(addRunMiniSATMethod);
Module["noInitialRun"] = true;

0 comments on commit 23890a4

Please sign in to comment.