load the sink just in time

This commit is contained in:
Mathias Buus 2020-11-13 16:46:06 +01:00
parent 51093efbc5
commit ca32540960

View File

@ -1,21 +1,28 @@
/* eslint-disable camelcase */ /* eslint-disable camelcase */
var MessageChannel = global.MessageChannel
if (MessageChannel == null) ({ MessageChannel } = require('worker' + '_threads'))
function sodium_malloc (n) { function sodium_malloc (n) {
return new Uint8Array(n) return new Uint8Array(n)
} }
const sink = new MessageChannel()
function sodium_free (n) { function sodium_free (n) {
sodium_memzero(n) sodium_memzero(n)
sink.port1.postMessage(n.buffer, [n.buffer]) loadSink().port1.postMessage(n.buffer, [n.buffer])
} }
function sodium_memzero (arr) { function sodium_memzero (arr) {
arr.fill(0) arr.fill(0)
} }
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 = { module.exports = {
sodium_malloc, sodium_malloc,
sodium_free, sodium_free,