Skip to content

Commit

Permalink
add set(Timeout|Interval|Immediate) extra arguments fix for Bun (si…
Browse files Browse the repository at this point in the history
…milarly to IE9-)

oven-sh/bun#1633
  • Loading branch information
zloirock committed Dec 20, 2022
1 parent 22cfd5d commit c5e2d95
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@
- `{ Map, WeakMap }.prototype.emplace` became stricter [by the spec draft](https://tc39.es/proposal-upsert/)
- Removed some generic behavior (like `@@species` pattern) of some `.prototype` methods from the [new collections methods proposal](https://github.com/tc39/proposal-collection-methods) and the [`Array` deduplication proposal](https://github.com/tc39/proposal-array-unique) that *most likely* will not be implemented since it contradicts the current TC39 policy
- Added pure version of the `Number` constructor, [#1154](https://github.com/zloirock/core-js/issues/1154), [#1155](https://github.com/zloirock/core-js/issues/1155), thanks [@trosos](https://github.com/trosos)
- Added `set(Timeout|Interval|Immediate)` extra arguments fix for Bun (similarly to IE9-), [bun/1633](https://github.com/oven-sh/bun/issues/1633)
- Fixed handling of sparse arrays in `structuredClone`, [#1156](https://github.com/zloirock/core-js/issues/1156)
- Fixed a theoretically possible future conflict of polyfills definitions in the pure version
- Some refactoring and optimization
Expand Down
15 changes: 10 additions & 5 deletions packages/core-js-compat/src/data.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2299,7 +2299,8 @@ export const data = {
},
// TODO: Remove this module from `core-js@4` since it's split to submodules
'web.immediate': {
bun: '0.1.7',
// https://github.com/oven-sh/bun/issues/1633
// bun: '0.1.7',
ie: '10',
node: '0.9.1',
},
Expand All @@ -2322,13 +2323,15 @@ export const data = {
safari: '10',
},
'web.set-immediate': {
bun: '0.1.7',
// https://github.com/oven-sh/bun/issues/1633
// bun: '0.1.7',
ie: '10',
node: '0.9.1',
},
'web.set-interval': {
android: '1.5',
bun: '0.1.1',
// https://github.com/oven-sh/bun/issues/1633
// bun: '0.1.1',
chrome: '1',
deno: '1.0',
firefox: '1',
Expand All @@ -2341,7 +2344,8 @@ export const data = {
},
'web.set-timeout': {
android: '1.5',
bun: '0.1.1',
// https://github.com/oven-sh/bun/issues/1633
// bun: '0.1.1',
chrome: '1',
deno: '1.0',
firefox: '1',
Expand All @@ -2362,7 +2366,8 @@ export const data = {
// TODO: Remove this module from `core-js@4` since it's split to submodules
'web.timers': {
android: '1.5',
bun: '0.1.1',
// https://github.com/oven-sh/bun/issues/1633
// bun: '0.1.1',
chrome: '1',
deno: '1.0',
firefox: '1',
Expand Down
2 changes: 2 additions & 0 deletions packages/core-js/internals/engine-is-bun.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/* global Bun -- Deno case */
module.exports = typeof Bun == 'function' && Bun && typeof Bun.version == 'string';
40 changes: 20 additions & 20 deletions packages/core-js/internals/schedulers-fix.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
'use strict';
var global = require('../internals/global');
var apply = require('../internals/function-apply');
var isCallable = require('../internals/is-callable');
var userAgent = require('../internals/engine-user-agent');
var ENGINE_IS_BUN = require('../internals/engine-is-bun');
var USER_AGENT = require('../internals/engine-user-agent');
var arraySlice = require('../internals/array-slice');
var validateArgumentsLength = require('../internals/validate-arguments-length');

var MSIE = /MSIE .\./.test(userAgent); // <- dirty ie9- check
var Function = global.Function;
// dirty IE9- and Bun 0.3.0- checks
var WRAP = /MSIE .\./.test(USER_AGENT) || ENGINE_IS_BUN && (function () {
var version = global.Bun.version.split('.');
return version.length < 3 || version[0] == 0 && (version[1] < 3 || version[1] == 3 && version[2] == 0);
});

var wrap = function (scheduler) {
return MSIE ? function (handler, timeout /* , ...arguments */) {
var boundArgs = validateArgumentsLength(arguments.length, 1) > 2;
// IE9- / Bun 0.3.0- setTimeout / setInterval / setImmediate additional parameters fix
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#timers
// https://github.com/oven-sh/bun/issues/1633
module.exports = function (scheduler, hasTimeArg) {
var firstParamIndex = hasTimeArg ? 2 : 1;
return WRAP ? function (handler, timeout /* , ...arguments */) {
var boundArgs = validateArgumentsLength(arguments.length, 1) > firstParamIndex;
var fn = isCallable(handler) ? handler : Function(handler);
var args = boundArgs ? arraySlice(arguments, 2) : undefined;
return scheduler(boundArgs ? function () {
apply(fn, this, args);
} : fn, timeout);
var params = boundArgs ? arraySlice(arguments, firstParamIndex) : [];
var callback = boundArgs ? function () {
apply(fn, this, params);
} : fn;
return hasTimeArg ? scheduler(callback, timeout) : scheduler(callback);
} : scheduler;
};

// ie9- setTimeout & setInterval additional parameters fix
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#timers
module.exports = {
// `setTimeout` method
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-settimeout
setTimeout: wrap(global.setTimeout),
// `setInterval` method
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-setinterval
setInterval: wrap(global.setInterval)
};
6 changes: 5 additions & 1 deletion packages/core-js/modules/web.set-immediate.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
var $ = require('../internals/export');
var global = require('../internals/global');
var setImmediate = require('../internals/task').set;
var setTask = require('../internals/task').set;
var schedulersFix = require('../internals/schedulers-fix');

// https://github.com/oven-sh/bun/issues/1633
var setImmediate = global.setImmediate ? schedulersFix(setTask, false) : setTask;

// `setImmediate` method
// http://w3c.github.io/setImmediate/#si-setImmediate
Expand Down
6 changes: 4 additions & 2 deletions packages/core-js/modules/web.set-interval.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
var $ = require('../internals/export');
var global = require('../internals/global');
var setInterval = require('../internals/schedulers-fix').setInterval;
var schedulersFix = require('../internals/schedulers-fix');

// ie9- setInterval additional parameters fix
var setInterval = schedulersFix(global.setInterval, true);

// Bun / IE9- setInterval additional parameters fix
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-setinterval
$({ global: true, bind: true, forced: global.setInterval !== setInterval }, {
setInterval: setInterval
Expand Down
6 changes: 4 additions & 2 deletions packages/core-js/modules/web.set-timeout.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
var $ = require('../internals/export');
var global = require('../internals/global');
var setTimeout = require('../internals/schedulers-fix').setTimeout;
var schedulersFix = require('../internals/schedulers-fix');

// ie9- setTimeout additional parameters fix
var setTimeout = schedulersFix(global.setTimeout, true);

// Bun / IE9- setTimeout additional parameters fix
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#dom-settimeout
$({ global: true, bind: true, forced: global.setTimeout !== setTimeout }, {
setTimeout: setTimeout
Expand Down
11 changes: 7 additions & 4 deletions tests/compat/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var NOT_WHITESPACES = '\u200B\u0085\u180E';
var USERAGENT = GLOBAL.navigator && GLOBAL.navigator.userAgent || '';

var process = GLOBAL.process;
var Bun = GLOBAL.Bun;
var Deno = GLOBAL.Deno;
var versions = process && process.versions || Deno && Deno.version;
var v8 = versions && versions.v8;
Expand All @@ -32,7 +33,7 @@ if (!V8_VERSION && USERAGENT) {
}

var IS_BROWSER = typeof window == 'object' && typeof Deno != 'object';

var IS_BUN = typeof Bun == 'function' && Bun && typeof Bun.version == 'string';
var IS_DENO = typeof Deno == 'object' && Deno && typeof Deno.version == 'object';

// var IS_NODE = Object.prototype.toString.call(process) == '[object process]';
Expand Down Expand Up @@ -222,11 +223,11 @@ function createStringTrimMethodTest(METHOD_NAME) {
}

function IMMEDIATE() {
return setImmediate && clearImmediate;
return setImmediate && clearImmediate && !IS_BUN;
}

function TIMERS() {
return !/MSIE .\./.test(USERAGENT);
return !IS_BUN && !/MSIE .\./.test(USERAGENT);
}

GLOBAL.tests = {
Expand Down Expand Up @@ -1799,7 +1800,9 @@ GLOBAL.tests = {
return typeof btoa == 'function';
}
},
'web.clear-immediate': IMMEDIATE,
'web.clear-immediate': function () {
return setImmediate && clearImmediate;
},
'web.dom-collections.for-each': function () {
return (!GLOBAL.NodeList || (NodeList.prototype.forEach && NodeList.prototype.forEach === [].forEach))
&& (!GLOBAL.DOMTokenList || (DOMTokenList.prototype.forEach && DOMTokenList.prototype.forEach === [].forEach));
Expand Down

0 comments on commit c5e2d95

Please sign in to comment.