platform.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. var _a;
  6. const LANGUAGE_DEFAULT = 'en';
  7. let _isWindows = false;
  8. let _isMacintosh = false;
  9. let _isLinux = false;
  10. let _isLinuxSnap = false;
  11. let _isNative = false;
  12. let _isWeb = false;
  13. let _isIOS = false;
  14. let _locale = undefined;
  15. let _language = LANGUAGE_DEFAULT;
  16. let _translationsConfigFile = undefined;
  17. let _userAgent = undefined;
  18. export const globals = (typeof self === 'object' ? self : typeof global === 'object' ? global : {});
  19. let nodeProcess = undefined;
  20. if (typeof globals.vscode !== 'undefined' && typeof globals.vscode.process !== 'undefined') {
  21. // Native environment (sandboxed)
  22. nodeProcess = globals.vscode.process;
  23. }
  24. else if (typeof process !== 'undefined') {
  25. // Native environment (non-sandboxed)
  26. nodeProcess = process;
  27. }
  28. const isElectronRenderer = typeof ((_a = nodeProcess === null || nodeProcess === void 0 ? void 0 : nodeProcess.versions) === null || _a === void 0 ? void 0 : _a.electron) === 'string' && nodeProcess.type === 'renderer';
  29. // Web environment
  30. if (typeof navigator === 'object' && !isElectronRenderer) {
  31. _userAgent = navigator.userAgent;
  32. _isWindows = _userAgent.indexOf('Windows') >= 0;
  33. _isMacintosh = _userAgent.indexOf('Macintosh') >= 0;
  34. _isIOS = (_userAgent.indexOf('Macintosh') >= 0 || _userAgent.indexOf('iPad') >= 0 || _userAgent.indexOf('iPhone') >= 0) && !!navigator.maxTouchPoints && navigator.maxTouchPoints > 0;
  35. _isLinux = _userAgent.indexOf('Linux') >= 0;
  36. _isWeb = true;
  37. _locale = navigator.language;
  38. _language = _locale;
  39. }
  40. // Native environment
  41. else if (typeof nodeProcess === 'object') {
  42. _isWindows = (nodeProcess.platform === 'win32');
  43. _isMacintosh = (nodeProcess.platform === 'darwin');
  44. _isLinux = (nodeProcess.platform === 'linux');
  45. _isLinuxSnap = _isLinux && !!nodeProcess.env['SNAP'] && !!nodeProcess.env['SNAP_REVISION'];
  46. _locale = LANGUAGE_DEFAULT;
  47. _language = LANGUAGE_DEFAULT;
  48. const rawNlsConfig = nodeProcess.env['VSCODE_NLS_CONFIG'];
  49. if (rawNlsConfig) {
  50. try {
  51. const nlsConfig = JSON.parse(rawNlsConfig);
  52. const resolved = nlsConfig.availableLanguages['*'];
  53. _locale = nlsConfig.locale;
  54. // VSCode's default language is 'en'
  55. _language = resolved ? resolved : LANGUAGE_DEFAULT;
  56. _translationsConfigFile = nlsConfig._translationsConfigFile;
  57. }
  58. catch (e) {
  59. }
  60. }
  61. _isNative = true;
  62. }
  63. // Unknown environment
  64. else {
  65. console.error('Unable to resolve platform.');
  66. }
  67. let _platform = 0 /* Web */;
  68. if (_isMacintosh) {
  69. _platform = 1 /* Mac */;
  70. }
  71. else if (_isWindows) {
  72. _platform = 3 /* Windows */;
  73. }
  74. else if (_isLinux) {
  75. _platform = 2 /* Linux */;
  76. }
  77. export const isWindows = _isWindows;
  78. export const isMacintosh = _isMacintosh;
  79. export const isLinux = _isLinux;
  80. export const isNative = _isNative;
  81. export const isWeb = _isWeb;
  82. export const isIOS = _isIOS;
  83. export const userAgent = _userAgent;
  84. export const setImmediate = (function defineSetImmediate() {
  85. if (globals.setImmediate) {
  86. return globals.setImmediate.bind(globals);
  87. }
  88. if (typeof globals.postMessage === 'function' && !globals.importScripts) {
  89. let pending = [];
  90. globals.addEventListener('message', (e) => {
  91. if (e.data && e.data.vscodeSetImmediateId) {
  92. for (let i = 0, len = pending.length; i < len; i++) {
  93. const candidate = pending[i];
  94. if (candidate.id === e.data.vscodeSetImmediateId) {
  95. pending.splice(i, 1);
  96. candidate.callback();
  97. return;
  98. }
  99. }
  100. }
  101. });
  102. let lastId = 0;
  103. return (callback) => {
  104. const myId = ++lastId;
  105. pending.push({
  106. id: myId,
  107. callback: callback
  108. });
  109. globals.postMessage({ vscodeSetImmediateId: myId }, '*');
  110. };
  111. }
  112. if (typeof (nodeProcess === null || nodeProcess === void 0 ? void 0 : nodeProcess.nextTick) === 'function') {
  113. return nodeProcess.nextTick.bind(nodeProcess);
  114. }
  115. const _promise = Promise.resolve();
  116. return (callback) => _promise.then(callback);
  117. })();
  118. export const OS = (_isMacintosh || _isIOS ? 2 /* Macintosh */ : (_isWindows ? 1 /* Windows */ : 3 /* Linux */));
  119. let _isLittleEndian = true;
  120. let _isLittleEndianComputed = false;
  121. export function isLittleEndian() {
  122. if (!_isLittleEndianComputed) {
  123. _isLittleEndianComputed = true;
  124. const test = new Uint8Array(2);
  125. test[0] = 1;
  126. test[1] = 2;
  127. const view = new Uint16Array(test.buffer);
  128. _isLittleEndian = (view[0] === (2 << 8) + 1);
  129. }
  130. return _isLittleEndian;
  131. }