Skip to content

Commit

Permalink
Handle possible function calls in params expressions
Browse files Browse the repository at this point in the history
Signed-off-by: Marco Lampacrescia <[email protected]>
  • Loading branch information
MarcoLm993 committed Sep 10, 2024
1 parent 930b81d commit fda1718
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion as2fm_common/src/as2fm_common/ecmascript_interpretation.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
Module for interpreting ecmascript.
"""

from typing import Dict, Optional, Union
import re
from typing import Dict, List, Optional, Union
from array import array

import js2py
Expand All @@ -27,6 +28,16 @@

BASIC_JS_TYPES = Union[int, float, bool]

Check warning on line 29 in as2fm_common/src/as2fm_common/ecmascript_interpretation.py

View workflow job for this annotation

GitHub Actions / as2fm_common ⏩ pylint

Type alias name "BASIC_JS_TYPES" doesn't conform to predefined naming style

# Functions that, if found in the expression, should be interpreted as math functions (Math.fun())

Check warning on line 31 in as2fm_common/src/as2fm_common/ecmascript_interpretation.py

View workflow job for this annotation

GitHub Actions / as2fm_common ⏩ pycodestyle

line too long (98 > 79 characters)
MATH_FUNCTIONS: List[str] = ["abs", "floor", "ceil", "cos", "sin", "log", "pow", "min", "max"]

Check warning on line 32 in as2fm_common/src/as2fm_common/ecmascript_interpretation.py

View workflow job for this annotation

GitHub Actions / as2fm_common ⏩ pycodestyle

line too long (94 > 79 characters)


def prepend_math_functions(expr: str) -> str:
"""Append 'Math.' to the functions in the provided expression and return a copy."""

Check warning on line 36 in as2fm_common/src/as2fm_common/ecmascript_interpretation.py

View workflow job for this annotation

GitHub Actions / as2fm_common ⏩ pycodestyle

line too long (87 > 79 characters)
for fun in MATH_FUNCTIONS:
expr = re.sub(rf"(^|[^a-zA-Z0-9_]){fun}\(", rf"\g<1>Math.{fun}(", expr)
return expr


def interpret_ecma_script_expr(
expr: str, variables: Optional[Dict[str, ValidTypes]] = None) -> object:

Check warning on line 43 in as2fm_common/src/as2fm_common/ecmascript_interpretation.py

View workflow job for this annotation

GitHub Actions / as2fm_common ⏩ pycodestyle

line too long (80 > 79 characters)
Expand All @@ -36,6 +47,7 @@ def interpret_ecma_script_expr(
:param expr: The ECMA script expression
:return: The interpreted object
"""
expr = prepend_math_functions(expr)
if variables is None:
variables = {}
context = js2py.EvalJs(variables)
Expand Down

0 comments on commit fda1718

Please sign in to comment.