sodium-javascript/memory.js

31 lines
546 B
JavaScript
Raw Normal View History

/* eslint-disable camelcase */
function sodium_malloc (n) {
return new Uint8Array(n)
}
function sodium_free (n) {
sodium_memzero(n)
2020-11-13 15:46:06 +00:00
loadSink().port1.postMessage(n.buffer, [n.buffer])
}
function sodium_memzero (arr) {
arr.fill(0)
}
2020-11-13 15:46:06 +00:00
var sink
function loadSink () {
if (sink) return sink
var MessageChannel = global.MessageChannel
if (MessageChannel == null) ({ MessageChannel } = require('worker' + '_threads'))
sink = new MessageChannel()
return sink
}
module.exports = {
sodium_malloc,
sodium_free,
sodium_memzero
}