Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new.target instead of instanceof to detect if its a constructor call #7431

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

Jarred-Sumner
Copy link
Collaborator

What does this PR do?

Replaces usages of

if (!(this instanceof MyClass)) return new MyClass(...args)

with

if (!new.target) return new MyClass(...args)

It should be faster, though I have yet to benchmark it.

See also nodejs/node#51026

How did you verify your code works?

Existing tests should cover it

@paperdave
Copy link
Collaborator

stupid question but are you certain that all these classes have at least one test that calls the constructor without new?

Copy link

github-actions bot commented Dec 5, 2023

@paperdave 10 files with test failures on bun-darwin-aarch64:

  • test/cli/install/bunx.test.ts
  • test/cli/run/env.test.ts
  • test/cli/run/require-cache.test.ts
  • test/js/bun/test/test-test.test.ts
  • test/js/node/crypto/crypto.key-objects.test.ts
  • test/js/node/util/parse_args/parse-args.test.mjs
  • test/js/node/watch/fs.watchFile.test.ts
  • test/js/third_party/jsonwebtoken/schema.test.js
  • test/js/web/fetch/body-stream.test.ts
  • test/js/web/worker.test.ts

View test output

#1f324fee463c9021760c8058aaca425f4ae84a36

Copy link

github-actions bot commented Dec 5, 2023

@paperdave 6 files with test failures on linux-x64-baseline:

  • test/cli/install/bun-install.test.ts
  • test/cli/run/env.test.ts
  • test/js/node/crypto/crypto.key-objects.test.ts
  • test/js/node/util/parse_args/parse-args.test.mjs
  • test/js/node/v8/capture-stack-trace.test.js
  • test/js/third_party/jsonwebtoken/schema.test.js

View test output

#1f324fee463c9021760c8058aaca425f4ae84a36

Copy link

github-actions bot commented Dec 5, 2023

@paperdave 6 files with test failures on linux-x64:

  • test/cli/run/env.test.ts
  • test/js/node/crypto/crypto.key-objects.test.ts
  • test/js/node/util/parse_args/parse-args.test.mjs
  • test/js/node/v8/capture-stack-trace.test.js
  • test/js/node/watch/fs.watch.test.ts
  • test/js/third_party/jsonwebtoken/schema.test.js

View test output

#1f324fee463c9021760c8058aaca425f4ae84a36

Copy link

github-actions bot commented Dec 5, 2023

@Jarred-Sumner 12 files with test failures on bun-darwin-x64:

  • test/cli/run/env.test.ts
  • test/js/bun/spawn/spawn-streaming-stdout.test.ts
  • test/js/bun/util/which.test.ts
  • test/js/node/crypto/crypto.key-objects.test.ts
  • test/js/node/fs/fs.test.ts
  • test/js/node/string_decoder/string-decoder.test.js
  • test/js/node/util/parse_args/parse-args.test.mjs
  • test/js/node/v8/capture-stack-trace.test.js
  • test/js/node/watch/fs.watchFile.test.ts
  • test/js/third_party/jsonwebtoken/schema.test.js
  • test/js/third_party/webpack/webpack.test.ts
  • test/js/web/timers/setTimeout.test.js

View test output

#2f4f3bb76ae8f660593abdad871531abeb3913fd

Copy link

github-actions bot commented Dec 5, 2023

@paperdave 13 files with test failures on bun-darwin-x64-baseline:

  • test/cli/run/env.test.ts
  • test/integration/next/default-pages-dir/test/next-build.test.ts
  • test/js/bun/spawn/spawn-streaming-stdout.test.ts
  • test/js/bun/util/filesink.test.ts
  • test/js/bun/util/which.test.ts
  • test/js/node/crypto/crypto.key-objects.test.ts
  • test/js/node/fs/fs.test.ts
  • test/js/node/util/parse_args/parse-args.test.mjs
  • test/js/node/v8/capture-stack-trace.test.js
  • test/js/node/watch/fs.watchFile.test.ts
  • test/js/third_party/jsonwebtoken/schema.test.js
  • test/js/third_party/webpack/webpack.test.ts
  • test/js/web/timers/setTimeout.test.js

View test output

#1f324fee463c9021760c8058aaca425f4ae84a36

@paperdave
Copy link
Collaborator

in the future we need to not put multiple things into the same pr. i know that is hypocritical of me considering #6938 having like 50 changes, but still.

i'm skeptical of the streams changes and would honestly want that as a separate pr. it feels like something we're going to end up breaking and have to revert, but you have valuable changes to the syntax highlighter and napi.

Copy link
Collaborator

@paperdave paperdave left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ive moved the changes not related to new.target to another pr.

changes to node:stream must be done carefully. after looking over it a few times it looks alright, but i still wonder if there is anything that can break with using !new.target

i think this should only be merged after we manually verify that Next.js, Express, Fastify, Webpack, and some other packages that may abuse stream mechanics to verify they still work.

Copy link
Collaborator

@paperdave paperdave left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(see prev comment)

@paperdave paperdave marked this pull request as draft December 7, 2023 01:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants