Skip to content

Commit 64b48c5

Browse files
committed
Fixed problem on returning from a function to the caller's context.
1 parent d146e9a commit 64b48c5

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

runner.html

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,24 @@
1515
<script src="src/jazz.js"></script>
1616

1717
<script type="jazz">
18-
class Person {
19-
def greet() {
20-
log "hi"
18+
person = (_name) {
19+
priv = () {
20+
log "ha!"
21+
}
22+
23+
{
24+
name = _name
25+
talk = () {
26+
priv()
27+
log "hihi"
28+
log "my name is " + name
29+
}
2130
}
2231
}
23-
p = Person.new()
24-
p.greet()
32+
33+
p = person("bruno")
34+
p.talk()
35+
2536
</script>
2637
f = () {
2738
x = 1

spec/jazz.function.spec.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,5 +111,34 @@ describe("Jazz interpreter for functions", function () {
111111
'f()');
112112
expect(console.content).toEqual("1\n2");
113113
});
114+
115+
it("should return to the function's caller's context", function () {
116+
var code =
117+
'f = () {\n' +
118+
' x = 1\n' +
119+
' () {\n' +
120+
' log x\n' +
121+
' }\n' +
122+
'}\n' +
123+
'k = f()\n' +
124+
'log x';
125+
expect(function () { jazz.execute(code); }).toThrow("Unknown identifier: x");
126+
});
127+
128+
it("should return to the function's caller's context - more levels", function () {
129+
jazz.execute(
130+
'f = () {\n' +
131+
' g = () {\n' +
132+
' log 2\n' +
133+
' }\n' +
134+
' h = () {\n' +
135+
' g()\n' +
136+
' log 3\n' +
137+
' }\n' +
138+
'}\n' +
139+
'f()()\n' +
140+
'log 4');
141+
expect(console.content).toEqual("2\n3\n4");
142+
});
114143

115144
});

src/functionParser.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ jazz.FunctionParser = function (lexer, runtime, expressionParser) {
5252
functionParentContext = _function.context.parent;
5353

5454
_function.invoke = function (receiver, args) {
55+
var callingContext = runtime.currentContext;
5556
runtime.currentContext = _function.context;
5657
util.each(_function.params, function (param, index) {
5758
runtime.currentContext.add({
@@ -66,7 +67,7 @@ jazz.FunctionParser = function (lexer, runtime, expressionParser) {
6667
returnValue = expression();
6768
expression = runtime.currentContext.removeExpression();
6869
}
69-
runtime.currentContext = _function.context.parent;
70+
runtime.currentContext = callingContext;
7071
return returnValue;
7172
}
7273
return _function;

0 commit comments

Comments
 (0)