@@ -692,6 +692,7 @@ where compile() was called, in untrusted/run.js.
692
692
693
693
if ( ! foundfct ) {
694
694
// Check whether this line contains function calls inside strings, which will not need "await"
695
+ // const awaitpatt = new RegExp('\\W(\\w+)\\s*\\(') // find a function call
695
696
while ( true ) {
696
697
var m = line . slice ( start ) . match ( awaitpatt )
697
698
if ( m !== null ) {
@@ -876,7 +877,8 @@ where compile() was called, in untrusted/run.js.
876
877
arr += " Array.prototype['*']=function(r) {return __array_times_number(this, r)}\n" // multiplying Python list times number
877
878
if ( VPython_names . length <= 0 ) arr += vars
878
879
program = program . slice ( 0 , start ) + arr + program . slice ( start - 4 )
879
- } // end handle Python imports
880
+ } // end handle Python imports
881
+ //program = program.replace(new RegExp('\\(function\\(\\)', 'g'), '(async function()')
880
882
881
883
} else { // JavaScript
882
884
var prog = 'function __main__() {\n"use strict";\n'
@@ -922,6 +924,7 @@ where compile() was called, in untrusted/run.js.
922
924
923
925
// Prepend 'async ' to all user functions
924
926
while ( true ) {
927
+ // const asyncpatt1 = new RegExp('\\Wfunction\\s*(\\w+)\\s*\\(') // find "function f(...."
925
928
if ( options . lang != 'javascript' ) m = prog . slice ( start ) . match ( asyncpatt1 )
926
929
else m = prog . slice ( start ) . match ( jsasyncpatt1 )
927
930
if ( m === null ) break
@@ -939,23 +942,53 @@ where compile() was called, in untrusted/run.js.
939
942
940
943
// Prepend "await " to calls to user functions and the GlowScript async functions (all user functions are marked async).
941
944
start = initialstart
945
+ const noawait = [ 'if' , 'else' , 'defineProperties' , 'for' , 'while' , 'append' , 'function' , 'return' ,
946
+ 'vec' , 'vector' , 'cross' , 'dot' , 'add' , 'divide' , 'multiply' , 'sub' , 'mag' , 'mag2' , 'norm' ,
947
+ 'hat' , 'proj' , 'comp' , 'diff_angle' , 'radians' , 'degrees' , 'rotate' , 'pow' , 'radians' , 'degrees' ,
948
+ 'GS_power' , 'float' , 'format' , 'int' , 'len' , 'pow' , 'print' , 'range' , 'arange' , 'str' ,
949
+ 'abs' , 'sqrt' , 'sin' , 'cos' , 'tan' , 'asin' , 'acos' , 'atan' , 'atan2' , 'exp' , 'log' ,
950
+ 'ceil' , 'floor' , 'sign' , 'round' , 'max' , 'min' , 'random' , 'factorial' , 'combin' ,
951
+ 'box' , 'cylinder' , 'cone' , 'pyramid' , 'sphere' , 'simple_sphere' , 'arrow' , 'curve' , 'points' ,
952
+ 'paths' , 'shapes' , 'helix' , 'ring' , 'compound' , 'vertex' , 'triangle' , 'quad' , 'label' ,
953
+ 'distant_light' , 'attach_trail' , 'textures' , 'bumpmaps' , 'text' , 'wtext' , 'winput' ,
954
+ 'radio' , 'checkbox' , 'button' , 'slider' , 'menu' , 'input' , 'extrusion' ,
955
+ 'graph' , 'gdisplay' , 'series' , 'gcurve' , 'gdots' , 'gvbars' , 'ghbars' , 'ghistogram' ]
956
+ const noawait_method = [ 'rotate' , 'clone' , 'rgb_to_hsv' , 'hsv_to_rgb' , 'norm' , 'diff_angle' , 'project' ,
957
+ 'bind' , 'unbind' , 'trigger' ]
958
+
959
+ // VPython_import is the prefix of VPython objects, with value 'null' if no import statement
960
+ // const awaitpatt = new RegExp('\\W(\\w+)\\s*\\(') // find a function call
942
961
while ( true ) {
943
962
m = prog . slice ( start ) . match ( awaitpatt )
963
+ var period = false
944
964
if ( m === null ) break
965
+ period = ( prog [ start + m . index ] == '.' )
945
966
name = m [ 1 ]
946
- if ( fcts . indexOf ( name ) < 0 ) { // fcts is a list of user functions plus GlowScript async functions such as rate
947
- // If this is a class, delete the call to __init__ (we will insert this call after creating the class instance)
967
+
968
+ // If this is a class, delete the call to __init__ (we will insert this call after creating the class instance)
969
+ if ( ! period && classes . indexOf ( name ) >= 0 ) {
948
970
start += m . index + name . length + 1
949
- if ( classes . indexOf ( m [ 1 ] ) >= 0 ) {
950
- var lbrace = prog . slice ( start ) . search ( '{' )
951
- var rbrace = prog . slice ( start ) . search ( 'arguments' )
952
- prog = prog . slice ( 0 , start + lbrace + 1 ) + prog . slice ( start + 'arguments' . length + rbrace + 1 )
953
- }
971
+ var lbrace = prog . slice ( start ) . search ( '{' )
972
+ var rbrace = prog . slice ( start ) . search ( 'arguments' )
973
+ prog = prog . slice ( 0 , start + lbrace + 1 ) + prog . slice ( start + 'arguments' . length + rbrace + 1 )
954
974
continue
955
975
}
956
976
977
+ // Might start with 'RS_' or be string.format(...) or be color.gray(.7)
978
+ if ( name . slice ( 0 , 3 ) == 'RS_' || ( name == 'format' && period ) || ( name == 'gray' && period ) ) {
979
+ start += m . index + name . length + 1
980
+ continue
981
+ }
982
+
983
+ // If not a user function nor pause/waitfor/rate/sleep/read_local_file/get_library, and it
984
+ // is in the list of functions not to need await, don't prepend await to this function call:
985
+ if ( fcts . indexOf ( name ) < 0 && noawait . indexOf ( name ) >= 0 ) {
986
+ start += m . index + name . length + 1
987
+ continue
988
+ }
989
+
957
990
// find the beginning of ....f()
958
- ptr = start + m . index + 1
991
+ var ptr = start + m . index + 1
959
992
var brackets = 0
960
993
var parens = 0
961
994
while ( true ) {
@@ -1018,9 +1051,7 @@ where compile() was called, in untrusted/run.js.
1018
1051
while ( true ) {
1019
1052
var m = program . slice ( start ) . match ( kw )
1020
1053
if ( m == null ) break
1021
- if ( m [ 2 ] != 'this' ) {
1022
- program = program . slice ( 0 , start + m . index ) + 'await ' + program . slice ( start + m . index )
1023
- } else if ( fcts . indexOf ( m [ 3 ] ) >= 0 ) {
1054
+ if ( m [ 2 ] != 'this' || fcts . indexOf ( m [ 3 ] ) >= 0 ) {
1024
1055
program = program . slice ( 0 , start + m . index ) + 'await ' + program . slice ( start + m . index )
1025
1056
}
1026
1057
start += m . index + kw . length
@@ -1046,7 +1077,7 @@ where compile() was called, in untrusted/run.js.
1046
1077
// var p = program.split('\n')
1047
1078
// for (var i=0; i<p.length; i++) console.log(i, p[i])
1048
1079
// console.log('============================================================================')
1049
- // var i = program.search('scene = canvas() ')
1080
+ // var i = program.search('scene = ')
1050
1081
// console.log(program.slice(i))
1051
1082
//console.log(program)
1052
1083
return program
0 commit comments