Skip to content
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

Add undocumented/private API to hook into when a new frame is created. #701

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
12 changes: 11 additions & 1 deletion src/jsonata.js
Original file line number Diff line number Diff line change
Expand Up @@ -1837,7 +1837,7 @@ var jsonata = (function() {
*/
function createFrame(enclosingEnvironment) {
var bindings = {};
return {
const newFrame = {
bind: function (name, value) {
bindings[name] = value;
},
Expand All @@ -1857,6 +1857,16 @@ var jsonata = (function() {
ancestry: [ null ]
}
};

if (enclosingEnvironment) {
var framePushCallback = enclosingEnvironment.lookup(Symbol.for('jsonata.__createFrame_push'));
if(framePushCallback) {
framePushCallback(enclosingEnvironment, newFrame);
}
}


return newFrame
}

// Function registration
Expand Down
15 changes: 15 additions & 0 deletions test/implementation-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,21 @@ describe("Tests that include infinite recursion", () => {
});
});

describe("Tests that use internal frame push callbacks", () => {
describe("frame push callback bound to expression", function() {
it("calls callback when new frame created", function(done) {
var expr = jsonata("( )");
expr.assign(Symbol.for('jsonata.__createFrame_push'), function(parentEnv, newEnv) {
expect(parentEnv).to.not.equal(newEnv);
expect(parentEnv).to.include.keys(['lookup', 'bind']);
expect(newEnv).to.include.keys(['lookup', 'bind']);
done();
});
expr.evaluate();
});
});
});

/**
* Protect the process/browser from a runnaway expression
* i.e. Infinite loop (tail recursion), or excessive stack growth
Expand Down