Skip to content

Commit

Permalink
Merge pull request #82 from microavia/v1-js-implement
Browse files Browse the repository at this point in the history
JS port for V1 (draft)
  • Loading branch information
Roman- authored Apr 17, 2024
2 parents b76b2f4 + b2a8a34 commit 9f062d0
Show file tree
Hide file tree
Showing 34 changed files with 5,235 additions and 6,483 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# IntelliJ project files
.idea
.run

# Build files
*.pyc
Expand All @@ -14,6 +15,3 @@ dump.txt
tests/cpp/messgen
.done.*

tests/js/tmp
tests/js/coverage
tests/js/messgen
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ Each protocol should be placed in directory `base_dir/protocol`.
`protocol` can be single directory or multiple subdirectories, outer directories are used as namespace for generated messages, e.g. "my_company/core" or "my_company/the_product/protocol".

Message generator usage:
```
python3 messgen.py --basedir <base_dir> --protocol <protocol> --lang <lang> --outdir <out_dir> [--options key1=value1,key2=value2,...]
```bash
python3 messgen-generate.py --basedir <base_dir> --protocol <protocol> --lang <lang> --outdir <out_dir> [--options key1=value1,key2=value2,...]
```

Generated messages placed in `out_dir` directory.
Expand All @@ -50,16 +50,16 @@ Generated messages placed in `out_dir` directory.

Example for C++ messages generation:

```
python3 messgen.py --basedir ./base_dir --protocol my_namespace/my_protocol --lang cpp --outdir out/cpp --options cpp_standard=20
```bash
python3 messgen-generate.py --basedir ./base_dir --protocol my_namespace/my_protocol --lang cpp --outdir out/cpp --options cpp_standard=20
```

#### JSON

Example for JS messages generation:

```
python3 messgen.py --basedir ./base_dir --protocol my_namespace/my_protocol --lang json --outdir out/json
```bash
python3 messgen-generate.py --basedir ./base_dir --protocol my_namespace/my_protocol --lang json --outdir out/json
```
This command will generate json schema containing full protocol description.

Expand Down
27 changes: 23 additions & 4 deletions messgen/ts_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,27 @@
"float32": "number",
"float64": "bigint",
"string": "string",
}

ts_types_array = {
"int8": "Int8Array",
"uint8": "Uint8Array",
"int16": "Int16Array",
"uint16": "Uint16Array",
"int32": "Int32Array",
"uint32": "Uint32Array",
"int64": "BigInt64Array",
"uint64": "BigUint64Array",
"float32": "Float32Array",
"float64": "Float64Array",
}


def to_camelcase(str):
return ''.join(x for x in str.title().replace('_', '') if not x.isspace())

def format_type(f):

def format_type(f, use_typed_arrays=False):
t = ts_types_map.get(f["type"], f["type"])
f_type = t[0].lower() + t[1:]

Expand All @@ -29,18 +43,23 @@ def format_type(f):
f_type = din_type + "Message"

if (f["is_array"]):
f_type += "[]"
tArray = ts_types_array.get(f["type"])
if (tArray is not None and use_typed_arrays):
f_type = tArray
else:
f_type = f_type + "[]"

return f_type


class TsGenerator:
PROTO_TYPE_VAR_TYPE = "uint8"

def __init__(self, modules_map, data_types_map, module_sep, variables):
self.MODULE_SEP = module_sep
self._modules_map = modules_map
self._data_types_map = data_types_map

self._variables = variables

def generate(self, out_dir):
imports = []
Expand Down Expand Up @@ -80,7 +99,7 @@ def generate_interface(self, msg):
fields_p = []
for f in msg["fields"]:
f_name = f["name"]
f_type = format_type(f)
f_type = format_type(f, self._variables.get('typed_arrays','false') == 'true')
if f.get("descr") is not None:
fields_p.append(' /** %s */' % str(f.get("descr")))
fields_p.append(' %s: %s' % (f_name, f_type))
Expand Down
5 changes: 4 additions & 1 deletion port/js/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/dist
/node_modules
/coverage
/coverage

/tests/another_proto/
/tests/messgen/
5 changes: 0 additions & 5 deletions port/js/babel.config.js

This file was deleted.

115 changes: 115 additions & 0 deletions port/js/benchmarks/deserialize-variant.bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { bench } from 'vitest'

import {Buffer} from './deserialize-variant/messgen-old.js';
import { Buffer as BufferFromEntries} from './deserialize-variant/messgen-fromEntries.js';
import { Buffer as BufferPreBuild, Struct as StructPreBuild} from './deserialize-variant/messgen-pre-build.js';

import { bench, describe } from 'vitest'
import {Struct} from "../src/Struct.ts";

let srcStruct = new Struct({
id: 2,
fields: [
{ name: 'type_Int8', type: 'Int8' },
{ name: 'type_Uint8', type: 'Uint8' },
{ name: 'type_Int16', type: 'Int16' },
{ name: 'type_Uint16', type: 'Uint16' },
{ name: 'type_Int32', type: 'Int32' },
{ name: 'type_Uint32', type: 'Uint32' },
{ name: 'type_Int64', type: 'Int64' },
{ name: 'type_Uint64', type: 'Uint64' },
{ name: 'type_String', type: 'String' },
{ name: 'type_Double', type: 'Double' },
{ name: 'type_Char', type: 'Char' },
{ name: '_type_Int8', type: 'Int8' },
{ name: '_type_Uint8', type: 'Uint8' },
{ name: '_type_Int16', type: 'Int16' },
{ name: '_type_Uint16', type: 'Uint16' },
{ name: '_type_Int32', type: 'Int32' },
{ name: '_type_Uint32', type: 'Uint32' },
{ name: '_type_Int64', type: 'Int64' },
{ name: '_type_Uint64', type: 'Uint64' },
{ name: '_type_String', type: 'String' },
{ name: '_type_Double', type: 'Double' },
{ name: '_type_Char', type: 'Char' }
]
});

let srcStructPrebild = new StructPreBuild({
id: 2,
fields: [
{ name: 'type_Int8', type: 'Int8' },
{ name: 'type_Uint8', type: 'Uint8' },
{ name: 'type_Int16', type: 'Int16' },
{ name: 'type_Uint16', type: 'Uint16' },
{ name: 'type_Int32', type: 'Int32' },
{ name: 'type_Uint32', type: 'Uint32' },
{ name: 'type_Int64', type: 'Int64' },
{ name: 'type_Uint64', type: 'Uint64' },
{ name: 'type_String', type: 'String' },
{ name: 'type_Double', type: 'Double' },
{ name: 'type_Char', type: 'Char' },
{ name: '_type_Int8', type: 'Int8' },
{ name: '_type_Uint8', type: 'Uint8' },
{ name: '_type_Int16', type: 'Int16' },
{ name: '_type_Uint16', type: 'Uint16' },
{ name: '_type_Int32', type: 'Int32' },
{ name: '_type_Uint32', type: 'Uint32' },
{ name: '_type_Int64', type: 'Int64' },
{ name: '_type_Uint64', type: 'Uint64' },
{ name: '_type_String', type: 'String' },
{ name: '_type_Double', type: 'Double' },
{ name: '_type_Char', type: 'Char' }
]
});

let srcData = {
type_Int8: 8,
type_Uint8: 8,
type_Int16: 8,
type_Uint16: 8,
type_Int32: 8,
type_Uint32: 8,
type_Int64: BigInt(8),
type_Uint64: BigInt(8),
type_String: 'This is test string',
type_Double: -Math.PI,
type_Char: 'A',
_type_Int8: 8,
_type_Uint8: 8,
_type_Int16: 8,
_type_Uint16: 8,
_type_Int32: 8,
_type_Uint32: 8,
_type_Int64: BigInt(8),
_type_Uint64: BigInt(8),
_type_String: 'This is test string',
_type_Double: -Math.PI,
_type_Char: 'A'
};
srcData.__SIZE__ = Buffer.calcSize(Buffer.createValueArray(srcStruct.fields, srcData));
let b = Buffer.serializeObj(srcStruct.schema.fields, srcData);



describe('Buffer(b).deserialize ', () => {

beforeEach(() => {
srcData = {
...srcData,
type_Int32: srcData.type_Int32 + 1,
};
srcData.__SIZE__ = Buffer.calcSize(Buffer.createValueArray(srcStruct.fields, srcData));
b = Buffer.serializeObj(srcStruct.schema.fields, srcData);
})
bench('Object.fromEntries', () => {
let res = new BufferFromEntries(b).deserialize(srcStruct);
})
bench('pre build object Object', () => {
let res = new BufferPreBuild(b).deserialize(srcStructPrebild);
})
bench('mutation Object', () => {
let res = new Buffer(b).deserialize(srcStruct);
})

})
Loading

0 comments on commit 9f062d0

Please sign in to comment.