Skip to content

Instantly share code, notes, and snippets.

@oupo
Last active January 3, 2020 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oupo/0e1bdc12d0a2c1f2e2526965464af970 to your computer and use it in GitHub Desktop.
Save oupo/0e1bdc12d0a2c1f2e2526965464af970 to your computer and use it in GitHub Desktop.
togasat-wasm
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
#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();
}
}
<!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