Skip to content
Closed
Show file tree
Hide file tree
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
11 changes: 8 additions & 3 deletions src/Pointer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ class Pointer {
if (this.options.allowNull == null) { this.options.allowNull = true; }
if (this.options.nullValue == null) { this.options.nullValue = 0; }
if (this.options.lazy == null) { this.options.lazy = false; }
if (this.options.relativeTo) {
this.relativeToGetter = new Function('ctx', `return ctx.${this.options.relativeTo}`);
}
}

// this.options.relativeTo may be a string of properties
// 'foo'
// 'foo.bar'
// this property chain will be executed against `ctx`
relativeToGetter(ctx) {
return utils.getPropertyChain(ctx, this.options.relativeTo);
}

decode(stream, ctx) {
Expand Down
17 changes: 17 additions & 0 deletions src/VersionedStruct.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Struct = require('./Struct');
const utils = require('./utils');

class VersionedStruct extends Struct {
constructor(type, versions = {}) {
Expand All @@ -11,6 +12,22 @@ class VersionedStruct extends Struct {
}
}

versionGetter(parent) {
if (typeof this.type === 'string') {
// this.type may be a string of properties
// 'foo'
// 'foo.bar'
// this property chain will be executed against `parent`
return utils.getPropertyChain(parent, this.type);
} else {
return null;
}
}

versionSetter(parent, version) {
return utils.setPropertyChain(parent, this.type, version);
}

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

Expand Down
20 changes: 20 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,26 @@ exports.resolveLength = function(length, stream, parent) {
return res;
};

// 'chain' may be a string of properties
// 'foo'
// 'foo.bar'
// this property chain will be executed against `ref`
exports.getPropertyChain = function(ref, chain) {
return chain.split('.').reduce(function(obj, prop) {
return obj[prop];
}, ref);
}

exports.setPropertyChain = function(ref, chain, newValue) {
let props = chain.split('.');
let lastProp = props.pop();
let ref1 = props.reduce(function(obj, prop) {
return obj[prop];
}, ref);
ref1[lastProp] = newValue;
return newValue;
}

class PropertyDescriptor {
constructor(opts = {}) {
this.enumerable = true;
Expand Down