[Yunq] Factor out parsing most message fields to a shared method.

This commit is contained in:
Drew Galbraith 2023-11-10 12:51:23 -08:00
parent 0dacbb87dc
commit 83dbd18cb4
4 changed files with 40 additions and 51 deletions

View File

@ -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<ExtPointer>(offset + header_size + (8 * 0));
set_path(bytes.StringAt(offset + path_pointer.offset, path_pointer.length));
// Parse options.
auto options_pointer = bytes.At<ExtPointer>(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<uint64_t>(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<ExtPointer>(offset + header_size + (8 * 0));
set_path(bytes.StringAt(offset + path_pointer.offset, path_pointer.length));
// Parse options.
auto options_pointer = bytes.At<ExtPointer>(offset + header_size + (8 * 1))
auto options_pointer = bytes.At<ExtPointer>(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<uint64_t>(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<ExtPointer>(offset + header_size + (8 * 0));
set_path(bytes.StringAt(offset + path_pointer.offset, path_pointer.length));
// Parse attrs.
set_attrs(bytes.At<uint64_t>(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<uint64_t>(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<ExtPointer>(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<uint64_t>(offset + header_size + (8 * 1)));
// Parse mem_cap.
uint64_t mem_cap_ptr = bytes.At<uint64_t>(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 {

View File

@ -26,6 +26,8 @@ class OpenFileRequest {
glcr::String path_;
glcr::Vector<uint64_t> 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);
};

View File

@ -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<uint64_t>(offset + header_size + (8 * {{loop.index0}})));
{%- elif field.type == Type.I64 %}
set_{{field.name}}(bytes.At<int64_t>(offset + header_size + (8 * {{loop.index0}})));
{%- elif field.type == Type.STRING %}
auto {{field.name}}_pointer = bytes.At<ExtPointer>(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<ExtPointer>(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<uint64_t>(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<uint64_t>(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 {

View File

@ -40,5 +40,7 @@ class {{message.name}} {
{%- endif %}
{%- endfor %}
// Parses everything except for caps.
void ParseFromBytesInternal(const glcr::ByteBuffer&, uint64_t offset);
};
{%- endfor %}