Skip to content

Commit a04ace2

Browse files
committed
Fix passing of functions
1 parent 173618e commit a04ace2

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

lib/compiling/GScompiler.js

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,7 @@ where compile() was called, in untrusted/run.js.
692692

693693
if (!foundfct) {
694694
// 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
695696
while (true) {
696697
var m = line.slice(start).match(awaitpatt)
697698
if (m !== null) {
@@ -876,7 +877,8 @@ where compile() was called, in untrusted/run.js.
876877
arr += " Array.prototype['*']=function(r) {return __array_times_number(this, r)}\n" // multiplying Python list times number
877878
if (VPython_names.length <= 0) arr += vars
878879
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()')
880882

881883
} else { // JavaScript
882884
var prog = 'function __main__() {\n"use strict";\n'
@@ -922,6 +924,7 @@ where compile() was called, in untrusted/run.js.
922924

923925
// Prepend 'async ' to all user functions
924926
while (true) {
927+
// const asyncpatt1 = new RegExp('\\Wfunction\\s*(\\w+)\\s*\\(') // find "function f(...."
925928
if (options.lang != 'javascript') m = prog.slice(start).match(asyncpatt1)
926929
else m = prog.slice(start).match(jsasyncpatt1)
927930
if (m === null) break
@@ -939,23 +942,53 @@ where compile() was called, in untrusted/run.js.
939942

940943
// Prepend "await " to calls to user functions and the GlowScript async functions (all user functions are marked async).
941944
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
942961
while (true) {
943962
m = prog.slice(start).match(awaitpatt)
963+
var period = false
944964
if (m === null) break
965+
period = (prog[start+m.index] == '.')
945966
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) {
948970
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)
954974
continue
955975
}
956976

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+
957990
// find the beginning of ....f()
958-
ptr = start+m.index+1
991+
var ptr = start+m.index+1
959992
var brackets = 0
960993
var parens = 0
961994
while (true) {
@@ -1018,9 +1051,7 @@ where compile() was called, in untrusted/run.js.
10181051
while (true) {
10191052
var m = program.slice(start).match(kw)
10201053
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) {
10241055
program = program.slice(0,start+m.index)+'await '+program.slice(start+m.index)
10251056
}
10261057
start += m.index+kw.length
@@ -1046,7 +1077,7 @@ where compile() was called, in untrusted/run.js.
10461077
// var p = program.split('\n')
10471078
// for (var i=0; i<p.length; i++) console.log(i, p[i])
10481079
// console.log('============================================================================')
1049-
// var i = program.search('scene = canvas()')
1080+
// var i = program.search('scene = ')
10501081
// console.log(program.slice(i))
10511082
//console.log(program)
10521083
return program

0 commit comments

Comments
 (0)