Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5c39276

Browse filesBrowse files
committed
fixed cancellation in modules and function context
1 parent a26f5d2 commit 5c39276
Copy full SHA for 5c39276

File tree

Expand file treeCollapse file tree

7 files changed

+35
-12
lines changed
Filter options
Expand file treeCollapse file tree

7 files changed

+35
-12
lines changed

‎package.json

Copy file name to clipboardExpand all lines: package.json
+1-1Lines 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

Copy file name to clipboardExpand all lines: src/common/utils.ts
+2-1Lines 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

Copy file name to clipboardExpand all lines: src/evaluator/evaluator.ts
+7-2Lines 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

Copy file name to clipboardExpand all lines: src/evaluator/evaluatorAsync.ts
+7-4Lines 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

Copy file name to clipboardExpand all lines: src/evaluator/scope.ts
+10-2Lines 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

Copy file name to clipboardExpand all lines: src/initialScope.ts
+1-1Lines 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

Copy file name to clipboardExpand all lines: src/interpreter.ts
+7-1Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.