Repository navigation
Call wrapper functions with the current context as this - #339
Merged
Merged
Conversation
generateWrapper() emitted func.call(this, ...), so a wrapper saw whatever called the compiled parser as this, while formatters, assert and readUntil callbacks get the current parse context. Use ctx.generateVariable() like them, so a wrapper can read sibling fields (e.g. a key parsed before the wrapped data). Fixes keichi#263.
Owner
|
LGTM, thank you! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #263.
generateWrapper()emitsfunc.call(this, buffer). Inside the generated parser functionthisis whatever called the compiled parser, not the object being parsed. Formatters,assertandreadUntilcallbacks already receive the current context (ctx.generateVariable()), so a wrapper couldn't read sibling fields, e.g. a key parsed before the wrapped data:This change passes
ctx.generateVariable()asthis, as the issue suggests and as the other callbacks already do. In nestednest/arraycases the wrapper sees the object currently being built. WithuseContextVars()that object also has$parent/$root/$index, as for formatters. Arrow-function wrappers ignorethisand are unaffected.Test: new "should call wrapper with the current context as this" in
test/composite_parser.ts(runs for Buffer and Uint8Array). It fails without the change (a: 5instead of6) and passes with it.npm test: all passed.npm run formatandtsc --noEmitclean.