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: ...@@ -145,10 +145,12 @@ class TextMacro:
self.args = args self.args = args
self.body = body self.body = body
def expand(self, mapping): def expand(self, mapping):
result = self.body # Keys could be substrings of earlier values. To avoid unintended
for key, value in mapping.items(): # clobbering, apply all replacements simultaneously.
result = result.replace(key, value) any_key_pattern = "|".join(re.escape(k) for k in mapping.iterkeys())
return result def replace(match):
return mapping[match.group(0)]
return re.sub(any_key_pattern, replace, self.body)
class PythonMacro: class PythonMacro:
def __init__(self, args, fun): 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