Skip to content

Commit f6b842d

Browse files
committed
chore: move the exec js from CommonJS to ESM
1 parent eeb9abe commit f6b842d

2 files changed

Lines changed: 77 additions & 71 deletions

File tree

test/zencode_exec.js

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
1-
const fs = require('fs')
2-
const util = require('util')
1+
import fs from "fs";
2+
import { pathToFileURL } from "url";
33

4-
const C = require(process.argv[2])() // 1st arg is path to WASM zenroom.js
4+
// Read CLI arguments
5+
const [, , wasmPath, scriptFile, confString, keysFile, dataFile] = process.argv;
56

7+
// Dynamically import the Emscripten-generated module (with `-sEXPORT_ES6=1 -sMODULARIZE=1`)
8+
const C = (await import(pathToFileURL(wasmPath).href)).default();
69

7-
const zencodeExec = function(script, conf = null, keys = null, data = null) {
8-
C.then(function(Module){
9-
Module.exec_ok = () => 0
10-
Module.exec_error = () => 0
11-
Module.print = text => console.log(text)
12-
Module.printErr = text => console.error(text)
10+
const zencodeExec = (script, conf = null, keys = null, data = null) => {
11+
C.then((Module) => {
12+
Module.exec_ok = () => 0;
13+
Module.exec_error = () => 0;
14+
Module.print = (text) => console.log(text);
15+
Module.printErr = (text) => console.error(text);
1316
Module.ccall(
14-
'zencode_exec',
15-
'number',
16-
['string', 'string', 'string', 'string', 'number'],
17-
[script, conf, keys, data]
18-
)
19-
})
20-
}
21-
const zencode = function(script_file, conf_string=null, keys_file=null, data_file=null) {
22-
const enc = { encoding: 'utf8' }
23-
const script = fs.readFileSync(script_file, enc)
24-
const conf = conf_string
25-
const keys = (keys_file) ? fs.readFileSync(keys_file, enc) : null
26-
const data = (data_file) ? fs.readFileSync(data_file, enc) : null
27-
return zencodeExec(script, conf, keys, data)
28-
}
17+
"zencode_exec",
18+
"number",
19+
["string", "string", "string", "string", "number"],
20+
[script, conf, keys, data],
21+
);
22+
});
23+
};
2924

30-
// console.log("[JS] zenroom %s %s %s %s",
31-
// process.argv[3], // script file
32-
// process.argv[4], // conf string
33-
// process.argv[5], // keys file
34-
// process.argv[6], // data file
35-
// )
25+
const zencode = (
26+
script_file,
27+
conf_string = null,
28+
keys_file = null,
29+
data_file = null,
30+
) => {
31+
const enc = { encoding: "utf8" };
32+
const script = fs.readFileSync(script_file, enc);
33+
const conf = conf_string;
34+
const keys = keys_file ? fs.readFileSync(keys_file, enc) : null;
35+
const data = data_file ? fs.readFileSync(data_file, enc) : null;
36+
return zencodeExec(script, conf, keys, data);
37+
};
3638

37-
// console.time(process.argv[3])
38-
zencode(process.argv[3],process.argv[4],process.argv[5],process.argv[6])
39-
// console.timeEnd(process.argv[3])
40-
// console.log("@", "=".repeat(40), "@\n")
39+
zencode(scriptFile, confString, keysFile, dataFile);

test/zenroom_exec.js

Lines changed: 44 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,50 @@
1-
const fs = require('fs')
2-
const util = require('util')
3-
// you have to copy the js.mem file in the CWD as per design or --memory-init-file 0
4-
// but it is not recommended as stated in https://github.com/kripken/emscripten/issues/2537
5-
// fs.writeFileSync('zenroom.js.mem', fs.readFileSync('src/zenroom.js.mem'));
1+
import fs from "fs";
2+
import { pathToFileURL } from "url";
63

7-
const C = require(process.argv[2])() // 1st arg is path to WASM zenroom.js
4+
// Read CLI arguments
5+
const [, , wasmPath, scriptFile, confString, keysFile, dataFile] = process.argv;
86

7+
// Dynamically import the Emscripten-generated module (built with `-sMODULARIZE=1 -sEXPORT_ES6=1`)
8+
const C = (await import(pathToFileURL(wasmPath).href)).default();
99

10-
const zenroomExec = function(script, conf = null, keys = null, data = null) {
11-
C.then(function(Module){
12-
Module.exec_ok = () => 0
13-
Module.exec_error = () => 0
14-
Module.print = text => console.log(text)
15-
Module.printErr = text => console.error(text)
10+
const zenroomExec = (script, conf = null, keys = null, data = null) => {
11+
C.then((Module) => {
12+
Module.exec_ok = () => 0;
13+
Module.exec_error = () => 0;
14+
Module.print = (text) => console.log(text);
15+
Module.printErr = (text) => console.error(text);
1616
Module.ccall(
17-
'zenroom_exec',
18-
'number',
19-
['string', 'string', 'string', 'string', 'number'],
20-
[script, conf, keys, data]
21-
)
22-
})
23-
}
24-
const zenroom = function(script_file, conf_string=null, keys_file=null, data_file=null) {
25-
const enc = { encoding: 'utf8' }
26-
const script = fs.readFileSync(script_file, enc)
27-
const conf = conf_string
28-
const keys = (keys_file) ? fs.readFileSync(keys_file, enc) : null
29-
const data = (data_file) ? fs.readFileSync(data_file, enc) : null
30-
return zenroomExec(script, conf, keys, data)
31-
}
17+
"zenroom_exec",
18+
"number",
19+
["string", "string", "string", "string", "number"],
20+
[script, conf, keys, data],
21+
);
22+
});
23+
};
3224

33-
console.log("[JS] zenroom %s %s %s %s",
34-
process.argv[3], // script file
35-
process.argv[4], // conf string
36-
process.argv[5], // keys file
37-
process.argv[6], // data file
38-
)
25+
const zenroom = (
26+
script_file,
27+
conf_string = null,
28+
keys_file = null,
29+
data_file = null,
30+
) => {
31+
const enc = { encoding: "utf8" };
32+
const script = fs.readFileSync(script_file, enc);
33+
const conf = conf_string;
34+
const keys = keys_file ? fs.readFileSync(keys_file, enc) : null;
35+
const data = data_file ? fs.readFileSync(data_file, enc) : null;
36+
return zenroomExec(script, conf, keys, data);
37+
};
3938

40-
console.time(process.argv[3])
41-
zenroom(process.argv[3],process.argv[4],process.argv[5],process.argv[6])
42-
console.timeEnd(process.argv[3])
43-
console.log("@", "=".repeat(40), "@\n")
39+
console.log(
40+
"[JS] zenroom %s %s %s %s",
41+
scriptFile,
42+
confString,
43+
keysFile,
44+
dataFile,
45+
);
46+
47+
console.time(scriptFile);
48+
zenroom(scriptFile, confString, keysFile, dataFile);
49+
console.timeEnd(scriptFile);
50+
console.log("@", "=".repeat(40), "@\n");

0 commit comments

Comments
 (0)