Skip to content

Commit db83614

Browse files
committed
Enabling calling other functions when you are running with entryFunction
1 parent 33bd88c commit db83614

File tree

2 files changed

+39
-18
lines changed

2 files changed

+39
-18
lines changed

src/interpreter.spec.ts

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ describe('Interpreter', () => {
219219
});
220220

221221

222+
222223
it('set context variable (both ways)', async () => {
223224
const cxt = {
224225
obj: {
@@ -249,6 +250,19 @@ describe('Interpreter', () => {
249250
).toBe(75);
250251
});
251252

253+
it('entry function calling another function', async () => {
254+
expect(
255+
await Interpreter.create()
256+
.evaluate(
257+
`
258+
def func2():
259+
2 + 3
260+
261+
def func1():
262+
2 + 3 + func2()
263+
`, null, 'func1')).toBe(10);
264+
});
265+
252266
it('call a function from context', async () => {
253267
expect(
254268
await Interpreter.create()
@@ -288,6 +302,18 @@ describe('Interpreter', () => {
288302
expect(res).toBe(10);
289303
});
290304

305+
it('Function with promises and entry function', async () => {
306+
expect(
307+
await e.evaluate(`
308+
async def func2(p1):
309+
returnsPromise(p1)
310+
311+
async def func1():
312+
5 + func2(5)
313+
`, null, 'func1'
314+
)).toBe(10);
315+
});
316+
291317
it('Function with promises 2', async () => {
292318
const res = await e.evaluate([
293319
'async def func1(p1, p2):',
@@ -1230,7 +1256,7 @@ describe('Interpreter', () => {
12301256

12311257
it('null props', async () => {
12321258
expect(await e.evaluate(
1233-
`
1259+
`
12341260
x = {prop1: 25}
12351261
x?.prop1
12361262
`)).toBe(25)
@@ -1290,7 +1316,7 @@ describe('Interpreter', () => {
12901316
x = {}
12911317
x["t123"] = 33
12921318
x.t123
1293-
`
1319+
`
12941320
)).toBe(33)
12951321

12961322
expect(await e.evaluate(
@@ -1313,7 +1339,7 @@ describe('Interpreter', () => {
13131339
x.t123
13141340
13151341
foo()
1316-
`
1342+
`
13171343
)).toBe(33)
13181344

13191345
expect(await e.evaluate(
@@ -1335,7 +1361,7 @@ describe('Interpreter', () => {
13351361
`
13361362
x = {p1:33}
13371363
x["p1"]
1338-
`
1364+
`
13391365
)).toBe(33)
13401366

13411367
expect(await e.evaluate(
@@ -1356,7 +1382,7 @@ describe('Interpreter', () => {
13561382
x["p1"]
13571383
13581384
foo()
1359-
`
1385+
`
13601386
)).toBe(33)
13611387

13621388
expect(await e.evaluate(
@@ -1380,7 +1406,7 @@ describe('Interpreter', () => {
13801406
p1: p + "_" + 2
13811407
}
13821408
x.p1
1383-
`
1409+
`
13841410
)).toBe("t_2")
13851411

13861412
expect(await e.evaluate(
@@ -1391,7 +1417,7 @@ describe('Interpreter', () => {
13911417
p2: "some value"
13921418
}
13931419
x.p1
1394-
`
1420+
`
13951421
)).toBe("t_2")
13961422

13971423
})
@@ -1404,7 +1430,7 @@ describe('Interpreter', () => {
14041430
p1: p + "_" + 2 + returnsPromise(10)
14051431
}
14061432
x.p1
1407-
`
1433+
`
14081434
)).toBe("t_210")
14091435

14101436
expect(await e.evaluate(
@@ -1415,7 +1441,7 @@ describe('Interpreter', () => {
14151441
p2: "some value"
14161442
}
14171443
x.p1
1418-
`
1444+
`
14191445
)).toBe("t_210")
14201446

14211447
})

src/interpreter.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,12 @@ export class Interpreter {
124124
.setBlockRunnerFn((f, c, ...a) => codeEvaluator.invokePScriptFunction(f, c, ...a))
125125

126126
try {
127+
const retValue = await codeEvaluator.evalCodeBlockAsync(instuctionLines, blockContext);
128+
127129
if (!entryFunctionName || !entryFunctionName.length) {
128-
return await codeEvaluator.evalCodeBlockAsync(instuctionLines, blockContext);
130+
return retValue;
129131
} else {
130-
const startIndex = instuctionLines
131-
.findIndex(i => i.line.startsWith(`def ${entryFunctionName}(`) && i.line[i.line.length - 1] === ':');
132-
133-
if (startIndex >= 0) {
134-
const funcCodeLines = sliceBlock(instuctionLines, startIndex + 1);
135-
136-
return await codeEvaluator.evalCodeBlockAsync(funcCodeLines, blockContext);
137-
} else { return null; }
132+
return await blockContext.blockScope[entryFunctionName]()
138133
}
139134
} catch (error) {
140135
throw error;

0 commit comments

Comments
 (0)