11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci/* eslint no-invalid-this: 1 */
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_civar ERROR_MESSAGE = 'Function.prototype.bind called on incompatible ';
61cb0ef41Sopenharmony_civar toStr = Object.prototype.toString;
71cb0ef41Sopenharmony_civar max = Math.max;
81cb0ef41Sopenharmony_civar funcType = '[object Function]';
91cb0ef41Sopenharmony_ci
101cb0ef41Sopenharmony_civar concatty = function concatty(a, b) {
111cb0ef41Sopenharmony_ci    var arr = [];
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci    for (var i = 0; i < a.length; i += 1) {
141cb0ef41Sopenharmony_ci        arr[i] = a[i];
151cb0ef41Sopenharmony_ci    }
161cb0ef41Sopenharmony_ci    for (var j = 0; j < b.length; j += 1) {
171cb0ef41Sopenharmony_ci        arr[j + a.length] = b[j];
181cb0ef41Sopenharmony_ci    }
191cb0ef41Sopenharmony_ci
201cb0ef41Sopenharmony_ci    return arr;
211cb0ef41Sopenharmony_ci};
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_civar slicy = function slicy(arrLike, offset) {
241cb0ef41Sopenharmony_ci    var arr = [];
251cb0ef41Sopenharmony_ci    for (var i = offset || 0, j = 0; i < arrLike.length; i += 1, j += 1) {
261cb0ef41Sopenharmony_ci        arr[j] = arrLike[i];
271cb0ef41Sopenharmony_ci    }
281cb0ef41Sopenharmony_ci    return arr;
291cb0ef41Sopenharmony_ci};
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_civar joiny = function (arr, joiner) {
321cb0ef41Sopenharmony_ci    var str = '';
331cb0ef41Sopenharmony_ci    for (var i = 0; i < arr.length; i += 1) {
341cb0ef41Sopenharmony_ci        str += arr[i];
351cb0ef41Sopenharmony_ci        if (i + 1 < arr.length) {
361cb0ef41Sopenharmony_ci            str += joiner;
371cb0ef41Sopenharmony_ci        }
381cb0ef41Sopenharmony_ci    }
391cb0ef41Sopenharmony_ci    return str;
401cb0ef41Sopenharmony_ci};
411cb0ef41Sopenharmony_ci
421cb0ef41Sopenharmony_cimodule.exports = function bind(that) {
431cb0ef41Sopenharmony_ci    var target = this;
441cb0ef41Sopenharmony_ci    if (typeof target !== 'function' || toStr.apply(target) !== funcType) {
451cb0ef41Sopenharmony_ci        throw new TypeError(ERROR_MESSAGE + target);
461cb0ef41Sopenharmony_ci    }
471cb0ef41Sopenharmony_ci    var args = slicy(arguments, 1);
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_ci    var bound;
501cb0ef41Sopenharmony_ci    var binder = function () {
511cb0ef41Sopenharmony_ci        if (this instanceof bound) {
521cb0ef41Sopenharmony_ci            var result = target.apply(
531cb0ef41Sopenharmony_ci                this,
541cb0ef41Sopenharmony_ci                concatty(args, arguments)
551cb0ef41Sopenharmony_ci            );
561cb0ef41Sopenharmony_ci            if (Object(result) === result) {
571cb0ef41Sopenharmony_ci                return result;
581cb0ef41Sopenharmony_ci            }
591cb0ef41Sopenharmony_ci            return this;
601cb0ef41Sopenharmony_ci        }
611cb0ef41Sopenharmony_ci        return target.apply(
621cb0ef41Sopenharmony_ci            that,
631cb0ef41Sopenharmony_ci            concatty(args, arguments)
641cb0ef41Sopenharmony_ci        );
651cb0ef41Sopenharmony_ci
661cb0ef41Sopenharmony_ci    };
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ci    var boundLength = max(0, target.length - args.length);
691cb0ef41Sopenharmony_ci    var boundArgs = [];
701cb0ef41Sopenharmony_ci    for (var i = 0; i < boundLength; i++) {
711cb0ef41Sopenharmony_ci        boundArgs[i] = '$' + i;
721cb0ef41Sopenharmony_ci    }
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci    bound = Function('binder', 'return function (' + joiny(boundArgs, ',') + '){ return binder.apply(this,arguments); }')(binder);
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci    if (target.prototype) {
771cb0ef41Sopenharmony_ci        var Empty = function Empty() {};
781cb0ef41Sopenharmony_ci        Empty.prototype = target.prototype;
791cb0ef41Sopenharmony_ci        bound.prototype = new Empty();
801cb0ef41Sopenharmony_ci        Empty.prototype = null;
811cb0ef41Sopenharmony_ci    }
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci    return bound;
841cb0ef41Sopenharmony_ci};
85