Commit 95db63ab authored by jkummerow's avatar jkummerow Committed by Commit bot

[js2c] Fix ordering issue of TextMacro expansion

If a key is a substring of an earlier value, then the earlier value
will unintentionally be clobbered. For example with:
  macro SET_PRIVATE(obj, sym, val) = (obj[sym] = val);
  SET_PRIVATE(iterator, arrayIteratorObjectSymbol, object);
if the mapping is:
  {'val': 'object',
   'obj': 'iterator',
   'sym': 'arrayIteratorObjectSymbol'}
then 'obj' -> 'iterator' will clobber 'val' -> 'object', resulting in
'val' -> 'iteratorect'. To fix this, replace all substitutions
simultaneously.

Patch from Zoe Clifford <zoeclifford@google.com>

Review-Url: https://codereview.chromium.org/2249873004
Cr-Commit-Position: refs/heads/master@{#38665}
parent 888c67e9
......@@ -145,10 +145,12 @@ class TextMacro:
self.args = args
self.body = body
def expand(self, mapping):
result = self.body
for key, value in mapping.items():
result = result.replace(key, value)
return result
# Keys could be substrings of earlier values. To avoid unintended
# clobbering, apply all replacements simultaneously.
any_key_pattern = "|".join(re.escape(k) for k in mapping.iterkeys())
def replace(match):
return mapping[match.group(0)]
return re.sub(any_key_pattern, replace, self.body)
class PythonMacro:
def __init__(self, args, fun):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment