Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions src/VersionedStruct.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@ const Struct = require('./Struct');
class VersionedStruct extends Struct {
constructor(type, versions = {}) {
super();
this.type = type;
this.type = type;
this.versions = versions;
if (typeof this.type === 'string') {
this.versionGetter = new Function('parent', `return parent.${this.type}`);
this.versionSetter = new Function('parent', 'version', `return parent.${this.type} = version`);
}
}

decode(stream, parent, length = 0) {
decode(stream, parent, length = 0) {
const res = this._setup(stream, parent, length);

if (typeof this.type === 'string') {
res.version = this.versionGetter(parent);
res.version = parent[this.type];
} else {
res.version = this.type.decode(stream);
}
Expand All @@ -42,11 +38,11 @@ class VersionedStruct extends Struct {
}

size(val, parent, includePointers = true) {
let key, type;
let key, type;
if (!val) {
throw new Error('Not a fixed size');
}

const ctx = {
parent,
val,
Expand Down