11import { AstBlock , ImportNode , Token } from './common' ;
2+ import { getImportType } from './common/utils' ;
23import { Evaluator } from './evaluator' ;
34import { EvaluatorAsync } from './evaluator/evaluatorAsync' ;
45import { BlockContext , Scope } from './evaluator/scope' ;
@@ -94,11 +95,15 @@ export class Interpreter {
9495 this . _lastExecutionContext = blockContext . blockScope . getScope ( ) ;
9596
9697 const result = await evaluator
98+ . registerJsonFileLoader ( async ( modulePath : string ) =>
99+ await ( this . moduleLoader ? this . moduleLoader ( modulePath )
100+ : Promise . reject ( 'ModuleLoader is not registered' )
101+ )
102+ )
97103 . registerModuleParser ( async ( modulePath ) => await this . moduleParser ( modulePath ) )
98104 . registerBlockContextFactory ( ( moduleName , ast : AstBlock ) => {
99- // this line will not be required when we have move package loaders to the evaluator
100- const newContext = this . assignLegacyImportContext ( ast , scope ) ;
101-
105+ // enrich context
106+ const newContext = this . assignImportContext ( ast , scope ) ;
102107 const moduleContext = { moduleName, blockScope : new Scope ( newContext ) }
103108 moduleContext . blockScope . set ( 'printExecutionContext' , ( ) => console . log ( moduleContext . blockScope . getScope ( ) ) ) ;
104109 moduleContext . blockScope . set ( 'getExecutionContext' , ( ) => moduleContext . blockScope . getScope ( ) ) ;
@@ -118,15 +123,15 @@ export class Interpreter {
118123 }
119124
120125 /**
121- * Compatibility method! Will be deprecated soon
126+ * Compatibility method (with v1). !
122127 */
123128 async evaluate ( script : string , context : object = { } , entryFunctionName : string = ''
124129 , moduleName : string = 'main.jspy' ) : Promise < any > {
125130 if ( ! script || ! script . length ) { return null ; }
126131 const ast = this . parse ( script , moduleName ) ;
127132
128133 context = ( context && typeof context === 'object' ) ? context : { } ;
129- context = this . assignLegacyImportContext ( ast , context ) ;
134+ context = this . assignImportContext ( ast , context ) ;
130135
131136 const globalScope = {
132137 ...this . initialScope ,
@@ -169,7 +174,7 @@ export class Interpreter {
169174 return scripts . indexOf ( `def ${ funcName } ` ) > - 1 ;
170175 }
171176
172- private assignLegacyImportContext ( ast : AstBlock , context : object ) : Record < string , unknown > {
177+ private assignImportContext ( ast : AstBlock , context : object ) : Record < string , unknown > {
173178
174179 const nodeToPackage = ( im : ImportNode ) : PackageToImport => {
175180 return {
@@ -182,7 +187,7 @@ export class Interpreter {
182187 const importNodes = ast . body . filter ( n => n . type === 'import' ) as ImportNode [ ] ;
183188
184189 const jsImport = importNodes
185- . filter ( im => ! im . module . name . startsWith ( '/' ) )
190+ . filter ( im => getImportType ( im . module . name ) === 'jsPackage' )
186191 . map ( im => nodeToPackage ( im ) ) ;
187192
188193 if ( jsImport . length && this . packageLoader ) {
0 commit comments