Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
this.onmessage = function (evt) {
// message from UI thread
var msg = evt.data
switch (msg.purpose) {
// Message: Load new wasm module
case 'wasmModule':
// Instantiate the compiled module we were passed.
wasm = loader.instantiate(msg.wasmModule, importObj) // Throws
// Tell nBodySimulation.js we are ready
this.postMessage({ purpose: 'wasmReady' })
return
// Message: Given array of floats describing a system of bodies (x,y,x,mass),
// calculate the Grav forces to be applied to each body
case 'nBodyForces':
if (!wasm) throw new Error('wasm not initialized')
// Copy msg.arrBodies array into the wasm instance, increase GC count
const dataRef = wasm.__retain(wasm.__allocArray(wasm.FLOAT64ARRAY_ID, msg.arrBodies));
// Do the calculations in this thread synchronously
const resultRef = wasm.nBodyForces(dataRef);
// Copy result array from the wasm instance to our javascript runtime