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

Sign up
Appearance settings

Commit 8d98398

Browse files
committed
Made execution context cancellable
1 parent b19a671 commit 8d98398

File tree

6 files changed

+25
-7
lines changed

6 files changed

+25
-7
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.7",
3+
"version": "2.1.8",
44
"description": "JSPython is a javascript implementation of Python language that runs within web browser or NodeJS environment",
55
"keywords": [
66
"python",

‎src/evaluator/evaluator.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,13 @@ export class Evaluator {
2323
);
2424
}
2525

26-
for (const node of ast.body) {
26+
for(let i = 0; i < ast.body.length; i++) {
27+
const node = ast.body[i];
28+
if(blockContext.cancel) {
29+
const loc = node.loc || [];
30+
return `Cancelled. ${blockContext.moduleName}: ${loc[0]}, ${loc[1]}`;
31+
}
32+
2733
if (node.type === 'comment') { continue; }
2834
if (node.type === 'import') {
2935
// we can't use it here, because loader has to be promise

‎src/evaluator/evaluatorAsync.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@ export class EvaluatorAsync {
5252
newScope.set(funcDef.funcAst.name, invoker);
5353
}
5454

55-
for (const node of ast.body) {
55+
for(let i = 0; i < ast.body.length; i++) {
56+
const node = ast.body[i];
57+
if(blockContext.cancel){
58+
const loc = node.loc || [];
59+
return `Cancelled. ${blockContext.moduleName}: ${loc[0]}, ${loc[1]}`;
60+
}
61+
5662
if (node.type === 'comment') { continue; }
5763
if (node.type === 'import') {
5864
const importNode = node as ImportNode;

‎src/evaluator/scope.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface BlockContext {
66
breakCalled?: boolean;
77
continueCalled?: boolean;
88
returnObject?: any;
9+
cancel?: boolean;
910
}
1011

1112
export function cloneContext(context: BlockContext): BlockContext {

‎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.7`, "(c) 2021 FalconSoft Ltd. All rights reserved."].join('\n')
5+
return [`JSPython v2.1.8`, "(c) 2022 FalconSoft Ltd. All rights reserved."].join('\n')
66
},
77
dateTime: (str: number | string | any = null) => parseDatetimeOrNull(str) || new Date(),
88
range: range,

‎src/interpreter.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,19 @@ export class Interpreter {
8282
}
8383

8484
async evalAsync(codeOrAst: string | AstBlock, scope: Record<string, unknown> = {}
85-
, entryFunctionName: string = '', moduleName: string = 'main.jspy'): Promise<unknown> {
85+
, entryFunctionName: string = '', moduleName: string = 'main.jspy'
86+
, ctxInitialized?: (ctx: BlockContext) => void): Promise<unknown> {
8687
const ast = (typeof codeOrAst === 'string') ? this.parse(codeOrAst as string, moduleName) : codeOrAst as AstBlock;
8788
const evaluator = new EvaluatorAsync();
8889
const blockContext = {
8990
moduleName: moduleName,
9091
blockScope: new Scope(scope)
9192
} as BlockContext;
9293

94+
if(typeof ctxInitialized === 'function') {
95+
ctxInitialized(blockContext);
96+
}
97+
9398
blockContext.blockScope.set('printExecutionContext', () => console.log(blockContext.blockScope.getScope()));
9499
blockContext.blockScope.set('getExecutionContext', () => blockContext.blockScope.getScope());
95100
this._lastExecutionContext = blockContext.blockScope.getScope();
@@ -126,7 +131,7 @@ export class Interpreter {
126131
* Compatibility method (with v1). !
127132
*/
128133
async evaluate(script: string, context: object = {}, entryFunctionName: string = ''
129-
, moduleName: string = 'main.jspy'): Promise<any> {
134+
, moduleName: string = 'main.jspy',ctxInitialized?: (ctx: BlockContext)=>void): Promise<any> {
130135
if (!script || !script.length) { return null; }
131136
const ast = this.parse(script, moduleName);
132137

@@ -138,7 +143,7 @@ export class Interpreter {
138143
...context
139144
} as Record<string, unknown>;
140145

141-
return await this.evalAsync(ast, globalScope, entryFunctionName, moduleName);
146+
return await this.evalAsync(ast, globalScope, entryFunctionName, moduleName,ctxInitialized);
142147
}
143148

144149
registerPackagesLoader(loader: PackageLoader): Interpreter {

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /