@@ -14,9 +14,9 @@ export function jsPython(): Interpreter {
1414}
1515
1616export 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+ const globalScope = {
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