_export.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. var global = require('./_global');
  2. var core = require('./_core');
  3. var ctx = require('./_ctx');
  4. var hide = require('./_hide');
  5. var has = require('./_has');
  6. var PROTOTYPE = 'prototype';
  7. var $export = function (type, name, source) {
  8. var IS_FORCED = type & $export.F;
  9. var IS_GLOBAL = type & $export.G;
  10. var IS_STATIC = type & $export.S;
  11. var IS_PROTO = type & $export.P;
  12. var IS_BIND = type & $export.B;
  13. var IS_WRAP = type & $export.W;
  14. var exports = IS_GLOBAL ? core : core[name] || (core[name] = {});
  15. var expProto = exports[PROTOTYPE];
  16. var target = IS_GLOBAL ? global : IS_STATIC ? global[name] : (global[name] || {})[PROTOTYPE];
  17. var key, own, out;
  18. if (IS_GLOBAL) source = name;
  19. for (key in source) {
  20. // contains in native
  21. own = !IS_FORCED && target && target[key] !== undefined;
  22. if (own && has(exports, key)) continue;
  23. // export native or passed
  24. out = own ? target[key] : source[key];
  25. // prevent global pollution for namespaces
  26. exports[key] = IS_GLOBAL && typeof target[key] != 'function' ? source[key]
  27. // bind timers to global for call from export context
  28. : IS_BIND && own ? ctx(out, global)
  29. // wrap global constructors for prevent change them in library
  30. : IS_WRAP && target[key] == out ? (function (C) {
  31. var F = function (a, b, c) {
  32. if (this instanceof C) {
  33. switch (arguments.length) {
  34. case 0: return new C();
  35. case 1: return new C(a);
  36. case 2: return new C(a, b);
  37. } return new C(a, b, c);
  38. } return C.apply(this, arguments);
  39. };
  40. F[PROTOTYPE] = C[PROTOTYPE];
  41. return F;
  42. // make static versions for prototype methods
  43. })(out) : IS_PROTO && typeof out == 'function' ? ctx(Function.call, out) : out;
  44. // export proto methods to core.%CONSTRUCTOR%.methods.%NAME%
  45. if (IS_PROTO) {
  46. (exports.virtual || (exports.virtual = {}))[key] = out;
  47. // export proto methods to core.%CONSTRUCTOR%.prototype.%NAME%
  48. if (type & $export.R && expProto && !expProto[key]) hide(expProto, key, out);
  49. }
  50. }
  51. };
  52. // type bitmap
  53. $export.F = 1; // forced
  54. $export.G = 2; // global
  55. $export.S = 4; // static
  56. $export.P = 8; // proto
  57. $export.B = 16; // bind
  58. $export.W = 32; // wrap
  59. $export.U = 64; // safe
  60. $export.R = 128; // real proto method for `library`
  61. module.exports = $export;