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 typeof BigInt to check for BigInt support #488

Merged
merged 7 commits into from
Jun 12, 2023

Conversation

lukasIO
Copy link
Contributor

@lukasIO lukasIO commented May 15, 2023

fixes #486

@CLAassistant
Copy link

CLAassistant commented May 15, 2023

CLA assistant check
All committers have signed the CLA.

@lukasIO lukasIO changed the title Add feature check for globalThis Use typeof BigInt to check for BigInt support May 15, 2023
@smaye81
Copy link
Member

smaye81 commented May 22, 2023

Thanks for the PR @lukasIO. It's definitely unfortunate that globalThis is only supported in ES2020 and above.

It looks like we have a few other uses of globalThis, however. For example, take a look at packages/protoplugin/src/ecmascript/generated-file.ts. Would you be willing to fix those occurrences also and put up a few unit tests to ensure we're fixing all the right things?

@lukasIO
Copy link
Contributor Author

lukasIO commented May 23, 2023

Sure, I can do that!

Should the unit tests go into the protobuf-test package?
How do you usually run that (didn't see any scripts that invoke jest)?

@smaye81
Copy link
Member

smaye81 commented May 23, 2023

It depends what you are testing. Anything testing functionality in packages/protobuf is tested in packages/protobuf-test. If you are testing protoplugin code, then the tests should go in packages/protoplugin-test.

For running tests, you can run make test from the app root. If you want to run tests for a specific package, you can run:

  • make test-protobuf
  • make test-protoplugin

Thanks again!

@lukasIO
Copy link
Contributor Author

lukasIO commented May 26, 2023

put up a few unit tests to ensure we're fixing all the right things

I realised I'm not sure what you're looking for test wise regarding the changes in this PR.
Still happy to write them as part of this PR, but could you elaborate on what specifically you'd like to test for?

@timostamm
Copy link
Member

If we refer to Number without the globalThis modifier, it can clash with a protobuf message named "Number". We have to avoid this situation. Our generated code should always compile.

@lukasIO
Copy link
Contributor Author

lukasIO commented May 26, 2023

would adding Number to the reservedIdentifiers suffice for that situation? (the lowercase number is already in there)

@timostamm
Copy link
Member

would adding Number to the reservedIdentifiers suffice for that situation? (the lowercase number is already in there)

It should automatically avoid name clashes by generating a class named Number$ instead of Number for a protobuf message named Number.

But this is technically a breaking change. If any user is using such a protobuf message, we break their generated code, which we definitely want to avoid.

I don't have an immediate idea to solve this 🤔

@lukasIO
Copy link
Contributor Author

lukasIO commented May 31, 2023

Are you sure it would be a breaking change? Afaict using Number as a protobuf message name would potentially break already today.

If globalThis.Number was emitted until now that means that the environment in which the code was running had a global variable called Number defined already, which means the generated protobuf message would clash with the globally defined Number. Obviously trying to argue for my case here, but that sounds like a bug fix to me 👀

@smaye81
Copy link
Member

smaye81 commented Jun 7, 2023

Hi @lukasIO. Sorry for the delay, but we did some thinking on how best to solve this. To answer your question, it would be a breaking change. If we started generating a class name of Number$ to avoid the name clash, then we would break the code of anyone using Number for a message name.

However, we think we've come up with a way to remove the globalThis usage while still not breaking anyone's code. Take a look at #504, which should solve this problem.

I think as far as this PR is concerned, you can revert your changes to packages/protoplugin/src/ecmascript/generated-file.ts and we can land your change as-is to proto-int64. This PR coupled with #504 should hopefully solve the globalThis issue.

@lukasIO
Copy link
Contributor Author

lukasIO commented Jun 8, 2023

nice! reverted the other changes.
Let me know if you prefer typeof BigInt === 'function' over the current typeof BigInt !== 'undefined'

smaye81 added a commit that referenced this pull request Jun 8, 2023
This removes the usage of `globalThis` in generated code by instead
using an export of global `Number` constants.

This is done so that we can safely use these global constants when
generating code and be assured we're using the correct values. We cannot
use `globalThis` since we support ES2017 and `globalThis` was introduced
in ES2020. We also don't want to explicitly generate code using, for
example, `Number.NaN`, since this could clash with a message name of
`Number`.

Instead we can export them in a local `proto-double.js` file since this
will be in a different scope as the generated code and we are guaranteed
to use the intended global values.

Note that this complements
#488 to remove usage of
`globalThis` in the repo.
@smaye81
Copy link
Member

smaye81 commented Jun 8, 2023

Let's change it to typeof BigInt === 'function' to stay consistent with the other checks and to be more precise.

@smaye81
Copy link
Member

smaye81 commented Jun 8, 2023

re: the CI failure, you just need to run make locally and push up the changes (looks to just be the code benchmark README).

@lukasIO
Copy link
Contributor Author

lukasIO commented Jun 9, 2023

hm, getting an error on make now running locally

protobuf-es/.tmp/protobuf-23.0/src/google/protobuf/BUILD.bazel:749:14: Linking src/google/protobuf/libtest_messages_proto3_proto.so failed: (Exit 1): cc_wrapper.sh failed: error executing command external/local_config_cc/cc_wrapper.sh @bazel-out/darwin-fastbuild/bin/src/google/protobuf/libtest_messages_proto3_proto.so-2.params

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
ld: illegal thread local variable reference to regular symbol __ZN6google8protobuf8internal15ThreadSafeArena13thread_cache_E for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

running make bench by itself does work however. not sure if something else is needed. feel free to push up any other changes that are required.

@smaye81 smaye81 merged commit f955b29 into bufbuild:main Jun 12, 2023
3 checks passed
@smaye81
Copy link
Member

smaye81 commented Jun 12, 2023

Merged. Thanks for the PR @lukasIO!

@smaye81 smaye81 mentioned this pull request Jul 14, 2023
smaye81 added a commit that referenced this pull request Jul 14, 2023
## What's Changed
* Add `toPlainMessage` to convert `Message` objects to their
`PlainMessage` variants by @srikrsna-buf in
#511
* Remove usage of globalThis in generated code by @smaye81 in
#504
* Use `typeof BigInt` to check for BigInt support by @lukasIO in
#488

## New Contributors
* @lukasIO made their first contribution in #488.

**Full Changelog**:
https://github.com/bufbuild/protobuf-es/compare/v1.2.1..v1.3.0
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.

Guard against globalThis not being defined
4 participants