|
1 | 1 | 'use strict'; |
2 | 2 |
|
3 | | -const smalloc = process.binding('smalloc'); |
4 | | -const kMaxLength = smalloc.kMaxLength; |
5 | | -const kMinType = smalloc.kMinType; |
6 | | -const kMaxType = smalloc.kMaxType; |
7 | | -const util = require('util'); |
| 3 | +const smalloc = require('internal/smalloc'); |
| 4 | +const deprecate = require('util').deprecate; |
8 | 5 |
|
9 | | -exports.alloc = alloc; |
10 | | -exports.copyOnto = copyOnto; |
11 | | -exports.dispose = dispose; |
12 | | -exports.hasExternalData = hasExternalData; |
| 6 | +exports.alloc = |
| 7 | + deprecate(smalloc.alloc, 'smalloc.alloc: Deprecated, use typed arrays'); |
| 8 | + |
| 9 | +exports.copyOnto = |
| 10 | + deprecate(smalloc.copyOnto, |
| 11 | + 'smalloc.copyOnto: Deprecated, use typed arrays'); |
| 12 | + |
| 13 | +exports.dispose = |
| 14 | + deprecate(smalloc.dispose, |
| 15 | + 'smalloc.dispose: Deprecated, use typed arrays'); |
| 16 | + |
| 17 | +exports.hasExternalData = |
| 18 | + deprecate(smalloc.hasExternalData, |
| 19 | + 'smalloc.hasExternalData: Deprecated, use typed arrays'); |
13 | 20 |
|
14 | | -// don't allow kMaxLength to accidentally be overwritten. it's a lot less |
15 | | -// apparent when a primitive is accidentally changed. |
16 | 21 | Object.defineProperty(exports, 'kMaxLength', { |
17 | | - enumerable: true, value: kMaxLength, writable: false |
| 22 | + enumerable: true, value: smalloc.kMaxLength, writable: false |
18 | 23 | }); |
19 | 24 |
|
20 | 25 | Object.defineProperty(exports, 'Types', { |
21 | | - enumerable: true, value: Object.freeze(smalloc.types), writable: false |
| 26 | + enumerable: true, value: Object.freeze(smalloc.Types), writable: false |
22 | 27 | }); |
23 | | - |
24 | | - |
25 | | -// usage: obj = alloc(n[, obj][, type]); |
26 | | -function alloc(n, obj, type) { |
27 | | - n = n >>> 0; |
28 | | - |
29 | | - if (obj === undefined) |
30 | | - obj = {}; |
31 | | - |
32 | | - if (typeof obj === 'number') { |
33 | | - type = obj >>> 0; |
34 | | - obj = {}; |
35 | | - } else if (util.isPrimitive(obj)) { |
36 | | - throw new TypeError('obj must be an Object'); |
37 | | - } |
38 | | - |
39 | | - if (Array.isArray(obj)) |
40 | | - throw new TypeError('obj cannot be an array'); |
41 | | - if (obj instanceof Buffer) |
42 | | - throw new TypeError('obj cannot be a Buffer'); |
43 | | - if (smalloc.isTypedArray(obj)) |
44 | | - throw new TypeError('obj cannot be a typed array'); |
45 | | - if (smalloc.hasExternalData(obj)) |
46 | | - throw new TypeError('object already has external array data'); |
47 | | - |
48 | | - if (type < kMinType || type > kMaxType) |
49 | | - throw new TypeError('unknown external array type: ' + type); |
50 | | - if (n > kMaxLength) |
51 | | - throw new RangeError('Attempt to allocate array larger than maximum ' + |
52 | | - 'size: 0x' + kMaxLength.toString(16) + ' elements'); |
53 | | - |
54 | | - return smalloc.alloc(obj, n, type); |
55 | | -} |
56 | | - |
57 | | - |
58 | | -function dispose(obj) { |
59 | | - if (util.isPrimitive(obj)) |
60 | | - throw new TypeError('obj must be an Object'); |
61 | | - if (obj instanceof Buffer) |
62 | | - throw new TypeError('obj cannot be a Buffer'); |
63 | | - if (smalloc.isTypedArray(obj)) |
64 | | - throw new TypeError('obj cannot be a typed array'); |
65 | | - if (!smalloc.hasExternalData(obj)) |
66 | | - throw new TypeError('obj has no external array data'); |
67 | | - |
68 | | - smalloc.dispose(obj); |
69 | | -} |
70 | | - |
71 | | - |
72 | | -function copyOnto(source, sourceStart, dest, destStart, copyLength) { |
73 | | - if (util.isPrimitive(source)) |
74 | | - throw new TypeError('source must be an Object'); |
75 | | - if (util.isPrimitive(dest)) |
76 | | - throw new TypeError('dest must be an Object'); |
77 | | - if (!smalloc.hasExternalData(source)) |
78 | | - throw new TypeError('source has no external array data'); |
79 | | - if (!smalloc.hasExternalData(dest)) |
80 | | - throw new TypeError('dest has no external array data'); |
81 | | - |
82 | | - return smalloc.copyOnto(source, sourceStart, dest, destStart, copyLength); |
83 | | -} |
84 | | - |
85 | | - |
86 | | -function hasExternalData(obj) { |
87 | | - if (util.isPrimitive(obj)) |
88 | | - return false; |
89 | | - |
90 | | - return smalloc.hasExternalData(obj); |
91 | | -} |
0 commit comments