Author: Stephan <[email protected]>
Branch: 
Changeset: r144:9ed880d86cf8
Date: 2011-11-02 12:31 +0100
http://bitbucket.org/pypy/lang-js/changeset/9ed880d86cf8/

Log:    remove callfunc attribute from W_PrimitiveObject, handled in
        W_CallableObject

diff --git a/js/builtins.py b/js/builtins.py
--- a/js/builtins.py
+++ b/js/builtins.py
@@ -474,8 +474,8 @@
         return this
 
 class W_NativeObject(W_Object):
-    def __init__(self, Class, Prototype, ctx=None, Value=w_Undefined, 
callfunc=None):
-        W_Object.__init__(self, ctx, Prototype, Class, Value, callfunc)
+    def __init__(self, Class, Prototype, ctx=None, Value=w_Undefined):
+        W_Object.__init__(self, ctx, Prototype, Class, Value)
 
 
 class W_DateObject(W_NativeObject):
diff --git a/js/jsobj.py b/js/jsobj.py
--- a/js/jsobj.py
+++ b/js/jsobj.py
@@ -176,8 +176,8 @@
         return True
 
 class W_PrimitiveObject(W_Root):
-    _immutable_fields_ = ['Class', 'callfunc', 'Property', 'ctx', 'Scope', 
'Value']
-    def __init__(self, ctx=None, Prototype=None, Class='Object', 
Value=w_Undefined, callfunc=None):
+    _immutable_fields_ = ['Class', 'Property', 'Scope', 'Value']
+    def __init__(self, ctx=None, Prototype=None, Class='Object', 
Value=w_Undefined):
         self.Prototype = Prototype
         self.property_map = root_map()
         self.property_values = []
@@ -185,11 +185,7 @@
             Prototype = w_Undefined
         self._set_property('prototype', Prototype, DONT_ENUM | DONT_DELETE)
         self.Class = Class
-        self.callfunc = callfunc
-        if callfunc is not None:
-            self.ctx = ctx
-        else:
-            self.Scope = None
+        self.Scope = None
         self.Value = Value
 
     def _set_property(self, name, value, flags):
@@ -245,34 +241,8 @@
     def _get_property_keys(self):
         return self.property_map.keys()
 
-    @jit.unroll_safe
     def Call(self, ctx, args=[], this=None):
-        if self.callfunc is None: # XXX Not sure if I should raise it here
-            raise JsTypeError('not a function')
-
-        # TODO
-        if this:
-            from js.jsobj import W_Root
-            assert isinstance(this, W_Root)
-
-        from js.jsexecution_context import make_activation_context, 
make_function_context
-
-        w_Arguments = W_Arguments(self, args)
-        act = make_activation_context(self.ctx, this, w_Arguments)
-        newctx = make_function_context(act, self.callfunc)
-
-        paramn = len(self.callfunc.params)
-        for i in range(paramn):
-            paramname = self.callfunc.params[i]
-            try:
-                value = args[i]
-            except IndexError:
-                value = w_Undefined
-            newctx.declare_variable(paramname)
-            newctx.assign(paramname, value)
-
-        val = self.callfunc.run(ctx=newctx)
-        return val
+        raise JsTypeError('not a function')
 
     def Construct(self, ctx, args=[]):
         obj = W_Object(Class='Object')
@@ -361,35 +331,68 @@
         return "<Object class: %s>" % self.Class
 
     def type(self):
-        if self.callfunc is not None:
-            return 'function'
-        else:
-            return 'object'
+        return 'object'
 
+def str_builtin(ctx, args, this):
+    return W_String(this.ToString(ctx))
+
+class W_Object(W_PrimitiveObject):
+    def __init__(self, ctx=None, Prototype=None, Class='Object', 
Value=w_Undefined):
+        W_PrimitiveObject.__init__(self, ctx, Prototype, Class, Value)
+
+    def ToNumber(self, ctx):
+        return self.Get(ctx, 'valueOf').Call(ctx, args=[], 
this=self).ToNumber(ctx)
+
+class W_CallableObject(W_Object):
+    _immutable_fields_ = ['callfunc', 'ctx']
+    def __init__(self, ctx, Prototype, callfunc):
+        W_Object.__init__(self, ctx, Prototype, 'Function')
+        self.ctx = ctx
+        self.callfunc = callfunc
+
+    @jit.unroll_safe
+    def Call(self, ctx, args=[], this=None):
+        # TODO
+        if this:
+            from js.jsobj import W_Root
+            assert isinstance(this, W_Root)
+
+        from js.jsexecution_context import make_activation_context, 
make_function_context
+
+        w_Arguments = W_Arguments(self, args)
+        act = make_activation_context(self.ctx, this, w_Arguments)
+        newctx = make_function_context(act, self.callfunc)
+
+        paramn = len(self.callfunc.params)
+        for i in range(paramn):
+            paramname = self.callfunc.params[i]
+            try:
+                value = args[i]
+            except IndexError:
+                value = w_Undefined
+            newctx.declare_variable(paramname)
+            newctx.assign(paramname, value)
+
+        val = self.callfunc.run(ctx=newctx)
+        return val
+
+    def type(self):
+        return 'function'
 
 class W_Primitive(W_Root):
     """unifying parent for primitives"""
     def ToPrimitive(self, ctx, hint=""):
         return self
 
-def str_builtin(ctx, args, this):
-    return W_String(this.ToString(ctx))
-
-class W_Object(W_PrimitiveObject):
-    def __init__(self, ctx=None, Prototype=None, Class='Object', 
Value=w_Undefined, callfunc=None):
-        W_PrimitiveObject.__init__(self, ctx, Prototype, Class, Value, 
callfunc)
-
-    def ToNumber(self, ctx):
-        return self.Get(ctx, 'valueOf').Call(ctx, args=[], 
this=self).ToNumber(ctx)
 
 class W_NewBuiltin(W_PrimitiveObject):
     length = -1
-    def __init__(self, ctx, Prototype=None, Class='function', 
Value=w_Undefined, callfunc=None):
+    def __init__(self, ctx, Prototype=None, Class='function', 
Value=w_Undefined):
         if Prototype is None:
             proto = ctx.get_global().Get(ctx, 'Function').Get(ctx, 'prototype')
             Prototype = proto
 
-        W_PrimitiveObject.__init__(self, ctx, Prototype, Class, Value, 
callfunc)
+        W_PrimitiveObject.__init__(self, ctx, Prototype, Class, Value)
 
         if self.length != -1:
             self.Put(ctx, 'length', W_IntNumber(self.length), flags = 
DONT_ENUM|DONT_DELETE|READ_ONLY)
@@ -402,9 +405,8 @@
         return self.Class
 
 class W_Builtin(W_PrimitiveObject):
-    def __init__(self, builtin=None, ctx=None, Prototype=None, 
Class='function',
-                 Value=w_Undefined, callfunc=None):
-        W_PrimitiveObject.__init__(self, ctx, Prototype, Class, Value, 
callfunc)
+    def __init__(self, builtin=None, ctx=None, Prototype=None, 
Class='function', Value=w_Undefined):
+        W_PrimitiveObject.__init__(self, ctx, Prototype, Class, Value)
         self.set_builtin_call(builtin)
 
     def set_builtin_call(self, callfuncbi):
@@ -448,8 +450,8 @@
         return str(self.property_map)
 
 class W_Array(W_ListObject):
-    def __init__(self, ctx=None, Prototype=None, Class='Array', 
Value=w_Undefined, callfunc=None):
-        W_ListObject.__init__(self, ctx, Prototype, Class, Value, callfunc)
+    def __init__(self, ctx=None, Prototype=None, Class='Array', 
Value=w_Undefined):
+        W_ListObject.__init__(self, ctx, Prototype, Class, Value)
         self.Put(ctx, 'length', W_IntNumber(0), flags = DONT_DELETE)
         self.length = r_uint(0)
 
@@ -702,11 +704,11 @@
     def empty(self):
         return len(self.elements_w) == 0
 
-def create_object(ctx, prototypename, callfunc=None, Value=w_Undefined):
+def create_object(ctx, prototypename, Value=w_Undefined):
     proto = ctx.get_global().Get(ctx, prototypename).Get(ctx, 'prototype')
     # TODO get Object prototype from interp.w_Object
     assert isinstance(proto, W_PrimitiveObject)
-    obj = W_Object(ctx, callfunc = callfunc,Prototype=proto, Class = 
proto.Class, Value = Value)
+    obj = W_Object(ctx, Prototype=proto, Class = proto.Class, Value = Value)
     obj.Put(ctx, '__proto__', proto, DONT_ENUM | DONT_DELETE | READ_ONLY)
     return obj
 
diff --git a/js/opcodes.py b/js/opcodes.py
--- a/js/opcodes.py
+++ b/js/opcodes.py
@@ -1,7 +1,7 @@
 from js.jsobj import W_IntNumber, W_FloatNumber, W_String,\
      W_Array, W_PrimitiveObject, ActivationObject,\
      create_object, W_Object, w_Undefined, newbool,\
-     w_True, w_False, W_List, w_Null, W_Iterator, W_Root
+     w_True, w_False, W_List, w_Null, W_Iterator, W_Root, W_CallableObject
 import js.jsobj as jsobj
 from js.execution import JsTypeError, ReturnException, ThrowException
 from js.baseop import plus, sub, compare, AbstractEC, StrictEC,\
@@ -175,8 +175,7 @@
 
     def eval(self, ctx):
         proto = ctx.get_global().Get(ctx, 'Function').Get(ctx, 'prototype')
-        w_func = W_Object(ctx=ctx, Prototype=proto, Class='Function',
-                          callfunc=self.funcobj)
+        w_func = W_CallableObject(ctx, proto, self.funcobj)
         w_func.Put(ctx, 'length', W_IntNumber(len(self.funcobj.params)))
         w_obj = create_object(ctx, 'Object')
         w_obj.Put(ctx, 'constructor', w_func, flags = jsobj.DONT_ENUM)
@@ -470,7 +469,7 @@
     def eval(self, ctx):
         # function declaration actyally don't run anything
         proto = ctx.get_global().Get(ctx, 'Function').Get(ctx, 'prototype')
-        w_func = W_Object(ctx=ctx, Prototype=proto, Class='Function', 
callfunc=self.funcobj)
+        w_func = W_CallableObject(ctx, proto, self.funcobj)
         w_func.Put(ctx, 'length', W_IntNumber(len(self.funcobj.params)))
         w_obj = create_object(ctx, 'Object')
         w_obj.Put(ctx, 'constructor', w_func, flags = jsobj.DONT_ENUM)
_______________________________________________
pypy-commit mailing list
[email protected]
http://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to