Skip to content

Commit

Permalink
Merge pull request es-shims#381 from Xotic750/bind
Browse files Browse the repository at this point in the history
[Robustness] `Function#bind`, `Array#join` robustness.

Fixes es-shims#378.
  • Loading branch information
ljharb committed Jan 27, 2016
2 parents d9dc203 + 654135d commit 9046218
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions es5-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ var $Array = Array;
var ArrayPrototype = $Array.prototype;
var $Object = Object;
var ObjectPrototype = $Object.prototype;
var FunctionPrototype = Function.prototype;
var $Function = Function;
var FunctionPrototype = $Function.prototype;
var $String = String;
var StringPrototype = $String.prototype;
var $Number = Number;
Expand All @@ -55,6 +56,7 @@ var array_splice = ArrayPrototype.splice;
var array_push = ArrayPrototype.push;
var array_unshift = ArrayPrototype.unshift;
var array_concat = ArrayPrototype.concat;
var array_join = ArrayPrototype.join;
var call = FunctionPrototype.call;
var apply = FunctionPrototype.apply;
var max = Math.max;
Expand Down Expand Up @@ -226,7 +228,8 @@ defineProperties(FunctionPrototype, {
// 5. Return the result of calling the [[Construct]] internal
// method of target providing args as the arguments.

var result = target.apply(
var result = apply.call(
target,
this,
array_concat.call(args, array_slice.call(arguments))
);
Expand Down Expand Up @@ -255,7 +258,8 @@ defineProperties(FunctionPrototype, {
// providing args as the arguments.

// equiv: target.call(this, ...boundArgs, ...args)
return target.apply(
return apply.call(
target,
that,
array_concat.call(args, array_slice.call(arguments))
);
Expand Down Expand Up @@ -285,7 +289,7 @@ defineProperties(FunctionPrototype, {
// for ex.) all use of eval or Function costructor throws an exception.
// However in all of these environments Function.prototype.bind exists
// and so this code will never be executed.
bound = Function('binder', 'return function (' + boundArgs.join(',') + '){ return binder.apply(this, arguments); }')(binder);
bound = $Function('binder', 'return function (' + array_join.call(boundArgs, ',') + '){ return binder.apply(this, arguments); }')(binder);

if (target.prototype) {
Empty.prototype = target.prototype;
Expand Down

0 comments on commit 9046218

Please sign in to comment.