Skip to content

Commit e14b61f

Browse files
committed
Passing parameters to the function with eval and evalAsync
1 parent 1d31b43 commit e14b61f

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ <h4>JSPython development console</h4>
3232
a = 33
3333
b = 33
3434
if b > a:
35-
print("b is greater than a")
35+
return print("b is greater than a")
3636
elif a == b:
37-
print("a and b are equal")
37+
return print("a and b are equal")
3838
</div>
3939

4040
<button onclick="tokenize()">Tokenize</button>

src/interpreter.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,15 @@ describe('Interpreter', () => {
288288
expect(e.eval(script)).toBe(5);
289289
});
290290

291+
it('funcCall with params', () => {
292+
const script = `
293+
def times(a, b):
294+
return a * b
295+
`;
296+
expect(e.eval(script, {}, ['times', 2, 3])).toBe(6);
297+
});
298+
299+
291300
it('long comments issue', () => {
292301
const script = `
293302
async def f2():

src/interpreter.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class Interpreter {
5757
eval(
5858
codeOrAst: string | AstBlock,
5959
scope: Record<string, unknown> = {},
60-
entryFunctionName = '',
60+
entryFunctionName: string | [string, ...unknown[]] = '',
6161
moduleName = 'main.jspy'
6262
): unknown {
6363
const ast =
@@ -81,18 +81,20 @@ export class Interpreter {
8181
if (!entryFunctionName || !entryFunctionName.length) {
8282
return result;
8383
} else {
84-
const func = blockContext.blockScope.get(entryFunctionName);
84+
const funcName = Array.isArray(entryFunctionName)? entryFunctionName[0] : entryFunctionName as string
85+
const funcParams = Array.isArray(entryFunctionName)? entryFunctionName.slice(1) : []
86+
const func = blockContext.blockScope.get(funcName);
8587
if (typeof func !== 'function') {
8688
throw Error(`Function ${entryFunctionName} does not exists or not a function`);
8789
}
88-
return func();
90+
return func(...funcParams);
8991
}
9092
}
9193

9294
async evalAsync(
9395
codeOrAst: string | AstBlock,
9496
scope: Record<string, unknown> = {},
95-
entryFunctionName = '',
97+
entryFunctionName: string | [string, ...unknown[]] = '',
9698
moduleName = 'main.jspy',
9799
ctxInitialized?: (ctx: BlockContext) => void
98100
): Promise<unknown> {
@@ -146,11 +148,14 @@ export class Interpreter {
146148
if (!entryFunctionName || !entryFunctionName.length) {
147149
return result;
148150
} else {
149-
const func = blockContext.blockScope.get(entryFunctionName);
151+
const funcName = Array.isArray(entryFunctionName)? entryFunctionName[0] : entryFunctionName as string
152+
const funcParams = Array.isArray(entryFunctionName)? entryFunctionName.slice(1) : []
153+
154+
const func = blockContext.blockScope.get(funcName);
150155
if (typeof func !== 'function') {
151156
throw Error(`Function ${entryFunctionName} does not exists or not a function`);
152157
}
153-
return await func();
158+
return await func(...funcParams);
154159
}
155160
}
156161

@@ -160,7 +165,7 @@ export class Interpreter {
160165
async evaluate(
161166
script: string,
162167
context: Record<string, unknown> = {},
163-
entryFunctionName = '',
168+
entryFunctionName: string | [string, ...unknown[]] = '',
164169
moduleName = 'main.jspy',
165170
ctxInitialized?: (ctx: BlockContext) => void
166171
): Promise<unknown> {

src/interpreter.v1.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1376,7 +1376,8 @@ describe('Interpreter', () => {
13761376
expect(o[1][1]).toBe('ss22');
13771377
expect(o[1][2]).toBe(6);
13781378

1379-
expect(o[2][1].toISOString()).toBeDefined(); //.toBe(new Date('2020-03-07').toISOString());
1379+
expect(o[2][1]).toBeInstanceOf(Date);
1380+
expect((o[2][1] as Date).getFullYear()).toBe(2020);
13801381
expect(o[3].length).toBe(0);
13811382
});
13821383

0 commit comments

Comments
 (0)