mirror of
https://github.com/matrix-construct/construct
synced 2024-12-30 17:34:04 +01:00
ircd::js: Checkpoint corrections to XDR layout.
This commit is contained in:
parent
3ba3ef46af
commit
b80468d6c8
2 changed files with 135 additions and 114 deletions
|
@ -32,20 +32,19 @@ struct xdr
|
|||
|
||||
const struct header *header;
|
||||
const struct sourcecode *sourcecode;
|
||||
const struct atom *name;
|
||||
const struct binding *binding;
|
||||
const struct sourcemap *sourcemap;
|
||||
const struct displayurl *displayurl;
|
||||
const struct filename *filename;
|
||||
const struct source *source;
|
||||
const uint32_t *body_scope_index;
|
||||
const struct bytecode *bytecode;
|
||||
const struct srcnote *srcnote;
|
||||
const struct atom *atom;
|
||||
const struct binding *binding;
|
||||
const struct consts *consts;
|
||||
const struct object *object;
|
||||
|
||||
void for_each_atom(const std::function<void (const struct atom &)> &) const;
|
||||
void for_each_name(const std::function<void (const struct atom &)> &) const;
|
||||
void for_each_binding(const std::function<void (const struct binding &)> &) const;
|
||||
void for_each_bytecode(const std::function<void (const struct bytecode &)> &) const;
|
||||
void for_each_const(const std::function<void (const struct consts &)> &) const;
|
||||
|
@ -54,10 +53,11 @@ struct xdr
|
|||
xdr(const const_buffer &);
|
||||
};
|
||||
|
||||
// Punnable
|
||||
struct xdr::header
|
||||
{
|
||||
uint32_t build_id_length;
|
||||
uint32_t build_id;
|
||||
uint32_t build_id_length; // Always 4 for IRCd build_id codes
|
||||
uint32_t build_id; // Custom IRCd 4 byte build_id code
|
||||
uint32_t length;
|
||||
uint32_t prologue_length;
|
||||
uint32_t version;
|
||||
|
@ -99,14 +99,16 @@ struct xdr::sourcemap
|
|||
uint8_t have;
|
||||
uint32_t len;
|
||||
const char16_t url[0];
|
||||
};
|
||||
}
|
||||
__attribute__((packed));
|
||||
|
||||
struct xdr::displayurl
|
||||
{
|
||||
uint8_t have;
|
||||
uint32_t len;
|
||||
const char16_t url[0];
|
||||
};
|
||||
}
|
||||
__attribute__((packed));
|
||||
|
||||
struct xdr::filename
|
||||
{
|
||||
|
@ -114,24 +116,6 @@ struct xdr::filename
|
|||
const char name[0];
|
||||
};
|
||||
|
||||
struct xdr::atom
|
||||
{
|
||||
uint32_t encoding : 1;
|
||||
uint32_t length : 31; union
|
||||
{
|
||||
const char latin1[0];
|
||||
const char16_t two_byte[0];
|
||||
};
|
||||
};
|
||||
static_assert(sizeof(struct xdr::atom) == 4, "");
|
||||
|
||||
struct xdr::binding
|
||||
{
|
||||
uint8_t aliased : 1;
|
||||
uint8_t kind : 7;
|
||||
};
|
||||
static_assert(sizeof(struct xdr::binding) == 1, "");
|
||||
|
||||
struct xdr::source
|
||||
{
|
||||
uint32_t start;
|
||||
|
@ -168,6 +152,18 @@ struct xdr::srcnote
|
|||
uint8_t note;
|
||||
};
|
||||
|
||||
struct xdr::atom
|
||||
{
|
||||
uint32_t encoding : 1; // 1 = latin1; 0 = ucs2
|
||||
uint32_t length : 31;
|
||||
union
|
||||
{
|
||||
const char latin1[0];
|
||||
const char16_t ucs2[0];
|
||||
};
|
||||
};
|
||||
static_assert(sizeof(struct xdr::atom) == 4, "");
|
||||
|
||||
struct xdr::object
|
||||
{
|
||||
struct block
|
||||
|
@ -202,12 +198,6 @@ struct xdr::object
|
|||
};
|
||||
};
|
||||
|
||||
size_t length(const struct xdr::object::block &);
|
||||
size_t length(const struct xdr::object::with &);
|
||||
size_t length(const struct xdr::object::function &);
|
||||
size_t length(const struct xdr::object::literal &);
|
||||
size_t length(const struct xdr::object &);
|
||||
|
||||
struct xdr::consts
|
||||
{
|
||||
uint32_t tag; union
|
||||
|
@ -221,6 +211,19 @@ struct xdr::consts
|
|||
|
||||
size_t length(const struct xdr::consts &consts);
|
||||
|
||||
struct xdr::binding
|
||||
{
|
||||
uint8_t aliased : 1;
|
||||
uint8_t kind : 7;
|
||||
};
|
||||
static_assert(sizeof(struct xdr::binding) == 1, "");
|
||||
|
||||
size_t length(const struct xdr::object::block &);
|
||||
size_t length(const struct xdr::object::with &);
|
||||
size_t length(const struct xdr::object::function &);
|
||||
size_t length(const struct xdr::object::literal &);
|
||||
size_t length(const struct xdr::object &);
|
||||
|
||||
} // namespace js
|
||||
} // namespace ircd
|
||||
|
||||
|
|
184
ircd/xdr.cc
184
ircd/xdr.cc
|
@ -81,6 +81,94 @@ ircd::js::xdr::xdr(const const_buffer &buf)
|
|||
|
||||
return reinterpret_cast<const struct sourcecode *>(this->header + 1);
|
||||
}()}
|
||||
,sourcemap{[this]
|
||||
{
|
||||
//assert(this->sourcecode->has_source);
|
||||
assert(!this->sourcecode->compressed_length);
|
||||
const auto *const ptr
|
||||
{
|
||||
reinterpret_cast<const uint8_t *>(this->sourcecode->code + this->sourcecode->length)
|
||||
};
|
||||
|
||||
return reinterpret_cast<const struct sourcemap *>(ptr);
|
||||
}()}
|
||||
,displayurl{[this]
|
||||
{
|
||||
if(!this->sourcemap->have)
|
||||
{
|
||||
const auto ptr(reinterpret_cast<const uint8_t *>(this->sourcemap) + 1);
|
||||
return reinterpret_cast<const struct displayurl *>(ptr);
|
||||
}
|
||||
|
||||
const auto ptr(reinterpret_cast<const uint8_t *>(this->sourcemap->url + this->sourcemap->len));
|
||||
return reinterpret_cast<const struct displayurl *>(ptr);
|
||||
}()}
|
||||
,filename{[this]
|
||||
{
|
||||
if(!this->displayurl->have)
|
||||
{
|
||||
const auto ptr(reinterpret_cast<const uint8_t *>(this->displayurl) + 1);
|
||||
return reinterpret_cast<const struct filename *>(ptr);
|
||||
}
|
||||
|
||||
const auto ptr(reinterpret_cast<const uint8_t *>(this->displayurl->url + this->displayurl->len));
|
||||
return reinterpret_cast<const struct filename *>(ptr);
|
||||
}()}
|
||||
,source{[this]
|
||||
{
|
||||
if(!this->filename->have)
|
||||
{
|
||||
const auto ptr(reinterpret_cast<const uint8_t *>(this->filename) + 1);
|
||||
return reinterpret_cast<const struct source *>(ptr);
|
||||
}
|
||||
|
||||
const auto ptr
|
||||
{
|
||||
reinterpret_cast<const uint8_t *>(this->filename->name + strlen(this->filename->name))
|
||||
};
|
||||
|
||||
return reinterpret_cast<const struct source *>(ptr);
|
||||
}()}
|
||||
,body_scope_index{[this]
|
||||
{
|
||||
return reinterpret_cast<const uint32_t *>(this->source + 1);
|
||||
}()}
|
||||
,bytecode{[this]
|
||||
{
|
||||
return reinterpret_cast<const struct bytecode *>(this->body_scope_index + 1);
|
||||
}()}
|
||||
,srcnote{[this]
|
||||
{
|
||||
const auto ptr
|
||||
{
|
||||
reinterpret_cast<const uint8_t *>(this->bytecode) + this->header->length
|
||||
};
|
||||
|
||||
return reinterpret_cast<const struct srcnote *>(ptr);
|
||||
}()}
|
||||
,atom{[this]
|
||||
{
|
||||
const auto ptr
|
||||
{
|
||||
this->srcnote + this->header->n_srcnotes
|
||||
};
|
||||
|
||||
return reinterpret_cast<const struct atom *>(ptr);
|
||||
}()}
|
||||
,consts{[this]
|
||||
{
|
||||
const auto *ptr
|
||||
{
|
||||
reinterpret_cast<const uint8_t *>(this->atom)
|
||||
};
|
||||
|
||||
for_each_atom([&ptr](const struct atom &atom)
|
||||
{
|
||||
ptr += sizeof(struct atom) + atom.length;
|
||||
});
|
||||
|
||||
return reinterpret_cast<const struct consts *>(ptr);
|
||||
}()}
|
||||
/*
|
||||
,name
|
||||
{
|
||||
|
@ -88,8 +176,6 @@ ircd::js::xdr::xdr(const const_buffer &buf)
|
|||
}
|
||||
,binding{[this]
|
||||
{
|
||||
std::cout << *this->header << std::endl;
|
||||
|
||||
const auto *ret
|
||||
{
|
||||
reinterpret_cast<const uint8_t *>(this->name)
|
||||
|
@ -102,69 +188,6 @@ ircd::js::xdr::xdr(const const_buffer &buf)
|
|||
|
||||
return reinterpret_cast<const struct binding *>(ret);
|
||||
}()}
|
||||
,sourcemap{[this]
|
||||
{
|
||||
const auto *ptr(reinterpret_cast<const uint8_t *>(this->sourcecode + 1) + this->sourcecode->compressed_length);
|
||||
return reinterpret_cast<const struct sourcemap *>(ptr);
|
||||
}()}
|
||||
,displayurl{[this]
|
||||
{
|
||||
if(!this->sourcemap->have)
|
||||
{
|
||||
const auto ptr(reinterpret_cast<const uint8_t *>(this->sourcemap) + 1);
|
||||
return reinterpret_cast<const struct displayurl *>(ptr);
|
||||
}
|
||||
|
||||
const auto ptr(reinterpret_cast<const uint8_t *>(this->sourcemap + 1) + this->sourcemap->len);
|
||||
return reinterpret_cast<const struct displayurl *>(ptr);
|
||||
}()}
|
||||
,filename{[this]
|
||||
{
|
||||
if(!this->displayurl->have)
|
||||
{
|
||||
const auto ptr(reinterpret_cast<const uint8_t *>(this->displayurl) + 1);
|
||||
return reinterpret_cast<const struct filename *>(ptr);
|
||||
}
|
||||
|
||||
const auto ptr(reinterpret_cast<const uint8_t *>(this->displayurl + 1) + this->displayurl->len);
|
||||
return reinterpret_cast<const struct filename *>(ptr);
|
||||
}()}
|
||||
,source{[this]
|
||||
{
|
||||
if(!this->filename->have)
|
||||
{
|
||||
const auto ptr(reinterpret_cast<const uint8_t *>(this->filename) + 1);
|
||||
return reinterpret_cast<const struct source *>(ptr);
|
||||
}
|
||||
|
||||
const auto ptr(reinterpret_cast<const uint8_t *>(this->filename + 1) + strlen(this->filename->name) + 1);
|
||||
return reinterpret_cast<const struct source *>(ptr);
|
||||
}()}
|
||||
,bytecode{[this]
|
||||
{
|
||||
const auto ptr(reinterpret_cast<const uint8_t *>(this->source + 1));
|
||||
return reinterpret_cast<const struct bytecode *>(ptr);
|
||||
}()}
|
||||
,srcnote{[this]
|
||||
{
|
||||
const auto ptr(reinterpret_cast<const uint8_t *>(this->bytecode + this->header->length));
|
||||
return reinterpret_cast<const struct srcnote *>(ptr);
|
||||
}()}
|
||||
,atom{[this]
|
||||
{
|
||||
const auto ptr(reinterpret_cast<const uint8_t *>(this->srcnote + this->header->n_srcnotes));
|
||||
return reinterpret_cast<const struct atom *>(ptr);
|
||||
}()}
|
||||
,consts{[this]
|
||||
{
|
||||
const auto *ptr(reinterpret_cast<const uint8_t *>(this->atom));
|
||||
for_each_atom([&ptr](const struct atom &atom)
|
||||
{
|
||||
ptr += sizeof(struct atom) + atom.length;
|
||||
});
|
||||
|
||||
return reinterpret_cast<const struct consts *>(ptr);
|
||||
}()}
|
||||
,object{[this]
|
||||
{
|
||||
const auto *ptr(reinterpret_cast<const uint8_t *>(this->consts));
|
||||
|
@ -198,7 +221,11 @@ ircd::js::xdr::for_each_const(const std::function<void (const struct consts &)>
|
|||
const
|
||||
{
|
||||
auto consts(this->consts);
|
||||
const auto *ptr(reinterpret_cast<const uint8_t *>(this->consts));
|
||||
const auto *ptr
|
||||
{
|
||||
reinterpret_cast<const uint8_t *>(this->consts)
|
||||
};
|
||||
|
||||
for(size_t i(0); i< header->n_consts; i++)
|
||||
{
|
||||
cb(*consts);
|
||||
|
@ -231,20 +258,6 @@ const
|
|||
cb(*binding);
|
||||
}
|
||||
|
||||
void
|
||||
ircd::js::xdr::for_each_name(const std::function<void (const struct atom &)> &cb)
|
||||
const
|
||||
{
|
||||
const uint8_t *ptr(reinterpret_cast<const uint8_t *>(name));
|
||||
const auto names(header->num_names());
|
||||
for(size_t i(0); i < names; ++i)
|
||||
{
|
||||
const auto atom(reinterpret_cast<const struct atom *>(ptr));
|
||||
cb(*atom);
|
||||
ptr += sizeof(struct atom) + atom->length;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
ircd::js::xdr::for_each_atom(const std::function<void (const struct atom &)> &cb)
|
||||
const
|
||||
|
@ -252,9 +265,14 @@ const
|
|||
const uint8_t *ptr(reinterpret_cast<const uint8_t *>(atom));
|
||||
for(size_t i(0); i < header->n_atoms; ++i)
|
||||
{
|
||||
const auto atom(reinterpret_cast<const struct atom *>(ptr));
|
||||
cb(*atom);
|
||||
ptr += sizeof(struct atom) + atom->length;
|
||||
const auto &atom
|
||||
{
|
||||
*reinterpret_cast<const struct atom *>(ptr)
|
||||
};
|
||||
|
||||
cb(atom);
|
||||
ptr += sizeof(struct atom) + atom.length;
|
||||
static_assert(sizeof(struct atom) == 4);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue