mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-12 16:00:59 +00:00
[TS/JS] BigInt implementation (#6998)
* BigInt implementation * Unit test reading long from existing bytebuffer * Code review
This commit is contained in:
@@ -291,8 +291,7 @@ class TsGenerator : public BaseGenerator {
|
||||
|
||||
std::string GenBBAccess() const { return "this.bb!"; }
|
||||
|
||||
std::string GenDefaultValue(const FieldDef &field, const std::string &context,
|
||||
import_set &imports) {
|
||||
std::string GenDefaultValue(const FieldDef &field, import_set &imports) {
|
||||
if (field.IsScalarOptional()) { return "null"; }
|
||||
|
||||
const auto &value = field.value;
|
||||
@@ -319,10 +318,7 @@ class TsGenerator : public BaseGenerator {
|
||||
|
||||
case BASE_TYPE_LONG:
|
||||
case BASE_TYPE_ULONG: {
|
||||
int64_t constant = StringToInt(value.constant.c_str());
|
||||
std::string createLong = context + ".createLong";
|
||||
return createLong + "(" + NumToString(static_cast<int32_t>(constant)) +
|
||||
", " + NumToString(static_cast<int32_t>(constant >> 32)) + ")";
|
||||
return "BigInt('" + value.constant + "')";
|
||||
}
|
||||
|
||||
default: return value.constant;
|
||||
@@ -348,7 +344,7 @@ class TsGenerator : public BaseGenerator {
|
||||
case BASE_TYPE_BOOL: return allowNull ? "boolean|null" : "boolean";
|
||||
case BASE_TYPE_LONG:
|
||||
case BASE_TYPE_ULONG:
|
||||
return allowNull ? "flatbuffers.Long|null" : "flatbuffers.Long";
|
||||
return allowNull ? "bigint|null" : "bigint";
|
||||
default:
|
||||
if (IsScalar(type.base_type)) {
|
||||
if (type.enum_def) {
|
||||
@@ -912,7 +908,7 @@ class TsGenerator : public BaseGenerator {
|
||||
// the variable name from field_offset_decl
|
||||
std::string field_offset_val;
|
||||
const auto field_default_val =
|
||||
GenDefaultValue(field, "flatbuffers", imports);
|
||||
GenDefaultValue(field, imports);
|
||||
|
||||
// Emit a scalar field
|
||||
const auto is_string = IsString(field.value.type);
|
||||
@@ -1232,7 +1228,7 @@ class TsGenerator : public BaseGenerator {
|
||||
if (is_string) { index += ", optionalEncoding"; }
|
||||
code += offset_prefix +
|
||||
GenGetter(field.value.type, "(" + index + ")") + " : " +
|
||||
GenDefaultValue(field, GenBBAccess(), imports);
|
||||
GenDefaultValue(field, imports);
|
||||
code += ";\n";
|
||||
}
|
||||
}
|
||||
@@ -1328,7 +1324,7 @@ class TsGenerator : public BaseGenerator {
|
||||
code += "false";
|
||||
} else if (field.value.type.element == BASE_TYPE_LONG ||
|
||||
field.value.type.element == BASE_TYPE_ULONG) {
|
||||
code += GenBBAccess() + ".createLong(0, 0)";
|
||||
code += "BigInt(0)";
|
||||
} else if (IsScalar(field.value.type.element)) {
|
||||
if (field.value.type.enum_def) {
|
||||
code += field.value.constant;
|
||||
@@ -1482,13 +1478,13 @@ class TsGenerator : public BaseGenerator {
|
||||
code += "0";
|
||||
} else if (HasNullDefault(field)) {
|
||||
if (IsLong(field.value.type.base_type)) {
|
||||
code += "builder.createLong(0, 0)";
|
||||
code += "BigInt(0)";
|
||||
} else {
|
||||
code += "0";
|
||||
}
|
||||
} else {
|
||||
if (field.value.type.base_type == BASE_TYPE_BOOL) { code += "+"; }
|
||||
code += GenDefaultValue(field, "builder", imports);
|
||||
code += GenDefaultValue(field, imports);
|
||||
}
|
||||
code += ");\n}\n\n";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user