Skip to content

Commit 5c39276

Browse files
committed
fixed cancellation in modules and function context
1 parent a26f5d2 commit 5c39276

File tree

7 files changed

+35
-12
lines changed

7 files changed

+35
-12
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jspython-interpreter",
3-
"version": "2.1.8",
3+
"version": "2.1.9",
44
"description": "JSPython is a javascript implementation of Python language that runs within web browser or NodeJS environment",
55
"keywords": [
66
"python",

src/common/utils.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Token } from "./token-types";
22

3-
export function parseDatetimeOrNull(value: string | Date): Date | null {
3+
export function parseDatetimeOrNull(value: string | number | Date): Date | null {
44
if (!value) { return null; }
5+
if(typeof value === 'number') { return new Date(value); }
56
if (value instanceof Date && !isNaN(value.valueOf())) { return value; }
67
// only string values can be converted to Date
78
if (typeof value !== 'string') { return null; }

src/evaluator/evaluator.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,14 @@ export class Evaluator {
2525

2626
for(let i = 0; i < ast.body.length; i++) {
2727
const node = ast.body[i];
28-
if(blockContext.cancel) {
28+
if(blockContext.cancellationToken.cancel) {
2929
const loc = node.loc || [];
30-
return `Cancelled. ${blockContext.moduleName}: ${loc[0]}, ${loc[1]}`;
30+
31+
if(!blockContext.cancellationToken.message){
32+
blockContext.cancellationToken.message = `Cancelled. ${blockContext.moduleName}: ${loc[0]}, ${loc[1]}`
33+
}
34+
35+
return blockContext.cancellationToken.message;
3136
}
3237

3338
if (node.type === 'comment') { continue; }

src/evaluator/evaluatorAsync.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import {
22
ArrowFuncDefNode,
33
AssignNode, AstBlock, AstNode, BinOpNode, BracketObjectAccessNode, ConstNode, CreateArrayNode,
44
CreateObjectNode, DotObjectAccessNode, ForNode, FuncDefNode, FunctionCallNode, FunctionDefNode, GetSingleVarNode,
5-
getStartLine,
6-
getTokenLoc,
75
IfNode, ImportNode, IsNullCoelsing, LogicalOpNode, OperationFuncs, Primitive, RaiseNode, ReturnNode, SetSingleVarNode, TryExceptNode, WhileNode
86
} from '../common';
97
import { JspyEvalError, JspyError, getImportType } from '../common/utils';
@@ -54,9 +52,14 @@ export class EvaluatorAsync {
5452

5553
for(let i = 0; i < ast.body.length; i++) {
5654
const node = ast.body[i];
57-
if(blockContext.cancel){
55+
if(blockContext.cancellationToken.cancel){
5856
const loc = node.loc || [];
59-
return `Cancelled. ${blockContext.moduleName}: ${loc[0]}, ${loc[1]}`;
57+
58+
if(!blockContext.cancellationToken.message){
59+
blockContext.cancellationToken.message = `Cancelled. ${blockContext.moduleName}: ${loc[0]}, ${loc[1]}`
60+
}
61+
62+
return blockContext.cancellationToken.message;
6063
}
6164

6265
if (node.type === 'comment') { continue; }

src/evaluator/scope.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11

2+
export interface CancellationToken {
3+
cancel?: boolean;
4+
message?: string;
5+
}
6+
27
export interface BlockContext {
38
moduleName: string;
49
blockScope: Scope;
10+
cancellationToken: CancellationToken;
511
returnCalled?: boolean;
612
breakCalled?: boolean;
713
continueCalled?: boolean;
814
returnObject?: any;
9-
cancel?: boolean;
1015
}
1116

1217
export function cloneContext(context: BlockContext): BlockContext {
1318
return {
1419
moduleName: context.moduleName,
15-
blockScope: context.blockScope.clone()
20+
blockScope: context.blockScope.clone(),
21+
// this instance should never change. Otherwise cancel won't work
22+
cancellationToken: context.cancellationToken
23+
1624
} as BlockContext;
1725
}
1826

src/initialScope.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { parseDatetimeOrNull } from "./common/utils";
22

33
export const INITIAL_SCOPE = {
44
jsPython(): string {
5-
return [`JSPython v2.1.8`, "(c) 2022 FalconSoft Ltd. All rights reserved."].join('\n')
5+
return `JSPython v2.1.9 (c) 2022 FalconSoft Ltd. All rights reserved.`;
66
},
77
dateTime: (str: number | string | any = null) => parseDatetimeOrNull(str) || new Date(),
88
range: range,

src/interpreter.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ export class Interpreter {
6262

6363
const blockContext = {
6464
moduleName: moduleName,
65+
cancellationToken: { cancel: false },
6566
blockScope: new Scope(scope)
6667
} as BlockContext;
6768

@@ -88,6 +89,7 @@ export class Interpreter {
8889
const evaluator = new EvaluatorAsync();
8990
const blockContext = {
9091
moduleName: moduleName,
92+
cancellationToken: { cancel: false },
9193
blockScope: new Scope(scope)
9294
} as BlockContext;
9395

@@ -109,7 +111,11 @@ export class Interpreter {
109111
.registerBlockContextFactory((moduleName, ast: AstBlock) => {
110112
// enrich context
111113
const newContext = this.assignImportContext(ast, scope);
112-
const moduleContext = { moduleName, blockScope: new Scope(newContext) }
114+
const moduleContext = {
115+
moduleName,
116+
blockScope: new Scope(newContext),
117+
cancellationToken: blockContext.cancellationToken
118+
};
113119
moduleContext.blockScope.set('printExecutionContext', () => console.log(moduleContext.blockScope.getScope()));
114120
moduleContext.blockScope.set('getExecutionContext', () => moduleContext.blockScope.getScope());
115121
return moduleContext;

0 commit comments

Comments
 (0)