sodium-javascript/memory.js
Kasper Isager Dalsgarð 87ff2a56bd
Use globalThis instead of global (#61)
* Use `globalThis` instead of `global`

* Update `standard`
2021-11-02 16:36:15 +01:00

31 lines
550 B
JavaScript

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