forked from sipavlovic/wasm2js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wasm2js.cpp
44 lines (25 loc) · 773 Bytes
/
wasm2js.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
wasm2js
*/
#include <iostream>
#include <fstream>
#include "base64.hpp"
using namespace std;
string encode_wasm(string inputfilename) {
ifstream stream(inputfilename, ios::in | ios::binary);
vector<base64::byte> vec(
(istreambuf_iterator<char>(stream)),
istreambuf_iterator<char>());
return base64::encode(vec);
}
string wrap_js_code(string base64_wasm) {
return "const wasm_strbuffer = atob(\""+base64_wasm+"\");\nlet wasm_codearray = new Uint8Array(wasm_strbuffer.length);\nfor (var i in wasm_strbuffer) wasm_codearray[i] = wasm_strbuffer.charCodeAt(i);";
}
int main(int argc, char *argv[]) {
if (argc != 2) {
cout << "Usage: wasm2js <wasm file>" << endl;
return 0;
}
cout << wrap_js_code(encode_wasm(argv[1])) << endl;
return 0;
}