1 // Copyright Joyent, Inc. and other Node contributors.
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
24 var smalloc = process.binding('smalloc');
25 var kMaxLength = smalloc.kMaxLength;
26 var util = require('util');
28 exports.alloc = alloc;
29 exports.copyOnto = smalloc.copyOnto;
30 exports.dispose = dispose;
31 exports.hasExternalData = smalloc.hasExternalData;
33 // don't allow kMaxLength to accidentally be overwritten. it's a lot less
34 // apparent when a primitive is accidentally changed.
35 Object.defineProperty(exports, 'kMaxLength', {
36 enumerable: true, value: kMaxLength, writable: false
39 // enumerated values for different external array types
42 // Must match enum v8::ExternalArrayType.
43 Object.defineProperties(Types, {
44 'Int8': { enumerable: true, value: 1, writable: false },
45 'Uint8': { enumerable: true, value: 2, writable: false },
46 'Int16': { enumerable: true, value: 3, writable: false },
47 'Uint16': { enumerable: true, value: 4, writable: false },
48 'Int32': { enumerable: true, value: 5, writable: false },
49 'Uint32': { enumerable: true, value: 6, writable: false },
50 'Float': { enumerable: true, value: 7, writable: false },
51 'Double': { enumerable: true, value: 8, writable: false },
52 'Uint8Clamped': { enumerable: true, value: 9, writable: false }
55 Object.defineProperty(exports, 'Types', {
56 enumerable: true, value: Types, writable: false
60 // usage: obj = alloc(n[, obj][, type]);
61 function alloc(n, obj, type) {
64 if (util.isUndefined(obj))
67 if (util.isNumber(obj)) {
70 } else if (util.isPrimitive(obj)) {
71 throw new TypeError('obj must be an Object');
74 // 1 == v8::kExternalUint8Array, 9 == v8::kExternalUint8ClampedArray
75 if (type < 1 || type > 9)
76 throw new TypeError('unknown external array type: ' + type);
77 if (util.isArray(obj))
78 throw new TypeError('Arrays are not supported');
80 throw new RangeError('n > kMaxLength');
82 return smalloc.alloc(obj, n, type);
86 function dispose(obj) {
87 if (util.isPrimitive(obj))
88 throw new TypeError('obj must be an Object');
89 if (util.isBuffer(obj))
90 throw new TypeError('obj cannot be a Buffer');