Use __slots__ to decrease jstypes memory footprint (bug 939603)

This commit is contained in:
Matt Basta 2013-11-19 16:48:58 -08:00
Родитель 4901889f02
Коммит 5b72803a59
1 изменённых файлов: 15 добавлений и 2 удалений

Просмотреть файл

@ -8,18 +8,23 @@ def fake(traverser, **kw):
return JSObject(traverser=traverser, **kw) return JSObject(traverser=traverser, **kw)
BASE_MEMBERS = ["const", "traverser", "type_", "callable", "recursing",
"data", "TYPEOF"]
class JSObject(object): class JSObject(object):
""" """
Mimics a JS object (function) and is capable of serving as an active Mimics a JS object (function) and is capable of serving as an active
context to enable static analysis of `with` statements. context to enable static analysis of `with` statements.
""" """
TYPEOF = "object" __slots__ = BASE_MEMBERS
def __init__(self, data=None, traverser=None, callable_=False, const=False): def __init__(self, data=None, traverser=None, callable_=False, const=False):
self.const = False self.const = False
self.traverser = traverser self.traverser = traverser
self.type_ = "object" # For use when an object is pushed as a context. self.type_ = "object" # For use when an object is pushed as a context.
self.TYPEOF = "object"
self.data = {} self.data = {}
if data: if data:
self.data.update(data) self.data.update(data)
@ -117,6 +122,8 @@ class JSObject(object):
class JSGlobal(JSObject): class JSGlobal(JSObject):
__slots__ = BASE_MEMBERS + ["name", "global_data"]
def __init__(self, global_data, traverser=None, **kw): def __init__(self, global_data, traverser=None, **kw):
self.global_data = utils.evaluate_lambdas(traverser, global_data) self.global_data = utils.evaluate_lambdas(traverser, global_data)
super(JSGlobal, self).__init__(traverser=traverser, **kw) super(JSGlobal, self).__init__(traverser=traverser, **kw)
@ -207,6 +214,8 @@ class JSGlobal(JSObject):
class JSContext(JSObject): class JSContext(JSObject):
"""A variable context""" """A variable context"""
__slots__ = BASE_MEMBERS
def __init__(self, context_type="default", traverser=None, **kw): def __init__(self, context_type="default", traverser=None, **kw):
super(JSContext, self).__init__(traverser=traverser, **kw) super(JSContext, self).__init__(traverser=traverser, **kw)
self.type_ = context_type self.type_ = context_type
@ -240,13 +249,15 @@ LITERAL_TYPEOF = {
class JSLiteral(JSObject): class JSLiteral(JSObject):
"""Represents a literal JavaScript value.""" """Represents a literal JavaScript value."""
__slots__ = BASE_MEMBERS + ["value"]
def __init__(self, value=None, traverser=None, **kw): def __init__(self, value=None, traverser=None, **kw):
super(JSLiteral, self).__init__(traverser=traverser, **kw) super(JSLiteral, self).__init__(traverser=traverser, **kw)
if isinstance(value, JSLiteral): if isinstance(value, JSLiteral):
self.value = value.value self.value = value.value
else: else:
self.value = value self.value = value
self.TYPEOF = LITERAL_TYPEOF.get(type(value), self.TYPEOF) self.TYPEOF = LITERAL_TYPEOF.get(type(value), "object")
def __str__(self): def __str__(self):
if isinstance(self.value, bool): if isinstance(self.value, bool):
@ -273,6 +284,8 @@ class JSLiteral(JSObject):
class JSArray(JSObject): class JSArray(JSObject):
"""A class that represents both a JS Array and a JS list.""" """A class that represents both a JS Array and a JS list."""
__slots__ = BASE_MEMBERS + ["elements"]
def __init__(self, elements=None, traverser=None, **kw): def __init__(self, elements=None, traverser=None, **kw):
super(JSArray, self).__init__(traverser=traverser, **kw) super(JSArray, self).__init__(traverser=traverser, **kw)
self.elements = elements or [] self.elements = elements or []