Skip to content

Disable context variables by default #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 18, 2021
Merged
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
7 changes: 0 additions & 7 deletions .prettierrc

This file was deleted.

10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -520,13 +520,17 @@ These options can be used in all parsers.
```

### Context variables
You can use some special fields while parsing to traverse your structure. These
context variables will be removed after the parsing process:
You can use some special fields while parsing to traverse your structure.
These context variables will be removed after the parsing process.
Note that this feature is turned off by default for performance reasons, and
you need to call `.useContextVars()` to enable it.

- `$parent` - This field references the parent structure. This variable will be
`null` while parsing the root structure.

```javascript
var parser = new Parser()
.useContextVars()
.nest("header", {
type: new Parser().uint32("length"),
})
Expand All @@ -542,6 +546,7 @@ context variables will be removed after the parsing process:

```javascript
var parser = new Parser()
.useContextVars()
.nest("header", {
type: new Parser().uint32("length"),
})
Expand All @@ -562,6 +567,7 @@ context variables will be removed after the parsing process:

```javascript
var parser = new Parser()
.useContextVars()
.nest("header", {
type: new Parser().uint32("length"),
})
Expand Down
113 changes: 58 additions & 55 deletions benchmark/bench.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,54 @@
var binary = require('binary');
var Benchmark = require('benchmark');
var bp = require('binparse').bp;
var Parser = require('binary-parser').Parser;
var Destruct = require('destruct-js');
const Struct = require('structron');
var binary = require("binary");
var Benchmark = require("benchmark");
var bp = require("binparse").bp;
var Parser = require("../dist/binary_parser").Parser;
var Destruct = require("destruct-js");
const Struct = require("structron");

var suite = new Benchmark.Suite;
var suite = new Benchmark.Suite();

// binparse
const PointParser = bp.object('Point', {
x: bp.lu16,
y: bp.lu16,
z: bp.lu16,
const PointParser = bp.object("Point", {
x: bp.lu16,
y: bp.lu16,
z: bp.lu16,
});

const PointsParser = bp.object('SimpleObject', {
length: bp.variable('len', bp.lu32),
points: bp.array('Points', PointParser, 'len'),
const PointsParser = bp.object("SimpleObject", {
length: bp.variable("len", bp.lu32),
points: bp.array("Points", PointParser, "len"),
});

// binary-parser
const Points = new Parser()
.uint32le('len')
.array('points', {
length: 'len',
type: new Parser()
.uint16le('x')
.uint16le('y')
.uint16le('z')
});
const Points = new Parser().uint32le("len").array("points", {
length: "len",
type: new Parser().uint16le("x").uint16le("y").uint16le("z"),
});

// destruct-js
const spec = new Destruct.Spec({mode: Destruct.Mode.LE});
spec.field('len', Destruct.UInt32)
.loop('points', (r) => r.len, new Destruct.Spec({mode: Destruct.Mode.LE})
.field('x', Destruct.UInt16)
.field('y', Destruct.UInt16)
.field('z', Destruct.UInt16));
const spec = new Destruct.Spec({ mode: Destruct.Mode.LE });
spec
.field("len", Destruct.UInt32)
.loop(
"points",
(r) => r.len,
new Destruct.Spec({ mode: Destruct.Mode.LE })
.field("x", Destruct.UInt16)
.field("y", Destruct.UInt16)
.field("z", Destruct.UInt16)
);

// structron
const PointsStruct = new Struct()
.addMember(Struct.TYPES.UINT_LE, 'len')
.addMember(Struct.TYPES.UINT_LE, "len")
.addArray(
new Struct().addMember(Struct.TYPES.USHORT_LE, 'x')
.addMember(Struct.TYPES.USHORT_LE, 'y')
.addMember(Struct.TYPES.USHORT_LE, 'z'),
'points', 0, 'len'
new Struct()
.addMember(Struct.TYPES.USHORT_LE, "x")
.addMember(Struct.TYPES.USHORT_LE, "y")
.addMember(Struct.TYPES.USHORT_LE, "z"),
"points",
0,
"len"
);

// Prepare input
Expand All @@ -54,29 +57,29 @@ var buf = Buffer.alloc(4 + n * 2 * 3);

buf.writeUInt32LE(n, 0);
for (var i = 0; i < n; i++) {
buf.writeUInt16LE(123, i * 6 + 0 + 4);
buf.writeUInt16LE(456, i * 6 + 2 + 4);
buf.writeUInt16LE(789, i * 6 + 4 + 4);
buf.writeUInt16LE(123, i * 6 + 0 + 4);
buf.writeUInt16LE(456, i * 6 + 2 + 4);
buf.writeUInt16LE(789, i * 6 + 4 + 4);
}

// Run benchmarks
suite
.add('binparse', function() {
const points = PointsParser.read(buf);
})
.add('binary-parser', function() {
const points = Points.parse(buf);
})
.add('destruct-js', function() {
const points = spec.read(buf);
})
.add('structron', function() {
const points = PointsStruct.read(buf);
})
.on('cycle', function(event) {
.add("binparse", function () {
const points = PointsParser.read(buf);
})
.add("binary-parser", function () {
const points = Points.parse(buf);
})
.add("destruct-js", function () {
const points = spec.read(buf);
})
.add("structron", function () {
const points = PointsStruct.read(buf);
})
.on("cycle", function (event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
.run({ 'async': true });
})
.on("complete", function () {
console.log("Fastest is " + this.filter("fastest").map("name"));
})
.run({ async: true });
46 changes: 23 additions & 23 deletions example/bmp.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Parser = require('../dist/binary_parser').Parser;
var Parser = require("../dist/binary_parser").Parser;

// C structure BITMAPFILEHEADER
// typedef struct tagBITMAPFILEHEADER {
Expand All @@ -9,15 +9,15 @@ var Parser = require('../dist/binary_parser').Parser;
// DWORD bfOffBits;
// } BITMAPFILEHEADER, *PBITMAPFILEHEADER;
var bmpFileHeader = new Parser()
.endianess('little')
.string('type', {
.endianess("little")
.string("type", {
length: 2,
assert: 'BM',
assert: "BM",
})
.uint32('size')
.uint16('reserved1')
.uint16('reserved2')
.uint32('offBits');
.uint32("size")
.uint16("reserved1")
.uint16("reserved2")
.uint32("offBits");

// C structure BITMAPINFOHEADER definition
// typedef struct tagBITMAPINFOHEADER {
Expand All @@ -34,27 +34,27 @@ var bmpFileHeader = new Parser()
// DWORD biClrImportant;
// } BITMAPINFOHEADER;
var bmpInfoHeader = new Parser()
.endianess('little')
.uint32('size')
.int32('width')
.int32('height')
.uint16('planes')
.uint16('bitCount')
.uint32('compression')
.uint32('sizeImage')
.int32('xPelsPerMeter')
.int32('yPelsPerMeter')
.uint32('clrUsed')
.uint32('clrImportant');
.endianess("little")
.uint32("size")
.int32("width")
.int32("height")
.uint16("planes")
.uint16("bitCount")
.uint32("compression")
.uint32("sizeImage")
.int32("xPelsPerMeter")
.int32("yPelsPerMeter")
.uint32("clrUsed")
.uint32("clrImportant");

var bmpFile = new Parser()
.nest('fileHeader', {
.nest("fileHeader", {
type: bmpFileHeader,
})
.nest('infoHeader', {
.nest("infoHeader", {
type: bmpInfoHeader,
});

require('fs').readFile('test.bmp', function (err, data) {
require("fs").readFile("test.bmp", function (err, data) {
console.log(bmpFile.parse(data));
});
70 changes: 35 additions & 35 deletions example/classfile.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
var Parser = require('../dist/binary_parser').Parser;
var Parser = require("../dist/binary_parser").Parser;

var ConstantClassInfo = Parser.start().uint16be('name_index');
var ConstantClassInfo = Parser.start().uint16be("name_index");

var ConstantFieldrefInfo = Parser.start()
.uint16be('class_index')
.uint16be('name_and_type_index');
.uint16be("class_index")
.uint16be("name_and_type_index");

var ConstantMethodrefInfo = Parser.start()
.uint16be('class_index')
.uint16be('name_and_type_index');
.uint16be("class_index")
.uint16be("name_and_type_index");

var ConstantInterfaceMethodrefInfo = Parser.start()
.uint16be('class_index')
.uint16be('name_and_type_index');
.uint16be("class_index")
.uint16be("name_and_type_index");

var ConstantStringInfo = Parser.start().uint16be('string_index');
var ConstantStringInfo = Parser.start().uint16be("string_index");

var ConstantIntegerInfo = Parser.start().uint32be('bytes');
var ConstantIntegerInfo = Parser.start().uint32be("bytes");

var ConstantFloatInfo = Parser.start().uint32be('bytes');
var ConstantFloatInfo = Parser.start().uint32be("bytes");

var ConstantLongInfo = Parser.start()
.uint32be('high_bytes')
.uint32be('low_bytes');
.uint32be("high_bytes")
.uint32be("low_bytes");

var ConstantDoubleInfo = Parser.start()
.uint32be('high_bytes')
.uint32be('low_bytes');
.uint32be("high_bytes")
.uint32be("low_bytes");

var ConstantNameAndTypeInfo = Parser.start()
.uint16be('name_index')
.uint16be('descriptor_index');
.uint16be("name_index")
.uint16be("descriptor_index");

var ConstantUtf8Info = Parser.start()
.uint16be('len')
.string('bytes', { length: 'len' });
.uint16be("len")
.string("bytes", { length: "len" });

var ConstantMethodHandleInfo = Parser.start()
.uint8('reference_kind')
.uint16be('reference_index');
.uint8("reference_kind")
.uint16be("reference_index");

var ConstantMethodTypeInfo = Parser.start().uint16be('descriptor_index');
var ConstantMethodTypeInfo = Parser.start().uint16be("descriptor_index");

var ConstantInvokeDynamicInfo = Parser.start()
.uint16be('bootstrap_method_attr_index')
.uint16be('name_and_type_index');
.uint16be("bootstrap_method_attr_index")
.uint16be("name_and_type_index");

var CpInfo = Parser.start()
.uint8('tag')
.choice('info', {
tag: 'tag',
.uint8("tag")
.choice("info", {
tag: "tag",
choices: {
7: ConstantClassInfo,
9: ConstantFieldrefInfo,
Expand All @@ -68,18 +68,18 @@ var CpInfo = Parser.start()
});

var ClassFile = Parser.start()
.endianess('big')
.uint32('magic', { assert: 0xcafebabe })
.uint16('minor_version')
.uint16('major_version')
.uint16('constant_pool_count')
.array('cp_info', {
.endianess("big")
.uint32("magic", { assert: 0xcafebabe })
.uint16("minor_version")
.uint16("major_version")
.uint16("constant_pool_count")
.array("cp_info", {
type: CpInfo,
length: function () {
return this.constant_pool_count - 1;
},
});

require('fs').readFile('Hello.class', function (err, data) {
console.log(require('util').inspect(ClassFile.parse(data), { depth: null }));
require("fs").readFile("Hello.class", function (err, data) {
console.log(require("util").inspect(ClassFile.parse(data), { depth: null }));
});
Loading