Skip to content

Commit

Permalink
fix: new javascript wrapper for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jaromil committed Nov 6, 2024
1 parent 7cac502 commit 30fced0
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 14 deletions.
6 changes: 2 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,9 @@ check: ## Run tests using the current binary executable build (meson/ninja)
ninja -C meson test

check-js: ## Run tests using a wasm build for nodejs
yarn --cwd bindings/javascript
yarn --cwd bindings/javascript build
yarn --cwd bindings/javascript test
@echo "#!/bin/sh\nnode test/zenroom_exec.js src/zenroom.js \$$@\n" \
> test/zenroom && chmod +x test/zenroom
@sed 's@=ROOT=@'"${pwd}"'@' test/zexe_js_wrapper.sh > zenroom
@chmod +x zenroom

install: destbin=${DESTDIR}${PREFIX}/bin
install: destdocs=${DESTDIR}${PREFIX}/share/zenroom
Expand Down
40 changes: 40 additions & 0 deletions test/zencode_exec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const fs = require('fs')
const util = require('util')

const C = require(process.argv[2])() // 1st arg is path to WASM zenroom.js


const zencodeExec = function(script, conf = null, keys = null, data = null) {
C.then(function(Module){
Module.exec_ok = () => 0
Module.exec_error = () => 0
Module.print = text => console.log(text)
Module.printErr = text => console.error(text)
Module.ccall(
'zencode_exec',
'number',
['string', 'string', 'string', 'string', 'number'],
[script, conf, keys, data]
)
})
}
const zencode = function(script_file, conf_string=null, keys_file=null, data_file=null) {
const enc = { encoding: 'utf8' }
const script = fs.readFileSync(script_file, enc)
const conf = conf_string
const keys = (keys_file) ? fs.readFileSync(keys_file, enc) : null
const data = (data_file) ? fs.readFileSync(data_file, enc) : null
return zencodeExec(script, conf, keys, data)
}

// console.log("[JS] zenroom %s %s %s %s",
// process.argv[3], // script file
// process.argv[4], // conf string
// process.argv[5], // keys file
// process.argv[6], // data file
// )

// console.time(process.argv[3])
zencode(process.argv[3],process.argv[4],process.argv[5],process.argv[6])
// console.timeEnd(process.argv[3])
// console.log("@", "=".repeat(40), "@\n")
21 changes: 11 additions & 10 deletions test/zenroom_exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ const util = require('util')
// but it is not recommended as stated in https://github.com/kripken/emscripten/issues/2537
// fs.writeFileSync('zenroom.js.mem', fs.readFileSync('src/zenroom.js.mem'));

const C = require(process.argv[2])()
const C = require(process.argv[2])() // 1st arg is path to WASM zenroom.js


const zenroomExec = function(script, conf = null, keys = null, data = null) {
C.then(function(Module){
Module.exec_ok = () => 0
Module.exec_error = () => 0
Module.print = text => console.log(text)
Module.printErr = text => console.log(text)
Module.printErr = text => console.error(text)
Module.ccall(
'zenroom_exec',
'number',
Expand All @@ -21,22 +21,23 @@ const zenroomExec = function(script, conf = null, keys = null, data = null) {
)
})
}

const zenroom = function(script_file, conf_file=null, keys_file=null, data_file=process.argv[4]) {
const zenroom = function(script_file, conf_string=null, keys_file=null, data_file=null) {
const enc = { encoding: 'utf8' }
const script = fs.readFileSync(script_file, enc)
const conf = (conf_file) ? fs.readFileSync(conf_file, enc) : null
const conf = conf_string
const keys = (keys_file) ? fs.readFileSync(keys_file, enc) : null
const data = (data_file) ? fs.readFileSync(data_file, enc) : null

return zenroomExec(script, conf, keys, data)
}

console.log("[JS] zenroom_exec %s %s",
process.argv[2],
process.argv[3])
console.log("[JS] zenroom %s %s %s %s",
process.argv[3], // script file
process.argv[4], // conf string
process.argv[5], // keys file
process.argv[6], // data file
)

console.time(process.argv[3])
zenroom(process.argv[3])
zenroom(process.argv[3],process.argv[4],process.argv[5],process.argv[6])
console.timeEnd(process.argv[3])
console.log("@", "=".repeat(40), "@\n")
26 changes: 26 additions & 0 deletions test/zexe_js_wrapper.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash

# template: %PATH% has to be subsituted with absolute path
# this is processed by check-js to create a zenroom wrapper

conf=${conf:-"logfmt=text,debug=0"}

while getopts "zc:k:a:" arg; do
case $arg in
z) ;; # autodetected from script name
k) keys="${OPTARG}" ;;
a) data="${OPTARG}" ;;
c) conf="${OPTARG},logfmt=text,debug=0" ;;
esac
done
shift $((OPTIND-1))

if [[ $1 == *.lua ]]; then
node =ROOT=/test/zenroom_exec.js \
=ROOT=/bindings/javascript/dist/main/zenroom.js $1 $conf $keys $data
elif [[ $1 == *.zen ]]; then
node =ROOT=/test/zencode_exec.js \
=ROOT=/bindings/javascript/dist/main/zenroom.js $1 $conf $keys $data
else
echo "Unsupported script: $1"
fi

0 comments on commit 30fced0

Please sign in to comment.