-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix]
ES5+
: Use spec‑accurate IsCallable
and IsConstructor
impls
Co-authored-by: Jordan Harband <[email protected]>
- Loading branch information
Showing
14 changed files
with
289 additions
and
24 deletions.
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 |
---|---|---|
@@ -1,5 +1,14 @@ | ||
'use strict'; | ||
|
||
var hasSymbols = require('has-symbols')(); | ||
var callBound = require('../helpers/callBound.js'); | ||
|
||
var $toStr = callBound('%Object.prototype.toString%'); | ||
|
||
// http://www.ecma-international.org/ecma-262/5.1/#sec-9.11 | ||
|
||
module.exports = require('is-callable'); | ||
module.exports = function IsCallable(argument) { | ||
// some older engines say that typeof /abc/ === 'function', | ||
return typeof argument === 'function' | ||
&& (hasSymbols || $toStr(argument) !== '[object RegExp]'); | ||
}; |
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,7 +1,40 @@ | ||
'use strict'; | ||
|
||
var GetIntrinsic = require('../GetIntrinsic.js'); | ||
|
||
var $construct = GetIntrinsic('%Reflect.construct%', true); | ||
|
||
var DefinePropertyOrThrow = require('./DefinePropertyOrThrow'); | ||
try { | ||
DefinePropertyOrThrow({}, '', { '[[Get]]': function () {} }); | ||
} catch (e) { | ||
// Accessor properties aren't supported | ||
DefinePropertyOrThrow = null; | ||
} | ||
|
||
// https://www.ecma-international.org/ecma-262/6.0/#sec-isconstructor | ||
|
||
module.exports = function IsConstructor(argument) { | ||
return typeof argument === 'function' && !!argument.prototype; // unfortunately there's no way to truly check this without try/catch `new argument` | ||
}; | ||
if (DefinePropertyOrThrow && $construct) { | ||
var isConstructorMarker = {}; | ||
var badArrayLike = {}; | ||
DefinePropertyOrThrow(badArrayLike, 'length', { | ||
'[[Get]]': function () { | ||
throw isConstructorMarker; | ||
}, | ||
'[[Enumerable]]': true | ||
}); | ||
|
||
module.exports = function IsConstructor(argument) { | ||
try { | ||
// `Reflect.construct` invokes `IsConstructor(target)` before `Get(args, 'length')`: | ||
$construct(argument, badArrayLike); | ||
} catch (err) { | ||
return err === isConstructorMarker; | ||
} | ||
}; | ||
} else { | ||
module.exports = function IsConstructor(argument) { | ||
// unfortunately there's no way to truly check this without try/catch `new argument` in old environments | ||
return typeof argument === 'function' && !!argument.prototype; | ||
}; | ||
} |
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,5 +1,14 @@ | ||
'use strict'; | ||
|
||
var hasSymbols = require('has-symbols')(); | ||
var callBound = require('../helpers/callBound.js'); | ||
|
||
var $toStr = callBound('%Object.prototype.toString%'); | ||
|
||
// http://www.ecma-international.org/ecma-262/5.1/#sec-9.11 | ||
|
||
module.exports = require('is-callable'); | ||
module.exports = function IsCallable(argument) { | ||
// some older engines say that typeof /abc/ === 'function', | ||
return typeof argument === 'function' | ||
&& (hasSymbols || $toStr(argument) !== '[object RegExp]'); | ||
}; |
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,7 +1,40 @@ | ||
'use strict'; | ||
|
||
var GetIntrinsic = require('../GetIntrinsic.js'); | ||
|
||
var $construct = GetIntrinsic('%Reflect.construct%', true); | ||
|
||
var DefinePropertyOrThrow = require('./DefinePropertyOrThrow'); | ||
try { | ||
DefinePropertyOrThrow({}, '', { '[[Get]]': function () {} }); | ||
} catch (e) { | ||
// Accessor properties aren't supported | ||
DefinePropertyOrThrow = null; | ||
} | ||
|
||
// https://www.ecma-international.org/ecma-262/6.0/#sec-isconstructor | ||
|
||
module.exports = function IsConstructor(argument) { | ||
return typeof argument === 'function' && !!argument.prototype; // unfortunately there's no way to truly check this without try/catch `new argument` | ||
}; | ||
if (DefinePropertyOrThrow && $construct) { | ||
var isConstructorMarker = {}; | ||
var badArrayLike = {}; | ||
DefinePropertyOrThrow(badArrayLike, 'length', { | ||
'[[Get]]': function () { | ||
throw isConstructorMarker; | ||
}, | ||
'[[Enumerable]]': true | ||
}); | ||
|
||
module.exports = function IsConstructor(argument) { | ||
try { | ||
// `Reflect.construct` invokes `IsConstructor(target)` before `Get(args, 'length')`: | ||
$construct(argument, badArrayLike); | ||
} catch (err) { | ||
return err === isConstructorMarker; | ||
} | ||
}; | ||
} else { | ||
module.exports = function IsConstructor(argument) { | ||
// unfortunately there's no way to truly check this without try/catch `new argument` in old environments | ||
return typeof argument === 'function' && !!argument.prototype; | ||
}; | ||
} |
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,5 +1,14 @@ | ||
'use strict'; | ||
|
||
var hasSymbols = require('has-symbols')(); | ||
var callBound = require('../helpers/callBound.js'); | ||
|
||
var $toStr = callBound('%Object.prototype.toString%'); | ||
|
||
// http://www.ecma-international.org/ecma-262/5.1/#sec-9.11 | ||
|
||
module.exports = require('is-callable'); | ||
module.exports = function IsCallable(argument) { | ||
// some older engines say that typeof /abc/ === 'function', | ||
return typeof argument === 'function' | ||
&& (hasSymbols || $toStr(argument) !== '[object RegExp]'); | ||
}; |
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,7 +1,40 @@ | ||
'use strict'; | ||
|
||
var GetIntrinsic = require('../GetIntrinsic.js'); | ||
|
||
var $construct = GetIntrinsic('%Reflect.construct%', true); | ||
|
||
var DefinePropertyOrThrow = require('./DefinePropertyOrThrow'); | ||
try { | ||
DefinePropertyOrThrow({}, '', { '[[Get]]': function () {} }); | ||
} catch (e) { | ||
// Accessor properties aren't supported | ||
DefinePropertyOrThrow = null; | ||
} | ||
|
||
// https://www.ecma-international.org/ecma-262/6.0/#sec-isconstructor | ||
|
||
module.exports = function IsConstructor(argument) { | ||
return typeof argument === 'function' && !!argument.prototype; // unfortunately there's no way to truly check this without try/catch `new argument` | ||
}; | ||
if (DefinePropertyOrThrow && $construct) { | ||
var isConstructorMarker = {}; | ||
var badArrayLike = {}; | ||
DefinePropertyOrThrow(badArrayLike, 'length', { | ||
'[[Get]]': function () { | ||
throw isConstructorMarker; | ||
}, | ||
'[[Enumerable]]': true | ||
}); | ||
|
||
module.exports = function IsConstructor(argument) { | ||
try { | ||
// `Reflect.construct` invokes `IsConstructor(target)` before `Get(args, 'length')`: | ||
$construct(argument, badArrayLike); | ||
} catch (err) { | ||
return err === isConstructorMarker; | ||
} | ||
}; | ||
} else { | ||
module.exports = function IsConstructor(argument) { | ||
// unfortunately there's no way to truly check this without try/catch `new argument` in old environments | ||
return typeof argument === 'function' && !!argument.prototype; | ||
}; | ||
} |
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,5 +1,14 @@ | ||
'use strict'; | ||
|
||
var hasSymbols = require('has-symbols')(); | ||
var callBound = require('../helpers/callBound.js'); | ||
|
||
var $toStr = callBound('%Object.prototype.toString%'); | ||
|
||
// http://www.ecma-international.org/ecma-262/5.1/#sec-9.11 | ||
|
||
module.exports = require('is-callable'); | ||
module.exports = function IsCallable(argument) { | ||
// some older engines say that typeof /abc/ === 'function', | ||
return typeof argument === 'function' | ||
&& (hasSymbols || $toStr(argument) !== '[object RegExp]'); | ||
}; |
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,7 +1,40 @@ | ||
'use strict'; | ||
|
||
var GetIntrinsic = require('../GetIntrinsic.js'); | ||
|
||
var $construct = GetIntrinsic('%Reflect.construct%', true); | ||
|
||
var DefinePropertyOrThrow = require('./DefinePropertyOrThrow'); | ||
try { | ||
DefinePropertyOrThrow({}, '', { '[[Get]]': function () {} }); | ||
} catch (e) { | ||
// Accessor properties aren't supported | ||
DefinePropertyOrThrow = null; | ||
} | ||
|
||
// https://www.ecma-international.org/ecma-262/6.0/#sec-isconstructor | ||
|
||
module.exports = function IsConstructor(argument) { | ||
return typeof argument === 'function' && !!argument.prototype; // unfortunately there's no way to truly check this without try/catch `new argument` | ||
}; | ||
if (DefinePropertyOrThrow && $construct) { | ||
var isConstructorMarker = {}; | ||
var badArrayLike = {}; | ||
DefinePropertyOrThrow(badArrayLike, 'length', { | ||
'[[Get]]': function () { | ||
throw isConstructorMarker; | ||
}, | ||
'[[Enumerable]]': true | ||
}); | ||
|
||
module.exports = function IsConstructor(argument) { | ||
try { | ||
// `Reflect.construct` invokes `IsConstructor(target)` before `Get(args, 'length')`: | ||
$construct(argument, badArrayLike); | ||
} catch (err) { | ||
return err === isConstructorMarker; | ||
} | ||
}; | ||
} else { | ||
module.exports = function IsConstructor(argument) { | ||
// unfortunately there's no way to truly check this without try/catch `new argument` in old environments | ||
return typeof argument === 'function' && !!argument.prototype; | ||
}; | ||
} |
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,5 +1,14 @@ | ||
'use strict'; | ||
|
||
var hasSymbols = require('has-symbols')(); | ||
var callBound = require('../helpers/callBound.js'); | ||
|
||
var $toStr = callBound('%Object.prototype.toString%'); | ||
|
||
// http://www.ecma-international.org/ecma-262/5.1/#sec-9.11 | ||
|
||
module.exports = require('is-callable'); | ||
module.exports = function IsCallable(argument) { | ||
// some older engines say that typeof /abc/ === 'function', | ||
return typeof argument === 'function' | ||
&& (hasSymbols || $toStr(argument) !== '[object RegExp]'); | ||
}; |
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,7 +1,40 @@ | ||
'use strict'; | ||
|
||
var GetIntrinsic = require('../GetIntrinsic.js'); | ||
|
||
var $construct = GetIntrinsic('%Reflect.construct%', true); | ||
|
||
var DefinePropertyOrThrow = require('./DefinePropertyOrThrow'); | ||
try { | ||
DefinePropertyOrThrow({}, '', { '[[Get]]': function () {} }); | ||
} catch (e) { | ||
// Accessor properties aren't supported | ||
DefinePropertyOrThrow = null; | ||
} | ||
|
||
// https://www.ecma-international.org/ecma-262/6.0/#sec-isconstructor | ||
|
||
module.exports = function IsConstructor(argument) { | ||
return typeof argument === 'function' && !!argument.prototype; // unfortunately there's no way to truly check this without try/catch `new argument` | ||
}; | ||
if (DefinePropertyOrThrow && $construct) { | ||
var isConstructorMarker = {}; | ||
var badArrayLike = {}; | ||
DefinePropertyOrThrow(badArrayLike, 'length', { | ||
'[[Get]]': function () { | ||
throw isConstructorMarker; | ||
}, | ||
'[[Enumerable]]': true | ||
}); | ||
|
||
module.exports = function IsConstructor(argument) { | ||
try { | ||
// `Reflect.construct` invokes `IsConstructor(target)` before `Get(args, 'length')`: | ||
$construct(argument, badArrayLike); | ||
} catch (err) { | ||
return err === isConstructorMarker; | ||
} | ||
}; | ||
} else { | ||
module.exports = function IsConstructor(argument) { | ||
// unfortunately there's no way to truly check this without try/catch `new argument` in old environments | ||
return typeof argument === 'function' && !!argument.prototype; | ||
}; | ||
} |
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,5 +1,14 @@ | ||
'use strict'; | ||
|
||
var hasSymbols = require('has-symbols')(); | ||
var callBound = require('../helpers/callBound.js'); | ||
|
||
var $toStr = callBound('%Object.prototype.toString%'); | ||
|
||
// http://www.ecma-international.org/ecma-262/5.1/#sec-9.11 | ||
|
||
module.exports = require('is-callable'); | ||
module.exports = function IsCallable(argument) { | ||
// some older engines say that typeof /abc/ === 'function', | ||
return typeof argument === 'function' | ||
&& (hasSymbols || $toStr(argument) !== '[object RegExp]'); | ||
}; |
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
Oops, something went wrong.