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 3d61009

Browse files
committed
added last execution context and related cleanup
1 parent 5e1ba31 commit 3d61009

File tree

4 files changed

+35
-22
lines changed

4 files changed

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

‎src/evaluator/scope.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ export interface BlockContext {
88
blockScope: Scope
99
}
1010

11-
export function cloneContext(context: BlockContext): BlockContext{
11+
export function cloneContext(context: BlockContext): BlockContext{
1212
return {
1313
moduleName: context.moduleName,
1414
blockScope: context.blockScope.clone()
15-
} as BlockContext;
15+
} as BlockContext;
1616
}
1717

1818
export class Scope {
@@ -22,6 +22,10 @@ export class Scope {
2222
this.scope = { ...initialScope };
2323
}
2424

25+
getScope(): Record<string, unknown> {
26+
return this.scope;
27+
}
28+
2529
clone(): Scope {
2630
return new Scope(this.scope);
2731
}

‎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.0.16`, "(c) 2021 FalconSoft Ltd. All rights reserved."].join('\n')
5+
return [`JSPython v2.0.17`, "(c) 2021 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: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ export function jsPython(): Interpreter {
1414
}
1515

1616
export class Interpreter {
17-
private readonly initialScope: {[index: string]: any} = { ...INITIAL_SCOPE };
17+
private readonly initialScope: Record<string,unknown> = { ...INITIAL_SCOPE };
1818

19-
private globalScope: {[index: string]: any}={};
19+
private _lastExecutionContext: Record<string,unknown>|null=null;
2020

2121
private packageLoader?: PackageLoader;
2222
private fileLoader?: FileLoader;
@@ -26,17 +26,28 @@ export class Interpreter {
2626
return new Interpreter();
2727
}
2828

29-
jsPythonInfo(){
30-
return this.initialScope.jsPython();
29+
get initialExecutionContext(): Record<string, unknown> {
30+
return this.initialScope;
31+
}
32+
33+
get lastExecutionContext(): Record<string, unknown> | null {
34+
return this._lastExecutionContext;
35+
}
36+
37+
cleanUp() {
38+
this._lastExecutionContext = null;
39+
}
40+
41+
jsPythonInfo() {
42+
return INITIAL_SCOPE.jsPython();
3143
}
3244

3345
tokenize(script: string): Token[] {
3446
const tokenizer = new Tokenizer();
3547
return tokenizer.tokenize(script);
3648
}
3749

38-
39-
parse(script: string, moduleName: string='main.jspy'): AstBlock {
50+
parse(script: string, moduleName: string = 'main.jspy'): AstBlock {
4051
const tokenizer = new Tokenizer();
4152
const parser = new Parser();
4253
const jspyAst = parser.parse(tokenizer.tokenize(script), moduleName);
@@ -46,15 +57,16 @@ export class Interpreter {
4657
eval(codeOrAst: string | AstBlock, scope: Record<string, unknown> = {}
4758
, entryFunctionName: string = '', moduleName: string = 'main.jspy'): unknown {
4859
const ast = (typeof codeOrAst === 'string') ? this.parse(codeOrAst as string, moduleName) : codeOrAst as AstBlock;
49-
60+
5061
const blockContext = {
5162
moduleName: moduleName,
5263
blockScope: new Scope(scope)
5364
} as BlockContext;
5465

55-
blockContext.blockScope.set('printExecutionContext', () => console.log(blockContext.blockScope));
56-
blockContext.blockScope.set('getExecutionContext', () => blockContext.blockScope.clone());
57-
66+
blockContext.blockScope.set('printExecutionContext', () => console.log(blockContext.blockScope.getScope()));
67+
blockContext.blockScope.set('getExecutionContext', () => blockContext.blockScope.getScope());
68+
this._lastExecutionContext = blockContext.blockScope.getScope();
69+
5870
const result = new Evaluator().evalBlock(ast, blockContext);
5971
if (!entryFunctionName || !entryFunctionName.length) {
6072
return result;
@@ -75,9 +87,10 @@ export class Interpreter {
7587
moduleName: moduleName,
7688
blockScope: new Scope(scope)
7789
} as BlockContext;
78-
blockContext.blockScope.set('printExecutionContext', () => console.log(blockContext.blockScope));
79-
blockContext.blockScope.set('getExecutionContext', () => blockContext.blockScope.clone());
8090

91+
blockContext.blockScope.set('printExecutionContext', () => console.log(blockContext.blockScope.getScope()));
92+
blockContext.blockScope.set('getExecutionContext', () => blockContext.blockScope.getScope());
93+
this._lastExecutionContext = blockContext.blockScope.getScope();
8194

8295
const result = await evaluator.evalBlockAsync(ast, blockContext);
8396

@@ -103,16 +116,12 @@ export class Interpreter {
103116
context = (context && typeof context === 'object') ? context : {};
104117
context = await this.assignLegacyImportContext(ast, context);
105118

106-
this.globalScope = {
119+
constglobalScope = {
107120
...this.initialScope,
108121
...context
109122
} as Record<string, unknown>;
110123

111-
try {
112-
return this.evalAsync(ast, this.globalScope, entryFunctionName, moduleName);
113-
} catch (error) {
114-
throw error;
115-
}
124+
return await this.evalAsync(ast, globalScope, entryFunctionName, moduleName);
116125
}
117126

118127

0 commit comments

Comments
(0)

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