Author: Stephan <[email protected]>
Branch:
Changeset: r264:a635f05fa061
Date: 2012-06-19 22:12 +0200
http://bitbucket.org/pypy/lang-js/changeset/a635f05fa061/
Log: added objectspace
diff --git a/js/builtins.py b/js/builtins.py
--- a/js/builtins.py
+++ b/js/builtins.py
@@ -15,10 +15,11 @@
def new_native_function(function, name = None, params = []):
from js.functions import JsNativeFunction
from js.jsobj import W__Function
+ from js.object_space import object_space
- scope = None
jsfunc = JsNativeFunction(function, name)
- return W__Function(jsfunc, formal_parameter_list = params)
+ obj = object_space.new_obj(W__Function, jsfunc, formal_parameter_list =
params)
+ return obj
# 15
def put_native_function(obj, name, func, writable = True, configurable = True,
enumerable = False, params = []):
@@ -29,10 +30,10 @@
def put_intimate_function(obj, name, func, writable = True, configurable =
True, enumerable = False, params = []):
from js.functions import JsIntimateFunction
from js.jsobj import W__Function
+ from js.object_space import object_space
- scope = None
jsfunc = JsIntimateFunction(func, name)
- w_func = W__Function(jsfunc, formal_parameter_list = params)
+ w_func = object_space.new_obj(W__Function, jsfunc, formal_parameter_list =
params)
put_property(obj, name, w_func, writable = writable, configurable =
configurable, enumerable = enumerable)
# 15
@@ -41,45 +42,35 @@
_put_property(obj, name, value, writable, configurable, enumerable)
def setup_builtins(global_object):
- # Forward declaration
- # 15.2.3
- from js.jsobj import W_ObjectConstructor
- w_Object = W_ObjectConstructor()
- put_property(global_object, u'Object', w_Object)
+ from js.object_space import object_space
- # 15.2.4
+ # 15.2.4 Properties of the Object Prototype Object
from js.jsobj import W_BasicObject
- w_ObjectPrototype = W_BasicObject()
+ w_ObjectPrototype = object_space.new_obj_with_proto(W_BasicObject, w_Null)
+ object_space.proto_object = w_ObjectPrototype
# 15.3.2
from js.jsobj import W_FunctionConstructor
w_Function = W_FunctionConstructor()
put_property(global_object, u'Function', w_Function)
- # 15.3.4
+ # 15.3.4 Properties of the Function Prototype Object
import js.builtins_function as function_builtins
- w_FunctionPrototype = new_native_function(function_builtins.empty,
u'Empty')
+ from js.jsobj import W__Object, W__Function, W_BasicFunction
+ from js.functions import JsNativeFunction
- # 15.2.4 Properties of the Object Prototype Object
- w_ObjectPrototype._prototype_ = w_Null
-
- # 15.3.4 Properties of the Function Prototype Object
- w_FunctionPrototype._prototype_ = w_ObjectPrototype
-
- # initial prototype
- from js.jsobj import W__Object, W__Function, W_BasicFunction
- W__Object._prototype_ = w_ObjectPrototype
-
- W__Function._prototype_ = w_FunctionPrototype
- W_BasicFunction._prototype_ = w_FunctionPrototype
+ empty_func = JsNativeFunction(function_builtins.empty, u'Empty')
+ w_FunctionPrototype = object_space.new_obj_with_proto(W__Function,
w_ObjectPrototype, empty_func, formal_parameter_list = [])
+ object_space.proto_function = w_FunctionPrototype
# 15.2 Object Objects
# 15.2.3 Properties of the Object Constructor
- w_Object._prototype_ = w_FunctionPrototype
- del(w_Object._properties_['__proto__'])
- #put_property(w_Object, '__proto__', w_Object._prototype_)
+ from js.jsobj import W_ObjectConstructor
+ w_Object = object_space.new_obj_with_proto(W_ObjectConstructor,
object_space.proto_function)
put_property(w_Object, u'length', _w(1))
+ put_property(global_object, u'Object', w_Object)
+
# 15.2.3.1 Object.prototype
put_property(w_Object, u'prototype', w_ObjectPrototype, writable = False,
configurable = False, enumerable = False)
diff --git a/js/builtins_array.py b/js/builtins_array.py
--- a/js/builtins_array.py
+++ b/js/builtins_array.py
@@ -4,16 +4,16 @@
def setup(global_object):
from js.builtins import put_property, put_native_function
from js.jsobj import W_ArrayConstructor, W__Array, W__Object
+ from js.object_space import object_space
+
w_Array = W_ArrayConstructor()
put_property(global_object, u'Array', w_Array)
# 15.4.4
- w_ArrayPrototype = W__Array()
-
- w_ArrayPrototype._prototype_ = W__Object._prototype_
+ w_ArrayPrototype = object_space.new_obj_with_proto(W__Array,
object_space.proto_object)
+ object_space.proto_array = w_ArrayPrototype
# 15.4.3.1
- W__Array._prototype_ = w_ArrayPrototype
put_property(w_Array, u'prototype', w_ArrayPrototype, writable = False,
enumerable = False, configurable = False)
# 15.4.4.1
diff --git a/js/builtins_boolean.py b/js/builtins_boolean.py
--- a/js/builtins_boolean.py
+++ b/js/builtins_boolean.py
@@ -4,6 +4,7 @@
def setup(global_object):
from js.builtins import put_property, put_native_function
+ from js.object_space import object_space
# 15.6.2
from js.jsobj import W_BooleanConstructor
@@ -14,12 +15,10 @@
put_property(w_Boolean, u'length', _w(1), writable = False, enumerable =
False, configurable = False)
# 15.6.4
- w_BooleanPrototype = W_BooleanObject(_w(False))
+ w_BooleanPrototype = object_space.new_obj_with_proto(W_BooleanObject,
object_space.proto_object, _w(False))
- from js.jsobj import W__Object
- w_BooleanPrototype._prototype_ = W__Object._prototype_
- #del(w_BooleanPrototype._properties_['__proto__'])
- #put_property(w_BooleanPrototype, '__proto__',
w_BooleanPrototype._prototype_, writable = False, enumerable = False,
configurable = False)
+ # 15.6.3.1
+ object_space.proto_boolean = w_BooleanPrototype
# 15.6.3.1
put_property(w_Boolean, u'prototype', w_BooleanPrototype, writable =
False, enumerable = False, configurable = False)
@@ -33,9 +32,6 @@
# 15.6.4.3
put_native_function(w_BooleanPrototype, u'valueOf', value_of)
- # 15.6.3.1
- W_BooleanObject._prototype_ = w_BooleanPrototype
-
# 15.6.4.2
def to_string(this, args):
if isinstance(this, W_Boolean):
diff --git a/js/builtins_date.py b/js/builtins_date.py
--- a/js/builtins_date.py
+++ b/js/builtins_date.py
@@ -8,13 +8,12 @@
from js.builtins import put_property, put_native_function
from js.builtins_number import w_NAN
from js.jsobj import W_DateObject, W_DateConstructor, W__Object
+ from js.object_space import object_space
##Date
# 15.9.5
- w_DatePrototype = W_DateObject(w_NAN)
- w_DatePrototype._prototype_ = W__Object._prototype_
-
- W_DateObject._prototype_ = w_DatePrototype
+ w_DatePrototype = object_space.new_obj_with_proto(W_DateObject,
object_space.proto_object, w_NAN)
+ object_space.proto_date = w_DatePrototype
def putf(name, func):
put_native_function(w_DatePrototype, name, func)
diff --git a/js/builtins_number.py b/js/builtins_number.py
--- a/js/builtins_number.py
+++ b/js/builtins_number.py
@@ -10,16 +10,13 @@
w_Number = W_NumberConstructor()
put_property(global_object, u'Number', w_Number)
- #put_property(w_Number, '__proto__', w_Number._prototype_, writable =
False, enumerable = False, configurable = False)
-
# 15.7.3
put_property(w_Number, u'length', _w(1), writable = False, enumerable =
False, configurable = False)
# 15.7.4
- from js.jsobj import W__Object
- w_NumberPrototype = W_NumericObject(_w(0))
- w_NumberPrototype._prototype_ = W__Object._prototype_
- #put_property(w_NumberPrototype, '__proto__',
w_NumberPrototype._prototype_, writable = False, enumerable = False,
configurable = False)
+ from js.object_space import object_space
+ w_NumberPrototype = object_space.new_obj_with_proto(W_NumericObject,
object_space.proto_object, _w(0))
+ object_space.proto_number = w_NumberPrototype
# 15.7.4.1
put_property(w_NumberPrototype, u'constructor', w_Number)
@@ -32,7 +29,6 @@
# 15.7.3.1
put_property(w_Number, u'prototype', w_NumberPrototype, writable = False,
enumerable = False, configurable = False)
- W_NumericObject._prototype_ = w_NumberPrototype
# 15.7.3.2
put_property(w_Number, u'MAX_VALUE', w_MAX_VALUE, writable = False,
configurable = False, enumerable = False)
diff --git a/js/builtins_string.py b/js/builtins_string.py
--- a/js/builtins_string.py
+++ b/js/builtins_string.py
@@ -10,19 +10,18 @@
# 15.5.1
from js.jsobj import W_StringConstructor
w_String = W_StringConstructor()
- #put_property(w_String, '__proto__', w_String._prototype_, writable =
False, enumerable = False, configurable = False)
put_property(w_String, u'length', _w(1), writable = False, enumerable =
False, configurable = False)
put_property(global_object, u'String', w_String)
# 15.5.4
- from js.jsobj import W_StringObject, W__Object
- w_StringPrototype = W_StringObject(_w(u''))
- w_StringPrototype._prototype_ = W__Object._prototype_
+ from js.jsobj import W_StringObject
+ from js.object_space import object_space
+ w_StringPrototype = object_space.new_obj_with_proto(W_StringObject,
object_space.proto_object, _w(u''))
# 15.5.3.1
- W_StringObject._prototype_ = w_StringPrototype
+ object_space.proto_string = w_StringPrototype
put_property(w_String, u'prototype', w_StringPrototype, writable = False,
enumerable = False, configurable = False)
# 15.5.3.2
diff --git a/js/execution_context.py b/js/execution_context.py
--- a/js/execution_context.py
+++ b/js/execution_context.py
@@ -169,7 +169,7 @@
self._this_binding_ = this
elif this is None or isnull_or_undefined(this):
from js.object_space import object_space
- self._this_binding_ = object_space.get_global_object()
+ self._this_binding_ = object_space.global_object
elif this.klass() is not 'Object':
self._this_binding_ = this.ToObject()
else:
diff --git a/js/interpreter.py b/js/interpreter.py
--- a/js/interpreter.py
+++ b/js/interpreter.py
@@ -71,8 +71,8 @@
from js.execution_context import GlobalExecutionContext
ctx = GlobalExecutionContext(c, self.global_object)
- object_space.set_global_context(ctx)
- object_space.set_global_object(self.global_object)
+ object_space.global_context = ctx
+ object_space.global_object = self.global_object
result = c.run(ctx)
return result.value
diff --git a/js/jsobj.py b/js/jsobj.py
--- a/js/jsobj.py
+++ b/js/jsobj.py
@@ -274,14 +274,12 @@
class W_BasicObject(W_Root):
_type_ = 'object'
_class_ = 'Object'
- _prototype_ = w_Null
_extensible_ = True
def __init__(self):
W_Root.__init__(self)
self._properties_ = {}
-
- #desc = PropertyDescriptor(value = self._prototype_, writable = False,
enumerable = False, configurable = False)
+ self._prototype_ = w_Null
desc = proto_desc
W_BasicObject.define_own_property(self, u'__proto__', desc)
@@ -659,14 +657,15 @@
# 13.2.2
def Construct(self, args=[]):
- obj = W__Object()
+ from js.object_space import object_space
+
proto = self.get(u'prototype')
if isinstance(proto, W_BasicObject):
- obj._prototype_ = proto
+ obj = object_space.new_obj_with_proto(W__Object, proto)
else:
# would love to test this
# but I fail to find a case that falls into this
- obj._prototype_ = W__Object._prototype_
+ obj = object_space.new_obj(W__Object)
result = self.Call(args, this=obj)
if isinstance(result, W__Object):
@@ -713,7 +712,8 @@
assert isnull_or_undefined(value)
- obj = W__Object()
+ from js.object_space import object_space
+ obj = object_space.new_obj(W__Object)
return obj
# TODO
@@ -758,7 +758,7 @@
scope = object_space.get_global_environment()
strict = func.strict
params = func.params()
- w_func = W__Function(func, formal_parameter_list = params, scope =
scope, strict = strict)
+ w_func = object_space.new_obj(W__Function, func, formal_parameter_list
= params, scope = scope, strict = strict)
return w_func
# TODO
@@ -844,7 +844,9 @@
else:
value = _w(int(time.time() * 1000))
- return W_DateObject(value)
+ from js.object_space import object_space
+ obj = object_space.new_obj(W_DateObject, value)
+ return obj
# 15.7.2.1
def Construct(self, args=[]):
@@ -857,6 +859,7 @@
class W__Function(W_BasicFunction):
def __init__(self, function_body, formal_parameter_list=[], scope=None,
strict=False):
+ from js.object_space import object_space
W_BasicFunction.__init__(self)
self._function_ = function_body
self._scope_ = scope
@@ -869,7 +872,7 @@
# 15.
put_property(self, u'length', _w(_len), writable = False, enumerable =
False, configurable = False)
# 16.
- proto_obj = W__Object()
+ proto_obj = object_space.new_obj(W__Object)
# 17.
put_property(proto_obj, u'constructor', self, writable = True,
enumerable = False, configurable = True)
# 18.
@@ -937,7 +940,8 @@
_len = len(args)
put_property(self, u'length', _w(_len), writable = True, enumerable =
False, configurable = True)
- _map = W__Object()
+ from js.object_space import object_space
+ _map = object_space.new_obj(W__Object)
mapped_names = []
indx = _len - 1
while indx >= 0:
@@ -979,21 +983,23 @@
return True
def Call(self, args = [], this = None, calling_context = None):
+ from js.object_space import object_space
+
if len(args) == 1:
_len = args[0]
if isinstance(_len, W_Number):
length = _len.ToUInt32()
if length != _len.ToNumber():
raise JsRangeError()
- array = W__Array(length)
+ array = object_space.new_obj(W__Array, length)
else:
length = 1
- array = W__Array(length)
+ array = object_space.new_obj(W__Array, length)
array.put(u'0', _len)
return array
else:
- array = W__Array()
+ array = object_space.new_obj(W__Array)
for index, obj in enumerate(args):
array.put(unicode(str(index)), obj)
return array
@@ -1120,7 +1126,8 @@
return 'W_Bool(%s)' % (str(self._boolval_), )
def ToObject(self):
- return W_BooleanObject(self)
+ from js.object_space import object_space
+ return object_space.new_obj(W_BooleanObject, self)
def to_string(self):
if self._boolval_ == True:
@@ -1151,7 +1158,8 @@
return u'W_String("%s")' % (self._strval_)
def ToObject(self):
- return W_StringObject(self)
+ from js.object_space import object_space
+ return object_space.new_obj(W_StringObject, self)
def to_string(self):
return self._strval_
@@ -1198,7 +1206,9 @@
# 9.9
def ToObject(self):
- return W_NumericObject(self)
+ from js.object_space import object_space
+ obj = object_space.new_obj(W_NumericObject, self)
+ return obj
def to_boolean(self):
num = self.ToNumber()
@@ -1341,7 +1351,8 @@
elif isinstance(value, str):
return W_String(unicode(value))
elif isinstance(value, list):
- a = W__Array()
+ from js.object_space import object_space
+ a = object_space.new_obj(W__Array)
for index, item in enumerate(value):
put_property(a, unicode(index), _w(item), writable = True,
enumerable = True, configurable = True)
return a
diff --git a/js/lexical_environment.py b/js/lexical_environment.py
--- a/js/lexical_environment.py
+++ b/js/lexical_environment.py
@@ -106,7 +106,7 @@
name = v.get_referenced_name()
# TODO how to solve this ????
from js.object_space import object_space
- global_object = object_space.get_global_object()
+ global_object = object_space.global_object
global_object.put(name, w, throw = False)
elif v.is_property_reference():
diff --git a/js/object_space.py b/js/object_space.py
--- a/js/object_space.py
+++ b/js/object_space.py
@@ -1,21 +1,34 @@
class ObjectSpace(object):
- def __init__(self):
- self.global_object = None
- self.global_context = None
-
- def set_global_object(self, obj):
- self.global_object = obj
-
def get_global_object(self):
return self.global_object
- def set_global_context(self, ctx):
- self.global_context = ctx
+ def get_global_environment(self):
+ return self.global_context.variable_environment()
- def get_global_context(self):
- return self.global_context
+ def new_obj_with_proto(self, cls, proto, *args, **kwargs):
+ obj = cls(*args, **kwargs)
+ obj._prototype_ = proto
+ return obj
- def get_global_environment(self):
- return self.get_global_context().variable_environment()
+ def new_obj(self, cls, *args, **kwargs):
+ from js.jsobj import W_BasicFunction, W_BooleanObject, W_StringObject,
W_NumericObject, W_DateObject, W__Array
+ obj = cls(*args, **kwargs)
+
+ if issubclass(cls, W_BasicFunction):
+ obj._prototype_ = self.proto_function
+ elif issubclass(cls, W_BooleanObject):
+ obj._prototype_ = self.proto_boolean
+ elif issubclass(cls, W_StringObject):
+ obj._prototype_ = self.proto_string
+ elif issubclass(cls, W_NumericObject):
+ obj._prototype_ = self.proto_number
+ elif issubclass(cls, W_DateObject):
+ obj._prototype_ = self.proto_date
+ elif issubclass(cls, W__Array):
+ obj._prototype_ = self.proto_array
+ else:
+ obj._prototype_ = self.proto_object
+
+ return obj
object_space = ObjectSpace()
diff --git a/js/opcodes.py b/js/opcodes.py
--- a/js/opcodes.py
+++ b/js/opcodes.py
@@ -143,7 +143,9 @@
def eval(self, ctx):
from js.jsobj import W__Array
- array = W__Array()
+ from js.object_space import object_space
+ array = object_space.new_obj(W__Array)
+
list_w = ctx.stack_pop_n(self.counter) # [:] # pop_n returns a
non-resizable list
for index, el in enumerate(list_w):
array.put(unicode(str(index)), el)
@@ -178,11 +180,12 @@
# 13.2 Creating Function Objects
def eval(self, ctx):
from js.jsobj import W__Object
+ from js.object_space import object_space
func = self.funcobj
scope = ctx.lexical_environment()
params = func.params()
strict = func.strict
- w_func = W__Function(func, formal_parameter_list = params, scope =
scope, strict = strict)
+ w_func = object_space.new_obj(W__Function, func, formal_parameter_list
= params, scope = scope, strict = strict)
ctx.stack_append(w_func)
@@ -195,7 +198,8 @@
self.counter = counter
def eval(self, ctx):
- w_obj = W__Object()
+ from js.object_space import object_space
+ w_obj = object_space.new_obj(W__Object)
for _ in range(self.counter):
name = ctx.stack_pop().to_string()
w_elem = ctx.stack_pop()
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit