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

Remove usage of globalThis in generated code #504

Merged
merged 2 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/protobuf-bench/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ server would usually do.

| code generator | bundle size | minified | compressed |
|---------------------|------------------------:|-----------------------:|-------------------:|
| protobuf-es | 87,781 b | 37,518 b | 9,777 b |
| protobuf-es | 87,781 b | 37,518 b | 9,782 b |
| protobuf-javascript | 394,384 b | 288,761 b | 45,123 b |
14 changes: 7 additions & 7 deletions packages/protobuf-test/src/gen/js/google/protobuf/unittest_pb.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions packages/protobuf-test/src/gen/ts/google/protobuf/unittest_pb.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/protobuf/src/codegen-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type RuntimeSymbolName =
| "JsonWriteOptions"
| "JsonValue"
| "JsonObject"
| "protoDouble"
| "protoInt64"
| "ScalarType"
| "MethodKind"
Expand Down Expand Up @@ -84,6 +85,7 @@ export const codegenInfo: CodegenInfo = {
JsonWriteOptions: {typeOnly: true, privateImportPath: "./json-format.js", publicImportPath: packageName},
JsonValue: {typeOnly: true, privateImportPath: "./json-format.js", publicImportPath: packageName},
JsonObject: {typeOnly: true, privateImportPath: "./json-format.js", publicImportPath: packageName},
protoDouble: {typeOnly: false, privateImportPath: "./proto-double.js", publicImportPath: packageName},
protoInt64: {typeOnly: false, privateImportPath: "./proto-int64.js", publicImportPath: packageName},
ScalarType: {typeOnly: false, privateImportPath: "./field.js", publicImportPath: packageName},
MethodKind: {typeOnly: false, privateImportPath: "./service-type.js", publicImportPath: packageName},
Expand Down
1 change: 1 addition & 0 deletions packages/protobuf/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

export { proto3 } from "./proto3.js";
export { proto2 } from "./proto2.js";
export { protoDouble } from "./proto-double.js";
export { protoInt64 } from "./proto-int64.js";
export { protoBase64 } from "./proto-base64.js";
export { protoDelimited } from "./proto-delimited.js";
Expand Down
27 changes: 27 additions & 0 deletions packages/protobuf/src/proto-double.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2021-2023 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// Export 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 rely on 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 here since this will
// be in a different scope as the generated code and we are guaranteed to use
// the intended global values.
export const protoDouble = {
NaN: Number.NaN,
POSITIVE_INFINITY: Number.POSITIVE_INFINITY,
NEGATIVE_INFINITY: Number.NEGATIVE_INFINITY,
} as const;
13 changes: 8 additions & 5 deletions packages/protoplugin/src/ecmascript/generated-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ function printableToEl(
el.push(p);
break;
case "number":
el.push(literalNumber(p));
el.push(...literalNumber(p, runtimeImports));
break;
case "boolean":
el.push(p.toString());
Expand Down Expand Up @@ -408,15 +408,18 @@ function processImports(
return symbolToIdentifier;
}

function literalNumber(value: number): string {
function literalNumber(
value: number,
runtimeImports: RuntimeImports
): El[] | string {
if (Number.isNaN(value)) {
return "globalThis.Number.NaN";
return [runtimeImports.protoDouble, ".NaN"];
}
if (value === Number.POSITIVE_INFINITY) {
return "globalThis.Number.POSITIVE_INFINITY";
return [runtimeImports.protoDouble, ".POSITIVE_INFINITY"];
}
if (value === Number.NEGATIVE_INFINITY) {
return "globalThis.Number.NEGATIVE_INFINITY";
return [runtimeImports.protoDouble, ".NEGATIVE_INFINITY"];
}
return value.toString(10);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/protoplugin/src/ecmascript/runtime-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface RuntimeImports {
JsonWriteOptions: ImportSymbol;
JsonValue: ImportSymbol;
JsonObject: ImportSymbol;
protoDouble: ImportSymbol;
protoInt64: ImportSymbol;
ScalarType: ImportSymbol;
MethodKind: ImportSymbol;
Expand All @@ -53,6 +54,7 @@ export function createRuntimeImports(bootstrapWkt: boolean): RuntimeImports {
JsonWriteOptions: infoToSymbol("JsonWriteOptions", bootstrapWkt),
JsonValue: infoToSymbol("JsonValue", bootstrapWkt),
JsonObject: infoToSymbol("JsonObject", bootstrapWkt),
protoDouble: infoToSymbol("protoDouble", bootstrapWkt),
protoInt64: infoToSymbol("protoInt64", bootstrapWkt),
ScalarType: infoToSymbol("ScalarType", bootstrapWkt),
MethodKind: infoToSymbol("MethodKind", bootstrapWkt),
Expand Down