[jsinterp] Calling field and test

This commit is contained in:
sulyi 2016-11-24 22:33:30 +01:00
parent cc895cd712
commit 8c87a18029
2 changed files with 9 additions and 8 deletions

View File

@ -111,7 +111,8 @@ class TestJSInterpreter(unittest.TestCase):
function z() { return y(3); } function z() { return y(3); }
''') ''')
self.assertEqual(jsi.call_function('z'), 5) self.assertEqual(jsi.call_function('z'), 5)
jsi = JSInterpreter('function w(a) { return a.split(""); }', objects={'a': 'abc'})
self.assertEqual(jsi.call_function('w'), ["a", "b", "c"])
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -34,9 +34,9 @@ _ASSIGN_OPERATORS_RE = r'|'.join(re.escape(op) for op, opfunc in _ASSIGN_OPERATO
_NAME_RE = r'[a-zA-Z_$][a-zA-Z_$0-9]*' _NAME_RE = r'[a-zA-Z_$][a-zA-Z_$0-9]*'
# can't use raw string, starts with " and end with ' _SINGLE_QUOTED = r"""'(?:[^'\\\\]*(?:\\\\\\\\|\\\\['"nurtbfx/\\n]))*[^'\\\\]*'"""
_STRING_RE = '''"(?:[^"\\\\]*(?:\\\\\\\\|\\\\[\'"nurtbfx/\\n]))*[^"\\\\]*"| _DOUBLE_QUOTED = r'''"(?:[^"\\\\]*(?:\\\\\\\\|\\\\['"nurtbfx/\\n]))*[^"\\\\]*"'''
\'(?:[^\'\\\\]*(?:\\\\\\\\|\\\\[\'"nurtbfx/\\n]))*[^\'\\\\]*\'''' _STRING_RE = r'%s|%s' % (_SINGLE_QUOTED, _DOUBLE_QUOTED)
_INTEGER_RE = r'%(hex)s|%(dec)s|%(oct)s' % {'hex': __HEXADECIMAL_RE, 'dec': __DECIMAL_RE, 'oct': __OCTAL_RE} _INTEGER_RE = r'%(hex)s|%(dec)s|%(oct)s' % {'hex': __HEXADECIMAL_RE, 'dec': __DECIMAL_RE, 'oct': __OCTAL_RE}
_FLOAT_RE = r'%(dec)s\.%(dec)s' % {'dec': __DECIMAL_RE} _FLOAT_RE = r'%(dec)s\.%(dec)s' % {'dec': __DECIMAL_RE}
@ -46,17 +46,17 @@ _BOOL_RE = r'true|false'
# r'/(?=[^*])[^/\n]*/(?![gimy]*(?P<reflag>[gimy])[gimy]*\g<reflag>)[gimy]{0,4}' # r'/(?=[^*])[^/\n]*/(?![gimy]*(?P<reflag>[gimy])[gimy]*\g<reflag>)[gimy]{0,4}'
_REGEX_RE = r'/(?=[^*])[^/\n]*/[gimy]{0,4}' _REGEX_RE = r'/(?=[^*])[^/\n]*/[gimy]{0,4}'
_LITERAL_RE = r'(%(int)s|%(float)s|%(str)s|%(bool)s|%(regex)s)' % { _LITERAL_RE = r'((?P<int>%(int)s)|(?P<float>%(float)s)|(?P<str>%(str)s)|(?P<bool>%(bool)s)|(?P<regex>%(regex)s))' % {
'int': _INTEGER_RE, 'int': _INTEGER_RE,
'float': _FLOAT_RE, 'float': _FLOAT_RE,
'str': _STRING_RE, 'str': _STRING_RE,
'bool': _BOOL_RE, 'bool': _BOOL_RE,
'regex': _REGEX_RE 'regex': _REGEX_RE
} }
_ARRAY_RE = r'\[(%(literal)s\s*,\s*)*(%(literal)s\s*)?\]' % {'literal': _LITERAL_RE} # TODO nested array
_VALUE_RE = r'(?:%(literal)s)|(%(array)s)' % {'literal': _LITERAL_RE, 'array': _ARRAY_RE} # _ARRAY_RE = r'\[(%(literal)s\s*,\s*)*(%(literal)s\s*)?\]' % {'literal': _LITERAL_RE}
_CALL_RE = r'%(name)s\s*\(' % {'name': _NAME_RE} # _VALUE_RE = r'(?:%(literal)s)|(%(array)s)' % {'literal': _LITERAL_RE, 'array': _ARRAY_RE}
_CALL_RE = r'\.?%(name)s\s*\(' % {'name': _NAME_RE} # function or method!
_COMMENT_RE = r'/\*(?:(?!\*/)(?:\n|.))*\*/' _COMMENT_RE = r'/\*(?:(?!\*/)(?:\n|.))*\*/'