@@ -51,7 +51,7 @@ export class EvaluatorAsync {
5151 if ( node . type === 'import' ) {
5252 const importNode = node as ImportNode ;
5353
54- if ( ! importNode . module . name . startsWith ( '/' ) /* || !importNode.module.name.endsWith('.jspy')*/ ) {
54+ if ( ! importNode . module . name . startsWith ( '/' ) /* || !importNode.module.name.endsWith('.jspy')*/ ) {
5555 // it is not JSPY imort. It is JS and should be handled externally
5656 continue ;
5757 }
@@ -60,12 +60,19 @@ export class EvaluatorAsync {
6060 throw new Error ( 'blockContextFactory is not initialized' ) ;
6161 }
6262
63- const moduleAst = await this . moduleParser ( importNode . module . name )
63+ const moduleAst = await this . moduleParser ( importNode . module . name ) ;
6464 const moduleBlockContext = this . blockContextFactory ( importNode . module . name , moduleAst ) ;
65- await this . evalBlockAsync ( moduleAst , moduleBlockContext )
65+ await this . evalBlockAsync ( moduleAst , moduleBlockContext ) ;
6666
67- blockContext . blockScope . set ( importNode . module . alias || this . defaultModuleName ( importNode . module . name ) , moduleBlockContext . blockScope . getScope ( ) )
67+ let scope = blockContext . blockScope . getScope ( ) ;
6868
69+ if ( ! importNode . parts ?. length ) {
70+ // if no parts, then we need to assign to a separate object
71+ scope = { } ;
72+ blockContext . blockScope . set ( importNode . module . alias || this . defaultModuleName ( importNode . module . name ) , scope ) ;
73+ }
74+
75+ this . assignFunctionsToScope ( scope , moduleBlockContext , moduleAst , importNode . parts ?. map ( p => p . name ) ) ;
6976 continue ;
7077 }
7178
@@ -102,6 +109,22 @@ export class EvaluatorAsync {
102109 return lastResult ;
103110 }
104111
112+ private assignFunctionsToScope ( scope : Record < string , unknown > , moduleBlockContext : BlockContext ,
113+ moduleAst : AstBlock , parts ?: string [ ] ) : void {
114+
115+ const funcs = moduleAst . funcs . filter ( f => ! parts || parts . indexOf ( f . funcAst ?. name ) >= 0 ) ;
116+
117+ for ( let i = 0 ; i < funcs . length ; i ++ ) {
118+ const funcDef = funcs [ i ] as FunctionDefNode ;
119+
120+ const invoker = ( funcDef . isAsync ) ?
121+ async ( ...args : unknown [ ] ) : Promise < unknown > => await this . jspyFuncInvokerAsync ( funcDef , moduleBlockContext , ...args )
122+ : ( ...args : unknown [ ] ) : unknown => new Evaluator ( ) . jspyFuncInvoker ( funcDef , moduleBlockContext , ...args ) ;
123+
124+ scope [ funcDef . funcAst . name ] = invoker ;
125+ }
126+ }
127+
105128 private defaultModuleName ( name : string ) : string {
106129 return name . substring ( name . lastIndexOf ( '/' ) + 1 , name . lastIndexOf ( '.' ) )
107130 }
@@ -156,7 +179,27 @@ export class EvaluatorAsync {
156179 return await func ( fps [ 0 ] , fps [ 1 ] , fps [ 2 ] , fps [ 3 ] , fps [ 4 ] , fps [ 5 ] , fps [ 6 ] , fps [ 7 ] , fps [ 8 ] , fps [ 9 ] ) ;
157180 }
158181
159- if ( fps . length > 10 ) {
182+ if ( fps . length === 11 ) {
183+ return await func ( fps [ 0 ] , fps [ 1 ] , fps [ 2 ] , fps [ 3 ] , fps [ 4 ] , fps [ 5 ] , fps [ 6 ] , fps [ 7 ] , fps [ 8 ] , fps [ 9 ] , fps [ 10 ] ) ;
184+ }
185+
186+ if ( fps . length === 12 ) {
187+ return await func ( fps [ 0 ] , fps [ 1 ] , fps [ 2 ] , fps [ 3 ] , fps [ 4 ] , fps [ 5 ] , fps [ 6 ] , fps [ 7 ] , fps [ 8 ] , fps [ 9 ] , fps [ 10 ] , fps [ 11 ] ) ;
188+ }
189+
190+ if ( fps . length === 13 ) {
191+ return await func ( fps [ 0 ] , fps [ 1 ] , fps [ 2 ] , fps [ 3 ] , fps [ 4 ] , fps [ 5 ] , fps [ 6 ] , fps [ 7 ] , fps [ 8 ] , fps [ 9 ] , fps [ 10 ] , fps [ 11 ] , fps [ 12 ] ) ;
192+ }
193+
194+ if ( fps . length === 14 ) {
195+ return await func ( fps [ 0 ] , fps [ 1 ] , fps [ 2 ] , fps [ 3 ] , fps [ 4 ] , fps [ 5 ] , fps [ 6 ] , fps [ 7 ] , fps [ 8 ] , fps [ 9 ] , fps [ 10 ] , fps [ 11 ] , fps [ 12 ] , fps [ 13 ] ) ;
196+ }
197+
198+ if ( fps . length === 15 ) {
199+ return await func ( fps [ 0 ] , fps [ 1 ] , fps [ 2 ] , fps [ 3 ] , fps [ 4 ] , fps [ 5 ] , fps [ 6 ] , fps [ 7 ] , fps [ 8 ] , fps [ 9 ] , fps [ 10 ] , fps [ 11 ] , fps [ 12 ] , fps [ 13 ] , fps [ 14 ] ) ;
200+ }
201+
202+ if ( fps . length > 15 ) {
160203 throw Error ( 'Function has too many parameters. Current limitation is 10' ) ;
161204 }
162205 }
0 commit comments