Skip to content

Commit

Permalink
squash! Fix the crash when hashing an async function
Browse files Browse the repository at this point in the history
Co-authored-by: Anna Henningsen <[email protected]>
  • Loading branch information
addaleax committed Feb 10, 2020
1 parent 7b95f78 commit 8705eec
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
14 changes: 12 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,18 @@ describe('hash', function() {

it('distinguishes async functions based on their properties', function() {
var a, b;

async function Foo() {}

var Foo;

try {
eval('async function Foo() {}; Foo');
} catch (err) {
if (err.name === 'SyntaxError')
return this.skip('Not available on Node 6');
else
throw err;
}

a = hash(Foo);

Foo.foo = 22;
Expand Down
18 changes: 12 additions & 6 deletions test/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@ var validSha1 = /^[0-9a-f]{40}$/i;
describe('hash()ing different types', function() {
it('hashes non-object types', function() {
var func = function(a){ return a + 1; };
var asyncFunc = async function(a){ return a + 1; };
var asyncFunc;
try {
asyncFunc = eval('async function(a) { return a + 1; }');
} catch (err) {
if (err.name === 'SyntaxError') asyncFunc = func;
else throw err;
}
assert.ok(validSha1.test(hash('Shazbot!')), 'hash string');
assert.ok(validSha1.test(hash(42)), 'hash number');
assert.ok(validSha1.test(hash(NaN)), 'hash bool');
Expand Down Expand Up @@ -36,7 +42,7 @@ describe('hash()ing different types', function() {
if (typeof process !== 'undefined') {
assert.ok(validSha1.test(hash(process)), 'hash process');
}

var timer = setTimeout(function() {}, 0);
assert.ok(validSha1.test(hash(timer)), 'hash timer');
});
Expand All @@ -55,7 +61,7 @@ describe('hash()ing different types', function() {

if (typeof Uint8Array !== 'undefined') {
it("Typed arrays can be hashed", function() {

assert.ok(validSha1.test(hash(new Uint8Array([1,2,3,4]))), 'hashes Uint8Array');
assert.ok(validSha1.test(hash(new Int8Array([1,2,3,4]))), 'hashes Int8Array');
assert.ok(validSha1.test(hash(new Uint16Array([1,2,3,4]))), 'hashes Uint16Array');
Expand Down Expand Up @@ -132,7 +138,7 @@ describe('hash()ing different types', function() {
// Self check; did we really hash all the types?
assert.equal(no, types.length);
});

it("Builtin types might result in identical hashes with respectFunctionNames = false", function() {
var hashcount = {};
var types = [Object, Date, Number, String, Function, RegExp,
Expand Down Expand Up @@ -161,14 +167,14 @@ describe('hash()ing different types', function() {
// Self check; did we really hash all the types?
assert.equal(no, types.length);
});

it("Functions with identical bodies and different names result in identical hashes with respectFunctionNames = false", function() {
var fn1 = function a() {};
var fn2 = function b() {};
var toStringDummy = function() { return '...'; };
fn1.toString = toStringDummy;
fn2.toString = toStringDummy;

var h1 = hash(fn1, { respectFunctionNames: false });
var h2 = hash(fn2, { respectFunctionNames: false });
assert.strictEqual(h1, h2);
Expand Down

0 comments on commit 8705eec

Please sign in to comment.