[pypy-commit] lang-js default: lots of uncicode related fixes for translator
stepahn
noreply at buildbot.pypy.org
Fri Dec 28 11:34:45 CET 2012
Author: Stephan <stephan at stzal.com>
Branch:
Changeset: r263:143a45444a00
Date: 2012-06-19 20:45 +0200
http://bitbucket.org/pypy/lang-js/changeset/143a45444a00/
Log: lots of uncicode related fixes for translator
diff too long, truncating to 2000 out of 3347 lines
diff --git a/js/astbuilder.py b/js/astbuilder.py
--- a/js/astbuilder.py
+++ b/js/astbuilder.py
@@ -17,11 +17,13 @@
self.next_index = 0
def add_symbol(self, identifyer):
- assert identifyer is not None
+ assert isinstance(identifyer, unicode)
if identifyer not in self.symbols:
self.symbols[identifyer] = self.next_index
self.next_index += 1
- return self.symbols[identifyer]
+ idx = self.symbols[identifyer]
+ assert isinstance(idx, int)
+ return idx
def add_variable(self, identifyer):
idx = self.add_symbol(identifyer)
@@ -36,9 +38,11 @@
return idx
def add_parameter(self, identifyer):
- idx = self.add_symbol(identifyer)
-
- self.parameters.append(identifyer)
+ assert isinstance(identifyer, unicode)
+ f = unicode(identifyer)
+ assert isinstance(f, unicode)
+ idx = self.add_symbol(f)
+ self.parameters.append(f)
return idx
def get_index(self, identifyer):
@@ -118,18 +122,24 @@
#print 'starting new scope %d' % (self.depth, )
def declare_symbol(self, symbol):
- idx = self.scopes[-1].add_symbol(symbol)
+ s = unicode(symbol)
+ assert isinstance(s, unicode)
+ idx = self.scopes[-1].add_symbol(s)
#print 'symbol "%s"@%d in scope %d' % (symbol, idx, self.depth,)
return idx
def declare_variable(self, symbol):
- idx = self.scopes[-1].add_variable(symbol)
+ s = unicode(symbol)
+ assert isinstance(s, unicode)
+ idx = self.scopes[-1].add_variable(s)
#print 'var declaration "%s"@%d in scope %d' % (symbol, idx, self.depth,)
return idx
def declare_function(self, symbol, funcobj):
- self.funclists[-1][symbol] = funcobj
- idx = self.scopes[-1].add_function(symbol)
+ s = unicode(symbol)
+ assert isinstance(s, unicode)
+ self.funclists[-1][s] = funcobj
+ idx = self.scopes[-1].add_function(s)
#print 'func declaration "%s"@%d in scope %d' % (symbol, idx, self.depth,)
return idx
@@ -206,7 +216,7 @@
def string(self,node):
pos = self.get_pos(node)
- return operations.String(pos, node.additional_info)
+ return operations.String(pos, unicode(node.additional_info))
visit_DOUBLESTRING = string
visit_SINGLESTRING = string
@@ -290,13 +300,15 @@
visit_objectliteral = listop
def visit_block(self, node):
- def isnotempty(node):
- return node is not None and not isinstance(node, operations.Empty)
-
op = node.children[0]
pos = self.get_pos(op)
l = [self.dispatch(child) for child in node.children[1:]]
- nodes = [node for node in l if isnotempty(node)]
+
+ nodes = []
+ for node in l:
+ if not(node is None or isinstance(node, operations.Empty)):
+ nodes.append(node)
+
return operations.Block(pos, nodes)
def visit_arguments(self, node):
@@ -322,9 +334,11 @@
l = node.children[0]
if l.symbol == "IDENTIFIERNAME":
identifier = l.additional_info
- index = self.declare_symbol(identifier)
+ i = unicode(identifier)
+ assert isinstance(i, unicode)
+ index = self.declare_symbol(i)
lpos = self.get_pos(l)
- left = operations.Identifier(lpos, identifier, index)
+ left = operations.Identifier(lpos, i, index)
else:
left = self.dispatch(l)
right = self.dispatch(node.children[1])
@@ -333,11 +347,13 @@
def visit_IDENTIFIERNAME(self, node):
pos = self.get_pos(node)
name = node.additional_info
- index = self.declare_symbol(name)
+ n = unicode(name)
+ assert isinstance(n, unicode)
+ index = self.declare_symbol(n)
#if self.scopes.is_local(name):
#local = self.scopes.get_local(name)
#return operations.LocalIdentifier(pos, name, local)
- return operations.Identifier(pos, name, index)
+ return operations.Identifier(pos, n, index)
def visit_program(self, node):
self.enter_scope()
@@ -361,12 +377,9 @@
self.funclists.append({})
nodes=[]
- def isnotempty(node):
- return node is not None and not isinstance(node, operations.Empty)
-
for child in node.children:
n = self.dispatch(child)
- if isnotempty(n):
+ if not (n is None or isinstance(n, operations.Empty)):
nodes.append(n)
var_decl = self.current_scope_variables()
@@ -389,18 +402,20 @@
params = self.current_scope_parameters()
- funcname = None
if identifier is not None:
funcname = identifier.get_literal()
+ else:
+ funcname = u''
scope = self.current_scope()
self.exit_scope()
- funcindex = None
-
+ funcindex = -1
if declaration:
- funcindex = self.declare_symbol(funcname)
+ f = unicode(funcname)
+ assert isinstance(f, unicode)
+ funcindex = self.declare_symbol(f)
funcobj = operations.FunctionStatement(pos, funcname, funcindex, functionbody, scope)
@@ -454,8 +469,8 @@
return left
def is_identifier(self, obj):
- from js.operations import Identifier, VariableIdentifier
- return isinstance(obj, Identifier) or isinstance(obj, VariableIdentifier)
+ from js.operations import Identifier
+ return isinstance(obj, Identifier)
def is_member(self, obj):
from js.operations import Member, MemberDot
@@ -477,7 +492,9 @@
return operations.LocalAssignmentOperation(pos, left, right, operation)
elif self.is_identifier(left):
identifier = left.get_literal()
- index = self.declare_symbol(identifier)
+ i = unicode(identifier)
+ assert isinstance(i, unicode)
+ index = self.declare_symbol(i)
return operations.AssignmentOperation(pos, left, identifier, index, right, operation)
elif self.is_member(left):
return operations.MemberAssignmentOperation(pos, left, right, operation)
@@ -654,8 +671,7 @@
def visit_primaryexpression(self, node):
pos = self.get_pos(node)
- index = self.declare_symbol('this')
- return operations.This(pos, 'this', index)
+ return operations.This(pos)
def visit_withstatement(self, node):
pos = self.get_pos(node)
@@ -689,7 +705,8 @@
return tree
def parse_to_ast(code):
+ assert isinstance(code, unicode)
from js.jsparser import parse, ParseError
- parse_tree = parse(code.encode('utf-8'))
+ parse_tree = parse(str(code))
ast = parse_tree_to_ast(parse_tree)
return ast
diff --git a/js/baseop.py b/js/baseop.py
--- a/js/baseop.py
+++ b/js/baseop.py
@@ -93,17 +93,18 @@
from math import copysign
return copysign(1.0, x)
+def sign_of(a, b):
+ sign_a = sign(a)
+ sign_b = sign(b)
+ return sign_a * sign_b
+
+def w_signed_inf(sign):
+ if sign < 0.0:
+ return w_NEGATIVE_INFINITY
+ return w_POSITIVE_INFINITY
+
# 11.5.2
def division(ctx, nleft, nright):
- def sign_of(a, b):
- sign_a = sign(a)
- sign_b = sign(b)
- return sign_a * sign_b
-
- def w_signed_inf(sign):
- if sign < 0.0:
- return w_NEGATIVE_INFINITY
- return w_POSITIVE_INFINITY
fleft = nleft.ToNumber()
fright = nright.ToNumber()
diff --git a/js/builtins.py b/js/builtins.py
--- a/js/builtins.py
+++ b/js/builtins.py
@@ -41,12 +41,11 @@
_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, 'Object', w_Object)
+ put_property(global_object, u'Object', w_Object)
# 15.2.4
from js.jsobj import W_BasicObject
@@ -55,11 +54,11 @@
# 15.3.2
from js.jsobj import W_FunctionConstructor
w_Function = W_FunctionConstructor()
- put_property(global_object, 'Function', w_Function)
+ put_property(global_object, u'Function', w_Function)
# 15.3.4
import js.builtins_function as function_builtins
- w_FunctionPrototype = new_native_function(function_builtins.empty, 'Empty')
+ w_FunctionPrototype = new_native_function(function_builtins.empty, u'Empty')
# 15.2.4 Properties of the Object Prototype Object
w_ObjectPrototype._prototype_ = w_Null
@@ -76,48 +75,47 @@
# 15.2 Object Objects
# 15.2.3 Properties of the Object Constructor
- #w_Object._prototype_ = w_FunctionPrototype
- #del(w_Object._properties_['__proto__'])
+ w_Object._prototype_ = w_FunctionPrototype
+ del(w_Object._properties_['__proto__'])
#put_property(w_Object, '__proto__', w_Object._prototype_)
-
- put_property(w_Object, 'length', _w(1))
+ put_property(w_Object, u'length', _w(1))
# 15.2.3.1 Object.prototype
- put_property(w_Object, 'prototype', w_ObjectPrototype, writable = False, configurable = False, enumerable = False)
+ put_property(w_Object, u'prototype', w_ObjectPrototype, writable = False, configurable = False, enumerable = False)
# 14.2.4.1 Object.prototype.constructor
- put_property(w_ObjectPrototype, 'constructor', w_Object)
+ put_property(w_ObjectPrototype, u'constructor', w_Object)
import js.builtins_object as object_builtins
# 15.2.4.2 Object.prototype.toString()
- put_native_function(w_ObjectPrototype, 'toString', object_builtins.to_string)
- put_native_function(w_ObjectPrototype, 'toLocaleString', object_builtins.to_string)
+ put_native_function(w_ObjectPrototype, u'toString', object_builtins.to_string)
+ put_native_function(w_ObjectPrototype, u'toLocaleString', object_builtins.to_string)
# 15.2.4.3 Object.prototype.valueOf()
- put_native_function(w_ObjectPrototype, 'valueOf', object_builtins.value_of)
+ put_native_function(w_ObjectPrototype, u'valueOf', object_builtins.value_of)
# 15.3 Function Objects
# 15.3.3 Properties of the Function Constructor
# 15.3.3.1 Function.prototype
- put_property(w_Function, 'prototype', w_FunctionPrototype, writable = False, configurable = False, enumerable = False)
+ put_property(w_Function, u'prototype', w_FunctionPrototype, writable = False, configurable = False, enumerable = False)
# 15.3.3.2 Function.length
- put_property(w_Function, 'length', _w(1), writable = False, configurable = False, enumerable = False)
+ put_property(w_Function, u'length', _w(1), writable = False, configurable = False, enumerable = False)
# 14.3.4.1 Function.prototype.constructor
- put_property(w_FunctionPrototype, 'constructor', w_Function)
+ put_property(w_FunctionPrototype, u'constructor', w_Function)
import js.builtins_function as function_builtins
# 15.3.4.2 Function.prototype.toString()
- put_native_function(w_FunctionPrototype, 'toString', function_builtins.to_string)
+ put_native_function(w_FunctionPrototype, u'toString', function_builtins.to_string)
# 15.3.4.3 Function.prototype.apply
- put_native_function(w_FunctionPrototype, 'apply', function_builtins.apply)
+ put_native_function(w_FunctionPrototype, u'apply', function_builtins.apply)
# 15.3.4.4 Function.prototype.call
- put_intimate_function(w_FunctionPrototype, 'call', function_builtins.call)
+ put_intimate_function(w_FunctionPrototype, u'call', function_builtins.call)
import js.builtins_boolean
js.builtins_boolean.setup(global_object)
diff --git a/js/builtins_array.py b/js/builtins_array.py
--- a/js/builtins_array.py
+++ b/js/builtins_array.py
@@ -5,7 +5,7 @@
from js.builtins import put_property, put_native_function
from js.jsobj import W_ArrayConstructor, W__Array, W__Object
w_Array = W_ArrayConstructor()
- put_property(global_object, 'Array', w_Array)
+ put_property(global_object, u'Array', w_Array)
# 15.4.4
w_ArrayPrototype = W__Array()
@@ -14,43 +14,43 @@
# 15.4.3.1
W__Array._prototype_ = w_ArrayPrototype
- put_property(w_Array, 'prototype', w_ArrayPrototype, writable = False, enumerable = False, configurable = False)
+ put_property(w_Array, u'prototype', w_ArrayPrototype, writable = False, enumerable = False, configurable = False)
# 15.4.4.1
- put_property(w_ArrayPrototype, 'constructor', w_Array)
+ put_property(w_ArrayPrototype, u'constructor', w_Array)
# 15.4.4.2
- put_native_function(w_ArrayPrototype, 'toString', to_string)
+ put_native_function(w_ArrayPrototype, u'toString', to_string)
# 15.4.4.5
- put_native_function(w_ArrayPrototype, 'join', join, params = ['separator'])
+ put_native_function(w_ArrayPrototype, u'join', join, params = [u'separator'])
# 15.4.4.6
- put_native_function(w_ArrayPrototype, 'pop', pop)
+ put_native_function(w_ArrayPrototype, u'pop', pop)
# 15.4.4.7
- put_native_function(w_ArrayPrototype, 'push', push)
+ put_native_function(w_ArrayPrototype, u'push', push)
# 15.4.4.8
- put_native_function(w_ArrayPrototype, 'reverse', reverse)
+ put_native_function(w_ArrayPrototype, u'reverse', reverse)
# 15.4.4.11
- put_native_function(w_ArrayPrototype, 'sort', sort)
+ put_native_function(w_ArrayPrototype, u'sort', sort)
# 15.4.4.7
def push(this, args):
o = this.ToObject()
- len_val = o.get('length')
+ len_val = o.get(u'length')
n = len_val.ToUInt32()
for item in args:
e = item
- o.put(str(n), e, True)
+ o.put(unicode(n), e, True)
n = n + 1
- o.put('length', _w(n), True)
+ o.put(u'length', _w(n), True)
return n
# 15.4.4.2
def to_string(this, args):
array = this.ToObject()
- func = array.get('join')
+ func = array.get(u'join')
if func.is_callable():
return func.Call(this = this).to_string()
else:
@@ -61,20 +61,20 @@
separator = get_arg(args, 0)
o = this.ToObject()
- len_val = o.get('length')
+ len_val = o.get(u'length')
length = len_val.ToUInt32()
if separator is w_Undefined:
- sep = ','
+ sep = u','
else:
sep = separator.to_string()
if length == 0:
- return ''
+ return u''
- element0 = o.get('0')
+ element0 = o.get(u'0')
if isnull_or_undefined(element0):
- r = ''
+ r = u''
else:
r = element0.to_string()
@@ -82,7 +82,7 @@
while(k < length):
s = r + sep
- element = o.get(str(k))
+ element = o.get(unicode(k))
if isnull_or_undefined(element):
_next = ''
else:
@@ -95,24 +95,24 @@
# 15.4.4.6
def pop(this, args):
o = this.ToObject()
- lenVal = o.get('length')
+ lenVal = o.get(u'length')
l = lenVal.ToUInt32()
if l == 0:
- o.put('length', _w(0))
+ o.put(u'length', _w(0))
return w_Undefined
else:
indx = l - 1
- indxs = str(indx)
+ indxs = unicode(indx)
element = o.get(indxs)
o.delete(indxs, True)
- o.put('length', _w(indx))
+ o.put(u'length', _w(indx))
return element
# 15.4.4.8
def reverse(this, args):
o = this.ToObject()
- length = o.get('length').ToUInt32()
+ length = o.get(u'length').ToUInt32()
import math
middle = math.floor(length/2)
@@ -120,8 +120,8 @@
lower = 0
while lower != middle:
upper = length - lower - 1
- lower_p = str(lower)
- upper_p = str(upper)
+ lower_p = unicode(lower)
+ upper_p = unicode(upper)
lower_value = o.get(lower_p)
upper_value = o.get(upper_p)
lower_exists = o.has_property(lower_p)
@@ -142,7 +142,7 @@
# 15.4.4.11
def sort(this, args):
obj = this
- length = this.get('length').ToUInt32()
+ length = this.get(u'length').ToUInt32()
comparefn = get_arg(args, 0)
@@ -152,8 +152,8 @@
while True:
swapped = False
for i in xrange(1, length):
- x = str(i - 1)
- y = str(i)
+ x = unicode(i - 1)
+ y = unicode(i)
comp = sort_compare(obj, x, y, comparefn)
if comp == 1:
tmp_x = obj.get(x)
@@ -191,7 +191,7 @@
if comparefn is not w_Undefined:
if not comparefn.is_callable():
- raise JsTypeError()
+ raise JsTypeError(u'')
res = comparefn.Call(args = [x, y], this = w_Undefined)
return res.ToInteger()
diff --git a/js/builtins_boolean.py b/js/builtins_boolean.py
--- a/js/builtins_boolean.py
+++ b/js/builtins_boolean.py
@@ -8,13 +8,13 @@
# 15.6.2
from js.jsobj import W_BooleanConstructor
w_Boolean = W_BooleanConstructor()
- put_property(global_object, 'Boolean', w_Boolean)
+ put_property(global_object, u'Boolean', w_Boolean)
# 15.6.3
- put_property(w_Boolean, 'length', _w(1), writable = False, enumerable = False, configurable = False)
+ put_property(w_Boolean, u'length', _w(1), writable = False, enumerable = False, configurable = False)
# 15.6.4
- w_BooleanPrototype = W_BooleanObject(False)
+ w_BooleanPrototype = W_BooleanObject(_w(False))
from js.jsobj import W__Object
w_BooleanPrototype._prototype_ = W__Object._prototype_
@@ -22,16 +22,16 @@
#put_property(w_BooleanPrototype, '__proto__', w_BooleanPrototype._prototype_, writable = False, enumerable = False, configurable = False)
# 15.6.3.1
- put_property(w_Boolean, 'prototype', w_BooleanPrototype, writable = False, enumerable = False, configurable = False)
+ put_property(w_Boolean, u'prototype', w_BooleanPrototype, writable = False, enumerable = False, configurable = False)
# 15.6.4.1
- put_property(w_BooleanPrototype, 'constructor', w_Boolean)
+ put_property(w_BooleanPrototype, u'constructor', w_Boolean)
# 15.6.4.2
- put_native_function(w_BooleanPrototype, 'toString', to_string)
+ put_native_function(w_BooleanPrototype, u'toString', to_string)
# 15.6.4.3
- put_native_function(w_BooleanPrototype, 'valueOf', value_of)
+ put_native_function(w_BooleanPrototype, u'valueOf', value_of)
# 15.6.3.1
W_BooleanObject._prototype_ = w_BooleanPrototype
@@ -43,7 +43,7 @@
elif isinstance(this, W_BooleanObject):
b = this.PrimitiveValue()
else:
- raise JsTypeError()
+ raise JsTypeError(u'')
if b.to_boolean() == True:
return 'true'
@@ -57,6 +57,6 @@
elif isinstance(this, W_BooleanObject):
b = this.PrimitiveValue()
else:
- raise JsTypeError()
+ raise JsTypeError(u'')
return b
diff --git a/js/builtins_date.py b/js/builtins_date.py
--- a/js/builtins_date.py
+++ b/js/builtins_date.py
@@ -19,83 +19,83 @@
def putf(name, func):
put_native_function(w_DatePrototype, name, func)
- putf('toString', to_string)
+ putf(u'toString', to_string)
- putf('valueOf', value_of)
+ putf(u'valueOf', value_of)
- putf('getTime', get_time)
+ putf(u'getTime', get_time)
- putf('getFullYear', get_full_year)
- putf('getUTCFullYear', get_utc_full_year)
+ putf(u'getFullYear', get_full_year)
+ putf(u'getUTCFullYear', get_utc_full_year)
- putf('getMonth', get_month)
- putf('getUTCMonth', get_utc_month)
+ putf(u'getMonth', get_month)
+ putf(u'getUTCMonth', get_utc_month)
- putf('getDate', get_date)
- putf('getUTCDate', get_utc_date)
+ putf(u'getDate', get_date)
+ putf(u'getUTCDate', get_utc_date)
- putf('getDay', get_day)
- putf('getUTCDay', get_utc_day)
+ putf(u'getDay', get_day)
+ putf(u'getUTCDay', get_utc_day)
- putf('getHours', get_hours)
- putf('getUTCHours', get_utc_hours)
+ putf(u'getHours', get_hours)
+ putf(u'getUTCHours', get_utc_hours)
- putf('getMinutes', get_minutes)
- putf('getUTCMinutes', get_utc_minutes)
+ putf(u'getMinutes', get_minutes)
+ putf(u'getUTCMinutes', get_utc_minutes)
- putf('getSeconds', get_seconds)
- putf('getUTCSeconds', get_utc_seconds)
+ putf(u'getSeconds', get_seconds)
+ putf(u'getUTCSeconds', get_utc_seconds)
- putf('getMilliseconds', get_milliseconds)
- putf('getUTCMilliseconds', get_utc_milliseconds)
+ putf(u'getMilliseconds', get_milliseconds)
+ putf(u'getUTCMilliseconds', get_utc_milliseconds)
- putf('getTimezoneOffset', get_timezone_offset)
+ putf(u'getTimezoneOffset', get_timezone_offset)
- putf('setTime', set_time)
+ putf(u'setTime', set_time)
- putf('setMilliseconds', set_milliseconds)
- putf('setUTCMilliseconds', set_utc_milliseconds)
+ putf(u'setMilliseconds', set_milliseconds)
+ putf(u'setUTCMilliseconds', set_utc_milliseconds)
- putf('setSeconds', set_seconds)
- putf('setUTCSeconds', set_utc_seconds)
+ putf(u'setSeconds', set_seconds)
+ putf(u'setUTCSeconds', set_utc_seconds)
- putf('setMinutes', set_minutes)
- putf('setUTCMinutes', set_utc_minutes)
+ putf(u'setMinutes', set_minutes)
+ putf(u'setUTCMinutes', set_utc_minutes)
- putf('setHours', set_hours)
- putf('setUTCHours', set_utc_hours)
+ putf(u'setHours', set_hours)
+ putf(u'setUTCHours', set_utc_hours)
- putf('setDate', set_date)
- putf('setUTCDate', set_utc_date)
+ putf(u'setDate', set_date)
+ putf(u'setUTCDate', set_utc_date)
- putf('setMonth', set_month)
- putf('setUTCMonth', set_utc_month)
+ putf(u'setMonth', set_month)
+ putf(u'setUTCMonth', set_utc_month)
- putf('setFullYear', set_full_year)
- putf('setUTCFullYear', set_utc_full_year)
+ putf(u'setFullYear', set_full_year)
+ putf(u'setUTCFullYear', set_utc_full_year)
- putf('getYear', get_year)
- putf('setYear', set_year)
+ putf(u'getYear', get_year)
+ putf(u'setYear', set_year)
- putf('toUTCString', to_utc_string)
- putf('toGMTString', to_gmt_string)
+ putf(u'toUTCString', to_utc_string)
+ putf(u'toGMTString', to_gmt_string)
# 15.9.3
w_Date = W_DateConstructor()
- put_property(global_object, 'Date', w_Date)
+ put_property(global_object, u'Date', w_Date)
- put_property(w_Date, 'prototype', w_DatePrototype, writable = False, enumerable = False, configurable = False)
+ put_property(w_Date, u'prototype', w_DatePrototype, writable = False, enumerable = False, configurable = False)
- put_native_function(w_Date, 'parse', parse)
+ put_native_function(w_Date, u'parse', parse)
- put_native_function(w_Date, 'UTC', parse)
+ put_native_function(w_Date, u'UTC', parse)
def to_string(this, args):
d = w_date_to_datetime(this)
local = to_local(d)
s = local.strftime('%a %b %d %Y %H:%M:%S GMT%z (%Z)')
- return s
+ return unicode(s)
# 15.9.5.8
def value_of(this, args):
diff --git a/js/builtins_function.py b/js/builtins_function.py
--- a/js/builtins_function.py
+++ b/js/builtins_function.py
@@ -7,7 +7,7 @@
def to_string(this, args):
from js.jsobj import W_BasicFunction
if not isinstance(this, W_BasicFunction):
- raise JsTypeError()
+ raise JsTypeError(u'')
return this._to_string_()
@@ -20,7 +20,7 @@
args = ctx.argv()
if not func.is_callable():
- raise JsTypeError()
+ raise JsTypeError(u'')
this_arg = get_arg(args, 0)
arg_list = args[1:]
@@ -43,14 +43,14 @@
from js.jsobj import W_BasicObject
if not isinstance(arg_array, W_BasicObject):
- raise JsTypeError()
+ raise JsTypeError(u'')
- length = arg_array.get('length')
+ length = arg_array.get(u'length')
n = length.ToUInt32()
arg_list = []
index = 0
while index < n:
- index_name = str(index)
+ index_name = unicode(index)
next_arg = arg_array.get(index_name)
arg_list.append(next_arg)
index += 1
diff --git a/js/builtins_global.py b/js/builtins_global.py
--- a/js/builtins_global.py
+++ b/js/builtins_global.py
@@ -11,42 +11,42 @@
from pypy.rlib.objectmodel import we_are_translated
# 15.1.1.1
- put_property(global_object, 'NaN', w_NAN, writable = False, enumerable = False, configurable = False)
+ put_property(global_object, u'NaN', w_NAN, writable = False, enumerable = False, configurable = False)
# 15.1.1.2
- put_property(global_object, 'Infinity', w_POSITIVE_INFINITY, writable = False, enumerable = False, configurable = False)
+ put_property(global_object, u'Infinity', w_POSITIVE_INFINITY, writable = False, enumerable = False, configurable = False)
# 15.1.1.3
- put_property(global_object, 'undefined', w_Undefined, writable = False, enumerable = False, configurable = False)
+ put_property(global_object, u'undefined', w_Undefined, writable = False, enumerable = False, configurable = False)
# 15.1.2.1
- put_intimate_function(global_object, 'eval', js_eval, params = ['x'])
+ put_intimate_function(global_object, u'eval', js_eval, params = [u'x'])
# 15.1.2.2
- put_native_function(global_object, 'parseInt', parse_int, params = ['string', 'radix'])
+ put_native_function(global_object, u'parseInt', parse_int, params = [u'string', u'radix'])
# 15.1.2.3
- put_native_function(global_object, 'parseFloat', parse_float, params = ['string'])
+ put_native_function(global_object, u'parseFloat', parse_float, params = [u'string'])
# 15.1.2.4
- put_native_function(global_object, 'isNaN', is_nan, params = ['number'])
+ put_native_function(global_object, u'isNaN', is_nan, params = [u'number'])
# 15.1.2.5
- put_native_function(global_object, 'isFinite', is_finite, params = ['number'])
+ put_native_function(global_object, u'isFinite', is_finite, params = [u'number'])
- put_native_function(global_object, 'alert', alert)
+ put_native_function(global_object, u'alert', alert)
- put_native_function(global_object, 'print', printjs)
+ put_native_function(global_object, u'print', printjs)
- put_native_function(global_object, 'escape', escape, params = ['string'])
- put_native_function(global_object, 'unescape', unescape, params = ['string'])
+ put_native_function(global_object, u'escape', escape, params = [u'string'])
+ put_native_function(global_object, u'unescape', unescape, params = [u'string'])
- put_native_function(global_object, 'version', version)
+ put_native_function(global_object, u'version', version)
## debugging
if not we_are_translated():
- put_native_function(global_object, 'pypy_repr', pypy_repr)
- put_native_function(global_object, 'inspect', inspect)
+ put_native_function(global_object, u'pypy_repr', pypy_repr)
+ put_native_function(global_object, u'inspect', inspect)
# 15.1.2.4
def is_nan(this, args):
@@ -67,7 +67,7 @@
# 15.1.2.2
def parse_int(this, args):
- NUMERALS = '0123456789abcdefghijklmnopqrstuvwxyz'
+ NUMERALS = u'0123456789abcdefghijklmnopqrstuvwxyz'
string = get_arg(args, 0)
radix = get_arg(args, 1)
@@ -75,9 +75,9 @@
s = input_string.lstrip()
sign = 1
- if s.startswith('-'):
+ if s.startswith(u'-'):
sign = -1
- if s.startswith('-') or s.startswith('+'):
+ if s.startswith(u'-') or s.startswith(u'+'):
s = s[1:]
r = radix.ToInt32()
@@ -202,7 +202,7 @@
pass # goto step 18
else:
# 16
- hex_numeral = '00%s' % (r1[k+1:k+3])
+ hex_numeral = u'00%s' % (r1[k+1:k+3])
number = int(hex_numeral, 16)
c = unichr(number)
#17
@@ -224,7 +224,7 @@
def pypy_repr(this, args):
o = args[0]
- return str(o)
+ return unicode(o)
def inspect(this, args):
pass
@@ -266,7 +266,7 @@
except LexerError, e:
error_lineno = e.source_pos.lineno
error_pos = e.source_pos.columnno
- error_msg = 'LexerError'
+ error_msg = u'LexerError'
raise JsSyntaxError(msg = error_msg, src = src, line = error_lineno, column = error_pos)
symbol_map = ast.symbol_map
diff --git a/js/builtins_math.py b/js/builtins_math.py
--- a/js/builtins_math.py
+++ b/js/builtins_math.py
@@ -9,52 +9,52 @@
from js.jsobj import W_Math
# 15.8
w_Math = W_Math()
- put_property(global_object, 'Math', w_Math)
+ put_property(global_object, u'Math', w_Math)
- put_native_function(w_Math, 'abs', js_abs, params = ['x'])
- put_native_function(w_Math, 'floor', floor, params = ['x'])
- put_native_function(w_Math, 'round', js_round, params = ['x'])
- put_native_function(w_Math, 'random', random)
- put_native_function(w_Math, 'min', js_min, params = ['value1', 'value2'])
- put_native_function(w_Math, 'max', js_max, params = ['value1', 'value2'])
- put_native_function(w_Math, 'pow', js_pow, params = ['x', 'y'])
- put_native_function(w_Math, 'sqrt', js_sqrt, params = ['x'])
- put_native_function(w_Math, 'log', js_log, params = ['x'])
- put_native_function(w_Math, 'sin', js_sin, params = ['x'])
- put_native_function(w_Math, 'tan', js_tan, params = ['x'])
- put_native_function(w_Math, 'acos', js_acos, params = ['x'])
- put_native_function(w_Math, 'asin', js_asin, params = ['x'])
- put_native_function(w_Math, 'atan', js_atan, params = ['x'])
- put_native_function(w_Math, 'atan2', js_atan2, params = ['y', 'x'])
- put_native_function(w_Math, 'ceil', js_ceil, params = ['x'])
- put_native_function(w_Math, 'cos', js_cos, params = ['x'])
- put_native_function(w_Math, 'exp', js_exp, params = ['x'])
+ put_native_function(w_Math, u'abs', js_abs, params = [u'x'])
+ put_native_function(w_Math, u'floor', floor, params = [u'x'])
+ put_native_function(w_Math, u'round', js_round, params = [u'x'])
+ put_native_function(w_Math, u'random', random)
+ put_native_function(w_Math, u'min', js_min, params = [u'value1', u'value2'])
+ put_native_function(w_Math, u'max', js_max, params = [u'value1', u'value2'])
+ put_native_function(w_Math, u'pow', js_pow, params = [u'x', u'y'])
+ put_native_function(w_Math, u'sqrt', js_sqrt, params = [u'x'])
+ put_native_function(w_Math, u'log', js_log, params = [u'x'])
+ put_native_function(w_Math, u'sin', js_sin, params = [u'x'])
+ put_native_function(w_Math, u'tan', js_tan, params = [u'x'])
+ put_native_function(w_Math, u'acos', js_acos, params = [u'x'])
+ put_native_function(w_Math, u'asin', js_asin, params = [u'x'])
+ put_native_function(w_Math, u'atan', js_atan, params = [u'x'])
+ put_native_function(w_Math, u'atan2', js_atan2, params = [u'y', u'x'])
+ put_native_function(w_Math, u'ceil', js_ceil, params = [u'x'])
+ put_native_function(w_Math, u'cos', js_cos, params = [u'x'])
+ put_native_function(w_Math, u'exp', js_exp, params = [u'x'])
# 15.8.1
# 15.8.1.1
- put_property(w_Math, 'E', _w(E), writable = False, enumerable = False, configurable = False)
+ put_property(w_Math, u'E', _w(E), writable = False, enumerable = False, configurable = False)
# 15.8.1.2
- put_property(w_Math, 'LN10', _w(LN10), writable = False, enumerable = False, configurable = False)
+ put_property(w_Math, u'LN10', _w(LN10), writable = False, enumerable = False, configurable = False)
# 15.8.1.3
- put_property(w_Math, 'LN2', _w(LN2), writable = False, enumerable = False, configurable = False)
+ put_property(w_Math, u'LN2', _w(LN2), writable = False, enumerable = False, configurable = False)
# 15.8.1.4
- put_property(w_Math, 'LOG2E', _w(LOG2E), writable = False, enumerable = False, configurable = False)
+ put_property(w_Math, u'LOG2E', _w(LOG2E), writable = False, enumerable = False, configurable = False)
# 15.8.1.5
- put_property(w_Math, 'LOG10E', _w(LOG10E), writable = False, enumerable = False, configurable = False)
+ put_property(w_Math, u'LOG10E', _w(LOG10E), writable = False, enumerable = False, configurable = False)
# 15.8.1.6
- put_property(w_Math, 'PI', _w(PI), writable = False, enumerable = False, configurable = False)
+ put_property(w_Math, u'PI', _w(PI), writable = False, enumerable = False, configurable = False)
# 15.8.1.7
- put_property(w_Math, 'SQRT1_2', _w(SQRT1_2), writable = False, enumerable = False, configurable = False)
+ put_property(w_Math, u'SQRT1_2', _w(SQRT1_2), writable = False, enumerable = False, configurable = False)
# 15.8.1.8
- put_property(w_Math, 'SQRT2', _w(SQRT2), writable = False, enumerable = False, configurable = False)
+ put_property(w_Math, u'SQRT2', _w(SQRT2), writable = False, enumerable = False, configurable = False)
# 15.8.2.9
def floor(this, args):
diff --git a/js/builtins_number.py b/js/builtins_number.py
--- a/js/builtins_number.py
+++ b/js/builtins_number.py
@@ -8,46 +8,46 @@
# 15.7.2
from js.jsobj import W_NumberConstructor
w_Number = W_NumberConstructor()
- put_property(global_object, 'Number', w_Number)
+ 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, 'length', _w(1), writable = False, enumerable = False, configurable = False)
+ 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(0)
+ 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)
# 15.7.4.1
- put_property(w_NumberPrototype, 'constructor', w_Number)
+ put_property(w_NumberPrototype, u'constructor', w_Number)
# 15.7.4.2
- put_native_function(w_NumberPrototype, 'toString', to_string)
+ put_native_function(w_NumberPrototype, u'toString', to_string)
# 15.7.4.4
- put_native_function(w_NumberPrototype, 'valueOf', value_of)
+ put_native_function(w_NumberPrototype, u'valueOf', value_of)
# 15.7.3.1
- put_property(w_Number, 'prototype', w_NumberPrototype, writable = False, enumerable = False, configurable = False)
+ 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, 'MAX_VALUE', w_MAX_VALUE, writable = False, configurable = False, enumerable = False)
+ put_property(w_Number, u'MAX_VALUE', w_MAX_VALUE, writable = False, configurable = False, enumerable = False)
# 15.7.3.3
- put_property(w_Number, 'MIN_VALUE', w_MIN_VALUE, writable = False, configurable = False, enumerable = False)
+ put_property(w_Number, u'MIN_VALUE', w_MIN_VALUE, writable = False, configurable = False, enumerable = False)
# 15.7.3.4
- put_property(w_Number, 'NaN', w_NAN, writable = False, configurable = False, enumerable = False)
+ put_property(w_Number, u'NaN', w_NAN, writable = False, configurable = False, enumerable = False)
# 15.7.3.5
- put_property(w_Number, 'POSITIVE_INFINITY', w_POSITIVE_INFINITY, writable = False, configurable = False, enumerable = False)
+ put_property(w_Number, u'POSITIVE_INFINITY', w_POSITIVE_INFINITY, writable = False, configurable = False, enumerable = False)
# 15.7.3.6
- put_property(w_Number, 'NEGATIVE_INFINITY', w_NEGATIVE_INFINITY, writable = False, configurable = False, enumerable = False)
+ put_property(w_Number, u'NEGATIVE_INFINITY', w_NEGATIVE_INFINITY, writable = False, configurable = False, enumerable = False)
# 15.7.3.2
w_MAX_VALUE = _w(1.7976931348623157e308)
@@ -76,7 +76,7 @@
elif isinstance(this, W_NumericObject):
num = this.PrimitiveValue()
else:
- raise JsTypeError()
+ raise JsTypeError(u'')
# TODO radix, see 15.7.4.2
return num.to_string()
@@ -88,6 +88,6 @@
elif isinstance(this, W_NumericObject):
num = this.PrimitiveValue()
else:
- raise JsTypeError()
+ raise JsTypeError(u'')
return num.ToNumber()
diff --git a/js/builtins_string.py b/js/builtins_string.py
--- a/js/builtins_string.py
+++ b/js/builtins_string.py
@@ -11,58 +11,58 @@
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, 'length', _w(1), writable = False, enumerable = False, configurable = False)
+ put_property(w_String, u'length', _w(1), writable = False, enumerable = False, configurable = False)
- put_property(global_object, 'String', w_String)
+ put_property(global_object, u'String', w_String)
# 15.5.4
from js.jsobj import W_StringObject, W__Object
- w_StringPrototype = W_StringObject('')
+ w_StringPrototype = W_StringObject(_w(u''))
w_StringPrototype._prototype_ = W__Object._prototype_
# 15.5.3.1
W_StringObject._prototype_ = w_StringPrototype
- put_property(w_String, 'prototype', w_StringPrototype, writable = False, enumerable = False, configurable = False)
+ put_property(w_String, u'prototype', w_StringPrototype, writable = False, enumerable = False, configurable = False)
# 15.5.3.2
- put_native_function(w_String, 'fromCharCode', from_char_code, params = ['char1'])
+ put_native_function(w_String, u'fromCharCode', from_char_code, params = [u'char1'])
# 15.5.4.1
- put_property(w_StringPrototype, 'constructor', w_String)
+ put_property(w_StringPrototype, u'constructor', w_String)
# 15.5.4.2
- put_native_function(w_StringPrototype, 'toString', to_string)
+ put_native_function(w_StringPrototype, u'toString', to_string)
# 15.5.4.3
- put_native_function(w_StringPrototype, 'valueOf', value_of)
+ put_native_function(w_StringPrototype, u'valueOf', value_of)
# 15.5.4.4
- put_native_function(w_StringPrototype, 'charAt', char_at, params = ['pos'])
+ put_native_function(w_StringPrototype, u'charAt', char_at, params = [u'pos'])
# 15.5.4.5
- put_native_function(w_StringPrototype, 'charCodeAt', char_code_at, params = ['pos'])
+ put_native_function(w_StringPrototype, u'charCodeAt', char_code_at, params = [u'pos'])
# 15.5.4.6
- put_native_function(w_StringPrototype, 'concat', concat, params = ['string1'])
+ put_native_function(w_StringPrototype, u'concat', concat, params = [u'string1'])
# 15.5.4.7
- put_native_function(w_StringPrototype, 'indexOf', index_of, params = ['searchstring'])
+ put_native_function(w_StringPrototype, u'indexOf', index_of, params = [u'searchstring'])
# 15.5.4.8
- put_native_function(w_StringPrototype, 'lastIndexOf', last_index_of, params = ['searchstring'])
+ put_native_function(w_StringPrototype, u'lastIndexOf', last_index_of, params = [u'searchstring'])
# 15.5.4.14
- put_native_function(w_StringPrototype, 'split', split, params = ['separator', 'limit'])
+ put_native_function(w_StringPrototype, u'split', split, params = [u'separator', u'limit'])
# 15.5.4.15
- put_native_function(w_StringPrototype, 'substring', substring, params = ['start', 'end'])
+ put_native_function(w_StringPrototype, u'substring', substring, params = [u'start', u'end'])
# 15.5.4.16
- put_native_function(w_StringPrototype, 'toLowerCase', to_lower_case)
+ put_native_function(w_StringPrototype, u'toLowerCase', to_lower_case)
# 15.5.4.18
- put_native_function(w_StringPrototype, 'toUpperCase', to_upper_case)
+ put_native_function(w_StringPrototype, u'toUpperCase', to_upper_case)
# 15.5.3.2
def from_char_code(this, args):
@@ -79,7 +79,7 @@
elif isinstance(this, W_StringObject):
s = this.PrimitiveValue()
else:
- raise JsTypeError()
+ raise JsTypeError(u'')
assert isinstance(s, W_String)
return s.to_string()
@@ -91,7 +91,7 @@
elif isinstance(this, W_StringObject):
s = this.PrimitiveValue()
else:
- raise JsTypeError()
+ raise JsTypeError(u'')
assert isinstance(s, W_String)
return s
diff --git a/js/completion.py b/js/completion.py
--- a/js/completion.py
+++ b/js/completion.py
@@ -21,3 +21,12 @@
def is_return_completion(c):
return isinstance(c, ReturnCompletion)
+
+def is_normal_completion(c):
+ return isinstance(c, NormalCompletion)
+
+def is_empty_completion(c):
+ return is_normal_completion(c) and c.value is None
+
+def is_completion(c):
+ return isinstance(c, Completion)
diff --git a/js/console.py b/js/console.py
--- a/js/console.py
+++ b/js/console.py
@@ -40,14 +40,15 @@
assert filename is not None
program = load_file(filename)
except EnvironmentError, e:
- msg = W_String("Can't open %s: %s" % (filename, e))
+ msg = W_String(u"Can't open %s: %s" % (filename, e))
raise ThrowException(msg)
self.interpreter.run(program)
return w_Undefined
class W_ReadLine(W_NewBuiltin):
def Call(self, ctx, args=[], this=None):
- return W_String(readline())
+ line = unicode(readline())
+ return W_String(line)
class JSConsole(object):
prompt_ok = 'js> '
diff --git a/js/environment_record.py b/js/environment_record.py
--- a/js/environment_record.py
+++ b/js/environment_record.py
@@ -8,19 +8,19 @@
return False
def create_mutuable_binding(self, identifier, deletable):
- pass
+ raise NotImplementedError
- def set_mutable_binding(self, identifier, value, strict=False):
- pass
+ def set_mutable_binding(self, identifier, value, strict):
+ raise NotImplementedError
def get_binding_value(self, identifier, strict=False):
- pass
+ raise NotImplementedError
def delete_binding(self, identifier):
- pass
+ raise NotImplementedError
def implicit_this_value(self):
- pass
+ raise NotImplementedError
class DeclarativeEnvironmentRecord(EnvironmentRecord):
def __init__(self):
@@ -43,7 +43,7 @@
# 10.2.1.1.1
def has_binding(self, identifier):
- return self.bindings.has_key(identifier)
+ return identifier in self.bindings
# 10.2.1.1.2
def create_mutuable_binding(self, identifier, deletable):
@@ -54,10 +54,12 @@
self._set_deletable_binding(identifier)
# 10.2.1.1.3
- def set_mutable_binding(self, identifier, value, strict=False):
+ def set_mutable_binding(self, identifier, value, strict):
+ assert isinstance(identifier, unicode)
assert self.has_binding(identifier)
if not self._is_mutable_binding(identifier):
- raise JsTypeError('immutable binding')
+ from js.execution import JsTypeError
+ raise JsTypeError(u'immutable binding')
self.bindings[identifier] = value
# 10.2.1.1.4
@@ -65,10 +67,11 @@
assert self.has_binding(identifier)
if not identifier in self.bindings:
if strict:
+ from js.execution import JsReferenceError
raise JsReferenceError
else:
return w_Undefined
- return self.bindings.get(identifier)
+ return self.bindings[identifier]
# 10.2.1.1.5
def delete_binding(self, identifier):
@@ -121,7 +124,8 @@
bindings.define_own_property(n, desc, True)
# 10.2.1.2.3
- def set_mutable_binding(self, n, v, s = False):
+ def set_mutable_binding(self, n, v, s):
+ assert isinstance(n, unicode)
bindings = self.binding_object
bindings.put(n, v, s)
@@ -133,6 +137,7 @@
if s is False:
return w_Undefined
else:
+ from execution import JsReferenceError
raise JsReferenceError(self.__class__)
return bindings.get(n)
diff --git a/js/execution.py b/js/execution.py
--- a/js/execution.py
+++ b/js/execution.py
@@ -25,48 +25,45 @@
class JsException(Exception):
def _msg(self):
- return 'Exception'
+ return u'Exception'
def msg(self):
from js.jsobj import _w
return _w(self._msg())
class JsThrowException(JsException):
- def __init__(self, value = None):
- JsException.__init__(self)
- from js.jsobj import _w
- self.value = _w(value)
+ def __init__(self, value):
+ from js.jsobj import W_Root
+ assert isinstance(value, W_Root)
+ self.value = value
def _msg(self):
return self.value
class JsTypeError(JsException):
- def __init__(self, value = None):
- JsException.__init__(self)
+ def __init__(self, value):
+ assert isinstance(value, unicode)
self.value = value
def _msg(self):
- return 'TypeError: %s' % (self.value)
+ return u'TypeError: %s' % (self.value)
class JsReferenceError(JsException):
def __init__(self, identifier):
- JsException.__init__(self)
self.identifier = identifier
def _msg(self):
- return 'ReferenceError: %s is not defined' % (self.identifier)
+ return u'ReferenceError: %s is not defined' % (self.identifier)
class JsRangeError(JsException):
def __init__(self, value = None):
- JsException.__init__(self)
self.value = value
def _msg(self):
- return 'RangeError: %s' %(str(self.value))
+ return u'RangeError: %s' %(self.value)
class JsSyntaxError(JsException):
- def __init__(self, msg = '', src = '', line = 0, column = 0):
- JsException.__init__(self)
+ def __init__(self, msg = u'', src = u'', line = 0, column = 0):
self.error_msg = msg
self.src = src
self.line = line
@@ -75,6 +72,6 @@
def _msg(self):
error_src = self.src.encode('unicode_escape')
if self.error_msg:
- return 'SyntaxError: "%s" in "%s" at line:%d, column:%d' %(self.error_msg, error_src, self.line, self.column)
+ return u'SyntaxError: "%s" in "%s" at line:%d, column:%d' %(self.error_msg, error_src, self.line, self.column)
else:
- return 'SyntaxError: in "%s" at line:%d, column:%d' %(error_src, self.line, self.column)
+ return u'SyntaxError: in "%s" at line:%d, column:%d' %(error_src, self.line, self.column)
diff --git a/js/execution_context.py b/js/execution_context.py
--- a/js/execution_context.py
+++ b/js/execution_context.py
@@ -1,15 +1,5 @@
from js.jsobj import w_Undefined
-
-def get_global_object():
- return GlobalExecutionContext.global_object
-
-def get_global_context():
- return GlobalExecutionContext.global_context
-
-def get_global_environment():
- return get_global_context().variable_environment()
-
class ExecutionContext(object):
def __init__(self):
self._stack_ = []
@@ -30,10 +20,13 @@
if n < 1:
return []
- i = -1 * n
- r = self._stack_[i:]
- s = self._stack_[:i]
- self._stack_ = s
+ r = []
+ i = n
+ while i > 0:
+ i -= 1
+ e = self.stack_pop()
+ r = [e] + r
+
return r
def this_binding(self):
@@ -75,7 +68,7 @@
arg_already_declared = env.has_binding(arg_name)
if arg_already_declared is False:
env.create_mutuable_binding(arg_name, configurable_bindings)
- env.set_mutable_binding(arg_name, v)
+ env.set_mutable_binding(arg_name, v, False)
# 5.
func_declarations = code.functions()
@@ -86,9 +79,9 @@
env.create_mutuable_binding(fn, configurable_bindings)
else:
pass #see 10.5 5.e
- env.set_mutable_binding(fn, fo)
+ env.set_mutable_binding(fn, fo, False)
- arguments_already_declared = env.has_binding('arguments')
+ arguments_already_declared = env.has_binding(u'arguments')
# 7.
if code.is_function_code() and arguments_already_declared is False:
from js.jsobj import W_Arguments
@@ -98,11 +91,11 @@
names = self._formal_parameters_
args_obj = W_Arguments(func, names, arguments, env, strict)
if strict is True:
- env.create_immutable_bining('arguments')
- env.initialize_immutable_binding('arguments', args_obj)
+ env.create_immutable_bining(u'arguments')
+ env.initialize_immutable_binding(u'arguments', args_obj)
else:
- env.create_mutuable_binding('arguments', False) # TODO not sure if mutable binding is deletable
- env.set_mutable_binding('arguments', args_obj, False)
+ env.create_mutuable_binding(u'arguments', False) # TODO not sure if mutable binding is deletable
+ env.set_mutable_binding(u'arguments', args_obj, False)
# 8.
var_declarations = code.variables()
@@ -110,7 +103,7 @@
var_already_declared = env.has_binding(dn)
if var_already_declared == False:
env.create_mutuable_binding(dn, configurable_bindings)
- env.set_mutable_binding(dn, w_Undefined)
+ env.set_mutable_binding(dn, w_Undefined, False)
def get_ref(self, symbol):
## TODO pre-bind symbols, work with idndex, does not work, see test_foo18
@@ -119,8 +112,6 @@
return ref
class GlobalExecutionContext(ExecutionContext):
- global_object = None
- global_context = None
def __init__(self, code, global_object, strict = False):
ExecutionContext.__init__(self)
self._code_ = code
@@ -130,8 +121,6 @@
localEnv = ObjectEnvironment(global_object)
self._lexical_environment_ = localEnv
self._variable_environment_ = localEnv
- GlobalExecutionContext.global_object = global_object
- GlobalExecutionContext.global_context = self
self._this_binding_ = global_object
self.declaration_binding_initialization()
@@ -179,7 +168,8 @@
if strict:
self._this_binding_ = this
elif this is None or isnull_or_undefined(this):
- self._this_binding_ = get_global_object()
+ from js.object_space import object_space
+ self._this_binding_ = object_space.get_global_object()
elif this.klass() is not 'Object':
self._this_binding_ = this.ToObject()
else:
diff --git a/js/functions.py b/js/functions.py
--- a/js/functions.py
+++ b/js/functions.py
@@ -16,7 +16,7 @@
return 2
def to_string(self):
- return 'function() {}'
+ return u'function() {}'
def variables(self):
return []
@@ -56,18 +56,23 @@
def run(self, ctx):
from js.completion import ReturnCompletion
+ from js.jsobj import W_Root
+
args = ctx.argv()
this = ctx.this_binding()
res = self._function_(this, args)
- compl = ReturnCompletion(value = _w(res))
+ w_res = _w(res)
+ #w_res = res
+ assert isinstance(w_res, W_Root)
+ compl = ReturnCompletion(value = w_res)
return compl
def to_string(self):
name = self.name()
if name is not None:
- return 'function %s() { [native code] }' % (name, )
+ return u'function %s() { [native code] }' % (name, )
else:
- return 'function () { [native code] }'
+ return u'function () { [native code] }'
class JsIntimateFunction(JsNativeFunction):
def run(self, ctx):
@@ -76,56 +81,57 @@
class JsExecutableCode(JsBaseFunction):
def __init__(self, js_code):
JsBaseFunction.__init__(self)
+ from js.jscode import JsCode
+ assert isinstance(js_code, JsCode)
self._js_code_ = js_code
- self.stack_size = js_code.estimated_stack_size()
+ #self.stack_size = js_code.estimated_stack_size()
#def estimated_stack_size(self):
#return self.stack_size
+ def get_js_code(self):
+ from js.jscode import JsCode
+ assert isinstance(self._js_code_, JsCode)
+ return self._js_code_
+
def run(self, ctx):
- result = self._js_code_.run(ctx)
+ code = self.get_js_code()
+ result = code.run(ctx)
return result
def variables(self):
- return self._js_code_.variables()
+ code = self.get_js_code()
+ return code.variables()
def functions(self):
- return self._js_code_.functions()
-
- #def index_for_symbol(self, symbol):
- #return self._js_code_.index_for_symbol(symbol)
-
- #def symbols(self):
- #return self._js_code_.symbols()
-
- #def symbol_for_index(self, index):
- #return self._js_code_.symbol_for_index(index)
+ code = self.get_js_code()
+ return code.functions()
def params(self):
- return self._js_code_.params()
+ code = self.get_js_code()
+ return code.params()
def name(self):
- return '_unnamed_'
+ return u'_unnamed_'
def to_string(self):
name = self.name()
if name is not None:
- return 'function %s() { }' % (name, )
+ return u'function %s() { }' % (name, )
else:
- return 'function () { }'
+ return u'function () { }'
class JsGlobalCode(JsExecutableCode):
- pass
+ def __init__(self, js_code):
+ return JsExecutableCode.__init__(self, js_code)
class JsEvalCode(JsExecutableCode):
- pass
-
+ def __init__(self, js_code):
+ return JsExecutableCode.__init__(self, js_code)
def is_eval_code(self):
return True
class JsFunction(JsExecutableCode):
- #_immutable_fields_ = ["opcodes[*]", 'name', 'params', 'code', 'scope']
-
def __init__(self, name, js_code):
JsExecutableCode.__init__(self, js_code)
self._name_ = name
@@ -135,78 +141,3 @@
def is_function_code(self):
return True
-
- #@jit.unroll_safe
- #def run(self, ctx, args=[], this=None):
- #from js.jsexecution_context import make_activation_context, make_function_context
-
- #from js.jsobj import W_Arguments, w_Undefined
- #w_Arguments = W_Arguments(self, args)
- #act = make_activation_context(ctx, this, w_Arguments)
- #newctx = make_function_context(act, self)
-
- #paramn = len(self.params)
- #for i in range(paramn):
- #paramname = self.params[i]
- #try:
- #value = args[i]
- #except IndexError:
- #value = w_Undefined
- #newctx.declare_variable(paramname)
- #newctx.assign(paramname, value)
-
- #return self._run_with_context(ctx=newctx, save_stack = False)
-
- #def _run_with_context(self, ctx, check_stack=True, save_stack=True):
- #state = ([], 0)
- #if save_stack:
- #state = _save_stack(ctx, self.estimated_stack_size())
-
- #try:
- #self._run_bytecode(ctx)
- #if check_stack:
- #ctx.check_stack()
- #return ctx.top()
- #except ReturnException, e:
- #return e.value
- #finally:
- #if save_stack:
- #_restore_stack(ctx, state)
-
- #def _run_bytecode(self, ctx, pc=0):
- #while True:
- #jitdriver.jit_merge_point(pc=pc, self=self, ctx=ctx)
- #if pc >= len(self.opcodes):
- #break
-
- #opcode = self._get_opcode(pc)
- ##if we_are_translated():
- ## #this is an optimization strategy for translated code
- ## #on top of cpython it destroys the performance
- ## #besides, this code might be completely wrong
- ## for name, op in opcode_unrolling:
- ## opcode = hint(opcode, deepfreeze=True)
- ## if isinstance(opcode, op):
- ## result = opcode.eval(ctx, stack)
- ## assert result is None
- ## break
- ##else:
- #result = opcode.eval(ctx)
- #assert result is None
-
- #if isinstance(opcode, BaseJump):
- #new_pc = opcode.do_jump(ctx, pc)
- #condition = new_pc < pc
- #pc = new_pc
- #if condition:
- #jitdriver.can_enter_jit(pc=pc, self=self, ctx=ctx)
- #continue
- #else:
- #pc += 1
-
- #if isinstance(opcode, WITH_START):
- #pc = self._run_bytecode(opcode.newctx, pc)
- #elif isinstance(opcode, WITH_END):
- #break
-
- #return pc
diff --git a/js/interpreter.py b/js/interpreter.py
--- a/js/interpreter.py
+++ b/js/interpreter.py
@@ -8,7 +8,8 @@
f = open_file_as_stream(str(filename))
src = f.readall()
- ast = parse_to_ast(src)
+ usrc = unicode(src)
+ ast = parse_to_ast(usrc)
f.close()
return ast
@@ -26,21 +27,21 @@
from js.builtins import put_native_function
def js_trace(this, args):
import pdb; pdb.set_trace()
- put_native_function(global_object, 'trace', js_trace)
+ put_native_function(global_object, u'trace', js_trace)
interp = self
def js_load(this, args):
filename = args[0].to_string()
interp.js_load(str(filename))
- put_native_function(global_object, 'load', js_load)
+ put_native_function(global_object, u'load', js_load)
def js_debug(this, args):
import js.globals
js.globals.DEBUG = not js.globals.DEBUG
return js.globals.DEBUG
- put_native_function(global_object, 'debug', js_debug)
+ put_native_function(global_object, u'debug', js_debug)
def js_load(self, filename):
ast = load_file(filename)
@@ -56,15 +57,22 @@
def run_src(self, src):
from js.astbuilder import parse_to_ast
- ast = parse_to_ast(src)
+ ast = parse_to_ast(unicode(src))
return self.run_ast(ast)
def run(self, code, interactive=False):
from js.functions import JsGlobalCode
+
+ from js.jscode import JsCode
+ assert isinstance(code, JsCode)
c = JsGlobalCode(code)
+ from js.object_space import object_space
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)
result = c.run(ctx)
return result.value
diff --git a/js/js_interactive.py b/js/js_interactive.py
--- a/js/js_interactive.py
+++ b/js/js_interactive.py
@@ -80,7 +80,7 @@
3) The input is complete. Executes the source code.
"""
try:
- ast = parse_to_ast(str(source))
+ ast = parse_to_ast(unicode(source))
except ParseError, exc:
if exc.source_pos.i == len(source):
# Case 2
diff --git a/js/jscode.py b/js/jscode.py
--- a/js/jscode.py
+++ b/js/jscode.py
@@ -109,12 +109,12 @@
def emit_break(self):
if not self.endlooplabel:
- raise ThrowException(W_String("Break outside loop"))
+ raise ThrowException(W_String(u"Break outside loop"))
self.emit('JUMP', self.endlooplabel[-1])
def emit_continue(self):
if not self.startlooplabel:
- raise ThrowException(W_String("Continue outside loop"))
+ raise ThrowException(W_String(u"Continue outside loop"))
self.emit('JUMP', self.updatelooplabel[-1])
def continue_at_label(self, label):
@@ -207,14 +207,14 @@
def run(self, ctx):
from js.globals import DEBUG
- from js.completion import ReturnCompletion, NormalCompletion
+ from js.completion import NormalCompletion, is_completion, is_return_completion, is_empty_completion
from js.opcodes import RETURN, BaseJump
from js.jsobj import w_Undefined
self.unlabel()
if len(self.opcodes) == 0:
- return w_Undefined
+ return NormalCompletion()
if DEBUG:
print('start running %s' % (str(self)))
@@ -230,8 +230,10 @@
if DEBUG:
print(u'%3d %25s %s %s' % (pc, str(opcode), unicode([unicode(s) for s in ctx._stack_]), str(result)))
- if isinstance(result, ReturnCompletion):
+ if is_return_completion(result):
break;
+ elif not is_completion(result):
+ result = NormalCompletion()
if isinstance(opcode, BaseJump):
new_pc = opcode.do_jump(ctx, pc)
@@ -240,7 +242,8 @@
else:
pc += 1
- if result is None:
+ assert is_completion(result)
+ if is_empty_completion(result):
result = NormalCompletion(value = ctx.stack_top())
return result
diff --git a/js/jsobj.py b/js/jsobj.py
--- a/js/jsobj.py
+++ b/js/jsobj.py
@@ -11,7 +11,7 @@
return self.to_string()
def to_string(self):
- return ''
+ return u''
def type(self):
return self._type_
@@ -23,7 +23,7 @@
return self
def ToObject(self):
- raise JsTypeError()
+ raise JsTypeError(u'W_Root.ToObject')
def ToNumber(self):
return 0.0
@@ -85,10 +85,10 @@
return NAN
def to_string(self):
- return self._type_
+ return unicode(self._type_)
def check_object_coercible(self):
- raise JsTypeError()
+ raise JsTypeError(u'W_Undefined.check_object_coercible')
class W_Null(W_Primitive):
_type_ = 'null'
@@ -97,36 +97,25 @@
return False
def to_string(self):
- return 'null'
+ return u'null'
def check_object_coercible(self):
- raise JsTypeError()
+ raise JsTypeError(u'W_Null.check_object_coercible')
w_Undefined = W_Undefined()
w_Null = W_Null()
+NOT_SET = -1
+
# 8.6.1
class Property(object):
- value = w_Undefined
- getter = w_Undefined
- setter = w_Undefined
- writable = False
- enumerable = False
- configurable = False
-
- def __init__(self, value = None, writable = None, getter = None, setter = None, enumerable = None, configurable = None):
- if value is not None:
- self.value = value
- if writable is not None:
- self.writable = writable
- if getter is not None:
- self.getter = getter
- if setter is not None:
- self.setter = setter
- if enumerable is not None:
- self.enumerable = enumerable
- if configurable is not None:
- self.configurable = configurable
+ def __init__(self, value = None, writable = NOT_SET, getter = None, setter = None, enumerable = NOT_SET, configurable = NOT_SET):
+ self.value = value
+ self.writable = writable
+ self.getter = getter
+ self.setter = setter
+ self.enumerable = enumerable
+ self.configurable = configurable
def is_data_property(self):
return False
@@ -137,56 +126,50 @@
def update_with(self, other):
if other.value is not None:
self.value = other.value
- if other.writable is not None:
+ if other.writable is not NOT_SET:
self.writable = other.writable
if other.getter is not None:
self.getter = other.getter
if other.setter is not None:
self.setter = other.setter
- if other.writable is not None:
+ if other.writable is not NOT_SET:
self.writable = other.writable
- if other.configurable is not None:
+ if other.configurable is not NOT_SET:
self.configurable = other.configurable
class DataProperty(Property):
- def __init__(self, value = None, writable = None, enumerable = None, configurable = None):
+ def __init__(self, value = None, writable = NOT_SET, enumerable = NOT_SET, configurable = NOT_SET):
Property.__init__(self, value = value, writable = writable, enumerable = enumerable, configurable = configurable)
def is_data_property(self):
return True
class AccessorProperty(Property):
- def __init__(self, getter = None, setter = None, enumerable = None, configurable = None):
+ def __init__(self, getter = None, setter = None, enumerable = NOT_SET, configurable = NOT_SET):
Property.__init__(self, getter = getter, setter = setter, enumerable = enumerable, configurable = configurable)
def is_accessor_property(self):
return True
def is_data_descriptor(desc):
- if desc is w_Undefined:
+ if desc is None:
return False
return desc.is_data_descriptor()
def is_accessor_descriptor(desc):
- if desc is w_Undefined:
+ if desc is None:
return False
return desc.is_accessor_descriptor()
def is_generic_descriptor(desc):
- if desc is w_Undefined:
+ if desc is None:
return False
return desc.is_generic_descriptor()
# 8.10
class PropertyDescriptor(object):
- value = None
- writable = None
- getter = None
- setter = None
- configurable = None
- enumerable = None
- def __init__(self, value = None, writable = None, getter = None, setter = None, configurable = None, enumerable = None):
+ def __init__(self, value = None, writable = NOT_SET, getter = None, setter = None, configurable = NOT_SET, enumerable = NOT_SET):
self.value = value
self.writable = writable
self.getter = getter
@@ -198,7 +181,7 @@
return self.getter is not None and self.setter is not None
def is_data_descriptor(self):
- return self.value is not None and self.writable is not None
+ return self.value is not None and self.writable is not NOT_SET
def is_generic_descriptor(self):
return self.is_accessor_descriptor() is False and self.is_data_descriptor() is False
@@ -207,9 +190,9 @@
return self.getter is None\
and self.setter is None\
and self.value is None\
- and self.writable is None\
- and self.enumerable is None\
- and self.configurable is None
+ and self.writable is NOT_SET\
+ and self.enumerable is NOT_SET\
+ and self.configurable is NOT_SET
def __eq__(self, other):
assert isinstance(other, PropertyDescriptor)
@@ -220,31 +203,31 @@
if self.getter is not None and self.getter != other.getter:
return False
- if self.writable is not None and self.writable != other.writable:
+ if self.writable is not NOT_SET and self.writable != other.writable:
return False
if self.value is not None and self.value != other.value:
return False
- if self.configurable is not None and self.configurable != other.configurable:
+ if self.configurable is not NOT_SET and self.configurable != other.configurable:
return False
- if self.enumerable is not None and self.enumerable != other.enumerable:
+ if self.enumerable is not NOT_SET and self.enumerable != other.enumerable:
return False
def update_with(self, other):
assert isinstance(other, PropertyDescriptor)
- if other.enumerable is not None:
+ if other.enumerable is not NOT_SET:
self.enumerable = other.enumerable
- if other.configurable is not None:
+ if other.configurable is not NOT_SET:
self.configurable = other.configurable
if other.value is not None:
self.value = other.value
- if other.writable is not None:
+ if other.writable is not NOT_SET:
self.writable = other.writable
if other.getter is not None:
@@ -264,7 +247,7 @@
def Call(self, args = [], this = None, calling_context = None):
if not isinstance(this, W_BasicObject):
- raise JsTypeError()
+ raise JsTypeError(u'')
return this._prototype_
@@ -274,7 +257,7 @@
def Call(self, args = [], this = None, calling_context = None):
if not isinstance(this, W_BasicObject):
- raise JsTypeError()
+ raise JsTypeError(u'')
proto = args[0]
this._prototype_ = proto
@@ -283,6 +266,11 @@
w_proto_setter = W_ProtoSetter()
proto_desc = PropertyDescriptor(getter = w_proto_getter, setter = w_proto_setter, enumerable = False, configurable = False)
+def reject(throw):
+ if throw:
+ raise JsTypeError(u'')
+ return False
+
class W_BasicObject(W_Root):
_type_ = 'object'
_class_ = 'Object'
@@ -295,7 +283,7 @@
#desc = PropertyDescriptor(value = self._prototype_, writable = False, enumerable = False, configurable = False)
desc = proto_desc
- W_BasicObject.define_own_property(self, '__proto__', desc)
+ W_BasicObject.define_own_property(self, u'__proto__', desc)
def __str__(self):
return "%s: %s" % (object.__repr__(self), self.klass())
@@ -314,16 +302,17 @@
# 8.12.3
def get(self, p):
+ assert isinstance(p, unicode)
desc = self.get_property(p)
- if desc is w_Undefined:
+ if desc is None:
return w_Undefined
if is_data_descriptor(desc):
return desc.value
getter = desc.getter
- if getter is w_Undefined:
+ if getter is None:
return w_Undefined
res = getter.Call(this = self)
@@ -331,8 +320,9 @@
# 8.12.1
More information about the pypy-commit
mailing list