From f409a873f512b1dd9de42e6926a9b31f1235e1d1 Mon Sep 17 00:00:00 2001 From: mattbasta Date: Fri, 24 Dec 2010 19:05:36 -0500 Subject: [PATCH] Updates to javascript tests; completeness, accuracy, better MemberExpression support --- jsmain.py | 9 +++-- validator/testcases/javascript/actions.py | 7 ++-- .../testcases/javascript/nodedefinitions.py | 2 +- validator/testcases/javascript/traverser.py | 34 +++++++++++++++---- 4 files changed, 39 insertions(+), 13 deletions(-) diff --git a/jsmain.py b/jsmain.py index b62be72..85ad20a 100644 --- a/jsmain.py +++ b/jsmain.py @@ -2,10 +2,13 @@ import sys import os import validator.testcases.scripting +from validator.testcases.javascript.traverser import MockBundler +import validator.testcases.javascript.traverser +validator.testcases.javascript.traverser.DEBUG = True path = sys.argv[1] script = open(path).read() - -validator.testcases.scripting.test_js_file(None, +err = MockBundler() +validator.testcases.scripting.test_js_file(err, path, - script) \ No newline at end of file + script) diff --git a/validator/testcases/javascript/actions.py b/validator/testcases/javascript/actions.py index 8ce2253..4a021af 100644 --- a/validator/testcases/javascript/actions.py +++ b/validator/testcases/javascript/actions.py @@ -13,7 +13,8 @@ def trace_member(traverser, node): # base = x if node["property"]["type"] == "Identifier": # y = token identifier - return base.get(node["property"]["name"]) + return base.get(traverser=traverser, + name=node["property"]["name"]) else: # y = literal value property = traverser._traverse_node(node["property"]) @@ -24,8 +25,10 @@ def trace_member(traverser, node): None elif node["type"] == "Identifier": + traverser._debug("MEMBER_EXP>>ROOT:IDENTIFIER") return traverser._seek_variable(node["name"]) else: + traverser._debug("MEMBER_EXP>>ROOT:EXPRESSION") # It's an expression, so just try your damndest. return JSWrapper(traverser._traverse_node(node), traverser=traverser) @@ -312,8 +315,8 @@ def _expr_assignment(traverser, node): "^=":lambda:lit_left ^ lit_right, "&=":lambda:lit_left & lit_right} - traverser._debug("ASSIGNMENT>>OPERATION") token = node["operator"] + traverser._debug("ASSIGNMENT>>OPERATION:%s" % token) if token not in operators: traverser._debug("ASSIGNMENT>>OPERATOR NOT FOUND") traverser.debug_level -= 1 diff --git a/validator/testcases/javascript/nodedefinitions.py b/validator/testcases/javascript/nodedefinitions.py index 740b7d4..7ff86e7 100644 --- a/validator/testcases/javascript/nodedefinitions.py +++ b/validator/testcases/javascript/nodedefinitions.py @@ -78,7 +78,7 @@ DEFINITIONS = { False, False, actions._call_expression, True, False), "MemberExpression": (("object", "property"), - False, False, None, True, False), + False, False, actions.trace_member, True, False), "YieldExpression": (("argument"), True, False, None, True, False), "ComprehensionExpression": (("body", "filter"), diff --git a/validator/testcases/javascript/traverser.py b/validator/testcases/javascript/traverser.py index 07e28ba..b7c0628 100644 --- a/validator/testcases/javascript/traverser.py +++ b/validator/testcases/javascript/traverser.py @@ -318,6 +318,9 @@ class Traverser: # Build out the wrapper object from the global definition. result = JSWrapper(is_global=True, traverser=self, lazy=True) result.value = entity + + self._debug("BUILT_GLOBAL") + return result def _set_variable(self, name, value, glob=False): @@ -472,13 +475,30 @@ class JSWrapper(object): if self.value is None: return JSWrapper(traverser=self) - if self.value is JSLiteral: - return None # This might need tweaking for properties - elif self.value is JSObject or \ - self.value is JSArray: - output = self.value.get(traverser, name) - elif self.value is JSPrototype: - output = self.value.get(name) + value = self.value + if self.is_global: + if "value" not in value: + return JSWrapper(None, traverser=self) + + value_val = value["value"] + if isinstance(value_val, types.LambdaType): + value_val = value_val() + + if isinstance(value_val, dict): + if name in value_val: + return JSWrapper(value=value_val[name], + is_global=True, + traverser=traverser) + else: + value = value_val + + + if value is JSLiteral: + return None # This will need tweaking for properties + elif isinstance(value, (JSObject, JSArray)): + output = value.get(traverser, name) + elif isinstance(value, JSPrototype): + output = value.get(name) else: output = None