$.export.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. var global = require('./$.global')
  2. , core = require('./$.core')
  3. , ctx = require('./$.ctx')
  4. , PROTOTYPE = 'prototype';
  5. var $export = function(type, name, source){
  6. var IS_FORCED = type & $export.F
  7. , IS_GLOBAL = type & $export.G
  8. , IS_STATIC = type & $export.S
  9. , IS_PROTO = type & $export.P
  10. , IS_BIND = type & $export.B
  11. , IS_WRAP = type & $export.W
  12. , exports = IS_GLOBAL ? core : core[name] || (core[name] = {})
  13. , target = IS_GLOBAL ? global : IS_STATIC ? global[name] : (global[name] || {})[PROTOTYPE]
  14. , key, own, out;
  15. if(IS_GLOBAL)source = name;
  16. for(key in source){
  17. // contains in native
  18. own = !IS_FORCED && target && key in target;
  19. if(own && key in exports)continue;
  20. // export native or passed
  21. out = own ? target[key] : source[key];
  22. // prevent global pollution for namespaces
  23. exports[key] = IS_GLOBAL && typeof target[key] != 'function' ? source[key]
  24. // bind timers to global for call from export context
  25. : IS_BIND && own ? ctx(out, global)
  26. // wrap global constructors for prevent change them in library
  27. : IS_WRAP && target[key] == out ? (function(C){
  28. var F = function(param){
  29. return this instanceof C ? new C(param) : C(param);
  30. };
  31. F[PROTOTYPE] = C[PROTOTYPE];
  32. return F;
  33. // make static versions for prototype methods
  34. })(out) : IS_PROTO && typeof out == 'function' ? ctx(Function.call, out) : out;
  35. if(IS_PROTO)(exports[PROTOTYPE] || (exports[PROTOTYPE] = {}))[key] = out;
  36. }
  37. };
  38. // type bitmap
  39. $export.F = 1; // forced
  40. $export.G = 2; // global
  41. $export.S = 4; // static
  42. $export.P = 8; // proto
  43. $export.B = 16; // bind
  44. $export.W = 32; // wrap
  45. module.exports = $export;