[pypy-svn] r55577 - pypy/branch/js-refactoring/pypy/lang/js
santagada at codespeak.net
santagada at codespeak.net
Wed Jun 4 21:16:55 CEST 2008
Author: santagada
Date: Wed Jun 4 21:16:52 2008
New Revision: 55577
Modified:
pypy/branch/js-refactoring/pypy/lang/js/interpreter.py
pypy/branch/js-refactoring/pypy/lang/js/jscode.py
pypy/branch/js-refactoring/pypy/lang/js/jsobj.py
Log:
new string operations, removal of ctx.Put and string objects and primitives are different things
Modified: pypy/branch/js-refactoring/pypy/lang/js/interpreter.py
==============================================================================
--- pypy/branch/js-refactoring/pypy/lang/js/interpreter.py (original)
+++ pypy/branch/js-refactoring/pypy/lang/js/interpreter.py Wed Jun 4 21:16:52 2008
@@ -94,15 +94,23 @@
return create_object(ctx, 'String', Value = Value)
return create_object(ctx, 'String', Value = W_String(''))
+def create_array(ctx, elements=[]):
+ proto = ctx.get_global().Get(ctx, 'Array').Get(ctx, 'prototype')
+ array = W_Array(ctx, Prototype=proto, Class = proto.Class)
+ i = 0
+ while i < len(elements):
+ array.Put(ctx, str(i), elements[i])
+ i += 1
+
+ return array
+
class W_ArrayObject(W_NativeObject):
def Call(self, ctx, args=[], this=None):
- proto = ctx.get_global().Get(ctx, 'Array').Get(ctx, 'prototype')
- array = W_Array(ctx, Prototype=proto, Class = proto.Class)
if len(args) == 1 and isinstance(args[0], W_BaseNumber):
+ array = create_array(ctx)
array.Put(ctx, 'length', args[0])
else:
- for i in range(len(args)):
- array.Put(ctx, str(i), args[i])
+ array = create_array(ctx, args)
return array
def Construct(self, ctx, args=[]):
@@ -353,6 +361,14 @@
return this.Value
return W_ValueValueOf
+class W_FromCharCode(W_NewBuiltin):
+ def Call(self, ctx, args=[], this=None):
+ temp = []
+ for arg in args:
+ temp.append(chr(arg.ToInt32(ctx)))
+
+ return W_String(''.join(temp))
+
class W_CharAt(W_NewBuiltin):
def Call(self, ctx, args=[], this=None):
string = this.ToString(ctx)
@@ -404,6 +420,32 @@
end = max(tmp1, tmp2)
return W_String(string[start:end])
+class W_Split(W_NewBuiltin):
+ def Call(self, ctx, args=[], this=None):
+ string = this.ToString(ctx)
+
+ if len(args) < 1 or args[0] is w_Undefined:
+ return create_array(ctx, [W_String(string)])
+ else:
+ separator = args[0].ToString(ctx)
+
+ if len(args) >= 2:
+ limit = args[1].ToUInt32(ctx)
+ raise ThrowException(W_String("limit not implemented"))
+ # array = string.split(separator, limit)
+ else:
+ array = string.split(separator)
+
+ w_array = create_array(ctx)
+ i = 0
+ while i < len(array):
+ w_str = W_String(array[i])
+ w_array.Put(ctx, str(i), w_str)
+ i += 1
+
+ return w_array
+
+
def common_join(ctx, this, sep=','):
length = this.Get(ctx, 'length').ToUInt32(ctx)
l = []
@@ -581,9 +623,11 @@
'concat': W_Concat(ctx),
'indexOf': W_IndexOf(ctx),
'substring': W_Substring(ctx),
+ 'split': W_Split(ctx),
})
w_String.Put(ctx, 'prototype', w_StrPrototype)
+ w_String.Put(ctx, 'fromCharCode', W_FromCharCode(ctx))
w_Global.Put(ctx, 'String', w_String)
w_Array = W_ArrayObject('Array', w_FncPrototype)
Modified: pypy/branch/js-refactoring/pypy/lang/js/jscode.py
==============================================================================
--- pypy/branch/js-refactoring/pypy/lang/js/jscode.py (original)
+++ pypy/branch/js-refactoring/pypy/lang/js/jscode.py Wed Jun 4 21:16:52 2008
@@ -740,7 +740,7 @@
w_obj.Put(ctx, 'constructor', w_func, flags = DE)
w_func.Put(ctx, 'prototype', w_obj)
if self.funcobj.name is not None:
- ctx.Put(ctx, self.funcobj.name, w_func)
+ ctx.scope[-1].Put(ctx, self.funcobj.name, w_func)
def __repr__(self):
funcobj = self.funcobj
@@ -756,7 +756,7 @@
self.name = name
def eval(self, ctx, stack):
- ctx.Put(ctx, self.name, w_Undefined, flags = DD)
+ ctx.scope[-1].Put(ctx, self.name, w_Undefined, flags = DD)
def __repr__(self):
return 'DECLARE_VAR "%s"' % (self.name,)
Modified: pypy/branch/js-refactoring/pypy/lang/js/jsobj.py
==============================================================================
--- pypy/branch/js-refactoring/pypy/lang/js/jsobj.py (original)
+++ pypy/branch/js-refactoring/pypy/lang/js/jsobj.py Wed Jun 4 21:16:52 2008
@@ -3,7 +3,6 @@
ovfcheck_float_to_int, NAN
from pypy.lang.js.execution import ThrowException, JsTypeError,\
RangeError, ReturnException
-
DE = 1
DD = 2
RO = 4
@@ -390,15 +389,10 @@
def __repr__(self):
return 'W_String(%s)' % (self.strval,)
- def Get(self, ctx, P): #as hackinsh as can get
- if P == 'length':
- return W_FloatNumber(len(self.strval))
- else:
- proto = ctx.get_global().Get(ctx, 'String').Get(ctx, 'prototype')
- return proto.Get(ctx, P)
-
def ToObject(self, ctx):
- return self #create_object(ctx, 'String', Value=self)
+ o = create_object(ctx, 'String', Value=self)
+ o.Put(ctx, 'length', W_IntNumber(len(self.strval)), flags = RO|DD)
+ return o
def ToString(self, ctx=None):
return self.strval
@@ -588,10 +582,6 @@
pass
return False
- def Put(self, ctx, name, value, flags = 0):
- assert name is not None
- self.variable.Put(ctx, name, value, flags = flags)
-
def get_global(self):
return self.scope[0]
More information about the Pypy-commit
mailing list