Author: Stephan <[email protected]>
Branch:
Changeset: r123:64f19a1fa65f
Date: 2011-09-06 19:15 +0200
http://bitbucket.org/pypy/lang-js/changeset/64f19a1fa65f/
Log: added DynamicMapDict for use in GlobalScope
diff --git a/js/jsexecution_context.py b/js/jsexecution_context.py
--- a/js/jsexecution_context.py
+++ b/js/jsexecution_context.py
@@ -1,4 +1,4 @@
-from js.utils import MapDict, StackMixin
+from js.utils import DynamicMapDict, MapDict, StackMixin
from js.jsobj import DONT_DELETE
class JSContext(object):
@@ -121,7 +121,7 @@
JSContext.__init__(self, parent)
StackMixin.__init__(self)
# TODO size of gloabl context
- self.values = MapDict(2048)
+ self.values = DynamicMapDict()
class ExecutionContext(JSContext, StackMixin):
def __init__(self, parent=None):
diff --git a/js/test/test_map.py b/js/test/test_map.py
--- a/js/test/test_map.py
+++ b/js/test/test_map.py
@@ -1,6 +1,6 @@
import py
-from js.utils import Map, MapDict
+from js.utils import Map, MapDict, DynamicMapDict
class TestMap(object):
def test_addname(self):
@@ -96,3 +96,13 @@
m = MapDict(2)
py.test.raises(KeyError, m.getindex, Map.NOT_FOUND)
py.test.raises(KeyError, m.get, 'foo')
+
+class TestDynamicMapDict(object):
+ def test_set(self):
+ m = DynamicMapDict()
+ assert len(m.values) == 0
+ m.set('foo', 4)
+ m.set('bar', 8)
+ assert m.get('foo') == 4
+ assert m.get('bar') == 8
+ assert len(m.values) == 2
diff --git a/js/utils.py b/js/utils.py
--- a/js/utils.py
+++ b/js/utils.py
@@ -94,6 +94,15 @@
def setindex(self, idx, value):
self.values[idx] = value
+class DynamicMapDict(MapDict):
+ def __init__(self):
+ MapDict.__init__(self, 0)
+
+ def addname(self, name):
+ while len(self.values) <= self.next_index:
+ self.values.append(None)
+ return MapDict.addname(self, name)
+
def mapdict_with_map(m):
assert isinstance(m, Map)
indexes = m.indexes
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit