Skip to content

Commit e233bef

Browse files
committedJun 8, 2024
Add undocumented API to hook into when a new frame is created.
This is helpful in scenarios where you wish to debug the environment behavior, or modify the env for advanced scenarios. As the callback is set via a Symbol, it can not be manipulated or read inside a query. See jsonata-js/jsonata/jsonata-js#700.
1 parent 9eda3c7 commit e233bef

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed
 

‎src/jsonata.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -1837,7 +1837,7 @@ var jsonata = (function() {
18371837
*/
18381838
function createFrame(enclosingEnvironment) {
18391839
var bindings = {};
1840-
return {
1840+
const newFrame = {
18411841
bind: function (name, value) {
18421842
bindings[name] = value;
18431843
},
@@ -1857,6 +1857,16 @@ var jsonata = (function() {
18571857
ancestry: [ null ]
18581858
}
18591859
};
1860+
1861+
if (enclosingEnvironment) {
1862+
var framePushCallback = enclosingEnvironment.lookup(Symbol.for('jsonata.__createFrame_push'));
1863+
if(framePushCallback) {
1864+
framePushCallback(enclosingEnvironment, newFrame);
1865+
}
1866+
}
1867+
1868+
1869+
return newFrame
18601870
}
18611871

18621872
// Function registration

‎test/implementation-tests.js

+15
Original file line numberDiff line numberDiff line change
@@ -1024,6 +1024,21 @@ describe("Tests that include infinite recursion", () => {
10241024
});
10251025
});
10261026

1027+
describe("Tests that use internal frame push callbacks", () => {
1028+
describe("frame push callback bound to expression", function() {
1029+
it("calls callback when new frame created", function(done) {
1030+
var expr = jsonata("( )");
1031+
expr.assign(Symbol.for('jsonata.__createFrame_push'), function(parentEnv, newEnv) {
1032+
expect(parentEnv).to.not.equal(newEnv);
1033+
expect(parentEnv).to.include.keys(['lookup', 'bind']);
1034+
expect(newEnv).to.include.keys(['lookup', 'bind']);
1035+
done();
1036+
});
1037+
expr.evaluate();
1038+
});
1039+
});
1040+
});
1041+
10271042
/**
10281043
* Protect the process/browser from a runnaway expression
10291044
* i.e. Infinite loop (tail recursion), or excessive stack growth

0 commit comments

Comments
 (0)
Please sign in to comment.