From a269bca6133b3d93e3075399be4e8c578ad8f24e Mon Sep 17 00:00:00 2001 From: BruceSherwood Date: Tue, 5 May 2020 17:10:03 -0500 Subject: [PATCH] Bind class methods; classinstance.method => classinstance.method.bind(classinstance) --- lib/compiling/GScompiler.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/compiling/GScompiler.js b/lib/compiling/GScompiler.js index 96f43553..5b6634ae 100644 --- a/lib/compiling/GScompiler.js +++ b/lib/compiling/GScompiler.js @@ -633,6 +633,18 @@ where compile() was called, in untrusted/run.js. } } + start = 0 + const bindmethodpatt = new RegExp('(\\w+)\\.(\\w+)\\s*') + while (true) { // convert all classinstance.classmethod without parens to classinstance.classmethod.bind(classinstance) + m = line.slice(start).match(bindmethodpatt) + if (m === null) break + if (classinstances.indexOf(m[1]) >= 0 && classmethods.indexOf(m[2]) >= 0 && + (start+m.index+m[0].length >= line.length || line[start+m.index+m[0].length] != '(')) { + line = line.slice(0,start+m.index)+line.slice(start+m.index).replace(m[2], m[2]+'.bind('+m[1]+')') + start += m.index+m[0].length+m[1].length+7 + } else start += m.index+m[0].length + } + if (!declaration) { // not function declaration nor class declaration const leftpatt = new RegExp('^(\\w+)\\s*=\\s*') // find p = ..... var left = null, right = line