@@ -203,12 +203,14 @@ where compile() was called, in untrusted/run.js.
203
203
const asyncpatt1 = new RegExp ( '\\Wfunction\\s*(\\w+)\\s*\\(' ) // find "function f(...."
204
204
const awaitpatt = new RegExp ( '\\W([ρσ\\w]+)[ ]*\\(' ) // find a function call
205
205
const filterpatt = new RegExp ( '\\Wfilter\\s*\\(\\s*(\\w+)+\\s*,' ) // find name of filter function
206
+ const pysortkey = new RegExp ( '\\.pysort\\s*\\(\\s*key\\s*=\\s*(\\w+)\\s*\\)' )
206
207
207
208
// fcts is a list of GlowScript functions that need "await" and of all user functions that are not
208
209
// classes, instances of classes, or methods of classes:
209
210
var vpfcts = [ 'pause' , 'waitfor' , 'capture' ] // These are preceded by "scene."
210
211
var vpwaits = [ 'rate' , 'sleep' , 'get_library' , 'read_local_file' ]
211
212
var fcts = [ ]
213
+ var nonasyncfcts = [ ] // a list of functions that should NOT be labeled async
212
214
213
215
// If ...name(... is encountered inside a string during the first pass, it is changed
214
216
// to ...name+string_insert+.... to prevent finding this apparent function or function call,
@@ -629,6 +631,10 @@ where compile() was called, in untrusted/run.js.
629
631
}
630
632
}
631
633
634
+ // In the form a.pysort(key=k), k must not be prepended with async, nor the call to k be prepended with await
635
+ m = line . match ( pysortkey )
636
+ if ( m !== null ) nonasyncfcts . push ( m [ 1 ] )
637
+
632
638
start = 0
633
639
const bindmethodpatt = new RegExp ( '(\\w+)\\.(\\w+)\\s*' )
634
640
while ( true ) { // convert all classinstance.classmethod without parens to classinstance.classmethod.bind(classinstance)
@@ -923,7 +929,8 @@ where compile() was called, in untrusted/run.js.
923
929
// console.log('classinstances', classinstances)
924
930
// console.log('classmethods', classmethods)
925
931
926
- // Prepend 'async ' to all user functions
932
+ // Prepend 'async ' to all user functions (but not if in nonasyncfcts)
933
+
927
934
let filter_functions = [ ]
928
935
while ( true ) { // locate Python filter(name, list)
929
936
m = prog . slice ( start ) . match ( filterpatt )
@@ -933,6 +940,7 @@ where compile() was called, in untrusted/run.js.
933
940
no_await . push ( m [ 1 ] ) // add this user function to the no_await list
934
941
start += m . index + m [ 0 ] . length
935
942
}
943
+
936
944
start = initialstart
937
945
while ( true ) {
938
946
// const asyncpatt1 = new RegExp('\\Wfunction\\s*(\\w+)\\s*\\(') // find "function f(...."
@@ -946,12 +954,19 @@ where compile() was called, in untrusted/run.js.
946
954
start += m [ 0 ] . length
947
955
continue
948
956
}
957
+ if ( nonasyncfcts . indexOf ( name ) >= 0 ) {
958
+ no_await . push ( name )
959
+ start += m [ 0 ] . length
960
+ continue
961
+ }
949
962
if ( filter_functions . indexOf ( name ) >= 0 ) start += m [ 0 ] . length
950
963
else {
951
964
prog = prog . slice ( 0 , start + m . index + 1 ) + 'async function ' + name + string_insert + prog . slice ( start + m . index + m [ 0 ] . length )
952
965
start += m . index + 'async function ' . length + m [ 1 ] . length + string_insert . length + 1
953
966
}
967
+
954
968
}
969
+
955
970
// ===================== End of prepending 'async' to user functions =====================
956
971
// Next, prepend "await " to calls to user functions and the GlowScript async functions (all user functions and class methods are marked async).
957
972
@@ -1245,8 +1260,8 @@ where compile() was called, in untrusted/run.js.
1245
1260
// console.log('classmethods', classmethods)
1246
1261
// console.log('============================================================================')
1247
1262
// let i = program.search('"2";')
1248
- // let i = program.search('async function __main__')
1249
- // console.log(program.slice(i))
1263
+ let i = program . search ( 'async function __main__' )
1264
+ console . log ( program . slice ( i ) )
1250
1265
// console.log(program)
1251
1266
return program
1252
1267
} // end of compile function
0 commit comments