From 83dbd18cb4fa64ee929aed531f46621487909bdc Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Fri, 10 Nov 2023 12:51:23 -0800 Subject: [PATCH] [Yunq] Factor out parsing most message fields to a shared method. --- yunq/example/example.yunq.cpp | 42 ++++++++++++++-------------------- yunq/example/example.yunq.h | 4 ++++ yunq/message.cpp.jinja | 43 ++++++++++++++--------------------- yunq/message.h.jinja | 2 ++ 4 files changed, 40 insertions(+), 51 deletions(-) diff --git a/yunq/example/example.yunq.cpp b/yunq/example/example.yunq.cpp index 91b30f5..5e3fcff 100644 --- a/yunq/example/example.yunq.cpp +++ b/yunq/example/example.yunq.cpp @@ -28,31 +28,21 @@ void WriteHeader(glcr::ByteBuffer& bytes, uint64_t offset, uint32_t core_size, u } // namespace void OpenFileRequest::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset) { - CheckHeader(bytes); - // Parse path. - auto path_pointer = bytes.At(offset + header_size + (8 * 0)); - - set_path(bytes.StringAt(offset + path_pointer.offset, path_pointer.length)); - // Parse options. - auto options_pointer = bytes.At(offset + header_size + (8 * 1)) - - options_.Resize(options_pointer.length); - for (uint64_t i = offset + options_pointer.offset; - i < offset + options_pointer.offset + (sizeof(uint64_t) * options_pointer.length); - i += sizeof(uint64_t)) { - options_.PushBack(bytes.At(i)); - } - + ParseFromBytesInternal(bytes, offset); } void OpenFileRequest::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset, const glcr::CapBuffer& caps) { + ParseFromBytesInternal(bytes, offset); +} + +void OpenFileRequest::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) { CheckHeader(bytes); // Parse path. auto path_pointer = bytes.At(offset + header_size + (8 * 0)); set_path(bytes.StringAt(offset + path_pointer.offset, path_pointer.length)); // Parse options. - auto options_pointer = bytes.At(offset + header_size + (8 * 1)) + auto options_pointer = bytes.At(offset + header_size + (8 * 1)); options_.Resize(options_pointer.length); for (uint64_t i = offset + options_pointer.offset; @@ -61,6 +51,7 @@ void OpenFileRequest::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t off options_.PushBack(bytes.At(i)); } + } uint64_t OpenFileRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { @@ -132,19 +123,21 @@ uint64_t OpenFileRequest::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t off return next_extension; } void File::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset) { - CheckHeader(bytes); - // Parse path. - auto path_pointer = bytes.At(offset + header_size + (8 * 0)); - - set_path(bytes.StringAt(offset + path_pointer.offset, path_pointer.length)); - // Parse attrs. - set_attrs(bytes.At(offset + header_size + (8 * 1))); + ParseFromBytesInternal(bytes, offset); // Parse mem_cap. // FIXME: Implement in-buffer capabilities for inprocess serialization. set_mem_cap(0); } void File::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset, const glcr::CapBuffer& caps) { + ParseFromBytesInternal(bytes, offset); + // Parse mem_cap. + uint64_t mem_cap_ptr = bytes.At(offset + header_size + (8 * 2)); + + set_mem_cap(caps.At(mem_cap_ptr)); +} + +void File::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) { CheckHeader(bytes); // Parse path. auto path_pointer = bytes.At(offset + header_size + (8 * 0)); @@ -153,9 +146,8 @@ void File::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset, const // Parse attrs. set_attrs(bytes.At(offset + header_size + (8 * 1))); // Parse mem_cap. - uint64_t mem_cap_ptr = bytes.At(offset + header_size + (8 * 2)); + // Skip Cap. - set_mem_cap(caps.At(mem_cap_ptr)); } uint64_t File::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { diff --git a/yunq/example/example.yunq.h b/yunq/example/example.yunq.h index 7abd28e..3c162f0 100644 --- a/yunq/example/example.yunq.h +++ b/yunq/example/example.yunq.h @@ -26,6 +26,8 @@ class OpenFileRequest { glcr::String path_; glcr::Vector options_; + // Parses everything except for caps. + void ParseFromBytesInternal(const glcr::ByteBuffer&, uint64_t offset); }; class File { public: @@ -50,4 +52,6 @@ class File { uint64_t attrs_; z_cap_t mem_cap_; + // Parses everything except for caps. + void ParseFromBytesInternal(const glcr::ByteBuffer&, uint64_t offset); }; \ No newline at end of file diff --git a/yunq/message.cpp.jinja b/yunq/message.cpp.jinja index 296d0dc..3b227ee 100644 --- a/yunq/message.cpp.jinja +++ b/yunq/message.cpp.jinja @@ -30,39 +30,31 @@ void WriteHeader(glcr::ByteBuffer& bytes, uint64_t offset, uint32_t core_size, u {%- for message in messages %} void {{message.name}}::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset) { - CheckHeader(bytes); + ParseFromBytesInternal(bytes, offset); {%- for field in message.fields %} +{%- if field.type == Type.CAPABILITY %} // Parse {{field.name}}. -{%- if not field.repeated %} -{%- if field.type == Type.U64 %} - set_{{field.name}}(bytes.At(offset + header_size + (8 * {{loop.index0}}))); -{%- elif field.type == Type.I64 %} - set_{{field.name}}(bytes.At(offset + header_size + (8 * {{loop.index0}}))); -{%- elif field.type == Type.STRING %} - auto {{field.name}}_pointer = bytes.At(offset + header_size + (8 * {{loop.index0}})); - - set_{{field.name}}(bytes.StringAt(offset + {{field.name}}_pointer.offset, {{field.name}}_pointer.length)); -{%- elif field.type == Type.CAPABILITY %} // FIXME: Implement in-buffer capabilities for inprocess serialization. set_{{field.name}}(0); -{%- else %} - // TODO: Unimplemented parsing {{field.type}} {%- endif %} -{%- else %} - auto {{field.name}}_pointer = bytes.At(offset + header_size + (8 * {{loop.index0}})); - - {{field.name}}_.Resize({{field.name}}_pointer.length); - for (uint64_t i = offset + {{field.name}}_pointer.offset; - i < offset + {{field.name}}_pointer.offset + (sizeof({{field.cpp_type()}}) * {{field.name}}_pointer.length); - i += sizeof({{field.cpp_type()}})) { - {{field.name}}_.PushBack(bytes.At<{{field.cpp_type()}}>(i)); - } -{% endif %} {%- endfor %} } void {{message.name}}::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t offset, const glcr::CapBuffer& caps) { + ParseFromBytesInternal(bytes, offset); + +{%- for field in message.fields %} +{%- if field.type == Type.CAPABILITY %} + // Parse {{field.name}}. + uint64_t {{field.name}}_ptr = bytes.At(offset + header_size + (8 * {{loop.index0}})); + + set_{{field.name}}(caps.At({{field.name}}_ptr)); +{%- endif %} +{%- endfor %} +} + +void {{message.name}}::ParseFromBytesInternal(const glcr::ByteBuffer& bytes, uint64_t offset) { CheckHeader(bytes); {%- for field in message.fields %} @@ -77,9 +69,7 @@ void {{message.name}}::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t of set_{{field.name}}(bytes.StringAt(offset + {{field.name}}_pointer.offset, {{field.name}}_pointer.length)); {%- elif field.type == Type.CAPABILITY %} - uint64_t {{field.name}}_ptr = bytes.At(offset + header_size + (8 * {{loop.index0}})); - - set_{{field.name}}(caps.At({{field.name}}_ptr)); + // Skip Cap. {%- else %} // TODO: Unimplemented parsing {{field.type}} {%- endif %} @@ -94,6 +84,7 @@ void {{message.name}}::ParseFromBytes(const glcr::ByteBuffer& bytes, uint64_t of } {% endif %} {%- endfor %} + } uint64_t {{message.name}}::SerializeToBytes(glcr::ByteBuffer& bytes, uint64_t offset) const { diff --git a/yunq/message.h.jinja b/yunq/message.h.jinja index 058b970..93a29fc 100644 --- a/yunq/message.h.jinja +++ b/yunq/message.h.jinja @@ -40,5 +40,7 @@ class {{message.name}} { {%- endif %} {%- endfor %} + // Parses everything except for caps. + void ParseFromBytesInternal(const glcr::ByteBuffer&, uint64_t offset); }; {%- endfor %}