-
Notifications
You must be signed in to change notification settings - Fork 30.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
lib: deprecate the smalloc module #1564
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
'use strict'; | ||
|
||
const smalloc = process.binding('smalloc'); | ||
const kMaxLength = smalloc.kMaxLength; | ||
const kMinType = smalloc.kMinType; | ||
const kMaxType = smalloc.kMaxType; | ||
const util = require('util'); | ||
|
||
exports.alloc = alloc; | ||
exports.copyOnto = copyOnto; | ||
exports.dispose = dispose; | ||
exports.hasExternalData = hasExternalData; | ||
|
||
// don't allow kMaxLength to accidentally be overwritten. it's a lot less | ||
// apparent when a primitive is accidentally changed. | ||
Object.defineProperty(exports, 'kMaxLength', { | ||
enumerable: true, value: kMaxLength, writable: false | ||
}); | ||
|
||
Object.defineProperty(exports, 'Types', { | ||
enumerable: true, value: Object.freeze(smalloc.types), writable: false | ||
}); | ||
|
||
|
||
// usage: obj = alloc(n[, obj][, type]); | ||
function alloc(n, obj, type) { | ||
n = n >>> 0; | ||
|
||
if (obj === undefined) | ||
obj = {}; | ||
|
||
if (typeof obj === 'number') { | ||
type = obj >>> 0; | ||
obj = {}; | ||
} else if (util.isPrimitive(obj)) { | ||
throw new TypeError('obj must be an Object'); | ||
} | ||
|
||
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'); | ||
|
||
if (type < kMinType || type > kMaxType) | ||
throw new TypeError('unknown external array type: ' + type); | ||
if (n > kMaxLength) | ||
throw new RangeError('Attempt to allocate array larger than maximum ' + | ||
'size: 0x' + kMaxLength.toString(16) + ' elements'); | ||
|
||
return smalloc.alloc(obj, n, type); | ||
} | ||
|
||
|
||
function dispose(obj) { | ||
if (util.isPrimitive(obj)) | ||
throw new TypeError('obj must be an Object'); | ||
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('obj has no external array data'); | ||
|
||
smalloc.dispose(obj); | ||
} | ||
|
||
|
||
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,91 +1,27 @@ | ||
'use strict'; | ||
|
||
const smalloc = process.binding('smalloc'); | ||
const kMaxLength = smalloc.kMaxLength; | ||
const kMinType = smalloc.kMinType; | ||
const kMaxType = smalloc.kMaxType; | ||
const util = require('util'); | ||
const smalloc = require('internal/smalloc'); | ||
const deprecate = require('util').deprecate; | ||
|
||
exports.alloc = alloc; | ||
exports.copyOnto = copyOnto; | ||
exports.dispose = dispose; | ||
exports.hasExternalData = hasExternalData; | ||
exports.alloc = | ||
deprecate(smalloc.alloc, 'smalloc.alloc: Deprecated, use typed arrays'); | ||
|
||
exports.copyOnto = | ||
deprecate(smalloc.copyOnto, | ||
'smalloc.copyOnto: Deprecated, use typed arrays'); | ||
|
||
exports.dispose = | ||
deprecate(smalloc.dispose, | ||
'smalloc.dispose: Deprecated, use typed arrays'); | ||
|
||
exports.hasExternalData = | ||
deprecate(smalloc.hasExternalData, | ||
'smalloc.hasExternalData: Deprecated, use typed arrays'); | ||
|
||
// don't allow kMaxLength to accidentally be overwritten. it's a lot less | ||
// apparent when a primitive is accidentally changed. | ||
Object.defineProperty(exports, 'kMaxLength', { | ||
enumerable: true, value: kMaxLength, writable: false | ||
enumerable: true, value: smalloc.kMaxLength, writable: false | ||
}); | ||
|
||
Object.defineProperty(exports, 'Types', { | ||
enumerable: true, value: Object.freeze(smalloc.types), writable: false | ||
enumerable: true, value: Object.freeze(smalloc.Types), writable: false | ||
}); | ||
|
||
|
||
// usage: obj = alloc(n[, obj][, type]); | ||
function alloc(n, obj, type) { | ||
n = n >>> 0; | ||
|
||
if (obj === undefined) | ||
obj = {}; | ||
|
||
if (typeof obj === 'number') { | ||
type = obj >>> 0; | ||
obj = {}; | ||
} else if (util.isPrimitive(obj)) { | ||
throw new TypeError('obj must be an Object'); | ||
} | ||
|
||
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'); | ||
|
||
if (type < kMinType || type > kMaxType) | ||
throw new TypeError('unknown external array type: ' + type); | ||
if (n > kMaxLength) | ||
throw new RangeError('Attempt to allocate array larger than maximum ' + | ||
'size: 0x' + kMaxLength.toString(16) + ' elements'); | ||
|
||
return smalloc.alloc(obj, n, type); | ||
} | ||
|
||
|
||
function dispose(obj) { | ||
if (util.isPrimitive(obj)) | ||
throw new TypeError('obj must be an Object'); | ||
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('obj has no external array data'); | ||
|
||
smalloc.dispose(obj); | ||
} | ||
|
||
|
||
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,6 +71,7 @@ | |
'lib/zlib.js', | ||
|
||
'lib/internal/freelist.js', | ||
'lib/internal/smalloc.js', | ||
], | ||
}, | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems suspicious? But I don't really know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
smalloc
here is fromprocess.binding()
. So it was being exported as lower case. Must have been something I missed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's because I moved the file, then added it again. In the original,
.types
comes from the binding object and is exported assmalloc.Types
. Here, I'm re-exporting the latter.