Skip to content

Commit d5ce47e

Browse files
committed
lib: deprecate the smalloc module
Upcoming V8 changes will make it impossible to keep supporting the smalloc module so deprecate it now and tell people to switch to typed arrays. The module is used in core in a few places so this commit makes the public module private and replaces the public part with wrappers that print deprecation notices. PR-URL: #1564 Reviewed-By: Domenic Denicola <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
1 parent 3c92ca2 commit d5ce47e

File tree

4 files changed

+111
-83
lines changed

4 files changed

+111
-83
lines changed

lib/internal/smalloc.js

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
'use strict';
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');
8+
9+
exports.alloc = alloc;
10+
exports.copyOnto = copyOnto;
11+
exports.dispose = dispose;
12+
exports.hasExternalData = hasExternalData;
13+
14+
// don't allow kMaxLength to accidentally be overwritten. it's a lot less
15+
// apparent when a primitive is accidentally changed.
16+
Object.defineProperty(exports, 'kMaxLength', {
17+
enumerable: true, value: kMaxLength, writable: false
18+
});
19+
20+
Object.defineProperty(exports, 'Types', {
21+
enumerable: true, value: Object.freeze(smalloc.types), writable: false
22+
});
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+
}

lib/smalloc.js

+18-82
Original file line numberDiff line numberDiff line change
@@ -1,91 +1,27 @@
11
'use strict';
22

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;
85

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');
1320

14-
// don't allow kMaxLength to accidentally be overwritten. it's a lot less
15-
// apparent when a primitive is accidentally changed.
1621
Object.defineProperty(exports, 'kMaxLength', {
17-
enumerable: true, value: kMaxLength, writable: false
22+
enumerable: true, value: smalloc.kMaxLength, writable: false
1823
});
1924

2025
Object.defineProperty(exports, 'Types', {
21-
enumerable: true, value: Object.freeze(smalloc.types), writable: false
26+
enumerable: true, value: Object.freeze(smalloc.Types), writable: false
2227
});
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-
}

lib/v8.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
'use strict';
1616

1717
const v8binding = process.binding('v8');
18-
const smalloc = require('smalloc');
18+
const smalloc = require('internal/smalloc');
1919

2020
const heapStatisticsBuffer =
2121
smalloc.alloc(v8binding.kHeapStatisticsBufferLength,

node.gyp

+1
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
'lib/zlib.js',
7272

7373
'lib/internal/freelist.js',
74+
'lib/internal/smalloc.js',
7475
],
7576
},
7677

0 commit comments

Comments
 (0)