Skip to content

Use aliased parser's own constructor for named parsers - #321

Open
keichi wants to merge 1 commit into
masterfrom
fix-alias-constructor
Open

keichi wants to merge 1 commit into
masterfrom
fix-alias-constructor

Conversation

@keichi

@keichi keichi commented Oct 18, 2026

Copy link
Copy Markdown
Owner

Problem

addAliasedCode emitted new constructorFn(), which resolves to the constructorFn parameter of the top-level compiled function (the top-level parser's constructor), not the constructor set via create() on the aliased parser.

Repro

class Foo { constructor() { this.isFoo = true; } }
Parser.start().create(Foo).uint8("v").namely("WithFoo");
new Parser().nest("n", { type: "WithFoo" }).parse(Buffer.from([1]));
// TypeError: constructorFn is not a constructor

If the top-level parser has its own create(Bar), the nested alias objects are wrongly constructed as Bar.

Fix

Reference the aliased parser's constructor through the imports mechanism: new ${ctx.addImport(this.constructorFn)}(). Added a regression test covering a plain top-level parser, a top-level parser with a different constructor, and a top-level aliased parser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant