<commit_before>// Copyright 2014-2015 runtime.js project authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';
var VirtioDevice = require('./device');
var runtime = require('../../core');

function initializeRNGDevice(pciDevice) {
  var ioSpace = pciDevice.getBAR(0).resource;
  var irq = pciDevice.getIRQ();
  var allocator = runtime.allocator;

  var features = {};

  var dev = new VirtioDevice('rng', ioSpace, allocator);
  dev.setDriverAck();

  var driverFeatures = {};

  var deviceFeatures = dev.readDeviceFeatures(driverFeatures);

  if (!dev.writeGuestFeatures(features, driverFeatures, deviceFeatures)) {
    debug('[virtio] driver is unable to start');
    return;
  }

  var QUEUE_ID_REQ = 0;

  var reqQueue = dev.queueSetup(QUEUE_ID_REQ);

  function fillRequestQueue(length) {
    if (reqQueue.descriptorTable.descriptorsAvailable) {
      if (!reqQueue.placeBuffers([new Uint8Array(length || 1)], true)) {
        debug('[virtio rng] error placing descriptor buffers');
      }
    }

    if (reqQueue.isNotificationNeeded()) {
      dev.queueNotify(QUEUE_ID_REQ);
    }
  }

  runtime.driver.rng = {
    getRand: function(cb) {
      if (!cb) {
        cb = function() {};
      }

      fillRequestQueue(1);
      dev.hasPendingIRQ();
      reqQueue.fetchBuffers(cb);
    },
    getRandLength: function(length, cb) {
      if (typeof length === 'function') {
        cb = length;
        length = 1;
      }

      if (!cb) {
        cb = function() {};
      }

      fillRequestQueue(length);
      dev.hasPendingIRQ();
      reqQueue.fetchBuffers(cb);
    }
  };

  dev.setDriverReady();
}

module.exports = initializeRNGDevice;<commit_msg>Make functions sync, add randomBytes<commit_after>// Copyright 2014-2015 runtime.js project authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';
var VirtioDevice = require('./device');
var runtime = require('../../core');

function initializeRNGDevice(pciDevice) {
  var ioSpace = pciDevice.getBAR(0).resource;
  var irq = pciDevice.getIRQ();
  var allocator = runtime.allocator;

  var features = {};

  var dev = new VirtioDevice('rng', ioSpace, allocator);
  dev.setDriverAck();

  var driverFeatures = {};

  var deviceFeatures = dev.readDeviceFeatures(driverFeatures);

  if (!dev.writeGuestFeatures(features, driverFeatures, deviceFeatures)) {
    debug('[virtio] driver is unable to start');
    return;
  }

  var QUEUE_ID_REQ = 0;

  var reqQueue = dev.queueSetup(QUEUE_ID_REQ);

  function fillRequestQueue(length) {
    if (reqQueue.descriptorTable.descriptorsAvailable) {
      if (!reqQueue.placeBuffers([new Uint8Array(length || 1)], true)) {
        debug('[virtio rng] error placing descriptor buffers');
      }
    }

    if (reqQueue.isNotificationNeeded()) {
      dev.queueNotify(QUEUE_ID_REQ);
    }
  }

  runtime.driver.rng = {
    getRand: function() {
      if (!cb) {
        cb = function() {};
      }

      fillRequestQueue(1);
      dev.hasPendingIRQ();
      return reqQueue.getBuffer();
    },
    getRandLength: function(length) {
      fillRequestQueue(length || 1);
      dev.hasPendingIRQ();
      return reqQueue.getBuffer();
    },
    randomBytes: function(length) {
      var retbuf = null;
      length = length || 1;
      fillRequestQueue(length);
      dev.hasPendingIRQ();
      var u8 = reqQueue.getBuffer();
      retbuf = new Buffer(u8.length);
      for (let k in u8) {
        retbuf.writeUInt8(u8[k], k);
      }
      return retbuf;
    }
  };

  dev.setDriverReady();
}

module.exports = initializeRNGDevice;
{'fraction_non_alphanumeric': 0.12248908296943231, 'fraction_numerical': 0.009170305676855894, 'mean_word_length': 3.1085201793721975, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '776654', 'n_tokens_mistral': 1389, 'n_tokens_neox': 1335, 'n_words': 384}