Compare commits

..

533 Commits

Author SHA1 Message Date
Derek Bailey
d0cede9c90 Bumping to Version 2.0.5 (#6967) 2021-12-09 10:31:57 -08:00
David Sanders
fcc2bee0b6 Fix typo in variable name (#6973) 2021-12-09 10:30:15 -08:00
Derek Bailey
e08da4dea2 remove Appveyor's Vs2010 and 13 builds (#6969) 2021-12-06 14:18:22 -08:00
tira-misu
18538c401c [TS] Fix reserved words as arguments (#6955) (#6956)
* Fix C/C++ Create<Type>Direct with sorted vectors

If a struct has a key the vector has to be sorted. To sort the vector
you can't use "const".

* Changes due to code review

* Improve code readability

* Add generate of JSON schema to string to lib

* option indent_step is supported

* Remove unused variables

* Fix break in test

* Fix style to be consistent with rest of the code

* [TS] Fix reserved words as arguments (#6955)
2021-12-06 14:16:04 -08:00
Derek Bailey
e57f4ab2d2 Update stale bot version and provide away to exempt issues with the not-stale label (#6968) 2021-12-03 13:51:05 -08:00
Derek Bailey
deed68db5d missing generated files (#6966)
These shouldn't be used, so short circuiting CI.
2021-12-03 11:23:58 -08:00
Derek Bailey
061d61f3f8 Lua Generator using IR. (#6940)
* initial hack to get new Lua generator into flatc

* Starting to output enum defs for Lua

* Continue to work on table generation for Lua

* Finished basic getter access for Lua

* Added ability to get object by index

* Finished struct builder

* aliased reflection to r

* finish table builder generation

* register requiring files

* better generated header info

* Tying up loose ends

* Updated reflection to handle struct padding

* Addd type sizes to reflection

* Fixed some vector indirect issues

* Lua tests passed

* Misc cleanup

* ci fixes 1

* ci fixes 2

* renaming

* up size of type sizes

* manually ran clang-format-11 -i src/idl_parser.cpp

* fixed some windows casting

* remove stupid auto import

* more static_casting

* remove std

* update other build environments

* remove scoped enums

* replaced std::to_string with NumToString

* more win fixes

* more win fixes

* replaced old lua with new

* removed auto import

* review responses

* more style fixes

* refactor bfbs_gen_len to use code +=

* added consts

* fix lambda capture for windows

* remove unused return type
2021-12-02 21:29:19 -08:00
Markus Junginger
cffe0c4546 FixedTypedVector: add const to ElementType() and size() (#6965) 2021-12-02 20:32:51 -08:00
Panagiotis Gourgaris
fadd40e402 Have grpc include file with correct filename-suffix given to flatc (#6954)
When generating code with --grpc,  --cpp and using filename-suffix, the generated grpc files where not including the correct header that had the filename-suffix. As a suffix, they used the default "_generated".
Free functions for these were used to get the suffix. FlatBufFile had such methods, but also needed to be into its base File and use these.
- grpc generated files include the correct message header.
- grpc generated files also have the suffix
- grpc generated cc file does not include initial message header
2021-12-02 09:49:12 -08:00
Derek Bailey
e47dc0e465 Removed test/generate_code.{sh|bat} (#6873)
* removed test/generate_code.{sh|bat}

remove c++0x from generate_code.py

added check generate code script in python

add windows specific call

added flags to generate_code.py

Set c++-0x on BUILD_LEGACY

Skip generating monster_extra if requested

* added option to skip monster extra

* add conditional to skip 2010 check gen
2021-11-30 23:13:24 -08:00
Derek Bailey
5c54754790 switched to windows-lastest, removed deadcode (#6948) 2021-11-30 23:12:57 -08:00
engedy
4f7f6dc301 Restore FlatBufferBuilder::kFileIdentifierLength. (#6960)
Restore flatbuffers::FlatBufferBuilder::kFileIdentifierLength, which was a documented part of the public API, but the identifier was lost during the refactoring effected by comment commit 6c8c291559.
2021-11-30 23:06:19 -08:00
Wouter van Oortmerssen
0fadaf391d Enable verifier on nested_flatbuffers 2021-11-29 15:05:59 -08:00
Wouter van Oortmerssen
4d0e9a8706 Turn off nested FlatBuffers/FlexBuffers for the fuzzer
this is a temporary fix to allow the fuzzer to work until the real fix of adding a verifier for this is added.
2021-11-29 09:19:29 -08:00
Derek Bailey
c05f6783a6 invalid conditional running genrate_code.py (#6952) 2021-11-23 22:32:06 -08:00
Élie ROUDNINSKI
a14f4052cf rust: remove needless borrow (#6922)
This was discovered by running clippy.
2021-11-23 10:46:56 -06:00
Derek Bailey
9e4ca857b6 Vector Downward GetSize optimization (#6925)
* Added Google benchmarks (and gtests)

* Separate benchmark CMakeLists.txt to its own file

* Move output directory to target just flatbenchmark

* Reduced from encoding 210ns -> 188ns

* store size_ as uoffset_t

* fixed windows c4267 warning
2021-11-22 19:50:42 -08:00
jonathan-conder-sm
a2b99084b4 Fix flexbuffers clang-analyzer warning (#6947) 2021-11-22 13:37:36 -08:00
Sergei Trofimovich
85b4effac6 test: fix undefined order of functio parameters. (#6946)
Detected instability when built `flatbuffers-2.0.0` on `gcc-12`:

    [ 75%] Building CXX object CMakeFiles/flattests.dir/tests/test_builder.cpp.o
    .../c++/12.0.0/bits/shared_ptr_base.h:397:45: error: 'size' may be used uninitialized [-Werror=maybe-uninitialized]
      397 |       explicit _Sp_ebo_helper(_Tp&& __tp) : _M_tp(std::move(__tp)) { }
          |                                             ^~~~~~~~~~~~~~~~~~~~~~
    In file included from flatbuffers/tests/test_builder.cpp:1:
    flatbuffers/tests/test_builder.h: In function 'void builder_move_assign_after_releaseraw_test(Builder) [with Builder = flatbuffers::FlatBufferBuilder]':
    flatbuffers/tests/test_builder.h:63:10: note: 'size' was declared here
       63 |   size_t size, offset;
          |          ^~~~
    ...
    In file included from flatbuffers/tests/test_builder.cpp:1:
    flatbuffers/tests/test_builder.h: In function 'void builder_move_assign_after_releaseraw_test(Builder) [with Builder = GrpcLikeMessageBuilder]':
    flatbuffers/tests/test_builder.h:63:10: note: 'size' was declared here
       63 |   size_t size, offset;
          |          ^~~~
    cc1plus: all warnings being treated as errors

Here is the relevant bit of test:

    template<class Builder>
    void builder_move_assign_after_releaseraw_test(Builder b1) {
      auto root_offset1 = populate1(b1);
      b1.Finish(root_offset1);
      size_t size, offset;
      std::shared_ptr<uint8_t> raw(
          b1.ReleaseRaw(size, offset), [size](uint8_t *ptr) {
            flatbuffers::DefaultAllocator::dealloc(ptr, size);
          });

Note how `b1.ReleaseRaw(size, offset)` is expected to populate `size`
and `[size](uint8_t *ptr) {` captures the result. But both are parameters
to the same function call and thus evaluation order is unspecified.
2021-11-22 12:14:31 -08:00
Denis Protivensky
790f411542 [cmake] Fix getting version info from tags during the build (#6936)
Use `git describe --tags` to query for lightweight tags along with
the annotated ones.
2021-11-22 11:53:50 -08:00
Derek Bailey
e89de4411d clang-all (#6941) 2021-11-19 17:01:48 -08:00
Ivan Dlugos
256ab3798d WIP: Dart release 2.0 (#6927)
* chore: make flatc artifacts from CI executable

* chore: prepare dart 2.0.0 release

* refactor: update description in pubspec.yaml to make pub.dev happy

"The package description is too long.
Search engines display only the first part of the description. Try to keep the value of the description field in your package's pubspec.yaml file between 60 and 180 characters."
2021-11-18 10:56:29 -08:00
Derek Bailey
a59288a019 Add benchmarks to Linux CI (#6928) 2021-11-18 10:55:57 -08:00
Taiju Tsuiki
587bbd49a7 [C++] Fix compile failure on Object API union construction for struct member (#6923)
* Add dedicated traits to Object API version of unions.

* Add suppression for unused parameters on unions of structs.
2021-11-18 10:55:11 -08:00
Wouter van Oortmerssen
a9c341545f Fix integer overflow warnings 2021-11-18 09:41:44 -08:00
Derek Bailey
8aa18b6291 fixed cmp0048 error (#6932) 2021-11-17 20:22:23 -08:00
Catena cyber
d727579b6d fuzzers: fix typo in file path (#6931) 2021-11-17 14:43:56 -08:00
Derek Bailey
97a30171cb Added Raw C++ benchmarks (#6924) 2021-11-15 15:19:03 -08:00
Derek Bailey
0989fc5e59 Added Google benchmarks (and gtests) (#6920)
* Added Google benchmarks (and gtests)

* Default building benchmarks to OFF as it requires c++11

* Separate benchmark CMakeLists.txt to its own file

* Move output directory to target just flatbenchmark
2021-11-15 13:41:22 -08:00
taroplus
927175ea20 [Java] lookup by byteArray is giving back wrong entry (#6915) 2021-11-15 11:16:45 -08:00
Wouter van Oortmerssen
6748c373be Removal of support for FLATBUFFERS_CPP98_STL (#6918)
* Removal of support for FLATBUFFERS_CPP98_STL

For context, see https://github.com/google/flatbuffers/issues/6704

* Also removed vector_data
2021-11-12 14:01:11 -08:00
Derek Bailey
3fab0c6ee4 move [[ ]] to calling sites (#6914) 2021-11-11 10:31:15 -08:00
Derek Bailey
6c8c291559 [C++] Split flatbuffers.h into separate files (#6868)
* split flatbuffers.h into separate files

* wrong variable in cmakelists for android

* readded two accidentally deleted includes

* created buffer.h and moved buffer related things over
2021-11-10 22:26:09 -08:00
lu-wang-g
fd4ff23da0 Keep methods with struct name and switch them to default (#6879)
* Keep methods with struct name and switch them to default

This PR can help fix the following two issues:
1): A set of simplified API (without struct name) was added in
https://github.com/google/flatbuffers/pull/6336. It causes name
conflict when merging all generated python file into a single one
(the primary usage senario in Google).
2): Flatbuffers 2.0 generates absolute import path, which may cause
name space conflicts. See more details in
https://github.com/google/flatbuffers/issues/5840.

The solution for both is to generate the merged Python code, similar
C++. The merged code will not contain the simplied API, but only the
method with struct name. For issue (1), it will mimic the exactly
usage pattern for Google internal. For issue (2), users can generate
the merged flatbuffer code, without worrying about the imports.

The above idea will be implemented in the following steps:
Step 1 (this PR): revert changes in https://github.com/google/flatbuffers/pull/6336
that set the simplified API as default. Remove statements that the
original API will be deprecated, and reset the original API as default.
Step 2 (the following PR): create a flag to generate the merged code.
The Simplified API will be removed from the merged code, otherwise it
will cause name conflict.

* Update the generated sample code

* Update the generated example code

* Reverst the changes of GetRootAs

* Update examples from grpc/example/generate.sh
2021-11-04 15:12:25 -07:00
Tom Miller
f8b69e3308 [Python] Verify EnumDef is not generated in GenUnionCreator (#6903)
* [Python] Verify EnumDef is not generated in GenUnionCreator

* update test output
2021-11-04 14:21:31 -07:00
Alec Theriault
c0394bb09c Support --conform for vector of unions (#6883)
It seems like `--conform` already works for vectors of unions - there is
just a spurious check that prevents it from running. Fixes #6882

Also, if schemas do not conform, `flatc` no longer prints out the usage
(since the error is not due to bad usage). Fixes #6496
2021-11-01 11:26:36 -07:00
Even Rouault
8433eb108c Typo fixes in comments of flatbuffers.h and flexbuffers.h (#6901) 2021-10-28 13:03:03 -07:00
Fraser Crossman
f2f9380c86 [Java] Prevent generation of enum names when underlying type is long (#6781) (#6895) 2021-10-18 15:18:36 -07:00
xuyatian
e672dabfea [C++] Fix memory leak when using grpc (#6896) (#6897) 2021-10-18 11:24:52 -07:00
Brian King
9a4ca2764c Output errors / warnings on stderr instead of stdout (#6881) 2021-10-07 14:58:10 -07:00
mustiikhalil
4c7a9c10d3 Adds JSON encoding to swift (#6874)
Updates generated code & removes unneeded protocol

Updates cpp to only generate code when flag is passed

Updates code gen script
2021-10-07 23:22:22 +02:00
Paulo Pinheiro
38295a1873 [Kotlin] Fix missing escape for struct construction statements (#6877)
An escaping was missing leading to wrong generation when using
kotlin keywords.
2021-10-07 13:00:07 -07:00
Derek Bailey
31bb0b9726 add working directory (#6878) 2021-10-05 18:24:00 -07:00
T Sprecher
45e5642e91 Default Arguments for Mutators C++ [Updated] (#6872)
* CPP Default Value Generation in Mutators

If the mutator is for a value that is compatible with having a default value, then the single parameter becomes a default parameter. With this, a value can be mutated to it's default value without storing the default value, as that will be stored with the mutate function.
Fixed Casting When Generating Default for Enum Value

Added support for typecasting an int default value into the correct enum type in the default parameter. This fixed the issue of trying to use set a strongly typed enum parameter to an int which fails type checking.
Fixed Boolean Edge Case

Boolean types generate 0 != 0 when generating the underlying type which appears to be unique to the boolean type so it is now checked and the proper default value generated. It may be beneficial to check if it is instead an enum type, however the seeming edge case nature is why boolean was chosen to be checked.
Updated Generated Files

Regenerated the auto generated files to reflect the new changes.
Updated Remaining Files

Should fix auto generated header files that were not updated.

* Unified Repeated Code

Relocated identical append code to outside of conditional. Also changed 'casted' default value name from FIELD to INTERFACE to more accurately describe it.

* Moved Field Name Outside Conditional

Removed duplicate _{{FIELD_NAME}} and moved to unified append.
2021-09-30 16:44:39 -07:00
Derek Bailey
a592f4c89e [Cmake] Add a post build command after flatc to run the new generate_code.py script. (#6866)
* starting to add python script

* finish first draft of script

* add windows-specific edits

* Add cmake post flatc build generated_code.py command

* fixed windows issue

* Provided flatc location to generate_code.py

* Allow relative flatc pathing and fix macro typo

* escape post build arguments

* change script and args quoting

* skip running generate_code.py on old MSVC compilers
2021-09-30 12:38:52 -07:00
Ivan Dlugos
b9d43a557c dart - add Packable interface (#6846) 2021-09-28 13:23:30 -07:00
Wouter van Oortmerssen
2ece9e25b4 Bumped package.json to 2.0.4
See https://github.com/google/flatbuffers/pull/6850 for context
2021-09-27 15:49:45 -07:00
Yesudeep Mangalapilly
2dd2126379 Fix linker errors on (Free|Open)BSD. (#6860)
* Fix linker errors on (Free|Open)BSD.

* Adds a TODO for the FreeBSD linker flags: -lm and moves platform config to main BUILD.bazel
2021-09-27 12:07:04 -07:00
mustiikhalil
f63c130c28 Improves documentation, and adding DocC (#6784)
Finished documenting flatbuffersbuilder

Replaces swift 5.5 with 5.2 packages

Starts building the tutorial for xcode 13

Finishes building the tutorial for xcode 13

Removes docc files from old swift versions

Updates swift style guide
2021-09-27 20:59:19 +02:00
Derek Bailey
e2b26ee19b Make and generate_code.sh generate same outputs (#6855)
* added --bfbs-builtins

* update generate_code.bat

* forgot the .

* updated checking scripts

* added bypass for the monster_test.bfbs and arrays_test.bfbs diff issue

* removed check on windows for now
2021-09-24 12:01:34 -07:00
Louis DeScioli
9d686bf433 Include subdirectories in published npm package (#6850)
Updates the `files` globs in package.json to include subdirectories in order to also include the flexbuffers directory in the published npm package.

Updates .gitignore to include the tsc-generated JS files.
2021-09-23 16:22:49 -07:00
Louis DeScioli
1d26daff3e Use ArrayBuffer type for JS flexbuffer's toObject (#6851) 2021-09-23 16:22:13 -07:00
Ivan Dlugos
e72e18d9fd feat(build): compile "universal" libraries and executables for Mac (#6852)
to support both x86_64 and arm64 with a single artifact
2021-09-23 12:36:20 -07:00
Derek Bailey
354d97f6da fixed errant itemgroup element in C# test project (#6854)
errant save
2021-09-21 15:01:29 -07:00
Todd Hansen
6f7a57eaa0 [C#] Using 'global::' as qualifying_start_ within BaseGenerator (#6767)
* Update idl_gen_csharp.cpp

Change csharp generator to use "global::" for it's qualifying_start_ to disambiguate namespaces

* regenerate testing files

regenerate testing files

* Missed TableInC.cs

updated with global prefix

* Remove "global::" from qualifying_start_ for csharp generator

* C# global alias

* Tests and docs for --cs-global-alias

Add tests for --cs-global-alias to demonstrate use case for why it's needed.

Add documentation to Compiler.md

* Add also to help text

Add also to help text
2021-09-15 16:57:29 -05:00
Ivan Dlugos
90baa1444b Dart: binary lists (typed_data) (#6839)
* Dart - add eager mode to Uint8ListReader

* Dart - add Int8ListReader

* Dart - use binary reader where useful

* Dart - test binary list reader laziness

* Dart - update generated code
2021-09-15 08:50:57 -07:00
Casper
0a3b017f09 Delete IDLOptions::lang (#6841)
Co-authored-by: Casper Neo <cneo@google.com>
2021-09-13 18:18:00 -04:00
Artavazd Balaian
47d35f1053 BugFix: Optional enum when it is null (#6835)
* Test to make sure optional enum is written properly

* Handle optional enum codegen: when cast optional enum add `?`

* Run `tests/generate_code.sh` to generate code from schema

* Fix type casting in case of CreateXXXTypeVector

* Reason why vector's type is not optional
2021-09-10 12:55:46 -04:00
Derek Bailey
8fb8c2ce1d [C#] Use @ for keyword escaping (#6834) 2021-09-09 10:29:27 -07:00
Maksim Ivanov
1d063d87cf [C++] Let builder accept custom-alloc std::vector (#6814)
Change the FlatBufferBuilder's methods to accept std::vector parameters
with non-default allocator, by adding another template parameter to
them. This should make using the builder slightly more convenient, as
one won't need to manually pass data() and size() separately.
2021-09-08 13:25:06 -07:00
Herr Frei
338331b55b Changes to support binary schema file loading and parsing (flatc) (#6823)
* Changes to support binary schema file loading and parsing (flatc)
- parser.reset() is also called if binary schema file is given
- code flow changed to not try to load a binary schema as textual schema

* Removed unneeded braces
2021-09-08 13:19:00 -07:00
Rafal Kapuscik
156a30c753 Move Apple-specific build flags to Clang section (#6802)
This allows building FlatBuffers with gcc on macOS:
- avoid linking libc++ when not using clang
- look at compiler first, then OS-specific options

Signed-off-by: Rafal Kapuscik <rkapuscik@antmicro.com>
2021-09-08 13:14:53 -07:00
schoetbi
550d2f9048 Issue error if jsonschema has no root type defined #6821 (#6831) 2021-09-02 14:24:47 -07:00
SaintMalik
c3a38242e6 Fix typos in docs (#6829) 2021-09-02 11:38:45 -04:00
Tyler Denniston
74c3d7eba2 [C++] Add mutable version of LookupByKey and test (#6826)
* [C++] Add mutable version of LookupByKey and test

This adds an overload of LookupByKey to allow lookup in sorted Vectors to
return a mutable instance of the object (or nullptr if not found).

* Fix naming
2021-08-31 17:17:20 -04:00
Casper
db6eae5c42 Add html and latex to gitignore (#6819)
Co-authored-by: Casper Neo <cneo@google.com>
2021-08-30 16:36:00 -07:00
Björn Harrtell
0e9d79c355 [Java] Avoid casting ByteBuffer to Buffer (#6785)
* Revert "avoiding even more NoSuchMethod exceptions (#6729)"

This reverts commit 6fb2c90d9e.

* Revert "avoiding more NoSuchMethod exceptions (#6671)"

This reverts commit 752c7b576d.

* Revert "avoiding NoSuchMethod exception (#6658)"

This reverts commit 813d3632ec.

* Use Java 8 for Kotlin Linux builds to verify
2021-08-30 15:51:06 -07:00
Stefan F
b20c4d3aad [C++] Add GetMutableSizePrefixedRoot and generate GetMutableSizePrefixedXxx functions (#6815)
* flattests_cpp17 doesn't compile with Visual Studio 2017: warning C4100: 'indent': unreferenced formal parameter

stringify_util.h(127): error C2220: warning treated as error - no 'object' file generated
stringify_util.h(127): warning C4100: 'indent': unreferenced formal parameter
stringify_util.h(85): warning C4100: 'indent': unreferenced formal parameter
stringify_util.h(85): warning C4100: 'fbs': unreferenced formal parameter

* [C++] Add GetMutableSizePrefixedRoot() and generate a GetMutableSizePrefixed function
When using the mutable API together with size prefixed buffers these functions should be present.

* clang-format

* Cleanup branch for PR
Revert "flattests_cpp17 doesn't compile with Visual Studio 2017: warning C4100: 'indent': unreferenced formal parameter"

This reverts commit a92055203e.
2021-08-30 15:34:42 -07:00
Stefan F
ae145293cc flattests_cpp17 doesn't compile with Visual Studio 2017: warning C4100: 'indent': unreferenced formal parameter (#6811)
stringify_util.h(127): error C2220: warning treated as error - no 'object' file generated
stringify_util.h(127): warning C4100: 'indent': unreferenced formal parameter
stringify_util.h(85): warning C4100: 'indent': unreferenced formal parameter
stringify_util.h(85): warning C4100: 'fbs': unreferenced formal parameter
2021-08-30 15:32:36 -07:00
Ivan Dlugos
e3c76a5cde Dart lints (#6808)
* Dart - add analysis options with the recommended lints

* Dart - generate code without linter issues

* Dart - remove linter issues in example and test code

* Dart - remove linter issues in lib code

* Dart - update generate code in /tests/
2021-08-30 12:54:59 -07:00
Casper
4b9123baff fix parser string=null (#6810)
Co-authored-by: Casper Neo <cneo@google.com>
2021-08-24 09:41:39 -04:00
Casper
f89e0b1a6c Refactor idl_gen_rust and Rust generated code to use consistent whitespace (#6809)
* Remove dead code in idl_gen_rust

* Use 2space indentation in mod.rs

* use In/DecrementIdentValue in idl_gen_rust

Fix some whitespace too in generated code.

* make default fn 2space ident

* More 2space formatting

* git clang format

* make vs2015 happy

Co-authored-by: Casper Neo <cneo@google.com>
2021-08-23 18:53:10 -04:00
Casper
273f6084e5 Fix Rust keyword inconsistency (#6794)
* Make Rust keyword escaping more consistent

* fix comment

* git clang format

Co-authored-by: Casper Neo <cneo@google.com>
2021-08-18 15:37:44 -04:00
Todd Hansen
4a0879458d [C#] Remove superfluous semicolon in C# structs (#6791)
Aims to resolve #6788

-Remove superfluous semicolon in C# structs
2021-08-17 11:02:57 -04:00
Casper
d6f51ea16d fix ptr in flatbuffers cpp tutorial (#6787)
Co-authored-by: Casper Neo <cneo@google.com>
2021-08-16 14:43:26 -04:00
Todd Hansen
067dce6e79 [C#] Handle keywords (#6775)
* Handle keywords in C#

C# should handle keywords similarly to other languages by appending an underscore after the keyword.

* Fix clang-format for idl_gen_csharp.cpp

* Fix spacing clang-format issue
2021-08-16 14:09:21 -04:00
Todd Hansen
2ca5f0e72b [C#] Respect command line parameter for file name extension and suffix (#6779)
* Make CSharp use the command line parameter for file name extension and suffix

* Default file name now has _generated in it,
so we need to rename all the generated test files

tests/namespace_test/NamespaceA/TableInC.* are no
longer used, so these can be deleted

* Fix C# testing projects so that they reference
the new _generated.cs files

* Revert "Fix C# testing projects so that they reference"

This reverts commit cf62b9c5d4.

* Revert "Default file name now has _generated in it,"

This reverts commit 042aa81f10.

* Revert "Make CSharp use the command line parameter for file name extension and suffix"

This reverts commit ba8cd06646.

* Make CSharp use the command line parameter for file name extension and suffix

* Fixup clang tidy

* Fix more clang-tidy
2021-08-15 15:51:58 -04:00
Wouter van Oortmerssen
f20d2253ae update Java version to 2.0.3 2021-08-14 09:01:54 -07:00
Paulo Pinheiro
390d438e2a [Kotlin] Fix CI by changing compiler to Java 11 (#6783)
With the changes introduced on #6729, #6671, #6658 it seems that using java
compiler version 8 is no longer possible. We are adjusting our CI build for
Kotlin to use Java 11 as compiler, while still emiting 1.8 bytecode.

The Kotlin CI action is also being breakdown into two: Mac & Linux.
Kotlin mac will test ios & mac builds while Linux will test js and JVM.
This change will improve build times for Kotlin on CI, which is currently
taking long times.
2021-08-14 08:57:38 -07:00
Kamil Rojewski
6fb2c90d9e avoiding even more NoSuchMethod exceptions (#6729)
* avoiding more NoSuchMethod exceptions

refs #6657

* avoiding even more NoSuchMethod exceptions

refs #6657
2021-08-05 12:25:23 -07:00
Björn Harrtell
a7b527d942 java: Use maven profile to target Java 8 bytecode (#6764) 2021-08-05 11:44:13 -07:00
bakinovsky-m
5d77820b34 [C++] Using calculated fields_number for field_names size (#6769)
* using already defined "fields_number" in "field_names" size calculation

* tests

* forgotten test
2021-08-05 11:41:49 -07:00
RWauson
909ce970ae [C++] Use UnPackTo instead of UnPack if pointer exists (#6725)
Causes generated code to check for existing pointers in an object (when using the object based api) and use UnPackTo(existingPointer) rather than just using UnPack() to replace the pointer. This is a performance issue when unpacking to existing objects when using large buffer fields (e.g. image frames)

Co-authored-by: RobWauson <robwauson@gmail.com>
2021-08-05 11:40:03 -07:00
Casper
35e2cac6eb Store vtables sorted in Rust builder (#6765)
* benchmark many vtables

* Rust: Store written_table rev-positions sorted.

The previous implementation was slow if there were too many tables.

Asymototically when inserting the n^th vtable: The old implementation
took O(n) lookup steps and O(1) insertion. The new implementation is
O(log n) lookup and O(n) insertion. This might be improved further by
using a balanced btree.

Benchmarking, create_many_tables is 7.5x faster (on my laptop):

// Simple vector cache
test create_many_tables ... bench: 728,875 ns/iter (+/- 12,279) = 44 MB/s

// Sorted vector cache
test create_many_tables ... bench: 97,843 ns/iter (+/- 4,430) = 334 MB/s

* Fix lints

Co-authored-by: Casper Neo <cneo@google.com>
2021-08-03 12:31:45 -07:00
mogemimi
c39fc9dd9c [C++] Use nullptr instead of 0 in generated headers (#6762) 2021-08-02 13:41:15 -07:00
mogemimi
775c6567d1 [C++] Fix compiler warning -Wredundant-parens in clang (#6761) 2021-08-02 13:40:17 -07:00
Ivan Dlugos
97d9527f6c Dart 2.0 release prep (#6759)
* Dart - pubspec.yaml shouldn't contain authors section anymore - it's unused

* Dart - update CHANGELOG.md

* Dart - update test and publish scripts
2021-08-02 13:31:36 -07:00
Esun Kim
e012054667 [C++] Use proper gRPC C++ API when using MemoryBuffer Slice (#6756)
* Use ByteBuffer

* Use Slice

* Upgrade gRPC to 1.39.0

* Update gRPC build script

* Reformat

* Added CMAKE_CXX_STANDARD=11 when building Abseil

* Enabled ABSL_ENABLE_INSTALL

* Added absl to grpctest deps
2021-07-29 10:41:51 -07:00
Ivan Dlugos
5235133f32 Dart - make ascii optimization optional in StringReader, same as in writeString() (#6758) 2021-07-29 10:39:03 -07:00
Ivan Dlugos
c871df7702 Dart - change table building to assert() instead of exceptions (#6754) 2021-07-26 11:42:42 -07:00
Ivan Dlugos
a6eeeb3b9c Dart - inline small functions to increase performance (#6755)
~25 % for reading
~30-35 % for writing
2021-07-26 10:09:46 -07:00
tira-misu
3dd02144d5 [C#] Fix field name struct name collision (#6744) (#6757)
* Fix C/C++ Create<Type>Direct with sorted vectors

If a struct has a key the vector has to be sorted. To sort the vector
you can't use "const".

* Changes due to code review

* Improve code readability

* Add generate of JSON schema to string to lib

* option indent_step is supported

* Remove unused variables

* Fix break in test

* Fix style to be consistent with rest of the code

* [C#] Fix field name struct name collision (#6744)
2021-07-26 10:02:27 -07:00
mugisoba
e77926f0ed [C#] Add Union Constructor Utility for ObjectAPI. (#6105)
* [C#] add union constructor utility.

* add union utility test.

* std::tolower -> CharToLower

* use std::transform instead

* rebase to master. and regenerate test code
2021-07-26 10:01:41 -07:00
Ivan Dlugos
ac23482022 Dart - fixup writeString() return type after previous merges (#6753) 2021-07-26 09:54:13 -07:00
Taiju Tsuiki
8be8a0a713 [C++] Fix union type handling on object api in C++17 (#6745) 2021-07-25 16:20:34 +07:00
Casper
12e341e4f8 Rework how Rust generated files are laid out (#6731)
* Refactored Rust Generated code into a module directory.

Each symbol will be generated into one file and then
imported into a module. This breaks the "out_dir"
pattern where some users would generate code in their
target/ directory. Also, these objects are best used
in their own module. It will be hard for users to share
their own module structure with flatbuffers namespaces.

There may be solutions to these drawbacks but that should
be discussed. I don't want to overengineer here.

* shadow error

* try fix .bat file

* fix .bat 2

* Restore accidentally deleted files

* Fixed some DONOTSUBMITs and made Rust outdir pattern use symlinks.

* fixed binary files

* git clang format

* make generated onefiles not public and fix .bat

* reduced diff with master in generate_code.sh

* fix shadowed variable

* add object api flags to .bat

* space

* Removed extern crate and extra &

* use statement

* more clippy lints

* format

* Undo extern crate -> use change, it actually matters to our tests

Co-authored-by: Casper Neo <cneo@google.com>
2021-07-22 19:02:28 -04:00
Ivan Dlugos
c36672d803 Dart - optimize writeString for ASCII (#6736)
* Dart - optimize writeString for ASCII

* Dart - writeString() ASCII optimization optional
2021-07-22 10:36:37 -07:00
Ivan Dlugos
2bfc8e9f01 Dart - make writeString() argument non-nullable (#6737) 2021-07-20 07:54:48 -07:00
Casper
674a9f2aae fix lints in rust tests (#6743)
Co-authored-by: Casper Neo <cneo@google.com>
2021-07-19 18:47:20 -04:00
Sam Sobell
016e6aa13f Add a split on ':' to build_defs.bzl (#6742) 2021-07-19 16:42:27 -04:00
Ivan Dlugos
65700441d3 Dart - make vTable fixed size (expect the number of fields when creating) (#6735) 2021-07-19 11:27:01 -07:00
Ivan Dlugos
dd5bb55cad Dart - make vTable deduplication optional (#6734) 2021-07-15 15:50:12 -07:00
Ivan Dlugos
e8423da1b7 Dart - regenerate example after recent flatc changes (#6733) 2021-07-15 13:03:27 -07:00
Shlomi Regev
0980e39c9b flexbuffers: Add variant of Blob() that takes a key (#6730) 2021-07-12 09:42:55 -07:00
Ivan Dlugos
e73fab27d4 Dart - store empty vectors instead of NULL (#6728) 2021-07-09 12:44:40 -07:00
Ivan Dlugos
92ae532e43 Dart - finish/lowFinish/buffer changes (#6712)
* Dart - change Builder "lowFinish()" to "buffer" and "finish()" to not void return

Aligning the API with other languages, e.g. c++ and allowing custom use-cases to avoid creating a Uint8List

* Dart - change builder.buffer to check that finish() was already called

* Dart - builder - move !finished assertion to _prepare() which is run from all other functions
2021-07-09 11:45:45 -07:00
Philipp Wollermann
7482b25f8b Remove ubuntu1604 from presubmit.yml (#6715)
* Remove ubuntu1604 from presubmit.yml

Ubuntu 16.04 is end-of-life, we're going to remove it from Bazel CI.

If you like you can add testing on `ubuntu2004` platform which we also support.

* Add ubuntu2004 to presubmit.yml
2021-07-09 09:59:30 -07:00
Ivan Dlugos
3f77dc9a0e Dart - unpack() must use eager list reader (#6723) 2021-07-09 09:55:34 -07:00
Ivan Dlugos
838c93b843 Dart - fix flex-builder compilation error (#6722)
* Dart - fix flex-builder compilation error

* DartTest.sh - run all tests
2021-07-09 09:53:27 -07:00
Ivan Dlugos
089f48a4a6 Dart - make sure added padding is zeroed, same as in C++ (#6716) 2021-07-09 09:06:40 -07:00
Daniil Nikolenko
bf3470c161 Fix UB in CreateVectorOfStructs in case of an empty vector: avoid calling memcpy in this case (#6726) 2021-07-08 13:40:53 -07:00
paulharris
8f8196e136 Fix 6348 (#6717)
* Fixup CPP documentation's markdown errors

Note that I couldn't get the ~~~{.cpp} method to work,
so I switched to using ```cpp which did work.

* Fixup C++ docs, typo in repeated code example

Co-authored-by: Paul Harris <paulharris@computer.org>
2021-07-08 13:29:00 -07:00
Ivan Dlugos
8ab35b2a5f Dart - add custom allocator support (#6711)
* Dart - add custom allocator support

* Dart - only copy written bytes during resize, not the whole old buffer.
2021-07-08 13:02:09 -07:00
jdees
c0ba2870c9 Fix typo in docu (#6714)
wether => whether
2021-06-30 14:55:56 -04:00
Vladimir Glavnyy
22498cf3a9 [C++] add make_span for Array<T,N> (#6663)
* [C++] Add make_span for Array<T,N> and Vector<T>

* [C++] Add tests for make_span<T,N>(Array<T,N>)

* [C++] Add span iterators

* [C++] Checked span iterator (MSVC)

* Fix review notes
2021-06-28 10:22:02 -07:00
pkasting
bd37e67ac0 [C++] Fix a -Wdeprecated-copy warning. (#6708)
* Fix incorrect assertion usage.

* Fix a -Wdeprecated-copy warning.

Bug: chromium:1221591

* Just remove constructors/assignment
2021-06-27 09:50:12 +07:00
Ivan Dlugos
a6ee335574 Dart null safety (#6696)
* Dart null-safety - prepare migration annotations in library files

* Dart null-safety - apply migration

* Dart null-safety - update flatc to generate valid code

* Dart null-safety - fix flatc generated code and adjust tests

* Dart null-safety - update example and the generated code in the tests folder

* Dart null safety - minor review changes

* Dart - apply `dartfmt -w .`
2021-06-22 08:54:57 -07:00
Casper
71d43f3be9 Make --bfbs-filenames default to location of first schema file. (#6705)
* Make --bfbs-filenames default to location of first schema file.

Make RelativeToProjectRoot always work, applying "../" where needed. This is
needed for backwards compatibility. The first input file may be deeper in some
directory than the other files. Now, there will always be a declaration
file.

* documentation

* clang format

Co-authored-by: Casper Neo <cneo@google.com>
2021-06-22 10:19:46 -04:00
schoetbi
c8db1ca5d4 Jsonschema add prop comments (#6617)
* Add comments for properties

* regenerated test files

* Reverted changes in testfiles generated with generate_code.bat on windows

* fixed formatting in util.cpp because of failed .travis/format_check.sh

* removed the use of tuple

* util.cpp: Fixed format

* idl_gen_json_schema.cpp: removed extra comma in deprecated fields

* Corrected monster_test.schema.json

* idl_gen_json_schema: Inlined Trim function

* Fixed format

* PrepareDescription(): Avoid unnecessary string copy

* fixed formatting
2021-06-21 11:42:07 -07:00
Mika Raento
962751a6ec Improve generated comparisons for tables (#6486)
* Improve generated comparisons for tables

Previously, the generated code called std::unique_ptr<>::operator== on
non-scalar members, which would be correct only if the member was null
on both sides. After this CL, comparison is done on the pointed-to
values (including using a default-constructed value if the pointer is
null).

* Don't equate null Tables with all defaults

Also removes the cost of default-constructing tables for comparisons.

* Regenerate code

* fix formatting
2021-06-21 11:37:56 -07:00
Casper
06fd6d640c Replace filenames in reflection with filenames+includes. (#6703)
* Replace filenames in reflection with filenames+includes.

This is needed for some use cases and may be just useful metadata.

* deser files_included_per_file_

* check project_root

* fix bazel

* git clang format

Co-authored-by: Casper Neo <cneo@google.com>
2021-06-19 19:21:33 -04:00
pkasting
acce4ac3f0 Fix incorrect assertion usage. (#6702) 2021-06-18 15:49:55 -04:00
Neil Girdhar
8fd7861b75 Fix warning about deprecated module: imp (#6362) 2021-06-17 13:02:22 -07:00
Casper
c58ae94225 Add the file a symbol is declared in to Reflection (#6613)
* Add the file a symbol is declared in to Reflection

If we move a code-generator to depend on Reflection,
it may need to know which file something was declared in
to properly name generated files.

* Doc comments in reflection, and more precise tests

* Add --project-root flag to flatc, normalize declaraion_file to this root

* fix --project-root stuff

* posixpath

* fix scripts

* format

* rename --project-root to --bfbs-filenames

Also, make it optional, rather than defaulting to `./`, if its not
specified, then don't serialize the filenames.

* bfbs generation

* fix some tests

* uncomment a thing

* add  to project root directory conditionally

* fix

* git clang format

* Added help description and removed != nullptr

* "

* Remove accidental change to docs

* Remove accidental change to docs

* Pool strings

Co-authored-by: Casper Neo <cneo@google.com>
2021-06-17 11:50:04 -04:00
Casper
2cf7bb7966 Intermediate Representation docs (#6685)
* Intermediate Representation docs

* minor rewording

* addressing comments

* move IR to bottom

Co-authored-by: Casper Neo <cneo@google.com>
2021-06-17 11:46:34 -04:00
Ivan Dlugos
4e3a66c141 Dart object API (#6682)
* dart test scripts - generate with `--gen-object-api`

* Dart object API, pack and unpack methods (WIP)

* Dart flatc - extract Builder code to separate functions to reuse it in Pack()

* Dart flatc - use builder field-building implementation in pack()

* Dart flatc - add pack() as an intance method of the "T" class

* Dart object API - make inner fields unpacked as well

* Dart object API - use pack() when collecting field offsets

* Dart object API - use packOnce() for fields that are structs or vectors of structs

* Dart object API - remove obsolete union support TODO

* Dart object API - minor review changes, test and fixes

* Dart object API - revert packOnce() - not supported by other object API implementations

* Dart object API - update docs

* update dart generated code in tests/ to fix CI failure on ./scripts/check-generated-code.sh

* Dart flatc - fix compilation for old MSVC and c++0x
2021-06-14 10:15:56 -07:00
Ivan Dlugos
d959e23208 dart - change Builder.reset() to reuse an existing buffer (#6661) 2021-06-13 08:07:08 -07:00
Omer Akram
a9fb540884 use improved versioning (#6691) 2021-06-11 15:27:59 -07:00
Ivan Dlugos
337eb8b8f7 update docs - structs can't be roots (#6690) 2021-06-11 15:20:10 -07:00
Russell Chreptyk
6415ef05d3 [Python] [Codegen] Fixes nested structs with underscore names (#6686)
Fixes #6648
2021-06-10 12:52:19 -07:00
Huw Rogers
f069396d1b [C++] flatc --cpp-field-case-style option to permit camel-case field names in C++ (#6669)
* flatc --cpp-field-case option to permit camel-case field names in C++

* fixed option name; cleaned up tabs

* formatting fixed to conform to CI

* resolved comments

* fixed white space indentation

* per PR comments

* rename snake case option to unchanged for clarity, per PR comments

* cleanup of unchanged case option in C++ codegen, per PR 6669 comments

* incorporated PR feedback from vglavnyy

* cleaned up to pass Travis CI / clang format

* bumped PR to retry transient CI failure

* bumped PR to retry transient CI failure

* bump PR

* assert union type field name length > suffix, per PR 6669 comments
2021-06-08 11:23:05 -07:00
Omer Akram
021177af0d Fix snap version formatting (#6683) 2021-06-07 09:16:13 -07:00
Alexander Dzyuba
e1e9f93739 [C++/grpc] added hiding of unused variables in the generated code (#6677)
Co-authored-by: Alexander Dzyuba <a.dzyuba@seraphim.online>
2021-06-06 17:40:56 +07:00
Wouter van Oortmerssen
15110094eb Fix GitHub Actions CI gcc/clang versions 2021-06-04 13:32:16 -07:00
Jared Duke
093badb0a0 Use unoptimized path for ReadUInt64 for win32 build as the optimized path crashes. (#6681)
FlexBuffers are used by custom op handlers for TFLite. Using the optimized path for ReadUInt64 in FlexBuffers causes a crash in models using custom ops on win32 build. This change fixes the problem by using unoptimized implementation of ReadUInt64 for win32.
2021-06-04 13:27:47 -07:00
Kamil Rojewski
752c7b576d avoiding more NoSuchMethod exceptions (#6671)
refs #6657
2021-06-03 14:18:45 -07:00
ofirm93
7c3e267e1e [Java] ObjectAPI implementation (#6521) (#6582)
* [Java] ObjectAPI implementation (#6521)

* [Java] ObjectAPI implementation (#6521)

* [Java] ObjectAPI implementation (#6521)

* [Java] ObjectAPI implementation (#6521)

* [Java] ObjectAPI implementation (#6521)

* [Java] ObjectAPI implementation (#6521)

* [Java] ObjectAPI implementation (#6521)

* [Java] ObjectAPI implementation (#6521)

* [Java] ObjectAPI implementation (#6521)

* [Java] ObjectAPI implementation (#6521)

* [Java] ObjectAPI implementation (#6521)

* [Java] ObjectAPI implementation (#6521)

* [Java] ObjectAPI implementation (#6521)

* [Java] ObjectAPI implementation (#6521)

* [Java] ObjectAPI implementation (#6521)

* [Java] ObjectAPI implementation (#6521)

* [Java] ObjectAPI implementation (#6521)
2021-06-03 14:17:40 -07:00
Wouter van Oortmerssen
baaffbaedd npm update to 2.0.3 and script fix
Change-Id: Idacbc5973e9c1f713f5c8e03513a83590e336fdb
2021-06-03 13:40:35 -07:00
Björn Harrtell
4cb3f222b1 [TS] Fix module reference in package.json (#6676) 2021-06-01 09:10:55 -07:00
Björn Harrtell
eabdbda755 [TS] Generate entry point module (#6674) 2021-06-01 09:09:55 -07:00
Wouter van Oortmerssen
bec23700fc Prepare for Java 2.0.1 release
see https://github.com/google/flatbuffers/pull/6658
2021-05-24 12:20:45 -07:00
Wouter van Oortmerssen
12f2eedadc Update CI to GCC/Clang 11 2021-05-24 11:55:40 -07:00
kalabukdima
221eeb2312 Fix typo in C++ doc (#6664) 2021-05-24 11:22:01 -07:00
Kamil Rojewski
813d3632ec avoiding NoSuchMethod exception (#6658) 2021-05-24 11:09:45 -07:00
Derek Bailey
d84bccb0c7 Removed most heap allocations in builder (#6662)
* [C++] Removed most heap allocations in builder

* Updated API docs to indicate heap usage

* Override assertion for heap allocation in parser

* Cleaned up implemenations, enable heap alloc for tests

* Generalized CreateVectorOfStrings

* remove cmake option for heap alloc. reverted two heap removals

* Only use scratch space for vector of strings

* Override Windows SCL warning

* Changed std::transform to for loop to avoid MSCV warnings

* switched to const iterators

* Replaced iterator with for loop

* remove std::to_string in test to be compatible
2021-05-21 11:09:24 -07:00
Ivan Dlugos
b4e67f9bf0 Dart test fix (#6660)
* Dart cppData test - fix path on Dart v2.12 (Linux)

* Dart cppData test - fix expected data mismatch

* add Dart CI test in GH Actions
2021-05-20 12:00:51 -07:00
Samir Ahmed
54c11932fa [Java] Flexbuffers - Negative signed object length (#6651)
* retry with correct email

* update JavaTest.java
2021-05-20 11:57:10 -07:00
mustiikhalil
fbcb3c4236 [TS/JS] Updates the grpc (#6654)
* Updates go lang support to allow other languages to communicate with it

* Update js grpc lib to use grpc-js

Reformat code
2021-05-20 11:34:17 -07:00
mustiikhalil
8937dcfd78 Updates go lang support to allow other languages to communicate with it (#6653) 2021-05-20 11:32:56 -07:00
Derek Bailey
512d5a6897 Update readme.md to point to security policy 2021-05-19 11:59:58 -07:00
Derek Bailey
a92cb5dd7b Create Security.md
Defining our security reporting process.
2021-05-19 11:55:50 -07:00
Derek Bailey
d151dcbb9d Revert "[C++] Removed most heap allocations in builder (#6620)" (#6659)
This reverts commit 72730ecd8a.
2021-05-19 10:53:46 -07:00
Richard A Hofer
f1884c66f4 Fix gRPC test to use EndVector without specifying the length. (#6646) 2021-05-17 11:27:49 -07:00
Derek Bailey
72730ecd8a [C++] Removed most heap allocations in builder (#6620)
* [C++] Removed most heap allocations in builder

* Updated API docs to indicate heap usage

* Override assertion for heap allocation in parser

* Cleaned up implemenations, enable heap alloc for tests

* Generalized CreateVectorOfStrings

* remove cmake option for heap alloc. reverted two heap removals

* Only use scratch space for vector of strings

* Override Windows SCL warning

* Changed std::transform to for loop to avoid MSCV warnings

* switched to const iterators
2021-05-17 10:53:32 -07:00
Casper
fe2bc2b0a7 Added README for Rust Flatbuffers (before publishing v=2.0) (#6652)
Co-authored-by: Casper Neo <cneo@google.com>
2021-05-16 10:37:16 -04:00
Derek Bailey
4867c94564 Fixed generation of c++ code with flags (#6637)
* Fixed generation of c++ code with flags

* tweaking const

* moved to CONSTEXPR_CP11
2021-05-15 13:27:24 -07:00
Derek Bailey
ef0eb3701d Ran clang-format-all.sh. Removed default --style=file parameter (#6639)
* Ran clang-format-all.sh. Removed default --style=file parameter

* Moved scripts to scripts/ location
2021-05-15 12:54:08 -07:00
Fraser Crossman
f83ee1af59 [idl_parser] Check structs and enums do not clash in a namespace (#6562)
* [idl_parser] Check structs and enums do not clash in a namespace

Uses fully qualified names to check for clashes within a given namespace whether explicitly defined or in the global namespace.

* [idl_parser] Move type name clash check to ParseEnum and ParseDecl

Change point at which parsing error is returned to ensure error is caught more generally. This change means that the error is returned after parsing the entirety of the offending duplicate rather than at the start when parsing it's name.

* [idl_parser] Add single and multi file type name clash tests

Adds a selection of tests for valid single file schemas with types that have the same name but are in different namespaces.
Adds a test for an a valid schema that spans two files with two types that have the same name but are in different namespaces.
Adds a test for an an invalid schema that spans two files with two types that have the same name and are in the same namespace.
2021-05-14 13:56:52 -07:00
mustiikhalil
a5175c513a Implements verifier and code gen for swift (#6373)
Updates test cases on linux

Adhere to new protocol naming

Adds fuzzing

Adds documentation

Adds support for string unions

Updated fuzzer generated code
2021-05-14 20:59:28 +03:00
Casper
04b10f5a3a Deprecate EnumVal.object (#6612)
Co-authored-by: Casper Neo <cneo@google.com>
2021-05-13 14:57:36 -07:00
Wouter van Oortmerssen
c121c03456 Fixed Java gRPC version number dependency
Change-Id: I6c4ed59ab4bb06edcef4b33c179f6e02ec86fdbb
2021-05-13 13:32:13 -07:00
Casper
8fd10606c1 Implement Serialize for flexbuffer::Reader (#6635)
* Implement Serialize for flexbuffer::Reader

* bump version

* Remove use of experimantal or-patterns

* Remove more use of experimantal or-patterns

Co-authored-by: Casper Neo <cneo@google.com>
2021-05-10 23:15:46 -04:00
mustiikhalil
a1730fcea8 [Swift] Updated cocoapods to 2.0.0 (#6634) 2021-05-11 00:13:41 +03:00
Wouter van Oortmerssen
a9a295fecf More missing version changes 2021-05-10 11:45:16 -07:00
Wouter van Oortmerssen
5c01ad387f Dart generated code update
Change-Id: I317d731d17d733088cad608364b614bff8151c36
2021-05-10 11:35:00 -07:00
Wouter van Oortmerssen
42ca1b9140 Swift/Kotlin generated code version updates
Change-Id: I035163cb5edd087a190bb518e686c79887241383
2021-05-10 11:30:26 -07:00
Wouter van Oortmerssen
6ed780dbd5 C++/Rust version changes 2021-05-10 11:29:32 -07:00
Wouter van Oortmerssen
3412fab8ee C#/Java generated code version updates 2021-05-10 11:17:31 -07:00
Wouter van Oortmerssen
8a7d013f85 Updated main version numbers to 2.0 2021-05-10 11:14:47 -07:00
mogemimi
170af59788 [Go] Add missing namespace when using Object API (#6633) 2021-05-10 10:52:10 -07:00
Vladimir Glavnyy
c8c16de167 Fix reverse iterators for Vector and Array (#6626)
This commit fixes two serious issues connected with reverse iterators:
1. Vector's rbegin/rend was incompatible with std::reverse_iterator::base();
2. Array's rend() was incorrect, it based on end() instead of begin().
2021-05-10 09:07:56 -07:00
罗泽轩
4525cd9c56 [Lua] manipulate byte array as string (#6624)
* [Lua] manipulate byte array as string

Sometimes it would be more effective than reading byte by byte

* change according to the review

* update
2021-05-07 22:57:13 -07:00
Piotr Dziwiński
47361baf61 [C++] Fix union copy constructor to work with nested structs (#6552) (#6607) 2021-05-06 14:42:44 -07:00
mustiikhalil
8a582883ac Updates swift generated monster sample file (#6616)
Updates swift generated monster sample file
Adds frozen attribute
2021-05-06 07:55:03 +03:00
罗泽轩
a4bb8f0c2e [Lua] Avoid infinite loop when creating empty string (#6614)
* [Lua] Avoid infinite loop when creating empty string

* [Lua] Check empty string output
2021-05-05 19:39:42 -07:00
Derek Bailey
82aed82b84 Added support for Lua 5.1, 5.2 and 5.4 (#6606)
This adds basic support for different Lua versions.

For Lua 5.2 and Lua 5.3, both the Bit32 and Compat53 Lua modules must be
installed for it to work. You can typically get these on Linux using

apt install lua-compat53 lua-bit32

For Lua 5.4, it should work as is, as it is a clean superset of Lua 5.3,
which is what the original Lua Flatbuffers supported.
2021-05-05 16:36:49 -07:00
Björn Harrtell
60ff76630d [TS] Remove wrong and obsolete NS prefix use (#6604) 2021-05-03 12:04:34 -07:00
Derek Bailey
a27c7d8093 Fixed LuaJIT when not compiled with COMPAT mode (#6605)
LuaJIT as installed by apt doesn't include the COMPAT mode for using
Lua5.2 features. So this change just makes the flatbuffer code use a
common implementation.
2021-05-03 11:35:49 -07:00
Kamil Rojewski
3632208233 removed unneeded type prefixing (#6601) 2021-05-03 10:40:59 -07:00
Casper
6b44c605b8 Bump Rust to 0.9.0 (#6610)
Co-authored-by: Casper Neo <cneo@google.com>
2021-05-02 12:28:17 -04:00
罗泽轩
d3cd78a87c [Lua] Add LuaJIT support (#6584)
* [Lua] Add LuaJIT support

Here is the output of LuaTest.sh:

Run with LuaJIT:
built 100 512-byte flatbuffers in 0.16sec: 0.63/msec, 0.31MB/sec
built 1000 512-byte flatbuffers in 0.08sec: 12.06/msec, 5.89MB/sec
built 10000 512-byte flatbuffers in 0.80sec: 12.44/msec, 6.07MB/sec
built 10000 512-byte flatbuffers in 0.33sec: 30.58/msec, 14.93MB/sec
traversed 100 592-byte flatbuffers in 0.04sec: 2.51/msec, 1.42MB/sec
traversed 1000 592-byte flatbuffers in 0.03sec: 31.52/msec, 17.79MB/sec
traversed 10000 592-byte flatbuffers in 0.21sec: 48.77/msec, 27.53MB/sec
Run with Lua 5.3:
built 100 512-byte flatbuffers in 0.02sec: 5.44/msec, 2.66MB/sec
built 1000 512-byte flatbuffers in 0.17sec: 5.74/msec, 2.80MB/sec
built 10000 512-byte flatbuffers in 1.75sec: 5.72/msec, 2.79MB/sec
built 10000 512-byte flatbuffers in 1.38sec: 7.26/msec, 3.55MB/sec
traversed 100 592-byte flatbuffers in 0.00sec: 27.64/msec, 15.60MB/sec
traversed 1000 592-byte flatbuffers in 0.03sec: 30.46/msec, 17.20MB/sec
traversed 10000 592-byte flatbuffers in 0.34sec: 29.62/msec, 16.72MB/sec

* [Lua] Better usage description

* update according to the review
2021-04-30 10:38:16 -07:00
Casper
8fa3dfdb5d Introduce new_from_vec in Rust (also fix formatting) (#6599)
* Introduce new_from_vec in Rust (also fix formatting)

Also, rename `new_with_capacity` to `with_capacity` to match
how `Vec` does it.

* bump rust version

* mut_finished_buffer

Co-authored-by: Casper Neo <cneo@google.com>
2021-04-29 18:23:22 -04:00
shiyuge
29379e8e49 fix typo in CppUsage.md (#6595)
Thanks!
2021-04-29 10:05:55 -07:00
Derek Bailey
14725d6c3b [Lua] GetRootAs can accept strings. Made Luatest Benchmarks optional (#6593) 2021-04-27 13:02:13 -07:00
Casper
16836ff95a Add advance feature indicators to reflection (#6546)
* Add advance feature indicators to reflection

* deserialize too

* model advanced features as bitflags

* use uint64_t instead of AdvancedFeatures

* git clang format

* initialize advanced_features_

* remove whitespace

Co-authored-by: Casper Neo <cneo@google.com>
2021-04-27 10:10:59 -04:00
Casper
c87179e73e Rust Remove SafeSliceAccess for Arrays, and fix miri. (#6592)
* Fix Miri flag passing and bump Rust version.

* Fix Miri problems from Arrays PR.

SafeSliceAccess was removed for Arrays. It's kind of unsound.
It has two properties:
1. EndianSafe
2. Alignment 1

We only need 1. in create_vector_direct to memcpy data.
We both 1. and 2. for accessing things with slices as buffers are built on &[u8]
which is unaligned. Conditional compilation implements
SafeSliceAccess for >1byte scalars (like f32) on LittleEndian machines
which is wrong since they don't satisfy 2.

This UB is still accessible for Vectors (though not exercised our
tests) as it implements SafeSliceAccess. I'll fix this later by
splitting SafeSliceAccess into its 2 properties.

Co-authored-by: Casper Neo <cneo@google.com>
2021-04-26 19:28:25 -04:00
Casper
c24031c36b Mark endian_scalar as unsafe. (#6588)
* Mark endian_scalar as unsafe.

Also
- removed the deprecated flexbuffer slice from example
- fixed some cargo warnings

* Assertions and read_scalar made unsafe

* Clippy lints

* Add to Safety

Co-authored-by: Casper Neo <cneo@google.com>
2021-04-26 09:18:58 -04:00
mustiikhalil
4ccc52c7a0 [swift] Implements union strings (#6589) 2021-04-24 14:47:24 +03:00
Alexander Zaitsev
b82fe07384 [Rust] Fix small mistyping (#6585)
Hi!

Just a veeeeeery small fix in the Rust sample. No functionality is affected.

That's not important at all but annoying for my eyes :)
2021-04-22 12:36:23 -07:00
罗泽轩
1e7f6c8c7c [TS] Commit the generated tests code (#6579)
* [TS] Ignore generated monster_test_grpc files

* [TS] Commit the generated tests code
2021-04-22 11:15:18 -07:00
罗泽轩
84714b109b chore: ensure bash is used to generate code (#6577)
In some platforms, like Ubuntu, `sh` is linked to `dash` but not `bash`.
2021-04-19 14:30:55 -07:00
罗泽轩
1045d7dd44 [Lua] Fix EnforceNumberAndPack for bool type (#6576) 2021-04-19 14:30:20 -07:00
罗泽轩
0c77775966 [TS] Remove duplicated build-ts ci (#6575)
Now we already have a real build-ts ci job, let's remove the commented
one.
2021-04-19 14:28:26 -07:00
Ádám Lippai
c43ba17520 [Rust] Specify Minimum Supported Rust Version (#6573)
Related to #6572
2021-04-19 16:43:11 -04:00
Eddie Linder
da3bb64ef6 [Rust] Add support for fixed size arrays (#6548)
* Add support for fixed size arrays

* clang-format

* Update rust image to 1.51 to support const generics

* Handle correctly big endian

* Add fuzz tests and clean code

* Add struct fuzz test and optimize struct arrays for api

* Bump flatbuffers crate version
2021-04-16 11:15:59 -04:00
Paulo Pinheiro
151900ba96 [Kotlin][FlexBuffers] Add support for Kotlin-iOS (#6564)
With this change, Flexbuffers for Kotlin now supports iOS(arm32, arm64, X64).
WatchOS and tvOS not supported.
2021-04-15 11:29:51 -07:00
Kamil Rojewski
c012f29f91 silenced clippy warning (#6565) 2021-04-15 11:19:13 -04:00
Paulo Pinheiro
df2df21ec1 [Kotlin] Bump kotlinx.benchmark dependency to 0.3.0 (#6560)
Kotlinx.benchmark project just abandoned bintray and moving to maven central,
removing the artifacts from bintray and causing missing dependencies in our build.
So we are updating the dependency to point to the new version on maven central.

More information in:

53ee45d0d9 (diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5)
2021-04-12 13:47:40 -07:00
Björn Harrtell
408e4db4af [TS] Add Build TS to CI jobs (#6524)
* Add Build TS to CI jobs

* Add npm compile step

* Fix syntax

* Add required code gen for TS

* Exit on failure

* Make TS code gen identical to test run

* Remove duplicate TS code gen artifacts

* Remove bad gitignore

* Forgot parts of generate_code and make sure same settings for test run

* Don't forget about the bat

* Try and fix flatc args

* Another attempt to fix args

* Fix typo

* Commit up to date code gen

* Another attempt to fix sh/bat

* Move -o param to after -I

* Commit missing code gen file

* Fix grpc code gen and test

* Another grpc code gen fix

* Rework to not use ts folder

* Fix env vars in bat and make less churn

* Move TS code gen to dedicated folder

* Fix transpilation output folder and module paths

* Fixes to code gen

* Include generated js

* Moved ts code gen

* Remove test ts code gen folder
2021-04-12 10:41:59 -07:00
Paulo Pinheiro
4d2364f342 [Kotlin][FlexBuffers] Add support for Kotlin-JS (#6554)
Flexbuffers for Kotlin currently supports JVM and MacOS. This change
introduces support to JS as well.
2021-04-12 10:30:15 -07:00
Casper
261cf3b204 Default-empty vectors of enums (#6505)
* disable clippy

* Vector of enum default

* swift and tests

* git clang format

* Rewrite enum parser checks

* Remove Voids from more_defaults

* vector enum swift

* remove vector accessor from swift

* clang format

Co-authored-by: Casper Neo <cneo@google.com>
2021-04-06 07:23:45 -04:00
mustiikhalil
cd67261bba [CI] fixes buildkite (#6544) 2021-04-05 12:34:42 +03:00
mustiikhalil
1aa0c2f6a9 Limit formatter to pull requests only (#6540) 2021-04-01 20:09:43 +03:00
Casper
4133a39df8 Rust structz (#6539)
* Rust structz

* struct of structs test

* swift tmp variables

Co-authored-by: Casper Neo <cneo@google.com>
2021-03-29 19:56:45 -04:00
Paulo Pinheiro
1c26d2a1a0 [Kotlin][FlexBuffers] JSON support for Flexbuffers (#6417)
* [Kotlin][FlexBuffers] Add JSON support for FlexBuffers

* [Kotlin][Flexbuffers] Re-implement JSON parser with a tokenizer.
2021-03-29 15:57:23 -07:00
Jeong YunWon
276b1bc342 [grpc] Support latest version of grpc PoC (#6338)
* use grpcpp rather than deprecated grpc++

* grpcpp Deserialize take ByteBuffer instead of grpc_byte_buffer

* grpc 1.36.0

* Fix to use grpc 1.36.0

* Fix CMakeLists to refer gRPC

* add find_package(Threads)

* Update bazel deps

* Apply workaround about boringssl build error

* Install latest cmake for osx on travis-ci

Co-authored-by: Yeom Gyusun <omniavinco@gmail.com>
2021-03-25 12:12:35 -07:00
Kamil Rojewski
124654ffc4 fixed packing structs (#6530)
* fixed packing structs in nested buffers

* fixed packing structs
2021-03-25 10:25:00 -07:00
mustiikhalil
3b7d1e86b4 [GO] Tries to add go format to the CI (#6518)
* Trys to add go format to the CI

Adds Gofmt to CI

* Adds formatting for go dir in grpc

* Small fix
2021-03-18 11:03:44 -07:00
Michael
78f0c0d1d9 [C++] #6501 - Problem when mapping a native type multiple times (#6514)
* [C++] #6501 - Problem when mapping a native type multiple times
- idl.h:
added "native_type_pack_name"
- flatbuffers.h:
added CreateVectorOfNativeStructs variants which receive a pointer to the serialization function
- idl_gen_cpp.cpp:
adapted code generation in case "native_type_pack_name" attribute is present
- extended tests & docs; improved surrounding native_type docs a little

* integrated review feedback
2021-03-18 11:01:50 -07:00
Vladimir Glavnyy
c992eafb5b [fuzzer] Add monster_debug target (#6513)
Add the `monster_debug` target for better troubleshooting.
Improve oss-fuzz logs.
2021-03-18 10:56:28 -07:00
mustiikhalil
ef8dd7792a [Swift] Removes allman rule (#6519) 2021-03-17 13:22:49 +03:00
Vladimir Glavnyy
69b329fc87 [flexbuffers, json] Parse nan and inf (#6512) 2021-03-15 11:44:42 -07:00
Kamil Rojewski
6543ba5297 fixed packing structs in nested buffers (#6509) 2021-03-11 11:14:23 -08:00
Vladimir Glavnyy
0e453ac352 [idl_parser] Add kTokenNumericConstant token (#6432)
* [idl_parser] Add kTokenNumericConstant token

This commit adds the new token for correct parsing of signed numeric constants.
Before this expressions `-nan` or `-inf` were treated as kTokenStringConstant.
This was ambiguous if a real string field parsed.
For example, `{ "text_field" : -name }` was accepted by the parser as valid JSON object.

Related oss-fuzz issue: 6200301176619008

* Add additional positive tests fo 'inf' and 'nan' as identifiers

* Rebase to HEAD

* Move processing of signed constants to ParseSingleValue method.

* Add missed `--cpp-static-reflection` (#6324) to pass CI

* Remove `flatbuffers.pc` from repository to unblock CI (#6455).

Probably the generated flatbuffers.pc should not be a part of repo.

* Fix FieldIdentifierTest()
2021-03-11 11:12:06 -08:00
Vladimir Glavnyy
e9b4ae69dc Remove flatbuffers.pc from the repository (#6508) 2021-03-11 11:11:04 -08:00
mustiikhalil
fc4fffea41 Bump grpc to 1.0.0 (#6507)
Adds void to keywords
2021-03-09 10:56:06 +03:00
dpacbach
b240ab704d Move Traits struct and Create method out of --cpp-static-reflection. (#6503) 2021-03-08 10:59:17 -08:00
Ben Niu
9a4f1f434b Disable x64-specific optimizations for ARM64EC ReadInt64 (#6506)
ARM64EC is a new ARM64 ABI designed by Microsoft to support x64 application emulation on ARM64 CPUs. When compiling for ARM64EC, both the _M_X64 and _M_ARM64EC macros are defined. However, that causes problem in compiling this file, because the __movsb intrinsic, which is lowered to rep movsb, is not supported on ARM64, so the optimization for native x64 should be disabled for ARM64EC.
2021-03-08 10:53:04 -08:00
dpacbach
fac64918dc Add --cpp-static-reflection to generate_code.bat. (#6502) 2021-03-06 22:28:27 +07:00
David P. Sicilia
a69815f72c [C++17] Add compile-time reflection for fields. (#6324)
* [C++17] Add compile-time reflection for fields.

Included in this commit is the following:

  - The C++ generator has been modified so that,
    when in C++17 mode, it will emit Table and
    Struct field traits that can be used at com-
    pile time as a form of static reflection. This
    includes field types, field names, and a tuple
    of field getter results.

  - Diffs to the cpp17 generated files. No other
    generated files are affected.

  - A unit test that also serves as an example. It
    demonstrates how to use the full power of this
    reflection to implement a full recursive
    JSON-like stringifier for Flatbuffers types,
    but without needing any runtime access to the
    *.fbs definition files; the computation is
    done using only static reflection.

Tested on Linux with gcc 10.2.0.

Fixes #6285.

* Fix int-conversion warning on MSVC.

* Try to fix std::to_string ambiguity on MSVC.

* Fix clang-format diffs.

* Fix more clang-format diffs.

* Fix last clang-format diff.

* Enable C++17 build/test for VC 19 platform in CI.

* Forgot to add value to cmake command line variable.

* Various fixes/changes in response to @vglavnyy's feedback.

* Replace "fields pack" with index-based getters.

* Fix MSVC error.

* Fix clang-format diffs.

* getter_for method returns result instead of address-of-getter.

* Next round of reviewer suggestions.

* Use type instead of hardcoded struct name.

* Fix clang-format diff.

* Add test for FieldType since it is not used in the stringify test.

* Add fields_number field to Traits struct.

* Add --cpp-static-reflection flag and put those features behind it.

* Fix clang-format diffs.

* Remove <tuple> include.
2021-03-05 10:01:40 -08:00
Kamil Rojewski
4033ff5892 fixed invalid TS call and added test files (#6495)
e581013e3d broke TS generation - please don't use "replace" to refactor function names :)
2021-03-04 12:12:44 -08:00
Casper
a083572512 disable clippy (#6494)
Co-authored-by: Casper Neo <cneo@google.com>
2021-03-01 13:51:22 -08:00
Vladimir Glavnyy
5319dedb1a [idl_parser, JSON] Disable parsing of JSON for incomplete schemes (#6493)
This commit disable JSON parsing for an incomplete scheme if JSON object is embedded into one file with the scheme.
This should improve the quality of OSS-Fuzz inputs for the parser_fuzzer target.
2021-03-01 13:40:58 -08:00
Mark Spatz
bd4e0b30a7 [idl_parser] Track included files by hash (#6434)
* [idl_gen] Delete ts::GenPrefixedImport()

I don't know what this is for, but it's the only piece of code external
to idl_parser.cpp that expects the key of Parser::included_files_ to be
a path. And it appears to be unused.

* [idl_parser] Track included files by hash

Parser::included_files_ is a map whose main purpose is to keep track of
which files have already been parsed in order to protect against
multiple inclusion. Its key is the path that the file was found at
during parsing (or, if it's an in-memory file, just its name).

This commit changes the key to be the 64 bit FNV-1a hash of the file's
name (just the name, not the complete path) xor'd with the hash of the
file's contents (unless it's an in-memory file, then we only hash the
name.)

This allows multiple include protection to function even in the face of
unique per-file include paths (fixes #6425).

* Ran tests/generate_code.sh

CI told me to do it.

* Gracefullt handle case where source_filename == nullptr

I just learned source_filename might also be null. In that case, we
should exclude it from the hash instead of hashing a zero length
string, just like we exclude source when it is null.

Presumably nameless files will never be included (they can't, can they?)
so this doesn't really matter, but I think it's prettier anyways.
2021-03-01 12:34:01 -08:00
Kamil Rojewski
bf90612007 fix for noUncheckedIndexedAccess in ts (#6474) 2021-02-25 14:57:10 -08:00
mustiikhalil
8142fedd19 Working on a python example plus fixing python grpc code (#6456)
Refactored python grpc code gen

Adds example server & client + fixes ci

Fixes generated code

Making sure we encode the reply string as utf8

Adds Readme details to clarify issue regarding encoding when python is sending/receiving
2021-02-25 14:38:12 -08:00
Colin
c0be1cb7a5 [rust] Remove debug code (#6475)
* Remove debug code

This was added for testing in the recent genericize PR for flexbuffer Reader.

* Added alloc tests -> MapReader::{is_empty, index_key, len}

* Added , accessible through Deref to deprecation warning
2021-02-24 13:00:18 -05:00
Derek Bailey
8cccdfba53 Revert "[C#] Fix truncated ArraySegment<byte> if elementSize != 1 (#6462)" (#6488)
This reverts commit cbbbaa61b3.
2021-02-23 11:30:07 -08:00
Björn Harrtell
cbbbaa61b3 [C#] Fix truncated ArraySegment<byte> if elementSize != 1 (#6462)
* WIP: Fix returned truncated ArraySegment<byte> if elementSize is not byte

* Fix

* Regenerated test code
2021-02-23 09:34:20 -08:00
mustiikhalil
ffc2ef77ca [CI] Adds Code Generation tests on Github Actions (#6482)
Moves check-grpc-generation to scripts
2021-02-23 08:51:39 +03:00
Millian Poquet
1da6f4f18b [CMake] generate pkg-config flatbuffers.pc file (#6455)
This commit enables CMake to generate a flatbuffers.pc file on install.

pkg-config eases the utilization of software libraries by enabling the
developers of a library to define how the library should be used.

It can be used as a tool by build systems to find and manage dependencies,
this is notably the default Meson behavior and optionally used by CMake.
2021-02-22 10:23:38 -08:00
mustiikhalil
b5da526e6d [Swift] Moves grpc example to grpc/examples (#6479)
Updates generated Code

Removes grpc generation code from tests dir

Small fix to generate.sh
2021-02-20 23:07:48 +03:00
mustiikhalil
3b5365762d [TS] Moves grpc code to examples folder (#6476)
Adds readme
2021-02-20 20:31:00 +03:00
mustiikhalil
e2f5438ac1 Fixes grammer (#6477)
Updates docs
2021-02-19 12:16:38 -08:00
Richard A Hofer
5e3613f732 Fix sample_binary.py to use latest EndVector without a size. (#6478) 2021-02-19 12:13:50 -08:00
Kamil Rojewski
e6b911d40c updated JS docs to reflect current status (#6436) 2021-02-19 12:05:02 -08:00
mustiikhalil
0c7ae58164 [Go] Working on a go example plus fixing go grpc code (#6448)
Implemented server.go and half implemented client.go

Finishes implementation for greeter go example

Update grpc code for monster.fbs

Adds a couple of cpp methods

Adhere to gofmt standards

Adds a readme for issues with grpc
2021-02-19 12:47:59 +03:00
mustiikhalil
ae603b9770 [Swift] adds support for default vectors and strings (#6461)
Small fix

Adhere to the new protocol names
2021-02-19 12:47:28 +03:00
Arnaud LE CAM
7f47718b6d Update Building.md (#6473)
`cmake . -D FLATBUFFERS_CXX_FLAGS="Wno-error"` returns an error (file 'Wno-error' not found)
2021-02-18 19:51:41 -08:00
Thanabodee Charoenpiriyakij
54dc09e8ac GetUOffsetT must get value by GetUint32 not GetInt32 (#6072) 2021-02-18 18:38:25 -08:00
Liu Liu
334c6be496 Fix a typo in Swift codegen (#6470)
* Fix a typo in Swift codegen

* Fix code formatting.
2021-02-17 09:34:39 +03:00
Colin
4174c10e7a [rust] Genericize flexbuffer reader (#6450)
* feature/rust-tokio-bytes added feature name for tokio-bytes

* Added flexbuffer implementation, TODO: typecast to avoid recurse

* Converted codebase to utilize FlexBuffer implementation, need to resolve deserialization issues

* Added todo for lifetime issue, may use &'de [u8] for deserializer instead of current method

* Added proper &[u8] implementation

* Removed unused struct

* Added experimental fix to get_slice

* Added experimental fix to get_slice

* Avoided lifetime issues via ref structs, need to check if this hurts peformance

* Updated deserializer implementation to allow for borrowed data from Reader struct

* Fixed bug with str

* Removed unnecessary generic parameter

* Added unsafe to avoid lifetime complaints, current tests pass, need to review alternatives to unsafe

* Opinionated: Removed bytes crate as this implementation could be done in a separate crate

* Cleaned up flatbuffer

* Fixed sample / example

* Resolved PR feedback, need to resolve issues with tests

* Cleaned up FlexBuffer trait to be an auto impl

* Removed TODO

* Reverted Deserializer to only support &'de [u8]

* Cleaned up / renamed function for clarification

* Renamed FlexBuffer -> InternalBuffer for clarification on it's purpose

* Fixed issue with key bytes

* resolved issues with broken tests, confirming this is a breaking change

* Removed FIXME that's solved by splitting String and Key variants

* Implemented associated types approach

* Fixed backward slice logic

* Fixed MapReader compile error

* Added from_buffer for deserialization, removed  function since it's only needed for deserialization

* Removed dead code

* Cleaned up buffer, removed AsRef in favor of Deref

* Renamed Buffer::as_str -> Buffer::buffer_str

* Minor cleanup

* Updated documentation, need to fix tests

* Removed unnecessary &

* Removed unused lifetime

* removed unnecessary as_ref

* Minor optimization wrap-up

* resolved issue with Clone

* Added test to verify no deep-copy

* Added  for optimization

* Updated to use empty fn instead of default

* Updated comments / test name - plus the 0.3.0 version bump

* comment
2021-02-16 08:04:48 -05:00
mustiikhalil
a20f606c29 [Swift] Renaming protocols (#6469)
* Renaming protocols

* Updates Generated code

* format code
2021-02-16 14:55:47 +03:00
mustiikhalil
a72a208272 Update swift docs (#6460) 2021-02-14 10:09:11 +03:00
Casper
86401e078d Default strings and vectors: Parser + Rust support (#6421)
* Fix tests.cpp

* Parser support for vector/string defaults

* tests and default empty vectors

* addressed comments

* Default strings and vectors for Rust

* Tested Rust more_defaults

* git-clang-format

* add more_defaults_test

* fixed vector default

* removed commented out code

* more unreachable

Co-authored-by: Casper Neo <cneo@google.com>
2021-02-12 09:41:10 -05:00
mustiikhalil
6af37e6729 [CMake] Renames BUILD files (#6457)
* Renamed build to build.bazel to stop xcode from complaining about it

* Renamed all build to build.bazel

* Fixes small ci issue
2021-02-11 10:16:57 -08:00
Vladimir Glavnyy
1b88655b00 [Build, cmake] Add -Werror override option (#6429)
* [Build, cmake] Add -Werror override option

This commit adds FLATBUFFERS_CXX_FLAGS cmake option.
This option allows override the -Werror flag (or any other flags).
Related issues:  #6337, #5930

* Remove CMAKE_CXX_FLAGS replace option
2021-02-10 11:49:34 -08:00
blueshift155
0b15916e50 [C++]Fix extra char generation for byte type during json schema generation (#6276)
* Fix extra char generation for the byte type

* Fix unit tests for JSON
2021-02-10 11:48:24 -08:00
mustiikhalil
60eed0ca66 Updating working code (#6441)
Fixes issues with namespaces in grpc ts

Renamed welcome -> models
2021-02-10 11:46:45 -08:00
tira-misu
0f83367f57 JSON schema - tailing double quotes for maximum (#6452)
* Fix C/C++ Create<Type>Direct with sorted vectors

If a struct has a key the vector has to be sorted. To sort the vector
you can't use "const".

* Changes due to code review

* Improve code readability

* Add generate of JSON schema to string to lib

* option indent_step is supported

* Remove unused variables

* Fix break in test

* Fix style to be consistent with rest of the code

* Fix  double quotes for (u)int8 in json schema

* Fix reference file for JSON schema test

* Fix reference file for JSON schema test
2021-02-10 11:45:34 -08:00
Vladimir Glavnyy
fee095410b [idl_parser] Validate force_align on all possible paths (#6430)
* [idl_parser] Validate `force_align` on all possible paths

- added validation of `force_align` for `Vector`.
- added assertion to `flatbuffers::PreAlign` method

These changes should resolve following oss-fuzz issues:
- 6275816289861632
- 5713529908887552
- 4761242994606080

* size_t problem on Mac

* Fix review notes
2021-02-10 11:37:16 -08:00
Casper
6f3e45eca1 Implement Rust object API defaults (#6444)
* Implment Rust object API defaults

* satisfy return analysis

* git clang format

Co-authored-by: Casper Neo <cneo@google.com>
2021-02-07 16:51:33 -05:00
mustiikhalil
815d3e820d Upgrade swift grpc to alpha 24 (#6439)
Upgrade swift grpc to alpha 24
2021-02-04 02:01:18 +03:00
Vladimir Glavnyy
76e7a0ff55 [fuzzer] Limit parser_fuzzer execution time (#6431)
Limit the length of the fuzzer input to 16384 characters to prevent timeout
in JSON parser (Vector of tables with key).

Related oss-fuzz issues:
- 5742497110294528
2021-01-30 21:43:31 +07:00
Paulo Pinheiro
6d91096a2f This commit contains the initial implementation of Flexbuffers in Kotlin. The code was ported based (#6387)
on the current Java Implementation.

The code dependencies related to JVM were removed and the project is able to target all available platforms.

The only requirement to implement to fully support a target is to implement functions described in `ByteArray.kt`.
Right now the code support JVM and native targets. JS port still missing, but just be trivial to introduce.

Currently, only the `jvm` and `macosX64` targets are enabled until we figure out how to enable tests on all
platforms on CI.

A submodule called "benchmark" is also introduced. It contains a series
of benchmarks comparing Java and Kotlin implementations of FlexBuffers and the UTF8 API.

Finally, this commit does not contain the scripts necessary to publish the artifacts. This will
be introduced at a later stage once the team has an agreement on how to rollout Kotlin releases.
2021-01-28 14:49:25 -08:00
Richard A Hofer
13d9e35858 Better python generated code naming (#6336)
* Remove a lot of redundancy from the Python generated code.

Update tutorial to reflect new Python generated code.

* Add aliases for newly deprecated Python generated methods.

This should help with backwards compatibility.

* Fix incorrect names in deprecated comments.
2021-01-28 12:35:37 -08:00
Casper
6effe431bb Rust: remove inner attributes (#6410)
* remove inner attributes

* Added test for outdir in Rust

* add bin/outdir

* Moved outdir test to its own package and only run it if flatc is available

Co-authored-by: Casper Neo <cneo@google.com>
2021-01-26 11:09:29 -05:00
William Bain
efcbdc7698 [Rust] Ensure unions are referenced with the correct path (#6422)
* Add codegen test for namespaced unions

* [Rust] Handle cross-namespace union use

* [Rust] Test namespace handling

* [Rust] Drop trailing whitespace in codegen

* [Rust] Set flags in generate_code.bat to match .sh

* [C#] Add additional namespace test file
2021-01-25 09:33:47 -08:00
Casper
e581013e3d Refactor FieldDef to model presense as an enum rather than 2 bools. (#6420)
* Define presence.

* Migrate to IsRequired and IsOptional methods

* moved stuff around

* Removed optional and required bools from FieldDef

* change assert to return error

* Fix tests.cpp

* MakeFieldPresence helper

* fmt

* old c++ compatibility stuff

Co-authored-by: Casper Neo <cneo@google.com>
2021-01-25 09:29:43 -08:00
Andreas Kabel
0984d4328d [c++] Apply NativeName before WrapInNameSpace in idl_gen_cpp.cpp (#6419)
* Apply NativeName before WrapInNameSpace in idl_gen_cpp.cpp

* remove actual_type argument from GetUnionElement -- it's always true

* Merge GetUnionElement's native_type and wrap_native argument -- they always have the same value.

* Use convenience method WrapNativeNameInNameSpace

* Remove wrap_namespace argument from GetUnionElement

* Move declaration closer to first use.
2021-01-25 09:22:01 -08:00
mustiikhalil
786f69b248 Formats cpp code (#6349) 2021-01-22 10:46:53 -08:00
Casper
1da0a2dfac Rust Object API (#6070)
* inital commit of rust object api

* Required fields support.

* clang fallthrough

* Fix unused variables

* just don't fall through

* remove comment

* s/panic/unreachable

* Tests for object API

* Added defaults

* deleted unintentionally added files and updated .bat file

* fix bat file

* clang format

* Cargo clippy checks

* remove commented out code

* clippy allows

* Remove matches! macro since we're not yet at Rust v1.42

* install clippy in RustTest.sh

* move line

Co-authored-by: Casper Neo <cneo@google.com>
2021-01-22 13:07:32 -05:00
Juan Cruz Viotti
796ed68faf Clarify that (Flat|Flex)Buffers do not deduplicate vector elements (#6415)
I'm also clarifying that while elements are not deduplicated, vectors
may contain more than one offset to the same value.

Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
2021-01-21 16:09:35 -08:00
Juan Cruz Viotti
7b1ee31d80 Clarify that FlatBuffers unions do not support scalars (#6416)
I mention that a common practice is to wrap the scalar value in a table
or a struct.

Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
2021-01-21 16:08:29 -08:00
Juan Cruz Viotti
4aff1198dd Explain how FlatBuffers encodes unions (#6414)
This is an attempt to explain how FlatBuffers encodes union types as an
extra section in the "FlatBuffers internals" document.

Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
2021-01-21 16:07:34 -08:00
Ivan Dlugos
ad3a729f96 dart Builder - expose finished buffer size (#6403) 2021-01-21 16:06:11 -08:00
Juan Cruz Viotti
52e2177069 Remove invalid claim that Protocol Buffers does not support unions (#6413)
Protocol Buffers v3 supports the `oneof` operator:
https://developers.google.com/protocol-buffers/docs/proto3#oneof

Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
2021-01-21 12:23:54 -08:00
Björn Harrtell
760c657551 [TS/JS] New gen TS code gen (#6302)
* TS/ES6 modules spike iteration 1

* Initial modularized dasherized output

* Remove obsoleted parts and namespace wrapping

* Use _flatbuffers_ prefix

* First part of imports logic

* Second part of imports logic

* Fix TS/JS code removal mixup

* Alias imported symbols if same name from different namespaces and some fixes

* Use star import for bare imports

* Fix messed up string concat

* var to const and remove not needed semi

* Remove some cases of ns prefixing

* Add missing space

* Cleanups

* Completed initial import tracking logic

* Compilable output

* Adjust TypeScriptTest and dependents to work

* Use local flatbuffers package for tests

* Refactor away use of any

* Remove obsolete imported_fileset and reexport_map

* Still need any and fix JavaScriptTest.sh

* Fix test runs out of the box

* Temp add generated files

* TypeScriptTest replaces JavaScriptTest and cleanups

* Also remove reference to JavaScriptTest in TestAll.sh

* Remove old generated ts/js files

* Remove use of --js in generate_code scripts

* idl_gen_js_ts to idl_gen_ts and removal of js gen

* Remove obsoleted options

* Fix obsolete ts test detection

* Tweak ts compilation be as strict as possible

* Remove jsdoc type annotation generation

* Generated test ts files

* Fix search and replace messup

* Regenerated ts test output

* Use CharToLower

* Use normal for loop

* Rework namespacedir

* Revert "Rework namespacedir"

This reverts commit 6f4eb0104ceeb86011bb076ebca901138c48e068.

* Revert "Use normal for loop"

This reverts commit 676b2135bfaa1853dfbb06c92b5c16a0d81bb13a.

* Revert "Use CharToLower"

This reverts commit 2d08648d0d72d0af201fad80d54cdc76412b35e9.

* Again do rework but correct

* Avoid runtime cast

* Fix test runs

* Also add npm install to get tsc

* Bump node test versions

* for range to std for loop

* Clang format

* Missed one clang format

* Move accessor to later

* Attempt to make windows version of TypeScriptTest

* Want to see the output

* Try to get newer node at appveyor

* Style changes
2021-01-19 12:51:13 -08:00
Vladimir Glavnyy
75c859e98f [idl_parser] Improve symbols lookup thru parent namespaces (#6407)
Added the new method LookupTableByName for searching symbols in parent namespaces.
This method speedup (x50 or greater) symbol resolution (enum or struct) in parent namespaces.
The speedup was measured without `table.empty()` guard condition.

This method should suppress fuzzer timeout issue without artificial limits for nested namespaces (https://oss-fuzz.com/testcase-detail/6244168439169024).

Additionally, this commit speedup (x5) the GetFullyQualifiedName method by removing unnecessary temporary std::string object.
2021-01-19 12:49:24 -08:00
Mark Spatz
91b0958c43 Search for includes in the directory containg the current file (#6371)
* [codegen] Search for includes in the directory containg the current file

This matches the behavior of a C preprocessor, see:
https://gcc.gnu.org/onlinedocs/cpp/Search-Path.html

* Skip FileExists() when file is guaranteed to not exist

* end comment with period to match others
2021-01-19 12:26:34 -08:00
Casper
8008dde117 Upgrade Rust dependencies (#6406)
Co-authored-by: Casper Neo <cneo@google.com>
2021-01-19 13:39:29 -05:00
Björn Harrtell
c81cf82492 [TS/JS] New gen TS code gen prequel to preserve history (#6404)
* Rename idl_gen_js_ts to idl_gen_ts to preserve history

* Change build related files
2021-01-14 11:34:44 -08:00
Casper
8573108bbe Unset FieldDef.optional if its key (#6402)
* Unset FieldDef.optional if its key

* removed StringKey table, just removed an extra "required, key"

* removed extra newline

Co-authored-by: Casper Neo <cneo@google.com>
2021-01-12 14:06:43 -05:00
Vladimir Glavnyy
7abe612b59 [fuzzer] Fix the binary schema loading in the monster_fuzzer (#6396)
This commit should fix issue 29374 in oss-fuzz.
2021-01-12 10:20:47 -08:00
Casper
408cf58024 Fix Rust UB problems (#6393)
* Fix miri problems by assuming alignment is 1 in rust

* Removed is_aligned fn from rust verifier.

* Add back is_aligned, but make it w.r.t. buffer[0]

* touch unused variable

* touch unused variable

* +nightly

* Move Rust miri testing into its own docker

* fix bash

* missing one endian conversion

* fix endianness2

* format stuff

Co-authored-by: Casper Neo <cneo@google.com>
2021-01-11 15:24:52 -05:00
brian-brt
39e115fdb4 Define Vector::value_type for STL compatibility (#6394)
Specifically, this lets the googlemock container matchers work with a
flatbuffers::Vector.

https://github.com/google/googletest/blob/master/googlemock/docs/cheat_sheet.md#container-matchers
2021-01-08 11:35:55 -08:00
Vladimir Glavnyy
85719669cb [fuzzer] Debug the monster_tets.bfbs on clusterfuzz server (#6392)
For some reason, this fuzzer failed to load the binary schema file
when run on the `/clusterfuzz` server.
Issue: https://oss-fuzz.com/testcase-detail/6215075358703616
This issue doesn't reproduce locally with the latest oss-fuzz docker image.
2021-01-08 11:09:41 -08:00
Dan Field
809fe49c7a Fix up scripts, fix generated enum value for strong mode, regenerate files (#6389)
* Fix up scripts, fix generated enum value for strong mode, regenerate files

* missing files

* GH action

* remove action
2021-01-07 14:51:52 -08:00
Aaron Son
41253e574e [go] tests/GoTest.sh: Fix flags.Parse location to work on new go SDKs. (#6388)
* tests/GoTest.sh: Fix flags.Parse location to work on new go SDKs.

Calling flags.Parse() within init() races with other packages which register
flags in their init(), and in particular with the testing package itself. It is
more reliable to call flags.Parse() from a TestMain implementation.

See https://github.com/golang/go/issues/31859,
https://github.com/golang/go/issues/33869.

* .github: Enable build-go action in build.yaml workflow.
2021-01-07 11:54:00 -08:00
Ivan Dlugos
08d2ce844b fix Dart Builder._writeString() - always write trailing zero byte (#6390) 2021-01-07 11:40:47 -08:00
Ivan Dlugos
a15a8d930d fix Dart Builder.reset() - clear vTables (#6386) 2021-01-07 11:28:33 -08:00
Vladimir Glavnyy
83ce29cc22 [C++, JSON] Fix nullptr access when reading a key with a default value. (#6375)
This commit fixes handling of default and NULL `key` fields in `Parser::ParseVector` (#5928).

The JSON generator updated. It outputs `key` fields even if the `--force-defaults` option is inactive.

Additional test cases for `key` added.
2021-01-07 11:24:59 -08:00
Richard A Hofer
4363c1d2cb Fix generated EndVector. (#6385) 2021-01-05 14:45:23 -08:00
Stefan de Konink
1bf1ec0270 Implements type promotion for Java enum (#6382)
* Implements type promotion for Java enum as suggested in #3890, #5111, #6369

* After generate_code.sh
2021-01-05 11:11:29 -08:00
Derek Bailey
0800976533 Delete label_notify.yml
Removing this workflow as it isn't used.
2021-01-05 10:06:33 -08:00
Wouter van Oortmerssen
795408115a Disabled PHP CI (#6381)
It errors with "Fatal error: Uncaught exception 'InvalidArgumentException' with message 'bad number  for type byte.. in /home/runner/work/flatbuffers/flatbuffers/php/ByteBuffer.php:490" which I can't reproduce locally, and trying to fix it on CI runs into PHP's insane handling of numbers vs strings.
2021-01-04 14:50:56 -08:00
Kamil Rojewski
46545e6273 fixed warnings (#6355)
* semver-compatible deprecations

* removed unneeded lifetimes (+ clippy warnings)

* silenced too many args warning
2021-01-04 14:11:59 -08:00
Bruno ZIKI Kongawi
0168178a17 Fix multiple fbs code generation failure (#6365) 2021-01-04 13:29:32 -08:00
Vladimir Glavnyy
82836a62be [idl_parser] Improve stack overflow protection (#6364)
* [idl_parser] Improve stack overflow protection

Add stack overflow protection for Flexbuffer and nested Flatbuffer parsers.
Replaces the `Recurse()` method by the new ParseDepthGuard RAII class.

* Remove move operator from Parser.

It was wrong decision to add move ctor and assignment into Parser class.
These operators will make it extremely difficult to add constant or reference fields in the future.

* Remove ';' from definition of FLATBUFFERS_DELETE_FUNC

* Format code

* Make this PR compatible with MSVC2010 (it doesn't support inherited ctor)
2021-01-04 12:39:12 -08:00
Vladimir Glavnyy
e7430bbebd [idl_parser] Check the range of explicitly set field's id value (#6363)
* [idl_parser] Check the range of explicitly set field's id value

The explicitly set `id` attribute should be a non-negative value of the `voffset_t` type.

* Format FieldIdentifierTest()
2021-01-04 12:33:31 -08:00
Richard A Hofer
24dd85fd28 Generate code to encode and decode nested flatbuffers in Python. (#6354)
* Generate code to encode and decode nested flatbuffers in Python.

* Delete accidental trailing whitespace.

* Fully delete trailing whitespace.
2021-01-04 12:18:19 -08:00
mustiikhalil
57f68e2896 [Rust] Shared String (#6367)
* Adds shared strings and tests for shared strings

* Adds resets on string_map

* Moved shared strings to use vector instead of hashmap

* Addresses all the issues

* Resolves some comments
2021-01-04 06:18:35 -08:00
mustiikhalil
44cf2bde19 Updates license date to 2021 (#6378) 2021-01-02 11:46:46 +03:00
Kamil Rojewski
be37d4da14 include_prefix support for rust (#6330)
* include_prefix support for rust

* include_prefix support for java

* formatting fixes

* Revert "include_prefix support for java"

* style fix
2020-12-30 12:02:36 -08:00
mustiikhalil
4e79d129cb [Swift] Rebuild the way swift handles structs from scratch (#6326)
* Rebuild the way swift handles structs from scratch

* Updates docs, and sample binary

* Replaces InMemory to Mutable

* Migrates docs from inmemory

* use inline for some functions

* Renamed Mutable objects

* Updates documentation
2020-12-17 14:55:32 -08:00
mqy
05192553f4 Fix typos in usage/comments; Make rust generator respect to --filenam… (#6342)
* Fix typos in usage/comments; Make rust generator respect to --filename-suffix

* run clang format
2020-12-17 13:15:27 -08:00
Kjetil Østerås
f2511d7d49 Renaming infinity variables in test.cpp (#6340)
The infinityf symbol is causing a conflict when building for cygwin. In
the cygwin math.h header there is also a symbol called infinityf. So
this patch is needed to be able to build the flatbuffer tests in a
cygwin environment.
2020-12-17 13:08:23 -08:00
Richard A Hofer
f8b203c9c4 Add vectorNumElements attribute to Builder for simpler vector creation. (#6328)
* Add vectorNumElements attribute to Builder for simpler vector creation.

This adds a default to EndVector which should simplify its use.

* Update tutorial to reflect new default arg in Python EndVector.

* Remove optional argument to Python EndVector.

* Add generated files.

* Unset Builder.vectorNumElems when not in use.
2020-12-14 12:00:28 -08:00
mustiikhalil
8ab7c7e2c8 [CI] Adds formatter to CI (#6272)
* Adds formatters CI

Adds Error message & setup formatting like cpp

Adds Swift

Adds typescript

Adds python

tests yarn

Adds format.md

* Removes unneeded scripts + moves install script to install phase

* Adds format.md content

* Adds cpp to the formatter.md and fixes ci

* Adds cpp to formatter ci
2020-12-11 10:46:10 -08:00
Wouter van Oortmerssen
7e00390289 Fix Max CI build path (#6333)
apparently the default xcodebuild path of ./build clashes with the BUILD file present (case insensitive file system?)
2020-12-11 10:31:20 -08:00
Stefan F
65c415911a Generate nullable properties in C# object-based API for optional scalars. (without -gen-mutable) (#6273)
* Added missing EndTable() call to VerifyObject()

VerifyObject called VerifyTableStart() but not EndTable(). This made Verifier::VerifyComplexity() increase depth_ with each table, not with the depth of tables.

https://groups.google.com/forum/#!topic/flatbuffers/OpxtW5UFAdg

* Added Check to VerifyAlignment

https://stackoverflow.com/questions/59376308/flatbuffers-verifier-returns-false-without-any-assertion-flatbuffers-debug-veri

* Add GetStringView (Convenience function to get string_view from a String returning an empty string_view on null pointer) like GetString, GetCstring

* flatc should warn, when an attribute is attached more than once.

flatc.exe -b duplicate.fbs
warning: duplicate.fbs(5, 36): warning: attribute already found: priority

duplicate.fbs:

namespace MyGame;

attribute "priority";

table Monster (priority:1, priority:2) {
}

root_type Monster;

* flatc should support --binary --schema with optional scalar fields.

This fixes 'error: Optional scalars are not yet supported in at least one the of the specified programming languages.' when calling flatc.exe --binary --schema with a schema containing optional scalars.

* Generate nullable properties in C# object-based API for optional scalars.
tests\generate_code.bat extended to test this.

Ran tests\generate_code.bat
Ran tests\Flatbuffers.Test\NetTest.bat

* %TEST_BASE_FLAGS% replaced with --gen-object-api in generate_code.bat, because only this is part of this PR. Added this same flag to generate_code.sh

* generate_code.bat and generate_code.sh changed to only test c# with object based api.
2020-12-10 16:50:30 -08:00
David P. Sicilia
a9e91116d2 [Python] Commit some orphan python genfile diffs. (#6325) 2020-12-10 16:43:40 -08:00
Derek Bailey
80a745d9b0 Fixed missing ending quotes in labeller (#6327) 2020-12-10 14:19:46 -08:00
tira-misu
9fca5e4f42 Add flatc option to inhibit all warnings #6005 (#6301)
* Fix C/C++ Create<Type>Direct with sorted vectors

If a struct has a key the vector has to be sorted. To sort the vector
you can't use "const".

* Changes due to code review

* Improve code readability

* Add generate of JSON schema to string to lib

* option indent_step is supported

* Remove unused variables

* Fix break in test

* Fix style to be consistent with rest of the code

* Add option --no-warnings to inhibit all warnings

* Fix order of member initialization

* Add documentation for --no-warnings
2020-12-10 14:03:53 -08:00
Vladimir Glavnyy
92a806b4e8 [fuzzer] Rename fuzzing dictionaries for oss-fuzz (#6318)
This commit makes the names of fuzzing dictionaries the same as the target binary names.
Also it explicitly limits size of test inputs to prevent failures in `regex` and fuzzing time-outs.
2020-12-10 14:02:01 -08:00
Casper
9c9baf6d58 bumprust (#6322)
Co-authored-by: Casper Neo <cneo@google.com>
2020-12-09 11:34:19 -08:00
Richard A Hofer
aafc5dc950 Set default initialSize for Builder to 0 (#6310)
* Set default initialSize for Builder to 0

* Change default size of builder to 1024.

This matches what the C++ and Java versions do.
2020-12-07 16:20:26 -08:00
Casper
442949bc11 Rust Flatbuffers Verifier (#6269)
* Updated comments and fixed a fundemental type error.

* bump rust flatbuffers semver

* Initial commit with verifier, need to clean up

* Verifier tested. Needs clean up and refactoring.

* Display for InvalidFlatbuffer and better errors for strings

* SimpleToVerify, some refactoring

* Combined VerifierType TableAccessorFuncBody into FollowType

* scrub todos

* Update Rust get_root functions.

There are 6 variants, with verifier options, default verifier options
and no verification "fast".

* Rename root fns

* inline

* Update to use thiserror

* fix for bad compiler

* improve error formatting

* Replace multiply with saturating_multiply

* saturating adds too

* Add docs disclaiming experimental verification system

Co-authored-by: Casper Neo <cneo@google.com>
2020-12-07 18:37:51 -05:00
David P. Sicilia
9064072e8c Version message should be a "STATUS" to avoid going to stderr. (#6316) 2020-12-07 11:50:29 -08:00
Austin Schuh
fd4c1b5ff7 Replace std::string and const char* CreateSharedString with string_view (#6315)
It is useful to be able to call CreateSharedString with a string_view.
A string_view can be implicitly converted from a std::string or a const
char*.  This means if string_view is available, we can use it instead of
both other functions and get all 3.
2020-12-07 11:49:24 -08:00
Vladimir Glavnyy
bc7eb8adeb [fuzzer] Fix mistakes in the parser and scalar fuzzers. (#6314)
The flatbuffers::Parser::Parse() isn't an idempotent method for schema parsing.
This commit removes a wrong for-loop that tried to check the same schema twice.
2020-12-07 11:47:33 -08:00
Richard A Hofer
fc960f3670 Add default to offset param of Python generated GetRootAs (#6312) 2020-12-07 11:34:07 -08:00
Vladimir Glavnyy
f437f0f7ed [fuzzer] Fix loading of schema in monster_fuzzer (#6308)
This is fix for (https://oss-fuzz.com/testcase-detail/6251772204810240)
2020-12-07 11:21:23 -08:00
Vladimir Glavnyy
7f33cf682a [C++] Switch flatc to --cpp-std c++11 C++ code generator (#6306)
Prior to this commit the default C++ code generator was `c++0x`.
A code generated with `c++0x` code-gen might have a vulnerability (undefined behavior) connected evolution of enums in a schema. This UB could break the backward compatibility if previously generated code casts an unknown enumerator to enum type that knows nothing about future enumerators added to the schema.

The main differences between `c++0x` and `c++11`:

- generated enums use explicitly declared underlying type;
- generated object-API tables don't declare default ctor() explicitly, instead of it default data member initializers are generated.

Please use `flatc --cpp-std c++0x` option for backward compatibility with old compilers.
2020-12-07 11:19:36 -08:00
Vladimir Glavnyy
8d9eae9ac9 [idl_parser] Unify parsing of NaN values read from .fbs and .json files (#6296)
This commit unifies parsing of NaN values read
from .fbs and .json files by converting them to unsigned NaN.
2020-12-07 11:12:58 -08:00
Wouter van Oortmerssen
2046bffa40 Moved various language tests from AppVeyor to GitHub Actions (#6300) 2020-12-03 11:26:27 -08:00
Casper
338393f854 Documentation updates for Optional Scalars (#6014) (#6270)
* Documentation updates for Optional Scalars

* Updated Support

* Reword stuff

* s/NULL/null

Co-authored-by: Casper Neo <cneo@google.com>
2020-11-24 14:05:39 -05:00
Vladimir Glavnyy
c27bc2d76f [C++] Add ParseJson(), Parser(Parser&&), update fuzzers (#6284)
- add a new method ParseJson to minimize failures during fuzzing
- add default (conditional) move-constructor for Parser
- add a new monster_fuzzer
- switch fuzzers to C++17 and `test/cpp17` generated code
2020-11-23 10:17:44 -08:00
Wouter van Oortmerssen
bc518a5127 Fixed FlexBufferBuilder asserting on duplicate keys 2020-11-23 09:02:40 -08:00
Derek Bailey
100c59054c Added a few more paths for auto labeler (#6281) 2020-11-19 22:13:19 -08:00
Austin Schuh
e58c182443 Add --require-explicit-ids to require explicit ids (#6277)
* Add --require-explicit-ids to require explicit ids

We just got bit by a well intentioned developer forgetting that field
order by default is the field index.  3 people missed it in review.

I'm looking at ways to make it harder to mess up.  We are requesting
that developers explicitly id all fields in tables.  Automatic (opt in
for others) enforcement of this will help the effort succeed.  This
patch adds a command line flag which lets the user require ids on all
fields in tables.

* Added docs to Compiler.md as well
2020-11-19 17:16:45 -08:00
schoetbi
69a8b2a579 idl_gen_json_schema.cpp: Changed generation of array element types (#6253)
* idl_gen_json_schema.cpp: Changed generation of array element types
#6175

* idl_gen_json_schema.cpp: Simplified indent generation as suggested by @vglavnyy
#6175
2020-11-19 16:17:03 -08:00
Kelvin Hu
25eba6f35f fix typo (#6280) 2020-11-19 09:08:52 -08:00
Derek Bailey
e1f0f75ba6 Updated Ms build Action to fix build issue (#6279) 2020-11-19 00:20:47 -08:00
James Kuszmaul
faeb04fbe1 Add type annotation to unspecified array (#6264)
The lack of any type on the `ret` variable was causing our typescript compiler to complain.
2020-11-16 10:49:53 -08:00
mustiikhalil
537212afee [Swift] Adds a format file and reformats the swift project (#6250)
* Formats files & adds licence

* Revert arrays

* Keeps array indentation as is

* Adds licence to code formatter

* Updates code generators
2020-11-16 19:52:38 +03:00
mustiikhalil
6764f25d91 Adds a fix for enum generation (#6263) 2020-11-15 17:50:12 +03:00
AdamKorcz
d6a8dbd26f Experimental fix for failing oss-fuzz coverage build (#6259) 2020-11-13 15:48:05 -08:00
Casper
ed391e1777 BREAKING: Rust flexbuffers serde human readable set to false (#6257)
Co-authored-by: Casper Neo <cneo@google.com>
2020-11-13 07:53:01 -08:00
AdamKorcz
a49531414b Update to flags in fuzzing-cmake file (#6256) 2020-11-12 10:21:34 -08:00
Jason Lenz
de1f0342c8 Remove _POSIX_C_SOURCE and _XOPEN_SOURCE definitions when compiling o… (#6205)
* Remove _POSIX_C_SOURCE and _XOPEN_SOURCE definitions when compiling on OpenBSD

* Only define _POSIX_C_SOURCE and _XOPEN_SOURCE for mingw/cygwin platforms

* Only define POSIX statements for mingw/cygwin/qnx platforms
2020-11-12 10:12:32 -08:00
Stefan F
d0d51e2a5c flatc should support --binary --schema with optional scalar fields. (#6252)
* Added missing EndTable() call to VerifyObject()

VerifyObject called VerifyTableStart() but not EndTable(). This made Verifier::VerifyComplexity() increase depth_ with each table, not with the depth of tables.

https://groups.google.com/forum/#!topic/flatbuffers/OpxtW5UFAdg

* Added Check to VerifyAlignment

https://stackoverflow.com/questions/59376308/flatbuffers-verifier-returns-false-without-any-assertion-flatbuffers-debug-veri

* Add GetStringView (Convenience function to get string_view from a String returning an empty string_view on null pointer) like GetString, GetCstring

* flatc should warn, when an attribute is attached more than once.

flatc.exe -b duplicate.fbs
warning: duplicate.fbs(5, 36): warning: attribute already found: priority

duplicate.fbs:

namespace MyGame;

attribute "priority";

table Monster (priority:1, priority:2) {
}

root_type Monster;

* flatc should support --binary --schema with optional scalar fields.

This fixes 'error: Optional scalars are not yet supported in at least one the of the specified programming languages.' when calling flatc.exe --binary --schema with a schema containing optional scalars.
2020-11-12 10:06:09 -08:00
Ivo List
33ab26017d Bump version of rules_go to 0.24.5 (#6234)
Versions of rules_go below 0.24 include platforms repository, which are lacking
definitions of os:macos, cpu:arm64 constraints.

This definitions are needed to properly select toolchains in latest
bazel.
2020-11-12 10:04:34 -08:00
Derek Bailey
febb9d87c9 Union As Accessors for C# (#6251)
* Union As Accessors for C#

* Changed loop to be compatible with older compilers

* errant change fix
2020-11-12 09:28:21 -08:00
mustiikhalil
8778dc7c2b Resets buffer without deallocating current pointer (#6247)
Resets buffer without deallocating current pointer & Bumps version
2020-11-09 17:55:53 +03:00
OptoCloud
aae376e9a8 Add GetBufferSpan() function to bufferbuilder (#6235)
This will return the finished internal buffer data as a span
2020-11-05 18:09:01 -08:00
Paulo Pinheiro
0ff0471488 Modernize android build and sample (#6229)
Android build was dated, using the Android.mk approach. Current
project configuration on Android encourages the usage of CMake, so we
are updating the android project as an example on how to use either the
Java/Kotlin generate code or the native C++ one.
2020-11-05 14:23:37 -08:00
Casper
46a8c7e958 Added required-nested-flatbuffer to monster_test and fixed rust (#6236)
Co-authored-by: Casper Neo <cneo@google.com>
2020-11-05 13:58:06 -08:00
Derek Bailey
bc56c553ec Notify based on Labelling issues and PR (#6241) 2020-11-05 13:40:01 -08:00
Derek Bailey
07d7cd78a7 Converted globs to use single quotes (#6240) 2020-11-05 13:12:19 -08:00
Derek Bailey
cdef70e246 More adjustments to the auto labeler (#6239) 2020-11-05 13:02:33 -08:00
Derek Bailey
9dd44df35c Updated Lua labeller glob (#6238) 2020-11-05 12:50:33 -08:00
Charlie Yin
c9b29d0885 Support size-prefixed buffers and add tests for size-prefixed messages (#6232) 2020-11-05 11:23:56 -08:00
mustiikhalil
fba93e0abb Removes duplicate swift in labeler (#6228)
Removed grpc tag from swift label set & removes duplicate swift label
2020-10-31 18:35:44 +03:00
Derek Bailey
d1a545b1fb Added more labels for auto labeler (#6227) 2020-10-30 14:01:35 -07:00
Derek Bailey
ea92a668d8 [C#] Optional Scalars (#6217)
* [C#] Optional Scalars

* Moved scalar optional check to avoid null enum conversion
2020-10-30 13:18:59 -07:00
Paulo Pinheiro
6034de286a [Label Bot] Add Java and Kotlin support for the label bot (#6226)
CI is an unrelated timeout.
2020-10-30 13:18:36 -07:00
Casper
b08b0a4402 Implement Debug trait for Rust flatbuffers. (#6207)
* Refactor idl_gen_rust to a ForAllX continuation pattern.

* Removed unneeded SetValue and updated sample rust gencode

* Make Rust flatbuffers print right

* Generated code and removed unnecessary trait constraint

* bumped rust version. Release required

* removed an unwrap in Rust Debug-print unions

* Tested formatting flatbuffers in rust.

* Set float precision in flaky debug-print test

* impl Debug for structs too

Co-authored-by: Casper Neo <cneo@google.com>
2020-10-29 12:57:29 -07:00
mustiikhalil
17ae48decc [Label Bot] Adds some languages to labeler bot (#6222)
* Adds swift, ts, go, py to labeler bot

* Removes unneeded dir search
2020-10-29 09:05:25 +03:00
Derek Bailey
fc80979253 Auto Labeler Setup, Just C# for now (#6221) 2020-10-28 22:02:32 -07:00
Derek Bailey
55658f523a Auto Labeler Setup, Just C# for now (#6218)
* Auto Labeler Setup, Just C# for now

* Removed trailer comma as that might caused the issue

* Removed any[].

* Changed .cs glob
2020-10-28 21:25:02 -07:00
Casper
14ecfe4236 Updated comments and fixed a fundamental type error. (#6214)
* Updated comments and fixed a fundemental type error.

* bump rust flatbuffers semver

Co-authored-by: Casper Neo <cneo@google.com>
2020-10-28 19:23:22 -07:00
Kamil Rojewski
a0182cdb11 optional scalars for ts/js (#6215)
* optional scalars for ts/js

* removed range based for

* removed range based for
2020-10-28 09:45:23 -07:00
Derek Bailey
0dfcc0a378 Adds NetTest.bat to run .NET Core tests on Windows (#6216) 2020-10-27 22:51:58 -07:00
Paulo Pinheiro
f9a18ea635 [Java] Implement optional scalars (#6212)
Java implementation of optional scalars, following issue #6014.
2020-10-26 17:46:50 -07:00
mustiikhalil
c7586e85aa Empties the sharedString map on reset on go and csharp (#6187)
Fixes go tests
2020-10-26 12:38:24 -07:00
Derek Bailey
914c646014 Removed C# references from java generator. Move annotations closer to definitions (#6204) 2020-10-25 21:42:29 -07:00
mustiikhalil
42d7c79977 Adds readable size to asserts in read functions (#6210) 2020-10-25 15:59:30 +03:00
Casper
e68e8d7de9 Refactor idl_gen_rust (#6206)
* Refactor idl_gen_rust to a ForAllX continuation pattern.
* Updated rust sample code

Co-authored-by: Casper Neo <cneo@google.com>
2020-10-24 15:46:55 -07:00
Vladimir Glavnyy
84809be7e7 Fix typo in flatbuffers::span declaration. (#6202) 2020-10-22 14:07:29 -07:00
Paulo Pinheiro
1606fb6375 Kotlin test optional enum (#6201)
* Add test for optional enums in Kotlin

* Rename optional_scalars2.fbs into optional_scalars.fbs

Also updated all references in the project to point to
"optional_scalars.fbs" instead of "optional_scalars2.fbs".
2020-10-22 13:36:02 -07:00
Casper
fe8e3c7e5d Mass Refactoring to use IsString and other BASE_TYPE helpers (#6193)
* Updated SupportsAdvancedUnionFeatures to look out for string

* Mass refactoring to use BASE_TYPE helper functions.

Co-authored-by: Casper Neo <cneo@google.com>
2020-10-20 08:00:31 -07:00
Casper
8f6fa4b715 Updated SupportsAdvancedUnionFeatures to look out for string (#6190)
Co-authored-by: Casper Neo <cneo@google.com>
2020-10-19 13:16:00 -07:00
Björn Harrtell
b46db38f57 [JS/TS] Rewrite flexbuffers JS to TS (#6148)
* Partial TS rewrite

* Completed port but bugs remain

* Expose builder function

* Break out and fix stack-value and formatting
2020-10-19 13:11:35 -07:00
Casper
9fa1d27059 Rework enums in rust. (#6098)
* Rework enums in rust.

They're now a unit struct, rather than an enum. This is a
backwards incompatible change but the previous version had UB
and was also backwards incompatible so...

* Update and test sample rust flatbuffers

* Use bitflags crate to properly support rust enums.

Previously, the bitflags attribute was just ignored. This is a breaking change
as the bitflgs API is not like a normal rust enum (duh).

* variant_name() -> Option<_>

* repr transparent

* Reexport bitflags from flatbuffers

* Make bitflags constants CamelCase, matching normal enums

* Deprecate c-style associated enum constants

Co-authored-by: Casper Neo <cneo@google.com>
2020-10-19 11:40:03 -07:00
schoetbi
a402b3abae idl_gen_json_schema Fix generation of arrays of enums (#6184)
* idl_gen_json_schema.cpp: Fixed generation of arrays of enums
#6175

* Fixed failing unit tests

* GenBaseType generate "integer".
Fixes #6066

* Ran tests/generate_code.

* Removed modern R"()" strings

* changed std::to_string to NumToString
2020-10-16 10:43:09 -07:00
Niels Hofmans
0e1415b996 fix(go_test): remove deprecated grpc call (#6183) 2020-10-15 15:47:06 -07:00
tira-misu
5cd7137103 Add generation of JSON Schema to library (#6165)
* Fix C/C++ Create<Type>Direct with sorted vectors

If a struct has a key the vector has to be sorted. To sort the vector
you can't use "const".

* Changes due to code review

* Improve code readability

* Add generate of JSON schema to string to lib

* option indent_step is supported

* Remove unused variables

* Fix break in test

* Fix style to be consistent with rest of the code
2020-10-15 15:28:25 -07:00
dependabot[bot]
5be777e1d4 Bump junit from 4.12 to 4.13.1 in /grpc/tests (#6173)
Bumps [junit](https://github.com/junit-team/junit4) from 4.12 to 4.13.1.
- [Release notes](https://github.com/junit-team/junit4/releases)
- [Changelog](https://github.com/junit-team/junit4/blob/main/doc/ReleaseNotes4.12.md)
- [Commits](https://github.com/junit-team/junit4/compare/r4.12...r4.13.1)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2020-10-12 22:58:29 -07:00
dependabot[bot]
a49d440ecd Bump junit from 4.12 to 4.13.1 in /grpc (#6172)
Bumps [junit](https://github.com/junit-team/junit4) from 4.12 to 4.13.1.
- [Release notes](https://github.com/junit-team/junit4/releases)
- [Changelog](https://github.com/junit-team/junit4/blob/main/doc/ReleaseNotes4.12.md)
- [Commits](https://github.com/junit-team/junit4/compare/r4.12...r4.13.1)

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2020-10-12 22:58:03 -07:00
astange
4ec5e8db90 [C++] Add option to not generate direct copy methods. (#6166)
* Add option to not generate direct copy methods.

The direct copy methods generated by flatc utilize std::vector which isn't allowed on some embedded systems. Permit users of the compiler to not generate these methods so they don't have to be stubbed out.

* Update docs for no-cpp-direct-copy option.
2020-10-12 12:25:10 -07:00
Vladimir Glavnyy
04bec23a37 Add Array initialization from struct constructor (#5865) (#6147)
- add flatbuffers::span
- add new constructor for `struct` with `array`
- add some test for flatbuffers::span and 'arrays_test.fbs'
2020-10-12 12:24:18 -07:00
astange
77d57fd075 Cast to right type for reserved_ subtraction (#6167)
reserved_ is a size_t so ensure that the output of cur_ - buf_ is cast to that to avoid compiler warnings.
2020-10-12 09:14:45 -07:00
Casper
543c1bbeba Fixed rust nested flatbuffers for tables other than self (#6062)
* Fixed rust nested flatbuffers for tables other than self

* replaced lifetimes

* Use WrapInNameSpace and also update samples

Co-authored-by: Casper Neo <cneo@google.com>
2020-10-10 11:49:02 -07:00
Anass Al
cb971eece8 [C++] Fix -Wnarrowing and -Woverflow due to signed bitfields on G++ ARM (#6163)
Older versions of GCC (at least on ARM) complain about narrowing conversions and
overflows when setting the reference index (defined as an 11-bit signed integer
bitfield) to -1:

```
error: narrowing conversion of '-1' from 'int' to 'short unsigned int' inside { } [-Wnarrowing]
error: conversion from 'short unsigned int' to 'short unsigned int:11' changes value from '65535' to '2047'
```

This is due to the signedness of integer bitfields (not explicitly signed or
unsigned) being implementation-defined. This addresses this issue by explicitly
defining the signedness of all the bitfields in `flatbuffers::TypeCode` in C++.
2020-10-08 08:00:40 -07:00
mustiikhalil
7b9e61fccf [TS] GRPC Implementation (#6141)
* GRPC implementation for Typescript

* Fixes a couple of issues

* Finished implementing the typescript support for grpc

* Updated generated code

* Fixes CI
2020-10-07 09:56:30 -07:00
Casper
3359e3042f Moved C++ to optional_scalars2 and added some tests. (#6162)
* Moved C++ to optional_scalars2 and added some tests.

Also deleted unused optional_scalars_generated.lobster

* Fixed whitespece in C++ gencode & fixed BUILD file

* Moved C++ onto optional_scalars2 in the .bat file

Co-authored-by: Casper Neo <cneo@google.com>
2020-10-07 09:46:09 -07:00
ImmConCon
187a4787f9 [Rust] Upgrade flatbuffers library to 2018 edition (#6159)
* [Rust] Upgrade flatbuffers lib to 2018 edition

* Pushed version
2020-10-06 15:55:22 -07:00
mrmarkwell
08943aa26f Flatbuffer C++ UnpackTo optimization for vectors of non-bool bytes. (#6154)
UnpackTo copies vector elements one-by-one, which can be very inefficient depending on the quality of the compiler optimizations performed. This change updates the operation for vectors of bytes that aren't enums to use 'std::copy', which is usually highly optimized.

vectors of types that are more than one byte can't be optimized in this way because of the endianness of the serialized bytes vs. the target architecture endianness.

vectors of enums can't be optimized because they are required to be static_cast into the appropriate enum type when stored in the vector.

vectors of bools can be optimized in most cases, but since the standard
allows std::vector<bool> template specialization for space-savings,
std::copy doesn't work on every implementation (looking at you
Microsoft). Thus, this optimization is skipped for vector<bool>.

For a specific example, this improves the latency of unpacking large buffers on the Hexagon DSP by about 10x.

Co-authored-by: Matthew Markwell <markwell@google.com>
2020-10-06 10:56:45 -07:00
mustiikhalil
5975658ebd Enables optional enums in swift (#6160)
[Swift] Enables optional enums
2020-10-06 19:00:14 +03:00
Wouter van Oortmerssen
5d3cf440e5 Updated Lobster test for optional bools/enums
The codegen for this was already correct, just added more tests
2020-10-02 10:53:47 -07:00
Casper
8ec8322f09 Ruopt enum (#6156)
* Rust support for optional enums

* make optional_scalars2 for languages that support optional enums

Co-authored-by: Casper Neo <cneo@google.com>
2020-10-02 10:30:39 -07:00
Vladimir Glavnyy
bbcc85fd4c Fix generation of C++ code with Optional<Enum> (#6155) 2020-10-02 10:27:30 -07:00
Conan Chen
0bdf2fa156 [C#] Fix and improve project files (#6142)
* [C#] Fix and improve project files

* "net35" target included for Unity 5
* "net46" target included for Unity 2017
* "netstandard2.0" target included for Unity 2018 and general use
* "netstandard2.1" target included for Span<T> support

Included project properties for defining UNSAFE_BYTEBUFFER, BYTEBUFFER_NO_BOUNDS_CHECK, and ENABLE_SPAN_T conditional compilation symbols.

* Add documentation on building for C#
2020-09-28 11:56:59 -07:00
Kamil Rojewski
2eedc769d5 possibility to create a vector from an iterator (#6135) 2020-09-28 09:45:48 -07:00
Stefan F
ab01ae1620 flatc should output a warning, when an attribute is attached more than once (#6146)
* Added missing EndTable() call to VerifyObject()

VerifyObject called VerifyTableStart() but not EndTable(). This made Verifier::VerifyComplexity() increase depth_ with each table, not with the depth of tables.

https://groups.google.com/forum/#!topic/flatbuffers/OpxtW5UFAdg

* Added Check to VerifyAlignment

https://stackoverflow.com/questions/59376308/flatbuffers-verifier-returns-false-without-any-assertion-flatbuffers-debug-veri

* Add GetStringView (Convenience function to get string_view from a String returning an empty string_view on null pointer) like GetString, GetCstring

* flatc should warn, when an attribute is attached more than once.

flatc.exe -b duplicate.fbs
warning: duplicate.fbs(5, 36): warning: attribute already found: priority

duplicate.fbs:

namespace MyGame;

attribute "priority";

table Monster (priority:1, priority:2) {
}

root_type Monster;
2020-09-28 09:28:47 -07:00
Anass Al
689bfafa7e [Python/JS/TS] Codegen SizeOf method for structs (#6136)
* [Python] Codegen SizeOf classmethod for structs

This codegens a `SizeOf()` classmethod for all structs since we can't
determine the size of a FlatBuffer generated struct from Python otherwise.

* [JS/TS] Codegen sizeOf static method for structs

This codegens a `sizeOf()` static method for all structs since we can't
determine the size of a FlatBuffer generated struct from JavaScript or
TypeScript otherwise.
2020-09-28 08:54:50 -07:00
石晓伟
641309a5bf unix2dos on tests/FlatBuffers.Test/FlatBuffers.Core.Test.csproj (#6133) 2020-09-28 08:10:06 -07:00
春条
52e3628794 SpanT is available in .Net Standard 2.0. (#6137) 2020-09-25 10:39:28 -07:00
Nat Jeffries
dca12522a9 Add static cast to avoid implicit double promotion. (#6132)
Add static cast from float to double in flexbuffers.h to avoid implicit double promotion error. This error is surfacing during the tensorflow lite for microcontrollers build since we enabled -Werror and -Wdouble-promotion.
2020-09-24 09:35:29 -07:00
春条
e0bbaa6f9c [C#]Change to ENABLE_SPAN_T that doesn't require UNSAFE_BYTEBUFFER. (#6073)
* ENABLE_SPAN_T doesn't require UNSAFE_BYTEBUFFER.

Change to ENABLE_SPAN_T that doesn't require UNSAFE_BYTEBUFFER.

* Selectable framework.

Changed target framework to allow selection of 2.0 or 2.1 (or higher)

* Added target framework version check.

* Add core test project.

* Added run on .Net Core.
2020-09-23 13:16:00 -07:00
Derek Bailey
ab139d6bea Revert "[C#] Fix and improve project files (#6116)" (#6130)
This reverts commit 750281630b.
2020-09-23 13:14:51 -07:00
Mark Nauwelaerts
34d67b425e Minireflect fixed array (#6129)
* CMakeLists: also really generate optional_scalars as needed by test elsewhere

* [C++] Handle fixed-length array in minireflection

Fixes #6128
2020-09-22 17:57:01 -07:00
Björn Harrtell
96d5e35977 [JS/TS] fix flatbuffers default export (#6123)
* [TS/JS] Export flatbuffers namespace as default export

* [TS/JS] Fully qualified name

* Export as default and named
2020-09-21 09:46:24 -07:00
Vladimir Glavnyy
eb686a86f3 Add missed file generated by optional_scalar.fbs (#6125) 2020-09-21 09:37:01 -07:00
Conan Chen
750281630b [C#] Fix and improve project files (#6116)
* [C#] Fix and improve project files

* "net35" target included for Unity 5
* "net46" target included for Unity 2017
* "netstandard2.0" target included for Unity 2018 and general use
* "netstandard2.1" target included for Span<T> support

Included project properties for defining UNSAFE_BYTEBUFFER, BYTEBUFFER_NO_BOUNDS_CHECK, and ENABLE_SPAN_T conditional compiler constants.

* Add documentation on building for C#
2020-09-21 09:32:59 -07:00
Vladimir Glavnyy
fb4e1c34f9 Add CharToLower and CharToUpper into util.s (#6126)
This commit adds replacement of `::tolower` and `::toupper`.
Added CharToLower and CharToUpper routines reduce the number of cast operators
that required for correct usage of standard C/C++ `::tolower/toupper` routines.
2020-09-21 09:31:27 -07:00
Vladimir Glavnyy
8c67b5b129 Add support of Optional<T> scalars to C++ code generator (#6092) 2020-09-18 12:51:17 -07:00
Paulo Pinheiro
6228b66d3d [Kotlin] Support for optional scalars. (#6115)
More information on #6014
2020-09-17 23:31:51 -07:00
mustiikhalil
e1be8aaadd Bump version for latest swift version (#6121) 2020-09-17 12:28:42 -07:00
Björn Harrtell
94873e595c [JS/TS] Modernize TypeScript / JavaScript flatbuffers support (#6095) 2020-09-17 12:21:14 -07:00
Anass Al
b8e87fafe4 [JS] Add getFullyQualifiedName() (#6119)
This optionally generates a static `getFullyQualifiedName()` function to get
the fully-qualified name of a type in JavaScript and TypeScript in a similar
fashion to the C++ codegen.
2020-09-17 12:19:07 -07:00
Diogo Monteiro
f96d1ef744 [Java] allowing larger buffer sizes when growing a byte buffer (#6118)
Related with issue #6113.

`(old_buf_size & 0xC0000000) != 0` checks if we can duplicate old_buf_size
 and still be under 2GB (by checking if bit 30 or 31 is 1). This doesn't
allow buffers larger than 1GB.

The strategy now is to allocate a buffer with the maximum array size
when we detect that we are overflowing the 2GB.

Also changed default buffer size to 1024.
2020-09-17 11:55:24 -07:00
mustiikhalil
89435303b7 [Swift] Migrates struct write APIS to write directly to the buffer (#6093)
* Migrates struct write APIS to in place APIS

* Fixes indentation in grpc swift
2020-09-17 17:10:59 +03:00
Casper
c75ae24293 Optional-ness in reflection (#6097)
* Optional scalars in reflection

* fixed name collision

* Remove code duplicated by merge

Co-authored-by: Casper Neo <cneo@google.com>
2020-09-11 14:20:32 -07:00
Casper
338944d3d9 Rename Nullable scalars to Optional scalars (#6112)
Co-authored-by: Casper Neo <cneo@google.com>
2020-09-10 13:04:36 -07:00
Ruslan Burakov
f5ab24bc41 Avoid memcpy call for empty vectors (#6111)
Co-authored-by: kuddai <kuddai92@gmail.com>
2020-09-10 13:03:15 -07:00
Maxim Zaks
92a8c1a0f2 [JS] FlexBuffers Fix for wrong type of offset and length values (#6107)
* Adding FlexBuffers support for Dart language

* Introduce snapshot method.

* Fix docu

* Replacing extension methods with static methods in order to support older Dart version

* Improving code based on PR feedback. Mainly rename refactoring.

* Addressing all PR feedback which does not need clarification

* exchange dynamic type with Object

* Adds better API documentation.
[] operator throws a very descriptive exception in case of a bad key.

* Implementation of JavaScript FlexBuffers decoder

* implements JS FlexBuffers builder

* replacing _toF32 with Math.fround

* Introducing test for BigInt number

* Moving functions from BitWitdth & ValueType object into BitWidthUtil and ValueTypeUtil accordingly.
Removing defensive checks.
Introducing fix for large int numbers by converting them to BigInt type.
Introducing handling for BigInt type in `add` method.
Using TextEncoder and Decoder to handle string / utf8 conversion.

* rename variable

* Lets user turn deduplication strategies for strings, keys and vector of keys off while building FlexBuffer.
Implements quick sort and choses quick sort if the number of keys is bigger then 20.
Removes unnecessary dict lookups in BitWidthUtil helper functions

* make iwidth and uwidth computation simpler and faster

* Making redInt and readUint a bit faster and shim the BigInt / BigUint usage

* Fixing a bug in FlexBuffers JS, where offsets and lengths are stored and read as int and not as uint values.

* Fixing a bug in FlexBuffers Dart, where offset and length values are stored and read as int values instead of uint values
2020-09-10 12:36:37 -07:00
mugisoba
6cea45dcd3 fix c# json serializer commandline argument docs (#6104) 2020-09-09 15:02:24 -07:00
Laurentiu Cristofor
fec58aa129 Fix for issue 6100: incorrect shrinking logic in ResizeContext() (#6102) 2020-09-04 15:52:13 -07:00
Maxim Zaks
71aca81ff9 [JS] FlexBuffers Support (#5973)
* Adding FlexBuffers support for Dart language

* Introduce snapshot method.

* Fix docu

* Replacing extension methods with static methods in order to support older Dart version

* Improving code based on PR feedback. Mainly rename refactoring.

* Addressing all PR feedback which does not need clarification

* exchange dynamic type with Object

* Adds better API documentation.
[] operator throws a very descriptive exception in case of a bad key.

* Implementation of JavaScript FlexBuffers decoder

* implements JS FlexBuffers builder

* replacing _toF32 with Math.fround

* Introducing test for BigInt number

* Moving functions from BitWitdth & ValueType object into BitWidthUtil and ValueTypeUtil accordingly.
Removing defensive checks.
Introducing fix for large int numbers by converting them to BigInt type.
Introducing handling for BigInt type in `add` method.
Using TextEncoder and Decoder to handle string / utf8 conversion.

* rename variable

* Lets user turn deduplication strategies for strings, keys and vector of keys off while building FlexBuffer.
Implements quick sort and choses quick sort if the number of keys is bigger then 20.
Removes unnecessary dict lookups in BitWidthUtil helper functions

* make iwidth and uwidth computation simpler and faster

* Making redInt and readUint a bit faster and shim the BigInt / BigUint usage
2020-08-31 12:16:55 -07:00
Vladimir Glavnyy
04d87ffec3 [C++] Small refactoring of the C++ code generator (#6091)
This commit performs refactoring (Extract Method) of the C++ code generator.
It extracts code generation of a table getter/mutator into separated methods.
2020-08-31 12:13:23 -07:00
Navin Mohan
bb25956f09 Wrap verify file id condition in Check call (#6085)
Run clang-format

Update testcase
2020-08-31 11:32:13 -07:00
Wybe Westra
49f4948f06 + Add removable-media plug to the snapcraft config (#6083) 2020-08-24 09:55:38 -07:00
mustiikhalil
eeacc53d22 Adds proper access types for swift object api & flatbuffers & grpc (#6081)
Fixes accessor in greeter.swift

Small fix for ci
2020-08-22 22:39:09 +03:00
Wouter van Oortmerssen
f3003e08d0 [Lobster] missed a test 2020-08-18 14:29:44 -07:00
Wouter van Oortmerssen
d713a00843 [CMake] enabled multi-core builds in VS projects 2020-08-18 14:00:22 -07:00
Wouter van Oortmerssen
77f966f89f [Lobster] optional scalars support 2020-08-18 14:00:02 -07:00
Yoon Kyong Sik
e86d5b8e97 [Kotlin] Attach JvmStatic annotation to each method in companion object (#6052)
* Attach JvmStatic annotation to each method of companion object

Kotlin does not have static accessor so companion object used instead of static.
It's so natural. But when use kotlin companion object methods on java it is very inconvenient.
```java
GeneratedClassByFlatBuffer.Companion.someMethod()
```

If use @JvmStatic annotation it can be shorten like below.
```java
GeneratedClassByFlatBuffer.someMethod()
```

* Formatting by Idea Google C++ style

* Add comments - Commit for missing cla

Signed-off-by: Yoon KyongSik <sam1287@gmail.com>

* Add comments - Commit for missing cla

Signed-off-by: Yoon KyongSik <sam1287@gmail.com>

* Reset code formatting except modified lines

Signed-off-by: Yoon KyongSik <sam1287@gmail.com>

* Pass missing flag to validateVersion method

Signed-off-by: Yoon KyongSik <sam1287@gmail.com>

* Add annotations to missing method in companion object

* addVector
* createVector
* endVector
* tableCreator

And also I tried add compiler option for generate annotation who don't like this operation.

Signed-off-by: Yoon KyongSik <sam1287@gmail.com>

* Modify unmatched option name in compiler usage

Signed-off-by: Yoon KyongSik <sam1287@gmail.com>

* Add missing operation for finishSizePrefixed and finishStructBuffer method.

Signed-off-by: Yoon KyongSik <sam1287@gmail.com>

* Add compiled example with --kotlin-gen-jvmstatic option.

Signed-off-by: Yoon KyongSik <sam1287@gmail.com>

* Fix Compiler.md

Change option name from --gen-jvm-static-annotation to --kotlin-gen-jvmstatic

Signed-off-by: Yoon KyongSik <sam1287@gmail.com>

* Use IDLOptions reference instead of bool parameter.

Signed-off-by: Yoon KyongSik <sam1287@gmail.com>

* Change option name - kotlin_gen_jvmstatic to gen_jvmstatic

Signed-off-by: Yoon KyongSik <sam1287@gmail.com>

* Use IDLOptions reference instead of bool parameter and missing process @JvmStatic as suffix.

Signed-off-by: Yoon KyongSik <sam1287@gmail.com>

* Add code generation for --gen-jvmstatic option

Signed-off-by: Yoon KyongSik <sam1287@gmail.com>

* Miss typo directory for including.

Signed-off-by: Yoon KyongSik <sam1287@gmail.com>

* Miss typo variable suffix for including.

Signed-off-by: Yoon KyongSik <sam1287@gmail.com>

* Fix camel case to snake case.

Signed-off-by: Yoon KyongSik <sam1287@gmail.com>

* Rollback generate code for gen_jvmstatic option.

Signed-off-by: Yoon KyongSik <sam1287@gmail.com>

* Delete generated test files.

Signed-off-by: Yoon KyongSik <sam1287@gmail.com>

* add missing new line at end of file.

Signed-off-by: Yoon KyongSik <sam1287@gmail.com>

* Remove generated test file by command line.

Signed-off-by: Yoon KyongSik <sam1287@gmail.com>

Co-authored-by: sam <sam@jennifersoft.com>
2020-08-18 09:44:43 -07:00
Derek Bailey
db2aa9b4ec [C#] Cleaned up .NET testing script for Mono (#6016)
* Cleaned up .NET testing script for Mono

Cleaned up the .NET testing script to make it a little better. It
purposefully doesn't delete the .NET installer and SDk after running the
script, so that they can be used in subsequent invocations. This greatly
speeds up the script.

The downloaded files are ignored by git by default. They can be
explicitly cleaned up by runnning the clean script (clean.sh).

* Trying using older Version indicator

* Remove lins to monsterdata and reference it directly.

* Updated appveryor script to remove copying of files no longer needed

* Continue to update appveyor script to work. Disabled CS0169 in ByteBufferTest
2020-08-17 13:10:10 -07:00
mustiikhalil
63cc0eec4e Adds a serialize helper function to native table (#6059)
* Adds a serialize helper function to native table
* Updated version
2020-08-04 13:53:40 +03:00
Max Burke
c30a87de6f [TS] Fix four bugs with imported types in TypeScript. (#6054)
* [TS] Fix four bugs with imported types in TypeScript.

* When a type had a vector of imported enums:

1) the enum type's file wasn't added to the generated code's list of
imports; and

2) the enum wasn't prefixed with the NS<hash> prefix and wasn't getting
resolved; but

3) non-enum types (ie, "flatbuffers.Offset") were getting the NS<hash>
prefix when they weren't.

* Also, type name prefixes weren't properly attributed with imported
structs in unions because the source definition passed to the typename
prefixing method was for the union, not for the location of the imported
struct.

* clang fmt

* Use of enum_def / struct_def for prefixing types needs to have the files
added to imported files when not generating all types.

* clang fmt
2020-08-03 12:08:52 -07:00
Liu Liu
a0fb30575c [Swift] Append namespace for Swift Grpc implementation (#6049)
* [Swift] Append namespace for Swift Grpc implementation
* Separate ServiceName from ServiceQualifiedName.
2020-07-31 10:07:45 +03:00
Omer Akram
77c18c1d69 export a __version__ variable for python module (#5309)
* export a __version__ variable for python module

* assign version to be used below

* better support python2

* Add copyright header
2020-07-30 15:56:21 -07:00
Kamil Rojewski
f1f23d08ed adding fb import when no other imports are present (#6030)
Co-authored-by: Kamil Rojewski <kamil.rojewski@gmail.com>
Co-authored-by: Wouter van Oortmerssen <aardappel@gmail.com>
2020-07-30 15:55:11 -07:00
mustiikhalil
f1025b2847 [Feature] Checks for Nullable strings (#6050)
* Allows null strings in createString method c#

* Adds nullable strings to JS and swift

* Changes js checks

* Fixes typo
2020-07-27 09:57:50 -07:00
Liu Liu
5d052f4e55 [Swift] RFC: Switch Swift namespace from public enum to ordinary concat with _ (#6045)
This PR attempts to switch namespace from public enum back to ordinary
concat with _ in Swift. This kept style similar with protobuf, but
different from other popular style guide in Swift.

This is needed because previously, when we do `public enum`, we don't
really know when to declare and when to extend (extension). With namespace
implementation in this PR, there is no such ambiguity.
2020-07-27 11:06:11 +03:00
Casper
18b015d25a Rust codegen improvements and lint fixes (#6046)
* Improve rust codegen

* one more unneeded lifetime

* Added a derive default

Co-authored-by: Casper Neo <cneo@google.com>
2020-07-25 13:46:20 -07:00
mustiikhalil
d76e93f277 adds code gen for optional scalars in swift (#6038)
Adds swift test code

Replaces if statments

Adds swift to supported languages for optionals

Moved std::string to auto

Adds nullable scalars support in object api
2020-07-25 17:59:34 +03:00
jdess
82fac326c5 [C++] Fix compiler error from deleted assignment operator (#6036) (#6047)
* [C++] Fix compiler error from deleted assignment operator (#6036)

The assignment operator of the `buf_` member is deleted, we cannot call it from the assignment operator of the `TableKeyComparator` struct.

=> Also delete the assignment operator of the `TableKeyComparator` struct (already private anyhow).

* [C++] Fix compiler error from deleted assignment operator (#6036) - fix extraneous semicolon

The assignment operator of the `buf_` member is deleted, we cannot call it from the assignment operator of the `TableKeyComparator` struct.

=> Also delete the assignment operator of the `TableKeyComparator` struct (already private anyhow).
2020-07-24 10:35:27 -07:00
Casper
043b52bd4a Optional Scalars support for Rust (#6034)
* First draft of rust optionals

* Code cleanup around ftBool and ftVectorOfBool

* Tests for Rust optional scalars

* test bools too

Co-authored-by: Casper Neo <cneo@google.com>
2020-07-23 16:30:27 -07:00
Shengzhe
c8fa0afdfc Allow to run cpp tests under grpc/tests/ using bazel. (#6040)
This is the first step to upgrade grpc dependency to the latest version.

- Patch protobuf 3.6.1. and grpc 1.15.1 to fix build errors when using the latest Bazel version (3.4.1).
- Add grpc/tests:grpc_test. One can kick off tests in Bazel by calling `bazel test grpc/tests/...`.
- Add missing build targets in tests/BUILD in order to support grpc/tests/BUILD
2020-07-21 11:08:01 -07:00
Olivier Le Doeuff
6d0aae73cd Fix git command executed in wrong folder when doing cmake superbuild (#6039) 2020-07-20 12:26:01 -07:00
mustiikhalil
ff1b73128d [Swift] Optional Scalars Preparation (#6028)
* Perpares swift to take optional scalars + adds optional string helper method + disables linters in generated code

* Small fix for generated code

* Update grpc support to alpha 17 for swift
2020-07-20 20:38:50 +03:00
Vasyl Gello
2e48c8dd31 tests: Check for both quiet and signaling NaN on mipsel/hppa (#6029)
MIPSEL, MIPS64EL and HP-PA architectures have quiet and
signaling NaNs swapped. The test assumes correct values
for i386 / amd64 but obviously fails on these architectures.
It is documented in many places, like

https://software.llnl.gov/yorick-doc/stdlib/ieee.html :

Warning-- apparently there is no universal standard for what
  constitutes signalling versus quiet NaN
  on MIPS and HPPA architectures, qNaN and sNaN are reversed

https://gcc.gnu.org/onlinedocs/gcc/MIPS-Options.html :

  -mnan=2008
  -mnan=legacy

  These options control the encoding of the special
  not-a-number (NaN) IEEE 754 floating-point data.

  The -mnan=legacy option selects the legacy encoding.
  In this case quiet NaNs (qNaNs) are denoted by the first
  bit of their trailing significand field being 0, whereas
  signaling NaNs (sNaNs) are denoted by the first bit of
  their trailing significand field being 1.

  The -mnan=2008 option selects the IEEE 754-2008 encoding.
  In this case qNaNs are denoted by the first bit of their
  trailing significand field being 1, whereas sNaNs are
  denoted by the first bit of their trailing significand
  field being 0.

  The default is -mnan=legacy unless GCC has been configured
  with --with-nan=2008.

The behavior mentioned above causes FTBFS in Debian starting
with 1.12.0:

https://buildd.debian.org/status/fetch.php?pkg=flatbuffers&arch=mips64el&ver=1.12.1%7Egit20200711.33e2d80%2Bdfsg1-0.1&stamp=1594830681&raw=0
https://buildd.debian.org/status/fetch.php?pkg=flatbuffers&arch=mipsel&ver=1.12.1%7Egit20200711.33e2d80%2Bdfsg1-0.1&stamp=1594832647&raw=0
https://buildd.debian.org/status/fetch.php?pkg=flatbuffers&arch=hppa&ver=1.12.1%7Egit20200711.33e2d80%2Bdfsg1-0.1&stamp=1594830726&raw=0

For HP-PA (PA-RISC) there is no way to override the reversal,
and for MIPSEL/MIPS64EL there is no macro defining the '-mnan'
choice, so both variants were added with OR boolean operator.

Signed-off-by: Vasyl Gello <vasek.gello@gmail.com>
2020-07-20 10:18:02 -07:00
Dmitry
6942704f2b support deprecated flag in json schema (#6022) 2020-07-16 13:49:42 -07:00
Casper
9ecd2e16c2 Flatc parser support for nullable scalars (#6026)
* Parser support for nullable scalars

* Use older C++ features

* use default element

* Add a test for json, flexbuffers, and null

* test comments and names

Co-authored-by: Casper Neo <cneo@google.com>
2020-07-16 13:43:47 -07:00
Zhaoxuan Jiang
33e2d80791 [Dart] Generate constant values map for enums (#6025)
Dart schema compiler generated a static getter for enum values, which
always created a new map instance for its callers. See #5819.
Now it generates const map for better performance
and readability.
2020-07-10 17:42:22 -07:00
Kamil Rojewski
969d0f7a63 Using proper symbol name for reexport (#6021)
Co-authored-by: Kamil Rojewski <kamil.rojewski@gmail.com>
2020-07-06 08:40:56 -07:00
Vasyl Gello
515a4052a7 Silence false positive "-Wstringop-overflow" on GCC 10.0 to 11.0 (#6020) 2020-07-06 08:38:06 -07:00
Kamil Rojewski
36fbe6f13e Updated FB import (#6019)
Previous FB import was based on the original early TS implementation
and did not take into accout NPM. There doesn't seem to be a use
for current implemenentation anymore, while NPM compatibility is
needed.

Co-authored-by: Kamil Rojewski <kamil.rojewski@gmail.com>
2020-07-02 12:44:55 -07:00
Paulo Pinheiro
b69fc8cc95 [Java] Add support for shared strings on FlatBufferBuilder. (#6012)
Added a method FlatBufferBuilder::createSharedString that
enable string sharing for building messages on java.

The shared pool will only contains strings inserted by
this methods.
2020-07-02 12:40:56 -07:00
Kamil Rojewski
ab6af18d9f Not using non-existent create method for obj api (#6015)
Co-authored-by: Kamil Rojewski <kamil.rojewski@gmail.com>
2020-07-02 12:18:21 -07:00
mustiikhalil
37a5dee105 Code cleanup + updates test and readme (#6004)
Updates Readme + adds asserts

Fixes documentation
2020-06-30 21:37:43 +03:00
Casper
8a721f69a1 Serde with bytes maps to Blob (#6009)
* Serde with_bytes maps to Blob

* Bump rust flexbuffers minor version number

Co-authored-by: Casper Neo <cneo@google.com>
2020-06-28 19:58:08 -07:00
Liu Liu
e810635eaa [Swift] FlatBuffers createMonster method doesn't treat struct properly (#5992)
* [Swift] FlatBuffers createMonster method doesn't treat struct properly

This PR fixed a issue where a struct is not treated properly when use
create inside.

A example would be the pos inside Monster. The createMonster method
takes an Offset for pos. However, FlatBuffersBuilder.add(struct:)
doesn't really take Offset argument. That means we don't really add a
struct at all for Monster.

It will show up as the pos never set.

This doesn't show up in FlatBuffersMonsterWriterTests.swift because it
implements its own createMonster method, which happens do the dance
properly (i.e. first call create(struct) and then immediately call
add:).

This PR modified the `add(pos:)` interface such that it takes the
UnsafeMutableRawPointer directly, calling `create(struct:)` under the hood.

I can add unit tests once the direction of this PR approved.

* Fix object api pack method codegen.

* Add unit tests that uses Monster.createMonster method to serialize.

* Updated sample_binary.swift
2020-06-28 12:36:55 +03:00
Shengzhe
4995e15273 Manage grpc dependency in Bazel's WORKSPACE file. (#5995)
flatbuffers project currently depends on an old version of GRPC library and has known issues working with the latest version (issue #5836). Therefore, a patch file is created and put under bazel/ directory before supporting the latest GRPC version.
2020-06-26 15:39:23 -07:00
Derek Bailey
60b6066feb Add warning to schema parser if field name is not snake_case. (#6005)
* Added code gen for evolution tests back in.

* General generate code and clang format

* Added code gen for evolution tests back in.

* General generate code and clang format

* reran generate_code.sh

* Added code gen for evolution tests back in.

* General generate code and clang format

* Added code gen for evolution tests back in.

* General generate code and clang format

* Enforce snake_case for schema field names

* Switched to basic for loop, Fixed warning message
2020-06-26 11:16:02 -07:00
Johan Andersson
35d45cac7a [Rust] Flexbuffers dependency cleanup and fixes (#5998)
* Fix doc comment warnings

Can't use doc comment "///" syntax on macros, that generates the following warning:

warning: unused doc comment
   --> src\flexbuffer_type.rs:236:5
    |
236 |     /// returns true if and only if the flexbuffer type is `VectorFloat4`.
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ rustdoc does not generate documentation for macros
    |
    = help: to document an item produced by a macro, the macro must produce the documentation as part of its expansion

So switched to just use ordinary "//" comments on these to be warning free

* Upgrade num_enum 0.4.1 -> 0.5.0

* Remove unused and non-working usage of test crates

* Remove usage of abandoned debug_stub_derive crate

Which brought in old pre-v1 syn and quote crates.

This replaces it with just manual Debug trait implementation instead for the 2 cases
2020-06-25 17:12:10 -07:00
Derek Bailey
165a6e3d1e Re-added Evolution Schema Code Generation Command (#5999)
* Fixed refractoring issue in reflection/generate_code.sh. Also, mv deletes the original file, so I don't need to clean it up manually in that case.

* Fixed Dart Tests by removing code-gen for included files.

* Added code gen for evolution tests back in.

* General generate code and clang format

* Added evolution schema generation to .bat file

* Added code gen for evolution tests back in.

* General generate code and clang format

* Added evolution schema generation to .bat file

* reran generate_code.sh

* Removed wildcard from generate_code.bat that doesn't work
2020-06-25 15:35:23 -07:00
martind1111
13d3fb2ea6 Fix RPM install conflict (#6003) 2020-06-25 15:33:58 -07:00
Liu Liu
d64078eb27 [Swift] Initialize memory when clear ByteBuffer (#5982)
* Initialize memory when clear ByteBuffer

It seems (based on my limited understanding) that FlatBuffers requires
the writable area to be 0 initialized. However, we missed it when clear
the buffer to reinitialize it.

This PR fixed that by calling initialize (also fixed the typo)
explicitly.

* Update version to 0.5.3
2020-06-25 15:11:38 -07:00
hanstzou
ca1190a3d8 [TS] Use proper TypedArray in create*Vector (#5991)
* [TS] Use proper TypedArray in create*Vector

This commit adds TypeScript function overloads to create*Vector for
proper TypedArray types, effectively resolves #5373.

* Add @deprecated to old Uint8Array overloads
2020-06-25 14:42:47 -07:00
mjvankampen
7571b2ac56 [C++] Updates real_path to be truly portable (#5787)
* Removes posix definition

stat and fseeko are not used. Tested on QNX (GCC 5.4.0) and MinGw

* Updates realpath to more modern (2008) version

* Removes unix specific headers

* Adds detection of strtoll_l as android API < 21 does not provide it

* Includes cstdlib and formatting

Removes cmakelist alterations as not necessary. Formatting not complete.

* Stdlib outside if

Stdlib is available on all platforms

* Fixes indentation

* Adds locale check to android build

* Adds missing brace

* full names

* Removes again, no clue

* Updates base to check for locale independent android

Cmake already checks for others, also MSC?

* Changes to test on available and not requested

* Fixes android bad xopen_version define

* Removes warning
2020-06-25 14:40:10 -07:00
Jean-François Roy
e5a8f76a4b [C++] Generate default member initializers for >= C++11 (#5989)
In >= C++11 mode, generate default member initializers instead of a
default constructor.

The new code is semantically equivalent, but will allow aggregate
initialization in C++20.

This is a different take on #5951.
2020-06-22 18:06:15 -07:00
Paulo Pinheiro
413bb9b553 [Kotlin] Fix Access to union of vector element (#5994)
Kotlin code generation was producing wrong logic for accessors
of vector of union elements. This was shadowed by the fact[1] that asserts
in Kotlin are silently ignored unless the flag "-ea" is passed to the JVM.

The tests are also updated to enable asserts.

1 - https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/assert.html
2020-06-22 17:08:35 -07:00
Liu Liu
f35184aef9 [Swift] Add parsing from unowned UnsafeMutableRawPointer for ByteBuffer (#5981)
* RFC: Add ExternalStorage for ByteBuffer in Swift implementation

This PR proposed one more API for ByteBuffer such that no copy is
required to parse FlatBuffers content. This API has limited use, but for
cases that you need to read part of the flatbuffers' data to decide
whether you want to parse / copy the full buffer out, it is useful.

* Use a variable rather than protocol.

* Revert grouping changes from the other PR.

* Add unit test to read from unowned UnsafePointer.

* Manifest changed.
2020-06-22 17:05:36 -07:00
Alex Ames
b124b76258 Removed requirement that enums be declared in ascending order. (#5887) 2020-06-22 17:02:15 -07:00
Javier Serrano
0ec7600c67 Do not remove the last digit from float values (#5974)
Trailing zeros are already removed inside the function FloatToString,
that is called immediately before the lines deleted by this commit.
2020-06-22 16:57:45 -07:00
Gleb Mazovetskiy
14baf45c90 Mark GetBufferMinAlignment() const (#5985)
Like GetBufferPointer() and GetSize(), marks GetBufferMinAlignment function as const
2020-06-18 11:51:43 -07:00
Dimitri Mitropoulos
9abb2ec2cc TypeScript/JavaScript docs improvements (#5984)
* use correct language formatter for TypeScript examples

* fixes typo in JS/TS copied from PHP (apparently)

the variables are not named with a prefixed `$`

* fixes bizarre line breaks in markdown examples

* fixes snake case typo to fit JS/TS conventions

* makes example of Uint8Array usage explicit

* removes random extra lines between language blocks

* adds simple example for writing to file in node

* typo: flabuffers => flatbuffers

* adds (previously missing) code blocks to TypeScript code block

* adds context about where `monster_generated` comes from

to the uninitiated, a bit of help like this is welcome
2020-06-18 10:23:32 -07:00
mustiikhalil
2e57d80b13 [Swift] Internal library improvements (#5965)
* Moves addition to overflow addition in swift by using &+

Moves code to use Int instead of UInt32 & fixes functions

Updates swift performance to great

Updated version to 0.5.2

Updated swift package version to 5.2

Updated docker to swift 5.2

Removed all none & arithmetic operations

* Small refactoring
2020-06-18 10:14:38 -07:00
laurens-dg
cfc7753a4c [Doc] Added missing letters to compiler options (#5976)
These are correct in the help section of compiler, but missing some letters here.
2020-06-18 10:02:00 -07:00
Casper
12ddc8a920 Rust Flexbuffers Documentation update (#5979)
* Update samples and docs

* Fixed a line of documentation

Co-authored-by: Casper Neo <cneo@google.com>
2020-06-18 00:01:48 -07:00
Dimitri Mitropoulos
24ad35709d [docs] typo: updates monsterdata.json to be valid json (#5978)
* updates monsterdata.json to be valid json

the same monster.json file was not valid json

* updates reference to monsterdata.json in docs to also be valid json
2020-06-17 22:35:24 -07:00
Liu Liu
cc44a44427 [idl_parser] Mark typefield as deprecated (#5958)
* Mark typefield as deprecated

* Add UnionDeprecation Test.

* Update to use evolution schema instead.

* Use --scoped-enums to compile.
2020-06-15 23:18:12 -07:00
Derek Bailey
9ab4a5c0e2 Deleted old stale bot
The new stale action replaces the function of this bot, so removing the old one. 

It also prevents the two bots from battling each other :)
2020-06-15 13:39:46 -07:00
Derek Bailey
6682cfe870 Increased Operations per run in stale.yml
The default operations per run is 30 which would be consumed on the reprocessing already-marked issues. Greatly increased the operations per run to process all issues and pull request (~200 + 83 * 5).
2020-06-15 10:05:53 -07:00
Derek Bailey
64922904bc Adding Stale Action to clean up PR and Issues
Uses the Stale action (https://github.com/actions/stale) to help clean up older Issues and PR. Set to 6 months to mark as stale, and 14 days to close stale.
2020-06-12 13:15:11 -07:00
volkan-ozdemir
8e505cb677 [C++] Fixed/Enabled --cpp-ptr-type std::shared_ptr [#5813] (#5959) 2020-06-11 16:38:40 -07:00
Mika Raento
a28357d7ac Propagate boolean default values from proto to fbs (#5964) 2020-06-11 10:15:19 -07:00
mustiikhalil
7cb4762a61 [Swift] Improving reallocation time by using memcpy and moving reallocation code to storage (#5960)
Removes stride

Use capacity - current size to initialize

Fixes memory leak

Updated test code for linux
2020-06-11 08:53:32 -07:00
Vladimir Glavnyy
4e45f7c9e8 Fix error in SimpleQSort (#5955) 2020-06-09 23:35:07 -07:00
Malar Kannan
7ac0264053 fix error on GRPC Python - ignore namespace tree if not specified (#5862) (#5922) 2020-06-08 10:18:46 -07:00
mustiikhalil
108e981dbe Required is now implemented in swift (#5952) 2020-06-08 10:08:16 -07:00
Wouter van Oortmerssen
94a78e3853 Fixed: Access violation and ASAN/UNSAN failures with sorted tables
https://github.com/google/flatbuffers/issues/5945
2020-06-08 09:50:40 -07:00
Matt Brubeck
53fb453e04 [rust] Add FlatBufferBuilder::force_defaults API (#5946)
* [rust] Add force_defaults method FlatBufferBuilder

This works just like the same method already available in other
languages.

* Add binary format test for force_defaults
2020-06-08 09:47:36 -07:00
Paulo Pinheiro
17c1f35fa0 [FlexBuffer][Java] ReadWriteBuf and ReadBuf interface public (#5948)
Those interfaces need to be public for use cases where the user pass a buffer to FlexBuffers class that is not a ByteBuffer.

This will fix #5944
2020-06-08 09:35:51 -07:00
greenrobot Team
2eaf57778c [Java] Grow ArrayReadWriteBuf enough to match requested capacity. (#5921)
* [Java] Grow ArrayReadWriteBuf enough to match requested capacity.

Also handle requested capacity overflowing.

* [Java] Improve readability of ArrayReadWriteBuf.requestCapacity()

* [Java] prepare testBuilderGrowth() to fail again once FlexBuffersBuilder uses ByteBufferReadWriteBuf internally, add TestFail() as a better alternative to a plain "assert false"

* [Java] Revert some test changes and extract a small string that was used more than once; one could say three times, however, it might be worthy of a discussion if the third occasion also falls into this category, as it is an independent use case and thus would work in the same way even if the value changed.

Co-authored-by: Markus <markus@greenrobot>
2020-06-04 19:48:25 -07:00
mustiikhalil
666800da36 Adds bool support in structs + updates grpc support + CI upgrades (#5943) 2020-06-04 09:14:18 -07:00
Kamil Rojewski
38ed69eb3d fixed mutating inline values (#5942)
Co-authored-by: Kamil Rojewski <kamil.rojewski@gmail.com>
2020-06-04 09:08:41 -07:00
Laurentiu Cristofor
d026e6f071 Add static asserts to ensure that reflection API arrays are kept in sync (#5934)
* Add static asserts to ensure that reflection API arrays are kept in sync

* Move changes from generated file into source fbs file

* Rename enum value and regenerate reflection_generated.h

* Add comments to each entries of type sizes array
2020-06-02 09:50:56 -07:00
bakinovsky-m
988164f6e1 [C++] Got rid of memset's in constructors (#5938)
* [C++] removed array's memsets from struct parametrized constructor

now POD-typed arrays are zero-initialized
and class-typed arrays are default-initialized

* [C++] memset -> zero/default initialization in default constructor

* [C++] Struct-type and array default initialization

* [C++] Newly generated tests

* [C++] forgotten test

* [C++] curly brace by code style

* [C++] test if memory is 0's after placement new

* [C++] memory leak fix

* [C++] simplifying and non-dynamic memory in test

* [C++] code cleanup

* [C++] disable old-compiler warning

* [C++] windows build fix (try)

* [C++] debug-new win build fix
2020-06-01 17:58:52 -07:00
Derek Bailey
7179a5a8ba General Codebase clean up (#5939)
* Fixed refractoring issue in reflection/generate_code.sh. Also, mv deletes the original file, so I don't need to clean it up manually in that case.

* Ensuring test/generate_code.sh was ran

* Fixed Dart Tests by removing code-gen for included files.

* General cleanup of codebase.
2020-06-01 08:40:55 -07:00
stefan301
a0da0c08c6 Add GetStringView like GetString, GetCstring (#5937)
* Added missing EndTable() call to VerifyObject()

VerifyObject called VerifyTableStart() but not EndTable(). This made Verifier::VerifyComplexity() increase depth_ with each table, not with the depth of tables.

https://groups.google.com/forum/#!topic/flatbuffers/OpxtW5UFAdg

* Added Check to VerifyAlignment

https://stackoverflow.com/questions/59376308/flatbuffers-verifier-returns-false-without-any-assertion-flatbuffers-debug-veri

* Add GetStringView (Convenience function to get string_view from a String returning an empty string_view on null pointer) like GetString, GetCstring

Co-authored-by: stefan301 <Stefan.Felkel@de.Zuken.com>
2020-05-29 10:00:46 -07:00
mugisoba
ac203b2092 [C#] Add file identifier to ObjectAPI Serialization Utility. (#5920)
* use Finish***Buffer instead.

* add file_identifier test.
2020-05-21 09:14:20 -07:00
Kamil Rojewski
8dd1bf25b5 not creating creation methods when using structs (#5919)
* not creating creation methods when using structs

* vs2015 build fix

Co-authored-by: Kamil Rojewski <kamil.rojewski@gmail.com>
2020-05-18 09:39:19 -07:00
Maxim Zaks
5aa443d98c [Dart] Adding FlexBuffers support (#5853)
* Adding FlexBuffers support for Dart language
2020-05-18 07:50:39 -07:00
Derek Bailey
0fa087657e [Dart] Getting tests/DartTest.sh to work on master. (#5915)
* Fixed refractoring issue in reflection/generate_code.sh. Also, mv deletes the original file, so I don't need to clean it up manually in that case.

* Fixed Dart Tests by removing code-gen for included files.
2020-05-15 18:13:29 -07:00
Vladimir Glavnyy
424a473e1f Schema parser: prohibit declaration of an array of pointers inside structs (#5907)
* Fix issue #5906, Prohibit declaration of an array of pointers inside structs

- idl_parser.cpp: Prohibit declaration of an array of pointers inside structs
- idl_gen_cpp.cpp: Extract GenStructConstructor() method from GenStruct() to simplify future modification
- idl_gen_cpp.cpp: Add assert for checking of Array fields in structs on code-generation stage

* Fix the error 'unused local variable' in release build

* Fix: format the PR code according to coding rules

* Add test-case and fix review notes
2020-05-14 11:39:58 -07:00
Casper
c3faa83463 Fix Cargo.toml dependencies (#5911)
* Fix Cargo.toml dependencies

* less specific serde dependency

Co-authored-by: Casper Neo <cneo@google.com>
2020-05-13 11:24:32 -07:00
Dan Field
91399ad055 fix union type names (#5902)
* fix union type names

* fix up generation and test

* format

* update codegen
2020-05-13 10:26:32 -07:00
Casper
32782e4ad1 Update Rust Flexbuffers metadata before publishing (#5905)
Co-authored-by: Casper Neo <cneo@google.com>
2020-05-11 19:46:37 -07:00
Khoi Dinh Trinh
e7f3b16905 [TS] Make Obj-API work with --short-names (#5898)
* changed function used for getting struct name

* re-generated code
2020-05-11 14:55:57 -07:00
Kamil Rojewski
12ed1fe4a9 fixed invalid imports with --gen-all (#5895)
* fixed invalid imports with --gen-all

* only excluding crate imports

Co-authored-by: Kamil Rojewski <kamil.rojewski@gmail.com>
2020-05-07 16:21:30 -07:00
Derek Bailey
85ee4df7a2 [C#] Thread safe reads of Double and Float values from a ByteBuffer (#5900)
* Fixed refractoring issue in reflection/generate_code.sh. Also, mv deletes the original file, so I don't need to clean it up manually in that case.

* Thread safe reads of Double and Floats from ByteBuffer
2020-05-07 14:33:29 -07:00
Dmitriy Kovalev
de89bd1933 Implement flexbuffers in python (#5880) 2020-05-07 14:26:33 -07:00
Casper
8be05f6bd4 Rust Flexbuffers (#5669)
* Cargo clippy lints

* more lints

* more lints

* Restored a doc comment

* Comment on float eps-eq and adjusted casting

* Rust Flexbuffers

* more serde tests, removed some unsafe

* Redid serde to be map-like and Reader is Display

* Moved iter from Reader to VectorReader

* Serious quickcheck + bugs

* wvo api

* Made types smaller for a reasonable speedup

* redid reading in a way that's a bit faster.

Profiling shows the rust slowdown as building +10%, reading +20%

* src/bin are developer binaries in rust

* Root and Map width are not packed

* key null check is debug only + doc changes

* BuilderOptions

* Documentation

* Documentation

* Moved tests to rust_usage_test

* Moved rust flexbuffers samples to Flatbuffers/samples

* Fixed RustTest

* Fixed for Rust 1.37.0

* Upgraded to rust 1_40_0

* fixed a little-endian-only feature in a test

* 1.40.0

* fixed some benchmarks for bigendian

* Updated .bat file

* misspelling

* Gold Flexbuffer test.

* Serialize,Deserialize, std::error::Error for Errors.

* Undo rustfmt in integration_test.rs

* from_slice instead of from_vec

* Added comments to unsafe blocks

* expanded on comment

* bump

Co-authored-by: CasperN <cneo@google.com>
2020-05-07 14:11:26 -07:00
mustiikhalil
870ecbc09a [swift] Moves code to use VTablesStorage (#5888)
* Moves the code to use _vtablestorage

Rebuilt the test to confirm to the new API

Adds documentation + generates code for grpc

Reverts indentation

v0.4.0

Updated swift/readme.md

Updates VtableStorage to ensure space instead of reallocating each time

Fixes str count not being correct

* Fixes issue with boolean constant not being set + removes unused function
2020-05-07 12:28:42 -07:00
Markus Junginger
c2da8d5d85 [Java][FlexBuffers] Make FlexBuffersBuilder reusable by adding clear() (#5889) (#5890) 2020-05-07 12:26:55 -07:00
Adam Oleksy
e84cbff673 Align package name to FindFlatBuffers.cmake (#5899)
This change is for fix warning:
```
CMake Warning (dev) at /home/aoleksy/share/cmake-3.17/Modules/FindPackageHandleStandardArgs.cmake:272 (message):
  The package name passed to `find_package_handle_standard_args`
  (flatbuffers) does not match the name of the calling package (FlatBuffers).
  This can lead to problems in calling code that expects `find_package`
  result variables (e.g., `_FOUND`) to follow a certain pattern.
Call Stack (most recent call first):
  FindFlatBuffers.cmake:33 (find_package_handle_standard_args)
  CMakeLists.txt:6 (find_package)
This warning is for project developers.  Use -Wno-dev to suppress it.
```
2020-05-07 12:22:12 -07:00
Carol (Nichols || Goulding)
f94e6c84e0 Small tutorial improvements - documentation only (#5894)
* Use code formatting for a code snippet

* Minor grammar, spelling, and Markdown fixes
2020-05-07 12:15:11 -07:00
Wouter van Oortmerssen
f12cca8bcb Attempt at adding Github Actions CI 2020-05-05 13:56:37 -07:00
Dmitry Volosnykh
7e4124d6e6 Handle git program or .git folder absence (#5878)
Assume version is 0.0.0.0 in such cases.
2020-05-04 13:23:41 -07:00
Thomas
a875d247ad only add native_include_files if object based api is enabled (#5886)
The native include files tag is intended to be used together with the object based api. A client which does not use the object based api should have no need to know about the additional include files needed for the object based api. The generated flatbuffers code without object api enabled should compile without the additional header files.
2020-05-04 12:27:50 -07:00
Mika Raento
6e9f5d9810 Respect shared attribute in Parser (#5885)
Respects the 'shared' field attribute when reading data with Parser.
2020-05-04 10:33:11 -07:00
Even Rouault
ff1c78233d include/flatbuffers: typo fixes in comments (#5884) 2020-05-04 10:21:57 -07:00
mustiikhalil
2e9a196734 Updates swift docs for package management (#5883)
Small fix for the clear function
2020-05-04 10:18:38 -07:00
jrop
e3cb07d321 [Rust] idl_gen_rust.cpp: (Option/required-aware codegen for unions) (#5850)
* idl_gen_rust.cpp: Fix google/flatbuffers#5849 (Option/required-aware
codegen for unions)

The generated code was assuming that an Option is always returned by the
union-getter method: however, this is only true if the field is not
marked as `(required)`.

* idl_gen_rust.cpp: flip conditional

* idl_gen_rust.cpp: Add comment, as requested in review

The code added is not covered by the integration test
2020-05-04 00:16:57 -07:00
Mika Raento
712866d57b Propagate use_string_pooling in CopyTable (#5879)
This allows using CopyTable for deduplicating strings.
2020-04-30 08:58:43 -07:00
Kamil Rojewski
44c919a9e8 Not using reexports with --gen-all (#5873)
Co-authored-by: Kamil Rojewski <kamil.rojewski@gmail.com>
2020-04-27 12:35:42 -07:00
Alex Ames
99aa1ef21d Added INCLUDE_PREFIX option for flatbuffers_generate_headers (#5866)
This option was in an earlier revision of flatbuffers_generate_headers
but was lost while refactoring by mistake. We have it as a function
argument rather than one of the optional flags so that we can use it to
inform the rule where to place the generated headers, so that the file
location matches the prefix that will be added to the filenames in the
generated files.

Also fixed some of the documentation in the file.
2020-04-23 10:09:19 -07:00
Wouter van Oortmerssen
40ba170c94 Fixed text in internals doc that implied structs can be root 2020-04-23 09:28:22 -07:00
mustiikhalil
cb4d0f72e3 [Swift] Object API support (#5826)
* Adds Object-api support to swift

* Fixed indentation issues

* Removed indentation within namespaces
2020-04-13 09:28:56 -07:00
Khoi Dinh Trinh
003e164057 [TS] Add Obj API (#5788)
* added basic code

* backup work

* got class property to work

* backup progress

* implementented fmt for creating code

* added docs for genFieldUtils

* back up work

* added base helper js func

* added union js code

* added unpackTo and base for pack

* added pack code

* added null check for packing struct list

* passes compile test

* fixed some spacing of generated functions

* added annotations for constructors

* added obj api unpack test

* tested pack to work

* merge branch

* separated js and ts test

* fixed union signature to include string

* fixed generator to support string union

* hardcoded fb builder name

* refactored struct vector creation

* work around createLong

* handle default value in constructor

* update typescript docs

* added notes about import flag

* fixed formatting stuffs

* undo TypescriptTest change

* refactored fmt

* updated generated code

* remove ignoring union_vector for js

* revert changes for .project

* revert changes for package.json

* don't generate js in ts test

* fixed android project file

* removed unused js function

* removed package-lock.json

* adjust createObjList to new signature

* changed regex to callback style

* fixed package.json

* used existing func for generating annotation

* changed ternary to !!

* added return type for lambda

* removed callback style for obj api generator

* fixed js file indentation

* removed unused header

* added tests for string only union

* handle string only union and refactor union conv func

* updated generated ts files

* renamed union conv func

* made js test create files like other languages

* removed union string only handling

* don't allow null in createObjectOffsetList

* updated generated ts code

* changed the line that triggers Windows build errors

* hopefully fix CI error
2020-04-09 09:53:16 -07:00
Meng Yang
21cf300f4c fix cpp usage markdown error (#5845) 2020-04-09 09:38:58 -07:00
mustiikhalil
9655e12d6d Upgraded swift implementation for grpc (#5843)
Updated version number
2020-04-06 10:05:56 -07:00
Anass Al
fb96fadc20 [C#] Fix nested structs and arrays in Object API (#5765)
* [C#] Fix nested structs and arrays in Object API

The adds support for nested structs and fixed size arrays in the C#
Object API codegen which previously generated invalid code that wouldn't
compile.

- Nested structs would originally generate syntax errors due to adding an
  additional `.` to separate fields.
- Fixed size arrays of nested structs would originally generate code for
  the first field in the top most struct, and would lead to a compiler
  error due to referencing undefined variables.

* [C#] fix nested structs and arrays of structs.

* fix nested structs + arrays

* add table support

* Cleanup code

Co-authored-by: mugisoba <mugisoba+github@icloud.com>
2020-04-06 09:27:13 -07:00
Max Burke
408f11fbdd [ts] Fix empty source/dest namespaces when reexporting. (#5841)
When either source or destination or both namespaces were empty, flatc
was generating incorrect TS code.

For example:

```
export namespace  {
export import ObjectId = NS10770172024577249292..ObjectId;}
```

In this case the target namespace is empty, and so is the namespace
between the NSxxx placeholder and the target type.
2020-04-06 09:09:15 -07:00
mustiikhalil
a83caf5910 Improves performance for the swift library by using structs + a storage class (#5835) 2020-04-02 12:55:30 -07:00
Paulo Pinheiro
925fab6b15 [Java][FlexBuffers] Optimize Map access (#5735)
The original implementation of map access is very naive:
- Encode String to UTF8 byte[]
- Creates a new KeyVector
- Performs a binary search to find the key
- return value

So every access to the Map there was useless allocations of Keys and KeyVector
and complete encoding of the search key, which for most comparisons would be wasteful.

This changes completely removes the use of KeyVector and compute the key
positions on the spot. Besides that, it compares keys codepoint-by-codepoint,
avoiding unnecessary allocations and reducing encoding for most cases.

Some benchmarks result in a 2.75x speedup.
2020-03-30 13:46:42 -07:00
Olivier Le Doeuff
d9fecc3327 [CMake] : Add precompiled header support with FLATBUFFERS_ENABLE_PCH (#5827)
* [CMake] : Add precompiled header support with FLATBUFFERS_ENABLE_PCH

FLATBUFFERS_ENABLE_PCH enable precompile headers support for 'flatbuffers' and 'flatc'. Default if OFF.
You need to set it to ON to make it work. 'cmake -DFLATBUFFERS_ENABLE_PCH=ON ..'
This will only work if CMake version is >= 3.16, because target_precompile_headers is used.
If CMake doesn't have target_precompile_headers, then nothing will happen.
This can speed up compilation time.
Precompiled header file for 'flatbuffers' is 'pch.h'
Precompiled header file for 'flatc' is 'flatc_pch.h'

* Enable FLATBUFFERS_ENABLE_PCH for VC 2017 and VS2019 builds

* [CMake]: Fix error in FLATBUFFERS_ENABLE_PCH description

* Add a function add_pch_to_target to avoid copy and pasting the same code like add_fsanitize_to_target
Move pch.h and flatc_pch.h from include/flatbuffers to include/flatbuffers/pch
Make flatc_pch.h depends on pch.h since it extend it
2020-03-27 08:35:44 -07:00
Alex Ames
e9d4532401 Added flatbuffers_generate_headers and flatbuffers_generate_binary_files cmake functions. (#5830)
flatbuffers_generate_headers is a function that can be used to generate Flatbuffers headers and binary headers. It uses named argument flags to wrap all the flags relevant to C++ header generation that flatc uses. It generates an interface library that can be added as a dependency of other targets.

flatbuffers_generate_binary_files is a function that can be used to generate Flatbuffers binaries. It uses named argument flags to wrap all the flags relevant to C++ binary generation from JSON file that flatc uses. It generates an interface library that can be added as a dependency of other targets.
2020-03-27 08:33:12 -07:00
Dmitriy Kovalev
c37c989ed6 Correct calculation of vector element size (#5831)
Number of elements on the stack shouldn't affect the calculation
of ElemWidth(). Variable 'start' needs to be subtracted from the
loop variable 'i' to make indexing zero-based.

There is an additional unit test to pack nested vectors. Size of
the packed buffer *without* this fix is 798 and only 664 bytes
*with* the fix.
2020-03-26 08:54:52 -07:00
Arthur O'Dwyer
6b271b7ecb Fix Clang-trunk warnings about special members deprecated in C++20. (#5829)
For example:

    include/flatbuffers/reflection.h:365:8: error: definition of implicit copy
          constructor for 'pointer_inside_vector<flatbuffers::Table, unsigned char>'
          is deprecated because it has a user-declared copy assignment operator
          [-Werror,-Wdeprecated-copy]
      void operator=(const pointer_inside_vector &piv);
           ^

It's unclear why the old code wanted to declare a public `operator=`
without defining it; that just seems like a misunderstanding of the C++03 idiom
for deleting a member function. And anyway, we don't *want* to delete the
assignment operator; these are polymorphic types that do not follow value
semantics and nobody should ever be trying to copy them. So the simplest fix
is just to go back to the Rule of Zero: remove the declaration of `operator=`
and let the compiler do what it wanted to do originally anyway.
"The best code is no code."

Also, update the generated .h files.

Fixes #5649.
2020-03-23 10:01:11 -07:00
Vladimir Glavnyy
90f3b8e8c7 Fix float_constant definition in './doc/Grammar.md` (#5828) 2020-03-23 09:51:57 -07:00
stefan301
3af7359345 [csharp] flatc should generate a 'Create…' method for tables with struct field… (#5818)
* flatc should generate a 'Create…' method for tables with struct fields when also generating the object based api (C#)
https://stackoverflow.com/questions/60724317/flatc-should-generate-a-create-method-for-tables-with-struct-fields-when-al

* missing namespace fixed: C:\projects\flatbuffers\tests\namespace_test\NamespaceA\TableInFirstNS.cs(30,7): error CS0246: The type or namespace name 'StructInNestedNST' could not be found (are you missing a using directive or an assembly reference?) [C:\projects\flatbuffers\tests\FlatBuffers.Test\FlatBuffers.Test.csproj]

Co-authored-by: stefan301 <Stefan.Felkel@de.Zuken.com>
2020-03-23 09:39:26 -07:00
Yurii Nakonechnyi
c4231c3cb9 Updated doxyfile - added missing files (#5824)
Added missing Java/C#/Switft files - that fixes references to them. Removed non-existent "JavaCsharpUsage.md".
2020-03-20 10:13:47 -07:00
Ekene Madu
9657df184e Update Grammar.md (#5820)
* Update Grammar.md

Some consistency fixes to the grammar file

* Some more minor fixes to Grammar.md

* Update Grammar.md
2020-03-20 10:12:23 -07:00
a-maurice
97ffc590e3 Include CPack only when explictly packaging (#5817)
Including CPack without the additional packaging information creates unused project files, and can potentially cause issues for projects including Flatbuffers that also use CPack.
2020-03-17 13:38:29 -07:00
stefan301
8b52af65bc [C++] Add max_depth and max_tables parameters to reflection::Verify (#5815)
* Added missing EndTable() call to VerifyObject()

VerifyObject called VerifyTableStart() but not EndTable(). This made Verifier::VerifyComplexity() increase depth_ with each table, not with the depth of tables.

https://groups.google.com/forum/#!topic/flatbuffers/OpxtW5UFAdg

* Added Check to VerifyAlignment

https://stackoverflow.com/questions/59376308/flatbuffers-verifier-returns-false-without-any-assertion-flatbuffers-debug-veri

* [C++] Add max_depth and max_tables parameters to reflection::Verify

Co-authored-by: Felkel <stefan@eas.local>
Co-authored-by: stefan301 <Stefan.Felkel@de.Zuken.com>
2020-03-16 12:19:11 -07:00
Vladimir Glavnyy
9b034eee12 Fix interpretation of 'nan(number)' by the idl_parser (#5810)
* Parser reject "nan(n)" string as it does with nan(n)

* Adjust scalar fuzzer to ignore '$schema' substrings

- Scalar fuzzer ignores '$schema' substrings at the input
- Added 'scalar_debug' target to simplify research of fuzzed cases

* Improve formatting of './tests/fuzzer/CMakeLists.txt'
2020-03-16 11:59:34 -07:00
mustiikhalil
3e9ac3cff9 [Scripts] Adds swift to generated code (#5806)
* Moved swifttest.sh code generator to generate_code + fix issue in ghpages

* Fixes small issue with cocoapods documentation
2020-03-16 11:55:59 -07:00
962 changed files with 99262 additions and 33089 deletions

View File

@@ -1,41 +0,0 @@
:: Copyright 2018 Google Inc. All rights reserved.
::
:: 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.
set buildtype=Release
if "%1"=="-b" set buildtype=%2
cd tests
call generate_code.bat -b %buildtype% || goto FAIL
:: TODO: Release and Debug builds produce differences here for some reason.
git checkout HEAD -- monster_test.bfbs
git checkout HEAD -- arrays_test.bfbs
git -c core.autocrlf=true diff --exit-code --quiet || goto :DIFFFOUND
goto SUCCESS
:DIFFFOUND
@echo "" >&2
@echo "ERROR: ********************************************************" >&2
@echo "ERROR: The following differences were found after running the" >&2
@echo "ERROR: tests/generate_code.sh script. Maybe you forgot to run" >&2
@echo "ERROR: it after making changes in a generator or schema?" >&2
@echo "ERROR: ********************************************************" >&2
@echo "" >&2
@git -c core.autocrlf=true --no-pager diff --binary
:FAIL
set EXITCODE=1
:SUCCESS
cd ..
EXIT /B %EXITCODE%

View File

@@ -1,18 +1,18 @@
---
buildifier: latest
platforms:
ubuntu1604:
build_targets:
- "..."
test_targets:
- "..."
ubuntu1804:
build_targets:
- "..."
- "//..."
test_targets:
- "..."
- "//..."
ubuntu2004:
build_targets:
- "//..."
test_targets:
- "//..."
macos:
build_targets:
- "..."
- "//..."
test_targets:
- "..."
- "//..."

13
.eslintrc.js Normal file
View File

@@ -0,0 +1,13 @@
/* eslint-env node */
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint',
],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
]
};

View File

@@ -2,18 +2,17 @@ Thank you for submitting a PR!
Please delete this standard text once you've created your own description.
Make sure you include the names of the affected language(s) in your PR title.
This helps us get the correct maintainers to look at your issue.
If you make changes to any of the code generators (`src/idl_gen*`) be sure to
build your project, as it will generate code based on the changes. If necessary
the code generation script can be directly run (`scripts/generate_code.py`),
requires Python3. This allows us to better see the effect of the PR.
If you make changes to any of the code generators, be sure to run
`cd tests && sh generate_code.sh` (or equivalent .bat) and include the generated
code changes in the PR. This allows us to better see the effect of the PR.
If your PR includes C++ code, please adhere to the Google C++ Style Guide,
If your PR includes C++ code, please adhere to the
[Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html),
and don't forget we try to support older compilers (e.g. VS2010, GCC 4.6.3),
so only some C++11 support is available.
For any C++ changes, please make sure to run `sh src/clang-format-git.sh`
For any C++ changes, please make sure to run `sh scripts/clang-format-git.sh`
Include other details as appropriate.

97
.github/labeler.yml vendored Normal file
View File

@@ -0,0 +1,97 @@
# Configuration for Auto Labeler during pull request
#
# See https://github.com/actions/labeler for file format
# and https://github.com/google/flatbuffers/labels for a list of valid labels
#
# See .github/workflows/label.yml for Github Action workflow script
c#:
- '**/*.cs'
- net/**/*
- tests/FlatBuffers.Test/**/*
- tests/FlatBuffers.Benchmarks/**/*
- src/idl_gen_csharp.cpp
swift:
- '**/*.swift'
- swift/**/*
- tests/FlatBuffers.GRPC.Swift/**/*
- tests/FlatBuffers.Benchmarks.swift/**/*
- tests/FlatBuffers.Test.Swift/**/*
- src/idl_gen_swift.cpp
javascript:
- '**/*.js'
- src/idl_gen_ts.cpp
typescript:
- '**/*.ts'
- src/idl_gen_ts.cpp
- grpc/flatbuffers-js-grpc/**/*.ts
golang:
- '**/*.go'
- src/idl_gen_go.cpp
python:
- '**/*.py'
- src/idl_gen_python.cpp
java:
- '**/*.java'
- src/idl_gen_java.cpp
kotlin:
- '**/*.kt'
- src/idl_gen_kotlin.cpp
lua:
- '**/*.lua'
- lua/**/*
- src/idl_gen_lua.cpp
- src/bfbs_gen_lua.cpp
lobster:
- '**/*.lobster'
- src/idl_gen_lobster.cpp
php:
- '**/*.php'
- src/idl_gen_php.cpp
rust:
- '**/*.rs'
- rust/**/*
- src/idl_gen_rust.cpp
dart:
- '**/*.dart'
- src/idl_gen_dart.cpp
c++:
- '**/*.cc'
- '**/*.cpp'
- '**/*.h'
json:
- '**/*.json'
- src/idl_gen_json_schema.cpp
codegen:
- src/**/*
documentation:
- docs/**/*
- '**/*.md'
CI:
- '.github/**/*'
- '.appveyor/**/*'
- '.travis/**/*'
- '.bazelci/**/*'
- .travis.yml
- appveyor.yml
grpc:
- grpc/**/*
- src/idl_gen_grpc.cpp

18
.github/stale.yml vendored
View File

@@ -1,18 +0,0 @@
# Number of days of inactivity before an issue becomes stale
daysUntilStale: 365
# Number of days of inactivity before a stale issue is closed
daysUntilClose: 14
# Issues with these labels will never be considered stale
exemptLabels:
- pinned
- security
# Label to use when marking an issue as stale
staleLabel: stale
# Comment to post when marking an issue as stale. Set to `false` to disable
markComment: >
This issue has been automatically marked as stale because it has not had
activity for 1 year. It will be automatically closed if no further activity occurs.
To keep it open, simply post a new comment. Maintainers will re-open on
new activity. Thank you for your contributions.
# Comment to post when closing a stale issue. Set to `false` to disable
closeComment: false

274
.github/workflows/build.yml vendored Normal file
View File

@@ -0,0 +1,274 @@
name: CI
on:
push:
branches:
- master
pull_request:
branches:
- master
jobs:
build-linux:
name: Build Linux
runs-on: ubuntu-latest
strategy:
matrix:
cxx: [g++-10, clang++-12]
steps:
- uses: actions/checkout@v1
- name: cmake
run: CXX=${{ matrix.cxx }} cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release .
- name: build
run: make -j4
- name: test
run: ./flattests
- name: make flatc executable
run: |
chmod +x flatc
./flatc --version
- name: upload build artifacts
uses: actions/upload-artifact@v1
with:
name: Linux flatc binary ${{ matrix.cxx }}
path: flatc
build-windows:
name: Build Windows 2019
runs-on: windows-latest
steps:
- uses: actions/checkout@v1
- name: Add msbuild to PATH
uses: microsoft/setup-msbuild@v1.1
- name: cmake
run: cmake -G "Visual Studio 16 2019" -A x64 -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_BUILD_CPP17=ON .
- name: build
run: msbuild.exe FlatBuffers.sln /p:Configuration=Release /p:Platform=x64
- name: test
run: Release\flattests.exe
- name: upload build artifacts
uses: actions/upload-artifact@v1
with:
name: Windows flatc binary
path: Release\flatc.exe
build-windows-2017:
name: Build Windows 2017
runs-on: windows-latest
steps:
- uses: actions/checkout@v1
- name: Add msbuild to PATH
uses: microsoft/setup-msbuild@v1.1
- name: cmake
run: cmake -G "Visual Studio 16 2019" -A x64 -DCMAKE_BUILD_TYPE=Release .
- name: build tool version 15 (VS 2017)
run: msbuild.exe FlatBuffers.sln /p:Configuration=Release /p:Platform=x64 /p:VisualStudioVersion=15.0
- name: test
run: Release\flattests.exe
build-windows-2015:
name: Build Windows 2015
runs-on: windows-latest
steps:
- uses: actions/checkout@v1
- name: Add msbuild to PATH
uses: microsoft/setup-msbuild@v1.1
- name: cmake
run: cmake -G "Visual Studio 14 2015" -A x64 -DCMAKE_BUILD_TYPE=Release .
- name: build tool version 14 (VS 2015)
run: msbuild.exe FlatBuffers.sln /p:Configuration=Release /p:Platform=x64 /p:VisualStudioVersion=14.0
- name: test
run: Release\flattests.exe
build-mac:
name: Build Mac
runs-on: macos-latest
steps:
- uses: actions/checkout@v1
- name: cmake
run: cmake -G "Xcode" -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_FLATC_EXECUTABLE=_build/Release/flatc .
- name: build
# NOTE: we need this _build dir to not have xcodebuild's default ./build dir clash with the BUILD file.
run: xcodebuild -toolchain clang -configuration Release -target flattests SYMROOT=$(PWD)/_build
- name: check that the binary is "universal"
run: |
info=$(file _build/Release/flatc)
echo $info
echo $info | grep "universal binary with 2 architectures"
- name: test
run: _build/Release/flattests
- name: make flatc executable
run: |
chmod +x _build/Release/flatc
./_build/Release/flatc --version
- name: upload build artifacts
uses: actions/upload-artifact@v1
with:
name: Mac flatc binary
path: _build/Release/flatc
build-android:
name: Build Android (on Linux)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: set up JDK 1.8
uses: actions/setup-java@v1
with:
java-version: 1.8
- name: set up flatc
run: |
cmake -DFLATBUFFERS_BUILD_TESTS=OFF -DFLATBUFFERS_BUILD_FLATLIB=OFF -DFLATBUFFERS_BUILD_FLATHASH=OFF .
make
echo "${PWD}" >> $GITHUB_PATH
- name: build
working-directory: android
run: bash ./gradlew clean build
build-generator:
name: Check Generated Code
runs-on: ubuntu-latest
strategy:
matrix:
cxx: [g++-10, clang++-12]
steps:
- uses: actions/checkout@v1
- name: cmake
run: CXX=${{ matrix.cxx }} cmake -G "Unix Makefiles" -DFLATBUFFERS_BUILD_TESTS=OFF -DCMAKE_BUILD_TYPE=Release . && make -j4
- name: Generate
run: scripts/check_generate_code.py
- name: Generate gRPC
run: bash scripts/check-grpc-generated-code.sh
build-benchmarks:
name: Build Benchmarks (on Linux)
runs-on: ubuntu-latest
strategy:
matrix:
cxx: [g++-10]
steps:
- uses: actions/checkout@v1
- name: cmake
run: CXX=${{ matrix.cxx }} cmake -G "Unix Makefiles" -DFLATBUFFERS_CXX_FLAGS="-Wno-unused-parameter -fno-aligned-new" -DFLATBUFFERS_BUILD_BENCHMARKS=ON -DCMAKE_BUILD_TYPE=Release . && make -j4
- name: Run benchmarks
run: ./flatbenchmark --benchmark_repetitions=5 --benchmark_display_aggregates_only=true --benchmark_out_format=console --benchmark_out=benchmarks/results_${{matrix.cxx}}
- name: Upload benchmarks results
uses: actions/upload-artifact@v1
with:
name: Linux flatbenchmark results ${{matrix.cxx}}
path: benchmarks/results_${{matrix.cxx}}
build-java:
name: Build Java
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: test
working-directory: tests
run: bash JavaTest.sh
build-kotlin-macos:
name: Build Kotlin MacOS
runs-on: macos-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- uses: actions/setup-java@v2
with:
distribution: 'adopt-hotspot'
java-version: '11'
- name: Build
working-directory: kotlin
run: ./gradlew clean iosX64Test macosX64Test jsTest jsBrowserTest
build-kotlin-linux:
name: Build Kotlin Linux
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- uses: actions/setup-java@v2
with:
distribution: 'adopt-hotspot'
java-version: '8'
- name: Build
working-directory: kotlin
run: ./gradlew jvmMainClasses jvmTest
- name: Run Benchmark
working-directory: kotlin
run: ./gradlew jvmBenchmark
- name: Generate Benchmark Report
working-directory: kotlin
run: |
./gradlew jmhReport;
mv benchmark/build/reports/benchmarks/main/* benchmark_latest
- name: Archive benchmark report
uses: actions/upload-artifact@v1
with:
name: Kotlin Benchmark Report
path: kotlin/benchmark_latest
build-rust:
name: Build Rust
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: test
working-directory: tests
run: bash RustTest.sh
build-python:
name: Build Python
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: test
working-directory: tests
run: bash PythonTest.sh
build-go:
name: Build Go
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: flatc
# FIXME: make test script not rely on flatc
run: cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_BUILD_TESTS=OFF -DFLATBUFFERS_INSTALL=OFF -DFLATBUFFERS_BUILD_FLATLIB=OFF -DFLATBUFFERS_BUILD_FLATHASH=OFF . && make -j4
- name: test
working-directory: tests
run: bash GoTest.sh
build-swift:
name: Build Swift
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: test
working-directory: tests/FlatBuffers.Test.Swift
run: sh SwiftTest.sh
build-ts:
name: Build TS
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: compile
run: npm run compile
- name: test
working-directory: tests
run: sh TypeScriptTest.sh
build-dart:
name: Build Dart
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: dart-lang/setup-dart@v1
with:
sdk: stable
- name: flatc
# FIXME: make test script not rely on flatc
run: cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DFLATBUFFERS_BUILD_TESTS=OFF -DFLATBUFFERS_INSTALL=OFF -DFLATBUFFERS_BUILD_FLATLIB=OFF -DFLATBUFFERS_BUILD_FLATHASH=OFF . && make -j4
- name: test
working-directory: tests
run: bash DartTest.sh

19
.github/workflows/label.yml vendored Normal file
View File

@@ -0,0 +1,19 @@
# This workflow will triage pull requests and apply a label based on the
# paths that are modified in the pull request.
#
# To use this workflow, you will need to set up a .github/labeler.yml
# file with configuration. For more information, see:
# https://github.com/actions/labeler
name: Labeler
on: [pull_request_target]
jobs:
label:
runs-on: ubuntu-latest
steps:
- uses: actions/labeler@main
with:
repo-token: "${{ secrets.GITHUB_TOKEN }}"

23
.github/workflows/stale.yml vendored Normal file
View File

@@ -0,0 +1,23 @@
name: Mark stale issues and pull requests
on:
schedule:
- cron: "30 20 * * *"
jobs:
stale:
runs-on: ubuntu-latest
steps:
- uses: actions/stale@v4.0.0
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
stale-pr-message: 'This pull request is stale because it has been open 6 months with no activity. Please comment or this will be closed in 14 days.'
stale-issue-message: 'This issue is stale because it has been open 6 months with no activity. Please comment or this will be closed in 14 days.'
days-before-stale: 182 # 6 months
days-before-close: 14
operations-per-run: 1500
exempt-issue-labels: not-stale
exempt-pr-labels: not-stale
exempt-all-milestones: true

28
.gitignore vendored
View File

@@ -2,6 +2,7 @@
*_wire.bin
.DS_Store
**/.build
build
**/Packages
/*.xcodeproj
**/xcuserdata/
@@ -45,6 +46,8 @@ project.properties
proguard-project.txt
linklint_results
Makefile
flatbenchmark
flatbenchmark.exe
flatc
flatc.exe
flathash
@@ -69,8 +72,6 @@ tests/monsterdata_go_wire.mon
tests/monsterdata_javascript_wire.mon
tests/monsterdata_lobster_wire.mon
tests/monsterdata_rust_wire.mon
tests/unicode_test.mon
tests/ts/
tests/php/
CMakeLists.txt.user
CMakeScripts/**
@@ -102,7 +103,10 @@ android/build/
samples/android/.externalNativeBuild/
samples/android/.gradle/
samples/android/build/
js/flatbuffers.mjs
js/**/*.js
js/**/*.d.ts
mjs/**/*.js
mjs/**/*.d.ts
/bazel-bin
/bazel-flatbuffers
/bazel-genfiles
@@ -122,6 +126,24 @@ dart/doc/api/
Cargo.lock
.corpus**
.seed**
.crash**
grpc/google/
**/Package.resolved
.clangd/**
package-lock.json
/*.ilk
/*.pdb
.clwb
yarn-error.log
.cache/
/flatbuffers.lib
.cmake/
**/dist
**/vendor
**/go.sum
flatbuffers.pc
**/FlatBuffers.Test.Swift.xcodeproj
**/html/**
**/latex/**
# https://cmake.org/cmake/help/latest/module/FetchContent.html#variable:FETCHCONTENT_BASE_DIR
_deps/

View File

@@ -104,6 +104,7 @@ matrix:
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo ln -s -v -f $(which gcc-$GCC_VERSION) /usr/bin/gcc; fi
script:
- pip install cmake
- bash .travis/check-sources.sh
- bash grpc/build_grpc.sh
- cmake .
@@ -114,7 +115,6 @@ matrix:
-DFLATBUFFERS_CODE_SANITIZE=ON
- cmake --build . --target all --clean-first -- -j${JOBS}
- LD_LIBRARY_PATH=$TRAVIS_BUILD_DIR/google/grpc/install/lib ctest --extra-verbose --output-on-failure
- bash .travis/check-generate-code.sh
- language: cpp
os: osx
@@ -125,6 +125,11 @@ matrix:
- BUILD_TYPE=Release
script:
- pip install --user cmake
- mkdir ~/cmake_path
- ln -s $(find ~/Library/Python -name cmake -type f | head -n 1) ~/cmake_path/cmake
- ln -s $(find ~/Library/Python -name ctest -type f | head -n 1) ~/cmake_path/ctest
- export PATH=~/cmake_path:${PATH}
- bash grpc/build_grpc.sh
- cmake .
-DCMAKE_BUILD_TYPE=$BUILD_TYPE
@@ -134,7 +139,6 @@ matrix:
-DFLATBUFFERS_CODE_SANITIZE=ON
- cmake --build . -- -j${JOBS}
- DYLD_LIBRARY_PATH=$TRAVIS_BUILD_DIR/google/grpc/install/lib ctest --extra-verbose --output-on-failure
- bash .travis/check-generate-code.sh
- <<: *conan-linux-master
env: CONAN_GCC_VERSIONS=8 CONAN_DOCKER_IMAGE=conanio/gcc8
@@ -185,37 +189,27 @@ matrix:
components:
- tools
- platform-tools
- build-tools-25.0.2
- android-25
- extra-android-m2repository
licenses:
- 'android-sdk-preview-license-52d11cd2'
- 'android-sdk-license-.+'
- 'google-gdk-license-.+'
compiler:
- gcc
before_install:
# Output something every 10 minutes or Travis kills the job
- while sleep 540; do echo "=====[ $SECONDS seconds still running ]====="; done &
# Install the r17c version of the NDK that still so that we can continue to test with gnustl
# and stlport.
- export ANDROID_NDK_HOME=$HOME/android-ndk
- NDK_ZIP=$ANDROID_NDK_HOME/ndk.zip
- mkdir -p $ANDROID_NDK_HOME
- curl -o $NDK_ZIP https://dl.google.com/android/repository/android-ndk-r17c-linux-x86_64.zip
- unzip -q -d $ANDROID_NDK_HOME $NDK_ZIP
- rm $NDK_ZIP
- mv $ANDROID_NDK_HOME/android-ndk-*/* $ANDROID_NDK_HOME
- rmdir $ANDROID_NDK_HOME/android-ndk-*
- export CMAKE=$(which cmake)
# libc required for prebuilt llvm toolchain the NDK r17c.
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get update -qq; fi
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get install -qq libc6; fi
# Setup environment for Linux build which is required to build the sample.
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test; fi
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get update -qq; fi
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get install -qq g++-$GCC_VERSION; fi
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo apt-get install -qq gcc-$GCC_VERSION; fi
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo ln -s -v -f $(which g++-$GCC_VERSION) /usr/bin/g++; fi
- if [ "$TRAVIS_OS_NAME" == "linux" ]; then sudo ln -s -v -f $(which gcc-$GCC_VERSION) /usr/bin/gcc; fi
- echo y | sdkmanager "platforms;android-30"
- echo y | sdkmanager "build-tools;30.0.2"
- echo y | sdkmanager "ndk-bundle"
- echo y | sdkmanager "cmake;3.6.4111459"
script:
- failed=0; for build_gradle in $(git ls-files | grep build.gradle); do ( cd "$(dirname "${build_gradle}")" && ./gradlew build ) || failed=1; done; exit $((failed))
# Kill the sleep loop
- kill %1
- cmake -DFLATBUFFERS_BUILD_TESTS=OFF -DFLATBUFFERS_BUILD_FLATLIB=OFF -DFLATBUFFERS_BUILD_FLATHASH=OFF .; make; export PATH="$PATH:${PWD}"
- cd android; ./gradlew clean build
- language: generic
if: type IN (pull_request)
os: linux
install:
- bash .travis/format_install.sh
script:
- bash .travis/format_check.sh

52
.travis/format_check.sh Normal file
View File

@@ -0,0 +1,52 @@
#!/bin/bash
#
# Copyright 2021 Google Inc. All rights reserved.
#
# 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.
set -e
# HACKY solution to make nodejs work.
source ~/.nvm/nvm.sh
nvm alias default node
nvm use default
sh scripts/clang-format-git.sh
# Check formatting for go lang
cd go
gofmt -w .
cd ..
cd grpc/examples/go
sh format.sh
cd ../../..
node_modules/.bin/eslint ts/** --ext .ts --quiet --fix
#PYTHON IS DISABLED UNTIL WE CREATE A .pylintrc FILE FOR IT
pylint python/** --disable=all
swiftformat --config swift.swiftformat .
if ! git diff --quiet; then
echo >&2
echo "ERROR: ********************************************************" >&2
echo "ERROR: The following differences were found after running" >&2
echo "ERROR: .travis/format_check.sh script. Maybe you forgot to format" >&2
echo "ERROR: the code after making changes? please check Formatters.md" >&2
echo "ERROR: ********************************************************" >&2
echo >&2
git diff --binary --exit-code
fi

90
.travis/format_install.sh Normal file
View File

@@ -0,0 +1,90 @@
#!/bin/bash
#
# Copyright 2020 Google Inc. All rights reserved.
#
# 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.
set -e
set -x
# install devtools
install_languages() {
sudo apt update
# Install nodeJS and yarn
wget https://raw.githubusercontent.com/creationix/nvm/v0.31.0/nvm.sh -O ~/.nvm/nvm.sh
source ~/.nvm/nvm.sh
nvm install node
node --version
curl -o- -L https://yarnpkg.com/install.sh | bash
export PATH="$HOME/.yarn/bin:$PATH"
yarn config set prefix ~/.yarn -g
export PATH="$HOME/.yarn/bin:$HOME/.config/yarn/global/node_modules/.bin:$PATH"
# Install swift
sudo apt-get install \
binutils \
git \
libc6-dev \
libcurl3 \
libedit2 \
libgcc-5-dev \
libpython2.7 \
libsqlite3-0 \
libstdc++-5-dev \
libxml2 \
pkg-config \
tzdata \
zlib1g-dev
SWIFT_URL=https://swift.org/builds/swift-5.3.1-release/ubuntu1604/swift-5.3.1-RELEASE/swift-5.3.1-RELEASE-ubuntu16.04.tar.gz
curl -fSsL "$SWIFT_URL" -o swift.tar.gz
mkdir ~/swiftbuild
tar -xvzf swift.tar.gz -C ~/swiftbuild
export PATH="~/swiftbuild/swift-5.3.1-RELEASE-ubuntu16.04/usr/bin:$PATH"
mkdir ~/gobuild
wget -c https://golang.org/dl/go1.15.2.linux-amd64.tar.gz
tar -xvzf go1.15.2.linux-amd64.tar.gz -C ~/gobuild
export PATH="~/gobuild/go/bin:$PATH"
swift --version
go version
yarn -v
node -v
}
install_formatters() {
# installing swift formatter
git clone --depth 1 --branch 0.47.4 https://github.com/nicklockwood/SwiftFormat.git
cd SwiftFormat
swift build -c release
sudo cp .build/release/swiftformat /usr/local/bin/swiftformat
cd ..
which yarn
which node
yarn -v
node -v
yarn install
pip install pylint
}
install_languages
export PATH="~/swift/swift/usr/bin:$PATH"
install_formatters

View File

@@ -1,7 +1,7 @@
licenses(["notice"])
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")
licenses(["notice"])
package(
default_visibility = ["//visibility:public"],
)
@@ -10,6 +10,20 @@ exports_files([
"LICENSE",
])
config_setting(
name = "platform_freebsd",
constraint_values = [
"@platforms//os:freebsd",
],
)
config_setting(
name = "platform_openbsd",
constraint_values = [
"@platforms//os:openbsd",
],
)
# Public flatc library to compile flatbuffer files at runtime.
cc_library(
name = "flatbuffers",
@@ -23,10 +37,19 @@ cc_library(
filegroup(
name = "public_headers",
srcs = [
"include/flatbuffers/allocator.h",
"include/flatbuffers/array.h",
"include/flatbuffers/base.h",
"include/flatbuffers/bfbs_generator.h",
"include/flatbuffers/buffer.h",
"include/flatbuffers/buffer_ref.h",
"include/flatbuffers/code_generators.h",
"include/flatbuffers/default_allocator.h",
"include/flatbuffers/detached_buffer.h",
"include/flatbuffers/flatbuffer_builder.h",
"include/flatbuffers/flatbuffers.h",
"include/flatbuffers/flexbuffers.h",
"include/flatbuffers/grpc.h",
"include/flatbuffers/hash.h",
"include/flatbuffers/idl.h",
"include/flatbuffers/minireflect.h",
@@ -34,7 +57,13 @@ filegroup(
"include/flatbuffers/reflection_generated.h",
"include/flatbuffers/registry.h",
"include/flatbuffers/stl_emulation.h",
"include/flatbuffers/string.h",
"include/flatbuffers/struct.h",
"include/flatbuffers/table.h",
"include/flatbuffers/util.h",
"include/flatbuffers/vector.h",
"include/flatbuffers/vector_downward.h",
"include/flatbuffers/verifier.h",
],
)
@@ -72,6 +101,8 @@ cc_library(
"include/flatbuffers/flexbuffers.h",
"include/flatbuffers/stl_emulation.h",
"include/flatbuffers/util.h",
"include/flatbuffers/vector.h",
"include/flatbuffers/verifier.h",
],
linkstatic = 1,
strip_include_prefix = "/include",

View File

@@ -95,7 +95,7 @@ function(build_flatbuffers flatbuffers_schemas
set(generated_include ${generated_includes_dir}/${filename}_generated.h)
add_custom_command(
OUTPUT ${generated_include}
COMMAND ${FLATC} ${FLATC_SCHEMA_ARGS}
COMMAND ${FLATC} ${FLATC_ARGS}
-o ${generated_includes_dir}
${include_params}
-c ${schema}
@@ -150,3 +150,254 @@ function(build_flatbuffers flatbuffers_schemas
${copy_text_schemas_dir})
endif()
endfunction()
# Creates a target that can be linked against that generates flatbuffer headers.
#
# This function takes a target name and a list of schemas. You can also specify
# other flagc flags using the FLAGS option to change the behavior of the flatc
# tool.
#
# Arguments:
# TARGET: The name of the target to generate.
# SCHEMAS: The list of schema files to generate code for.
# BINARY_SCHEMAS_DIR: Optional. The directory in which to generate binary
# schemas. Binary schemas will only be generated if a path is provided.
# INCLUDE: Optional. Search for includes in the specified paths. (Use this
# instead of "-I <path>" and the FLAGS option so that CMake is aware of
# the directories that need to be searched).
# INCLUDE_PREFIX: Optional. The directory in which to place the generated
# files. Use this instead of the --include-prefix option.
# FLAGS: Optional. A list of any additional flags that you would like to pass
# to flatc.
#
# Example:
#
# flatbuffers_generate_headers(
# TARGET my_generated_headers_target
# INCLUDE_PREFIX ${MY_INCLUDE_PREFIX}"
# SCHEMAS ${MY_SCHEMA_FILES}
# BINARY_SCHEMAS_DIR "${MY_BINARY_SCHEMA_DIRECTORY}"
# FLAGS --gen-object-api)
#
# target_link_libraries(MyExecutableTarget
# PRIVATE my_generated_headers_target
# )
function(flatbuffers_generate_headers)
# Parse function arguments.
set(options)
set(one_value_args
"TARGET"
"INCLUDE_PREFIX"
"BINARY_SCHEMAS_DIR")
set(multi_value_args
"SCHEMAS"
"INCLUDE"
"FLAGS")
cmake_parse_arguments(
PARSE_ARGV 0
FLATBUFFERS_GENERATE_HEADERS
"${options}"
"${one_value_args}"
"${multi_value_args}")
# Test if including from FindFlatBuffers
if(FLATBUFFERS_FLATC_EXECUTABLE)
set(FLATC_TARGET "")
set(FLATC ${FLATBUFFERS_FLATC_EXECUTABLE})
else()
set(FLATC_TARGET flatc)
set(FLATC flatc)
endif()
set(working_dir "${CMAKE_CURRENT_SOURCE_DIR}")
# Generate the include files parameters.
set(include_params "")
foreach (include_dir ${FLATBUFFERS_GENERATE_HEADERS_INCLUDE})
set(include_params -I ${include_dir} ${include_params})
endforeach()
# Create a directory to place the generated code.
set(generated_target_dir "${CMAKE_CURRENT_BINARY_DIR}/${FLATBUFFERS_GENERATE_HEADERS_TARGET}")
set(generated_include_dir "${generated_target_dir}")
if (NOT ${FLATBUFFERS_GENERATE_HEADERS_INCLUDE_PREFIX} STREQUAL "")
set(generated_include_dir "${generated_include_dir}/${FLATBUFFERS_GENERATE_HEADERS_INCLUDE_PREFIX}")
list(APPEND FLATBUFFERS_GENERATE_HEADERS_FLAGS
"--include-prefix" ${FLATBUFFERS_GENERATE_HEADERS_INCLUDE_PREFIX})
endif()
# Create rules to generate the code for each schema.
foreach(schema ${FLATBUFFERS_GENERATE_HEADERS_SCHEMAS})
get_filename_component(filename ${schema} NAME_WE)
set(generated_include "${generated_include_dir}/${filename}_generated.h")
add_custom_command(
OUTPUT ${generated_include}
COMMAND ${FLATC} ${FLATC_ARGS}
-o ${generated_include_dir}
${include_params}
-c ${schema}
${FLATBUFFERS_GENERATE_HEADERS_FLAGS}
DEPENDS ${FLATC_TARGET} ${schema}
WORKING_DIRECTORY "${working_dir}")
list(APPEND all_generated_header_files ${generated_include})
# Geneate the binary flatbuffers schemas if instructed to.
if (NOT ${FLATBUFFERS_GENERATE_HEADERS_BINARY_SCHEMAS_DIR} STREQUAL "")
set(binary_schema
"${FLATBUFFERS_GENERATE_HEADERS_BINARY_SCHEMAS_DIR}/${filename}.bfbs")
add_custom_command(
OUTPUT ${binary_schema}
COMMAND ${FLATC} -b --schema
-o ${FLATBUFFERS_GENERATE_HEADERS_BINARY_SCHEMAS_DIR}
${include_params}
${schema}
DEPENDS ${FLATC_TARGET} ${schema}
WORKING_DIRECTORY "${working_dir}")
list(APPEND all_generated_binary_files ${binary_schema})
endif()
endforeach()
# Set up interface library
add_library(${FLATBUFFERS_GENERATE_HEADERS_TARGET} INTERFACE)
target_sources(
${FLATBUFFERS_GENERATE_HEADERS_TARGET}
INTERFACE
${all_generated_header_files}
${all_generated_binary_files}
${FLATBUFFERS_GENERATE_HEADERS_SCHEMAS})
add_dependencies(
${FLATBUFFERS_GENERATE_HEADERS_TARGET}
${FLATC}
${FLATBUFFERS_GENERATE_HEADERS_SCHEMAS})
target_include_directories(
${FLATBUFFERS_GENERATE_HEADERS_TARGET}
INTERFACE ${generated_target_dir})
# Organize file layout for IDEs.
source_group(
TREE "${generated_target_dir}"
PREFIX "Flatbuffers/Generated/Headers Files"
FILES ${all_generated_header_files})
source_group(
TREE ${working_dir}
PREFIX "Flatbuffers/Schemas"
FILES ${FLATBUFFERS_GENERATE_HEADERS_SCHEMAS})
if (NOT ${FLATBUFFERS_GENERATE_HEADERS_BINARY_SCHEMAS_DIR} STREQUAL "")
source_group(
TREE "${FLATBUFFERS_GENERATE_HEADERS_BINARY_SCHEMAS_DIR}"
PREFIX "Flatbuffers/Generated/Binary Schemas"
FILES ${all_generated_binary_files})
endif()
endfunction()
# Creates a target that can be linked against that generates flatbuffer binaries
# from json files.
#
# This function takes a target name and a list of schemas and Json files. You
# can also specify other flagc flags and options to change the behavior of the
# flatc compiler.
#
# Adding this target to your executable ensurses that the flatbuffer binaries
# are compiled before your executable is run.
#
# Arguments:
# TARGET: The name of the target to generate.
# JSON_FILES: The list of json files to compile to flatbuffers binaries.
# SCHEMA: The flatbuffers schema of the Json files to be compiled.
# INCLUDE: Optional. Search for includes in the specified paths. (Use this
# instead of "-I <path>" and the FLAGS option so that CMake is aware of
# the directories that need to be searched).
# OUTPUT_DIR: The directly where the generated flatbuffers binaries should be
# placed.
# FLAGS: Optional. A list of any additional flags that you would like to pass
# to flatc.
#
# Example:
#
# flatbuffers_generate_binary_files(
# TARGET my_binary_data
# SCHEMA "${MY_SCHEMA_DIR}/my_example_schema.fbs"
# JSON_FILES ${MY_JSON_FILES}
# OUTPUT_DIR "${MY_BINARY_DATA_DIRECTORY}"
# FLAGS --strict-json)
#
# target_link_libraries(MyExecutableTarget
# PRIVATE my_binary_data
# )
function(flatbuffers_generate_binary_files)
# Parse function arguments.
set(options)
set(one_value_args
"TARGET"
"SCHEMA"
"OUTPUT_DIR")
set(multi_value_args
"JSON_FILES"
"INCLUDE"
"FLAGS")
cmake_parse_arguments(
PARSE_ARGV 0
FLATBUFFERS_GENERATE_BINARY_FILES
"${options}"
"${one_value_args}"
"${multi_value_args}")
# Test if including from FindFlatBuffers
if(FLATBUFFERS_FLATC_EXECUTABLE)
set(FLATC_TARGET "")
set(FLATC ${FLATBUFFERS_FLATC_EXECUTABLE})
else()
set(FLATC_TARGET flatc)
set(FLATC flatc)
endif()
set(working_dir "${CMAKE_CURRENT_SOURCE_DIR}")
# Generate the include files parameters.
set(include_params "")
foreach (include_dir ${FLATBUFFERS_GENERATE_BINARY_FILES_INCLUDE})
set(include_params -I ${include_dir} ${include_params})
endforeach()
# Create rules to generate the flatbuffers binary for each json file.
foreach(json_file ${FLATBUFFERS_GENERATE_BINARY_FILES_JSON_FILES})
get_filename_component(filename ${json_file} NAME_WE)
set(generated_binary_file "${FLATBUFFERS_GENERATE_BINARY_FILES_OUTPUT_DIR}/${filename}.bin")
add_custom_command(
OUTPUT ${generated_binary_file}
COMMAND ${FLATC} ${FLATC_ARGS}
-o ${FLATBUFFERS_GENERATE_BINARY_FILES_OUTPUT_DIR}
${include_params}
-b ${FLATBUFFERS_GENERATE_BINARY_FILES_SCHEMA} ${json_file}
${FLATBUFFERS_GENERATE_BINARY_FILES_FLAGS}
DEPENDS ${FLATC_TARGET} ${json_file}
WORKING_DIRECTORY "${working_dir}")
list(APPEND all_generated_binary_files ${generated_binary_file})
endforeach()
# Set up interface library
add_library(${FLATBUFFERS_GENERATE_BINARY_FILES_TARGET} INTERFACE)
target_sources(
${FLATBUFFERS_GENERATE_BINARY_FILES_TARGET}
INTERFACE
${all_generated_binary_files}
${FLATBUFFERS_GENERATE_BINARY_FILES_JSON_FILES}
${FLATBUFFERS_GENERATE_BINARY_FILES_SCHEMA})
add_dependencies(
${FLATBUFFERS_GENERATE_BINARY_FILES_TARGET}
${FLATC})
# Organize file layout for IDEs.
source_group(
TREE ${working_dir}
PREFIX "Flatbuffers/JSON Files"
FILES ${FLATBUFFERS_GENERATE_BINARY_FILES_JSON_FILES})
source_group(
TREE ${working_dir}
PREFIX "Flatbuffers/Schemas"
FILES ${FLATBUFFERS_GENERATE_BINARY_FILES_SCHEMA})
source_group(
TREE ${FLATBUFFERS_GENERATE_BINARY_FILES_OUTPUT_DIR}
PREFIX "Flatbuffers/Generated/Binary Files"
FILES ${all_generated_binary_files})
endfunction()

View File

@@ -30,7 +30,7 @@ find_program(FLATBUFFERS_FLATC_EXECUTABLE NAMES flatc)
find_path(FLATBUFFERS_INCLUDE_DIR NAMES flatbuffers/flatbuffers.h)
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(flatbuffers
find_package_handle_standard_args(FlatBuffers
DEFAULT_MSG FLATBUFFERS_FLATC_EXECUTABLE FLATBUFFERS_INCLUDE_DIR)
if(FLATBUFFERS_FOUND)
@@ -58,4 +58,4 @@ else()
set(FLATBUFFERS_INCLUDE_DIR)
endif()
include("${FLATBUFFERS_CMAKE_DIR}/BuildFlatBuffers.cmake")
include("${FLATBUFFERS_CMAKE_DIR}/BuildFlatBuffers.cmake")

View File

@@ -31,4 +31,14 @@ if (UNIX)
set(CPACK_RPM_PACKAGE_NAME "flatbuffers")
set(CPACK_PACKAGE_FILE_NAME
"${CPACK_RPM_PACKAGE_NAME}_${CPACK_RPM_PACKAGE_VERSION}_${CPACK_RPM_PACKAGE_ARCHITECTURE}")
if(NOT DEFINED ${CPACK_PACKAGING_INSTALL_PREFIX})
# Default packaging install prefix on RedHat systems is /usr.
# This is the assumed value when this variable is not defined.
# There is currently a conflict with
# /usr/${CMAKE_INSTALL_LIBDIR}/cmake which is installed by default
# by other packages on RedHat (most notably cmake-filesystem). Ensure
# that on these systems, flatbuffers does not package this path.
# This patch is required for cmake pre-3.17.
list(APPEND CPACK_RPM_EXCLUDE_FROM_AUTO_FILELIST_ADDITION "/usr/${CMAKE_INSTALL_LIBDIR}/cmake")
endif()
endif(UNIX)

View File

@@ -1,11 +1,28 @@
set(VERSION_MAJOR 2)
set(VERSION_MINOR 0)
set(VERSION_PATCH 5)
set(VERSION_COMMIT 0)
find_program(GIT git)
execute_process(
COMMAND ${GIT} describe
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE GIT_DESCRIBE_DIRTY
OUTPUT_STRIP_TRAILING_WHITESPACE
)
string(REGEX REPLACE "^v([0-9]+)\\..*" "\\1" VERSION_MAJOR "${GIT_DESCRIBE_DIRTY}")
string(REGEX REPLACE "^v[0-9]+\\.([0-9]+).*" "\\1" VERSION_MINOR "${GIT_DESCRIBE_DIRTY}")
string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" VERSION_PATCH "${GIT_DESCRIBE_DIRTY}")
string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.[0-9]+\\-([0-9]+).*" "\\1" VERSION_COMMIT "${GIT_DESCRIBE_DIRTY}")
if(GIT)
execute_process(
COMMAND ${GIT} describe --tags
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
OUTPUT_VARIABLE GIT_DESCRIBE_DIRTY
OUTPUT_STRIP_TRAILING_WHITESPACE
RESULT_VARIABLE GIT_DESCRIBE_RESULT
)
if(GIT_DESCRIBE_RESULT EQUAL 0)
string(REGEX REPLACE "^v([0-9]+)\\..*" "\\1" VERSION_MAJOR "${GIT_DESCRIBE_DIRTY}")
string(REGEX REPLACE "^v[0-9]+\\.([0-9]+).*" "\\1" VERSION_MINOR "${GIT_DESCRIBE_DIRTY}")
string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.([0-9]+).*" "\\1" VERSION_PATCH "${GIT_DESCRIBE_DIRTY}")
string(REGEX REPLACE "^v[0-9]+\\.[0-9]+\\.[0-9]+\\-([0-9]+).*" "\\1" VERSION_COMMIT "${GIT_DESCRIBE_DIRTY}")
else()
message(WARNING "git describe failed with exit code: ${GIT_DESCRIBE_RESULT}")
endif()
else()
message(WARNING "git is not found")
endif()
message(STATUS "Proceeding with version: ${VERSION_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}.${VERSION_COMMIT}")

9
CMake/flatbuffers.pc.in Normal file
View File

@@ -0,0 +1,9 @@
libdir=@CMAKE_INSTALL_FULL_LIBDIR@
includedir=@CMAKE_INSTALL_FULL_INCLUDEDIR@
Name: FlatBuffers
Description: Memory Efficient Serialization Library
Version: @VERSION_MAJOR@.@VERSION_MINOR@.@VERSION_PATCH@
Libs: -L${libdir} -lflatbuffers
Cflags: -I${includedir}

View File

@@ -1,9 +1,19 @@
cmake_minimum_required(VERSION 2.8.12)
if (POLICY CMP0048)
cmake_policy(SET CMP0048 NEW)
project(FlatBuffers
DESCRIPTION "Flatbuffers serialization library"
VERSION 2.0.0
LANGUAGES CXX)
else()
project(FlatBuffers)
endif (POLICY CMP0048)
include(CMake/Version.cmake)
# generate compile_commands.json
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
include(CheckCXXSymbolExists)
project(FlatBuffers)
# NOTE: Code coverage only works on Linux & OSX.
option(FLATBUFFERS_CODE_COVERAGE "Enable the code coverage build option." OFF)
@@ -16,6 +26,9 @@ option(FLATBUFFERS_BUILD_FLATC "Enable the build of the flatbuffers compiler"
option(FLATBUFFERS_STATIC_FLATC "Build flatbuffers compiler with -static flag"
OFF)
option(FLATBUFFERS_BUILD_FLATHASH "Enable the build of flathash" ON)
option(FLATBUFFERS_BUILD_BENCHMARKS "Enable the build of flatbenchmark. \"
Requires C++11."
OFF)
option(FLATBUFFERS_BUILD_GRPCTEST "Enable the build of grpctest" OFF)
option(FLATBUFFERS_BUILD_SHAREDLIB
"Enable the build of the flatbuffers shared library"
@@ -38,6 +51,14 @@ option(FLATBUFFERS_BUILD_CPP17
option(FLATBUFFERS_BUILD_LEGACY
"Run C++ code generator with '--cpp-std c++0x' switch."
OFF)
option(FLATBUFFERS_ENABLE_PCH
"Enable precompile headers support for 'flatbuffers' and 'flatc'. \"
Only work if CMake supports 'target_precompile_headers'. \"
This can speed up compilation time."
OFF)
option(FLATBUFFERS_SKIP_MONSTER_EXTRA
"Skip generating monster_extra.fbs that contains non-supported numerical\"
types." OFF)
if(NOT FLATBUFFERS_BUILD_FLATC AND FLATBUFFERS_BUILD_TESTS)
message(WARNING
@@ -53,6 +74,8 @@ endif()
# Auto-detect locale-narrow 'strtod_l' and 'strtoull_l' functions.
if(NOT DEFINED FLATBUFFERS_LOCALE_INDEPENDENT)
include(CheckCXXSymbolExists)
set(FLATBUFFERS_LOCALE_INDEPENDENT 0)
if(MSVC)
check_cxx_symbol_exists(_strtof_l stdlib.h FLATBUFFERS_HAS_STRTOF_L)
@@ -68,17 +91,31 @@ endif()
add_definitions(-DFLATBUFFERS_LOCALE_INDEPENDENT=$<BOOL:${FLATBUFFERS_LOCALE_INDEPENDENT}>)
set(FlatBuffers_Library_SRCS
include/flatbuffers/allocator.h
include/flatbuffers/array.h
include/flatbuffers/base.h
include/flatbuffers/bfbs_generator.h
include/flatbuffers/buffer.h
include/flatbuffers/buffer_ref.h
include/flatbuffers/default_allocator.h
include/flatbuffers/detached_buffer.h
include/flatbuffers/flatbuffer_builder.h
include/flatbuffers/flatbuffers.h
include/flatbuffers/flexbuffers.h
include/flatbuffers/hash.h
include/flatbuffers/idl.h
include/flatbuffers/util.h
include/flatbuffers/minireflect.h
include/flatbuffers/reflection.h
include/flatbuffers/reflection_generated.h
include/flatbuffers/stl_emulation.h
include/flatbuffers/flexbuffers.h
include/flatbuffers/registry.h
include/flatbuffers/minireflect.h
include/flatbuffers/stl_emulation.h
include/flatbuffers/string.h
include/flatbuffers/struct.h
include/flatbuffers/table.h
include/flatbuffers/util.h
include/flatbuffers/vector.h
include/flatbuffers/vector_downward.h
include/flatbuffers/verifier.h
src/idl_parser.cpp
src/idl_gen_text.cpp
src/reflection.cpp
@@ -93,7 +130,7 @@ set(FlatBuffers_Compiler_SRCS
src/idl_gen_kotlin.cpp
src/idl_gen_go.cpp
src/idl_gen_java.cpp
src/idl_gen_js_ts.cpp
src/idl_gen_ts.cpp
src/idl_gen_php.cpp
src/idl_gen_python.cpp
src/idl_gen_lobster.cpp
@@ -105,7 +142,10 @@ set(FlatBuffers_Compiler_SRCS
src/idl_gen_swift.cpp
src/flatc.cpp
src/flatc_main.cpp
src/bfbs_gen.h
src/bfbs_gen_lua.h
include/flatbuffers/code_generators.h
src/bfbs_gen_lua.cpp
src/code_generators.cpp
grpc/src/compiler/schema_interface.h
grpc/src/compiler/cpp_generator.h
@@ -115,10 +155,11 @@ set(FlatBuffers_Compiler_SRCS
grpc/src/compiler/java_generator.h
grpc/src/compiler/java_generator.cc
grpc/src/compiler/python_generator.h
grpc/src/compiler/python_private_generator.h
grpc/src/compiler/python_generator.cc
grpc/src/compiler/swift_generator.h
grpc/src/compiler/swift_generator.cc
grpc/src/compiler/ts_generator.h
grpc/src/compiler/ts_generator.cc
)
set(FlatHash_SRCS
@@ -153,6 +194,8 @@ set(FlatBuffers_Tests_SRCS
${CMAKE_CURRENT_BINARY_DIR}/tests/monster_extra_generated.h
# file generate by running compiler on tests/monster_test.fbs
${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_bfbs_generated.h
# file generate by running compiler on tests/optional_scalars.fbs
${CMAKE_CURRENT_BINARY_DIR}/tests/optional_scalars_generated.h
)
set(FlatBuffers_Tests_CPP17_SRCS
@@ -163,6 +206,8 @@ set(FlatBuffers_Tests_CPP17_SRCS
# file generate by running compiler on tests/monster_test.fbs
${CMAKE_CURRENT_BINARY_DIR}/tests/cpp17/generated_cpp17/monster_test_generated.h
${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h
${CMAKE_CURRENT_BINARY_DIR}/tests/cpp17/generated_cpp17/optional_scalars_generated.h
${CMAKE_CURRENT_BINARY_DIR}/tests/optional_scalars_generated.h
)
set(FlatBuffers_Sample_Binary_SRCS
@@ -210,10 +255,6 @@ if(EXISTS "${CMAKE_TOOLCHAIN_FILE}")
# do not apply any global settings if the toolchain
# is being configured externally
message(STATUS "Using toolchain file: ${CMAKE_TOOLCHAIN_FILE}.")
elseif(APPLE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Werror -Wextra -Wno-unused-parameter")
set(FLATBUFFERS_PRIVATE_CXX_FLAGS "-Wold-style-cast")
elseif(CMAKE_COMPILER_IS_GNUCXX)
if(CYGWIN)
set(CMAKE_CXX_FLAGS
@@ -240,8 +281,14 @@ elseif(CMAKE_COMPILER_IS_GNUCXX)
"${CMAKE_CXX_FLAGS} -fsigned-char")
elseif(${CMAKE_CXX_COMPILER_ID} MATCHES "Clang")
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -std=c++0x -Wall -pedantic -Werror -Wextra -Wno-unused-parameter")
if(APPLE)
set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic -Werror -Wextra -Wno-unused-parameter")
set(FLATBUFFERS_PRIVATE_CXX_FLAGS "-Wold-style-cast")
if(NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.8)
list(APPEND FLATBUFFERS_PRIVATE_CXX_FLAGS "-Wimplicit-fallthrough" "-Wextra-semi" "-Werror=unused-private-field") # enable warning
@@ -268,8 +315,18 @@ elseif(MSVC)
# warning C4512: assignment operator could not be generated
# warning C4316: object allocated on the heap may not be aligned
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4 /WX /wd4512 /wd4316")
# multi-core build.
add_definitions("/MP")
endif()
# Append FLATBUFFERS_CXX_FLAGS to CMAKE_CXX_FLAGS.
if(DEFINED FLATBUFFERS_CXX_FLAGS AND NOT EXISTS "${CMAKE_TOOLCHAIN_FILE}")
message(STATUS "extend CXX_FLAGS with ${FLATBUFFERS_CXX_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FLATBUFFERS_CXX_FLAGS}")
endif()
message(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
if(FLATBUFFERS_CODE_COVERAGE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fprofile-arcs -ftest-coverage")
set(CMAKE_EXE_LINKER_FLAGS
@@ -302,6 +359,15 @@ function(add_fsanitize_to_target _target _sanitizer)
endif()
endfunction()
function(add_pch_to_target _target _pch_header)
if(COMMAND target_precompile_headers)
target_precompile_headers(${_target} PRIVATE ${_pch_header})
if(NOT MSVC)
set_source_files_properties(src/util.cpp PROPERTIES SKIP_PRECOMPILE_HEADERS ON)
endif()
endif()
endfunction()
if(BIICODE)
include(biicode/cmake/biicode.cmake)
return()
@@ -316,10 +382,16 @@ if(FLATBUFFERS_BUILD_FLATLIB)
target_include_directories(flatbuffers INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
target_compile_options(flatbuffers PRIVATE "${FLATBUFFERS_PRIVATE_CXX_FLAGS}")
if(FLATBUFFERS_ENABLE_PCH)
add_pch_to_target(flatbuffers include/flatbuffers/pch/pch.h)
endif()
endif()
if(FLATBUFFERS_BUILD_FLATC)
add_executable(flatc ${FlatBuffers_Compiler_SRCS})
if(FLATBUFFERS_ENABLE_PCH)
add_pch_to_target(flatc include/flatbuffers/pch/flatc_pch.h)
endif()
target_compile_options(flatc PRIVATE "${FLATBUFFERS_PRIVATE_CXX_FLAGS}")
if(FLATBUFFERS_CODE_SANITIZE AND NOT WIN32)
add_fsanitize_to_target(flatc ${FLATBUFFERS_CODE_SANITIZE})
@@ -347,11 +419,14 @@ if(FLATBUFFERS_BUILD_SHAREDLIB)
# - micro updated every release when there is no API/ABI changes
# - minor updated when there are additions in API/ABI
# - major (ABI number) updated when there are changes in ABI (or removals)
set(FlatBuffers_Library_SONAME_MAJOR "1")
set(FlatBuffers_Library_SONAME_FULL "${FlatBuffers_Library_SONAME_MAJOR}.12.0")
set(FlatBuffers_Library_SONAME_MAJOR ${VERSION_MAJOR})
set(FlatBuffers_Library_SONAME_FULL "${FlatBuffers_Library_SONAME_MAJOR}.${VERSION_MINOR}.${VERSION_PATCH}")
set_target_properties(flatbuffers_shared PROPERTIES OUTPUT_NAME flatbuffers
SOVERSION "${FlatBuffers_Library_SONAME_MAJOR}"
VERSION "${FlatBuffers_Library_SONAME_FULL}")
if(FLATBUFFERS_ENABLE_PCH)
add_pch_to_target(flatbuffers_shared include/flatbuffers/pch/pch.h)
endif()
endif()
# Global list of generated files.
@@ -400,11 +475,12 @@ function(compile_flatbuffers_schema_to_binary SRC_FBS)
message(STATUS "`${SRC_FBS}`: add generation of binary (.bfbs) schema")
get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH)
string(REGEX REPLACE "\\.fbs$" ".bfbs" GEN_BINARY_SCHEMA ${SRC_FBS})
# For details about flags see generate_code.bat(sh)
# For details about flags see generate_code.py
add_custom_command(
OUTPUT ${GEN_BINARY_SCHEMA}
COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}"
-b --schema --bfbs-comments --bfbs-builtins
--bfbs-filenames ${SRC_FBS_DIR}
-I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test"
-o "${SRC_FBS_DIR}"
"${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
@@ -422,7 +498,7 @@ function(compile_flatbuffers_schema_to_embedded_binary SRC_FBS OPT)
message(STATUS "`${SRC_FBS}`: add generation of C++ embedded binary schema code with '${OPT}'")
get_filename_component(SRC_FBS_DIR ${SRC_FBS} PATH)
string(REGEX REPLACE "\\.fbs$" "_bfbs_generated.h" GEN_BFBS_HEADER ${SRC_FBS})
# For details about flags see generate_code.bat(sh)
# For details about flags see generate_code.py
add_custom_command(
OUTPUT ${GEN_BFBS_HEADER}
COMMAND "${FLATBUFFERS_FLATC_EXECUTABLE}"
@@ -430,6 +506,7 @@ function(compile_flatbuffers_schema_to_embedded_binary SRC_FBS OPT)
--cpp-ptr-type flatbuffers::unique_ptr # Used to test with C++98 STLs
${OPT}
--bfbs-comments --bfbs-builtins --bfbs-gen-embed
--bfbs-filenames ${SRC_FBS_DIR}
-I "${CMAKE_CURRENT_SOURCE_DIR}/tests/include_test"
-o "${SRC_FBS_DIR}"
"${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}"
@@ -438,6 +515,37 @@ function(compile_flatbuffers_schema_to_embedded_binary SRC_FBS OPT)
register_generated_output(${GEN_BFBS_HEADER})
endfunction()
# Look if we have python 3 installed so that we can run the generate code python
# script after flatc is built.
find_package(PythonInterp 3)
if(PYTHONINTERP_FOUND AND
# Skip doing this if the MSVC version is below VS 12.
# https://cmake.org/cmake/help/latest/variable/MSVC_VERSION.html
(NOT MSVC OR MSVC_VERSION GREATER 1800))
if(WIN32)
set(GENERATION_SCRIPT py scripts/generate_code.py)
else()
set(GENERATION_SCRIPT scripts/generate_code.py)
endif()
if(FLATBUFFERS_BUILD_LEGACY)
# Need to set --cpp-std c++-0x options
set(GENERATION_SCRIPT ${GENERATION_SCRIPT} --cpp-0x)
endif()
if(FLATBUFFERS_SKIP_MONSTER_EXTRA)
set(GENERATION_SCRIPT ${GENERATION_SCRIPT} --skip-monster-extra)
endif()
add_custom_command(
TARGET flatc
POST_BUILD
COMMAND ${GENERATION_SCRIPT} --flatc "${FLATBUFFERS_FLATC_EXECUTABLE}"
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
COMMENT "Running ${GENERATION_SCRIPT}..."
VERBATIM)
else()
message("No Python3 interpreter found! Unable to generate files automatically.")
endif()
if(FLATBUFFERS_BUILD_TESTS)
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/tests" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/samples" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}")
@@ -445,9 +553,10 @@ if(FLATBUFFERS_BUILD_TESTS)
# TODO Add (monster_test.fbs monsterdata_test.json)->monsterdata_test.mon
compile_flatbuffers_schema_to_cpp(tests/monster_test.fbs)
compile_flatbuffers_schema_to_binary(tests/monster_test.fbs)
compile_flatbuffers_schema_to_cpp(tests/namespace_test/namespace_test1.fbs)
compile_flatbuffers_schema_to_cpp(tests/namespace_test/namespace_test2.fbs)
compile_flatbuffers_schema_to_cpp(tests/union_vector/union_vector.fbs)
compile_flatbuffers_schema_to_cpp_opt(tests/namespace_test/namespace_test1.fbs "--no-includes;--gen-compare;--gen-name-strings")
compile_flatbuffers_schema_to_cpp_opt(tests/namespace_test/namespace_test2.fbs "--no-includes;--gen-compare;--gen-name-strings")
compile_flatbuffers_schema_to_cpp_opt(tests/union_vector/union_vector.fbs "--no-includes;--gen-compare;--gen-name-strings")
compile_flatbuffers_schema_to_cpp(tests/optional_scalars.fbs)
compile_flatbuffers_schema_to_cpp_opt(tests/native_type_test.fbs "")
compile_flatbuffers_schema_to_cpp_opt(tests/arrays_test.fbs "--scoped-enums;--gen-compare")
compile_flatbuffers_schema_to_binary(tests/arrays_test.fbs)
@@ -478,7 +587,7 @@ if(FLATBUFFERS_BUILD_TESTS)
if(FLATBUFFERS_BUILD_CPP17)
# Don't generate header for flattests_cpp17 target.
# This target uses "generated_cpp17/monster_test_generated.h"
# produced by direct call of generate_code.bat(sh) script.
# produced by direct call of generate_code.py script.
add_executable(flattests_cpp17 ${FlatBuffers_Tests_CPP17_SRCS})
add_dependencies(flattests_cpp17 generated_code)
target_compile_features(flattests_cpp17 PRIVATE cxx_std_17)
@@ -504,17 +613,20 @@ if(FLATBUFFERS_BUILD_GRPCTEST)
endif()
INCLUDE_DIRECTORIES(${GRPC_INSTALL_PATH}/include)
INCLUDE_DIRECTORIES(${PROTOBUF_DOWNLOAD_PATH}/src)
LINK_DIRECTORIES(${GRPC_INSTALL_PATH}/lib)
find_package(Threads REQUIRED)
list(APPEND CMAKE_PREFIX_PATH ${GRPC_INSTALL_PATH})
find_package(absl CONFIG REQUIRED)
find_package(protobuf CONFIG REQUIRED)
find_package(gRPC CONFIG REQUIRED)
add_executable(grpctest ${FlatBuffers_GRPCTest_SRCS})
add_dependencies(grpctest generated_code)
target_link_libraries(grpctest PRIVATE grpc++_unsecure grpc_unsecure gpr pthread dl)
target_link_libraries(grpctest PRIVATE gRPC::grpc++_unsecure gRPC::grpc_unsecure gRPC::gpr pthread dl)
if(FLATBUFFERS_CODE_SANITIZE AND NOT WIN32)
# GRPC test has problems with alignment and will fail under ASAN/UBSAN.
# add_fsanitize_to_target(grpctest ${FLATBUFFERS_CODE_SANITIZE})
endif()
endif()
include(CMake/Version.cmake)
if(FLATBUFFERS_INSTALL)
include(GNUInstallDirs)
@@ -589,6 +701,14 @@ if(FLATBUFFERS_INSTALL)
DESTINATION ${FB_CMAKE_DIR}
)
endif()
if(FLATBUFFERS_BUILD_SHAREDLIB OR FLATBUFFERS_BUILD_FLATLIB)
configure_file(CMake/flatbuffers.pc.in flatbuffers.pc @ONLY)
install(
FILES "${CMAKE_CURRENT_BINARY_DIR}/flatbuffers.pc"
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
)
endif()
endif()
if(FLATBUFFERS_BUILD_TESTS)
@@ -619,9 +739,15 @@ if(UNIX)
# Use of CPack only supported on Linux systems.
if(FLATBUFFERS_PACKAGE_DEBIAN)
include(CMake/PackageDebian.cmake)
include(CPack)
endif()
if (FLATBUFFERS_PACKAGE_REDHAT)
include(CMake/PackageRedhat.cmake)
include(CPack)
endif()
include(CPack)
endif()
# Include for running Google Benchmarks.
if(FLATBUFFERS_BUILD_BENCHMARKS AND CMAKE_VERSION VERSION_GREATER 3.13)
add_subdirectory(benchmarks)
endif()

22
Formatters.md Normal file
View File

@@ -0,0 +1,22 @@
# Format Guidelines
If you are interesting in contributing to the flatbuffers project, please take a second to read this document. Each language has it's own set of rules, that are defined in their respective formatter/linter documents.
# Notes
- Run the linter on the language you are working on before making a Pull Request.
- DONT format/lint the generated code.
# Languages
## C++
C++ uses `clang-format` as it's formatter. Run the following script `sh scripts/clang-format-git.sh`, and it should style the C++ code according to [google style guide](https://google.github.io/styleguide/cppguide.html).
## Swift
Swift uses swiftformat as it's formatter. Take a look at [how to install here](https://github.com/nicklockwood/SwiftFormat/blob/master/README.md#how-do-i-install-it). Run the following command `swiftformat --config swift.swiftformat .` in the root directory of the project
## Typescript
Typescript uses eslint as it's linter. Take a look at [how to install here](https://eslint.org/docs/user-guide/getting-started). Run the following command `eslint ts/** --ext .ts` in the root directory of the project

11
SECURITY.md Normal file
View File

@@ -0,0 +1,11 @@
# Security Policy
## Reporting a Vulnerability
To report a security issue, please use http://g.co/vulnz. We use
http://g.co/vulnz for our intake, and do coordination and disclosure here on
GitHub (including using GitHub Security Advisory). The Google Security Team will
respond within 5 working days of your report on g.co/vulnz.
Select the `I want to report a technical security or an abuse risk related bug
in a Google product (SQLi, XSS, etc.)` option and complete the form.

View File

@@ -4,15 +4,43 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "io_bazel_rules_go",
sha256 = "d1ffd055969c8f8d431e2d439813e42326961d0942bdf734d2c95dc30c369566",
urls = [
"https://storage.googleapis.com/bazel-mirror/github.com/bazelbuild/rules_go/releases/download/v0.20.3/rules_go-v0.20.3.tar.gz",
"https://github.com/bazelbuild/rules_go/releases/download/v0.20.3/rules_go-v0.20.3.tar.gz",
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.24.5/rules_go-v0.24.5.tar.gz",
"https://github.com/bazelbuild/rules_go/releases/download/v0.24.5/rules_go-v0.24.5.tar.gz",
],
sha256 = "e88471aea3a3a4f19ec1310a55ba94772d087e9ce46e41ae38ecebe17935de7b",
)
load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies", "go_register_toolchains")
load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
go_rules_dependencies()
go_register_toolchains()
##### Protobuf
_PROTOBUF_VERSION = "3.15.2"
http_archive(
name = "com_google_protobuf",
strip_prefix = "protobuf-" + _PROTOBUF_VERSION,
urls = [
"https://github.com/protocolbuffers/protobuf/archive/v" + _PROTOBUF_VERSION + ".tar.gz",
],
)
##### GRPC
_GRPC_VERSION = "1.39.0"
http_archive(
name = "com_github_grpc_grpc",
strip_prefix = "grpc-" + _GRPC_VERSION,
urls = ["https://github.com/grpc/grpc/archive/v" + _GRPC_VERSION + ".tar.gz"],
)
load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps")
grpc_deps()
load("@com_github_grpc_grpc//bazel:grpc_extra_deps.bzl", "grpc_extra_deps")
grpc_extra_deps()

1
android/app/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/build

134
android/app/build.gradle Normal file
View File

@@ -0,0 +1,134 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 30
buildToolsVersion "30.0.2"
defaultConfig {
applicationId "com.flatbuffers.app"
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
externalNativeBuild {
cmake {
arguments "-DFLATBUFFERS_SRC=${rootProject.projectDir}/.."
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
ndkVersion "21.3.6528147"
externalNativeBuild {
cmake {
path "src/main/cpp/CMakeLists.txt"
}
}
task generateFbsCpp(type: Exec) {
def inputDir = file("$projectDir/src/main/fbs")
def outputCppDir = file("$projectDir/src/main/cpp/generated/")
def fbsFiles = layout.files { file(inputDir).listFiles() }.filter { File f -> f.name.endsWith(".fbs") }.toList()
ignoreExitValue(true)
standardOutput = new ByteArrayOutputStream()
errorOutput = new ByteArrayOutputStream()
def commandLineArgs = ['flatc', '-o', outputCppDir, '--cpp']
fbsFiles.forEach{
commandLineArgs.add(it.path)
}
commandLine commandLineArgs
doFirst {
delete "$outputCppDir/"
mkdir "$outputCppDir/"
}
doLast {
if (execResult.getExitValue() != 0) {
println(standardOutput.toString())
throw new GradleException("flatc command line failed")
}
}
}
task generateFbsKotlin(type: Exec) {
def inputDir = file("$projectDir/src/main/fbs")
def outputKotlinDir = file("$projectDir/src/main/java/generated/")
def fbsFiles = layout.files { file(inputDir).listFiles() }.filter { File f -> f.name.endsWith(".fbs") }.toList()
ignoreExitValue(true)
standardOutput = new ByteArrayOutputStream()
errorOutput = new ByteArrayOutputStream()
def commandLineArgs = ['flatc', '-o', outputKotlinDir, '--kotlin']
fbsFiles.forEach{
commandLineArgs.add(it.path)
}
commandLine commandLineArgs
doFirst {
delete "$outputKotlinDir/"
mkdir "$outputKotlinDir/"
}
doLast {
if (execResult.getExitValue() != 0) {
println(standardOutput.toString())
throw new GradleException("flatc command line failed")
}
}
}
afterEvaluate {
android.applicationVariants.all { variant ->
variant.javaCompiler.dependsOn(generateFbsKotlin)
variant.javaCompiler.dependsOn(generateFbsCpp)
}
}
flavorDimensions "stl-variant"
productFlavors {
gnustl {
dimension "stl-variant"
applicationIdSuffix ".gnustl"
versionNameSuffix "-gnustl"
externalNativeBuild {
ndkBuild {
arguments "APP_STL=gnustl_static"
}
}
}
libcpp {
dimension "stl-variant"
applicationIdSuffix ".libcpp"
versionNameSuffix "-libcpp"
externalNativeBuild {
ndkBuild {
arguments "APP_STL=c++_static"
}
}
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.flatbuffers:flatbuffers-java:2.0.0'
}

21
android/app/proguard-rules.pro vendored Normal file
View File

@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

View File

@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.flatbuffers.app">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

View File

@@ -0,0 +1,54 @@
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.
include_directories(${FLATBUFFERS_SRC}/include)
add_subdirectory(flatbuffers)
FILE(GLOB Generated_SRCS generated/*.h)
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
animals.cpp
${Generated_SRCS}
)
# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
flatbuffers
flatbuffers_tests
# Links the target library to the log library
# included in the NDK.
${log-lib} )

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2014 Google Inc. All rights reserved.
*
* 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.
*/
#include <jni.h>
#include <string>
#include <search.h>
#include "generated/animal_generated.h"
using namespace com::fbs::app;
using namespace flatbuffers;
extern "C" JNIEXPORT jbyteArray JNICALL Java_com_flatbuffers_app_MainActivity_createAnimalFromJNI(
JNIEnv* env,
jobject /* this */) {
// create a new animal flatbuffers
auto fb = FlatBufferBuilder(1024);
auto tiger = CreateAnimalDirect(fb, "Tiger", "Roar", 300);
fb.Finish(tiger);
// copies it to a Java byte array.
auto buf = reinterpret_cast<jbyte*>(fb.GetBufferPointer());
int size = fb.GetSize();
auto ret = env->NewByteArray(size);
env->SetByteArrayRegion (ret, 0, fb.GetSize(), buf);
return ret;
}

View File

@@ -0,0 +1,73 @@
# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.4.1)
include_directories(${FLATBUFFERS_SRC}/include)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11 -fexceptions -Wall -DFLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE")
# Certain platforms such as ARM do not use signed chars by default
# which causes issues with certain bounds checks.
set(CMAKE_CXX_FLAGS
"${CMAKE_CXX_FLAGS} -fsigned-char")
set(FlatBuffers_Library_SRCS
${FLATBUFFERS_SRC}/include/flatbuffers/allocator.h
${FLATBUFFERS_SRC}/include/flatbuffers/array.h
${FLATBUFFERS_SRC}/include/flatbuffers/base.h
${FLATBUFFERS_SRC}/include/flatbuffers/bfbs_generator.h
${FLATBUFFERS_SRC}/include/flatbuffers/buffer.h
${FLATBUFFERS_SRC}/include/flatbuffers/buffer_ref.h
${FLATBUFFERS_SRC}/include/flatbuffers/default_allocator.h
${FLATBUFFERS_SRC}/include/flatbuffers/detached_buffer.h
${FLATBUFFERS_SRC}/include/flatbuffers/flatbuffer_builder.h
${FLATBUFFERS_SRC}/include/flatbuffers/flatbuffers.h
${FLATBUFFERS_SRC}/include/flatbuffers/flexbuffers.h
${FLATBUFFERS_SRC}/include/flatbuffers/hash.h
${FLATBUFFERS_SRC}/include/flatbuffers/idl.h
${FLATBUFFERS_SRC}/include/flatbuffers/minireflect.h
${FLATBUFFERS_SRC}/include/flatbuffers/reflection.h
${FLATBUFFERS_SRC}/include/flatbuffers/reflection_generated.h
${FLATBUFFERS_SRC}/include/flatbuffers/registry.h
${FLATBUFFERS_SRC}/include/flatbuffers/stl_emulation.h
${FLATBUFFERS_SRC}/include/flatbuffers/string.h
${FLATBUFFERS_SRC}/include/flatbuffers/struct.h
${FLATBUFFERS_SRC}/include/flatbuffers/table.h
${FLATBUFFERS_SRC}/include/flatbuffers/util.h
${FLATBUFFERS_SRC}/include/flatbuffers/vector.h
${FLATBUFFERS_SRC}/include/flatbuffers/vector_downward.h
${FLATBUFFERS_SRC}/include/flatbuffers/verifier.h
${FLATBUFFERS_SRC}/src/idl_parser.cpp
${FLATBUFFERS_SRC}/src/idl_gen_text.cpp
${FLATBUFFERS_SRC}/src/reflection.cpp
${FLATBUFFERS_SRC}/src/util.cpp
${FLATBUFFERS_SRC}/src/idl_gen_fbs.cpp
${FLATBUFFERS_SRC}/src/code_generators.cpp
)
set(FlatBuffers_Test_SRCS
${FLATBUFFERS_SRC}/tests/test.cpp
${FLATBUFFERS_SRC}/tests/test_assert.h
${FLATBUFFERS_SRC}/tests/test_builder.h
${FLATBUFFERS_SRC}/tests/test_assert.cpp
${FLATBUFFERS_SRC}/tests/test_builder.cpp
${FLATBUFFERS_SRC}/tests/native_type_test_impl.h
${FLATBUFFERS_SRC}/tests/native_type_test_impl.cpp
)
add_library( # Sets the name of the library.
flatbuffers
${FlatBuffers_Library_SRCS}
${FlatBuffers_Test_SRCS}
${Generated_SRCS}
)
add_library( # Sets the name of the library.
flatbuffers_tests
${FlatBuffers_Test_SRCS}
)

View File

@@ -0,0 +1,128 @@
// automatically generated by the FlatBuffers compiler, do not modify
#ifndef FLATBUFFERS_GENERATED_ANIMAL_COM_FBS_APP_H_
#define FLATBUFFERS_GENERATED_ANIMAL_COM_FBS_APP_H_
#include "flatbuffers/flatbuffers.h"
namespace com {
namespace fbs {
namespace app {
struct Animal;
struct AnimalBuilder;
struct Animal FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
typedef AnimalBuilder Builder;
enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE {
VT_NAME = 4,
VT_SOUND = 6,
VT_WEIGHT = 8
};
const flatbuffers::String *name() const {
return GetPointer<const flatbuffers::String *>(VT_NAME);
}
const flatbuffers::String *sound() const {
return GetPointer<const flatbuffers::String *>(VT_SOUND);
}
uint16_t weight() const {
return GetField<uint16_t>(VT_WEIGHT, 0);
}
bool Verify(flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyOffset(verifier, VT_NAME) &&
verifier.VerifyString(name()) &&
VerifyOffset(verifier, VT_SOUND) &&
verifier.VerifyString(sound()) &&
VerifyField<uint16_t>(verifier, VT_WEIGHT) &&
verifier.EndTable();
}
};
struct AnimalBuilder {
typedef Animal Table;
flatbuffers::FlatBufferBuilder &fbb_;
flatbuffers::uoffset_t start_;
void add_name(flatbuffers::Offset<flatbuffers::String> name) {
fbb_.AddOffset(Animal::VT_NAME, name);
}
void add_sound(flatbuffers::Offset<flatbuffers::String> sound) {
fbb_.AddOffset(Animal::VT_SOUND, sound);
}
void add_weight(uint16_t weight) {
fbb_.AddElement<uint16_t>(Animal::VT_WEIGHT, weight, 0);
}
explicit AnimalBuilder(flatbuffers::FlatBufferBuilder &_fbb)
: fbb_(_fbb) {
start_ = fbb_.StartTable();
}
AnimalBuilder &operator=(const AnimalBuilder &);
flatbuffers::Offset<Animal> Finish() {
const auto end = fbb_.EndTable(start_);
auto o = flatbuffers::Offset<Animal>(end);
return o;
}
};
inline flatbuffers::Offset<Animal> CreateAnimal(
flatbuffers::FlatBufferBuilder &_fbb,
flatbuffers::Offset<flatbuffers::String> name = 0,
flatbuffers::Offset<flatbuffers::String> sound = 0,
uint16_t weight = 0) {
AnimalBuilder builder_(_fbb);
builder_.add_sound(sound);
builder_.add_name(name);
builder_.add_weight(weight);
return builder_.Finish();
}
inline flatbuffers::Offset<Animal> CreateAnimalDirect(
flatbuffers::FlatBufferBuilder &_fbb,
const char *name = nullptr,
const char *sound = nullptr,
uint16_t weight = 0) {
auto name__ = name ? _fbb.CreateString(name) : 0;
auto sound__ = sound ? _fbb.CreateString(sound) : 0;
return com::fbs::app::CreateAnimal(
_fbb,
name__,
sound__,
weight);
}
inline const com::fbs::app::Animal *GetAnimal(const void *buf) {
return flatbuffers::GetRoot<com::fbs::app::Animal>(buf);
}
inline const com::fbs::app::Animal *GetSizePrefixedAnimal(const void *buf) {
return flatbuffers::GetSizePrefixedRoot<com::fbs::app::Animal>(buf);
}
inline bool VerifyAnimalBuffer(
flatbuffers::Verifier &verifier) {
return verifier.VerifyBuffer<com::fbs::app::Animal>(nullptr);
}
inline bool VerifySizePrefixedAnimalBuffer(
flatbuffers::Verifier &verifier) {
return verifier.VerifySizePrefixedBuffer<com::fbs::app::Animal>(nullptr);
}
inline void FinishAnimalBuffer(
flatbuffers::FlatBufferBuilder &fbb,
flatbuffers::Offset<com::fbs::app::Animal> root) {
fbb.Finish(root);
}
inline void FinishSizePrefixedAnimalBuffer(
flatbuffers::FlatBufferBuilder &fbb,
flatbuffers::Offset<com::fbs::app::Animal> root) {
fbb.FinishSizePrefixed(root);
}
} // namespace app
} // namespace fbs
} // namespace com
#endif // FLATBUFFERS_GENERATED_ANIMAL_COM_FBS_APP_H_

View File

@@ -12,11 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
namespace sample;
namespace com.fbs.app;
table Animal {
name:string;
sound:string;
weight: uint16;
}
root_type Animal;

View File

@@ -0,0 +1,51 @@
package com.flatbuffers.app
import android.annotation.SuppressLint
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import com.fbs.app.Animal
import com.google.flatbuffers.FlatBufferBuilder
import java.nio.ByteBuffer
@ExperimentalUnsignedTypes
class MainActivity : AppCompatActivity() {
@SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val tiger = Animal.getRootAsAnimal(ByteBuffer.wrap(createAnimalFromJNI()))
findViewById<TextView>(R.id.tv_animal_one).text = animalInfo(tiger)
findViewById<TextView>(R.id.tv_animal_two).text = animalInfo(createAnimalFromKotlin())
}
// This function is a sample of communicating FlatBuffers between JNI (native C++) and Java.
// Implementation can be found on animals.cpp file.
private external fun createAnimalFromJNI(): ByteArray
// Create a "Cow" Animal flatbuffers from Kotlin
private fun createAnimalFromKotlin():Animal {
val fb = FlatBufferBuilder(100)
val cowOffset = Animal.createAnimal(
builder = fb,
nameOffset = fb.createString("Cow"),
soundOffset = fb.createString("Moo"),
weight = 720u
)
fb.finish(cowOffset)
return Animal.getRootAsAnimal(fb.dataBuffer())
}
private fun animalInfo(animal: Animal): String =
"The ${animal.name} sound is ${animal.sound} and it weights ${animal.weight}kg."
companion object {
// Used to load the 'native-lib' library on application startup.
init {
System.loadLibrary("native-lib")
}
}
}

View File

@@ -0,0 +1,64 @@
// automatically generated by the FlatBuffers compiler, do not modify
package com.fbs.app
import java.nio.*
import kotlin.math.sign
import com.google.flatbuffers.*
@Suppress("unused")
@ExperimentalUnsignedTypes
class Animal : Table() {
fun __init(_i: Int, _bb: ByteBuffer) {
__reset(_i, _bb)
}
fun __assign(_i: Int, _bb: ByteBuffer) : Animal {
__init(_i, _bb)
return this
}
val name : String?
get() {
val o = __offset(4)
return if (o != 0) __string(o + bb_pos) else null
}
val nameAsByteBuffer : ByteBuffer get() = __vector_as_bytebuffer(4, 1)
fun nameInByteBuffer(_bb: ByteBuffer) : ByteBuffer = __vector_in_bytebuffer(_bb, 4, 1)
val sound : String?
get() {
val o = __offset(6)
return if (o != 0) __string(o + bb_pos) else null
}
val soundAsByteBuffer : ByteBuffer get() = __vector_as_bytebuffer(6, 1)
fun soundInByteBuffer(_bb: ByteBuffer) : ByteBuffer = __vector_in_bytebuffer(_bb, 6, 1)
val weight : UShort
get() {
val o = __offset(8)
return if(o != 0) bb.getShort(o + bb_pos).toUShort() else 0u
}
companion object {
fun validateVersion() = Constants.FLATBUFFERS_2_0_0()
fun getRootAsAnimal(_bb: ByteBuffer): Animal = getRootAsAnimal(_bb, Animal())
fun getRootAsAnimal(_bb: ByteBuffer, obj: Animal): Animal {
_bb.order(ByteOrder.LITTLE_ENDIAN)
return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb))
}
fun createAnimal(builder: FlatBufferBuilder, nameOffset: Int, soundOffset: Int, weight: UShort) : Int {
builder.startTable(3)
addSound(builder, soundOffset)
addName(builder, nameOffset)
addWeight(builder, weight)
return endAnimal(builder)
}
fun startAnimal(builder: FlatBufferBuilder) = builder.startTable(3)
fun addName(builder: FlatBufferBuilder, name: Int) = builder.addOffset(0, name, 0)
fun addSound(builder: FlatBufferBuilder, sound: Int) = builder.addOffset(1, sound, 0)
fun addWeight(builder: FlatBufferBuilder, weight: UShort) = builder.addShort(2, weight.toShort(), 0)
fun endAnimal(builder: FlatBufferBuilder) : Int {
val o = builder.endTable()
return o
}
fun finishAnimalBuffer(builder: FlatBufferBuilder, offset: Int) = builder.finish(offset)
fun finishSizePrefixedAnimalBuffer(builder: FlatBufferBuilder, offset: Int) = builder.finishSizePrefixed(offset)
}
}

View File

@@ -0,0 +1,30 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt"
android:width="108dp"
android:height="108dp"
android:viewportWidth="108"
android:viewportHeight="108">
<path android:pathData="M31,63.928c0,0 6.4,-11 12.1,-13.1c7.2,-2.6 26,-1.4 26,-1.4l38.1,38.1L107,108.928l-32,-1L31,63.928z">
<aapt:attr name="android:fillColor">
<gradient
android:endX="85.84757"
android:endY="92.4963"
android:startX="42.9492"
android:startY="49.59793"
android:type="linear">
<item
android:color="#44000000"
android:offset="0.0" />
<item
android:color="#00000000"
android:offset="1.0" />
</gradient>
</aapt:attr>
</path>
<path
android:fillColor="#FFFFFF"
android:fillType="nonZero"
android:pathData="M65.3,45.828l3.8,-6.6c0.2,-0.4 0.1,-0.9 -0.3,-1.1c-0.4,-0.2 -0.9,-0.1 -1.1,0.3l-3.9,6.7c-6.3,-2.8 -13.4,-2.8 -19.7,0l-3.9,-6.7c-0.2,-0.4 -0.7,-0.5 -1.1,-0.3C38.8,38.328 38.7,38.828 38.9,39.228l3.8,6.6C36.2,49.428 31.7,56.028 31,63.928h46C76.3,56.028 71.8,49.428 65.3,45.828zM43.4,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2c-0.3,-0.7 -0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C45.3,56.528 44.5,57.328 43.4,57.328L43.4,57.328zM64.6,57.328c-0.8,0 -1.5,-0.5 -1.8,-1.2s-0.1,-1.5 0.4,-2.1c0.5,-0.5 1.4,-0.7 2.1,-0.4c0.7,0.3 1.2,1 1.2,1.8C66.5,56.528 65.6,57.328 64.6,57.328L64.6,57.328z"
android:strokeWidth="1"
android:strokeColor="#00000000" />
</vector>

View File

@@ -0,0 +1,170 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="108dp"
android:height="108dp"
android:viewportWidth="108"
android:viewportHeight="108">
<path
android:fillColor="#3DDC84"
android:pathData="M0,0h108v108h-108z" />
<path
android:fillColor="#00000000"
android:pathData="M9,0L9,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M19,0L19,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M29,0L29,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M39,0L39,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M49,0L49,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M59,0L59,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M69,0L69,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M79,0L79,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M89,0L89,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M99,0L99,108"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,9L108,9"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,19L108,19"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,29L108,29"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,39L108,39"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,49L108,49"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,59L108,59"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,69L108,69"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,79L108,79"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,89L108,89"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M0,99L108,99"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M19,29L89,29"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M19,39L89,39"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M19,49L89,49"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M19,59L89,59"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M19,69L89,69"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M19,79L89,79"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M29,19L29,89"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M39,19L39,89"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M49,19L49,89"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M59,19L59,89"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M69,19L69,89"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
<path
android:fillColor="#00000000"
android:pathData="M79,19L79,89"
android:strokeWidth="0.8"
android:strokeColor="#33FFFFFF" />
</vector>

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_animal_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Text Sample"/>
<TextView
android:id="@+id/tv_animal_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Text Sample 2"/>
</LinearLayout>

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>

View File

@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@drawable/ic_launcher_background" />
<foreground android:drawable="@drawable/ic_launcher_foreground" />
</adaptive-icon>

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#6200EE</color>
<color name="colorPrimaryDark">#3700B3</color>
<color name="colorAccent">#03DAC5</color>
</resources>

View File

@@ -0,0 +1,3 @@
<resources>
<string name="app_name">FlatbuffersTestApp</string>
</resources>

View File

@@ -0,0 +1,10 @@
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>

View File

@@ -1,108 +1,35 @@
// Copyright (c) 2017 Google, Inc.
//
// This software is provided 'as-is', without any express or implied
// warranty. In no event will the authors be held liable for any damages
// arising from the use of this software.
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
// 1. The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software. If you use this software
// in a product, an acknowledgment in the product documentation would be
// appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.4.10"
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
classpath 'com.android.tools.build:gradle:4.1.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
apply plugin: 'com.android.application'
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
res.srcDirs = ['res']
}
}
externalNativeBuild {
ndkBuild {
path "jni/Android.mk"
}
}
defaultConfig {
applicationId 'com.example.FlatBufferTest'
// This is the platform API where NativeActivity was introduced.
minSdkVersion 9
targetSdkVersion 25
versionCode 1
versionName "1.0"
buildTypes {
release {
minifyEnabled false
}
}
externalNativeBuild {
ndkBuild {
targets "FlatBufferTest"
arguments "-j" + Runtime.getRuntime().availableProcessors()
abiFilters "armeabi-v7a", "arm64-v8a", "x86", "x86_64"
}
}
}
lintOptions {
abortOnError false
}
// Build with each STL variant.
productFlavors {
stlport {
applicationIdSuffix ".stlport"
versionNameSuffix "-stlport"
externalNativeBuild {
ndkBuild {
arguments "APP_STL=stlport_static"
}
}
}
gnustl {
applicationIdSuffix ".gnustl"
versionNameSuffix "-gnustl"
externalNativeBuild {
ndkBuild {
arguments "APP_STL=gnustl_static"
}
}
}
libcpp {
applicationIdSuffix ".libcpp"
versionNameSuffix "-libcpp"
externalNativeBuild {
ndkBuild {
arguments "APP_STL=c++_static"
}
}
}
compileKotlin {
dependsOn flatbuffer
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}

21
android/gradle.properties Normal file
View File

@@ -0,0 +1,21 @@
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx2048m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app"s APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official

View File

@@ -1,6 +1,6 @@
#Mon Jun 19 11:54:59 PDT 2017
#Thu Oct 29 19:47:23 CET 2020
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.0-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip

View File

@@ -1,64 +0,0 @@
# Copyright (c) 2013 Google, Inc.
#
# This software is provided 'as-is', without any express or implied
# warranty. In no event will the authors be held liable for any damages
# arising from the use of this software.
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely, subject to the following restrictions:
# 1. The origin of this software must not be misrepresented; you must not
# claim that you wrote the original software. If you use this software
# in a product, an acknowledgment in the product documentation would be
# appreciated but is not required.
# 2. Altered source versions must be plainly marked as such, and must not be
# misrepresented as being the original software.
# 3. This notice may not be removed or altered from any source distribution.
LOCAL_PATH := $(call my-dir)/../..
include $(LOCAL_PATH)/android/jni/include.mk
LOCAL_PATH := $(call realpath-portable,$(LOCAL_PATH))
# Empty static library so that other projects can include just the basic
# FlatBuffers headers as a module.
include $(CLEAR_VARS)
LOCAL_MODULE := flatbuffers
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_EXPORT_CPPFLAGS := -std=c++11 -fexceptions -Wall \
-DFLATBUFFERS_TRACK_VERIFIER_BUFFER_SIZE
include $(BUILD_STATIC_LIBRARY)
# static library that additionally includes text parsing/generation/reflection
# for projects that want richer functionality.
include $(CLEAR_VARS)
LOCAL_MODULE := flatbuffers_extra
LOCAL_SRC_FILES := src/idl_parser.cpp \
src/idl_gen_text.cpp \
src/reflection.cpp \
src/util.cpp
LOCAL_STATIC_LIBRARIES := flatbuffers
LOCAL_ARM_MODE := arm
include $(BUILD_STATIC_LIBRARY)
# FlatBuffers test
include $(CLEAR_VARS)
LOCAL_MODULE := FlatBufferTest
LOCAL_SRC_FILES := android/jni/main.cpp \
tests/test.cpp \
tests/test_assert.h \
tests/test_builder.h \
tests/test_assert.cpp \
tests/test_builder.cpp \
tests/native_type_test_impl.h \
tests/native_type_test_impl.cpp \
src/idl_gen_fbs.cpp \
src/code_generators.cpp
LOCAL_LDLIBS := -llog -landroid -latomic
LOCAL_STATIC_LIBRARIES := android_native_app_glue flatbuffers_extra
LOCAL_ARM_MODE := arm
include $(BUILD_SHARED_LIBRARY)
$(call import-module,android/native_app_glue)
$(call import-add-path,$(LOCAL_PATH)/../..)

View File

@@ -1,20 +0,0 @@
# Copyright (c) 2014 Google, Inc.
#
# This software is provided 'as-is', without any express or implied
# warranty. In no event will the authors be held liable for any damages
# arising from the use of this software.
# Permission is granted to anyone to use this software for any purpose,
# including commercial applications, and to alter it and redistribute it
# freely, subject to the following restrictions:
# 1. The origin of this software must not be misrepresented; you must not
# claim that you wrote the original software. If you use this software
# in a product, an acknowledgment in the product documentation would be
# appreciated but is not required.
# 2. Altered source versions must be plainly marked as such, and must not be
# misrepresented as being the original software.
# 3. This notice may not be removed or altered from any source distribution.
APP_PLATFORM := android-9
APP_PROJECT_PATH := $(call my-dir)/..
APP_STL ?= stlport_static
APP_ABI := armeabi-v7a
APP_CPPFLAGS += -std=c++11

View File

@@ -1,68 +0,0 @@
@rem Copyright (c) 2013 Google, Inc.
@rem
@rem This software is provided 'as-is', without any express or implied
@rem warranty. In no event will the authors be held liable for any damages
@rem arising from the use of this software.
@rem Permission is granted to anyone to use this software for any purpose,
@rem including commercial applications, and to alter it and redistribute it
@rem freely, subject to the following restrictions:
@rem 1. The origin of this software must not be misrepresented; you must not
@rem claim that you wrote the original software. If you use this software
@rem in a product, an acknowledgment in the product documentation would be
@rem appreciated but is not required.
@rem 2. Altered source versions must be plainly marked as such, and must not be
@rem misrepresented as being the original software.
@rem 3. This notice may not be removed or altered from any source distribution.
@echo off
setlocal enabledelayedexpansion
set thispath=%~dp0
rem Path to cmake passed in by caller.
set cmake=%1
rem Path to cmake project to build.
set cmake_project_path=%2
rem Newest and oldest version of Visual Studio that it's possible to select.
set visual_studio_version_max=20
set visual_studio_version_min=8
rem Determine the newest version of Visual Studio installed on this machine.
set visual_studio_version=
for /L %%a in (%visual_studio_version_max%,-1,%visual_studio_version_min%) do (
echo Searching for Visual Studio %%a >&2
reg query HKLM\SOFTWARE\Microsoft\VisualStudio\%%a.0 /ve 1>NUL 2>NUL
if !ERRORLEVEL! EQU 0 (
set visual_studio_version=%%a
goto found_vs
)
)
echo Unable to determine whether Visual Studio is installed. >&2
exit /B 1
:found_vs
rem Map Visual Studio version to cmake generator name.
if "%visual_studio_version%"=="8" (
set cmake_generator=Visual Studio 8 2005
)
if "%visual_studio_version%"=="9" (
set cmake_generator=Visual Studio 9 2008
)
if %visual_studio_version% GEQ 10 (
set cmake_generator=Visual Studio %visual_studio_version%
)
rem Set visual studio version variable for msbuild.
set VisualStudioVersion=%visual_studio_version%.0
rem Generate Visual Studio solution.
echo Generating solution for %cmake_generator%. >&2
cd "%cmake_project_path%"
%cmake% -G"%cmake_generator%"
if %ERRORLEVEL% NEQ 0 (
exit /B %ERRORLEVEL%
)
rem Build flatc
python %thispath%\msbuild.py flatc.vcxproj
if ERRORLEVEL 1 exit /B 1

View File

@@ -1,237 +0,0 @@
# Copyright 2014 Google Inc. All rights reserved.
#
# 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.
# This file contains utility functions for Android projects using Flatbuffers.
# To use this file, include it in your project's Android.mk by calling near the
# top of your android makefile like so:
#
# include $(FLATBUFFERS_DIR)/android/jni/include.mk
#
# You will also need to import the flatbuffers module using the standard
# import-module function.
#
# The main functionality this file provides are the following functions:
# flatbuffers_fbs_to_h: Converts flatbuffer schema paths to header paths.
# flatbuffers_header_build_rule:
# Creates a build rule for a schema's generated header. This build rule
# has a dependency on the flatc compiler which will be built if necessary.
# flatbuffers_header_build_rules:
# Creates build rules for generated headers for each schema listed and sets
# up depenedendies.
#
# More information and example usage can be found in the comments preceeding
# each function.
# Targets to build the Flatbuffers compiler as well as some utility definitions
ifeq (,$(FLATBUFFERS_INCLUDE_MK_))
FLATBUFFERS_INCLUDE_MK_ := 1
# Portable version of $(realpath) that omits drive letters on Windows.
realpath-portable = $(join $(filter %:,$(subst :,: ,$1)),\
$(realpath $(filter-out %:,$(subst :,: ,$1))))
PROJECT_OS := $(OS)
ifeq (,$(OS))
PROJECT_OS := $(shell uname -s)
else
ifneq ($(findstring Windows,$(PROJECT_OS)),)
PROJECT_OS := Windows
endif
endif
# The following block generates build rules which result in headers being
# rebuilt from flatbuffers schemas.
FLATBUFFERS_CMAKELISTS_DIR := \
$(call realpath-portable,$(dir $(lastword $(MAKEFILE_LIST)))/../..)
# Directory that contains the FlatBuffers compiler.
ifeq (Windows,$(PROJECT_OS))
FLATBUFFERS_FLATC_PATH?=$(FLATBUFFERS_CMAKELISTS_DIR)
FLATBUFFERS_FLATC := $(lastword \
$(wildcard $(FLATBUFFERS_FLATC_PATH)/*/flatc.exe) \
$(wildcard $(FLATBUFFERS_FLATC_PATH)/flatc.exe))
endif
ifeq (Linux,$(PROJECT_OS))
FLATBUFFERS_FLATC_PATH?=$(FLATBUFFERS_CMAKELISTS_DIR)
FLATBUFFERS_FLATC := $(FLATBUFFERS_FLATC_PATH)/flatc
endif
ifeq (Darwin,$(PROJECT_OS))
FLATBUFFERS_FLATC_PATH?=$(FLATBUFFERS_CMAKELISTS_DIR)
FLATBUFFERS_FLATC := $(lastword \
$(wildcard $(FLATBUFFERS_FLATC_PATH)/*/flatc) \
$(wildcard $(FLATBUFFERS_FLATC_PATH)/flatc))
endif
FLATBUFFERS_FLATC_ARGS?=
# Search for cmake.
CMAKE_ROOT := \
$(call realpath-portable,$(LOCAL_PATH)/../../../../../../prebuilts/cmake)
ifeq (,$(CMAKE))
ifeq (Linux,$(PROJECT_OS))
CMAKE := $(wildcard $(CMAKE_ROOT)/linux-x86/current/bin/cmake*)
endif
ifeq (Darwin,$(PROJECT_OS))
CMAKE := \
$(wildcard $(CMAKE_ROOT)/darwin-x86_64/current/*.app/Contents/bin/cmake)
endif
ifeq (Windows,$(PROJECT_OS))
CMAKE := $(wildcard $(CMAKE_ROOT)/windows/current/bin/cmake*)
endif
endif
ifeq (,$(CMAKE))
CMAKE := cmake
endif
# Windows friendly portable local path.
# GNU-make doesn't like : in paths, must use relative paths on Windows.
ifeq (Windows,$(PROJECT_OS))
PORTABLE_LOCAL_PATH =
else
PORTABLE_LOCAL_PATH = $(LOCAL_PATH)/
endif
# Generate a host build rule for the flatbuffers compiler.
ifeq (Windows,$(PROJECT_OS))
define build_flatc_recipe
$(FLATBUFFERS_CMAKELISTS_DIR)\android\jni\build_flatc.bat \
$(CMAKE) $(FLATBUFFERS_CMAKELISTS_DIR)
endef
endif
ifeq (Linux,$(PROJECT_OS))
define build_flatc_recipe
+cd $(FLATBUFFERS_CMAKELISTS_DIR) && \
$(CMAKE) . && \
$(MAKE) flatc
endef
endif
ifeq (Darwin,$(PROJECT_OS))
define build_flatc_recipe
cd $(FLATBUFFERS_CMAKELISTS_DIR) && "$(CMAKE)" -GXcode . && \
xcodebuild -target flatc
endef
endif
ifeq (,$(build_flatc_recipe))
ifeq (,$(FLATBUFFERS_FLATC))
$(error flatc binary not found!)
endif
endif
# Generate a build rule for flatc.
ifeq ($(strip $(FLATBUFFERS_FLATC)),)
flatc_target := build_flatc
.PHONY: $(flatc_target)
FLATBUFFERS_FLATC := \
python $(FLATBUFFERS_CMAKELISTS_DIR)/android/jni/run_flatc.py \
$(FLATBUFFERS_CMAKELISTS_DIR)
else
flatc_target := $(FLATBUFFERS_FLATC)
endif
$(flatc_target):
$(call build_flatc_recipe)
# $(flatbuffers_fbs_to_h schema_dir,output_dir,path)
#
# Convert the specified schema path to a Flatbuffers generated header path.
# For example:
#
# $(call flatbuffers_fbs_to_h,$(MY_PROJ_DIR)/schemas,\
# $(MY_PROJ_DIR)/gen/include,$(MY_PROJ_DIR)/schemas/example.fbs)
#
# This will convert the file path `$(MY_PROJ_DIR)/schemas/example.fbs)` to
# `$(MY_PROJ_DIR)/gen/include/example_generated.h`
define flatbuffers_fbs_to_h
$(subst $(1),$(2),$(patsubst %.fbs,%_generated.h,$(3)))
endef
# $(flatbuffers_header_build_rule schema_file,schema_dir,output_dir,\
# schema_include_dirs)
#
# Generate a build rule that will convert a Flatbuffers schema to a generated
# header derived from the schema filename using flatbuffers_fbs_to_h. For
# example:
#
# $(call flatbuffers_header_build_rule,$(MY_PROJ_DIR)/schemas/example.fbs,\
# $(MY_PROJ_DIR)/schemas,$(MY_PROJ_DIR)/gen/include)
#
# The final argument, schema_include_dirs, is optional and is only needed when
# the schema files depend on other schema files outside their own directory.
define flatbuffers_header_build_rule
$(eval \
$(call flatbuffers_fbs_to_h,$(2),$(3),$(1)): $(1) $(flatc_target)
$(call host-echo-build-step,generic,Generate) \
$(subst $(LOCAL_PATH)/,,$(call flatbuffers_fbs_to_h,$(2),$(3),$(1)))
$(hide) $$(FLATBUFFERS_FLATC) $(FLATBUFFERS_FLATC_ARGS) \
$(foreach include,$(4),-I $(include)) -o $$(dir $$@) -c $$<)
endef
# TODO: Remove when the LOCAL_PATH expansion bug in the NDK is fixed.
# Override the default behavior of local-source-file-path to workaround
# a bug which prevents the build of deeply nested projects when NDK_OUT is
# set.
local-source-file-path=\
$(if $(call host-path-is-absolute,$1),$1,$(call \
realpath-portable,$(LOCAL_PATH)/$1))
# $(flatbuffers_header_build_rules schema_files,schema_dir,output_dir,\
# schema_include_dirs,src_files,[build_target],[dependencies]))
#
# $(1) schema_files: Space separated list of flatbuffer schema files.
# $(2) schema_dir: Directory containing the flatbuffer schemas.
# $(3) output_dir: Where to place the generated files.
# $(4) schema_include_dirs: Directories to include when generating schemas.
# $(5) src_files: Files that should depend upon the headers generated from the
# flatbuffer schemas.
# $(6) build_target: Name of a build target that depends upon all generated
# headers.
# $(7) dependencies: Space seperated list of additional build targets src_files
# should depend upon.
#
# Use this in your own Android.mk file to generate build rules that will
# generate header files for your flatbuffer schemas as well as automatically
# set your source files to be dependent on the generated headers. For example:
#
# $(call flatbuffers_header_build_rules,$(MY_PROJ_SCHEMA_FILES),\
# $(MY_PROJ_SCHEMA_DIR),$(MY_PROJ_GENERATED_OUTPUT_DIR),
# $(MY_PROJ_SCHEMA_INCLUDE_DIRS),$(LOCAL_SRC_FILES))
#
# NOTE: Due problesm with path processing in ndk-build when presented with
# deeply nested projects must redefine LOCAL_PATH after include this makefile
# using:
#
# LOCAL_PATH := $(call realpath-portable,$(LOCAL_PATH))
#
define flatbuffers_header_build_rules
$(foreach schema,$(1),\
$(call flatbuffers_header_build_rule,\
$(schema),$(strip $(2)),$(strip $(3)),$(strip $(4))))\
$(foreach src,$(strip $(5)),\
$(eval $(call local-source-file-path,$(src)): \
$(foreach schema,$(strip $(1)),\
$(call flatbuffers_fbs_to_h,$(strip $(2)),$(strip $(3)),$(schema)))))\
$(if $(6),\
$(foreach schema,$(strip $(1)),\
$(eval $(6): \
$(call flatbuffers_fbs_to_h,$(strip $(2)),$(strip $(3)),$(schema)))),)\
$(if $(7),\
$(foreach src,$(strip $(5)),\
$(eval $(call local-source-file-path,$(src)): $(strip $(7)))),)\
$(if $(7),\
$(foreach dependency,$(strip $(7)),\
$(eval $(6): $(dependency))),)
endef
endif # FLATBUFFERS_INCLUDE_MK_

View File

@@ -1,77 +0,0 @@
#!/usr/bin/python
# Copyright 2014 Google Inc. All rights reserved.
#
# 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.
"""Simple script that locates the newest MSBuild in one of several locations.
This script will find the highest version number of MSBuild and run it,
passing its arguments through to MSBuild.
"""
import glob
import os
import re
import string
import subprocess
import sys
SYSTEMROOT = os.getenv("SYSTEMROOT", "c:\\windows")
PROGRAM_FILES = os.getenv("ProgramFiles", "c:\\Program Files")
PROGRAM_FILES_X86 = os.getenv("ProgramFiles(x86)", "c:\\Program Files (x86)")
SEARCH_FOLDERS = [ PROGRAM_FILES + "\\MSBuild\\*\\Bin\\MSBuild.exe",
PROGRAM_FILES_X86 + "\\MSBuild\\*\\Bin\\MSBuild.exe",
SYSTEMROOT + "\\Microsoft.NET\Framework\\*\\MSBuild.exe" ]
def compare_version(a, b):
"""Compare two version number strings of the form W.X.Y.Z.
The numbers are compared most-significant to least-significant.
For example, 12.345.67.89 > 2.987.88.99.
Args:
a: First version number string to compare
b: Second version number string to compare
Returns:
0 if the numbers are identical, a positive number if 'a' is larger, and
a negative number if 'b' is larger.
"""
aa = string.split(a, ".")
bb = string.split(b, ".")
for i in range(0, 4):
if aa[i] != bb[i]:
return cmp(int(aa[i]), int(bb[i]))
return 0
def main():
msbuilds = []
for folder in SEARCH_FOLDERS:
for file in glob.glob(folder):
p = subprocess.Popen([file, "/version"], stdout=subprocess.PIPE)
out, err = p.communicate()
match = re.search("^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+$", out, re.M)
if match:
msbuilds.append({ 'ver':match.group(), 'exe':file })
msbuilds.sort(lambda x, y: compare_version(x['ver'], y['ver']), reverse=True)
if len(msbuilds) == 0:
print "Unable to find MSBuild.\n"
return -1;
cmd = [msbuilds[0]['exe']]
cmd.extend(sys.argv[1:])
return subprocess.call(cmd)
if __name__ == '__main__':
sys.exit(main())

View File

@@ -1,46 +0,0 @@
#!/usr/bin/python
# Copyright 2015 Google Inc. All rights reserved.
#
# 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.
import os
import platform
import subprocess
import sys
EXECUTABLE_EXTENSION = '.exe' if platform.system() == 'Windows' else ''
# Paths to search for flatc relative to the current working directory.
FLATC_SEARCH_PATHS = [os.path.curdir, 'Release', 'Debug']
def main():
"""Script that finds and runs flatc built from source."""
if len(sys.argv) < 2:
sys.stderr.write('Usage: run_flatc.py flatbuffers_dir [flatc_args]\n')
return 1
cwd = os.getcwd()
flatc = ''
flatbuffers_dir = sys.argv[1]
for path in FLATC_SEARCH_PATHS:
current = os.path.join(flatbuffers_dir, path,
'flatc' + EXECUTABLE_EXTENSION)
if os.path.exists(current):
flatc = current
break
if not flatc:
sys.stderr.write('flatc not found\n')
return 1
command = [flatc] + sys.argv[2:]
return subprocess.call(command)
if __name__ == '__main__':
sys.exit(main())

View File

@@ -1,20 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2014 Google, Inc.
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
-->
<resources>
<string name="app_name">FlatBufferTest</string>
</resources>

2
android/settings.gradle Normal file
View File

@@ -0,0 +1,2 @@
include ':app'
rootProject.name = "FlatbuffersTest"

View File

@@ -3,6 +3,7 @@ branches:
- master
environment:
nodejs_version: "14"
global:
# Workaround for https://github.com/conda/conda-build/issues/636
@@ -10,29 +11,11 @@ environment:
CONDA_INSTALL_LOCN: "C:\\Miniconda35-x64"
CMAKE_OPTIONS: ""
CPP_TEST_OPTIONS: ""
CHECK_GEN_CODE: 1
matrix:
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
CMAKE_VS_VERSION: "10 2010"
CMAKE_OPTIONS: "-DFLATBUFFERS_BUILD_LEGACY=1"
CPP_TEST_OPTIONS: "--std-cpp c++0x"
MONSTER_EXTRA: "skip"
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
CMAKE_VS_VERSION: "12 2013"
MONSTER_EXTRA: "skip"
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
CMAKE_VS_VERSION: "14 2015"
MONSTER_EXTRA: ""
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
CMAKE_VS_VERSION: "15 2017"
MONSTER_EXTRA: ""
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019
CMAKE_VS_VERSION: "16 2019"
MONSTER_EXTRA: ""
platform:
- x86
@@ -43,12 +26,9 @@ configuration:
- Release
before_build:
- set MONSTER_EXTRA=%MONSTER_EXTRA%
- cmake . -G"Visual Studio %CMAKE_VS_VERSION%" -DFLATBUFFERS_CODE_SANITIZE=1 %CMAKE_OPTIONS%
# This cuts down on a lot of noise generated by xamarin warnings.
- if exist "C:\Program Files (x86)\MSBuild\14.0\Microsoft.Common.targets\ImportAfter\Xamarin.Common.targets" del "C:\Program Files (x86)\MSBuild\14.0\Microsoft.Common.targets\ImportAfter\Xamarin.Common.targets"
- if exist "C:\Program Files (x86)\MSBuild\15.0\Microsoft.Common.targets\ImportAfter\Xamarin.Common.targets" del "C:\Program Files (x86)\MSBuild\15.0\Microsoft.Common.targets\ImportAfter\Xamarin.Common.targets"
- if exist "C:\Program Files (x86)\MSBuild\16.0\Microsoft.Common.targets\ImportAfter\Xamarin.Common.targets" del "C:\Program Files (x86)\MSBuild\16.0\Microsoft.Common.targets\ImportAfter\Xamarin.Common.targets"
build:
project: ALL_BUILD.vcxproj
@@ -60,63 +40,34 @@ after_build:
install:
- set PATH=%CONDA_INSTALL_LOCN%;%CONDA_INSTALL_LOCN%\scripts;%PATH%;
- curl -sSf -o rustup-init.exe https://win.rustup.rs/
- rustup-init.exe -y
- set PATH=%PATH%;C:\Users\appveyor\.cargo\bin
- rustc -V
- cargo -V
- ps: Install-Product node $env:nodejs_version
test_script:
- call .appveyor\check-generate-code.bat -b %CONFIGURATION%
- if [%CHECK_GEN_CODE%]==[1] (call py scripts/check_generate_code.py "%CPP_TEST_OPTIONS%")
- "cd tests"
- rem "Building all code"
- generate_code.bat -b %CONFIGURATION% %CPP_TEST_OPTIONS%
- 7z a GeneratedMyGameCode.zip MyGame\
- rem "---------------- C++ -----------------"
- "cd .."
- "%CONFIGURATION%\\flattests.exe"
- "cd tests"
- rem "---------------- Java -----------------"
- "java -version"
- "JavaTest.bat"
- rem "---------------- Rust ----------------"
- "RustTest.bat"
- rem "---------------- JS -----------------"
- "node --version"
- "..\\%CONFIGURATION%\\flatc -b -I include_test monster_test.fbs unicode_test.json"
- "node JavaScriptTest ./monster_test_generated"
- rem "-------------- Python ---------------"
- where python
- python --version
- where pip
- pip --version
- where conda
- conda --version
- rem "installing flatbuffers python library"
- pip install ../python
- rem "testing without installing Numpy"
- python py_test.py 0 0 0
- rem "testing after installing Numpy - disabled"
# FIXME: This has a LOT of unnecessary dependencies and makes the tests fail
# with timeouts.
# - conda install --yes numpy
# - python py_test.py 0 0 0
- "npm install"
- "npm run compile"
- "cd tests"
- "TypeScriptTest.bat"
- rem "---------------- C# -----------------"
# Have to compile this here rather than in "build" above because AppVeyor only
# supports building one project??
- "cd FlatBuffers.Test"
- "copy ..\\monsterdata_test.mon Resources\\"
- "copy ..\\monsterdata_test.json Resources\\"
- "dotnet new sln"
- "dotnet sln add FlatBuffers.Test.csproj"
- "nuget restore"
- "msbuild.exe /property:Configuration=Release;OutputPath=tempcs /verbosity:minimal FlatBuffers.Test.csproj"
- "tempcs\\FlatBuffers.Test.exe"
- "mkdir .tmp"
- "msbuild.exe /property:Configuration=Release;OutputPath=.tmp /verbosity:minimal FlatBuffers.Test.csproj"
- ".tmp\\FlatBuffers.Test.exe"
# Run tests with UNSAFE_BYTEBUFFER
- "msbuild.exe /property:Configuration=Release;UnsafeByteBuffer=true;OutputPath=tempcsUnsafe /verbosity:minimal FlatBuffers.Test.csproj"
- "tempcsUnsafe\\FlatBuffers.Test.exe"
# TODO: add more languages.
- "cd ..\\.."
- "msbuild.exe /property:Configuration=Release;UnsafeByteBuffer=true;OutputPath=.tmp /verbosity:minimal FlatBuffers.Test.csproj"
- ".tmp\\FlatBuffers.Test.exe"
artifacts:
- path: $(CONFIGURATION)\flatc.exe

81
benchmarks/CMakeLists.txt Normal file
View File

@@ -0,0 +1,81 @@
# Setup for running Google Benchmarks (https://github.com/google/benchmark) on
# flatbuffers. This requires both that benchmark library and its depenency gtest
# to build. Instead of including them here or doing a submodule, this uses
# FetchContent (https://cmake.org/cmake/help/latest/module/FetchContent.html) to
# grab the dependencies at config time. This requires CMake 3.14 or higher.
cmake_minimum_required(VERSION 3.14)
include(FetchContent)
# No particular reason for the specific GIT_TAGs for the following repos, they
# were just the latest releases when this was added.
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG e2239ee6043f73722e7aa812a459f54a28552929 # release-1.11.0
)
FetchContent_Declare(
googlebenchmark
GIT_REPOSITORY https://github.com/google/benchmark.git
GIT_TAG f91b6b42b1b9854772a90ae9501464a161707d1e # v1.6.0
)
# For Windows: Prevent overriding the parent project's compiler/linker
# settings.
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(
googletest
googlebenchmark
)
set(CPP_BENCH_DIR cpp)
set(CPP_FB_BENCH_DIR ${CPP_BENCH_DIR}/flatbuffers)
set(CPP_RAW_BENCH_DIR ${CPP_BENCH_DIR}/raw)
set(CPP_BENCH_FBS ${CPP_FB_BENCH_DIR}/bench.fbs)
set(CPP_BENCH_FB_GEN ${CPP_FB_BENCH_DIR}/bench_generated.h)
set(FlatBenchmark_SRCS
${CPP_BENCH_DIR}/benchmark_main.cpp
${CPP_FB_BENCH_DIR}/fb_bench.cpp
${CPP_RAW_BENCH_DIR}/raw_bench.cpp
${CPP_BENCH_FB_GEN}
)
# Generate the flatbuffers benchmark code from the flatbuffers schema using
# flatc itself, thus it depends on flatc. This also depends on the C++ runtime
# flatbuffers and the schema file itself, so it should auto-generated at the
# correct times.
add_custom_command(
OUTPUT ${CPP_BENCH_FB_GEN}
COMMAND
"${FLATBUFFERS_FLATC_EXECUTABLE}"
--cpp
-o ${CPP_FB_BENCH_DIR}
${CPP_BENCH_FBS}
DEPENDS
flatc
flatbuffers
${CPP_BENCH_FBS}
COMMENT "Run Flatbuffers Benchmark Codegen: ${CPP_BENCH_FB_GEN}"
VERBATIM)
# The main flatbuffers benchmark executable
add_executable(flatbenchmark ${FlatBenchmark_SRCS})
# Benchmark requires C++11
target_compile_features(flatbenchmark PUBLIC
cxx_std_11
)
# Set the output directory to the root binary directory
set_target_properties(flatbenchmark
PROPERTIES RUNTIME_OUTPUT_DIRECTORY
"${CMAKE_BINARY_DIR}"
)
# The includes of the benchmark files are fully qualified from flatbuffers root.
target_include_directories(flatbenchmark PUBLIC ${CMAKE_SOURCE_DIR})
target_link_libraries(flatbenchmark
benchmark::benchmark_main # _main to use their entry point
gtest # Link to gtest so we can also assert in the benchmarks
)

19
benchmarks/cpp/bench.h Normal file
View File

@@ -0,0 +1,19 @@
#ifndef BENCHMARKS_CPP_BENCH_H_
#define BENCHMARKS_CPP_BENCH_H_
#include <cstdint>
struct Bench {
virtual ~Bench() {}
inline void Add(int64_t value) { sum += value; }
virtual uint8_t *Encode(void *buf, int64_t &len) = 0;
virtual void *Decode(void *buf, int64_t len) = 0;
virtual int64_t Use(void *decoded) = 0;
virtual void Dealloc(void *decoded) = 0;
int64_t sum = 0;
};
#endif // BENCHMARKS_CPP_BENCH_H_

View File

@@ -0,0 +1,96 @@
#include <benchmark/benchmark.h>
#include <gtest/gtest.h>
#include "benchmarks/cpp/bench.h"
#include "benchmarks/cpp/flatbuffers/fb_bench.h"
#include "benchmarks/cpp/raw/raw_bench.h"
static inline void Encode(benchmark::State &state,
std::unique_ptr<Bench> &bench, uint8_t *buffer) {
int64_t length;
for (auto _ : state) {
bench->Encode(buffer, length);
benchmark::DoNotOptimize(length);
}
}
static inline void Decode(benchmark::State &state,
std::unique_ptr<Bench> &bench, uint8_t *buffer) {
int64_t length;
uint8_t *encoded = bench->Encode(buffer, length);
for (auto _ : state) {
void *decoded = bench->Decode(encoded, length);
benchmark::DoNotOptimize(decoded);
}
}
static inline void Use(benchmark::State &state, std::unique_ptr<Bench> &bench,
uint8_t *buffer, int64_t check_sum) {
int64_t length;
uint8_t *encoded = bench->Encode(buffer, length);
void *decoded = bench->Decode(encoded, length);
int64_t sum = 0;
for (auto _ : state) { sum = bench->Use(decoded); }
EXPECT_EQ(sum, check_sum);
}
static void BM_Flatbuffers_Encode(benchmark::State &state) {
const int64_t kBufferLength = 1024;
uint8_t buffer[kBufferLength];
StaticAllocator allocator(&buffer[0]);
std::unique_ptr<Bench> bench = NewFlatBuffersBench(kBufferLength, &allocator);
Encode(state, bench, buffer);
}
BENCHMARK(BM_Flatbuffers_Encode);
static void BM_Flatbuffers_Decode(benchmark::State &state) {
const int64_t kBufferLength = 1024;
uint8_t buffer[kBufferLength];
StaticAllocator allocator(&buffer[0]);
std::unique_ptr<Bench> bench = NewFlatBuffersBench(kBufferLength, &allocator);
Decode(state, bench, buffer);
}
BENCHMARK(BM_Flatbuffers_Decode);
static void BM_Flatbuffers_Use(benchmark::State &state) {
const int64_t kBufferLength = 1024;
uint8_t buffer[kBufferLength];
StaticAllocator allocator(&buffer[0]);
std::unique_ptr<Bench> bench = NewFlatBuffersBench(kBufferLength, &allocator);
Use(state, bench, buffer, 218812692406581874);
}
BENCHMARK(BM_Flatbuffers_Use);
static void BM_Raw_Encode(benchmark::State &state) {
const int64_t kBufferLength = 1024;
uint8_t buffer[kBufferLength];
std::unique_ptr<Bench> bench = NewRawBench();
Encode(state, bench, buffer);
}
BENCHMARK(BM_Raw_Encode);
static void BM_Raw_Decode(benchmark::State &state) {
const int64_t kBufferLength = 1024;
uint8_t buffer[kBufferLength];
std::unique_ptr<Bench> bench = NewRawBench();
Decode(state, bench, buffer);
}
BENCHMARK(BM_Raw_Decode);
static void BM_Raw_Use(benchmark::State &state) {
const int64_t kBufferLength = 1024;
uint8_t buffer[kBufferLength];
std::unique_ptr<Bench> bench = NewRawBench();
Use(state, bench, buffer, 218812692406581874);
}
BENCHMARK(BM_Raw_Use);

View File

@@ -0,0 +1,52 @@
// Copyright 2021 Google Inc. All rights reserved.
//
// 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.
// trying to represent a typical mix of datatypes:
// 1 array of 3 elements, each element: 1 string, 3 nested objects, 9 scalars
// root element has the array, additional string and an enum
namespace benchmarks_flatbuffers;
enum Enum : short { Apples, Pears, Bananas}
struct Foo {
id:ulong;
count:short;
prefix:byte;
length:uint;
}
struct Bar {
parent:Foo;
time:int;
ratio:float;
size:ushort;
}
table FooBar {
sibling:Bar;
name:string;
rating:double;
postfix:ubyte;
}
table FooBarContainer {
list:[FooBar]; // 3 copies of the above
initialized:bool;
fruit:Enum;
location:string;
}
root_type FooBarContainer;

View File

@@ -0,0 +1,347 @@
// automatically generated by the FlatBuffers compiler, do not modify
#ifndef FLATBUFFERS_GENERATED_BENCH_BENCHMARKS_FLATBUFFERS_H_
#define FLATBUFFERS_GENERATED_BENCH_BENCHMARKS_FLATBUFFERS_H_
#include "flatbuffers/flatbuffers.h"
namespace benchmarks_flatbuffers {
struct Foo;
struct Bar;
struct FooBar;
struct FooBarBuilder;
struct FooBarContainer;
struct FooBarContainerBuilder;
enum Enum : int16_t {
Enum_Apples = 0,
Enum_Pears = 1,
Enum_Bananas = 2,
Enum_MIN = Enum_Apples,
Enum_MAX = Enum_Bananas
};
inline const Enum (&EnumValuesEnum())[3] {
static const Enum values[] = {
Enum_Apples,
Enum_Pears,
Enum_Bananas
};
return values;
}
inline const char * const *EnumNamesEnum() {
static const char * const names[4] = {
"Apples",
"Pears",
"Bananas",
nullptr
};
return names;
}
inline const char *EnumNameEnum(Enum e) {
if (flatbuffers::IsOutRange(e, Enum_Apples, Enum_Bananas)) return "";
const size_t index = static_cast<size_t>(e);
return EnumNamesEnum()[index];
}
FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(8) Foo FLATBUFFERS_FINAL_CLASS {
private:
uint64_t id_;
int16_t count_;
int8_t prefix_;
int8_t padding0__;
uint32_t length_;
public:
Foo()
: id_(0),
count_(0),
prefix_(0),
padding0__(0),
length_(0) {
(void)padding0__;
}
Foo(uint64_t _id, int16_t _count, int8_t _prefix, uint32_t _length)
: id_(flatbuffers::EndianScalar(_id)),
count_(flatbuffers::EndianScalar(_count)),
prefix_(flatbuffers::EndianScalar(_prefix)),
padding0__(0),
length_(flatbuffers::EndianScalar(_length)) {
(void)padding0__;
}
uint64_t id() const {
return flatbuffers::EndianScalar(id_);
}
int16_t count() const {
return flatbuffers::EndianScalar(count_);
}
int8_t prefix() const {
return flatbuffers::EndianScalar(prefix_);
}
uint32_t length() const {
return flatbuffers::EndianScalar(length_);
}
};
FLATBUFFERS_STRUCT_END(Foo, 16);
FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(8) Bar FLATBUFFERS_FINAL_CLASS {
private:
benchmarks_flatbuffers::Foo parent_;
int32_t time_;
float ratio_;
uint16_t size_;
int16_t padding0__; int32_t padding1__;
public:
Bar()
: parent_(),
time_(0),
ratio_(0),
size_(0),
padding0__(0),
padding1__(0) {
(void)padding0__;
(void)padding1__;
}
Bar(const benchmarks_flatbuffers::Foo &_parent, int32_t _time, float _ratio, uint16_t _size)
: parent_(_parent),
time_(flatbuffers::EndianScalar(_time)),
ratio_(flatbuffers::EndianScalar(_ratio)),
size_(flatbuffers::EndianScalar(_size)),
padding0__(0),
padding1__(0) {
(void)padding0__;
(void)padding1__;
}
const benchmarks_flatbuffers::Foo &parent() const {
return parent_;
}
int32_t time() const {
return flatbuffers::EndianScalar(time_);
}
float ratio() const {
return flatbuffers::EndianScalar(ratio_);
}
uint16_t size() const {
return flatbuffers::EndianScalar(size_);
}
};
FLATBUFFERS_STRUCT_END(Bar, 32);
struct FooBar FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
typedef FooBarBuilder Builder;
enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE {
VT_SIBLING = 4,
VT_NAME = 6,
VT_RATING = 8,
VT_POSTFIX = 10
};
const benchmarks_flatbuffers::Bar *sibling() const {
return GetStruct<const benchmarks_flatbuffers::Bar *>(VT_SIBLING);
}
const flatbuffers::String *name() const {
return GetPointer<const flatbuffers::String *>(VT_NAME);
}
double rating() const {
return GetField<double>(VT_RATING, 0.0);
}
uint8_t postfix() const {
return GetField<uint8_t>(VT_POSTFIX, 0);
}
bool Verify(flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyField<benchmarks_flatbuffers::Bar>(verifier, VT_SIBLING) &&
VerifyOffset(verifier, VT_NAME) &&
verifier.VerifyString(name()) &&
VerifyField<double>(verifier, VT_RATING) &&
VerifyField<uint8_t>(verifier, VT_POSTFIX) &&
verifier.EndTable();
}
};
struct FooBarBuilder {
typedef FooBar Table;
flatbuffers::FlatBufferBuilder &fbb_;
flatbuffers::uoffset_t start_;
void add_sibling(const benchmarks_flatbuffers::Bar *sibling) {
fbb_.AddStruct(FooBar::VT_SIBLING, sibling);
}
void add_name(flatbuffers::Offset<flatbuffers::String> name) {
fbb_.AddOffset(FooBar::VT_NAME, name);
}
void add_rating(double rating) {
fbb_.AddElement<double>(FooBar::VT_RATING, rating, 0.0);
}
void add_postfix(uint8_t postfix) {
fbb_.AddElement<uint8_t>(FooBar::VT_POSTFIX, postfix, 0);
}
explicit FooBarBuilder(flatbuffers::FlatBufferBuilder &_fbb)
: fbb_(_fbb) {
start_ = fbb_.StartTable();
}
flatbuffers::Offset<FooBar> Finish() {
const auto end = fbb_.EndTable(start_);
auto o = flatbuffers::Offset<FooBar>(end);
return o;
}
};
inline flatbuffers::Offset<FooBar> CreateFooBar(
flatbuffers::FlatBufferBuilder &_fbb,
const benchmarks_flatbuffers::Bar *sibling = nullptr,
flatbuffers::Offset<flatbuffers::String> name = 0,
double rating = 0.0,
uint8_t postfix = 0) {
FooBarBuilder builder_(_fbb);
builder_.add_rating(rating);
builder_.add_name(name);
builder_.add_sibling(sibling);
builder_.add_postfix(postfix);
return builder_.Finish();
}
inline flatbuffers::Offset<FooBar> CreateFooBarDirect(
flatbuffers::FlatBufferBuilder &_fbb,
const benchmarks_flatbuffers::Bar *sibling = nullptr,
const char *name = nullptr,
double rating = 0.0,
uint8_t postfix = 0) {
auto name__ = name ? _fbb.CreateString(name) : 0;
return benchmarks_flatbuffers::CreateFooBar(
_fbb,
sibling,
name__,
rating,
postfix);
}
struct FooBarContainer FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table {
typedef FooBarContainerBuilder Builder;
enum FlatBuffersVTableOffset FLATBUFFERS_VTABLE_UNDERLYING_TYPE {
VT_LIST = 4,
VT_INITIALIZED = 6,
VT_FRUIT = 8,
VT_LOCATION = 10
};
const flatbuffers::Vector<flatbuffers::Offset<benchmarks_flatbuffers::FooBar>> *list() const {
return GetPointer<const flatbuffers::Vector<flatbuffers::Offset<benchmarks_flatbuffers::FooBar>> *>(VT_LIST);
}
bool initialized() const {
return GetField<uint8_t>(VT_INITIALIZED, 0) != 0;
}
benchmarks_flatbuffers::Enum fruit() const {
return static_cast<benchmarks_flatbuffers::Enum>(GetField<int16_t>(VT_FRUIT, 0));
}
const flatbuffers::String *location() const {
return GetPointer<const flatbuffers::String *>(VT_LOCATION);
}
bool Verify(flatbuffers::Verifier &verifier) const {
return VerifyTableStart(verifier) &&
VerifyOffset(verifier, VT_LIST) &&
verifier.VerifyVector(list()) &&
verifier.VerifyVectorOfTables(list()) &&
VerifyField<uint8_t>(verifier, VT_INITIALIZED) &&
VerifyField<int16_t>(verifier, VT_FRUIT) &&
VerifyOffset(verifier, VT_LOCATION) &&
verifier.VerifyString(location()) &&
verifier.EndTable();
}
};
struct FooBarContainerBuilder {
typedef FooBarContainer Table;
flatbuffers::FlatBufferBuilder &fbb_;
flatbuffers::uoffset_t start_;
void add_list(flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<benchmarks_flatbuffers::FooBar>>> list) {
fbb_.AddOffset(FooBarContainer::VT_LIST, list);
}
void add_initialized(bool initialized) {
fbb_.AddElement<uint8_t>(FooBarContainer::VT_INITIALIZED, static_cast<uint8_t>(initialized), 0);
}
void add_fruit(benchmarks_flatbuffers::Enum fruit) {
fbb_.AddElement<int16_t>(FooBarContainer::VT_FRUIT, static_cast<int16_t>(fruit), 0);
}
void add_location(flatbuffers::Offset<flatbuffers::String> location) {
fbb_.AddOffset(FooBarContainer::VT_LOCATION, location);
}
explicit FooBarContainerBuilder(flatbuffers::FlatBufferBuilder &_fbb)
: fbb_(_fbb) {
start_ = fbb_.StartTable();
}
flatbuffers::Offset<FooBarContainer> Finish() {
const auto end = fbb_.EndTable(start_);
auto o = flatbuffers::Offset<FooBarContainer>(end);
return o;
}
};
inline flatbuffers::Offset<FooBarContainer> CreateFooBarContainer(
flatbuffers::FlatBufferBuilder &_fbb,
flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<benchmarks_flatbuffers::FooBar>>> list = 0,
bool initialized = false,
benchmarks_flatbuffers::Enum fruit = benchmarks_flatbuffers::Enum_Apples,
flatbuffers::Offset<flatbuffers::String> location = 0) {
FooBarContainerBuilder builder_(_fbb);
builder_.add_location(location);
builder_.add_list(list);
builder_.add_fruit(fruit);
builder_.add_initialized(initialized);
return builder_.Finish();
}
inline flatbuffers::Offset<FooBarContainer> CreateFooBarContainerDirect(
flatbuffers::FlatBufferBuilder &_fbb,
const std::vector<flatbuffers::Offset<benchmarks_flatbuffers::FooBar>> *list = nullptr,
bool initialized = false,
benchmarks_flatbuffers::Enum fruit = benchmarks_flatbuffers::Enum_Apples,
const char *location = nullptr) {
auto list__ = list ? _fbb.CreateVector<flatbuffers::Offset<benchmarks_flatbuffers::FooBar>>(*list) : 0;
auto location__ = location ? _fbb.CreateString(location) : 0;
return benchmarks_flatbuffers::CreateFooBarContainer(
_fbb,
list__,
initialized,
fruit,
location__);
}
inline const benchmarks_flatbuffers::FooBarContainer *GetFooBarContainer(const void *buf) {
return flatbuffers::GetRoot<benchmarks_flatbuffers::FooBarContainer>(buf);
}
inline const benchmarks_flatbuffers::FooBarContainer *GetSizePrefixedFooBarContainer(const void *buf) {
return flatbuffers::GetSizePrefixedRoot<benchmarks_flatbuffers::FooBarContainer>(buf);
}
inline bool VerifyFooBarContainerBuffer(
flatbuffers::Verifier &verifier) {
return verifier.VerifyBuffer<benchmarks_flatbuffers::FooBarContainer>(nullptr);
}
inline bool VerifySizePrefixedFooBarContainerBuffer(
flatbuffers::Verifier &verifier) {
return verifier.VerifySizePrefixedBuffer<benchmarks_flatbuffers::FooBarContainer>(nullptr);
}
inline void FinishFooBarContainerBuffer(
flatbuffers::FlatBufferBuilder &fbb,
flatbuffers::Offset<benchmarks_flatbuffers::FooBarContainer> root) {
fbb.Finish(root);
}
inline void FinishSizePrefixedFooBarContainerBuffer(
flatbuffers::FlatBufferBuilder &fbb,
flatbuffers::Offset<benchmarks_flatbuffers::FooBarContainer> root) {
fbb.FinishSizePrefixed(root);
}
} // namespace benchmarks_flatbuffers
#endif // FLATBUFFERS_GENERATED_BENCH_BENCHMARKS_FLATBUFFERS_H_

View File

@@ -0,0 +1,80 @@
#include "benchmarks/cpp/flatbuffers/fb_bench.h"
#include <cstdint>
#include <memory>
#include "benchmarks/cpp/bench.h"
#include "benchmarks/cpp/flatbuffers/bench_generated.h"
#include "flatbuffers/flatbuffers.h"
using namespace flatbuffers;
using namespace benchmarks_flatbuffers;
namespace {
struct FlatBufferBench : Bench {
explicit FlatBufferBench(int64_t initial_size, Allocator *allocator)
: fbb(initial_size, allocator, false) {}
uint8_t *Encode(void *, int64_t &len) {
fbb.Clear();
const int kVectorLength = 3;
Offset<FooBar> vec[kVectorLength];
for (int i = 0; i < kVectorLength; ++i) {
Foo foo(0xABADCAFEABADCAFE + i, 10000 + i, '@' + i, 1000000 + i);
Bar bar(foo, 123456 + i, 3.14159f + i, 10000 + i);
auto name = fbb.CreateString("Hello, World!");
auto foobar =
CreateFooBar(fbb, &bar, name, 3.1415432432445543543 + i, '!' + i);
vec[i] = foobar;
}
auto location = fbb.CreateString("http://google.com/flatbuffers/");
auto foobarvec = fbb.CreateVector(vec, kVectorLength);
auto foobarcontainer =
CreateFooBarContainer(fbb, foobarvec, true, Enum_Bananas, location);
fbb.Finish(foobarcontainer);
len = fbb.GetSize();
return fbb.GetBufferPointer();
}
int64_t Use(void *decoded) {
sum = 0;
auto foobarcontainer = GetFooBarContainer(decoded);
sum = 0;
Add(foobarcontainer->initialized());
Add(foobarcontainer->location()->Length());
Add(foobarcontainer->fruit());
for (unsigned int i = 0; i < foobarcontainer->list()->Length(); i++) {
auto foobar = foobarcontainer->list()->Get(i);
Add(foobar->name()->Length());
Add(foobar->postfix());
Add(static_cast<int64_t>(foobar->rating()));
auto bar = foobar->sibling();
Add(static_cast<int64_t>(bar->ratio()));
Add(bar->size());
Add(bar->time());
auto &foo = bar->parent();
Add(foo.count());
Add(foo.id());
Add(foo.length());
Add(foo.prefix());
}
return sum;
}
void *Decode(void *buffer, int64_t) { return buffer; }
void Dealloc(void *) override{};
FlatBufferBuilder fbb;
};
} // namespace
std::unique_ptr<Bench> NewFlatBuffersBench(int64_t initial_size,
Allocator *allocator) {
return std::unique_ptr<FlatBufferBench>(
new FlatBufferBench(initial_size, allocator));
}

View File

@@ -0,0 +1,23 @@
#ifndef BENCHMARKS_CPP_FLATBUFFERS_FB_BENCH_H_
#define BENCHMARKS_CPP_FLATBUFFERS_FB_BENCH_H_
#include <cstdint>
#include <memory>
#include "benchmarks/cpp/bench.h"
#include "include/flatbuffers/flatbuffers.h"
struct StaticAllocator : public flatbuffers::Allocator {
explicit StaticAllocator(uint8_t *buffer) : buffer_(buffer) {}
uint8_t *allocate(size_t) override { return buffer_; }
void deallocate(uint8_t *, size_t) override {}
uint8_t *buffer_;
};
std::unique_ptr<Bench> NewFlatBuffersBench(
int64_t initial_size = 1024, flatbuffers::Allocator *allocator = nullptr);
#endif // BENCHMARKS_CPP_FLATBUFFERS_FB_BENCH_H_

View File

@@ -0,0 +1,109 @@
#include "benchmarks/cpp/raw/raw_bench.h"
#include <cstdint>
#include <cstring>
#include <memory>
#include "benchmarks/cpp/bench.h"
namespace {
const int64_t kStringLength = 32;
const int64_t kVectorLength = 3;
enum Enum { Apples, Pears, Bananas };
struct Foo {
int64_t id;
short count;
char prefix;
int length;
};
struct Bar {
Foo parent;
int time;
float ratio;
unsigned short size;
};
struct FooBar {
Bar sibling;
// We have to stick this in, otherwise strlen() will make it slower than
// FlatBuffers:
int name_len;
char name[kStringLength];
double rating;
unsigned char postfix;
};
struct FooBarContainer {
FooBar list[kVectorLength]; // 3 copies of the above
bool initialized;
Enum fruit;
int location_len;
char location[kStringLength];
};
struct RawBench : Bench {
uint8_t *Encode(void *buf, int64_t &len) {
FooBarContainer *fbc = new (buf) FooBarContainer;
strcpy(fbc->location, "http://google.com/flatbuffers/"); // Unsafe eek!
fbc->location_len = (int)strlen(fbc->location);
fbc->fruit = Bananas;
fbc->initialized = true;
for (int i = 0; i < kVectorLength; i++) {
// We add + i to not make these identical copies for a more realistic
// compression test.
auto &foobar = fbc->list[i];
foobar.rating = 3.1415432432445543543 + i;
foobar.postfix = '!' + i;
strcpy(foobar.name, "Hello, World!");
foobar.name_len = (int)strlen(foobar.name);
auto &bar = foobar.sibling;
bar.ratio = 3.14159f + i;
bar.size = 10000 + i;
bar.time = 123456 + i;
auto &foo = bar.parent;
foo.id = 0xABADCAFEABADCAFE + i;
foo.count = 10000 + i;
foo.length = 1000000 + i;
foo.prefix = '@' + i;
}
len = sizeof(FooBarContainer);
return reinterpret_cast<uint8_t *>(fbc);
};
int64_t Use(void *decoded) {
auto foobarcontainer = reinterpret_cast<FooBarContainer *>(decoded);
sum = 0;
Add(foobarcontainer->initialized);
Add(foobarcontainer->location_len);
Add(foobarcontainer->fruit);
for (unsigned int i = 0; i < kVectorLength; i++) {
auto foobar = &foobarcontainer->list[i];
Add(foobar->name_len);
Add(foobar->postfix);
Add(static_cast<int64_t>(foobar->rating));
auto bar = &foobar->sibling;
Add(static_cast<int64_t>(bar->ratio));
Add(bar->size);
Add(bar->time);
auto &foo = bar->parent;
Add(foo.count);
Add(foo.id);
Add(foo.length);
Add(foo.prefix);
}
return sum;
}
void *Decode(void *buf, int64_t) { return buf; }
void Dealloc(void *) override{};
};
} // namespace
std::unique_ptr<Bench> NewRawBench() {
return std::unique_ptr<RawBench>(new RawBench());
}

View File

@@ -0,0 +1,10 @@
#ifndef BENCHMARKS_CPP_RAW_RAW_BENCH_H_
#define BENCHMARKS_CPP_RAW_RAW_BENCH_H_
#include <memory>
#include "benchmarks/cpp/bench.h"
std::unique_ptr<Bench> NewRawBench();
#endif // BENCHMARKS_CPP_RAW_RAW_BENCH_H_

View File

@@ -209,7 +209,7 @@ def flatbuffer_cc_library(
Happy dependent Flatbuffering!
'''
output_headers = [
(out_prefix + "%s_generated.h") % (s.replace(".fbs", "").split("/")[-1])
(out_prefix + "%s_generated.h") % (s.replace(".fbs", "").split("/")[-1].split(":")[-1])
for s in srcs
]
reflection_name = "%s_reflection" % name if gen_reflections else ""

View File

@@ -1,4 +1,22 @@
# CHANGELOG
## 2.0.0
- switch to null safety (#6696)
- add Object APIs (pack/unpack) (#6682, #6723, #6846)
- add custom builder buffer allocator support (#6711)
- add `Builder.size()` - finished buffer size (#6403)
- make `writeString()` argument non-nullable (#6737)
- make tables fixed size (expect the number of fields when creating) (#6735)
- make table deduplication optional (param `deduplicateTables`) (#6734)
- change `Builder.reset()` to reuse an existing buffer (#6661)
- change table building to assert() instead of exceptions (#6754)
- optimize `writeString()` for ASCII (param `asciiOptimization`) (#6736)
- change `StringReader` to make ASCII optimization optional (param `asciiOptimization`) (#6758)
- change `[byte]` and `[ubyte]` representation to `dart:typed_data` `Int8List` and `Uint8List` (#6839)
- rename `lowFinish()` to `buffer` getter (#6712)
- fix `Builder._writeString()` - always write trailing zero byte (#6390)
- fix `Builder.reset()` - clear vTables (#6386)
- make sure added padding is zeroed, same as in C++ (#6716)
- many performance improvements (#6755)
## 1.9.2
@@ -11,4 +29,4 @@
## 1.9.0
- Initial release, supports Dart 1.x and many dev versions of Dart 2.x
- Initial release, supports Dart 1.x and many dev versions of Dart 2.x

View File

@@ -1,13 +1,17 @@
# FlatBuffers for Dart
This package is used to read and write FlatBuffer files in Dart.
This package is used to read and write [FlatBuffers](https://google.github.io/flatbuffers/).
Most consumers will want to use the [`flatc`](https://github.com/google/flatbuffers)
compiler to generate Dart code from a FlatBuffers IDL schema. For example, the
`monster_my_game.sample_generated.dart` was generated with `flatc` from
`monster.fbs` in the example folder. The generated classes can be used to read
or write binary files that are interoperable with other languages and platforms
supported by FlatBuffers, as illustrated in the `example.dart` in the
Most consumers will want to use the [`flatc` - FlatBuffer compiler](https://github.com/google/flatbuffers) binary for your platform:
* [Linux](https://github.com/google/flatbuffers/suites/4363603985/artifacts/114682272)
* [macOS](https://github.com/google/flatbuffers/suites/4363603985/artifacts/114682273)
* [Windows](https://github.com/google/flatbuffers/suites/4363603985/artifacts/114682274)
The FlatBuffer compiler `flatc` reads a FlatBuffers IDL schema and generates Dart code.
The generated classes can be used to read or write binary data/files that are interoperable with
other languages and platforms supported by FlatBuffers, as illustrated in the `example.dart` in the
examples folder.
Additional documentation and examples are available [at the FlatBuffers site](https://google.github.io/flatbuffers/index.html)
For more details and documentation, head over to the official site and read the
[Tutorial](https://google.github.io/flatbuffers/flatbuffers_guide_tutorial.html) and how to
[use FlatBuffers in Dart](https://google.github.io/flatbuffers/flatbuffers_guide_use_dart.html).

View File

@@ -0,0 +1 @@
include: package:lints/recommended.yaml

View File

@@ -15,7 +15,7 @@
*/
import 'package:flat_buffers/flat_buffers.dart' as fb;
import './monster_my_game.sample_generated.dart' as myGame;
import './monster_my_game.sample_generated.dart' as my_game;
// Example how to use FlatBuffers to create and read binary buffers.
@@ -25,27 +25,27 @@ void main() {
}
void builderTest() {
final builder = new fb.Builder(initialSize: 1024);
final int weaponOneName = builder.writeString("Sword");
final builder = fb.Builder(initialSize: 1024);
final int? weaponOneName = builder.writeString("Sword");
final int weaponOneDamage = 3;
final int weaponTwoName = builder.writeString("Axe");
final int? weaponTwoName = builder.writeString("Axe");
final int weaponTwoDamage = 5;
final swordBuilder = new myGame.WeaponBuilder(builder)
final swordBuilder = my_game.WeaponBuilder(builder)
..begin()
..addNameOffset(weaponOneName)
..addDamage(weaponOneDamage);
final int sword = swordBuilder.finish();
final axeBuilder = new myGame.WeaponBuilder(builder)
final axeBuilder = my_game.WeaponBuilder(builder)
..begin()
..addNameOffset(weaponTwoName)
..addDamage(weaponTwoDamage);
final int axe = axeBuilder.finish();
// Serialize a name for our monster, called "Orc".
final int name = builder.writeString('Orc');
final int? name = builder.writeString('Orc');
// Create a list representing the inventory of the Orc. Each number
// could correspond to an item that can be claimed after he is slain.
@@ -54,7 +54,7 @@ void builderTest() {
final weapons = builder.writeList([sword, axe]);
// Struct builders are very easy to reuse.
final vec3Builder = new myGame.Vec3Builder(builder);
final vec3Builder = my_game.Vec3Builder(builder);
vec3Builder.finish(4.0, 5.0, 6.0);
vec3Builder.finish(1.0, 2.0, 3.0);
@@ -62,21 +62,21 @@ void builderTest() {
final int hp = 300;
final int mana = 150;
final monster = new myGame.MonsterBuilder(builder)
final monster = my_game.MonsterBuilder(builder)
..begin()
..addNameOffset(name)
..addInventoryOffset(inventory)
..addWeaponsOffset(weapons)
..addEquippedType(myGame.EquipmentTypeId.Weapon)
..addEquippedType(my_game.EquipmentTypeId.Weapon)
..addEquippedOffset(axe)
..addHp(hp)
..addMana(mana)
..addPos(vec3Builder.finish(1.0, 2.0, 3.0))
..addColor(myGame.Color.Red);
..addColor(my_game.Color.Red);
final int monsteroff = monster.finish();
final buffer = builder.finish(monsteroff);
if (verify(buffer)) {
builder.finish(monsteroff);
if (verify(builder.buffer)) {
print(
"The FlatBuffer was successfully created with a builder and verified!");
}
@@ -85,17 +85,17 @@ void builderTest() {
void objectBuilderTest() {
// Create the builder here so we can use it for both weapons and equipped
// the actual data will only be written to the buffer once.
var axe = new myGame.WeaponObjectBuilder(name: 'Axe', damage: 5);
var axe = my_game.WeaponObjectBuilder(name: 'Axe', damage: 5);
var monsterBuilder = new myGame.MonsterObjectBuilder(
pos: new myGame.Vec3ObjectBuilder(x: 1.0, y: 2.0, z: 3.0),
var monsterBuilder = my_game.MonsterObjectBuilder(
pos: my_game.Vec3ObjectBuilder(x: 1.0, y: 2.0, z: 3.0),
mana: 150,
hp: 300,
name: 'Orc',
inventory: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
color: myGame.Color.Red,
weapons: [new myGame.WeaponObjectBuilder(name: 'Sword', damage: 3), axe],
equippedType: myGame.EquipmentTypeId.Weapon,
color: my_game.Color.Red,
weapons: [my_game.WeaponObjectBuilder(name: 'Sword', damage: 3), axe],
equippedType: my_game.EquipmentTypeId.Weapon,
equipped: axe,
);
@@ -114,7 +114,7 @@ void objectBuilderTest() {
bool verify(List<int> buffer) {
// Get access to the root:
var monster = new myGame.Monster(buffer);
var monster = my_game.Monster(buffer);
// Get and test some scalar types from the FlatBuffer.
assert(monster.hp == 80);
@@ -122,31 +122,29 @@ bool verify(List<int> buffer) {
assert(monster.name == "MyMonster");
// Get and test a field of the FlatBuffer's `struct`.
var pos = monster.pos;
assert(pos != null);
var pos = monster.pos!;
assert(pos.z == 3.0);
// Get a test an element from the `inventory` FlatBuffer's `vector`.
var inv = monster.inventory;
assert(inv != null);
var inv = monster.inventory!;
assert(inv.length == 10);
assert(inv[9] == 9);
// Get and test the `weapons` FlatBuffers's `vector`.
var expected_weapon_names = ["Sword", "Axe"];
var expected_weapon_damages = [3, 5];
var weps = monster.weapons;
var expectedWeaponNames = ["Sword", "Axe"];
var expectedWeaponDamages = [3, 5];
var weps = monster.weapons!;
for (int i = 0; i < weps.length; i++) {
assert(weps[i].name == expected_weapon_names[i]);
assert(weps[i].damage == expected_weapon_damages[i]);
assert(weps[i].name == expectedWeaponNames[i]);
assert(weps[i].damage == expectedWeaponDamages[i]);
}
// Get and test the `Equipment` union (`equipped` field).
assert(monster.equippedType.value == myGame.EquipmentTypeId.Weapon.value);
assert(monster.equippedType == myGame.EquipmentTypeId.Weapon);
assert(monster.equippedType!.value == my_game.EquipmentTypeId.Weapon.value);
assert(monster.equippedType == my_game.EquipmentTypeId.Weapon);
assert(monster.equipped is myGame.Weapon);
var equipped = monster.equipped as myGame.Weapon;
assert(monster.equipped is my_game.Weapon);
var equipped = monster.equipped as my_game.Weapon;
assert(equipped.name == "Axe");
assert(equipped.damage == 5);

View File

@@ -1,5 +1,5 @@
// automatically generated by the FlatBuffers compiler, do not modify
// ignore_for_file: unused_import, non_constant_identifier_names
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable
library my_game.sample;
@@ -12,23 +12,29 @@ class Color {
const Color._(this.value);
factory Color.fromValue(int value) {
if (value == null) return null;
if (!values.containsKey(value)) {
throw new StateError('Invalid value $value for bit flag enum Color');
final result = values[value];
if (result == null) {
throw StateError('Invalid value $value for bit flag enum Color');
}
return values[value];
return result;
}
static Color? _createOrNull(int? value) =>
value == null ? null : Color.fromValue(value);
static const int minValue = 0;
static const int maxValue = 2;
static bool containsValue(int value) => values.containsKey(value);
static const Color Red = const Color._(0);
static const Color Green = const Color._(1);
static const Color Blue = const Color._(2);
static get values => {0: Red,1: Green,2: Blue,};
static const Color Red = Color._(0);
static const Color Green = Color._(1);
static const Color Blue = Color._(2);
static const Map<int, Color> values = {
0: Red,
1: Green,
2: Blue};
static const fb.Reader<Color> reader = const _ColorReader();
static const fb.Reader<Color> reader = _ColorReader();
@override
String toString() {
@@ -44,7 +50,7 @@ class _ColorReader extends fb.Reader<Color> {
@override
Color read(fb.BufferContext bc, int offset) =>
new Color.fromValue(const fb.Int8Reader().read(bc, offset));
Color.fromValue(const fb.Int8Reader().read(bc, offset));
}
class EquipmentTypeId {
@@ -52,22 +58,27 @@ class EquipmentTypeId {
const EquipmentTypeId._(this.value);
factory EquipmentTypeId.fromValue(int value) {
if (value == null) return null;
if (!values.containsKey(value)) {
throw new StateError('Invalid value $value for bit flag enum EquipmentTypeId');
final result = values[value];
if (result == null) {
throw StateError('Invalid value $value for bit flag enum EquipmentTypeId');
}
return values[value];
return result;
}
static EquipmentTypeId? _createOrNull(int? value) =>
value == null ? null : EquipmentTypeId.fromValue(value);
static const int minValue = 0;
static const int maxValue = 1;
static bool containsValue(int value) => values.containsKey(value);
static const EquipmentTypeId NONE = const EquipmentTypeId._(0);
static const EquipmentTypeId Weapon = const EquipmentTypeId._(1);
static get values => {0: NONE,1: Weapon,};
static const EquipmentTypeId NONE = EquipmentTypeId._(0);
static const EquipmentTypeId Weapon = EquipmentTypeId._(1);
static const Map<int, EquipmentTypeId> values = {
0: NONE,
1: Weapon};
static const fb.Reader<EquipmentTypeId> reader = const _EquipmentTypeIdReader();
static const fb.Reader<EquipmentTypeId> reader = _EquipmentTypeIdReader();
@override
String toString() {
@@ -83,13 +94,13 @@ class _EquipmentTypeIdReader extends fb.Reader<EquipmentTypeId> {
@override
EquipmentTypeId read(fb.BufferContext bc, int offset) =>
new EquipmentTypeId.fromValue(const fb.Uint8Reader().read(bc, offset));
EquipmentTypeId.fromValue(const fb.Uint8Reader().read(bc, offset));
}
class Vec3 {
Vec3._(this._bc, this._bcOffset);
static const fb.Reader<Vec3> reader = const _Vec3Reader();
static const fb.Reader<Vec3> reader = _Vec3Reader();
final fb.BufferContext _bc;
final int _bcOffset;
@@ -112,13 +123,11 @@ class _Vec3Reader extends fb.StructReader<Vec3> {
@override
Vec3 createObject(fb.BufferContext bc, int offset) =>
new Vec3._(bc, offset);
Vec3._(bc, offset);
}
class Vec3Builder {
Vec3Builder(this.fbBuilder) {
assert(fbBuilder != null);
}
Vec3Builder(this.fbBuilder);
final fb.Builder fbBuilder;
@@ -137,9 +146,9 @@ class Vec3ObjectBuilder extends fb.ObjectBuilder {
final double _z;
Vec3ObjectBuilder({
double x,
double y,
double z,
required double x,
required double y,
required double z,
})
: _x = x,
_y = y,
@@ -147,10 +156,7 @@ class Vec3ObjectBuilder extends fb.ObjectBuilder {
/// Finish building, and store into the [fbBuilder].
@override
int finish(
fb.Builder fbBuilder) {
assert(fbBuilder != null);
int finish(fb.Builder fbBuilder) {
fbBuilder.putFloat32(_z);
fbBuilder.putFloat32(_y);
fbBuilder.putFloat32(_x);
@@ -159,39 +165,39 @@ class Vec3ObjectBuilder extends fb.ObjectBuilder {
/// Convenience method to serialize to byte list.
@override
Uint8List toBytes([String fileIdentifier]) {
fb.Builder fbBuilder = new fb.Builder();
int offset = finish(fbBuilder);
return fbBuilder.finish(offset, fileIdentifier);
Uint8List toBytes([String? fileIdentifier]) {
final fbBuilder = fb.Builder(deduplicateTables: false);
fbBuilder.finish(finish(fbBuilder), fileIdentifier);
return fbBuilder.buffer;
}
}
class Monster {
Monster._(this._bc, this._bcOffset);
factory Monster(List<int> bytes) {
fb.BufferContext rootRef = new fb.BufferContext.fromBytes(bytes);
final rootRef = fb.BufferContext.fromBytes(bytes);
return reader.read(rootRef, 0);
}
static const fb.Reader<Monster> reader = const _MonsterReader();
static const fb.Reader<Monster> reader = _MonsterReader();
final fb.BufferContext _bc;
final int _bcOffset;
Vec3 get pos => Vec3.reader.vTableGet(_bc, _bcOffset, 4, null);
Vec3? get pos => Vec3.reader.vTableGetNullable(_bc, _bcOffset, 4);
int get mana => const fb.Int16Reader().vTableGet(_bc, _bcOffset, 6, 150);
int get hp => const fb.Int16Reader().vTableGet(_bc, _bcOffset, 8, 100);
String get name => const fb.StringReader().vTableGet(_bc, _bcOffset, 10, null);
List<int> get inventory => const fb.ListReader<int>(const fb.Uint8Reader()).vTableGet(_bc, _bcOffset, 14, null);
Color get color => new Color.fromValue(const fb.Int8Reader().vTableGet(_bc, _bcOffset, 16, 2));
List<Weapon> get weapons => const fb.ListReader<Weapon>(Weapon.reader).vTableGet(_bc, _bcOffset, 18, null);
EquipmentTypeId get equippedType => new EquipmentTypeId.fromValue(const fb.Uint8Reader().vTableGet(_bc, _bcOffset, 20, null));
String? get name => const fb.StringReader().vTableGetNullable(_bc, _bcOffset, 10);
List<int>? get inventory => const fb.Uint8ListReader().vTableGetNullable(_bc, _bcOffset, 14);
Color get color => Color.fromValue(const fb.Int8Reader().vTableGet(_bc, _bcOffset, 16, 2));
List<Weapon>? get weapons => const fb.ListReader<Weapon>(Weapon.reader).vTableGetNullable(_bc, _bcOffset, 18);
EquipmentTypeId? get equippedType => EquipmentTypeId._createOrNull(const fb.Uint8Reader().vTableGetNullable(_bc, _bcOffset, 20));
dynamic get equipped {
switch (equippedType?.value) {
case 1: return Weapon.reader.vTableGet(_bc, _bcOffset, 22, null);
case 1: return Weapon.reader.vTableGetNullable(_bc, _bcOffset, 22);
default: return null;
}
}
List<Vec3> get path => const fb.ListReader<Vec3>(Vec3.reader).vTableGet(_bc, _bcOffset, 24, null);
List<Vec3>? get path => const fb.ListReader<Vec3>(Vec3.reader).vTableGetNullable(_bc, _bcOffset, 24);
@override
String toString() {
@@ -204,57 +210,55 @@ class _MonsterReader extends fb.TableReader<Monster> {
@override
Monster createObject(fb.BufferContext bc, int offset) =>
new Monster._(bc, offset);
Monster._(bc, offset);
}
class MonsterBuilder {
MonsterBuilder(this.fbBuilder) {
assert(fbBuilder != null);
}
MonsterBuilder(this.fbBuilder);
final fb.Builder fbBuilder;
void begin() {
fbBuilder.startTable();
fbBuilder.startTable(10);
}
int addPos(int offset) {
fbBuilder.addStruct(0, offset);
return fbBuilder.offset;
}
int addMana(int mana) {
int addMana(int? mana) {
fbBuilder.addInt16(1, mana);
return fbBuilder.offset;
}
int addHp(int hp) {
int addHp(int? hp) {
fbBuilder.addInt16(2, hp);
return fbBuilder.offset;
}
int addNameOffset(int offset) {
int addNameOffset(int? offset) {
fbBuilder.addOffset(3, offset);
return fbBuilder.offset;
}
int addInventoryOffset(int offset) {
int addInventoryOffset(int? offset) {
fbBuilder.addOffset(5, offset);
return fbBuilder.offset;
}
int addColor(Color color) {
int addColor(Color? color) {
fbBuilder.addInt8(6, color?.value);
return fbBuilder.offset;
}
int addWeaponsOffset(int offset) {
int addWeaponsOffset(int? offset) {
fbBuilder.addOffset(7, offset);
return fbBuilder.offset;
}
int addEquippedType(EquipmentTypeId equippedType) {
int addEquippedType(EquipmentTypeId? equippedType) {
fbBuilder.addUint8(8, equippedType?.value);
return fbBuilder.offset;
}
int addEquippedOffset(int offset) {
int addEquippedOffset(int? offset) {
fbBuilder.addOffset(9, offset);
return fbBuilder.offset;
}
int addPathOffset(int offset) {
int addPathOffset(int? offset) {
fbBuilder.addOffset(10, offset);
return fbBuilder.offset;
}
@@ -265,28 +269,28 @@ class MonsterBuilder {
}
class MonsterObjectBuilder extends fb.ObjectBuilder {
final Vec3ObjectBuilder _pos;
final int _mana;
final int _hp;
final String _name;
final List<int> _inventory;
final Color _color;
final List<WeaponObjectBuilder> _weapons;
final EquipmentTypeId _equippedType;
final Vec3ObjectBuilder? _pos;
final int? _mana;
final int? _hp;
final String? _name;
final List<int>? _inventory;
final Color? _color;
final List<WeaponObjectBuilder>? _weapons;
final EquipmentTypeId? _equippedType;
final dynamic _equipped;
final List<Vec3ObjectBuilder> _path;
final List<Vec3ObjectBuilder>? _path;
MonsterObjectBuilder({
Vec3ObjectBuilder pos,
int mana,
int hp,
String name,
List<int> inventory,
Color color,
List<WeaponObjectBuilder> weapons,
EquipmentTypeId equippedType,
Vec3ObjectBuilder? pos,
int? mana,
int? hp,
String? name,
List<int>? inventory,
Color? color,
List<WeaponObjectBuilder>? weapons,
EquipmentTypeId? equippedType,
dynamic equipped,
List<Vec3ObjectBuilder> path,
List<Vec3ObjectBuilder>? path,
})
: _pos = pos,
_mana = mana,
@@ -301,69 +305,54 @@ class MonsterObjectBuilder extends fb.ObjectBuilder {
/// Finish building, and store into the [fbBuilder].
@override
int finish(
fb.Builder fbBuilder) {
assert(fbBuilder != null);
final int nameOffset = fbBuilder.writeString(_name);
final int inventoryOffset = _inventory?.isNotEmpty == true
? fbBuilder.writeListUint8(_inventory)
: null;
final int weaponsOffset = _weapons?.isNotEmpty == true
? fbBuilder.writeList(_weapons.map((b) => b.getOrCreateOffset(fbBuilder)).toList())
: null;
final int equippedOffset = _equipped?.getOrCreateOffset(fbBuilder);
final int pathOffset = _path?.isNotEmpty == true
? fbBuilder.writeListOfStructs(_path)
: null;
fbBuilder.startTable();
int finish(fb.Builder fbBuilder) {
final int? nameOffset = _name == null ? null
: fbBuilder.writeString(_name!);
final int? inventoryOffset = _inventory == null ? null
: fbBuilder.writeListUint8(_inventory!);
final int? weaponsOffset = _weapons == null ? null
: fbBuilder.writeList(_weapons!.map((b) => b.getOrCreateOffset(fbBuilder)).toList());
final int? equippedOffset = _equipped?.getOrCreateOffset(fbBuilder);
final int? pathOffset = _path == null ? null
: fbBuilder.writeListOfStructs(_path!);
fbBuilder.startTable(10);
if (_pos != null) {
fbBuilder.addStruct(0, _pos.finish(fbBuilder));
fbBuilder.addStruct(0, _pos!.finish(fbBuilder));
}
fbBuilder.addInt16(1, _mana);
fbBuilder.addInt16(2, _hp);
if (nameOffset != null) {
fbBuilder.addOffset(3, nameOffset);
}
if (inventoryOffset != null) {
fbBuilder.addOffset(5, inventoryOffset);
}
fbBuilder.addOffset(3, nameOffset);
fbBuilder.addOffset(5, inventoryOffset);
fbBuilder.addInt8(6, _color?.value);
if (weaponsOffset != null) {
fbBuilder.addOffset(7, weaponsOffset);
}
fbBuilder.addOffset(7, weaponsOffset);
fbBuilder.addUint8(8, _equippedType?.value);
if (equippedOffset != null) {
fbBuilder.addOffset(9, equippedOffset);
}
if (pathOffset != null) {
fbBuilder.addOffset(10, pathOffset);
}
fbBuilder.addOffset(9, equippedOffset);
fbBuilder.addOffset(10, pathOffset);
return fbBuilder.endTable();
}
/// Convenience method to serialize to byte list.
@override
Uint8List toBytes([String fileIdentifier]) {
fb.Builder fbBuilder = new fb.Builder();
int offset = finish(fbBuilder);
return fbBuilder.finish(offset, fileIdentifier);
Uint8List toBytes([String? fileIdentifier]) {
final fbBuilder = fb.Builder(deduplicateTables: false);
fbBuilder.finish(finish(fbBuilder), fileIdentifier);
return fbBuilder.buffer;
}
}
class Weapon {
Weapon._(this._bc, this._bcOffset);
factory Weapon(List<int> bytes) {
fb.BufferContext rootRef = new fb.BufferContext.fromBytes(bytes);
final rootRef = fb.BufferContext.fromBytes(bytes);
return reader.read(rootRef, 0);
}
static const fb.Reader<Weapon> reader = const _WeaponReader();
static const fb.Reader<Weapon> reader = _WeaponReader();
final fb.BufferContext _bc;
final int _bcOffset;
String get name => const fb.StringReader().vTableGet(_bc, _bcOffset, 4, null);
int get damage => const fb.Int16Reader().vTableGet(_bc, _bcOffset, 6, null);
String? get name => const fb.StringReader().vTableGetNullable(_bc, _bcOffset, 4);
int get damage => const fb.Int16Reader().vTableGet(_bc, _bcOffset, 6, 0);
@override
String toString() {
@@ -376,25 +365,23 @@ class _WeaponReader extends fb.TableReader<Weapon> {
@override
Weapon createObject(fb.BufferContext bc, int offset) =>
new Weapon._(bc, offset);
Weapon._(bc, offset);
}
class WeaponBuilder {
WeaponBuilder(this.fbBuilder) {
assert(fbBuilder != null);
}
WeaponBuilder(this.fbBuilder);
final fb.Builder fbBuilder;
void begin() {
fbBuilder.startTable();
fbBuilder.startTable(2);
}
int addNameOffset(int offset) {
int addNameOffset(int? offset) {
fbBuilder.addOffset(0, offset);
return fbBuilder.offset;
}
int addDamage(int damage) {
int addDamage(int? damage) {
fbBuilder.addInt16(1, damage);
return fbBuilder.offset;
}
@@ -405,36 +392,32 @@ class WeaponBuilder {
}
class WeaponObjectBuilder extends fb.ObjectBuilder {
final String _name;
final int _damage;
final String? _name;
final int? _damage;
WeaponObjectBuilder({
String name,
int damage,
String? name,
int? damage,
})
: _name = name,
_damage = damage;
/// Finish building, and store into the [fbBuilder].
@override
int finish(
fb.Builder fbBuilder) {
assert(fbBuilder != null);
final int nameOffset = fbBuilder.writeString(_name);
fbBuilder.startTable();
if (nameOffset != null) {
fbBuilder.addOffset(0, nameOffset);
}
int finish(fb.Builder fbBuilder) {
final int? nameOffset = _name == null ? null
: fbBuilder.writeString(_name!);
fbBuilder.startTable(2);
fbBuilder.addOffset(0, nameOffset);
fbBuilder.addInt16(1, _damage);
return fbBuilder.endTable();
}
/// Convenience method to serialize to byte list.
@override
Uint8List toBytes([String fileIdentifier]) {
fb.Builder fbBuilder = new fb.Builder();
int offset = finish(fbBuilder);
return fbBuilder.finish(offset, fileIdentifier);
Uint8List toBytes([String? fileIdentifier]) {
final fbBuilder = fb.Builder(deduplicateTables: false);
fbBuilder.finish(finish(fbBuilder), fileIdentifier);
return fbBuilder.buffer;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,2 @@
export 'src/builder.dart';
export 'src/reference.dart';

676
dart/lib/src/builder.dart Normal file
View File

@@ -0,0 +1,676 @@
import 'dart:convert';
import 'dart:typed_data';
import 'types.dart';
/// The main builder class for creation of a FlexBuffer.
class Builder {
final ByteData _buffer;
List<_StackValue> _stack = [];
List<_StackPointer> _stackPointers = [];
int _offset = 0;
bool _finished = false;
final Map<String, _StackValue> _stringCache = {};
final Map<String, _StackValue> _keyCache = {};
final Map<_KeysHash, _StackValue> _keyVectorCache = {};
final Map<int, _StackValue> _indirectIntCache = {};
final Map<double, _StackValue> _indirectDoubleCache = {};
/// Instantiate the builder if you intent to gradually build up the buffer by calling
/// add... methods and calling [finish] to receive the the resulting byte array.
///
/// The default size of internal buffer is set to 2048. Provide a different value in order to avoid buffer copies.
Builder({int size = 2048}) : _buffer = ByteData(size);
/// Use this method in order to turn an object into a FlexBuffer directly.
///
/// Use the manual instantiation of the [Builder] and gradual addition of values, if performance is more important than convenience.
static ByteBuffer buildFromObject(Object? value) {
final builder = Builder();
builder._add(value);
final buffer = builder.finish();
final byteData = ByteData(buffer.lengthInBytes);
byteData.buffer.asUint8List().setAll(0, buffer);
return byteData.buffer;
}
void _add(Object? value) {
if (value == null) {
addNull();
} else if (value is bool) {
addBool(value);
} else if (value is int) {
addInt(value);
} else if (value is double) {
addDouble(value);
} else if (value is ByteBuffer) {
addBlob(value);
} else if (value is String) {
addString(value);
} else if (value is List<dynamic>) {
startVector();
for (var i = 0; i < value.length; i++) {
_add(value[i]);
}
end();
} else if (value is Map<String, dynamic>) {
startMap();
value.forEach((key, value) {
addKey(key);
_add(value);
});
end();
} else {
throw UnsupportedError('Value of unexpected type: $value');
}
}
/// Use this method if you want to store a null value.
///
/// Specifically useful when building up a vector where values can be null.
void addNull() {
_integrityCheckOnValueAddition();
_stack.add(_StackValue.withNull());
}
/// Adds a string value.
void addInt(int value) {
_integrityCheckOnValueAddition();
_stack.add(_StackValue.withInt(value));
}
/// Adds a bool value.
void addBool(bool value) {
_integrityCheckOnValueAddition();
_stack.add(_StackValue.withBool(value));
}
/// Adds a double value.
void addDouble(double value) {
_integrityCheckOnValueAddition();
_stack.add(_StackValue.withDouble(value));
}
/// Adds a string value.
void addString(String value) {
_integrityCheckOnValueAddition();
if (_stringCache.containsKey(value)) {
_stack.add(_stringCache[value]!);
return;
}
final utf8String = utf8.encode(value);
final length = utf8String.length;
final bitWidth = BitWidthUtil.uwidth(length);
final byteWidth = _align(bitWidth);
_writeUInt(length, byteWidth);
final stringOffset = _offset;
final newOffset = _newOffset(length + 1);
_pushBuffer(utf8String);
_offset = newOffset;
final stackValue =
_StackValue.withOffset(stringOffset, ValueType.String, bitWidth);
_stack.add(stackValue);
_stringCache[value] = stackValue;
}
/// This methods adds a key to a map and should be followed by an add... value call.
///
/// It also implies that you call this method only after you called [startMap].
void addKey(String value) {
_integrityCheckOnKeyAddition();
if (_keyCache.containsKey(value)) {
_stack.add(_keyCache[value]!);
return;
}
final utf8String = utf8.encode(value);
final length = utf8String.length;
final keyOffset = _offset;
final newOffset = _newOffset(length + 1);
_pushBuffer(utf8String);
_offset = newOffset;
final stackValue =
_StackValue.withOffset(keyOffset, ValueType.Key, BitWidth.width8);
_stack.add(stackValue);
_keyCache[value] = stackValue;
}
/// Adds a byte array.
///
/// This method can be used to store any generic BLOB.
void addBlob(ByteBuffer value) {
_integrityCheckOnValueAddition();
final length = value.lengthInBytes;
final bitWidth = BitWidthUtil.uwidth(length);
final byteWidth = _align(bitWidth);
_writeUInt(length, byteWidth);
final blobOffset = _offset;
final newOffset = _newOffset(length);
_pushBuffer(value.asUint8List());
_offset = newOffset;
final stackValue =
_StackValue.withOffset(blobOffset, ValueType.Blob, bitWidth);
_stack.add(stackValue);
}
/// Stores int value indirectly in the buffer.
///
/// Adding large integer values indirectly might be beneficial if those values suppose to be store in a vector together with small integer values.
/// This is due to the fact that FlexBuffers will add padding to small integer values, if they are stored together with large integer values.
/// When we add integer indirectly the vector of ints will contain not the value itself, but only the relative offset to the value.
/// By setting the [cache] parameter to true, you make sure that the builder tracks added int value and performs deduplication.
void addIntIndirectly(int value, {bool cache = false}) {
_integrityCheckOnValueAddition();
if (_indirectIntCache.containsKey(value)) {
_stack.add(_indirectIntCache[value]!);
return;
}
final stackValue = _StackValue.withInt(value);
final byteWidth = _align(stackValue.width);
final newOffset = _newOffset(byteWidth);
final valueOffset = _offset;
_pushBuffer(stackValue.asU8List(stackValue.width));
final stackOffset = _StackValue.withOffset(
valueOffset, ValueType.IndirectInt, stackValue.width);
_stack.add(stackOffset);
_offset = newOffset;
if (cache) {
_indirectIntCache[value] = stackOffset;
}
}
/// Stores double value indirectly in the buffer.
///
/// Double are stored as 8 or 4 byte values in FlexBuffers. If they are stored in a mixed vector, values which are smaller than 4 / 8 bytes will be padded.
/// When we add double indirectly, the vector will contain not the value itself, but only the relative offset to the value. Which could occupy only 1 or 2 bytes, reducing the odds for unnecessary padding.
/// By setting the [cache] parameter to true, you make sure that the builder tracks already added double value and performs deduplication.
void addDoubleIndirectly(double value, {bool cache = false}) {
_integrityCheckOnValueAddition();
if (cache && _indirectDoubleCache.containsKey(value)) {
_stack.add(_indirectDoubleCache[value]!);
return;
}
final stackValue = _StackValue.withDouble(value);
final byteWidth = _align(stackValue.width);
final newOffset = _newOffset(byteWidth);
final valueOffset = _offset;
_pushBuffer(stackValue.asU8List(stackValue.width));
final stackOffset = _StackValue.withOffset(
valueOffset, ValueType.IndirectFloat, stackValue.width);
_stack.add(stackOffset);
_offset = newOffset;
if (cache) {
_indirectDoubleCache[value] = stackOffset;
}
}
/// This method starts a vector definition and needs to be followed by 0 to n add... value calls.
///
/// The vector definition needs to be finished with an [end] call.
/// It is also possible to add nested vector or map by calling [startVector] / [startMap].
void startVector() {
_integrityCheckOnValueAddition();
_stackPointers.add(_StackPointer(_stack.length, true));
}
/// This method starts a map definition.
///
/// This method call needs to be followed by 0 to n [addKey] + add... value calls.
/// The map definition needs to be finished with an [end] call.
/// It is also possible to add nested vector or map by calling [startVector] / [startMap] after calling [addKey].
void startMap() {
_integrityCheckOnValueAddition();
_stackPointers.add(_StackPointer(_stack.length, false));
}
/// Marks that the addition of values to the last vector, or map have ended.
void end() {
final pointer = _stackPointers.removeLast();
if (pointer.isVector) {
_endVector(pointer);
} else {
_sortKeysAndEndMap(pointer);
}
}
/// Finish building the FlatBuffer and return array of bytes.
///
/// Can be called multiple times, to get the array of bytes.
/// After the first call, adding values, or starting vectors / maps will result in an exception.
Uint8List finish() {
if (_finished == false) {
_finish();
}
return _buffer.buffer.asUint8List(0, _offset);
}
/// Builds a FlatBuffer with current state without finishing the builder.
///
/// Creates an internal temporary copy of current builder and finishes the copy.
/// Use this method, when the state of a long lasting builder need to be persisted periodically.
ByteBuffer snapshot() {
final tmp = Builder(size: _offset + 200);
tmp._offset = _offset;
tmp._stack = List.from(_stack);
tmp._stackPointers = List.from(_stackPointers);
tmp._buffer.buffer
.asUint8List()
.setAll(0, _buffer.buffer.asUint8List(0, _offset));
for (var i = 0; i < tmp._stackPointers.length; i++) {
tmp.end();
}
final buffer = tmp.finish();
final bd = ByteData(buffer.lengthInBytes);
bd.buffer.asUint8List().setAll(0, buffer);
return bd.buffer;
}
void _integrityCheckOnValueAddition() {
if (_finished) {
throw StateError('Adding values after finish is prohibited');
}
if (_stackPointers.isNotEmpty && _stackPointers.last.isVector == false) {
if (_stack.last.type != ValueType.Key) {
throw StateError(
'Adding value to a map before adding a key is prohibited');
}
}
}
void _integrityCheckOnKeyAddition() {
if (_finished) {
throw StateError('Adding values after finish is prohibited');
}
if (_stackPointers.isEmpty || _stackPointers.last.isVector) {
throw StateError('Adding key before staring a map is prohibited');
}
}
void _finish() {
if (_stack.length != 1) {
throw StateError(
'Stack has to be exactly 1, but is ${_stack.length}. You have to end all started vectors and maps, before calling [finish]');
}
final value = _stack[0];
final byteWidth = _align(value.elementWidth(_offset, 0));
_writeStackValue(value, byteWidth);
_writeUInt(value.storedPackedType(), 1);
_writeUInt(byteWidth, 1);
_finished = true;
}
_StackValue _createVector(int start, int vecLength, int step,
[_StackValue? keys]) {
var bitWidth = BitWidthUtil.uwidth(vecLength);
var prefixElements = 1;
if (keys != null) {
var elemWidth = keys.elementWidth(_offset, 0);
if (elemWidth.index > bitWidth.index) {
bitWidth = elemWidth;
}
prefixElements += 2;
}
var vectorType = ValueType.Key;
var typed = keys == null;
for (var i = start; i < _stack.length; i += step) {
final elemWidth = _stack[i].elementWidth(_offset, i + prefixElements);
if (elemWidth.index > bitWidth.index) {
bitWidth = elemWidth;
}
if (i == start) {
vectorType = _stack[i].type;
typed &= ValueTypeUtils.isTypedVectorElement(vectorType);
} else {
if (vectorType != _stack[i].type) {
typed = false;
}
}
}
final byteWidth = _align(bitWidth);
final fix = typed & ValueTypeUtils.isNumber(vectorType) &&
vecLength >= 2 &&
vecLength <= 4;
if (keys != null) {
_writeStackValue(keys, byteWidth);
_writeUInt(1 << keys.width.index, byteWidth);
}
if (fix == false) {
_writeUInt(vecLength, byteWidth);
}
final vecOffset = _offset;
for (var i = start; i < _stack.length; i += step) {
_writeStackValue(_stack[i], byteWidth);
}
if (typed == false) {
for (var i = start; i < _stack.length; i += step) {
_writeUInt(_stack[i].storedPackedType(), 1);
}
}
if (keys != null) {
return _StackValue.withOffset(vecOffset, ValueType.Map, bitWidth);
}
if (typed) {
final vType =
ValueTypeUtils.toTypedVector(vectorType, fix ? vecLength : 0);
return _StackValue.withOffset(vecOffset, vType, bitWidth);
}
return _StackValue.withOffset(vecOffset, ValueType.Vector, bitWidth);
}
void _endVector(_StackPointer pointer) {
final vecLength = _stack.length - pointer.stackPosition;
final vec = _createVector(pointer.stackPosition, vecLength, 1);
_stack.removeRange(pointer.stackPosition, _stack.length);
_stack.add(vec);
}
void _sortKeysAndEndMap(_StackPointer pointer) {
if (((_stack.length - pointer.stackPosition) & 1) == 1) {
throw StateError(
'The stack needs to hold key value pairs (even number of elements). Check if you combined [addKey] with add... method calls properly.');
}
var sorted = true;
for (var i = pointer.stackPosition; i < _stack.length - 2; i += 2) {
if (_shouldFlip(_stack[i], _stack[i + 2])) {
sorted = false;
break;
}
}
if (sorted == false) {
for (var i = pointer.stackPosition; i < _stack.length; i += 2) {
var flipIndex = i;
for (var j = i + 2; j < _stack.length; j += 2) {
if (_shouldFlip(_stack[flipIndex], _stack[j])) {
flipIndex = j;
}
}
if (flipIndex != i) {
var k = _stack[flipIndex];
var v = _stack[flipIndex + 1];
_stack[flipIndex] = _stack[i];
_stack[flipIndex + 1] = _stack[i + 1];
_stack[i] = k;
_stack[i + 1] = v;
}
}
}
_endMap(pointer);
}
void _endMap(_StackPointer pointer) {
final vecLength = (_stack.length - pointer.stackPosition) >> 1;
final offsets = <int>[];
for (var i = pointer.stackPosition; i < _stack.length; i += 2) {
offsets.add(_stack[i].offset!);
}
final keysHash = _KeysHash(offsets);
_StackValue? keysStackValue;
if (_keyVectorCache.containsKey(keysHash)) {
keysStackValue = _keyVectorCache[keysHash];
} else {
keysStackValue = _createVector(pointer.stackPosition, vecLength, 2);
_keyVectorCache[keysHash] = keysStackValue;
}
final vec =
_createVector(pointer.stackPosition + 1, vecLength, 2, keysStackValue);
_stack.removeRange(pointer.stackPosition, _stack.length);
_stack.add(vec);
}
bool _shouldFlip(_StackValue v1, _StackValue v2) {
if (v1.type != ValueType.Key || v2.type != ValueType.Key) {
throw StateError(
'Stack values are not keys $v1 | $v2. Check if you combined [addKey] with add... method calls properly.');
}
late int c1, c2;
var index = 0;
do {
c1 = _buffer.getUint8(v1.offset! + index);
c2 = _buffer.getUint8(v2.offset! + index);
if (c2 < c1) return true;
if (c1 < c2) return false;
index += 1;
} while (c1 != 0 && c2 != 0);
return false;
}
int _align(BitWidth width) {
final byteWidth = BitWidthUtil.toByteWidth(width);
_offset += BitWidthUtil.paddingSize(_offset, byteWidth);
return byteWidth;
}
void _writeStackValue(_StackValue value, int byteWidth) {
final newOffset = _newOffset(byteWidth);
if (value.isOffset) {
final relativeOffset = _offset - value.offset!;
if (byteWidth == 8 || relativeOffset < (1 << (byteWidth * 8))) {
_writeUInt(relativeOffset, byteWidth);
} else {
throw StateError(
'Unexpected size $byteWidth. This might be a bug. Please create an issue https://github.com/google/flatbuffers/issues/new');
}
} else {
_pushBuffer(value.asU8List(BitWidthUtil.fromByteWidth(byteWidth)));
}
_offset = newOffset;
}
void _writeUInt(int value, int byteWidth) {
final newOffset = _newOffset(byteWidth);
_pushUInt(value, BitWidthUtil.fromByteWidth(byteWidth));
_offset = newOffset;
}
int _newOffset(int newValueSize) {
final newOffset = _offset + newValueSize;
var size = _buffer.lengthInBytes;
final prevSize = size;
while (size < newOffset) {
size <<= 1;
}
if (prevSize < size) {
final newBuf = ByteData(size);
newBuf.buffer.asUint8List().setAll(0, _buffer.buffer.asUint8List());
}
return newOffset;
}
void _pushInt(int value, BitWidth width) {
switch (width) {
case BitWidth.width8:
_buffer.setInt8(_offset, value);
break;
case BitWidth.width16:
_buffer.setInt16(_offset, value, Endian.little);
break;
case BitWidth.width32:
_buffer.setInt32(_offset, value, Endian.little);
break;
case BitWidth.width64:
_buffer.setInt64(_offset, value, Endian.little);
break;
}
}
void _pushUInt(int value, BitWidth width) {
switch (width) {
case BitWidth.width8:
_buffer.setUint8(_offset, value);
break;
case BitWidth.width16:
_buffer.setUint16(_offset, value, Endian.little);
break;
case BitWidth.width32:
_buffer.setUint32(_offset, value, Endian.little);
break;
case BitWidth.width64:
_buffer.setUint64(_offset, value, Endian.little);
break;
}
}
void _pushBuffer(List<int> value) {
_buffer.buffer.asUint8List().setAll(_offset, value);
}
}
class _StackValue {
late Object _value;
int? _offset;
final ValueType _type;
final BitWidth _width;
_StackValue.withNull()
: _type = ValueType.Null,
_width = BitWidth.width8;
_StackValue.withInt(int value)
: _type = ValueType.Int,
_width = BitWidthUtil.width(value),
_value = value;
_StackValue.withBool(bool value)
: _type = ValueType.Bool,
_width = BitWidth.width8,
_value = value;
_StackValue.withDouble(double value)
: _type = ValueType.Float,
_width = BitWidthUtil.width(value),
_value = value;
_StackValue.withOffset(int value, ValueType type, BitWidth width)
: _offset = value,
_type = type,
_width = width;
BitWidth storedWidth({BitWidth width = BitWidth.width8}) {
return ValueTypeUtils.isInline(_type)
? BitWidthUtil.max(_width, width)
: _width;
}
int storedPackedType({BitWidth width = BitWidth.width8}) {
return ValueTypeUtils.packedType(_type, storedWidth(width: width));
}
BitWidth elementWidth(int size, int index) {
if (ValueTypeUtils.isInline(_type)) return _width;
final offset = _offset!;
for (var i = 0; i < 4; i++) {
final width = 1 << i;
final bitWidth = BitWidthUtil.uwidth(size +
BitWidthUtil.paddingSize(size, width) +
index * width -
offset);
if (1 << bitWidth.index == width) {
return bitWidth;
}
}
throw StateError(
'Element is of unknown. Size: $size at index: $index. This might be a bug. Please create an issue https://github.com/google/flatbuffers/issues/new');
}
List<int> asU8List(BitWidth width) {
if (ValueTypeUtils.isNumber(_type)) {
if (_type == ValueType.Float) {
if (width == BitWidth.width32) {
final result = ByteData(4);
result.setFloat32(0, _value as double, Endian.little);
return result.buffer.asUint8List();
} else {
final result = ByteData(8);
result.setFloat64(0, _value as double, Endian.little);
return result.buffer.asUint8List();
}
} else {
switch (width) {
case BitWidth.width8:
final result = ByteData(1);
result.setInt8(0, _value as int);
return result.buffer.asUint8List();
case BitWidth.width16:
final result = ByteData(2);
result.setInt16(0, _value as int, Endian.little);
return result.buffer.asUint8List();
case BitWidth.width32:
final result = ByteData(4);
result.setInt32(0, _value as int, Endian.little);
return result.buffer.asUint8List();
case BitWidth.width64:
final result = ByteData(8);
result.setInt64(0, _value as int, Endian.little);
return result.buffer.asUint8List();
}
}
}
if (_type == ValueType.Null) {
final result = ByteData(1);
result.setInt8(0, 0);
return result.buffer.asUint8List();
}
if (_type == ValueType.Bool) {
final result = ByteData(1);
result.setInt8(0, _value as bool ? 1 : 0);
return result.buffer.asUint8List();
}
throw StateError(
'Unexpected type: $_type. This might be a bug. Please create an issue https://github.com/google/flatbuffers/issues/new');
}
ValueType get type {
return _type;
}
BitWidth get width {
return _width;
}
bool get isOffset {
return !ValueTypeUtils.isInline(_type);
}
int? get offset => _offset;
bool get isFloat32 {
return _type == ValueType.Float && _width == BitWidth.width32;
}
}
class _StackPointer {
int stackPosition;
bool isVector;
_StackPointer(this.stackPosition, this.isVector);
}
class _KeysHash {
final List<int> keys;
const _KeysHash(this.keys);
@override
bool operator ==(Object other) {
if (other is _KeysHash) {
if (keys.length != other.keys.length) return false;
for (var i = 0; i < keys.length; i++) {
if (keys[i] != other.keys[i]) return false;
}
return true;
}
return false;
}
@override
int get hashCode {
var result = 17;
for (var i = 0; i < keys.length; i++) {
result = result * 23 + keys[i];
}
return result;
}
}

481
dart/lib/src/reference.dart Normal file
View File

@@ -0,0 +1,481 @@
import 'dart:collection';
import 'dart:convert';
import 'dart:typed_data';
import 'types.dart';
/// Main class to read a value out of a FlexBuffer.
///
/// This class let you access values stored in the buffer in a lazy fashion.
class Reference {
final ByteData _buffer;
final int _offset;
final BitWidth _parentWidth;
final String _path;
final int _byteWidth;
final ValueType _valueType;
int? _length;
Reference._(
this._buffer, this._offset, this._parentWidth, int packedType, this._path,
[int? byteWidth, ValueType? valueType])
: _byteWidth = byteWidth ?? 1 << (packedType & 3),
_valueType = valueType ?? ValueTypeUtils.fromInt(packedType >> 2);
/// Use this method to access the root value of a FlexBuffer.
static Reference fromBuffer(ByteBuffer buffer) {
final len = buffer.lengthInBytes;
if (len < 3) {
throw UnsupportedError('Buffer needs to be bigger than 3');
}
final byteData = ByteData.view(buffer);
final byteWidth = byteData.getUint8(len - 1);
final packedType = byteData.getUint8(len - 2);
final offset = len - byteWidth - 2;
return Reference._(ByteData.view(buffer), offset,
BitWidthUtil.fromByteWidth(byteWidth), packedType, "/");
}
/// Returns true if the underlying value is null.
bool get isNull => _valueType == ValueType.Null;
/// Returns true if the underlying value can be represented as [num].
bool get isNum =>
ValueTypeUtils.isNumber(_valueType) ||
ValueTypeUtils.isIndirectNumber(_valueType);
/// Returns true if the underlying value was encoded as a float (direct or indirect).
bool get isDouble =>
_valueType == ValueType.Float || _valueType == ValueType.IndirectFloat;
/// Returns true if the underlying value was encoded as an int or uint (direct or indirect).
bool get isInt => isNum && !isDouble;
/// Returns true if the underlying value was encoded as a string or a key.
bool get isString =>
_valueType == ValueType.String || _valueType == ValueType.Key;
/// Returns true if the underlying value was encoded as a bool.
bool get isBool => _valueType == ValueType.Bool;
/// Returns true if the underlying value was encoded as a blob.
bool get isBlob => _valueType == ValueType.Blob;
/// Returns true if the underlying value points to a vector.
bool get isVector => ValueTypeUtils.isAVector(_valueType);
/// Returns true if the underlying value points to a map.
bool get isMap => _valueType == ValueType.Map;
/// If this [isBool], returns the bool value. Otherwise, returns null.
bool? get boolValue {
if (_valueType == ValueType.Bool) {
return _readInt(_offset, _parentWidth) != 0;
}
return null;
}
/// Returns an [int], if the underlying value can be represented as an int.
///
/// Otherwise returns [null].
int? get intValue {
if (_valueType == ValueType.Int) {
return _readInt(_offset, _parentWidth);
}
if (_valueType == ValueType.UInt) {
return _readUInt(_offset, _parentWidth);
}
if (_valueType == ValueType.IndirectInt) {
return _readInt(_indirect, BitWidthUtil.fromByteWidth(_byteWidth));
}
if (_valueType == ValueType.IndirectUInt) {
return _readUInt(_indirect, BitWidthUtil.fromByteWidth(_byteWidth));
}
return null;
}
/// Returns [double], if the underlying value [isDouble].
///
/// Otherwise returns [null].
double? get doubleValue {
if (_valueType == ValueType.Float) {
return _readFloat(_offset, _parentWidth);
}
if (_valueType == ValueType.IndirectFloat) {
return _readFloat(_indirect, BitWidthUtil.fromByteWidth(_byteWidth));
}
return null;
}
/// Returns [num], if the underlying value is numeric, be it int uint, or float (direct or indirect).
///
/// Otherwise returns [null].
num? get numValue => doubleValue ?? intValue;
/// Returns [String] value or null otherwise.
///
/// This method performers a utf8 decoding, as FlexBuffers format stores strings in utf8 encoding.
String? get stringValue {
if (_valueType == ValueType.String || _valueType == ValueType.Key) {
return utf8.decode(_buffer.buffer.asUint8List(_indirect, length));
}
return null;
}
/// Returns [Uint8List] value or null otherwise.
Uint8List? get blobValue {
if (_valueType == ValueType.Blob) {
return _buffer.buffer.asUint8List(_indirect, length);
}
return null;
}
/// Can be used with an [int] or a [String] value for key.
/// If the underlying value in FlexBuffer is a vector, then use [int] for access.
/// If the underlying value in FlexBuffer is a map, then use [String] for access.
/// Returns [Reference] value. Throws an exception when [key] is not applicable.
Reference operator [](Object key) {
if (key is int && ValueTypeUtils.isAVector(_valueType)) {
final index = key;
if (index >= length || index < 0) {
throw ArgumentError(
'Key: [$key] is not applicable on: $_path of: $_valueType length: $length');
}
final elementOffset = _indirect + index * _byteWidth;
int packedType = 0;
int? byteWidth;
ValueType? valueType;
if (ValueTypeUtils.isTypedVector(_valueType)) {
byteWidth = 1;
valueType = ValueTypeUtils.typedVectorElementType(_valueType);
} else if (ValueTypeUtils.isFixedTypedVector(_valueType)) {
byteWidth = 1;
valueType = ValueTypeUtils.fixedTypedVectorElementType(_valueType);
} else {
packedType = _buffer.getUint8(_indirect + length * _byteWidth + index);
}
return Reference._(
_buffer,
elementOffset,
BitWidthUtil.fromByteWidth(_byteWidth),
packedType,
"$_path[$index]",
byteWidth,
valueType);
}
if (key is String && _valueType == ValueType.Map) {
final index = _keyIndex(key);
if (index != null) {
return _valueForIndexWithKey(index, key);
}
}
throw ArgumentError(
'Key: [$key] is not applicable on: $_path of: $_valueType');
}
/// Get an iterable if the underlying flexBuffer value is a vector.
/// Otherwise throws an exception.
Iterable<Reference> get vectorIterable {
if (isVector == false) {
throw UnsupportedError('Value is not a vector. It is: $_valueType');
}
return _VectorIterator(this);
}
/// Get an iterable for keys if the underlying flexBuffer value is a map.
/// Otherwise throws an exception.
Iterable<String> get mapKeyIterable {
if (isMap == false) {
throw UnsupportedError('Value is not a map. It is: $_valueType');
}
return _MapKeyIterator(this);
}
/// Get an iterable for values if the underlying flexBuffer value is a map.
/// Otherwise throws an exception.
Iterable<Reference> get mapValueIterable {
if (isMap == false) {
throw UnsupportedError('Value is not a map. It is: $_valueType');
}
return _MapValueIterator(this);
}
/// Returns the length of the the underlying FlexBuffer value.
/// If the underlying value is [null] the length is 0.
/// If the underlying value is a number, or a bool, the length is 1.
/// If the underlying value is a vector, or map, the length reflects number of elements / element pairs.
/// If the values is a string or a blob, the length reflects a number of bytes the value occupies (strings are encoded in utf8 format).
int get length {
if (_length == null) {
// needs to be checked before more generic isAVector
if (ValueTypeUtils.isFixedTypedVector(_valueType)) {
_length = ValueTypeUtils.fixedTypedVectorElementSize(_valueType);
} else if (_valueType == ValueType.Blob ||
ValueTypeUtils.isAVector(_valueType) ||
_valueType == ValueType.Map) {
_length = _readUInt(
_indirect - _byteWidth, BitWidthUtil.fromByteWidth(_byteWidth));
} else if (_valueType == ValueType.Null) {
_length = 0;
} else if (_valueType == ValueType.String) {
final indirect = _indirect;
var sizeByteWidth = _byteWidth;
var size = _readUInt(indirect - sizeByteWidth,
BitWidthUtil.fromByteWidth(sizeByteWidth));
while (_buffer.getInt8(indirect + size) != 0) {
sizeByteWidth <<= 1;
size = _readUInt(indirect - sizeByteWidth,
BitWidthUtil.fromByteWidth(sizeByteWidth));
}
_length = size;
} else if (_valueType == ValueType.Key) {
final indirect = _indirect;
var size = 1;
while (_buffer.getInt8(indirect + size) != 0) {
size += 1;
}
_length = size;
} else {
_length = 1;
}
}
return _length!;
}
/// Returns a minified JSON representation of the underlying FlexBuffer value.
///
/// This method involves materializing the entire object tree, which may be
/// expensive. It is more efficient to work with [Reference] and access only the needed data.
/// Blob values are represented as base64 encoded string.
String get json {
if (_valueType == ValueType.Bool) {
return boolValue! ? 'true' : 'false';
}
if (_valueType == ValueType.Null) {
return 'null';
}
if (ValueTypeUtils.isNumber(_valueType)) {
return jsonEncode(numValue);
}
if (_valueType == ValueType.String) {
return jsonEncode(stringValue);
}
if (_valueType == ValueType.Blob) {
return jsonEncode(base64Encode(blobValue!));
}
if (ValueTypeUtils.isAVector(_valueType)) {
final result = StringBuffer();
result.write('[');
for (var i = 0; i < length; i++) {
result.write(this[i].json);
if (i < length - 1) {
result.write(',');
}
}
result.write(']');
return result.toString();
}
if (_valueType == ValueType.Map) {
final result = StringBuffer();
result.write('{');
for (var i = 0; i < length; i++) {
result.write(jsonEncode(_keyForIndex(i)));
result.write(':');
result.write(_valueForIndex(i).json);
if (i < length - 1) {
result.write(',');
}
}
result.write('}');
return result.toString();
}
throw UnsupportedError(
'Type: $_valueType is not supported for JSON conversion');
}
/// Computes the indirect offset of the value.
///
/// To optimize for the more common case of being called only once, this
/// value is not cached. Callers that need to use it more than once should
/// cache the return value in a local variable.
int get _indirect {
final step = _readUInt(_offset, _parentWidth);
return _offset - step;
}
int _readInt(int offset, BitWidth width) {
_validateOffset(offset, width);
if (width == BitWidth.width8) {
return _buffer.getInt8(offset);
}
if (width == BitWidth.width16) {
return _buffer.getInt16(offset, Endian.little);
}
if (width == BitWidth.width32) {
return _buffer.getInt32(offset, Endian.little);
}
return _buffer.getInt64(offset, Endian.little);
}
int _readUInt(int offset, BitWidth width) {
_validateOffset(offset, width);
if (width == BitWidth.width8) {
return _buffer.getUint8(offset);
}
if (width == BitWidth.width16) {
return _buffer.getUint16(offset, Endian.little);
}
if (width == BitWidth.width32) {
return _buffer.getUint32(offset, Endian.little);
}
return _buffer.getUint64(offset, Endian.little);
}
double _readFloat(int offset, BitWidth width) {
_validateOffset(offset, width);
if (width.index < BitWidth.width32.index) {
throw StateError('Bad width: $width');
}
if (width == BitWidth.width32) {
return _buffer.getFloat32(offset, Endian.little);
}
return _buffer.getFloat64(offset, Endian.little);
}
void _validateOffset(int offset, BitWidth width) {
if (_offset < 0 ||
_buffer.lengthInBytes <= offset + width.index ||
offset & (BitWidthUtil.toByteWidth(width) - 1) != 0) {
throw StateError('Bad offset: $offset, width: $width');
}
}
int? _keyIndex(String key) {
final input = utf8.encode(key);
final keysVectorOffset = _indirect - _byteWidth * 3;
final indirectOffset = keysVectorOffset -
_readUInt(keysVectorOffset, BitWidthUtil.fromByteWidth(_byteWidth));
final byteWidth = _readUInt(
keysVectorOffset + _byteWidth, BitWidthUtil.fromByteWidth(_byteWidth));
var low = 0;
var high = length - 1;
while (low <= high) {
final mid = (high + low) >> 1;
final dif = _diffKeys(input, mid, indirectOffset, byteWidth);
if (dif == 0) return mid;
if (dif < 0) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return null;
}
int _diffKeys(List<int> input, int index, int indirectOffset, int byteWidth) {
final keyOffset = indirectOffset + index * byteWidth;
final keyIndirectOffset =
keyOffset - _readUInt(keyOffset, BitWidthUtil.fromByteWidth(byteWidth));
for (var i = 0; i < input.length; i++) {
final dif = input[i] - _buffer.getUint8(keyIndirectOffset + i);
if (dif != 0) {
return dif;
}
}
return (_buffer.getUint8(keyIndirectOffset + input.length) == 0) ? 0 : -1;
}
Reference _valueForIndexWithKey(int index, String key) {
final indirect = _indirect;
final elementOffset = indirect + index * _byteWidth;
final packedType = _buffer.getUint8(indirect + length * _byteWidth + index);
return Reference._(_buffer, elementOffset,
BitWidthUtil.fromByteWidth(_byteWidth), packedType, "$_path/$key");
}
Reference _valueForIndex(int index) {
final indirect = _indirect;
final elementOffset = indirect + index * _byteWidth;
final packedType = _buffer.getUint8(indirect + length * _byteWidth + index);
return Reference._(_buffer, elementOffset,
BitWidthUtil.fromByteWidth(_byteWidth), packedType, "$_path/[$index]");
}
String _keyForIndex(int index) {
final keysVectorOffset = _indirect - _byteWidth * 3;
final indirectOffset = keysVectorOffset -
_readUInt(keysVectorOffset, BitWidthUtil.fromByteWidth(_byteWidth));
final byteWidth = _readUInt(
keysVectorOffset + _byteWidth, BitWidthUtil.fromByteWidth(_byteWidth));
final keyOffset = indirectOffset + index * byteWidth;
final keyIndirectOffset =
keyOffset - _readUInt(keyOffset, BitWidthUtil.fromByteWidth(byteWidth));
var length = 0;
while (_buffer.getUint8(keyIndirectOffset + length) != 0) {
length += 1;
}
return utf8.decode(_buffer.buffer.asUint8List(keyIndirectOffset, length));
}
}
class _VectorIterator
with IterableMixin<Reference>
implements Iterator<Reference> {
final Reference _vector;
int index = -1;
_VectorIterator(this._vector);
@override
Reference get current => _vector[index];
@override
bool moveNext() {
index++;
return index < _vector.length;
}
@override
Iterator<Reference> get iterator => this;
}
class _MapKeyIterator with IterableMixin<String> implements Iterator<String> {
final Reference _map;
int index = -1;
_MapKeyIterator(this._map);
@override
String get current => _map._keyForIndex(index);
@override
bool moveNext() {
index++;
return index < _map.length;
}
@override
Iterator<String> get iterator => this;
}
class _MapValueIterator
with IterableMixin<Reference>
implements Iterator<Reference> {
final Reference _map;
int index = -1;
_MapValueIterator(this._map);
@override
Reference get current => _map._valueForIndex(index);
@override
bool moveNext() {
index++;
return index < _map.length;
}
@override
Iterator<Reference> get iterator => this;
}

190
dart/lib/src/types.dart Normal file
View File

@@ -0,0 +1,190 @@
import 'dart:typed_data';
/// Represents the number of bits a value occupies.
enum BitWidth { width8, width16, width32, width64 }
class BitWidthUtil {
static int toByteWidth(BitWidth self) {
return 1 << self.index;
}
static BitWidth width(num value) {
if (value.toInt() == value) {
var v = value.toInt().abs();
if (v >> 7 == 0) return BitWidth.width8;
if (v >> 15 == 0) return BitWidth.width16;
if (v >> 31 == 0) return BitWidth.width32;
return BitWidth.width64;
}
return value == _toF32(value as double)
? BitWidth.width32
: BitWidth.width64;
}
static BitWidth uwidth(num value) {
if (value.toInt() == value) {
var v = value.toInt().abs();
if (v >> 8 == 0) return BitWidth.width8;
if (v >> 16 == 0) return BitWidth.width16;
if (v >> 32 == 0) return BitWidth.width32;
return BitWidth.width64;
}
return value == _toF32(value as double)
? BitWidth.width32
: BitWidth.width64;
}
static BitWidth fromByteWidth(int value) {
if (value == 1) {
return BitWidth.width8;
}
if (value == 2) {
return BitWidth.width16;
}
if (value == 4) {
return BitWidth.width32;
}
if (value == 8) {
return BitWidth.width64;
}
throw Exception('Unexpected value $value');
}
static int paddingSize(int bufSize, int scalarSize) {
return (~bufSize + 1) & (scalarSize - 1);
}
static double _toF32(double value) {
var bdata = ByteData(4);
bdata.setFloat32(0, value);
return bdata.getFloat32(0);
}
static BitWidth max(BitWidth self, BitWidth other) {
if (self.index < other.index) {
return other;
}
return self;
}
}
/// Represents all internal FlexBuffer types.
enum ValueType {
Null,
Int,
UInt,
Float,
Key,
String,
IndirectInt,
IndirectUInt,
IndirectFloat,
Map,
Vector,
VectorInt,
VectorUInt,
VectorFloat,
VectorKey,
@Deprecated(
'VectorString is deprecated due to a flaw in the binary format (https://github.com/google/flatbuffers/issues/5627)')
VectorString,
VectorInt2,
VectorUInt2,
VectorFloat2,
VectorInt3,
VectorUInt3,
VectorFloat3,
VectorInt4,
VectorUInt4,
VectorFloat4,
Blob,
Bool,
VectorBool
}
class ValueTypeUtils {
static int toInt(ValueType self) {
if (self == ValueType.VectorBool) return 36;
return self.index;
}
static ValueType fromInt(int value) {
if (value == 36) return ValueType.VectorBool;
return ValueType.values[value];
}
static bool isInline(ValueType self) {
return self == ValueType.Bool || toInt(self) <= toInt(ValueType.Float);
}
static bool isNumber(ValueType self) {
return toInt(self) >= toInt(ValueType.Int) &&
toInt(self) <= toInt(ValueType.Float);
}
static bool isIndirectNumber(ValueType self) {
return toInt(self) >= toInt(ValueType.IndirectInt) &&
toInt(self) <= toInt(ValueType.IndirectFloat);
}
static bool isTypedVectorElement(ValueType self) {
return self == ValueType.Bool ||
(toInt(self) >= toInt(ValueType.Int) &&
toInt(self) <= toInt(ValueType.String));
}
static bool isTypedVector(ValueType self) {
return self == ValueType.VectorBool ||
(toInt(self) >= toInt(ValueType.VectorInt) &&
toInt(self) <= toInt(ValueType.VectorString));
}
static bool isFixedTypedVector(ValueType self) {
return (toInt(self) >= toInt(ValueType.VectorInt2) &&
toInt(self) <= toInt(ValueType.VectorFloat4));
}
static bool isAVector(ValueType self) {
return (isTypedVector(self) ||
isFixedTypedVector(self) ||
self == ValueType.Vector);
}
static ValueType toTypedVector(ValueType self, int length) {
if (length == 0) {
return ValueTypeUtils.fromInt(
toInt(self) - toInt(ValueType.Int) + toInt(ValueType.VectorInt));
}
if (length == 2) {
return ValueTypeUtils.fromInt(
toInt(self) - toInt(ValueType.Int) + toInt(ValueType.VectorInt2));
}
if (length == 3) {
return ValueTypeUtils.fromInt(
toInt(self) - toInt(ValueType.Int) + toInt(ValueType.VectorInt3));
}
if (length == 4) {
return ValueTypeUtils.fromInt(
toInt(self) - toInt(ValueType.Int) + toInt(ValueType.VectorInt4));
}
throw Exception('unexpected length ' + length.toString());
}
static ValueType typedVectorElementType(ValueType self) {
return ValueTypeUtils.fromInt(
toInt(self) - toInt(ValueType.VectorInt) + toInt(ValueType.Int));
}
static ValueType fixedTypedVectorElementType(ValueType self) {
return ValueTypeUtils.fromInt(
(toInt(self) - toInt(ValueType.VectorInt2)) % 3 + toInt(ValueType.Int));
}
static int fixedTypedVectorElementSize(ValueType self) {
return (toInt(self) - toInt(ValueType.VectorInt2)) ~/ 3 + 2;
}
static int packedType(ValueType self, BitWidth bitWidth) {
return bitWidth.index | (toInt(self) << 2);
}
}

View File

@@ -17,12 +17,20 @@
# Note to pub consumers: this file is used to assist with publishing the
# pub package from the flatbuffers repository and is not meant for general use.
# As pub does not currently provide a way to exclude files, it is included here.
set -e
command -v pub >/dev/null 2>&1 || { echo >&2 "Require `pub` but it's not installed. Aborting."; exit 1; }
command -v dart >/dev/null 2>&1 || { echo >&2 "Require `dart` but it's not installed. Aborting."; exit 1; }
cp ../samples/monster.fbs example/
cp ../tests/monster_test.fbs test/
pub publish
pushd ../tests
./DartTest.sh
popd
pushd ../samples
./dart_sample.sh
popd
dart pub publish
rm example/monster.fbs
rm test/monster_test.fbs
rm test/*.fbs
rm -rf test/sub

View File

@@ -1,20 +1,15 @@
name: flat_buffers
version: 1.12.0
description: >
FlatBuffers reading and writing library for Dart. Use the flatc compiler to
generate Dart classes for a FlatBuffers schema, and this library to assist with
reading and writing the binary format.
Based on original work by Konstantin Scheglov and Paul Berry of the Dart SDK team.
authors:
- Dan Field <dfield@gmail.com>
- Konstantin Scheglov
- Paul Berry
version: 2.0.0
description: FlatBuffers reading and writing library for Dart. Based on original work by Konstantin Scheglov and Paul Berry of the Dart SDK team.
homepage: https://github.com/google/flatbuffers
documentation: https://google.github.io/flatbuffers/index.html
dev_dependencies:
test: ^1.3.0
test_reflective_loader: ^0.1.4
path: ^1.5.1
environment:
sdk: '>=2.0.0-dev.28.0 <3.0.0'
sdk: '>=2.12.0 <3.0.0'
dev_dependencies:
test: ^1.17.7
test_reflective_loader: ^0.2.0
path: ^1.8.0
lints: ^1.0.1

View File

@@ -12,11 +12,14 @@ import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import './monster_test_my_game.example_generated.dart' as example;
import './monster_test_my_game.example2_generated.dart' as example2;
main() {
defineReflectiveSuite(() {
defineReflectiveTests(BuilderTest);
defineReflectiveTests(ObjectAPITest);
defineReflectiveTests(CheckOtherLangaugesData);
defineReflectiveTests(GeneratorTest);
});
}
@@ -27,50 +30,137 @@ int indexToField(int index) {
@reflectiveTest
class CheckOtherLangaugesData {
test_cppData() async {
List<int> data = await new io.File(path.join(
path.dirname(io.Platform.script.path),
List<int> data = await io.File(path.join(
path.context.current,
'test',
'monsterdata_test.mon',
))
.readAsBytes();
example.Monster mon = new example.Monster(data);
)).readAsBytes();
example.Monster mon = example.Monster(data);
expect(mon.hp, 80);
expect(mon.mana, 150);
expect(mon.name, 'MyMonster');
expect(mon.pos.x, 1.0);
expect(mon.pos.y, 2.0);
expect(mon.pos.z, 3.0);
expect(mon.pos.test1, 3.0);
expect(mon.pos.test2.value, 2.0);
expect(mon.pos.test3.a, 5);
expect(mon.pos.test3.b, 6);
expect(mon.testType.value, example.AnyTypeId.Monster.value);
expect(mon.pos!.x, 1.0);
expect(mon.pos!.y, 2.0);
expect(mon.pos!.z, 3.0);
expect(mon.pos!.test1, 3.0);
expect(mon.pos!.test2.value, 2.0);
expect(mon.pos!.test3.a, 5);
expect(mon.pos!.test3.b, 6);
expect(mon.testType!.value, example.AnyTypeId.Monster.value);
expect(mon.test is example.Monster, true);
final monster2 = mon.test as example.Monster;
expect(monster2.name, "Fred");
expect(mon.inventory.length, 5);
expect(mon.inventory.reduce((cur, next) => cur + next), 10);
expect(mon.test4.length, 2);
expect(
mon.test4[0].a + mon.test4[0].b + mon.test4[1].a + mon.test4[1].b, 100);
expect(mon.testarrayofstring.length, 2);
expect(mon.testarrayofstring[0], "test1");
expect(mon.testarrayofstring[1], "test2");
expect(mon.inventory!.length, 5);
expect(mon.inventory!.reduce((cur, next) => cur + next), 10);
final test4 = mon.test4!;
expect(test4.length, 2);
expect(test4[0].a + test4[0].b + test4[1].a + test4[1].b, 100);
expect(mon.testarrayofstring!.length, 2);
expect(mon.testarrayofstring![0], "test1");
expect(mon.testarrayofstring![1], "test2");
// this will fail if accessing any field fails.
expect(mon.toString(),
'Monster{pos: Vec3{x: 1.0, y: 2.0, z: 3.0, test1: 3.0, test2: Color{value: 2}, test3: Test{a: 5, b: 6}}, mana: 150, hp: 80, name: MyMonster, inventory: [0, 1, 2, 3, 4], color: Color{value: 8}, testType: AnyTypeId{value: 1}, test: Monster{pos: null, mana: 150, hp: 100, name: Fred, inventory: null, color: Color{value: 8}, testType: AnyTypeId{value: 0}, test: null, test4: null, testarrayofstring: null, testarrayoftables: null, enemy: null, testnestedflatbuffer: null, testempty: null, testbool: false, testhashs32Fnv1: 0, testhashu32Fnv1: 0, testhashs64Fnv1: 0, testhashu64Fnv1: 0, testhashs32Fnv1a: 0, testhashu32Fnv1a: 0, testhashs64Fnv1a: 0, testhashu64Fnv1a: 0, testarrayofbools: null, testf: 3.14159, testf2: 3.0, testf3: 0.0, testarrayofstring2: null, testarrayofsortedstruct: null, flex: null, test5: null, vectorOfLongs: null, vectorOfDoubles: null, parentNamespaceTest: null, vectorOfReferrables: null, singleWeakReference: 0, vectorOfWeakReferences: null, vectorOfStrongReferrables: null, coOwningReference: 0, vectorOfCoOwningReferences: null, nonOwningReference: 0, vectorOfNonOwningReferences: null}, test4: [Test{a: 10, b: 20}, Test{a: 30, b: 40}], testarrayofstring: [test1, test2], testarrayoftables: null, enemy: Monster{pos: null, mana: 150, hp: 100, name: Fred, inventory: null, color: Color{value: 8}, testType: AnyTypeId{value: 0}, test: null, test4: null, testarrayofstring: null, testarrayoftables: null, enemy: null, testnestedflatbuffer: null, testempty: null, testbool: false, testhashs32Fnv1: 0, testhashu32Fnv1: 0, testhashs64Fnv1: 0, testhashu64Fnv1: 0, testhashs32Fnv1a: 0, testhashu32Fnv1a: 0, testhashs64Fnv1a: 0, testhashu64Fnv1a: 0, testarrayofbools: null, testf: 3.14159, testf2: 3.0, testf3: 0.0, testarrayofstring2: null, testarrayofsortedstruct: null, flex: null, test5: null, vectorOfLongs: null, vectorOfDoubles: null, parentNamespaceTest: null, vectorOfReferrables: null, singleWeakReference: 0, vectorOfWeakReferences: null, vectorOfStrongReferrables: null, coOwningReference: 0, vectorOfCoOwningReferences: null, nonOwningReference: 0, vectorOfNonOwningReferences: null}, testnestedflatbuffer: null, testempty: null, testbool: false, testhashs32Fnv1: -579221183, testhashu32Fnv1: 3715746113, testhashs64Fnv1: 7930699090847568257, testhashu64Fnv1: 7930699090847568257, testhashs32Fnv1a: -1904106383, testhashu32Fnv1a: 2390860913, testhashs64Fnv1a: 4898026182817603057, testhashu64Fnv1a: 4898026182817603057, testarrayofbools: [true, false, true], testf: 3.14159, testf2: 3.0, testf3: 0.0, testarrayofstring2: null, testarrayofsortedstruct: null, flex: null, test5: [Test{a: 10, b: 20}, Test{a: 30, b: 40}], vectorOfLongs: [1, 100, 10000, 1000000, 100000000], vectorOfDoubles: [-1.7976931348623157e+308, 0.0, 1.7976931348623157e+308], parentNamespaceTest: null, vectorOfReferrables: null, singleWeakReference: 0, vectorOfWeakReferences: null, vectorOfStrongReferrables: null, coOwningReference: 0, vectorOfCoOwningReferences: null, nonOwningReference: 0, vectorOfNonOwningReferences: null}');
expect(
mon.toString(),
'Monster{'
'pos: Vec3{x: 1.0, y: 2.0, z: 3.0, test1: 3.0, test2: Color{value: 2}, test3: Test{a: 5, b: 6}}, '
'mana: 150, hp: 80, name: MyMonster, inventory: [0, 1, 2, 3, 4], '
'color: Color{value: 8}, testType: AnyTypeId{value: 1}, '
'test: Monster{pos: null, mana: 150, hp: 100, name: Fred, '
'inventory: null, color: Color{value: 8}, testType: null, '
'test: null, test4: null, testarrayofstring: null, '
'testarrayoftables: null, enemy: null, testnestedflatbuffer: null, '
'testempty: null, testbool: false, testhashs32Fnv1: 0, '
'testhashu32Fnv1: 0, testhashs64Fnv1: 0, testhashu64Fnv1: 0, '
'testhashs32Fnv1a: 0, testhashu32Fnv1a: 0, testhashs64Fnv1a: 0, '
'testhashu64Fnv1a: 0, testarrayofbools: null, testf: 3.14159, '
'testf2: 3.0, testf3: 0.0, testarrayofstring2: null, '
'testarrayofsortedstruct: null, flex: null, test5: null, '
'vectorOfLongs: null, vectorOfDoubles: null, parentNamespaceTest: null, '
'vectorOfReferrables: null, singleWeakReference: 0, '
'vectorOfWeakReferences: null, vectorOfStrongReferrables: null, '
'coOwningReference: 0, vectorOfCoOwningReferences: null, '
'nonOwningReference: 0, vectorOfNonOwningReferences: null, '
'anyUniqueType: null, anyUnique: null, anyAmbiguousType: null, '
'anyAmbiguous: null, vectorOfEnums: null, signedEnum: Race{value: -1}, '
'testrequirednestedflatbuffer: null, scalarKeySortedTables: null}, '
'test4: [Test{a: 10, b: 20}, Test{a: 30, b: 40}], '
'testarrayofstring: [test1, test2], testarrayoftables: null, '
'enemy: Monster{pos: null, mana: 150, hp: 100, name: Fred, '
'inventory: null, color: Color{value: 8}, testType: null, '
'test: null, test4: null, testarrayofstring: null, '
'testarrayoftables: null, enemy: null, testnestedflatbuffer: null, '
'testempty: null, testbool: false, testhashs32Fnv1: 0, '
'testhashu32Fnv1: 0, testhashs64Fnv1: 0, testhashu64Fnv1: 0, '
'testhashs32Fnv1a: 0, testhashu32Fnv1a: 0, testhashs64Fnv1a: 0, '
'testhashu64Fnv1a: 0, testarrayofbools: null, testf: 3.14159, '
'testf2: 3.0, testf3: 0.0, testarrayofstring2: null, '
'testarrayofsortedstruct: null, flex: null, test5: null, '
'vectorOfLongs: null, vectorOfDoubles: null, parentNamespaceTest: null, '
'vectorOfReferrables: null, singleWeakReference: 0, '
'vectorOfWeakReferences: null, vectorOfStrongReferrables: null, '
'coOwningReference: 0, vectorOfCoOwningReferences: null, '
'nonOwningReference: 0, vectorOfNonOwningReferences: null, '
'anyUniqueType: null, anyUnique: null, anyAmbiguousType: null, '
'anyAmbiguous: null, vectorOfEnums: null, signedEnum: Race{value: -1}, '
'testrequirednestedflatbuffer: null, scalarKeySortedTables: null}, '
'testnestedflatbuffer: null, testempty: null, testbool: true, '
'testhashs32Fnv1: -579221183, testhashu32Fnv1: 3715746113, '
'testhashs64Fnv1: 7930699090847568257, '
'testhashu64Fnv1: 7930699090847568257, '
'testhashs32Fnv1a: -1904106383, testhashu32Fnv1a: 2390860913, '
'testhashs64Fnv1a: 4898026182817603057, '
'testhashu64Fnv1a: 4898026182817603057, '
'testarrayofbools: [true, false, true], testf: 3.14159, testf2: 3.0, '
'testf3: 0.0, testarrayofstring2: null, testarrayofsortedstruct: ['
'Ability{id: 0, distance: 45}, Ability{id: 1, distance: 21}, '
'Ability{id: 5, distance: 12}], '
'flex: null, test5: [Test{a: 10, b: 20}, Test{a: 30, b: 40}], '
'vectorOfLongs: [1, 100, 10000, 1000000, 100000000], '
'vectorOfDoubles: [-1.7976931348623157e+308, 0.0, 1.7976931348623157e+308], '
'parentNamespaceTest: null, vectorOfReferrables: null, '
'singleWeakReference: 0, vectorOfWeakReferences: null, '
'vectorOfStrongReferrables: null, coOwningReference: 0, '
'vectorOfCoOwningReferences: null, nonOwningReference: 0, '
'vectorOfNonOwningReferences: null, '
'anyUniqueType: null, anyUnique: null, '
'anyAmbiguousType: null, '
'anyAmbiguous: null, vectorOfEnums: null, signedEnum: Race{value: -1}, '
'testrequirednestedflatbuffer: null, scalarKeySortedTables: [Stat{id: '
'miss, val: 0, count: 0}, Stat{id: hit, val: 10, count: 1}]}',
);
}
}
/// Test a custom, fixed-memory allocator (no actual allocations performed)
class CustomAllocator extends Allocator {
final _memory = ByteData(10 * 1024);
int _used = 0;
Uint8List buffer(int size) => _memory.buffer.asUint8List(_used - size, size);
@override
ByteData allocate(int size) {
if (size > _memory.lengthInBytes) {
throw UnsupportedError('Trying to allocate too much');
}
_used = size;
return ByteData.sublistView(_memory, 0, size);
}
@override
void deallocate(ByteData _) {}
}
@reflectiveTest
class BuilderTest {
void test_monsterBuilder() {
final fbBuilder = new Builder();
void test_monsterBuilder([Builder? builder]) {
final fbBuilder = builder ?? Builder();
final str = fbBuilder.writeString('MyMonster');
fbBuilder.writeString('test1');
fbBuilder.writeString('test2');
fbBuilder.writeString('test2', asciiOptimization: true);
final testArrayOfString = fbBuilder.endStructVector(2);
final fred = fbBuilder.writeString('Fred');
@@ -78,21 +168,20 @@ class BuilderTest {
final List<int> treasure = [0, 1, 2, 3, 4];
final inventory = fbBuilder.writeListUint8(treasure);
final monBuilder = new example.MonsterBuilder(fbBuilder)
final monBuilder = example.MonsterBuilder(fbBuilder)
..begin()
..addNameOffset(fred);
final mon2 = monBuilder.finish();
final testBuilder = new example.TestBuilder(fbBuilder);
final testBuilder = example.TestBuilder(fbBuilder);
testBuilder.finish(10, 20);
testBuilder.finish(30, 40);
final test4 = fbBuilder.endStructVector(2);
monBuilder
..begin()
..addPos(
new example.Vec3Builder(fbBuilder).finish(
example.Vec3Builder(fbBuilder).finish(
1.0,
2.0,
3.0,
@@ -112,50 +201,51 @@ class BuilderTest {
fbBuilder.finish(mon);
}
void test_error_addInt32_withoutStartTable() {
Builder builder = new Builder();
void test_error_addInt32_withoutStartTable([Builder? builder]) {
builder ??= Builder();
expect(() {
builder.addInt32(0, 0);
}, throwsStateError);
builder!.addInt32(0, 0);
}, throwsA(isA<AssertionError>()));
}
void test_error_addOffset_withoutStartTable() {
Builder builder = new Builder();
Builder builder = Builder();
expect(() {
builder.addOffset(0, 0);
}, throwsStateError);
}, throwsA(isA<AssertionError>()));
}
void test_error_endTable_withoutStartTable() {
Builder builder = new Builder();
Builder builder = Builder();
expect(() {
builder.endTable();
}, throwsStateError);
}, throwsA(isA<AssertionError>()));
}
void test_error_startTable_duringTable() {
Builder builder = new Builder();
builder.startTable();
Builder builder = Builder();
builder.startTable(0);
expect(() {
builder.startTable();
}, throwsStateError);
builder.startTable(0);
}, throwsA(isA<AssertionError>()));
}
void test_error_writeString_duringTable() {
Builder builder = new Builder();
builder.startTable();
Builder builder = Builder();
builder.startTable(1);
expect(() {
builder.writeString('12345');
}, throwsStateError);
}, throwsA(isA<AssertionError>()));
}
void test_file_identifier() {
Uint8List byteList;
{
Builder builder = new Builder(initialSize: 0);
builder.startTable();
Builder builder = Builder(initialSize: 0);
builder.startTable(0);
int offset = builder.endTable();
byteList = builder.finish(offset, 'Az~ÿ');
builder.finish(offset, 'Az~ÿ');
byteList = builder.buffer;
}
// Convert byteList to a ByteData so that we can read data from it.
ByteData byteData = byteList.buffer.asByteData(byteList.offsetInBytes);
@@ -167,8 +257,8 @@ class BuilderTest {
expect(byteData.getUint8(6), 126); // '~'
expect(byteData.getUint8(7), 255); // 'ÿ'
// First 4 bytes of the table data are a backwards offset to the vtable.
int vTableLoc = tableDataLoc -
byteData.getInt32(tableDataLoc, Endian.little);
int vTableLoc =
tableDataLoc - byteData.getInt32(tableDataLoc, Endian.little);
// First 2 bytes of the vtable are the size of the vtable in bytes, which
// should be 4.
expect(byteData.getUint16(vTableLoc, Endian.little), 4);
@@ -178,31 +268,46 @@ class BuilderTest {
}
void test_low() {
Builder builder = new Builder(initialSize: 0);
expect((builder..putUint8(1)).lowFinish(), [1]);
expect((builder..putUint32(2)).lowFinish(), [2, 0, 0, 0, 0, 0, 0, 1]);
expect((builder..putUint8(3)).lowFinish(),
[0, 0, 0, 3, 2, 0, 0, 0, 0, 0, 0, 1]);
expect((builder..putUint8(4)).lowFinish(),
[0, 0, 4, 3, 2, 0, 0, 0, 0, 0, 0, 1]);
expect((builder..putUint8(5)).lowFinish(),
[0, 5, 4, 3, 2, 0, 0, 0, 0, 0, 0, 1]);
expect((builder..putUint32(6)).lowFinish(),
final allocator = CustomAllocator();
final builder = Builder(initialSize: 0, allocator: allocator);
builder.putUint8(1);
expect(allocator.buffer(builder.size()), [1]);
builder.putUint32(2);
expect(allocator.buffer(builder.size()), [2, 0, 0, 0, 0, 0, 0, 1]);
builder.putUint8(3);
expect(
allocator.buffer(builder.size()), [0, 0, 0, 3, 2, 0, 0, 0, 0, 0, 0, 1]);
builder.putUint8(4);
expect(
allocator.buffer(builder.size()), [0, 0, 4, 3, 2, 0, 0, 0, 0, 0, 0, 1]);
builder.putUint8(5);
expect(
allocator.buffer(builder.size()), [0, 5, 4, 3, 2, 0, 0, 0, 0, 0, 0, 1]);
builder.putUint32(6);
expect(allocator.buffer(builder.size()),
[6, 0, 0, 0, 0, 5, 4, 3, 2, 0, 0, 0, 0, 0, 0, 1]);
}
void test_table_default() {
List<int> byteList;
{
Builder builder = new Builder(initialSize: 0);
builder.startTable();
final builder = Builder(initialSize: 0, allocator: CustomAllocator());
builder.startTable(2);
builder.addInt32(0, 10, 10);
builder.addInt32(1, 20, 10);
int offset = builder.endTable();
byteList = builder.finish(offset);
builder.finish(offset);
byteList = builder.buffer;
expect(builder.size(), byteList.length);
}
// read and verify
BufferContext buffer = new BufferContext.fromBytes(byteList);
BufferContext buffer = BufferContext.fromBytes(byteList);
int objectOffset = buffer.derefObject(0);
// was not written, so uses the new default value
expect(
@@ -216,23 +321,24 @@ class BuilderTest {
20);
}
void test_table_format() {
void test_table_format([Builder? builder]) {
Uint8List byteList;
{
Builder builder = new Builder(initialSize: 0);
builder.startTable();
builder ??= Builder(initialSize: 0);
builder.startTable(3);
builder.addInt32(0, 10);
builder.addInt32(1, 20);
builder.addInt32(2, 30);
byteList = builder.finish(builder.endTable());
builder.finish(builder.endTable());
byteList = builder.buffer;
}
// Convert byteList to a ByteData so that we can read data from it.
ByteData byteData = byteList.buffer.asByteData(byteList.offsetInBytes);
// First 4 bytes are an offset to the table data.
int tableDataLoc = byteData.getUint32(0, Endian.little);
// First 4 bytes of the table data are a backwards offset to the vtable.
int vTableLoc = tableDataLoc -
byteData.getInt32(tableDataLoc, Endian.little);
int vTableLoc =
tableDataLoc - byteData.getInt32(tableDataLoc, Endian.little);
// First 2 bytes of the vtable are the size of the vtable in bytes, which
// should be 10.
expect(byteData.getUint16(vTableLoc, Endian.little), 10);
@@ -242,10 +348,9 @@ class BuilderTest {
// Remaining 6 bytes are the offsets within the object where the ints are
// located.
for (int i = 0; i < 3; i++) {
int offset =
byteData.getUint16(vTableLoc + 4 + 2 * i, Endian.little);
expect(byteData.getInt32(tableDataLoc + offset, Endian.little),
10 + 10 * i);
int offset = byteData.getUint16(vTableLoc + 4 + 2 * i, Endian.little);
expect(
byteData.getInt32(tableDataLoc + offset, Endian.little), 10 + 10 * i);
}
}
@@ -254,30 +359,37 @@ class BuilderTest {
String unicodeString = 'Проба пера';
List<int> byteList;
{
Builder builder = new Builder(initialSize: 0);
int latinStringOffset = builder.writeString(latinString);
int unicodeStringOffset = builder.writeString(unicodeString);
builder.startTable();
Builder builder = Builder(initialSize: 0);
int? latinStringOffset =
builder.writeString(latinString, asciiOptimization: true);
int? unicodeStringOffset =
builder.writeString(unicodeString, asciiOptimization: true);
builder.startTable(2);
builder.addOffset(0, latinStringOffset);
builder.addOffset(1, unicodeStringOffset);
int offset = builder.endTable();
byteList = builder.finish(offset);
builder.finish(offset);
byteList = builder.buffer;
}
// read and verify
BufferContext buf = new BufferContext.fromBytes(byteList);
BufferContext buf = BufferContext.fromBytes(byteList);
int objectOffset = buf.derefObject(0);
expect(const StringReader().vTableGet(buf, objectOffset, indexToField(0)),
expect(
const StringReader()
.vTableGetNullable(buf, objectOffset, indexToField(0)),
latinString);
expect(const StringReader().vTableGet(buf, objectOffset, indexToField(1)),
expect(
const StringReader(asciiOptimization: true)
.vTableGetNullable(buf, objectOffset, indexToField(1)),
unicodeString);
}
void test_table_types() {
void test_table_types([Builder? builder]) {
List<int> byteList;
{
Builder builder = new Builder(initialSize: 0);
int stringOffset = builder.writeString('12345');
builder.startTable();
builder ??= Builder(initialSize: 0);
int? stringOffset = builder.writeString('12345');
builder.startTable(7);
builder.addBool(0, true);
builder.addInt8(1, 10);
builder.addInt32(2, 20);
@@ -286,24 +398,39 @@ class BuilderTest {
builder.addUint32(5, 0x9ABCDEF0);
builder.addUint8(6, 0x9A);
int offset = builder.endTable();
byteList = builder.finish(offset);
builder.finish(offset);
byteList = builder.buffer;
}
// read and verify
BufferContext buf = new BufferContext.fromBytes(byteList);
BufferContext buf = BufferContext.fromBytes(byteList);
int objectOffset = buf.derefObject(0);
expect(
const BoolReader().vTableGet(buf, objectOffset, indexToField(0)), true);
const BoolReader()
.vTableGetNullable(buf, objectOffset, indexToField(0)),
true);
expect(
const Int8Reader().vTableGet(buf, objectOffset, indexToField(1)), 10);
const Int8Reader()
.vTableGetNullable(buf, objectOffset, indexToField(1)),
10);
expect(
const Int32Reader().vTableGet(buf, objectOffset, indexToField(2)), 20);
expect(const StringReader().vTableGet(buf, objectOffset, indexToField(3)),
const Int32Reader()
.vTableGetNullable(buf, objectOffset, indexToField(2)),
20);
expect(
const StringReader()
.vTableGetNullable(buf, objectOffset, indexToField(3)),
'12345');
expect(
const Int32Reader().vTableGet(buf, objectOffset, indexToField(4)), 40);
expect(const Uint32Reader().vTableGet(buf, objectOffset, indexToField(5)),
const Int32Reader()
.vTableGetNullable(buf, objectOffset, indexToField(4)),
40);
expect(
const Uint32Reader()
.vTableGetNullable(buf, objectOffset, indexToField(5)),
0x9ABCDEF0);
expect(const Uint8Reader().vTableGet(buf, objectOffset, indexToField(6)),
expect(
const Uint8Reader()
.vTableGetNullable(buf, objectOffset, indexToField(6)),
0x9A);
}
@@ -312,12 +439,13 @@ class BuilderTest {
// write
List<int> byteList;
{
Builder builder = new Builder(initialSize: 0);
Builder builder = Builder(initialSize: 0);
int offset = builder.writeListUint32(values);
byteList = builder.finish(offset);
builder.finish(offset);
byteList = builder.buffer;
}
// read and verify
BufferContext buf = new BufferContext.fromBytes(byteList);
BufferContext buf = BufferContext.fromBytes(byteList);
List<int> items = const Uint32ListReader().read(buf, 0);
expect(items, hasLength(4));
expect(items, orderedEquals(values));
@@ -328,16 +456,17 @@ class BuilderTest {
// write
List<int> byteList;
{
Builder builder = new Builder(initialSize: 0);
List<bool> values = new List<bool>.filled(len, false);
Builder builder = Builder(initialSize: 0);
List<bool> values = List<bool>.filled(len, false);
for (int bit in trueBits) {
values[bit] = true;
}
int offset = builder.writeListBool(values);
byteList = builder.finish(offset);
builder.finish(offset);
byteList = builder.buffer;
}
// read and verify
BufferContext buf = new BufferContext.fromBytes(byteList);
BufferContext buf = BufferContext.fromBytes(byteList);
List<bool> items = const BoolListReader().read(buf, 0);
expect(items, hasLength(len));
for (int i = 0; i < items.length; i++) {
@@ -356,25 +485,26 @@ class BuilderTest {
verifyListBooleans(33, <int>[1, 2, 24, 25, 31, 32]);
verifyListBooleans(63, <int>[]);
verifyListBooleans(63, <int>[0, 1, 2, 61, 62]);
verifyListBooleans(63, new List<int>.generate(63, (i) => i));
verifyListBooleans(63, List<int>.generate(63, (i) => i));
verifyListBooleans(64, <int>[]);
verifyListBooleans(64, <int>[0, 1, 2, 61, 62, 63]);
verifyListBooleans(64, <int>[1, 2, 62]);
verifyListBooleans(64, <int>[0, 1, 2, 63]);
verifyListBooleans(64, new List<int>.generate(64, (i) => i));
verifyListBooleans(64, List<int>.generate(64, (i) => i));
verifyListBooleans(100, <int>[0, 3, 30, 60, 90, 99]);
}
void test_writeList_ofInt32() {
List<int> byteList;
{
Builder builder = new Builder(initialSize: 0);
Builder builder = Builder(initialSize: 0);
int offset = builder.writeListInt32(<int>[1, 2, 3, 4, 5]);
byteList = builder.finish(offset);
builder.finish(offset);
byteList = builder.buffer;
}
// read and verify
BufferContext buf = new BufferContext.fromBytes(byteList);
List<int> items = const ListReader<int>(const Int32Reader()).read(buf, 0);
BufferContext buf = BufferContext.fromBytes(byteList);
List<int> items = const ListReader<int>(Int32Reader()).read(buf, 0);
expect(items, hasLength(5));
expect(items, orderedEquals(<int>[1, 2, 3, 4, 5]));
}
@@ -384,13 +514,14 @@ class BuilderTest {
// write
List<int> byteList;
{
Builder builder = new Builder(initialSize: 0);
Builder builder = Builder(initialSize: 0);
int offset = builder.writeListFloat64(values);
byteList = builder.finish(offset);
builder.finish(offset);
byteList = builder.buffer;
}
// read and verify
BufferContext buf = new BufferContext.fromBytes(byteList);
BufferContext buf = BufferContext.fromBytes(byteList);
List<double> items = const Float64ListReader().read(buf, 0);
expect(items, hasLength(values.length));
@@ -404,12 +535,13 @@ class BuilderTest {
// write
List<int> byteList;
{
Builder builder = new Builder(initialSize: 0);
Builder builder = Builder(initialSize: 0);
int offset = builder.writeListFloat32(values);
byteList = builder.finish(offset);
builder.finish(offset);
byteList = builder.buffer;
}
// read and verify
BufferContext buf = new BufferContext.fromBytes(byteList);
BufferContext buf = BufferContext.fromBytes(byteList);
List<double> items = const Float32ListReader().read(buf, 0);
expect(items, hasLength(5));
for (int i = 0; i < values.length; i++) {
@@ -417,14 +549,14 @@ class BuilderTest {
}
}
void test_writeList_ofObjects() {
void test_writeList_ofObjects([Builder? builder]) {
List<int> byteList;
{
Builder builder = new Builder(initialSize: 0);
builder ??= Builder(initialSize: 0);
// write the object #1
int object1;
{
builder.startTable();
builder.startTable(2);
builder.addInt32(0, 10);
builder.addInt32(1, 20);
object1 = builder.endTable();
@@ -432,19 +564,20 @@ class BuilderTest {
// write the object #1
int object2;
{
builder.startTable();
builder.startTable(2);
builder.addInt32(0, 100);
builder.addInt32(1, 200);
object2 = builder.endTable();
}
// write the list
int offset = builder.writeList([object1, object2]);
byteList = builder.finish(offset);
builder.finish(offset);
byteList = builder.buffer;
}
// read and verify
BufferContext buf = new BufferContext.fromBytes(byteList);
BufferContext buf = BufferContext.fromBytes(byteList);
List<TestPointImpl> items =
const ListReader<TestPointImpl>(const TestPointReader()).read(buf, 0);
const ListReader<TestPointImpl>(TestPointReader()).read(buf, 0);
expect(items, hasLength(2));
expect(items[0].x, 10);
expect(items[0].y, 20);
@@ -455,36 +588,37 @@ class BuilderTest {
void test_writeList_ofStrings_asRoot() {
List<int> byteList;
{
Builder builder = new Builder(initialSize: 0);
int str1 = builder.writeString('12345');
int str2 = builder.writeString('ABC');
Builder builder = Builder(initialSize: 0);
int? str1 = builder.writeString('12345');
int? str2 = builder.writeString('ABC');
int offset = builder.writeList([str1, str2]);
byteList = builder.finish(offset);
builder.finish(offset);
byteList = builder.buffer;
}
// read and verify
BufferContext buf = new BufferContext.fromBytes(byteList);
List<String> items =
const ListReader<String>(const StringReader()).read(buf, 0);
BufferContext buf = BufferContext.fromBytes(byteList);
List<String> items = const ListReader<String>(StringReader()).read(buf, 0);
expect(items, hasLength(2));
expect(items, contains('12345'));
expect(items, contains('ABC'));
}
void test_writeList_ofStrings_inObject() {
void test_writeList_ofStrings_inObject([Builder? builder]) {
List<int> byteList;
{
Builder builder = new Builder(initialSize: 0);
builder ??= Builder(initialSize: 0);
int listOffset = builder.writeList(
[builder.writeString('12345'), builder.writeString('ABC')]);
builder.startTable();
builder.startTable(1);
builder.addOffset(0, listOffset);
int offset = builder.endTable();
byteList = builder.finish(offset);
builder.finish(offset);
byteList = builder.buffer;
}
// read and verify
BufferContext buf = new BufferContext.fromBytes(byteList);
StringListWrapperImpl reader = new StringListWrapperReader().read(buf, 0);
List<String> items = reader.items;
BufferContext buf = BufferContext.fromBytes(byteList);
StringListWrapperImpl reader = StringListWrapperReader().read(buf, 0);
List<String>? items = reader.items;
expect(items, hasLength(2));
expect(items, contains('12345'));
expect(items, contains('ABC'));
@@ -493,12 +627,13 @@ class BuilderTest {
void test_writeList_ofUint32() {
List<int> byteList;
{
Builder builder = new Builder(initialSize: 0);
Builder builder = Builder(initialSize: 0);
int offset = builder.writeListUint32(<int>[1, 2, 0x9ABCDEF0]);
byteList = builder.finish(offset);
builder.finish(offset);
byteList = builder.buffer;
}
// read and verify
BufferContext buf = new BufferContext.fromBytes(byteList);
BufferContext buf = BufferContext.fromBytes(byteList);
List<int> items = const Uint32ListReader().read(buf, 0);
expect(items, hasLength(3));
expect(items, orderedEquals(<int>[1, 2, 0x9ABCDEF0]));
@@ -507,12 +642,13 @@ class BuilderTest {
void test_writeList_ofUint16() {
List<int> byteList;
{
Builder builder = new Builder(initialSize: 0);
Builder builder = Builder(initialSize: 0);
int offset = builder.writeListUint16(<int>[1, 2, 60000]);
byteList = builder.finish(offset);
builder.finish(offset);
byteList = builder.buffer;
}
// read and verify
BufferContext buf = new BufferContext.fromBytes(byteList);
BufferContext buf = BufferContext.fromBytes(byteList);
List<int> items = const Uint16ListReader().read(buf, 0);
expect(items, hasLength(3));
expect(items, orderedEquals(<int>[1, 2, 60000]));
@@ -521,15 +657,188 @@ class BuilderTest {
void test_writeList_ofUint8() {
List<int> byteList;
{
Builder builder = new Builder(initialSize: 0);
int offset = builder.writeListUint8(<int>[1, 2, 3, 4, 0x9A]);
byteList = builder.finish(offset);
Builder builder = Builder(initialSize: 0);
int offset = builder.writeListUint8(<int>[1, 2, 3, 4, 0x9A, 0xFA]);
builder.finish(offset);
byteList = builder.buffer;
}
// read and verify
BufferContext buf = new BufferContext.fromBytes(byteList);
List<int> items = const Uint8ListReader().read(buf, 0);
expect(items, hasLength(5));
expect(items, orderedEquals(<int>[1, 2, 3, 4, 0x9A]));
BufferContext buf = BufferContext.fromBytes(byteList);
const buffOffset = 8; // 32-bit offset to the list, + 32-bit length
for (final lazy in [true, false]) {
List<int> items = Uint8ListReader(lazy: lazy).read(buf, 0);
expect(items, hasLength(6));
expect(items, orderedEquals(<int>[1, 2, 3, 4, 0x9A, 0xFA]));
// overwrite the buffer to verify the laziness
buf.buffer.setUint8(buffOffset + 1, 99);
expect(items, orderedEquals(<int>[1, lazy ? 99 : 2, 3, 4, 0x9A, 0xFA]));
// restore the previous value for the next loop
buf.buffer.setUint8(buffOffset + 1, 2);
}
}
void test_reset() {
// We'll run a selection of tests , reusing the builder between them.
final testCases = <void Function(Builder?)>[
test_monsterBuilder,
test_error_addInt32_withoutStartTable,
test_table_format,
test_table_types,
test_writeList_ofObjects,
test_writeList_ofStrings_inObject
];
// Execute all test cases in all permutations of their order.
// To do that, we generate permutations of test case indexes.
final testCasesPermutations =
_permutationsOf(List.generate(testCases.length, (index) => index));
expect(testCasesPermutations.length, _factorial(testCases.length));
for (var indexes in testCasesPermutations) {
// print the order so failures are reproducible
printOnFailure('Running reset() test cases in order: $indexes');
Builder? builder;
for (var index in indexes) {
if (builder == null) {
// Initial size small enough so at least one test case increases it.
// On the other hand, it's large enough so that some test cases don't.
builder = Builder(initialSize: 32);
} else {
builder.reset();
}
testCases[index](builder);
}
}
}
// Generate permutations of the given list
List<List<T>> _permutationsOf<T>(List<T> source) {
final result = <List<T>>[];
void permutate(List<T> items, int startAt) {
for (var i = startAt; i < items.length; i++) {
List<T> permutation = items.toList(growable: false);
permutation[i] = items[startAt];
permutation[startAt] = items[i];
// add the current list upon reaching the end
if (startAt == items.length - 1) {
result.add(items);
} else {
permutate(permutation, startAt + 1);
}
}
}
permutate(source, 0);
return result;
}
// a very simple implementation of n!
int _factorial(int n) {
var result = 1;
for (var i = 2; i <= n; i++) {
result *= i;
}
return result;
}
}
@reflectiveTest
class ObjectAPITest {
void test_tableStat() {
final object1 = example.StatT(count: 3, id: "foo", val: 4);
expect(object1 is Packable, isTrue);
final fbb = Builder();
fbb.finish(object1.pack(fbb));
final object2 = example.Stat(fbb.buffer).unpack();
expect(object2.count, object1.count);
expect(object2.id, object1.id);
expect(object2.val, object1.val);
expect(object2.toString(), object1.toString());
}
void test_tableMonster() {
final monster = example.MonsterT()
..pos = example.Vec3T(
x: 1,
y: 2,
z: 3,
test1: 4.0,
test2: example.Color.Red,
test3: example.TestT(a: 1, b: 2))
..mana = 2
..name = 'Monstrous'
..inventory = [24, 42]
..color = example.Color.Green
// TODO be smarter for unions and automatically set the `type` field?
..testType = example.AnyTypeId.MyGame_Example2_Monster
..test = example2.MonsterT()
..test4 = [example.TestT(a: 3, b: 4), example.TestT(a: 5, b: 6)]
..testarrayofstring = ["foo", "bar"]
..testarrayoftables = [example.MonsterT(name: 'Oof')]
..enemy = example.MonsterT(name: 'Enemy')
..testarrayofbools = [false, true, false]
..testf = 42.24
..testarrayofsortedstruct = [
example.AbilityT(id: 1, distance: 5),
example.AbilityT(id: 3, distance: 7)
]
..vectorOfLongs = [5, 6, 7]
..vectorOfDoubles = [8.9, 9.0, 10.1, 11.2]
..anyAmbiguousType = example.AnyAmbiguousAliasesTypeId.M2
..anyAmbiguous = null
..vectorOfEnums = [example.Color.Blue, example.Color.Green]
..signedEnum = example.Race.None;
final fbBuilder = Builder();
final offset = monster.pack(fbBuilder);
expect(offset, isNonZero);
fbBuilder.finish(offset);
final data = fbBuilder.buffer;
// TODO currently broken because of struct builder issue, see #6688
// final monster2 = example.Monster(data); // Monster (reader)
// expect(
// // map Monster => MonsterT, Vec3 => Vec3T, ...
// monster2.toString().replaceAllMapped(
// RegExp('([a-zA-z0-9]+){'), (match) => match.group(1) + 'T{'),
// monster.toString());
//
// final monster3 = monster2.unpack(); // MonsterT
// expect(monster3.toString(), monster.toString());
}
void test_Lists() {
// Ensure unpack() reads lists eagerly by reusing the same builder and
// overwriting data. Why: because standard reader reads lists lazily...
final fbb = Builder();
final object1 = example.TypeAliasesT(v8: [1, 2, 3], vf64: [5, 6]);
fbb.finish(object1.pack(fbb));
final object1Read = example.TypeAliases(fbb.buffer).unpack();
// overwrite the original buffer by writing to the same builder
fbb.reset();
final object2 = example.TypeAliasesT(v8: [7, 8, 9], vf64: [10, 11]);
fbb.finish(object2.pack(fbb));
final object2Read = example.TypeAliases(fbb.buffer).unpack();
// this is fine even with lazy lists:
expect(object2.toString(), object2Read.toString());
// this fails with lazy lists:
expect(object1.toString(), object1Read.toString());
// empty list must be serialized as such (were stored NULL before v2.0)
fbb.reset();
final object3 = example.TypeAliasesT(v8: [], vf64: null);
fbb.finish(object3.pack(fbb));
final object3Read = example.TypeAliases(fbb.buffer).unpack();
expect(object3.toString(), object3Read.toString());
}
}
@@ -539,8 +848,8 @@ class StringListWrapperImpl {
StringListWrapperImpl(this.bp, this.offset);
List<String> get items => const ListReader<String>(const StringReader())
.vTableGet(bp, offset, indexToField(0));
List<String>? get items => const ListReader<String>(StringReader())
.vTableGetNullable(bp, offset, indexToField(0));
}
class StringListWrapperReader extends TableReader<StringListWrapperImpl> {
@@ -548,7 +857,7 @@ class StringListWrapperReader extends TableReader<StringListWrapperImpl> {
@override
StringListWrapperImpl createObject(BufferContext object, int offset) {
return new StringListWrapperImpl(object, offset);
return StringListWrapperImpl(object, offset);
}
}
@@ -568,6 +877,19 @@ class TestPointReader extends TableReader<TestPointImpl> {
@override
TestPointImpl createObject(BufferContext object, int offset) {
return new TestPointImpl(object, offset);
return TestPointImpl(object, offset);
}
}
@reflectiveTest
class GeneratorTest {
void test_constantEnumValues() async {
expect(example.Color.values, same(example.Color.values));
expect(example.Race.values, same(example.Race.values));
expect(example.AnyTypeId.values, same(example.AnyTypeId.values));
expect(example.AnyUniqueAliasesTypeId.values,
same(example.AnyUniqueAliasesTypeId.values));
expect(example.AnyAmbiguousAliasesTypeId.values,
same(example.AnyAmbiguousAliasesTypeId.values));
}
}

View File

@@ -0,0 +1,593 @@
import 'dart:typed_data';
import 'package:flat_buffers/flex_buffers.dart' show Builder;
import 'package:test/test.dart';
void main() {
test('build with single value', () {
{
var flx = Builder();
flx.addNull();
expect(flx.finish(), [0, 0, 1]);
}
{
var flx = Builder();
flx.addBool(true);
expect(flx.finish(), [1, 104, 1]);
}
{
var flx = Builder();
flx.addBool(false);
expect(flx.finish(), [0, 104, 1]);
}
{
var flx = Builder();
flx.addInt(1);
expect(flx.finish(), [1, 4, 1]);
}
{
var flx = Builder();
flx.addInt(230);
expect(flx.finish(), [230, 0, 5, 2]);
}
{
var flx = Builder();
flx.addInt(1025);
expect(flx.finish(), [1, 4, 5, 2]);
}
{
var flx = Builder();
flx.addInt(-1025);
expect(flx.finish(), [255, 251, 5, 2]);
}
{
var flx = Builder();
flx.addDouble(0.1);
expect(flx.finish(), [154, 153, 153, 153, 153, 153, 185, 63, 15, 8]);
}
{
var flx = Builder();
flx.addDouble(0.5);
expect(flx.finish(), [0, 0, 0, 63, 14, 4]);
}
{
var flx = Builder();
flx.addString('Maxim');
expect(flx.finish(), [5, 77, 97, 120, 105, 109, 0, 6, 20, 1]);
}
{
var flx = Builder();
flx.addString('hello 😱');
expect(flx.finish(),
[10, 104, 101, 108, 108, 111, 32, 240, 159, 152, 177, 0, 11, 20, 1]);
}
});
test('build vector', () {
{
var flx = Builder()
..startVector()
..addInt(1)
..addInt(2)
..end();
expect(flx.finish(), [1, 2, 2, 64, 1]);
}
{
var flx = Builder()
..startVector()
..addInt(-1)
..addInt(256)
..end();
expect(flx.finish(), [255, 255, 0, 1, 4, 65, 1]);
}
{
var flx = Builder()
..startVector()
..addInt(-45)
..addInt(256000)
..end();
expect(flx.finish(), [211, 255, 255, 255, 0, 232, 3, 0, 8, 66, 1]);
}
{
var flx = Builder()
..startVector()
..addDouble(1.1)
..addDouble(-256)
..end();
expect(flx.finish(), [
154,
153,
153,
153,
153,
153,
241,
63,
0,
0,
0,
0,
0,
0,
112,
192,
16,
75,
1
]);
}
{
var flx = Builder()
..startVector()
..addInt(1)
..addInt(2)
..addInt(4)
..end();
expect(flx.finish(), [1, 2, 4, 3, 76, 1]);
}
{
var flx = Builder()
..startVector()
..addInt(-1)
..addInt(256)
..addInt(4)
..end();
expect(flx.finish(), [255, 255, 0, 1, 4, 0, 6, 77, 1]);
}
{
var flx = Builder()
..startVector()
..startVector()
..addInt(61)
..end()
..addInt(64)
..end();
expect(flx.finish(), [1, 61, 2, 2, 64, 44, 4, 4, 40, 1]);
}
{
var flx = Builder()
..startVector()
..addString('foo')
..addString('bar')
..addString('baz')
..end();
expect(flx.finish(), [
3,
102,
111,
111,
0,
3,
98,
97,
114,
0,
3,
98,
97,
122,
0,
3,
15,
11,
7,
3,
60,
1
]);
}
{
var flx = Builder()
..startVector()
..addString('foo')
..addString('bar')
..addString('baz')
..addString('foo')
..addString('bar')
..addString('baz')
..end();
expect(flx.finish(), [
3,
102,
111,
111,
0,
3,
98,
97,
114,
0,
3,
98,
97,
122,
0,
6,
15,
11,
7,
18,
14,
10,
6,
60,
1
]);
}
{
var flx = Builder()
..startVector()
..addBool(true)
..addBool(false)
..addBool(true)
..end();
expect(flx.finish(), [3, 1, 0, 1, 3, 144, 1]);
}
{
var flx = Builder()
..startVector()
..addString('foo')
..addInt(1)
..addInt(-5)
..addDouble(1.3)
..addBool(true)
..end();
expect(flx.finish(), [
3,
102,
111,
111,
0,
0,
0,
0,
5,
0,
0,
0,
0,
0,
0,
0,
15,
0,
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
0,
0,
0,
251,
255,
255,
255,
255,
255,
255,
255,
205,
204,
204,
204,
204,
204,
244,
63,
1,
0,
0,
0,
0,
0,
0,
0,
20,
4,
4,
15,
104,
45,
43,
1
]);
}
});
test('build map', () {
{
var flx = Builder()
..startMap()
..addKey('a')
..addInt(12)
..end();
expect(flx.finish(), [97, 0, 1, 3, 1, 1, 1, 12, 4, 2, 36, 1]);
}
{
var flx = Builder()
..startMap()
..addKey('a')
..addInt(12)
..addKey('')
..addInt(45)
..end();
expect(
flx.finish(), [97, 0, 0, 2, 2, 5, 2, 1, 2, 45, 12, 4, 4, 4, 36, 1]);
}
{
var flx = Builder()
..startVector()
..startMap()
..addKey('something')
..addInt(12)
..end()
..startMap()
..addKey('something')
..addInt(45)
..end()
..end();
expect(flx.finish(), [
115,
111,
109,
101,
116,
104,
105,
110,
103,
0,
1,
11,
1,
1,
1,
12,
4,
6,
1,
1,
45,
4,
2,
8,
4,
36,
36,
4,
40,
1
]);
}
});
test('build blob', () {
{
var flx = Builder()..addBlob(Uint8List.fromList([1, 2, 3]).buffer);
expect(flx.finish(), [3, 1, 2, 3, 3, 100, 1]);
}
});
test('build from object', () {
expect(
Builder.buildFromObject(Uint8List.fromList([1, 2, 3]).buffer)
.asUint8List(),
[3, 1, 2, 3, 3, 100, 1]);
expect(Builder.buildFromObject(null).asUint8List(), [0, 0, 1]);
expect(Builder.buildFromObject(true).asUint8List(), [1, 104, 1]);
expect(Builder.buildFromObject(false).asUint8List(), [0, 104, 1]);
expect(Builder.buildFromObject(25).asUint8List(), [25, 4, 1]);
expect(Builder.buildFromObject(-250).asUint8List(), [6, 255, 5, 2]);
expect(
Builder.buildFromObject(-2.50).asUint8List(), [0, 0, 32, 192, 14, 4]);
expect(Builder.buildFromObject('Maxim').asUint8List(),
[5, 77, 97, 120, 105, 109, 0, 6, 20, 1]);
expect(
Builder.buildFromObject([1, 3.3, 'max', true, null, false])
.asUint8List(),
[
3,
109,
97,
120,
0,
0,
0,
0,
6,
0,
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
0,
0,
0,
102,
102,
102,
102,
102,
102,
10,
64,
31,
0,
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
4,
15,
20,
104,
0,
104,
54,
43,
1
]);
expect(
Builder.buildFromObject([
{'something': 12},
{'something': 45}
]).asUint8List(),
[
115,
111,
109,
101,
116,
104,
105,
110,
103,
0,
1,
11,
1,
1,
1,
12,
4,
6,
1,
1,
45,
4,
2,
8,
4,
36,
36,
4,
40,
1
]);
});
test('add double indirectly', () {
var flx = Builder()..addDoubleIndirectly(0.1);
expect(flx.finish(), [154, 153, 153, 153, 153, 153, 185, 63, 8, 35, 1]);
});
test('add double indirectly to vector with cache', () {
var flx = Builder()
..startVector()
..addDoubleIndirectly(0.1, cache: true)
..addDoubleIndirectly(0.1, cache: true)
..addDoubleIndirectly(0.1, cache: true)
..addDoubleIndirectly(0.1, cache: true)
..end();
expect(flx.finish(), [
154,
153,
153,
153,
153,
153,
185,
63,
4,
9,
10,
11,
12,
35,
35,
35,
35,
8,
40,
1
]);
});
test('add int indirectly', () {
var flx = Builder()..addIntIndirectly(2345234523452345);
expect(flx.finish(), [185, 115, 175, 118, 250, 84, 8, 0, 8, 27, 1]);
});
test('add int indirectly to vector with cache', () {
var flx = Builder()
..startVector()
..addIntIndirectly(2345234523452345, cache: true)
..addIntIndirectly(2345234523452345, cache: true)
..addIntIndirectly(2345234523452345, cache: true)
..addIntIndirectly(2345234523452345, cache: true)
..end();
expect(flx.finish(), [
185,
115,
175,
118,
250,
84,
8,
0,
4,
9,
10,
11,
12,
27,
27,
27,
27,
8,
40,
1
]);
});
test('snapshot', () {
var flx = Builder();
flx.startVector();
flx.addInt(12);
expect(flx.snapshot().asUint8List(), [1, 12, 1, 44, 1]);
flx.addInt(24);
expect(flx.snapshot().asUint8List(), [12, 24, 2, 64, 1]);
flx.addInt(45);
expect(flx.snapshot().asUint8List(), [12, 24, 45, 3, 76, 1]);
});
}

View File

@@ -0,0 +1,990 @@
import 'dart:typed_data';
import 'package:flat_buffers/flex_buffers.dart' show Reference, Builder;
import 'package:test/test.dart';
void main() {
test('is null', () {
expect(Reference.fromBuffer(b([0, 0, 1])).isNull, isTrue);
});
test('bool value', () {
expect(Reference.fromBuffer(b([1, 104, 1])).boolValue, isTrue);
expect(Reference.fromBuffer(b([0, 104, 1])).boolValue, isFalse);
});
test('int value', () {
expect(Reference.fromBuffer(b([25, 4, 1])).intValue, 25);
expect(Reference.fromBuffer(b([231, 4, 1])).intValue, -25);
expect(Reference.fromBuffer(b([230, 8, 1])).intValue, 230);
expect(Reference.fromBuffer(b([230, 0, 5, 2])).intValue, 230);
expect(Reference.fromBuffer(b([1, 4, 5, 2])).intValue, 1025);
expect(Reference.fromBuffer(b([255, 251, 5, 2])).intValue, -1025);
expect(Reference.fromBuffer(b([1, 4, 9, 2])).intValue, 1025);
expect(Reference.fromBuffer(b([255, 255, 255, 127, 6, 4])).intValue,
2147483647);
expect(Reference.fromBuffer(b([0, 0, 0, 128, 6, 4])).intValue, -2147483648);
expect(
Reference.fromBuffer(b([255, 255, 255, 255, 0, 0, 0, 0, 7, 8]))
.intValue,
4294967295);
expect(
Reference.fromBuffer(b([255, 255, 255, 255, 255, 255, 255, 127, 7, 8]))
.intValue,
9223372036854775807);
expect(Reference.fromBuffer(b([0, 0, 0, 0, 0, 0, 0, 128, 7, 8])).intValue,
-9223372036854775808);
// Dart does not really support UInt64
// expect(FlxValue.fromBuffer(b([255, 255, 255, 255, 255, 255, 255, 255, 11, 8])).intValue, 18446744073709551615);
});
test('double value', () {
expect(Reference.fromBuffer(b([0, 0, 144, 64, 14, 4])).doubleValue, 4.5);
expect(Reference.fromBuffer(b([205, 204, 204, 61, 14, 4])).doubleValue,
closeTo(.1, .001));
expect(
Reference.fromBuffer(b([154, 153, 153, 153, 153, 153, 185, 63, 15, 8]))
.doubleValue,
.1);
});
test('num value', () {
expect(Reference.fromBuffer(b([0, 0, 144, 64, 14, 4])).numValue, 4.5);
expect(Reference.fromBuffer(b([205, 204, 204, 61, 14, 4])).numValue,
closeTo(.1, .001));
expect(
Reference.fromBuffer(b([154, 153, 153, 153, 153, 153, 185, 63, 15, 8]))
.numValue,
.1);
expect(Reference.fromBuffer(b([255, 251, 5, 2])).numValue, -1025);
});
test('string value', () {
expect(
Reference.fromBuffer(b([5, 77, 97, 120, 105, 109, 0, 6, 20, 1]))
.stringValue,
'Maxim');
expect(
Reference.fromBuffer(b([
10,
104,
101,
108,
108,
111,
32,
240,
159,
152,
177,
0,
11,
20,
1
])).stringValue,
'hello 😱');
});
test('blob value', () {
expect(
Reference.fromBuffer(b([3, 1, 2, 3, 3, 100, 1])).blobValue, [1, 2, 3]);
});
test('bool vector', () {
var flx = Reference.fromBuffer(b([3, 1, 0, 1, 3, 144, 1]));
expect(flx[0].boolValue, true);
expect(flx[1].boolValue, false);
expect(flx[2].boolValue, true);
});
test('number vector', () {
testNumbers([3, 1, 2, 3, 3, 44, 1], [1, 2, 3]);
testNumbers([3, 255, 2, 3, 3, 44, 1], [-1, 2, 3]);
testNumbers([3, 0, 1, 0, 43, 2, 3, 0, 6, 45, 1], [1, 555, 3]);
testNumbers([3, 0, 0, 0, 1, 0, 0, 0, 204, 216, 0, 0, 3, 0, 0, 0, 12, 46, 1],
[1, 55500, 3]);
testNumbers([
3,
0,
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
0,
0,
0,
172,
128,
94,
239,
12,
0,
0,
0,
3,
0,
0,
0,
0,
0,
0,
0,
24,
47,
1
], [
1,
55555555500,
3
]);
testNumbers(
[3, 0, 0, 0, 0, 0, 192, 63, 0, 0, 32, 64, 0, 0, 96, 64, 12, 54, 1],
[1.5, 2.5, 3.5]);
testNumbers([
3,
0,
0,
0,
0,
0,
0,
0,
154,
153,
153,
153,
153,
153,
241,
63,
154,
153,
153,
153,
153,
153,
1,
64,
102,
102,
102,
102,
102,
102,
10,
64,
24,
55,
1
], [
1.1,
2.2,
3.3
]);
});
test('number vector, fixed type', () {
testNumbers([1, 2, 2, 64, 1], [1, 2]);
testNumbers([255, 255, 0, 1, 4, 65, 1], [-1, 256]);
testNumbers([211, 255, 255, 255, 0, 232, 3, 0, 8, 66, 1], [-45, 256000]);
testNumbers([
211,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
127,
16,
67,
1
], [
-45,
9223372036854775807
]);
testNumbers([1, 2, 2, 68, 1], [1, 2]);
testNumbers([1, 0, 0, 1, 4, 69, 1], [1, 256]);
testNumbers([45, 0, 0, 0, 0, 232, 3, 0, 8, 70, 1], [45, 256000]);
testNumbers([205, 204, 140, 63, 0, 0, 0, 192, 8, 74, 1], [1.1, -2]);
testNumbers([
154,
153,
153,
153,
153,
153,
241,
63,
0,
0,
0,
0,
0,
0,
112,
192,
16,
75,
1
], [
1.1,
-256
]);
testNumbers([211, 255, 255, 255, 0, 232, 3, 0, 4, 0, 0, 0, 12, 78, 1],
[-45, 256000, 4]);
testNumbers([
211,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
255,
127,
4,
0,
0,
0,
0,
0,
0,
0,
9,
0,
0,
0,
0,
0,
0,
0,
32,
91,
1
], [
-45,
9223372036854775807,
4,
9
]);
testNumbers([
45,
0,
0,
0,
0,
0,
0,
0,
255,
255,
255,
255,
255,
255,
255,
127,
4,
0,
0,
0,
0,
0,
0,
0,
9,
0,
0,
0,
0,
0,
0,
0,
32,
95,
1
], [
45,
9223372036854775807,
4,
9
]);
testNumbers([
154,
153,
153,
153,
153,
153,
241,
63,
0,
0,
0,
0,
0,
0,
112,
64,
0,
0,
0,
0,
0,
0,
16,
64,
24,
87,
1
], [
1.1,
256,
4
]);
testNumbers([
154,
153,
153,
153,
153,
153,
241,
63,
0,
0,
0,
0,
0,
0,
112,
64,
0,
0,
0,
0,
0,
0,
16,
64,
0,
0,
0,
0,
0,
0,
34,
64,
32,
99,
1
], [
1.1,
256,
4,
9
]);
});
test('string vector', () {
testStrings([
3,
102,
111,
111,
0,
3,
98,
97,
114,
0,
3,
98,
97,
122,
0,
3,
15,
11,
7,
3,
60,
1
], [
'foo',
'bar',
'baz'
]);
testStrings([
3,
102,
111,
111,
0,
3,
98,
97,
114,
0,
3,
98,
97,
122,
0,
6,
15,
11,
7,
18,
14,
10,
6,
60,
1
], [
'foo',
'bar',
'baz',
'foo',
'bar',
'baz'
]);
});
test('mixed vector', () {
var flx = Reference.fromBuffer(b([
3,
102,
111,
111,
0,
0,
0,
0,
5,
0,
0,
0,
0,
0,
0,
0,
15,
0,
0,
0,
0,
0,
0,
0,
1,
0,
0,
0,
0,
0,
0,
0,
251,
255,
255,
255,
255,
255,
255,
255,
205,
204,
204,
204,
204,
204,
244,
63,
1,
0,
0,
0,
0,
0,
0,
0,
20,
4,
4,
15,
104,
45,
43,
1
]));
expect(flx.length, 5);
expect(flx[0].stringValue, 'foo');
expect(flx[1].numValue, 1);
expect(flx[2].numValue, -5);
expect(flx[3].numValue, 1.3);
expect(flx[4].boolValue, true);
});
test('single value map', () {
var flx = Reference.fromBuffer(b([97, 0, 1, 3, 1, 1, 1, 12, 4, 2, 36, 1]));
expect(flx.length, 1);
expect(flx['a'].numValue, 12);
});
test('two value map', () {
var flx = Reference.fromBuffer(
b([0, 97, 0, 2, 4, 4, 2, 1, 2, 45, 12, 4, 4, 4, 36, 1]));
expect(flx.length, 2);
expect(flx['a'].numValue, 12);
expect(flx[''].numValue, 45);
});
test('complex map', () {
var flx = complexMap();
expect(flx.length, 5);
expect(flx['age'].numValue, 35);
expect(flx['weight'].numValue, 72.5);
expect(flx['name'].stringValue, 'Maxim');
expect(flx['flags'].length, 4);
expect(flx['flags'][0].boolValue, true);
expect(flx['flags'][1].boolValue, false);
expect(flx['flags'][2].boolValue, true);
expect(flx['flags'][3].boolValue, true);
expect(flx['address'].length, 3);
expect(flx['address']['city'].stringValue, 'Bla');
expect(flx['address']['zip'].stringValue, '12345');
expect(flx['address']['countryCode'].stringValue, 'XX');
expect(
() => flx['address']['country'].stringValue,
throwsA(predicate((dynamic e) =>
e is ArgumentError &&
e.message ==
'Key: [country] is not applicable on: //address of: ValueType.Map')));
expect(
() => flx['address']['countryCode'][0],
throwsA(predicate((dynamic e) =>
e is ArgumentError &&
e.message ==
'Key: [0] is not applicable on: //address/countryCode of: ValueType.String')));
expect(
() => flx[1],
throwsA(predicate((dynamic e) =>
e is ArgumentError &&
e.message ==
'Key: [1] is not applicable on: / of: ValueType.Map')));
expect(
() => flx['flags'][4],
throwsA(predicate((dynamic e) =>
e is ArgumentError &&
e.message ==
'Key: [4] is not applicable on: //flags of: ValueType.VectorBool length: 4')));
expect(
() => flx['flags'][-1],
throwsA(predicate((dynamic e) =>
e is ArgumentError &&
e.message ==
'Key: [-1] is not applicable on: //flags of: ValueType.VectorBool length: 4')));
});
test('complex map to json', () {
var flx = complexMap();
expect(flx.json,
'{"address":{"city":"Bla","countryCode":"XX","zip":"12345"},"age":35,"flags":[true,false,true,true],"name":"Maxim","weight":72.5}');
});
test('complex map iterators', () {
var flx = complexMap();
expect(flx.mapKeyIterable.map((e) => e).toList(),
['address', 'age', 'flags', 'name', 'weight']);
expect(flx.mapValueIterable.map((e) => e.json).toList(), [
flx['address'].json,
flx['age'].json,
flx['flags'].json,
flx['name'].json,
flx['weight'].json
]);
expect(flx['flags'].vectorIterable.map((e) => e.boolValue).toList(),
[true, false, true, true]);
});
test('bug where offest were stored as int instead of uint', () {
const data = [
99,
104,
97,
110,
110,
101,
108,
115,
95,
105,
110,
0,
100,
105,
108,
97,
116,
105,
111,
110,
95,
104,
101,
105,
103,
104,
116,
95,
102,
97,
99,
116,
111,
114,
0,
100,
105,
108,
97,
116,
105,
111,
110,
95,
119,
105,
100,
116,
104,
95,
102,
97,
99,
116,
111,
114,
0,
102,
117,
115,
101,
100,
95,
97,
99,
116,
105,
118,
97,
116,
105,
111,
110,
95,
102,
117,
110,
99,
116,
105,
111,
110,
0,
112,
97,
100,
95,
118,
97,
108,
117,
101,
115,
0,
112,
97,
100,
100,
105,
110,
103,
0,
115,
116,
114,
105,
100,
101,
95,
104,
101,
105,
103,
104,
116,
0,
115,
116,
114,
105,
100,
101,
95,
119,
105,
100,
116,
104,
0,
8,
130,
119,
97,
76,
51,
41,
34,
21,
8,
1,
8,
64,
1,
1,
1,
1,
0,
1,
1,
4,
4,
4,
4,
4,
4,
4,
4,
16,
36,
1
];
var flx = Reference.fromBuffer(b(data));
expect(flx.json,
'{"channels_in":64,"dilation_height_factor":1,"dilation_width_factor":1,"fused_activation_function":1,"pad_values":1,"padding":0,"stride_height":1,"stride_width":1}');
const object = {
"channels_in": 64,
"dilation_height_factor": 1,
"dilation_width_factor": 1,
"fused_activation_function": 1,
"pad_values": 1,
"padding": 0,
"stride_height": 1,
"stride_width": 1
};
var data1 = Builder.buildFromObject(object).asUint8List();
expect(data1.length, data.length);
var flx1 = Reference.fromBuffer(b(data1));
expect(flx1.json,
'{"channels_in":64,"dilation_height_factor":1,"dilation_width_factor":1,"fused_activation_function":1,"pad_values":1,"padding":0,"stride_height":1,"stride_width":1}');
});
}
ByteBuffer b(List<int> values) {
var data = Uint8List.fromList(values);
return data.buffer;
}
void testNumbers(List<int> buffer, List<num> numbers) {
var flx = Reference.fromBuffer(b(buffer));
expect(flx.length, numbers.length);
for (var i = 0; i < flx.length; i++) {
expect(flx[i].numValue, closeTo(numbers[i], 0.001));
}
}
void testStrings(List<int> buffer, List<String> numbers) {
var flx = Reference.fromBuffer(b(buffer));
expect(flx.length, numbers.length);
for (var i = 0; i < flx.length; i++) {
expect(flx[i].stringValue, numbers[i]);
}
}
Reference complexMap() {
// {
// "age": 35,
// "flags": [True, False, True, True],
// "weight": 72.5,
// "name": "Maxim",
// "address": {
// "city": "Bla",
// "zip": "12345",
// "countryCode": "XX",
// }
// }
return Reference.fromBuffer(b([
97,
100,
100,
114,
101,
115,
115,
0,
99,
105,
116,
121,
0,
3,
66,
108,
97,
0,
99,
111,
117,
110,
116,
114,
121,
67,
111,
100,
101,
0,
2,
88,
88,
0,
122,
105,
112,
0,
5,
49,
50,
51,
52,
53,
0,
3,
38,
29,
14,
3,
1,
3,
38,
22,
15,
20,
20,
20,
97,
103,
101,
0,
102,
108,
97,
103,
115,
0,
4,
1,
0,
1,
1,
110,
97,
109,
101,
0,
5,
77,
97,
120,
105,
109,
0,
119,
101,
105,
103,
104,
116,
0,
5,
93,
36,
33,
23,
12,
0,
0,
7,
0,
0,
0,
1,
0,
0,
0,
5,
0,
0,
0,
60,
0,
0,
0,
35,
0,
0,
0,
51,
0,
0,
0,
45,
0,
0,
0,
0,
0,
145,
66,
36,
4,
144,
20,
14,
25,
38,
1
]));
}

View File

@@ -0,0 +1,184 @@
import 'package:flat_buffers/src/types.dart';
import 'package:test/test.dart';
void main() {
test('is inline', () {
expect(ValueTypeUtils.isInline(ValueType.Bool), isTrue);
expect(ValueTypeUtils.isInline(ValueType.Int), isTrue);
expect(ValueTypeUtils.isInline(ValueType.UInt), isTrue);
expect(ValueTypeUtils.isInline(ValueType.Float), isTrue);
expect(ValueTypeUtils.isInline(ValueType.Null), isTrue);
expect(ValueTypeUtils.isInline(ValueType.String), isFalse);
});
test('is type vector element', () {
expect(ValueTypeUtils.isTypedVectorElement(ValueType.Bool), isTrue);
expect(ValueTypeUtils.isTypedVectorElement(ValueType.Int), isTrue);
expect(ValueTypeUtils.isTypedVectorElement(ValueType.UInt), isTrue);
expect(ValueTypeUtils.isTypedVectorElement(ValueType.Float), isTrue);
expect(ValueTypeUtils.isTypedVectorElement(ValueType.Key), isTrue);
expect(ValueTypeUtils.isTypedVectorElement(ValueType.String), isTrue);
expect(ValueTypeUtils.isTypedVectorElement(ValueType.Null), isFalse);
expect(ValueTypeUtils.isTypedVectorElement(ValueType.Blob), isFalse);
});
test('is typed vector', () {
expect(ValueTypeUtils.isTypedVector(ValueType.VectorInt), isTrue);
expect(ValueTypeUtils.isTypedVector(ValueType.VectorUInt), isTrue);
expect(ValueTypeUtils.isTypedVector(ValueType.VectorFloat), isTrue);
expect(ValueTypeUtils.isTypedVector(ValueType.VectorBool), isTrue);
expect(ValueTypeUtils.isTypedVector(ValueType.VectorKey), isTrue);
expect(ValueTypeUtils.isTypedVector(ValueType.VectorString), isTrue);
expect(ValueTypeUtils.isTypedVector(ValueType.Vector), isFalse);
expect(ValueTypeUtils.isTypedVector(ValueType.Map), isFalse);
expect(ValueTypeUtils.isTypedVector(ValueType.Bool), isFalse);
expect(ValueTypeUtils.isTypedVector(ValueType.VectorInt2), isFalse);
});
test('is fixed typed vector', () {
expect(ValueTypeUtils.isFixedTypedVector(ValueType.VectorInt2), isTrue);
expect(ValueTypeUtils.isFixedTypedVector(ValueType.VectorInt3), isTrue);
expect(ValueTypeUtils.isFixedTypedVector(ValueType.VectorInt4), isTrue);
expect(ValueTypeUtils.isFixedTypedVector(ValueType.VectorUInt2), isTrue);
expect(ValueTypeUtils.isFixedTypedVector(ValueType.VectorUInt3), isTrue);
expect(ValueTypeUtils.isFixedTypedVector(ValueType.VectorUInt4), isTrue);
expect(ValueTypeUtils.isFixedTypedVector(ValueType.VectorFloat2), isTrue);
expect(ValueTypeUtils.isFixedTypedVector(ValueType.VectorFloat3), isTrue);
expect(ValueTypeUtils.isFixedTypedVector(ValueType.VectorFloat4), isTrue);
expect(ValueTypeUtils.isFixedTypedVector(ValueType.VectorInt), isFalse);
});
test('to typed vector', () {
expect(ValueTypeUtils.toTypedVector(ValueType.Int, 0),
equals(ValueType.VectorInt));
expect(ValueTypeUtils.toTypedVector(ValueType.UInt, 0),
equals(ValueType.VectorUInt));
expect(ValueTypeUtils.toTypedVector(ValueType.Bool, 0),
equals(ValueType.VectorBool));
expect(ValueTypeUtils.toTypedVector(ValueType.Float, 0),
equals(ValueType.VectorFloat));
expect(ValueTypeUtils.toTypedVector(ValueType.Key, 0),
equals(ValueType.VectorKey));
expect(ValueTypeUtils.toTypedVector(ValueType.String, 0),
equals(ValueType.VectorString));
expect(ValueTypeUtils.toTypedVector(ValueType.Int, 2),
equals(ValueType.VectorInt2));
expect(ValueTypeUtils.toTypedVector(ValueType.UInt, 2),
equals(ValueType.VectorUInt2));
expect(ValueTypeUtils.toTypedVector(ValueType.Float, 2),
equals(ValueType.VectorFloat2));
expect(ValueTypeUtils.toTypedVector(ValueType.Int, 3),
equals(ValueType.VectorInt3));
expect(ValueTypeUtils.toTypedVector(ValueType.UInt, 3),
equals(ValueType.VectorUInt3));
expect(ValueTypeUtils.toTypedVector(ValueType.Float, 3),
equals(ValueType.VectorFloat3));
expect(ValueTypeUtils.toTypedVector(ValueType.Int, 4),
equals(ValueType.VectorInt4));
expect(ValueTypeUtils.toTypedVector(ValueType.UInt, 4),
equals(ValueType.VectorUInt4));
expect(ValueTypeUtils.toTypedVector(ValueType.Float, 4),
equals(ValueType.VectorFloat4));
});
test('typed vector element type', () {
expect(ValueTypeUtils.typedVectorElementType(ValueType.VectorInt),
equals(ValueType.Int));
expect(ValueTypeUtils.typedVectorElementType(ValueType.VectorUInt),
equals(ValueType.UInt));
expect(ValueTypeUtils.typedVectorElementType(ValueType.VectorFloat),
equals(ValueType.Float));
expect(ValueTypeUtils.typedVectorElementType(ValueType.VectorString),
equals(ValueType.String));
expect(ValueTypeUtils.typedVectorElementType(ValueType.VectorKey),
equals(ValueType.Key));
expect(ValueTypeUtils.typedVectorElementType(ValueType.VectorBool),
equals(ValueType.Bool));
});
test('fixed typed vector element type', () {
expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorInt2),
equals(ValueType.Int));
expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorInt3),
equals(ValueType.Int));
expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorInt4),
equals(ValueType.Int));
expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorUInt2),
equals(ValueType.UInt));
expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorUInt3),
equals(ValueType.UInt));
expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorUInt4),
equals(ValueType.UInt));
expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorFloat2),
equals(ValueType.Float));
expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorFloat3),
equals(ValueType.Float));
expect(ValueTypeUtils.fixedTypedVectorElementType(ValueType.VectorFloat4),
equals(ValueType.Float));
});
test('fixed typed vector element size', () {
expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorInt2),
equals(2));
expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorInt3),
equals(3));
expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorInt4),
equals(4));
expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorUInt2),
equals(2));
expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorUInt3),
equals(3));
expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorUInt4),
equals(4));
expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorFloat2),
equals(2));
expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorFloat3),
equals(3));
expect(ValueTypeUtils.fixedTypedVectorElementSize(ValueType.VectorFloat4),
equals(4));
});
test('packed type', () {
expect(
ValueTypeUtils.packedType(ValueType.Null, BitWidth.width8), equals(0));
expect(
ValueTypeUtils.packedType(ValueType.Null, BitWidth.width16), equals(1));
expect(
ValueTypeUtils.packedType(ValueType.Null, BitWidth.width32), equals(2));
expect(
ValueTypeUtils.packedType(ValueType.Null, BitWidth.width64), equals(3));
expect(
ValueTypeUtils.packedType(ValueType.Int, BitWidth.width8), equals(4));
expect(
ValueTypeUtils.packedType(ValueType.Int, BitWidth.width16), equals(5));
expect(
ValueTypeUtils.packedType(ValueType.Int, BitWidth.width32), equals(6));
expect(
ValueTypeUtils.packedType(ValueType.Int, BitWidth.width64), equals(7));
});
test('bit width', () {
expect(BitWidthUtil.width(0), BitWidth.width8);
expect(BitWidthUtil.width(-20), BitWidth.width8);
expect(BitWidthUtil.width(127), BitWidth.width8);
expect(BitWidthUtil.width(128), BitWidth.width16);
expect(BitWidthUtil.width(128123), BitWidth.width32);
expect(BitWidthUtil.width(12812324534), BitWidth.width64);
expect(BitWidthUtil.width(-127), BitWidth.width8);
expect(BitWidthUtil.width(-128), BitWidth.width16);
expect(BitWidthUtil.width(-12812324534), BitWidth.width64);
expect(BitWidthUtil.width(-0.1), BitWidth.width64);
expect(BitWidthUtil.width(0.25), BitWidth.width32);
});
test('padding size', () {
expect(BitWidthUtil.paddingSize(10, 8), 6);
expect(BitWidthUtil.paddingSize(10, 4), 2);
expect(BitWidthUtil.paddingSize(15, 4), 1);
expect(BitWidthUtil.paddingSize(15, 2), 1);
expect(BitWidthUtil.paddingSize(15, 1), 0);
expect(BitWidthUtil.paddingSize(16, 8), 0);
expect(BitWidthUtil.paddingSize(17, 8), 7);
});
}

154
dart/test/monster_test.fbs Normal file
View File

@@ -0,0 +1,154 @@
// test schema file
include "include_test1.fbs";
namespace MyGame;
table InParentNamespace {}
namespace MyGame.Example2;
table Monster {} // Test having same name as below, but in different namespace.
namespace MyGame.Example;
attribute "priority";
/// Composite components of Monster color.
enum Color:ubyte (bit_flags) {
Red = 0, // color Red = (1u << 0)
/// \brief color Green
/// Green is bit_flag with value (1u << 1)
Green,
/// \brief color Blue (1u << 3)
Blue = 3,
}
enum Race:byte {
None = -1,
Human = 0,
Dwarf,
Elf,
}
union Any { Monster, TestSimpleTableWithEnum, MyGame.Example2.Monster }
union AnyUniqueAliases { M: Monster, TS: TestSimpleTableWithEnum, M2: MyGame.Example2.Monster }
union AnyAmbiguousAliases { M1: Monster, M2: Monster, M3: Monster }
struct Test { a:short; b:byte; }
table TestSimpleTableWithEnum (csharp_partial, private) {
color: Color = Green;
}
struct Vec3 (force_align: 8) {
x:float;
y:float;
z:float;
test1:double;
test2:Color;
test3:Test;
}
struct Ability {
id:uint(key);
distance:uint;
}
struct StructOfStructs {
a: Ability;
b: Test;
c: Ability;
}
table Stat {
id:string;
val:long;
count:ushort (key);
}
table Referrable {
id:ulong(key, hash:"fnv1a_64");
}
/// an example documentation comment: "monster object"
table Monster {
pos:Vec3 (id: 0);
hp:short = 100 (id: 2);
mana:short = 150 (id: 1);
name:string (id: 3, key);
color:Color = Blue (id: 6);
inventory:[ubyte] (id: 5);
friendly:bool = false (deprecated, priority: 1, id: 4);
/// an example documentation comment: this will end up in the generated code
/// multiline too
testarrayoftables:[Monster] (id: 11);
testarrayofstring:[string] (id: 10);
testarrayofstring2:[string] (id: 28);
testarrayofbools:[bool] (id: 24);
testarrayofsortedstruct:[Ability] (id: 29);
enemy:MyGame.Example.Monster (id:12); // Test referring by full namespace.
test:Any (id: 8);
test4:[Test] (id: 9);
test5:[Test] (id: 31);
testnestedflatbuffer:[ubyte] (id:13, nested_flatbuffer: "Monster");
testempty:Stat (id:14);
testbool:bool (id:15);
testhashs32_fnv1:int (id:16, hash:"fnv1_32");
testhashu32_fnv1:uint (id:17, hash:"fnv1_32");
testhashs64_fnv1:long (id:18, hash:"fnv1_64");
testhashu64_fnv1:ulong (id:19, hash:"fnv1_64");
testhashs32_fnv1a:int (id:20, hash:"fnv1a_32");
testhashu32_fnv1a:uint (id:21, hash:"fnv1a_32", cpp_type:"Stat");
testhashs64_fnv1a:long (id:22, hash:"fnv1a_64");
testhashu64_fnv1a:ulong (id:23, hash:"fnv1a_64");
testf:float = 3.14159 (id:25);
testf2:float = 3 (id:26);
testf3:float (id:27);
flex:[ubyte] (id:30, flexbuffer);
vector_of_longs:[long] (id:32);
vector_of_doubles:[double] (id:33);
parent_namespace_test:InParentNamespace (id:34);
vector_of_referrables:[Referrable](id:35);
single_weak_reference:ulong(id:36, hash:"fnv1a_64", cpp_type:"ReferrableT");
vector_of_weak_references:[ulong](id:37, hash:"fnv1a_64", cpp_type:"ReferrableT");
vector_of_strong_referrables:[Referrable](id:38, cpp_ptr_type:"default_ptr_type"); //was shared_ptr
co_owning_reference:ulong(id:39, hash:"fnv1a_64", cpp_type:"ReferrableT", cpp_ptr_type:"naked"); //was shared_ptr as well
vector_of_co_owning_references:[ulong](id:40, hash:"fnv1a_64", cpp_type:"ReferrableT", cpp_ptr_type:"default_ptr_type", cpp_ptr_type_get:".get()"); //was shared_ptr
non_owning_reference:ulong(id:41, hash:"fnv1a_64", cpp_type:"ReferrableT", cpp_ptr_type:"naked", cpp_ptr_type_get:""); //was weak_ptr
vector_of_non_owning_references:[ulong](id:42, hash:"fnv1a_64", cpp_type:"ReferrableT", cpp_ptr_type:"naked", cpp_ptr_type_get:""); //was weak_ptr
any_unique:AnyUniqueAliases(id:44);
any_ambiguous:AnyAmbiguousAliases (id:46);
vector_of_enums:[Color] (id:47);
signed_enum:Race = None (id:48);
testrequirednestedflatbuffer:[ubyte] (id:49, nested_flatbuffer: "Monster");
scalar_key_sorted_tables:[Stat] (id: 50);
}
table TypeAliases {
i8:int8;
u8:uint8;
i16:int16;
u16:uint16;
i32:int32;
u32:uint32;
i64:int64;
u64:uint64;
f32:float32;
f64:float64;
v8:[int8];
vf64:[float64];
}
rpc_service MonsterStorage {
Store(Monster):Stat (streaming: "none");
Retrieve(Stat):Monster (streaming: "server", idempotent);
GetMaxHitPoint(Monster):Stat (streaming: "client");
GetMinMaxHitPoints(Monster):Stat (streaming: "bidi");
}
root_type Monster;
file_identifier "MONS";
file_extension "mon";

View File

@@ -1,24 +1,22 @@
// automatically generated by the FlatBuffers compiler, do not modify
// ignore_for_file: unused_import, unused_field, unused_local_variable
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable
library my_game.example2;
import 'dart:typed_data' show Uint8List;
import 'package:flat_buffers/flat_buffers.dart' as fb;
import 'include_test1_my_game.example2_generated.dart';
import 'include_test2_my_game.example2_generated.dart';
import './monster_test_my_game_generated.dart' as my_game;
import './monster_test_my_game.example_generated.dart' as my_game_example;
class Monster {
Monster._(this._bc, this._bcOffset);
factory Monster(List<int> bytes) {
fb.BufferContext rootRef = new fb.BufferContext.fromBytes(bytes);
final rootRef = fb.BufferContext.fromBytes(bytes);
return reader.read(rootRef, 0);
}
static const fb.Reader<Monster> reader = const _MonsterReader();
static const fb.Reader<Monster> reader = _MonsterReader();
final fb.BufferContext _bc;
final int _bcOffset;
@@ -28,6 +26,26 @@ class Monster {
String toString() {
return 'Monster{}';
}
MonsterT unpack() => MonsterT();
static int pack(fb.Builder fbBuilder, MonsterT? object) {
if (object == null) return 0;
return object.pack(fbBuilder);
}
}
class MonsterT implements fb.Packable {
@override
int pack(fb.Builder fbBuilder) {
fbBuilder.startTable(0);
return fbBuilder.endTable();
}
@override
String toString() {
return 'MonsterT{}';
}
}
class _MonsterReader extends fb.TableReader<Monster> {
@@ -35,7 +53,7 @@ class _MonsterReader extends fb.TableReader<Monster> {
@override
Monster createObject(fb.BufferContext bc, int offset) =>
new Monster._(bc, offset);
Monster._(bc, offset);
}
class MonsterObjectBuilder extends fb.ObjectBuilder {
@@ -44,19 +62,16 @@ class MonsterObjectBuilder extends fb.ObjectBuilder {
/// Finish building, and store into the [fbBuilder].
@override
int finish(
fb.Builder fbBuilder) {
assert(fbBuilder != null);
fbBuilder.startTable();
int finish(fb.Builder fbBuilder) {
fbBuilder.startTable(0);
return fbBuilder.endTable();
}
/// Convenience method to serialize to byte list.
@override
Uint8List toBytes([String fileIdentifier]) {
fb.Builder fbBuilder = new fb.Builder();
int offset = finish(fbBuilder);
return fbBuilder.finish(offset, fileIdentifier);
Uint8List toBytes([String? fileIdentifier]) {
final fbBuilder = fb.Builder(deduplicateTables: false);
fbBuilder.finish(finish(fbBuilder), fileIdentifier);
return fbBuilder.buffer;
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,24 +1,22 @@
// automatically generated by the FlatBuffers compiler, do not modify
// ignore_for_file: unused_import, unused_field, unused_local_variable
// ignore_for_file: unused_import, unused_field, unused_element, unused_local_variable
library my_game;
import 'dart:typed_data' show Uint8List;
import 'package:flat_buffers/flat_buffers.dart' as fb;
import 'include_test1_my_game_generated.dart';
import 'include_test2_my_game_generated.dart';
import './monster_test_my_game.example_generated.dart' as my_game_example;
import './monster_test_my_game.example2_generated.dart' as my_game_example2;
class InParentNamespace {
InParentNamespace._(this._bc, this._bcOffset);
factory InParentNamespace(List<int> bytes) {
fb.BufferContext rootRef = new fb.BufferContext.fromBytes(bytes);
final rootRef = fb.BufferContext.fromBytes(bytes);
return reader.read(rootRef, 0);
}
static const fb.Reader<InParentNamespace> reader = const _InParentNamespaceReader();
static const fb.Reader<InParentNamespace> reader = _InParentNamespaceReader();
final fb.BufferContext _bc;
final int _bcOffset;
@@ -28,6 +26,26 @@ class InParentNamespace {
String toString() {
return 'InParentNamespace{}';
}
InParentNamespaceT unpack() => InParentNamespaceT();
static int pack(fb.Builder fbBuilder, InParentNamespaceT? object) {
if (object == null) return 0;
return object.pack(fbBuilder);
}
}
class InParentNamespaceT implements fb.Packable {
@override
int pack(fb.Builder fbBuilder) {
fbBuilder.startTable(0);
return fbBuilder.endTable();
}
@override
String toString() {
return 'InParentNamespaceT{}';
}
}
class _InParentNamespaceReader extends fb.TableReader<InParentNamespace> {
@@ -35,7 +53,7 @@ class _InParentNamespaceReader extends fb.TableReader<InParentNamespace> {
@override
InParentNamespace createObject(fb.BufferContext bc, int offset) =>
new InParentNamespace._(bc, offset);
InParentNamespace._(bc, offset);
}
class InParentNamespaceObjectBuilder extends fb.ObjectBuilder {
@@ -44,19 +62,16 @@ class InParentNamespaceObjectBuilder extends fb.ObjectBuilder {
/// Finish building, and store into the [fbBuilder].
@override
int finish(
fb.Builder fbBuilder) {
assert(fbBuilder != null);
fbBuilder.startTable();
int finish(fb.Builder fbBuilder) {
fbBuilder.startTable(0);
return fbBuilder.endTable();
}
/// Convenience method to serialize to byte list.
@override
Uint8List toBytes([String fileIdentifier]) {
fb.Builder fbBuilder = new fb.Builder();
int offset = finish(fbBuilder);
return fbBuilder.finish(offset, fileIdentifier);
Uint8List toBytes([String? fileIdentifier]) {
final fbBuilder = fb.Builder(deduplicateTables: false);
fbBuilder.finish(finish(fbBuilder), fileIdentifier);
return fbBuilder.buffer;
}
}

Binary file not shown.

View File

@@ -29,6 +29,27 @@ Building should also produce two sample executables, `flatsamplebinary` and
*Note that you MUST be in the root of the FlatBuffers distribution when you
run 'flattests' or `flatsampletext`, or it will fail to load its files.*
### Make all warnings into errors
By default all Flatbuffers `cmake` targets are build with `-Werror` flag.
With this flag (or `/WX` for MSVC) C++ compiler will treat all warnings as errors.
Additionally `-Wall -pedantic -Wextra` (or `/W4` form MSVC) flags are set.
These flags minimize the number of possible defects in code and keep code highly portable.
Using these flags is considered good practice but sometimes it can break dependent projects
if a compiler is upgraded or a toolset is changed.
Usually, newer compiler versions add new compile-time diagnostics that were unavailable before.
These new diagnostic warnings could stop the build process if `-Werror` flag is set.
It is possible to cancel `warnings as errors` flag at `cmake` configuration stage using
`FLATBUFFERS_CXX_FLAGS` option. Compilation flags declared in `FLATBUFFERS_CXX_FLAGS` will be
appended to the project-level `CMAKE_CXX_FLAGS` variable.
Examples:
- GCC and Clang: `cmake . -D FLATBUFFERS_CXX_FLAGS="-Wno-error"`
- MSVC: `cmake . -D FLATBUFFERS_CXX_FLAGS="/WX-"`
- MSVC: `cmake . -D FLATBUFFERS_CXX_FLAGS="/Wv <compiler.version>"`
## Building with VCPKG
You can download and install flatbuffers using the [vcpkg](https://github.com/Microsoft/vcpkg/) dependency manager:

View File

@@ -7,7 +7,7 @@ The `flatcc` C schema compiler can generate code offline as well as
online via a C library. It can also generate buffer verifiers and fast
JSON parsers, printers.
Great care has been taken to ensure compatibily with the main `flatc`
Great care has been taken to ensure compatibility with the main `flatc`
project.
## General Documention

View File

@@ -79,7 +79,7 @@ Additional options:
- `--allow-non-utf8` : Pass non-UTF-8 input through parser and emit nonstandard
\x escapes in JSON. (Default is to raise parse error on non-UTF-8 input.)
- `--natural-utf8` : Output strings with UTF-8 as human-readable strings.
- `--natural-utf8` : Output strings with UTF-8 as human-readable strings.
By default, UTF-8 characters are printed as \uXXXX escapes."
- `--defaults-json` : Output fields whose value is equal to the default value
@@ -116,6 +116,9 @@ Additional options:
- `--gen-generated` : Add @Generated annotation for Java.
- `--gen-jvmstatic` : Add @JvmStatic annotation for Kotlin methods
in companion object for interop from Java to Kotlin.
- `--gen-all` : Generate not just code for the current schema files, but
for all files it includes as well. If the language uses a single file for
output (by default the case for C++ and JS), all code will end up in
@@ -134,25 +137,19 @@ Additional options:
std::string from Flatbuffers, but (char* + length). This allows efficient
construction of custom string types, including zero-copy construction.
- `--no-cpp-direct-copy` : Don't generate direct copy methods for C++
object-based API.
- `--cpp-std CPP_STD` : Generate a C++ code using features of selected C++ standard.
Supported `CPP_STD` values:
* `c++0x` - generate code compatible with old compilers (VS2010).
* `c++11` - use C++11 code generator (default);
* `c++0x` - generate code compatible with old compilers (VS2010),
* `c++11` - use C++11 code generator (default),
* `c++17` - use C++17 features in generated code (experimental).
- `--object-prefix` : Customise class prefix for C++ object-based API.
- `--object-suffix` : Customise class suffix for C++ object-based API.
- `--no-js-exports` : Removes Node.js style export lines (useful for JS)
- `--goog-js-export` : Uses goog.exportsSymbol and goog.exportsProperty
instead of Node.js style exporting. Needed for compatibility with the
Google closure compiler (useful for JS).
- `--es6-js-export` : Generates ECMAScript v6 style export definitions
instead of Node.js style exporting. Useful when integrating flatbuffers
with modern Javascript projects.
- `--go-namespace` : Generate the overrided namespace in Golang.
- `--go-import` : Generate the overrided import for flatbuffers in Golang.
@@ -200,18 +197,14 @@ Additional options:
- `--keep-prefix` : Keep original prefix of schema include statement.
- `--no-fb-impor` : Don't include flatbuffers import statement for TypeScript.
- `--no-ts-reexpor` : Don't re-export imported dependencies for TypeScript.
- `--short-name` : Use short function names for JS and TypeScript.
- `--reflect-types` : Add minimal type reflection to code generation.
- `--reflect-names` : Add minimal type/name reflection.
- `--root-type T` : Select or override the default root_type.
- `--require-explicit-ids` : When parsing schemas, require explicit ids (id: x).
- `--force-defaults` : Emit default values in binary output from JSON.
- `--force-empty` : When serializing from object API representation, force
@@ -220,5 +213,12 @@ Additional options:
- `--force-empty-vectors` : When serializing from object API representation, force
vectors to empty rather than null.
- `--flexbuffers` : Used with "binary" and "json" options, it generates
data using schema-less FlexBuffers.
- `--no-warnings` : Inhibit all warning messages.
- `--cs-global-alias` : Prepend `global::` to all user generated csharp classes and structs.
NOTE: short-form options for generators are deprecated, use the long form
whenever possible.

View File

@@ -56,7 +56,7 @@ For example, here is how you would read a FlatBuffer binary file in C++:
First, include the library and generated code. Then read the file into
a `char *` array, which you pass to `GetMonster()`.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
```cpp
#include "flatbuffers/flatbuffers.h"
#include "monster_test_generate.h"
#include <iostream> // C++ header file for printing
@@ -73,18 +73,18 @@ a `char *` array, which you pass to `GetMonster()`.
infile.close();
auto monster = GetMonster(data);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```
`monster` is of type `Monster *`, and points to somewhere *inside* your
buffer (root object pointers are not the same as `buffer_pointer` !).
buffer (root object pointers are not the same as `buffer_pointer` \!).
If you look in your generated header, you'll see it has
convenient accessors for all fields, e.g. `hp()`, `mana()`, etc:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
std::cout << "hp : " << monster->hp() << std::endl; // `80`
std::cout << "mana : " << monster->mana() << std::endl; // default value of `150`
std::cout << "name : " << monster->name()->c_str() << std::endl; // "MyMonster"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```cpp
std::cout << "hp : " << monster->hp() << std::endl; // '80'
std::cout << "mana : " << monster->mana() << std::endl; // default value of '150'
std::cout << "name : " << monster->name()->c_str() << std::endl; // "MyMonster"
```
*Note: That we never stored a `mana` value, so it will return the default.*
@@ -96,7 +96,7 @@ The following attributes are supported:
Specifically, `CreateXxxDirect` functions and `Pack` functions for object
based API (see below) will use `CreateSharedString` to create strings.
## Object based API. {#flatbuffers_cpp_object_based_api}
## Object based API {#flatbuffers_cpp_object_based_api}
FlatBuffers is all about memory efficiency, which is why its base API is written
around using as little as possible of it. This does make the API clumsier
@@ -109,7 +109,7 @@ construction, access and mutation.
To use:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
```cpp
// Autogenerated class from table Monster.
MonsterT monsterobj;
@@ -117,13 +117,13 @@ To use:
GetMonster(flatbuffer)->UnPackTo(&monsterobj);
// Update object directly like a C++ class instance.
cout << monsterobj->name; // This is now a std::string!
monsterobj->name = "Bob"; // Change the name.
cout << monsterobj.name; // This is now a std::string!
monsterobj.name = "Bob"; // Change the name.
// Serialize into new flatbuffer.
FlatBufferBuilder fbb;
fbb.Finish(Monster::Pack(fbb, &monsterobj));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```
The following attributes are specific to the object-based API code generation:
@@ -133,27 +133,30 @@ The following attributes are specific to the object-based API code generation:
This attribute changes the member declaration to use the type directly
rather than wrapped in a unique_ptr.
- `native_default`: "value" (on a field): For members that are declared
- `native_default("value")` (on a field): For members that are declared
"native_inline", the value specified with this attribute will be included
verbatim in the class constructor initializer list for this member.
- `native_custom_alloc`:"custom_allocator" (on a table or struct): When using the
- `native_custom_alloc("custom_allocator")` (on a table or struct): When using the
object-based API all generated NativeTables that are allocated when unpacking
your flatbuffer will use "custom allocator". The allocator is also used by
any std::vector that appears in a table defined with `native_custom_alloc`.
This can be used to provide allocation from a pool for example, for faster
unpacking when using the object-based API.
Minimal Example:
Minimal Example:
schema:
schema:
```cpp
table mytable(native_custom_alloc:"custom_allocator") {
...
}
```
with custom_allocator defined before flatbuffers.h is included, as:
with `custom_allocator` defined before `flatbuffers.h` is included, as:
```cpp
template <typename T> struct custom_allocator : public std::allocator<T> {
typedef T *pointer;
@@ -172,51 +175,77 @@ The following attributes are specific to the object-based API code generation:
}
custom_allocator() throw() {}
template <class U>
custom_allocator(const custom_allocator<U>&) throw() {}
};
```
- `native_type`' "type" (on a struct): In some cases, a more optimal C++ data
type exists for a given struct. For example, the following schema:
- `native_type("type")` (on a struct): In some cases, a more optimal C++ data
type exists for a given struct. For example, the following schema:
struct Vec2 {
x: float;
y: float;
}
```cpp
struct Vec2 {
x: float;
y: float;
}
```
generates the following Object-Based API class:
generates the following Object-Based API class:
struct Vec2T : flatbuffers::NativeTable {
float x;
float y;
};
```cpp
struct Vec2T : flatbuffers::NativeTable {
float x;
float y;
};
```
However, it can be useful to instead use a user-defined C++ type since it
can provide more functionality, eg.
However, it can be useful to instead use a user-defined C++ type since it
can provide more functionality, eg.
struct vector2 {
float x = 0, y = 0;
vector2 operator+(vector2 rhs) const { ... }
vector2 operator-(vector2 rhs) const { ... }
float length() const { ... }
// etc.
};
```cpp
struct vector2 {
float x = 0, y = 0;
vector2 operator+(vector2 rhs) const { ... }
vector2 operator-(vector2 rhs) const { ... }
float length() const { ... }
// etc.
};
```
The `native_type` attribute will replace the usage of the generated class
with the given type. So, continuing with the example, the generated
code would use |vector2| in place of |Vec2T| for all generated code.
The `native_type` attribute will replace the usage of the generated class
with the given type. So, continuing with the example, the generated
code would use `vector2` in place of `Vec2T` for all generated code of
the Object-Based API.
However, becuase the native_type is unknown to flatbuffers, the user must
provide the following functions to aide in the serialization process:
However, because the `native_type` is unknown to flatbuffers, the user must
provide the following functions to aide in the serialization process:
namespace flatbuffers {
FlatbufferStruct Pack(const native_type& obj);
native_type UnPack(const FlatbufferStruct& obj);
}
```cpp
namespace flatbuffers {
Vec2 Pack(const vector2& obj);
vector2 UnPack(const Vec2& obj);
}
```
Finally, the following top-level attribute
- `native_type_pack_name("name")` (on a struct when `native_type` is
specified, too): when you want to use the same `native_type` multiple times
(e. g. with different precision) you must make the names of the Pack/UnPack
functions unique, otherwise you will run into compile errors. This attribute
appends a name to the expected Pack/UnPack functions. So when you
specify `native_type_pack_name("Vec2")` in the above example you now need to
implement these serialization functions instead:
- `native_include`: "path" (at file level): Because the `native_type` attribute
```cpp
namespace flatbuffers {
Vec2 PackVec2(const vector2& obj);
vector2 UnPackVec2(const Vec2& obj);
}
```
Finally, the following top-level attributes:
- `native_include("path")` (at file level): Because the `native_type` attribute
can be used to introduce types that are unknown to flatbuffers, it may be
necessary to include "external" header files in the generated code. This
attribute can be used to directly add an #include directive to the top of
@@ -225,7 +254,7 @@ Finally, the following top-level attribute
- `force_align`: this attribute may not be respected in the object API,
depending on the aligned of the allocator used with `new`.
# External references.
# External references
An additional feature of the object API is the ability to allow you to load
multiple independent FlatBuffers, and have them refer to eachothers objects
@@ -244,7 +273,7 @@ same string (or hash).
When you call `UnPack` (or `Create`), you'll need a function that maps from
hash to the object (see `resolver_function_t` for details).
# Using different pointer types.
# Using different pointer types
By default the object tree is built out of `std::unique_ptr`, but you can
influence this either globally (using the `--cpp-ptr-type` argument to
@@ -255,13 +284,13 @@ you, so you'll have to manage their lifecycles manually. To reference the
pointer type specified by the `--cpp-ptr-type` argument to `flatc` from a
flatbuffer field set the `cpp_ptr_type` attribute to `default_ptr_type`.
# Using different string type.
# Using different string type
By default the object tree is built out of `std::string`, but you can
influence this either globally (using the `--cpp-str-type` argument to
`flatc`) or per field using the `cpp_str_type` attribute.
The type must support T::c_str(), T::length() and T::empty() as member functions.
The type must support `T::c_str()`, `T::length()` and `T::empty()` as member functions.
Further, the type must be constructible from std::string, as by default a
std::string instance is constructed and then used to initialize the custom
@@ -270,7 +299,7 @@ custom string types; the `--cpp-str-flex-ctor` argument to `flatc` or the
per field attribute `cpp_str_flex_ctor` can be used to change this behavior,
so that the custom string type is constructed by passing the pointer and
length of the FlatBuffers String. The custom string class will require a
constructor in the following format: custom_str_class(const char *, size_t).
constructor in the following format: `custom_str_class(const char *, size_t)`.
Please note that the character array is not guaranteed to be NULL terminated,
you should always use the provided size to determine end of string.
@@ -281,7 +310,7 @@ read and write data even if you don't know the exact format of a buffer, and
even allows you to change sizes of strings and vectors in-place.
The way this works is very elegant; there is actually a FlatBuffer schema that
describes schemas (!) which you can find in `reflection/reflection.fbs`.
describes schemas (\!) which you can find in `reflection/reflection.fbs`.
The compiler, `flatc`, can write out any schemas it has just parsed as a binary
FlatBuffer, corresponding to this meta-schema.
@@ -299,7 +328,7 @@ And example of usage, for the time being, can be found in
## Mini Reflection
A more limited form of reflection is available for direct inclusion in
generated code, which doesn't any (binary) schema access at all. It was designed
generated code, which doesn't do any (binary) schema access at all. It was designed
to keep the overhead of reflection as low as possible (on the order of 2-6
bytes per field added to your executable), but doesn't contain all the
information the (binary) schema contains.
@@ -390,9 +419,9 @@ is accessed, all reads will end up inside the buffer.
Each root type will have a verification function generated for it,
e.g. for `Monster`, you can call:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
```cpp
bool ok = VerifyMonsterBuffer(Verifier(buf, len));
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```
if `ok` is true, the buffer is safe to read.
@@ -458,15 +487,15 @@ Load text (either a schema or json) into an in-memory buffer (there is a
convenient `LoadFile()` utility function in `flatbuffers/util.h` if you
wish). Construct a parser:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
```cpp
flatbuffers::Parser parser;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```
Now you can parse any number of text files in sequence:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{.cpp}
```cpp
parser.Parse(text_file.c_str());
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```
This works similarly to how the command-line compiler works: a sequence
of files parsed by the same `Parser` object allow later files to
@@ -498,7 +527,7 @@ Creating a FlatBuffer is not thread safe. All state related to building
a FlatBuffer is contained in a FlatBufferBuilder instance, and no memory
outside of it is touched. To make this thread safe, either do not
share instances of FlatBufferBuilder between threads (recommended), or
manually wrap it in synchronisation primites. There's no automatic way to
manually wrap it in synchronisation primitives. There's no automatic way to
accomplish this, by design, as we feel multithreaded construction
of a single buffer will be rare, and synchronisation overhead would be costly.

Some files were not shown because too many files have changed in this diff Show More