chaokunyang commented on code in PR #3394: URL: https://github.com/apache/fory/pull/3394#discussion_r3062307690
########## compiler/fory_compiler/generators/javascript.py: ########## @@ -0,0 +1,1083 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you 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. + +"""JavaScript code generator.""" + +from pathlib import Path +from typing import Dict, List, Optional, Set, Tuple, Union as TypingUnion + +from fory_compiler.frontend.utils import parse_idl_file +from fory_compiler.generators.base import BaseGenerator, GeneratedFile +from fory_compiler.ir.ast import ( + Enum, + Field, + FieldType, + ListType, + MapType, + Message, + NamedType, + PrimitiveType, + Schema, + Union, +) +from fory_compiler.ir.types import PrimitiveKind + + +class JavaScriptGenerator(BaseGenerator): + """Generates JavaScript type definitions and Fory registration helpers from IDL.""" + + language_name = "javascript" + file_extension = ".ts" + + # JavaScript reserved keywords that cannot be used as identifiers + TS_KEYWORDS = { + "abstract", + "any", + "as", + "asserts", + "async", + "await", + "bigint", + "boolean", + "break", + "case", + "catch", + "class", + "const", + "continue", + "debugger", + "declare", + "default", + "delete", + "do", + "else", + "enum", + "export", + "extends", + "false", + "finally", + "for", + "from", + "function", + "get", + "if", + "implements", + "import", + "in", + "infer", + "instanceof", + "interface", + "is", + "keyof", + "let", + "module", + "namespace", + "never", + "new", + "null", + "number", + "object", + "of", + "package", + "private", + "protected", + "public", + "readonly", + "require", + "return", + "set", + "static", + "string", + "super", + "switch", + "symbol", + "this", + "throw", + "true", + "try", + "type", + "typeof", + "undefined", + "unique", + "unknown", + "var", + "void", + "while", + "with", + "yield", + } + + # Mapping from FDL primitive types to JavaScript types + PRIMITIVE_MAP = { + PrimitiveKind.BOOL: "boolean", + PrimitiveKind.INT8: "number", + PrimitiveKind.INT16: "number", + PrimitiveKind.INT32: "number", + PrimitiveKind.VARINT32: "number", + PrimitiveKind.INT64: "bigint | number", + PrimitiveKind.VARINT64: "bigint | number", + PrimitiveKind.TAGGED_INT64: "bigint | number", + PrimitiveKind.UINT8: "number", + PrimitiveKind.UINT16: "number", + PrimitiveKind.UINT32: "number", + PrimitiveKind.VAR_UINT32: "number", + PrimitiveKind.UINT64: "bigint | number", + PrimitiveKind.VAR_UINT64: "bigint | number", + PrimitiveKind.TAGGED_UINT64: "bigint | number", + PrimitiveKind.FLOAT16: "number", + PrimitiveKind.BFLOAT16: "number", + PrimitiveKind.FLOAT32: "number", + PrimitiveKind.FLOAT64: "number", + PrimitiveKind.STRING: "string", + PrimitiveKind.BYTES: "Uint8Array", + PrimitiveKind.DATE: "Date", + PrimitiveKind.TIMESTAMP: "Date", + PrimitiveKind.DURATION: "number", + PrimitiveKind.DECIMAL: "bigint", + PrimitiveKind.ANY: "any", + } + + # Mapping from FDL primitive types to Fory JS runtime Type.xxx() calls + PRIMITIVE_RUNTIME_MAP = { + PrimitiveKind.BOOL: "Type.bool()", + PrimitiveKind.INT8: "Type.int8()", + PrimitiveKind.INT16: "Type.int16()", + PrimitiveKind.INT32: "Type.int32()", + PrimitiveKind.VARINT32: "Type.varInt32()", + PrimitiveKind.INT64: "Type.int64()", + PrimitiveKind.VARINT64: "Type.varInt64()", + PrimitiveKind.TAGGED_INT64: "Type.sliInt64()", + PrimitiveKind.UINT8: "Type.uint8()", + PrimitiveKind.UINT16: "Type.uint16()", + PrimitiveKind.UINT32: "Type.uint32()", + PrimitiveKind.VAR_UINT32: "Type.varUInt32()", + PrimitiveKind.UINT64: "Type.uint64()", + PrimitiveKind.VAR_UINT64: "Type.varUInt64()", + PrimitiveKind.TAGGED_UINT64: "Type.taggedUInt64()", + PrimitiveKind.FLOAT16: "Type.float16()", + PrimitiveKind.BFLOAT16: "Type.bfloat16()", + PrimitiveKind.FLOAT32: "Type.float32()", + PrimitiveKind.FLOAT64: "Type.float64()", + PrimitiveKind.STRING: "Type.string()", + PrimitiveKind.BYTES: "Type.binary()", + PrimitiveKind.DATE: "Type.date()", + PrimitiveKind.TIMESTAMP: "Type.timestamp()", + PrimitiveKind.DURATION: "Type.duration()", + # DECIMAL is not yet supported by the JS runtime; omitted intentionally. Review Comment: The generated TypeScript surface says `decimal` becomes `bigint`, but the runtime map intentionally omits `DECIMAL`, so `_field_type_expr()` falls back to `Type.any()`. Generating `message Money { decimal amount = 1; }` currently emits `amount: bigint;` plus `amount: Type.any().setId(1)`, which means runtime serialization uses data-dependent `any` semantics instead of the schema's decimal type. This generator should either reject `decimal` for JavaScript until the runtime supports it, or add real decimal support end to end. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
