2014-11-22 16:59:48 +01:00
|
|
|
'use strict';
|
|
|
|
|
2015-01-21 17:36:59 +01:00
|
|
|
const smalloc = process.binding('smalloc');
|
|
|
|
const kMaxLength = smalloc.kMaxLength;
|
2015-02-22 21:01:08 +01:00
|
|
|
const kMinType = smalloc.kMinType;
|
|
|
|
const kMaxType = smalloc.kMaxType;
|
2015-01-21 17:36:59 +01:00
|
|
|
const util = require('util');
|
2013-07-13 00:18:21 +02:00
|
|
|
|
|
|
|
exports.alloc = alloc;
|
2015-02-22 19:07:57 +01:00
|
|
|
exports.copyOnto = copyOnto;
|
2013-07-13 00:18:21 +02:00
|
|
|
exports.dispose = dispose;
|
2015-02-22 19:07:57 +01:00
|
|
|
exports.hasExternalData = hasExternalData;
|
2013-07-13 00:18:21 +02:00
|
|
|
|
|
|
|
// don't allow kMaxLength to accidentally be overwritten. it's a lot less
|
|
|
|
// apparent when a primitive is accidentally changed.
|
|
|
|
Object.defineProperty(exports, 'kMaxLength', {
|
2013-08-02 19:11:35 +02:00
|
|
|
enumerable: true, value: kMaxLength, writable: false
|
2013-07-13 00:18:21 +02:00
|
|
|
});
|
|
|
|
|
2013-07-20 01:15:25 +02:00
|
|
|
Object.defineProperty(exports, 'Types', {
|
2015-02-22 21:01:08 +01:00
|
|
|
enumerable: true, value: Object.freeze(smalloc.types), writable: false
|
2013-07-20 01:15:25 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// usage: obj = alloc(n[, obj][, type]);
|
|
|
|
function alloc(n, obj, type) {
|
2013-08-02 21:52:43 +02:00
|
|
|
n = n >>> 0;
|
2013-07-13 00:18:21 +02:00
|
|
|
|
2015-01-29 02:05:53 +01:00
|
|
|
if (obj === undefined)
|
2013-08-02 21:52:43 +02:00
|
|
|
obj = {};
|
2013-07-20 01:15:25 +02:00
|
|
|
|
2015-01-29 02:05:53 +01:00
|
|
|
if (typeof obj === 'number') {
|
2013-07-20 01:15:25 +02:00
|
|
|
type = obj >>> 0;
|
|
|
|
obj = {};
|
|
|
|
} else if (util.isPrimitive(obj)) {
|
2013-08-02 21:52:43 +02:00
|
|
|
throw new TypeError('obj must be an Object');
|
2013-07-20 01:15:25 +02:00
|
|
|
}
|
|
|
|
|
2015-02-22 19:07:57 +01:00
|
|
|
if (Array.isArray(obj))
|
|
|
|
throw new TypeError('obj cannot be an array');
|
|
|
|
if (obj instanceof Buffer)
|
|
|
|
throw new TypeError('obj cannot be a Buffer');
|
|
|
|
if (smalloc.isTypedArray(obj))
|
|
|
|
throw new TypeError('obj cannot be a typed array');
|
|
|
|
if (smalloc.hasExternalData(obj))
|
|
|
|
throw new TypeError('object already has external array data');
|
|
|
|
|
2015-02-22 21:01:08 +01:00
|
|
|
if (type < kMinType || type > kMaxType)
|
2013-07-20 01:15:25 +02:00
|
|
|
throw new TypeError('unknown external array type: ' + type);
|
|
|
|
if (n > kMaxLength)
|
2015-02-22 19:07:57 +01:00
|
|
|
throw new RangeError('Attempt to allocate array larger than maximum ' +
|
|
|
|
'size: 0x' + kMaxLength.toString(16) + ' elements');
|
2013-07-13 00:18:21 +02:00
|
|
|
|
2013-07-20 01:15:25 +02:00
|
|
|
return smalloc.alloc(obj, n, type);
|
2013-07-13 00:18:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function dispose(obj) {
|
2013-08-02 21:52:43 +02:00
|
|
|
if (util.isPrimitive(obj))
|
|
|
|
throw new TypeError('obj must be an Object');
|
2015-01-29 02:05:53 +01:00
|
|
|
if (obj instanceof Buffer)
|
2013-07-13 00:18:21 +02:00
|
|
|
throw new TypeError('obj cannot be a Buffer');
|
2014-11-18 10:30:27 +01:00
|
|
|
if (smalloc.isTypedArray(obj))
|
|
|
|
throw new TypeError('obj cannot be a typed array');
|
2014-10-31 23:15:12 +01:00
|
|
|
if (!smalloc.hasExternalData(obj))
|
2015-02-22 19:07:57 +01:00
|
|
|
throw new TypeError('obj has no external array data');
|
2013-08-02 21:52:43 +02:00
|
|
|
|
2013-07-13 00:18:21 +02:00
|
|
|
smalloc.dispose(obj);
|
|
|
|
}
|
2015-02-22 19:07:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
function copyOnto(source, sourceStart, dest, destStart, copyLength) {
|
|
|
|
if (util.isPrimitive(source))
|
|
|
|
throw new TypeError('source must be an Object');
|
|
|
|
if (util.isPrimitive(dest))
|
|
|
|
throw new TypeError('dest must be an Object');
|
|
|
|
if (!smalloc.hasExternalData(source))
|
|
|
|
throw new TypeError('source has no external array data');
|
|
|
|
if (!smalloc.hasExternalData(dest))
|
|
|
|
throw new TypeError('dest has no external array data');
|
|
|
|
|
|
|
|
return smalloc.copyOnto(source, sourceStart, dest, destStart, copyLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function hasExternalData(obj) {
|
|
|
|
if (util.isPrimitive(obj))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return smalloc.hasExternalData(obj);
|
|
|
|
}
|