Last active
January 3, 2020 10:36
-
-
Save oupo/0e1bdc12d0a2c1f2e2526965464af970 to your computer and use it in GitHub Desktop.
togasat-wasm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
emcc -std=c++11 main.cpp -O3 -s WASM=1 -s MODULARIZE=1 -s EXPORT_NAME=Togasat -s "EXPORTED_FUNCTIONS=['_solve', '_malloc', '_free']" -s "BINARYEN_METHOD='native-wasm'" -o togasat.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "togasat.hpp" | |
#include <emscripten/emscripten.h> | |
extern "C" { | |
EMSCRIPTEN_KEEPALIVE int solve(int *buf, int len) { | |
togasat::Solver solver; | |
std::vector<int> clause; | |
for (int i = 0; i < len; i ++) { | |
int val = buf[i]; | |
if (val == 0) { | |
solver.addClause(clause); | |
clause.clear(); | |
} else { | |
clause.push_back(val); | |
} | |
} | |
return solver.solve(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<title>togasat.js test</title> | |
</head> | |
<body> | |
<script src="togasat.js"></script> | |
<script> | |
Togasat().then(function(Module) { | |
let array = new Int32Array([1, 2, 0, -1, 0, -2, 0]); | |
let ptr = arrayToPtr(array); | |
console.log(Module._solve(ptr, array.length)); | |
Module._free(ptr); | |
function arrayToPtr(array) { | |
var ptr = Module._malloc(array.length * 4); | |
Module.HEAP32.set(array, ptr / 4); | |
return ptr; | |
} | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment