[pypy-commit] lang-js default: do not pass ctx reference around
stepahn
noreply at buildbot.pypy.org
Fri Dec 28 11:32:49 CET 2012
Author: Stephan <stephan at stzal.com>
Branch:
Changeset: r151:36d6bb5e1b34
Date: 2011-11-02 18:15 +0100
http://bitbucket.org/pypy/lang-js/changeset/36d6bb5e1b34/
Log: do not pass ctx reference around
diff --git a/js/baseop.py b/js/baseop.py
--- a/js/baseop.py
+++ b/js/baseop.py
@@ -13,8 +13,8 @@
def plus(ctx, nleft, nright):
if isinstance(nleft, W_String) or isinstance(nright, W_String):
- sleft = nleft.ToString(ctx)
- sright = nright.ToString(ctx)
+ sleft = nleft.ToString()
+ sright = nright.ToString()
return W_String(sleft + sright)
# hot path
if isinstance(nleft, W_IntNumber) and isinstance(nright, W_IntNumber):
@@ -25,8 +25,8 @@
except OverflowError:
return W_FloatNumber(float(ileft) + float(iright))
else:
- fleft = nleft.ToNumber(ctx)
- fright = nright.ToNumber(ctx)
+ fleft = nleft.ToNumber()
+ fright = nright.ToNumber()
return W_FloatNumber(fleft + fright)
def increment(ctx, nleft, constval=1):
@@ -50,32 +50,32 @@
return W_IntNumber(ovfcheck(ileft - iright))
except OverflowError:
return W_FloatNumber(float(ileft) - float(iright))
- fleft = nleft.ToNumber(ctx)
- fright = nright.ToNumber(ctx)
+ fleft = nleft.ToNumber()
+ fright = nright.ToNumber()
return W_FloatNumber(fleft - fright)
def mult(ctx, nleft, nright):
if isinstance(nleft, W_IntNumber) and isinstance(nright, W_IntNumber):
# XXXX test & stuff
- ileft = nleft.ToInteger(ctx)
- iright = nright.ToInteger(ctx)
+ ileft = nleft.ToInteger()
+ iright = nright.ToInteger()
try:
return W_IntNumber(ovfcheck(ileft * iright))
except OverflowError:
return W_FloatNumber(float(ileft) * float(iright))
- fleft = nleft.ToNumber(ctx)
- fright = nright.ToNumber(ctx)
+ fleft = nleft.ToNumber()
+ fright = nright.ToNumber()
return W_FloatNumber(fleft * fright)
def mod(ctx, nleft, nright): # XXX this one is really not following spec
- fleft = nleft.ToNumber(ctx)
- fright = nright.ToNumber(ctx)
+ fleft = nleft.ToNumber()
+ fright = nright.ToNumber()
return W_FloatNumber(math.fmod(fleft, fright))
def division(ctx, nleft, nright):
# XXX optimise for ints and floats
- fleft = nleft.ToNumber(ctx)
- fright = nright.ToNumber(ctx)
+ fleft = nleft.ToNumber()
+ fright = nright.ToNumber()
if fright == 0:
if fleft < 0:
val = -INFINITY
@@ -94,17 +94,17 @@
if isnan(x.floatval) or isnan(y.floatval):
return -1
return x.floatval > y.floatval
- s1 = x.ToPrimitive(ctx, 'Number')
- s2 = y.ToPrimitive(ctx, 'Number')
+ s1 = x.ToPrimitive('Number')
+ s2 = y.ToPrimitive('Number')
if not (isinstance(s1, W_String) and isinstance(s2, W_String)):
- s4 = s1.ToNumber(ctx)
- s5 = s2.ToNumber(ctx)
+ s4 = s1.ToNumber()
+ s5 = s2.ToNumber()
if isnan(s4) or isnan(s5):
return False
return s4 > s5
else:
- s4 = s1.ToString(ctx)
- s5 = s2.ToString(ctx)
+ s4 = s1.ToString()
+ s5 = s2.ToString()
return s4 > s5
def compare_e(ctx, x, y):
@@ -114,17 +114,17 @@
if isnan(x.floatval) or isnan(y.floatval):
return -1
return x.floatval >= y.floatval
- s1 = x.ToPrimitive(ctx, 'Number')
- s2 = y.ToPrimitive(ctx, 'Number')
+ s1 = x.ToPrimitive('Number')
+ s2 = y.ToPrimitive('Number')
if not (isinstance(s1, W_String) and isinstance(s2, W_String)):
- s4 = s1.ToNumber(ctx)
- s5 = s2.ToNumber(ctx)
+ s4 = s1.ToNumber()
+ s5 = s2.ToNumber()
if isnan(s4) or isnan(s5):
return False
return s4 >= s5
else:
- s4 = s1.ToString(ctx)
- s5 = s2.ToString(ctx)
+ s4 = s1.ToString()
+ s5 = s2.ToString()
return s4 >= s5
def AbstractEC(ctx, x, y):
@@ -144,38 +144,38 @@
if type1 == "undefined" or type1 == "null":
return True
if type1 == "number":
- n1 = x.ToNumber(ctx)
- n2 = y.ToNumber(ctx)
+ n1 = x.ToNumber()
+ n2 = y.ToNumber()
if isnan(n1) or isnan(n2):
return False
if n1 == n2:
return True
return False
elif type1 == "string":
- return x.ToString(ctx) == y.ToString(ctx)
+ return x.ToString() == y.ToString()
elif type1 == "boolean":
return x.ToBoolean() == x.ToBoolean()
# XXX rethink it here
- return x.ToString(ctx) == y.ToString(ctx)
+ return x.ToString() == y.ToString()
else:
#step 14
if (type1 == "undefined" and type2 == "null") or \
(type1 == "null" and type2 == "undefined"):
return True
if type1 == "number" and type2 == "string":
- return AbstractEC(ctx, x, W_FloatNumber(y.ToNumber(ctx)))
+ return AbstractEC(ctx, x, W_FloatNumber(y.ToNumber()))
if type1 == "string" and type2 == "number":
- return AbstractEC(ctx, W_FloatNumber(x.ToNumber(ctx)), y)
+ return AbstractEC(ctx, W_FloatNumber(x.ToNumber()), y)
if type1 == "boolean":
- return AbstractEC(ctx, W_FloatNumber(x.ToNumber(ctx)), y)
+ return AbstractEC(ctx, W_FloatNumber(x.ToNumber()), y)
if type2 == "boolean":
- return AbstractEC(ctx, x, W_FloatNumber(y.ToNumber(ctx)))
+ return AbstractEC(ctx, x, W_FloatNumber(y.ToNumber()))
if (type1 == "string" or type1 == "number") and \
type2 == "object":
- return AbstractEC(ctx, x, y.ToPrimitive(ctx))
+ return AbstractEC(ctx, x, y.ToPrimitive())
if (type2 == "string" or type2 == "number") and \
type1 == "object":
- return AbstractEC(ctx, x.ToPrimitive(ctx), y)
+ return AbstractEC(ctx, x.ToPrimitive(), y)
return False
@@ -185,9 +185,9 @@
return True
if isinstance(x, W_String) and isinstance(y, W_String):
- r = x.ToString(ctx) == y.ToString(ctx)
+ r = x.ToString() == y.ToString()
else:
- r = x.ToNumber(ctx) == y.ToNumber(ctx)
+ r = x.ToNumber() == y.ToNumber()
return r
def StrictEC(ctx, x, y):
@@ -202,15 +202,15 @@
if type1 == "undefined" or type1 == "null":
return True
if type1 == "number":
- n1 = x.ToNumber(ctx)
- n2 = y.ToNumber(ctx)
+ n1 = x.ToNumber()
+ n2 = y.ToNumber()
if isnan(n1) or isnan(n2):
return False
if n1 == n2:
return True
return False
if type1 == "string":
- return x.ToString(ctx) == y.ToString(ctx)
+ return x.ToString() == y.ToString()
if type1 == "boolean":
return x.ToBoolean() == x.ToBoolean()
return x == y
@@ -220,7 +220,7 @@
if not isinstance(obj, W_PrimitiveObject):
raise ThrowException(W_String('it is not a constructor'))
try:
- res = obj.Construct(ctx=ctx, args=args)
+ res = obj.Construct(args=args)
except JsTypeError:
raise ThrowException(W_String('it is not a constructor'))
return res
@@ -228,4 +228,4 @@
def uminus(obj, ctx):
if isinstance(obj, W_IntNumber):
return W_IntNumber(-obj.intval)
- return W_FloatNumber(-obj.ToNumber(ctx))
+ return W_FloatNumber(-obj.ToNumber())
diff --git a/js/builtins.py b/js/builtins.py
--- a/js/builtins.py
+++ b/js/builtins.py
@@ -42,16 +42,20 @@
return t
def make_loadjs(interp):
- def f(ctx, args, this):
- filename = str(args[0].ToString(ctx))
+ def f(args, this):
+ filename = str(args[0].ToString())
t = load_file(filename)
interp.run(t)
return w_Undefined
return f
class W_Eval(W_NewBuiltin):
+ def __init__(self, ctx):
+ W_NewBuiltin.__init__(self)
+ self.ctx = ctx
+
length = 1
- def Call(self, ctx, args=[], this=None):
+ def Call(self, args=[], this=None):
if len(args) >= 1:
arg0 = args[0]
if isinstance(arg0, W_String):
@@ -69,14 +73,14 @@
bytecode = JsCode()
node.emit(bytecode)
func = bytecode.make_js_function()
- return func.run(ctx)
+ return func.run(self.ctx)
class W_ParseInt(W_NewBuiltin):
length = 1
- def Call(self, ctx, args=[], this=None):
+ def Call(self, args=[], this=None):
if len(args) < 1:
return W_FloatNumber(NAN)
- s = args[0].ToString(ctx).strip(" ")
+ s = args[0].ToString().strip(" ")
if len(args) > 1:
radix = args[1].ToInt32()
else:
@@ -94,10 +98,10 @@
class W_ParseFloat(W_NewBuiltin):
length = 1
- def Call(self, ctx, args=[], this=None):
+ def Call(self, args=[], this=None):
if len(args) < 1:
return W_FloatNumber(NAN)
- s = args[0].ToString(ctx).strip(" ")
+ s = args[0].ToString().strip(" ")
try:
n = float(s)
except ValueError:
@@ -106,7 +110,7 @@
class W_FromCharCode(W_NewBuiltin):
length = 1
- def Call(self, ctx, args=[], this=None):
+ def Call(self, args=[], this=None):
temp = []
for arg in args:
i = arg.ToInt32() % 65536 # XXX should be uint16
@@ -115,8 +119,8 @@
class W_CharAt(W_NewBuiltin):
length = 1
- def Call(self, ctx, args=[], this=None):
- string = this.ToString(ctx)
+ def Call(self, args=[], this=None):
+ string = this.ToString()
if len(args)>=1:
pos = args[0].ToInt32()
if (not pos >=0) or (pos > len(string) - 1):
@@ -126,8 +130,8 @@
return W_String(string[pos])
class W_CharCodeAt(W_NewBuiltin):
- def Call(self, ctx, args=[], this=None):
- string = this.ToString(ctx)
+ def Call(self, args=[], this=None):
+ string = this.ToString()
if len(args)>=1:
pos = args[0].ToInt32()
if pos < 0 or pos > len(string) - 1:
@@ -138,44 +142,44 @@
return W_IntNumber(ord(char))
class W_Concat(W_NewBuiltin):
- def Call(self, ctx, args=[], this=None):
- string = this.ToString(ctx)
- others = [obj.ToString(ctx) for obj in args]
+ def Call(self, args=[], this=None):
+ string = this.ToString()
+ others = [obj.ToString() for obj in args]
string += ''.join(others)
return W_String(string)
class W_IndexOf(W_NewBuiltin):
length = 1
- def Call(self, ctx, args=[], this=None):
- string = this.ToString(ctx)
+ def Call(self, args=[], this=None):
+ string = this.ToString()
if len(args) < 1:
return W_IntNumber(-1)
- substr = args[0].ToString(ctx)
+ substr = args[0].ToString()
size = len(string)
subsize = len(substr)
if len(args) < 2:
pos = 0
else:
- pos = args[1].ToInteger(ctx)
+ pos = args[1].ToInteger()
pos = int(min(max(pos, 0), size))
assert pos >= 0
return W_IntNumber(string.find(substr, pos))
class W_LastIndexOf(W_NewBuiltin):
length = 1
- def Call(self, ctx, args=[], this=None):
- string = this.ToString(ctx)
+ def Call(self, args=[], this=None):
+ string = this.ToString()
if len(args) < 1:
return W_IntNumber(-1)
- substr = args[0].ToString(ctx)
+ substr = args[0].ToString()
if len(args) < 2:
pos = INFINITY
else:
- val = args[1].ToNumber(ctx)
+ val = args[1].ToNumber()
if isnan(val):
pos = INFINITY
else:
- pos = args[1].ToInteger(ctx)
+ pos = args[1].ToInteger()
size = len(string)
pos = int(min(max(pos, 0), size))
subsize = len(substr)
@@ -185,17 +189,17 @@
class W_Substring(W_NewBuiltin):
length = 2
- def Call(self, ctx, args=[], this=None):
- string = this.ToString(ctx)
+ def Call(self, args=[], this=None):
+ string = this.ToString()
size = len(string)
if len(args) < 1:
start = 0
else:
- start = args[0].ToInteger(ctx)
+ start = args[0].ToInteger()
if len(args) < 2:
end = size
else:
- end = args[1].ToInteger(ctx)
+ end = args[1].ToInteger()
tmp1 = min(max(start, 0), size)
tmp2 = min(max(end, 0), size)
start = min(tmp1, tmp2)
@@ -204,13 +208,13 @@
class W_Split(W_NewBuiltin):
length = 2
- def Call(self, ctx, args=[], this=None):
- string = this.ToString(ctx)
+ def Call(self, args=[], this=None):
+ string = this.ToString()
if len(args) < 1 or args[0] is w_Undefined:
- return create_array(ctx, [W_String(string)])
+ return create_array([W_String(string)])
else:
- separator = args[0].ToString(ctx)
+ separator = args[0].ToString()
if len(args) >= 2:
limit = args[1].ToUInt32()
@@ -219,7 +223,7 @@
else:
array = string.split(separator)
- w_array = create_array(ctx)
+ w_array = create_array()
i = 0
while i < len(array):
w_str = W_String(array[i])
@@ -230,36 +234,36 @@
class W_ToLowerCase(W_NewBuiltin):
length = 0
- def Call(self, ctx, args=[], this=None):
- string = this.ToString(ctx)
+ def Call(self, args=[], this=None):
+ string = this.ToString()
return W_String(string.lower())
class W_ToUpperCase(W_NewBuiltin):
length = 0
- def Call(self, ctx, args=[], this=None):
- string = this.ToString(ctx)
+ def Call(self, args=[], this=None):
+ string = this.ToString()
return W_String(string.upper())
class W_ToString(W_NewBuiltin):
- def Call(self, ctx, args=[], this=None):
+ def Call(self, args=[], this=None):
assert isinstance(this, W_PrimitiveObject)
return W_String("[object %s]"%this.Class)
class W_ValueOf(W_NewBuiltin):
length = 0
- def Call(self, ctx, args=[], this=None):
+ def Call(self, args=[], this=None):
return this
class W_HasOwnProperty(W_NewBuiltin):
- def Call(self, ctx, args=[], this=None):
+ def Call(self, args=[], this=None):
if len(args) >= 1:
- propname = args[0].ToString(ctx)
+ propname = args[0].ToString()
if propname in self._get_property_keys():
return newbool(True)
return newbool(False)
class W_IsPrototypeOf(W_NewBuiltin):
- def Call(self, ctx, args=[], this=None):
+ def Call(self, args=[], this=None):
w_obj = args[0]
if len(args) >= 1 and isinstance(w_obj, W_PrimitiveObject):
O = this
@@ -273,21 +277,25 @@
return newbool(False)
class W_PropertyIsEnumerable(W_NewBuiltin):
- def Call(self, ctx, args=[], this=None):
+ def Call(self, args=[], this=None):
if len(args) >= 1:
- propname = args[0].ToString(ctx)
+ propname = args[0].ToString()
if self._has_property(propname) and not self._get_property_flags(propname) & DONT_ENUM:
return newbool(True)
return newbool(False)
class W_Function(W_NewBuiltin):
- def Call(self, ctx, args=[], this=None):
+ def __init__(self, ctx, Prototype=None, Class='function', Value=w_Undefined):
+ W_NewBuiltin.__init__(self, Prototype, Class, Value)
+ self.ctx = ctx
+
+ def Call(self, args=[], this=None):
tam = len(args)
if tam >= 1:
- fbody = args[tam-1].ToString(ctx)
+ fbody = args[tam-1].ToString()
argslist = []
for i in range(tam-1):
- argslist.append(args[i].ToString(ctx))
+ argslist.append(args[i].ToString())
fargs = ','.join(argslist)
functioncode = "function (%s) {%s}"%(fargs, fbody)
else:
@@ -299,16 +307,16 @@
bytecode = JsCode()
ast.emit(bytecode)
func = bytecode.make_js_function()
- return func.run(ctx)
+ return func.run(self.ctx)
- def Construct(self, ctx, args=[]):
- return self.Call(ctx, args, this=None)
+ def Construct(self, args=[]):
+ return self.Call(args, this=None)
functionstring= 'function (arguments go here!) {\n'+ \
' [lots of stuff :)]\n'+ \
'}'
class W_FToString(W_NewBuiltin):
- def Call(self, ctx, args=[], this=None):
+ def Call(self, args=[], this=None):
assert isinstance(this, W_PrimitiveObject)
if this.Class == 'Function':
return W_String(functionstring)
@@ -316,43 +324,51 @@
raise JsTypeError('this is not a function object')
class W_Apply(W_NewBuiltin):
- def Call(self, ctx, args=[], this=None):
+ def __init__(self, ctx):
+ W_NewBuiltin.__init__(self)
+ self.ctx = ctx
+
+ def Call(self, args=[], this=None):
try:
if isnull_or_undefined(args[0]):
- thisArg = ctx.get_global()
+ thisArg = self.ctx.get_global()
else:
- thisArg = args[0].ToObject(ctx)
+ thisArg = args[0].ToObject()
except IndexError:
- thisArg = ctx.get_global()
+ thisArg = self.ctx.get_global()
try:
arrayArgs = args[1]
callargs = arrayArgs.tolist()
except IndexError:
callargs = []
- return this.Call(ctx, callargs, this=thisArg)
+ return this.Call(callargs, this=thisArg)
class W_Call(W_NewBuiltin):
- def Call(self, ctx, args=[], this=None):
+ def __init__(self, ctx):
+ W_NewBuiltin.__init__(self)
+ self.ctx = ctx
+
+ def Call(self, args=[], this=None):
if len(args) >= 1:
if isnull_or_undefined(args[0]):
- thisArg = ctx.get_global()
+ thisArg = self.ctx.get_global()
else:
thisArg = args[0]
callargs = args[1:]
else:
- thisArg = ctx.get_global()
+ thisArg = self.ctx.get_global()
callargs = []
- return this.Call(ctx, callargs, this = thisArg)
+ return this.Call(callargs, this = thisArg)
class W_ValueToString(W_NewBuiltin):
"this is the toString function for objects with Value"
mytype = ''
- def Call(self, ctx, args=[], this=None):
+ def Call(self, args=[], this=None):
assert isinstance(this, W_PrimitiveObject)
if this.Value.type() != self.mytype:
raise JsTypeError('Wrong type')
- return W_String(this.Value.ToString(ctx))
+ return W_String(this.Value.ToString())
class W_NumberValueToString(W_ValueToString):
@@ -366,21 +382,21 @@
class W_ArrayToString(W_NewBuiltin):
length = 0
- def Call(self, ctx, args=[], this=None):
- return W_String(common_join(ctx, this, sep=','))
+ def Call(self, args=[], this=None):
+ return W_String(common_join(this, sep=','))
class W_ArrayJoin(W_NewBuiltin):
length = 1
- def Call(self, ctx, args=[], this=None):
+ def Call(self, args=[], this=None):
if len(args) >= 1 and not args[0] is w_Undefined:
- sep = args[0].ToString(ctx)
+ sep = args[0].ToString()
else:
sep = ','
- return W_String(common_join(ctx, this, sep))
+ return W_String(common_join(this, sep))
class W_ArrayPush(W_NewBuiltin):
- def Call(self, ctx, args=[], this=None):
+ def Call(self, args=[], this=None):
n = this.Get('length').ToUInt32()
for arg in args:
this.Put(str(n), arg)
@@ -390,7 +406,7 @@
return j
class W_ArrayPop(W_NewBuiltin):
- def Call(self, ctx, args=[], this=None):
+ def Call(self, args=[], this=None):
len = this.Get('length').ToUInt32()
if(len == 0):
return w_Undefined
@@ -404,7 +420,7 @@
class W_ArrayReverse(W_NewBuiltin):
length = 0
- def Call(self, ctx, args=[], this=None):
+ def Call(self, args=[], this=None):
r2 = this.Get('length').ToUInt32()
k = r_uint(0)
r3 = r_uint(math.floor( float(r2)/2.0 ))
@@ -428,7 +444,7 @@
class W_ArraySort(W_NewBuiltin):
length = 1
#XXX: further optimize this function
- def Call(self, ctx, args=[], this=None):
+ def Call(self, args=[], this=None):
length = this.Get('length').ToUInt32()
# According to ECMA-262 15.4.4.11, non-existing properties always come after
@@ -450,9 +466,9 @@
# sort all values
if len(args) > 0 and args[0] is not w_Undefined:
- sorter = Sorter(values, compare_fn=args[0], ctx=ctx)
+ sorter = Sorter(values, compare_fn=args[0])
else:
- sorter = Sorter(values, ctx=ctx)
+ sorter = Sorter(values)
sorter.sort()
# put sorted values back
@@ -474,19 +490,18 @@
return this
class W_NativeObject(W_Object):
- def __init__(self, Class, Prototype, ctx=None, Value=w_Undefined):
- W_Object.__init__(self, ctx, Prototype, Class, Value)
-
+ def __init__(self, Class, Prototype, Value=w_Undefined):
+ W_Object.__init__(self, Prototype, Class, Value)
class W_DateObject(W_NativeObject):
- def Call(self, ctx, args=[], this=None):
- return create_object(ctx, 'Object')
+ def Call(self, args=[], this=None):
+ return create_object('Object')
- def Construct(self, ctx, args=[]):
+ def Construct(self, args=[]):
v = int(time.time()*1000)
- return create_object(ctx, 'Date', Value = W_IntNumber(v))
+ return create_object('Date', Value = W_IntNumber(v))
-def pypy_repr(ctx, args, this):
+def pypy_repr(args, this):
o = args[0]
t = 'Unknown'
if isinstance(o, W_FloatNumber):
@@ -505,14 +520,14 @@
def get_value_of(type):
class W_ValueValueOf(W_NewBuiltin):
"this is the valueOf function for objects with Value"
- def Call(self, ctx, args=[], this=None):
+ def Call(self, args=[], this=None):
assert isinstance(this, W_PrimitiveObject)
if type != this.Class:
raise JsTypeError('%s.prototype.valueOf called with incompatible type' % self.type())
return this.Value
return W_ValueValueOf
-def common_join(ctx, this, sep=','):
+def common_join(this, sep=','):
length = this.Get('length').ToUInt32()
l = []
i = 0
@@ -521,61 +536,60 @@
if isnull_or_undefined(item):
item_string = ''
else:
- item_string = item.ToString(ctx)
+ item_string = item.ToString()
l.append(item_string)
i += 1
return sep.join(l)
class Sorter(TimSort):
- def __init__(self, list, listlength=None, compare_fn=None, ctx=None):
+ def __init__(self, list, listlength=None, compare_fn=None):
TimSort.__init__(self, list, listlength)
self.compare_fn = compare_fn
- self.ctx = ctx
def lt(self, a, b):
if self.compare_fn:
- result = self.compare_fn.Call(self.ctx, [a, b]).ToInt32()
+ result = self.compare_fn.Call([a, b]).ToInt32()
return result == -1
- return a.ToString(self.ctx) < b.ToString(self.ctx)
+ return a.ToString() < b.ToString()
def writer(x):
print x
-def printjs(ctx, args, this):
- writer(",".join([i.ToString(ctx) for i in args]))
+def printjs(args, this):
+ writer(",".join([i.ToString() for i in args]))
return w_Undefined
def noop(*args):
return w_Undefined
-def isnanjs(ctx, args, this):
+def isnanjs(args, this):
if len(args) < 1:
return newbool(True)
- return newbool(isnan(args[0].ToNumber(ctx)))
+ return newbool(isnan(args[0].ToNumber()))
-def isfinitejs(ctx, args, this):
+def isfinitejs(args, this):
if len(args) < 1:
return newbool(True)
- n = args[0].ToNumber(ctx)
+ n = args[0].ToNumber()
if isinf(n) or isnan(n):
return newbool(False)
else:
return newbool(True)
-def absjs(ctx, args, this):
+def absjs(args, this):
val = args[0]
if isinstance(val, W_IntNumber):
if val.intval > 0:
return val # fast path
return W_IntNumber(-val.intval)
- return W_FloatNumber(abs(args[0].ToNumber(ctx)))
+ return W_FloatNumber(abs(args[0].ToNumber()))
-def floorjs(ctx, args, this):
+def floorjs(args, this):
if len(args) < 1:
return W_FloatNumber(NAN)
- val = args[0].ToNumber(ctx)
+ val = args[0].ToNumber()
pos = math.floor(val)
if isnan(val):
@@ -583,39 +597,39 @@
return W_FloatNumber(pos)
-def roundjs(ctx, args, this):
- return floorjs(ctx, args, this)
+def roundjs(args, this):
+ return floorjs(args, this)
-def powjs(ctx, args, this):
- return W_FloatNumber(math.pow(args[0].ToNumber(ctx), args[1].ToNumber(ctx)))
+def powjs(args, this):
+ return W_FloatNumber(math.pow(args[0].ToNumber(), args[1].ToNumber()))
-def sqrtjs(ctx, args, this):
- return W_FloatNumber(math.sqrt(args[0].ToNumber(ctx)))
+def sqrtjs(args, this):
+ return W_FloatNumber(math.sqrt(args[0].ToNumber()))
-def logjs(ctx, args, this):
- return W_FloatNumber(math.log(args[0].ToNumber(ctx)))
+def logjs(args, this):
+ return W_FloatNumber(math.log(args[0].ToNumber()))
-def versionjs(ctx, args, this):
+def versionjs(args, this):
return w_Undefined
-def randomjs(ctx, args, this):
+def randomjs(args, this):
return W_FloatNumber(random.random())
-def minjs(ctx, args, this):
- a = args[0].ToNumber(ctx)
- b = args[1].ToNumber(ctx)
+def minjs(args, this):
+ a = args[0].ToNumber()
+ b = args[1].ToNumber()
return W_FloatNumber(min(a, b))
-def maxjs(ctx, args, this):
- a = args[0].ToNumber(ctx)
- b = args[1].ToNumber(ctx)
+def maxjs(args, this):
+ a = args[0].ToNumber()
+ b = args[1].ToNumber()
return W_FloatNumber(max(a, b))
def _ishex(ch):
return ((ch >= 'a' and ch <= 'f') or (ch >= '0' and ch <= '9') or
(ch >= 'A' and ch <= 'F'))
-def unescapejs(ctx, args, this):
+def unescapejs(args, this):
# XXX consider using StringBuilder here
res = []
w_string = args[0]
@@ -641,70 +655,75 @@
return W_String(''.join(res))
class W_ObjectObject(W_NativeObject):
- def Call(self, ctx, args=[], this=None):
+ def __init__(self, Class, Prototype, Value=w_Undefined):
+ W_NativeObject.__init__(self, Class, Prototype, Value)
+
+ def Call(self, args=[], this=None):
if len(args) >= 1 and not isnull_or_undefined(args[0]):
- return args[0].ToObject(ctx)
+ return args[0].ToObject()
else:
- return self.Construct(ctx)
+ return self.Construct()
- def Construct(self, ctx, args=[]):
+ def Construct(self, args=[]):
if (len(args) >= 1 and not args[0] is w_Undefined and not
args[0] is w_Null):
# XXX later we could separate builtins and normal objects
- return args[0].ToObject(ctx)
- return create_object(ctx, 'Object')
+ return args[0].ToObject()
+ return create_object('Object')
class W_BooleanObject(W_NativeObject):
- def Call(self, ctx, args=[], this=None):
+ def Call(self, args=[], this=None):
if len(args) >= 1 and not isnull_or_undefined(args[0]):
return newbool(args[0].ToBoolean())
else:
return newbool(False)
- def Construct(self, ctx, args=[]):
+ def Construct(self, args=[]):
if len(args) >= 1 and not isnull_or_undefined(args[0]):
Value = newbool(args[0].ToBoolean())
- return create_object(ctx, 'Boolean', Value = Value)
- return create_object(ctx, 'Boolean', Value = newbool(False))
+ return create_object('Boolean', Value = Value)
+ return create_object('Boolean', Value = newbool(False))
class W_NumberObject(W_NativeObject):
- def Call(self, ctx, args=[], this=None):
+ def Call(self, args=[], this=None):
if len(args) >= 1 and not isnull_or_undefined(args[0]):
- return W_FloatNumber(args[0].ToNumber(ctx))
+ return W_FloatNumber(args[0].ToNumber())
elif len(args) >= 1 and args[0] is w_Undefined:
return W_FloatNumber(NAN)
else:
return W_FloatNumber(0.0)
- def ToNumber(self, ctx):
+ def ToNumber(self):
return 0.0
- def Construct(self, ctx, args=[]):
+ def Construct(self, args=[]):
if len(args) >= 1 and not isnull_or_undefined(args[0]):
- Value = W_FloatNumber(args[0].ToNumber(ctx))
- return create_object(ctx, 'Number', Value = Value)
- return create_object(ctx, 'Number', Value = W_FloatNumber(0.0))
+ Value = W_FloatNumber(args[0].ToNumber())
+ return create_object('Number', Value = Value)
+ return create_object('Number', Value = W_FloatNumber(0.0))
class W_StringObject(W_NativeObject):
length = 1
- def Call(self, ctx, args=[], this=None):
+ def Call(self, args=[], this=None):
if len(args) >= 1:
- return W_String(args[0].ToString(ctx))
+ return W_String(args[0].ToString())
else:
return W_String('')
- def Construct(self, ctx, args=[]):
+ def Construct(self, args=[]):
if len(args) >= 1:
- Value = W_String(args[0].ToString(ctx))
+ Value = W_String(args[0].ToString())
else:
Value = W_String('')
- return Value.ToObject(ctx)
+ return Value.ToObject()
-def create_array(ctx, elements=[]):
- proto = ctx.get_global().Get('Array').Get('prototype')
+def create_array(elements=[]):
# TODO do not get array prototype from global context?
+ #proto = ctx.get_global().Get('Array').Get('prototype')
+ from js.builtins import get_builtin_prototype
+ proto = get_builtin_prototype('Array')
assert isinstance(proto, W_PrimitiveObject)
- array = W_Array(ctx, Prototype=proto, Class = proto.Class)
+ array = W_Array(Prototype=proto, Class = proto.Class)
i = 0
while i < len(elements):
array.Put(str(i), elements[i])
@@ -713,16 +732,29 @@
return array
class W_ArrayObject(W_NativeObject):
- def Call(self, ctx, args=[], this=None):
+ def __init__(self, Class, Prototype):
+ W_NativeObject.__init__(self, Class, Prototype, None )
+
+ def Call(self, args=[], this=None):
if len(args) == 1 and isinstance(args[0], W_BaseNumber):
- array = create_array(ctx)
+ array = create_array()
array.Put('length', args[0])
else:
- array = create_array(ctx, args)
+ array = create_array(args)
return array
- def Construct(self, ctx, args=[]):
- return self.Call(ctx, args)
+ def Construct(self, args=[]):
+ return self.Call(args)
+
+_builtin_prototypes = {}
+def get_builtin_prototype(name):
+ p = _builtin_prototypes.get(name, None)
+ if p is None:
+ return _builtin_prototypes.get('Object', None)
+ return p
+
+def _register_builtin_prototype(name, obj):
+ _builtin_prototypes[name] = obj
def setup_builtins(interp):
allon = DONT_ENUM | DONT_DELETE | READ_ONLY
@@ -749,43 +781,46 @@
w_Function.Put('prototype', w_FncPrototype, flags = allon)
w_Function.Put('constructor', w_Function, flags=allon)
- toString = W_ToString(ctx)
+ toString = W_ToString()
put_values(ctx, w_ObjPrototype, {
'constructor': w_Object,
'__proto__': w_Null,
'toString': toString,
'toLocaleString': toString,
- 'valueOf': W_ValueOf(ctx),
- 'hasOwnProperty': W_HasOwnProperty(ctx),
- 'isPrototypeOf': W_IsPrototypeOf(ctx),
- 'propertyIsEnumerable': W_PropertyIsEnumerable(ctx),
+ 'valueOf': W_ValueOf(),
+ 'hasOwnProperty': W_HasOwnProperty(),
+ 'isPrototypeOf': W_IsPrototypeOf(),
+ 'propertyIsEnumerable': W_PropertyIsEnumerable(),
})
+ _register_builtin_prototype('Object', w_ObjPrototype)
#properties of the function prototype
put_values(ctx, w_FncPrototype, {
'constructor': w_Function,
'__proto__': w_FncPrototype,
- 'toString': W_FToString(ctx),
+ 'toString': W_FToString(),
'apply': W_Apply(ctx),
'call': W_Call(ctx),
'arguments': w_Null,
- 'valueOf': W_ValueOf(ctx),
+ 'valueOf': W_ValueOf(),
})
+ _register_builtin_prototype('Function', w_FncPrototype)
w_Boolean = W_BooleanObject('Boolean', w_FncPrototype)
w_Boolean.Put('constructor', w_FncPrototype, flags = allon)
w_Boolean.Put('length', W_IntNumber(1), flags = allon)
- w_BoolPrototype = create_object(ctx, 'Object', Value=newbool(False))
+ w_BoolPrototype = create_object('Object', Value=newbool(False))
w_BoolPrototype.Class = 'Boolean'
put_values(ctx, w_BoolPrototype, {
'constructor': w_FncPrototype,
'__proto__': w_ObjPrototype,
- 'toString': W_BooleanValueToString(ctx),
- 'valueOf': get_value_of('Boolean')(ctx),
+ 'toString': W_BooleanValueToString(),
+ 'valueOf': get_value_of('Boolean')(),
})
+ _register_builtin_prototype('Boolean', w_BoolPrototype)
w_Boolean.Put('prototype', w_BoolPrototype, flags = allon)
w_Global.Put('Boolean', w_Boolean)
@@ -793,16 +828,17 @@
#Number
w_Number = W_NumberObject('Number', w_FncPrototype)
- w_empty_fun = w_Function.Call(ctx, args=[W_String('')])
+ w_empty_fun = w_Function.Call(args=[W_String('')])
- w_NumPrototype = create_object(ctx, 'Object', Value=W_FloatNumber(0.0))
+ w_NumPrototype = create_object('Object', Value=W_FloatNumber(0.0))
w_NumPrototype.Class = 'Number'
put_values(ctx, w_NumPrototype, {
'constructor': w_Number,
'__proto__': w_empty_fun,
- 'toString': W_NumberValueToString(ctx),
- 'valueOf': get_value_of('Number')(ctx),
+ 'toString': W_NumberValueToString(),
+ 'valueOf': get_value_of('Number')(),
})
+ _register_builtin_prototype('Number', w_NumPrototype)
put_values(ctx, w_Number, {
'constructor': w_FncPrototype,
@@ -826,28 +862,29 @@
#String
w_String = W_StringObject('String', w_FncPrototype)
- w_StrPrototype = create_object(ctx, 'Object', Value=W_String(''))
+ w_StrPrototype = create_object('Object', Value=W_String(''))
w_StrPrototype.Class = 'String'
w_StrPrototype.Put('length', W_IntNumber(0))
put_values(ctx, w_StrPrototype, {
'constructor': w_String,
'__proto__': w_StrPrototype,
- 'toString': W_StringValueToString(ctx),
- 'valueOf': get_value_of('String')(ctx),
- 'charAt': W_CharAt(ctx),
- 'charCodeAt': W_CharCodeAt(ctx),
- 'concat': W_Concat(ctx),
- 'indexOf': W_IndexOf(ctx),
- 'lastIndexOf': W_LastIndexOf(ctx),
- 'substring': W_Substring(ctx),
- 'split': W_Split(ctx),
- 'toLowerCase': W_ToLowerCase(ctx),
- 'toUpperCase': W_ToUpperCase(ctx)
+ 'toString': W_StringValueToString(),
+ 'valueOf': get_value_of('String')(),
+ 'charAt': W_CharAt(),
+ 'charCodeAt': W_CharCodeAt(),
+ 'concat': W_Concat(),
+ 'indexOf': W_IndexOf(),
+ 'lastIndexOf': W_LastIndexOf(),
+ 'substring': W_Substring(),
+ 'split': W_Split(),
+ 'toLowerCase': W_ToLowerCase(),
+ 'toUpperCase': W_ToUpperCase()
})
+ _register_builtin_prototype('String', w_StrPrototype)
w_String.Put('prototype', w_StrPrototype, flags=allon)
- w_String.Put('fromCharCode', W_FromCharCode(ctx))
+ w_String.Put('fromCharCode', W_FromCharCode())
w_Global.Put('String', w_String)
w_Array = W_ArrayObject('Array', w_FncPrototype)
@@ -857,13 +894,14 @@
put_values(ctx, w_ArrPrototype, {
'constructor': w_FncPrototype,
'__proto__': w_ArrPrototype,
- 'toString': W_ArrayToString(ctx),
- 'join': W_ArrayJoin(ctx),
- 'reverse': W_ArrayReverse(ctx),
- 'sort': W_ArraySort(ctx),
- 'push': W_ArrayPush(ctx),
- 'pop': W_ArrayPop(ctx),
+ 'toString': W_ArrayToString(),
+ 'join': W_ArrayJoin(),
+ 'reverse': W_ArrayReverse(),
+ 'sort': W_ArraySort(),
+ 'push': W_ArrayPush(),
+ 'pop': W_ArrayPop(),
})
+ _register_builtin_prototype('Array', w_ArrPrototype)
w_Array.Put('prototype', w_ArrPrototype, flags = allon)
w_Array.Put('__proto__', w_FncPrototype, flags = allon)
@@ -899,14 +937,15 @@
#Date
w_Date = W_DateObject('Date', w_FncPrototype)
- w_DatePrototype = create_object(ctx, 'Object', Value=W_String(''))
+ w_DatePrototype = create_object('Object', Value=W_String(''))
w_DatePrototype.Class = 'Date'
put_values(ctx, w_DatePrototype, {
'__proto__': w_DatePrototype,
- 'valueOf': get_value_of('Date')(ctx),
- 'getTime': get_value_of('Date')(ctx)
+ 'valueOf': get_value_of('Date')(),
+ 'getTime': get_value_of('Date')()
})
+ _register_builtin_prototype('Date', w_DatePrototype)
w_Date.Put('prototype', w_DatePrototype, flags=allon)
@@ -916,8 +955,8 @@
w_Global.Put('Infinity', W_FloatNumber(INFINITY), flags = DONT_ENUM | DONT_DELETE)
w_Global.Put('undefined', w_Undefined, flags = DONT_ENUM | DONT_DELETE)
w_Global.Put('eval', W_Eval(ctx))
- w_Global.Put('parseInt', W_ParseInt(ctx))
- w_Global.Put('parseFloat', W_ParseFloat(ctx))
+ w_Global.Put('parseInt', W_ParseInt())
+ w_Global.Put('parseFloat', W_ParseFloat())
w_Global.Put('isNaN', W_Builtin(isnanjs))
w_Global.Put('isFinite', W_Builtin(isfinitejs))
w_Global.Put('print', W_Builtin(printjs))
diff --git a/js/jsobj.py b/js/jsobj.py
--- a/js/jsobj.py
+++ b/js/jsobj.py
@@ -32,21 +32,21 @@
def ToBoolean(self):
raise NotImplementedError(self.__class__)
- def ToPrimitive(self, ctx, hint=""):
+ def ToPrimitive(self, hint=""):
return self
- def ToString(self, ctx):
+ def ToString(self):
return ''
- def ToObject(self, ctx):
+ def ToObject(self):
# XXX should raise not implemented
return self
- def ToNumber(self, ctx = None):
+ def ToNumber(self):
return 0.0
- def ToInteger(self, ctx):
- return int(self.ToNumber(ctx = None))
+ def ToInteger(self):
+ return int(self.ToNumber())
def ToInt32(self):
return r_int32(int(self.ToNumber()))
@@ -60,17 +60,17 @@
def Put(self, P, V, flags = 0):
raise NotImplementedError(self.__class__)
- def PutValue(self, w, ctx):
+ def PutValue(self, w):
pass
def CanPut(self, P):
return False
- def Call(self, ctx, args=[], this=None):
+ def Call(self, args=[], this=None):
raise NotImplementedError(self.__class__)
def __str__(self):
- return self.ToString(ctx=None)
+ return self.ToString()
def type(self):
raise NotImplementedError(self.__class__)
@@ -88,16 +88,16 @@
def __str__(self):
return "w_undefined"
- def ToInteger(self, ctx):
+ def ToInteger(self):
return 0
- def ToNumber(self, ctx = None):
+ def ToNumber(self):
return NAN
def ToBoolean(self):
return False
- def ToString(self, ctx):
+ def ToString(self):
return "undefined"
def type(self):
@@ -113,7 +113,7 @@
def ToBoolean(self):
return False
- def ToString(self, ctx):
+ def ToString(self):
return "null"
def type(self):
@@ -152,8 +152,8 @@
return True
class W_PrimitiveObject(W_Root):
- _immutable_fields_ = ['Class', 'Property', 'Scope', 'Value']
- def __init__(self, ctx=None, Prototype=None, Class='Object', Value=w_Undefined):
+ _immutable_fields_ = ['Class', 'Prototype', 'Scope', 'Value']
+ def __init__(self, Prototype=None, Class='Object', Value=w_Undefined):
self.Prototype = Prototype
self.property_map = root_map()
self.property_values = []
@@ -217,19 +217,21 @@
def _get_property_keys(self):
return self.property_map.keys()
- def Call(self, ctx, args=[], this=None):
+ def Call(self, args=[], this=None):
raise JsTypeError('not a function')
- def Construct(self, ctx, args=[]):
+ def Construct(self, args=[]):
obj = W_Object(Class='Object')
prot = self.Get('prototype')
if isinstance(prot, W_PrimitiveObject):
obj.Prototype = prot
else: # would love to test this
#but I fail to find a case that falls into this
- obj.Prototype = ctx.get_global().Get('Object').Get('prototype')
+ #obj.Prototype = ctx.get_global().Get('Object').Get('prototype')
+ from js.builtins import get_builtin_prototype
+ obj.Prototype = get_builtin_prototype('Object')
try: #this is a hack to be compatible to spidermonkey
- self.Call(ctx, args, this=obj)
+ self.Call(args, this=obj)
return obj
except ReturnException, e:
return e.value
@@ -272,36 +274,37 @@
return True
return True
- def internal_def_value(self, ctx, tryone, trytwo):
+ def internal_def_value(self, tryone, trytwo):
+ # XXX: redo this!
t1 = self.Get(tryone)
if isinstance(t1, W_PrimitiveObject):
- val = t1.Call(ctx, this=self)
+ val = t1.Call(this=self)
if isinstance(val, W_Primitive):
return val
t2 = self.Get(trytwo)
if isinstance(t2, W_PrimitiveObject):
- val = t2.Call(ctx, this=self)
+ val = t2.Call(this=self)
if isinstance(val, W_Primitive):
return val
raise JsTypeError
- def DefaultValue(self, ctx, hint=""):
+ def DefaultValue(self, hint=""):
if hint == "String":
- return self.internal_def_value(ctx, "toString", "valueOf")
+ return self.internal_def_value("toString", "valueOf")
else: # hint can only be empty, String or Number
- return self.internal_def_value(ctx, "valueOf", "toString")
+ return self.internal_def_value("valueOf", "toString")
ToPrimitive = DefaultValue
def ToBoolean(self):
return True
- def ToString(self, ctx):
+ def ToString(self):
try:
- res = self.ToPrimitive(ctx, 'String')
+ res = self.ToPrimitive('String')
except JsTypeError:
return "[object %s]"%(self.Class,)
- return res.ToString(ctx)
+ return res.ToString()
def __str__(self):
return "<Object class: %s>" % self.Class
@@ -309,30 +312,22 @@
def type(self):
return 'object'
-def str_builtin(ctx, args, this):
- return W_String(this.ToString(ctx))
+class W_Object(W_PrimitiveObject):
+ def __init__(self, Prototype=None, Class='Object', Value=w_Undefined):
+ W_PrimitiveObject.__init__(self, Prototype, Class, Value)
-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 = None):
- return self.Get('valueOf').Call(ctx, args=[], this=self).ToNumber(ctx)
+ def ToNumber(self):
+ return self.Get('valueOf').Call(args=[], this=self).ToNumber()
class W_CallableObject(W_Object):
_immutable_fields_ = ['callfunc', 'ctx']
def __init__(self, ctx, Prototype, callfunc):
- W_Object.__init__(self, ctx, Prototype, 'Function')
+ W_Object.__init__(self, 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)
-
+ def Call(self, args=[], this=None):
from js.jsexecution_context import make_activation_context, make_function_context
w_Arguments = W_Arguments(self, args)
@@ -357,42 +352,42 @@
class W_Primitive(W_Root):
"""unifying parent for primitives"""
- def ToPrimitive(self, ctx, hint=""):
+ def ToPrimitive(self, hint=""):
return self
-
class W_NewBuiltin(W_PrimitiveObject):
length = -1
- def __init__(self, ctx, Prototype=None, Class='function', Value=w_Undefined):
+ def __init__(self, Prototype=None, Class='function', Value=w_Undefined):
if Prototype is None:
- proto = ctx.get_global().Get('Function').Get('prototype')
+ #proto = ctx.get_global().Get('Function').Get('prototype')
+ from js.builtins import get_builtin_prototype
+ proto = get_builtin_prototype('Function')
Prototype = proto
- W_PrimitiveObject.__init__(self, ctx, Prototype, Class, Value)
+ W_PrimitiveObject.__init__(self, Prototype, Class, Value)
if self.length != -1:
self.Put('length', W_IntNumber(self.length), flags = DONT_ENUM|DONT_DELETE|READ_ONLY)
-
- def Call(self, ctx, args=[], this = None):
+ def Call(self, args=[], this = None):
raise NotImplementedError
def type(self):
return self.Class
class W_Builtin(W_PrimitiveObject):
- def __init__(self, builtin=None, ctx=None, Prototype=None, Class='function', Value=w_Undefined):
- W_PrimitiveObject.__init__(self, ctx, Prototype, Class, Value)
+ def __init__(self, builtin=None, Prototype=None, Class='function', Value=w_Undefined):
+ W_PrimitiveObject.__init__(self, Prototype, Class, Value)
self.set_builtin_call(builtin)
def set_builtin_call(self, callfuncbi):
self.callfuncbi = callfuncbi
- def Call(self, ctx, args=[], this = None):
- return self.callfuncbi(ctx, args, this)
+ def Call(self, args=[], this = None):
+ return self.callfuncbi(args, this)
- def Construct(self, ctx, args=[]):
- return self.callfuncbi(ctx, args, None)
+ def Construct(self, args=[]):
+ return self.callfuncbi(args, None)
def type(self):
return self.Class
@@ -425,8 +420,8 @@
return str(self.property_map)
class W_Array(W_ListObject):
- def __init__(self, ctx=None, Prototype=None, Class='Array', Value=w_Undefined):
- W_ListObject.__init__(self, ctx, Prototype, Class, Value)
+ def __init__(self, Prototype=None, Class='Array', Value=w_Undefined):
+ W_ListObject.__init__(self, Prototype, Class, Value)
self.Put('length', W_IntNumber(0), flags = DONT_DELETE)
self.length = r_uint(0)
@@ -474,15 +469,15 @@
def __init__(self, boolval):
self.boolval = bool(boolval)
- def ToObject(self, ctx):
- return create_object(ctx, 'Boolean', Value=self)
+ def ToObject(self):
+ return create_object('Boolean', Value=self)
- def ToString(self, ctx=None):
+ def ToString(self):
if self.boolval == True:
return "true"
return "false"
- def ToNumber(self, ctx = None):
+ def ToNumber(self):
if self.boolval:
return 1.0
return 0.0
@@ -505,12 +500,12 @@
def __repr__(self):
return 'W_String(%s)' % (self.strval,)
- def ToObject(self, ctx):
- o = create_object(ctx, 'String', Value=self)
+ def ToObject(self):
+ o = create_object('String', Value=self)
o.Put('length', W_IntNumber(len(self.strval)), flags = READ_ONLY | DONT_DELETE | DONT_ENUM)
return o
- def ToString(self, ctx=None):
+ def ToString(self):
return self.strval
def ToBoolean(self):
@@ -525,7 +520,7 @@
def GetPropertyName(self):
return self.ToString()
- def ToNumber(self, ctx = None):
+ def ToNumber(self):
if not self.strval:
return 0.0
try:
@@ -544,8 +539,8 @@
""" Base class for numbers, both known to be floats
and those known to be integers
"""
- def ToObject(self, ctx):
- return create_object(ctx, 'Number', Value=self)
+ def ToObject(self):
+ return create_object('Number', Value=self)
def Get(self, P):
return w_Undefined
@@ -561,14 +556,14 @@
W_BaseNumber.__init__(self)
self.intval = intmask(intval)
- def ToString(self, ctx=None):
+ def ToString(self):
# XXX incomplete, this doesn't follow the 9.8.1 recommendation
return str(self.intval)
def ToBoolean(self):
return bool(self.intval)
- def ToNumber(self, ctx = None):
+ def ToNumber(self):
# XXX
return float(self.intval)
@@ -598,7 +593,7 @@
W_BaseNumber.__init__(self)
self.floatval = float(floatval)
- def ToString(self, ctx = None):
+ def ToString(self):
# XXX incomplete, this doesn't follow the 9.8.1 recommendation
if isnan(self.floatval):
return 'NaN'
@@ -624,10 +619,10 @@
return False
return bool(self.floatval)
- def ToNumber(self, ctx = None):
+ def ToNumber(self):
return self.floatval
- def ToInteger(self, ctx):
+ def ToInteger(self):
if isnan(self.floatval):
return 0
@@ -653,7 +648,7 @@
def __init__(self, list_w):
self.list_w = list_w
- def ToString(self, ctx = None):
+ def ToString(self):
raise SeePage(42)
def ToBoolean(self):
@@ -679,11 +674,13 @@
def empty(self):
return len(self.elements_w) == 0
-def create_object(ctx, prototypename, Value=w_Undefined):
- proto = ctx.get_global().Get(prototypename).Get('prototype')
+def create_object(prototypename, Value=w_Undefined):
+ #proto = ctx.get_global().Get(prototypename).Get('prototype')
+ from js.builtins import get_builtin_prototype
+ proto = get_builtin_prototype(prototypename)
# TODO get Object prototype from interp.w_Object
assert isinstance(proto, W_PrimitiveObject)
- obj = W_Object(ctx, Prototype=proto, Class = proto.Class, Value = Value)
+ obj = W_Object(Prototype=proto, Class = proto.Class, Value = Value)
obj.Put('__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
@@ -129,7 +129,7 @@
# TODO get array prototype?
# builtins make_array??
assert isinstance(proto, W_PrimitiveObject)
- array = W_Array(ctx, Prototype=proto, Class = proto.Class)
+ array = W_Array(Prototype=proto, Class = proto.Class)
for i in range(self.counter):
array.Put(str(self.counter - i - 1), ctx.pop())
ctx.append(array)
@@ -160,10 +160,12 @@
self.funcobj = funcobj
def eval(self, ctx):
- proto = ctx.get_global().Get('Function').Get('prototype')
+ #proto = ctx.get_global().Get('Function').Get('prototype')
+ from js.builtins import get_builtin_prototype
+ proto = get_builtin_prototype('Function')
w_func = W_CallableObject(ctx, proto, self.funcobj)
w_func.Put('length', W_IntNumber(len(self.funcobj.params)))
- w_obj = create_object(ctx, 'Object')
+ w_obj = create_object('Object')
w_obj.Put('constructor', w_func, flags = jsobj.DONT_ENUM)
w_func.Put('prototype', w_obj)
ctx.append(w_func)
@@ -190,9 +192,9 @@
@jit.unroll_safe
def eval(self, ctx):
- w_obj = create_object(ctx, 'Object')
+ w_obj = create_object('Object')
for _ in range(self.counter):
- name = ctx.pop().ToString(ctx)
+ name = ctx.pop().ToString()
w_elem = ctx.pop()
w_obj.Put(name, w_elem)
ctx.append(w_obj)
@@ -203,8 +205,8 @@
class LOAD_MEMBER(Opcode):
_stack_change = -1
def eval(self, ctx):
- w_obj = ctx.pop().ToObject(ctx)
- name = ctx.pop().ToString(ctx)
+ w_obj = ctx.pop().ToObject()
+ name = ctx.pop().ToString()
ctx.append(w_obj.Get(name))
class COMMA(BaseUnaryOperation):
@@ -222,7 +224,7 @@
def operation(self, ctx, left, right):
if not isinstance(right, W_Object):
raise ThrowException(W_String("TypeError"))
- name = left.ToString(ctx)
+ name = left.ToString()
return newbool(right.HasProperty(name))
class TYPEOF(BaseUnaryOperation):
@@ -306,7 +308,7 @@
return
if isinstance(ctx.top(), W_FloatNumber):
return
- ctx.append(W_FloatNumber(ctx.pop().ToNumber(ctx)))
+ ctx.append(W_FloatNumber(ctx.pop().ToNumber()))
class UMINUS(BaseUnaryOperation):
def eval(self, ctx):
@@ -366,8 +368,8 @@
left = ctx.pop()
member = ctx.pop()
value = ctx.pop()
- name = member.ToString(ctx)
- left.ToObject(ctx).Put(name, value)
+ name = member.ToString()
+ left.ToObject().Put(name, value)
ctx.append(value)
class STORE(Opcode):
@@ -454,10 +456,12 @@
def eval(self, ctx):
# function declaration actyally don't run anything
- proto = ctx.get_global().Get('Function').Get('prototype')
+ #proto = ctx.get_global().Get('Function').Get('prototype')
+ from js.builtins import get_builtin_prototype
+ proto = get_builtin_prototype('Function')
w_func = W_CallableObject(ctx, proto, self.funcobj)
w_func.Put('length', W_IntNumber(len(self.funcobj.params)))
- w_obj = create_object(ctx, 'Object')
+ w_obj = create_object('Object')
w_obj.Put('constructor', w_func, flags = jsobj.DONT_ENUM)
w_func.Put('prototype', w_obj)
if self.funcobj.name is not None:
@@ -495,12 +499,12 @@
def common_call(ctx, r1, args, this, name):
if not isinstance(r1, W_PrimitiveObject):
- raise ThrowException(W_String("%s is not a callable (%s)"%(r1.ToString(ctx), name)))
+ raise ThrowException(W_String("%s is not a callable (%s)"%(r1.ToString(), name)))
jit.promote(r1)
try:
- res = r1.Call(ctx=ctx, args=args.tolist(), this=this)
+ res = r1.Call(args.tolist(), this)
except JsTypeError:
- raise ThrowException(W_String("%s is not a function (%s)"%(r1.ToString(ctx), name)))
+ raise ThrowException(W_String("%s is not a function (%s)"%(r1.ToString(), name)))
return res
class CALL(Opcode):
@@ -508,7 +512,7 @@
def eval(self, ctx):
r1 = ctx.pop()
args = ctx.pop()
- name = r1.ToString(ctx)
+ name = r1.ToString()
this = ctx.to_context_object()
#XXX hack, this should be comming from context
ctx.append(common_call(ctx, r1, args, this, name))
@@ -517,9 +521,9 @@
_stack_change = -2
def eval(self, ctx):
method = ctx.pop()
- what = ctx.pop().ToObject(ctx)
+ what = ctx.pop().ToObject()
args = ctx.pop()
- name = method.ToString(ctx)
+ name = method.ToString()
r1 = what.Get(name)
ctx.append(common_call(ctx, r1, args, what, name))
@@ -584,7 +588,7 @@
class LOAD_ITERATOR(Opcode):
_stack_change = 0
def eval(self, ctx):
- obj = ctx.pop().ToObject(ctx)
+ obj = ctx.pop().ToObject()
props = []
assert isinstance(obj, W_PrimitiveObject)
@@ -623,7 +627,7 @@
self.newctx = None
def eval(self, ctx):
- obj = ctx.pop().ToObject(ctx)
+ obj = ctx.pop().ToObject()
from js.jsexecution_context import make_with_context
self.newctx = make_with_context(ctx, obj)
@@ -644,8 +648,8 @@
class DELETE_MEMBER(Opcode):
_stack_change = 0
def eval(self, ctx):
- what = ctx.pop().ToString(ctx)
- obj = ctx.pop().ToObject(ctx)
+ what = ctx.pop().ToString()
+ obj = ctx.pop().ToObject()
ctx.append(newbool(obj.Delete(what)))
class LOAD_LOCAL(Opcode):
diff --git a/js/test/ecma/conftest.py b/js/test/ecma/conftest.py
--- a/js/test/ecma/conftest.py
+++ b/js/test/ecma/conftest.py
@@ -34,7 +34,7 @@
def overriden_evaljs(ctx, args, this):
try:
w_eval = W_Eval(ctx)
- return w_eval.Call(ctx, args, this)
+ return w_eval.Call(args, this)
except JsBaseExcept:
return W_String("error")
diff --git a/js/test/test_interp.py b/js/test/test_interp.py
--- a/js/test/test_interp.py
+++ b/js/test/test_interp.py
@@ -15,7 +15,7 @@
bytecode.emit('POP')
func = bytecode.make_js_function()
res = func.run(ExecutionContext(), check_stack=False)
- assert res.ToNumber(None) == 6.0
+ assert res.ToNumber() == 6.0
def assertp(code, prints):
l = []
@@ -26,7 +26,7 @@
try:
jsint.run(interpreter.load_source(code, ''))
except ThrowException, excpt:
- l.append("uncaught exception: "+str(excpt.exception.ToString(ctx)))
+ l.append("uncaught exception: "+str(excpt.exception.ToString()))
print l, prints
if isinstance(prints, list):
assert l == prints
@@ -53,7 +53,7 @@
elif isinstance(value, float):
assert code_val.ToNumber() == value
else:
- assert code_val.ToString(jsint.global_context) == value
+ assert code_val.ToString() == value
def asserte(code, value):
jsint = interpreter.Interpreter()
More information about the pypy-commit
mailing list