diff options
author | 2023-08-31 22:31:01 -0700 | |
---|---|---|
committer | 2023-08-31 22:31:01 -0700 | |
commit | d0a58d329f67708fa3427882f04c7268746d937d (patch) | |
tree | f83de8b45833e9976f258ddf9bd31fbfd602f82d /src/js/internal/shared.ts | |
parent | b07bb3ce174b500b567bb760e82d4696c88a8b28 (diff) | |
download | bun-dave/es5-class-helper.tar.gz bun-dave/es5-class-helper.tar.zst bun-dave/es5-class-helper.zip |
add es5ClassCompatdave/es5-class-helper
Diffstat (limited to 'src/js/internal/shared.ts')
-rw-r--r-- | src/js/internal/shared.ts | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/js/internal/shared.ts b/src/js/internal/shared.ts index 98a5b0a71..0b20572ef 100644 --- a/src/js/internal/shared.ts +++ b/src/js/internal/shared.ts @@ -29,8 +29,31 @@ function hideFromStack(...fns) { } } +/** + * This utility ensures that old JS code that uses functions for classes still works. + * Taken from https://github.com/microsoft/vscode/blob/main/src/vs/workbench/api/common/extHostTypes.ts + */ +function es5ClassCompat(target: Function): any { + const interceptFunctions = { + apply: function () { + const args = arguments.length === 1 ? [] : arguments[1]; + return Reflect.construct(target, args, arguments[0].constructor); + }, + call: function () { + if (arguments.length === 0) { + return Reflect.construct(target, []); + } else { + const [thisArg, ...restArgs] = arguments; + return Reflect.construct(target, restArgs, thisArg.constructor); + } + }, + }; + return Object.assign(target, interceptFunctions); +} + export default { NotImplementedError, throwNotImplemented, hideFromStack, + es5ClassCompat, }; |