@@ -51,7 +51,7 @@ export class EvaluatorAsync {
51
51
if ( node . type === 'import' ) {
52
52
const importNode = node as ImportNode ;
53
53
54
- if ( ! importNode . module . name . startsWith ( '/' ) /* || !importNode.module.name.endsWith('.jspy')*/ ) {
54
+ if ( ! importNode . module . name . startsWith ( '/' ) /* || !importNode.module.name.endsWith('.jspy')*/ ) {
55
55
// it is not JSPY imort. It is JS and should be handled externally
56
56
continue ;
57
57
}
@@ -60,12 +60,19 @@ export class EvaluatorAsync {
60
60
throw new Error ( 'blockContextFactory is not initialized' ) ;
61
61
}
62
62
63
- const moduleAst = await this . moduleParser ( importNode . module . name )
63
+ const moduleAst = await this . moduleParser ( importNode . module . name ) ;
64
64
const moduleBlockContext = this . blockContextFactory ( importNode . module . name , moduleAst ) ;
65
- await this . evalBlockAsync ( moduleAst , moduleBlockContext )
65
+ await this . evalBlockAsync ( moduleAst , moduleBlockContext ) ;
66
66
67
- blockContext . blockScope . set ( importNode . module . alias || this . defaultModuleName ( importNode . module . name ) , moduleBlockContext . blockScope . getScope ( ) )
67
+ let scope = blockContext . blockScope . getScope ( ) ;
68
68
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 ) ) ;
69
76
continue ;
70
77
}
71
78
@@ -102,6 +109,22 @@ export class EvaluatorAsync {
102
109
return lastResult ;
103
110
}
104
111
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
+
105
128
private defaultModuleName ( name : string ) : string {
106
129
return name . substring ( name . lastIndexOf ( '/' ) + 1 , name . lastIndexOf ( '.' ) )
107
130
}
@@ -156,7 +179,27 @@ export class EvaluatorAsync {
156
179
return await func ( fps [ 0 ] , fps [ 1 ] , fps [ 2 ] , fps [ 3 ] , fps [ 4 ] , fps [ 5 ] , fps [ 6 ] , fps [ 7 ] , fps [ 8 ] , fps [ 9 ] ) ;
157
180
}
158
181
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 ) {
160
203
throw Error ( 'Function has too many parameters. Current limitation is 10' ) ;
161
204
}
162
205
}
0 commit comments