Skip to content

Instantly share code, notes, and snippets.

View bnjbvr's full-sized avatar
🌴

Benjamin Bouvier bnjbvr

🌴
View GitHub Profile
@bnjbvr
bnjbvr / wasm-shell.js
Created May 5, 2020 12:53
Run a wasm binary in Spidermonkey's shell
if (scriptArgs.length < 1) {
console.log('path to a wasm binary is required');
quit(-1);
}
let binary = os.file.readFile(scriptArgs[0], 'binary');
// If the wasm instance expects imported functions from js, put them in here.
// let maybeImports = { std: { f: function() { }} };
let maybeImports;
@bnjbvr
bnjbvr / compile.js
Created April 20, 2020 17:03
Spidermonkey script to compile a single wasm module
// Usage: $jsshell ./compile.js /path/to/wasm-binary.wasm
if (scriptArgs.length !== 1) {
console.log('one argument required: path to wasm binary');
}
let pathToBinary = scriptArgs[0];
let binary;
try {
@bnjbvr
bnjbvr / perf integration
Last active April 15, 2020 13:20
Misc spidermonkey commands
1. When running configure, pass the `--enable-perf` configuration option. Re-build the shell.
2. Run the shell with
```
IONPERF=func perf record $jsshell --no-wasm-multi-value --shared-memory=off --wasm-compiler=cranelift /path/to/script.js
```
An ExecutableAllocator is leaked on shutdown, which causes an assertion to
trigger: `Assertion failure: m_refCount == 1, at /home/ben/code/mozilla-inbound/js/src/jit/ExecutableAllocator.cpp:51`
@bnjbvr
bnjbvr / pubsub.rs
Last active May 16, 2017 14:53
server.rs
#[derive(Debug)]
pub enum Command {
Publish(String),
Retrieve
}
#[derive(Debug)]
pub enum Error {
VerbMissing,
UnknownVerb,
const wasmEvalText = (txt, maybeImports) => new WebAssembly.Instance(new WebAssembly.Module(wasmTextToBinary(txt)), maybeImports);
let module = new WebAssembly.Module(wasmTextToBinary(`
(module
(import "global" "func" (result i32))
(table (import "env" "table") 3 anyfunc)
(func (export "func_0") (result i32)
call 0 ;; calls the import, which is func #0
)
(memory (export "mem") 10)
### Keybase proof
I hereby claim:
* I am bnjbvr on github.
* I am bnjbvr (https://keybase.io/bnjbvr) on keybase.
* I have a public key whose fingerprint is FB2E 08CF ADFF 7CE0 6067 A3A2 4864 9898 2667 C10E
To claim this, I am signing this object:
@bnjbvr
bnjbvr / gist:a54828e75328d4922d89
Created March 27, 2015 19:55
Transform a (err, ..., callback) based function into a Promise-based function
function promisify(func) {
return function(...args) {
return new Promise(function(resolve, reject) {
func(...args, function(err, ...rest) {
if (err)
return reject(err);
resolve(...rest);
});
});
}
static bool add (JSContext *cx, unsigned argc, jsval *vp){
JS::CallArgs args = CallArgsFromVp(argc, vp);
std::cout<<(args[0].toInt32()+args[1].toInt32());
RootedFunction func(cx, JS_ValueToFunction(cx, args[2]));
// JSFunction* function;
JS::RootedValue rval(cx);
JS::RootedObject global(cx, JS::CurrentGlobalOrNull(cx));
JS_CallFunction(cx, global, &func, 0, nullptr, &rval);
return true;
}
@bnjbvr
bnjbvr / gist:e268b2eba1f6dc32b4ec
Last active August 29, 2015 14:13 — forked from padenot/gist:64d001ee1e396794331c
SIMD example for padenot
if (window.SIMD) {
// yay, SIMD available, let's do a little stereo mixdown routine
for (var i = 0; i < bufsize; i+=4) {
var lhs = SIMD.float32x4(left[i+0], left[i+1],
left[i+2], left[i+3]),
rhs = SIMD.float32x4(right[i+0], right[i+1],
right[i+2], right[i+3]);
var mixdown = SIMD.float32x4.add(lhs, rhs);
SIMD.float32x4.store(center, i, mixdown);
}
@bnjbvr
bnjbvr / gist:6750933
Last active December 24, 2015 05:29
Proxy all Math calls
var proxy = {
get: function(target, name, receiver) {
var original = target[name];
return function() {
// for instance, print all arguments
for(var i = 0; i<arguments.length;++i)
print(arguments[i]);
return original.apply(receiver, arguments)
}