def last_index_of(this, args): search_string = get_arg(args, 0) position = get_arg(args, 1) s = this.to_string() search_str = search_string.to_string() num_pos = position.ToNumber() from rpython.rlib.rfloat import INFINITY, isnan, isinf if isnan(num_pos): pos = INFINITY elif isinf(num_pos): pos = num_pos else: pos = int(num_pos) length = len(s) start = min(max(pos, 0), length) search_len = len(search_str) if isinf(start): idx = s.rfind(search_str) return idx end = int(start + search_len) assert end >= 0 idx = s.rfind(search_str, 0, end) return idx
def split(this, args): from js.object_space import isundefined this.check_object_coercible() separator = get_arg(args, 0, None) limit = get_arg(args, 1) string = this.to_string() if isundefined(limit): import math lim = int(math.pow(2, 32) - 1) else: lim = limit.ToUInt32() if lim == 0 or separator is None: return [string] r = separator.to_string() if r == u'': i = 0 splitted = [] while i < len(string): splitted += [string[i]] i += 1 return splitted else: splitted = _rsplit(string, r, lim) return splitted
def last_index_of(this, args): search_string = get_arg(args, 0) position = get_arg(args, 1) s = this.to_string() search_str = search_string.to_string() num_pos = position.ToNumber() from rpython.rlib.rfloat import INFINITY, isnan, isinf if isnan(num_pos): pos = INFINITY elif isinf(num_pos): pos = num_pos else: pos = int(num_pos) length = len(s) start = min(max(pos, 0), length) search_len = len(search_str) if isinf(start): idx = s.rfind(search_str) return idx end = int(start + search_len) assert end >= 0 idx = s.rfind(search_str, 0, end) return idx
def js_apply(ctx): from js.object_space import isnull_or_undefined func = ctx.this_binding() args = ctx.argv() this_arg = get_arg(args, 0) arg_array = get_arg(args, 1) if isnull_or_undefined(arg_array): res = func.Call(args=[], this=this_arg, calling_context=ctx) compl = NormalCompletion(value=_w(res)) return compl from js.jsobj import W_BasicObject if not isinstance(arg_array, W_BasicObject): raise JsTypeError(u'') length = arg_array.get(u'length') n = length.ToUInt32() arg_list = [] index = 0 while index < n: index_name = unicode(str(index)) next_arg = arg_array.get(index_name) arg_list.append(next_arg) index += 1 res = func.Call(args=arg_list, this=this_arg, calling_context=ctx) compl = NormalCompletion(value=_w(res)) return compl
def split(this, args): from js.object_space import isundefined this.check_object_coercible() separator = get_arg(args, 0, None) limit = get_arg(args, 1) string = this.to_string() if isundefined(limit): import math lim = int(math.pow(2, 32) - 1) else: lim = limit.ToUInt32() if lim == 0 or separator is None: return [string] r = separator.to_string() if r == u'': i = 0 splitted = [] while i < len(string): splitted += [string[i]] i += 1 return splitted else: splitted = _rsplit(string, r, lim) return splitted
def js_atan2(this, args): arg0 = get_arg(args, 0) arg1 = get_arg(args, 1) y = arg0.ToNumber() x = arg1.ToNumber() if isnan(x) or isnan(y): return NAN return math.atan2(y, x)
def js_atan2(this, args): arg0 = get_arg(args, 0) arg1 = get_arg(args, 1) y = arg0.ToNumber() x = arg1.ToNumber() if isnan(x) or isnan(y): return NAN return math.atan2(y, x)
def slice(this, args): o = this.ToObject() from_index = get_arg(args, 0).ToUInt32() to_index = get_arg(args, 1).ToUInt32() n = [] for k in xrange(from_index, to_index): n.append(o.get(unicode(str(k)))) return _w(n)
def js_pow(this, args): w_x = get_arg(args, 0) w_y = get_arg(args, 1) x = w_x.ToNumber() y = w_y.ToNumber() if isnan(y): return NAN if y == 0: return 1 if isnan(x): return NAN if abs(x) > 1 and y == INFINITY: return INFINITY if abs(x) > 1 and y == -INFINITY: return 0 if abs(x) == 1 and isinf(y): return NAN if abs(x) < 1 and y == INFINITY: return 0 if abs(x) < 1 and y == -INFINITY: return INFINITY if x == INFINITY and y > 0: return INFINITY if x == INFINITY and y < 0: return 0 if x == -INFINITY and y > 0 and isodd(y): return -INFINITY if x == -INFINITY and y > 0 and not isodd(y): return INFINITY if x == -INFINITY and y < 0 and isodd(y): return -0.0 if x == -INFINITY and y < 0 and not isodd(y): return 0 if eq_signed_zero(x, 0.0) and y > 0: return 0 if eq_signed_zero(x, 0.0) and y < 0: return INFINITY if eq_signed_zero(x, -0.0) and y > 0 and isodd(y): return -0.0 if eq_signed_zero(x, -0.0) and y > 0 and not isodd(y): return +0 if eq_signed_zero(x, -0.0) and y < 0 and isodd(y): return -INFINITY if eq_signed_zero(x, -0.0) and y < 0 and not isodd(y): return INFINITY if x < 0 and not isinstance(y, int): return NAN try: return math.pow(x, y) except OverflowError: return INFINITY
def index_of(this, args): obj = this length = this.get(u'length').ToUInt32() elem = get_arg(args, 0) from_index = get_arg(args, 1).ToUInt32() from js.jsobj import W_IntNumber for i in xrange(from_index, length): y = obj.get(unicode(str(i))) if elem == y: return W_IntNumber(i) return W_IntNumber(-1)
def index_of(this, args): obj = this length = this.get(u'length').ToUInt32() elem = get_arg(args, 0) from_index = get_arg(args, 1).ToUInt32() from js.jsobj import W_IntNumber for i in xrange(from_index, length): y = obj.get(unicode(i)) if elem == y: return W_IntNumber(i) return W_IntNumber(-1)
def slice(this, args): o = this.ToObject() from_index = get_arg(args, 0).ToUInt32() to_index = get_arg(args, 1).ToUInt32() from js.object_space import object_space n = object_space.new_array(length=_w(to_index - from_index)) from js.jsobj import put_property index = 0 for item in xrange(from_index, to_index): put_property(n, unicode(str(index)), o.get(unicode(str(item)))) index += 1 return n
def js_pow(this, args): w_x = get_arg(args, 0) w_y = get_arg(args, 1) x = w_x.ToNumber() y = w_y.ToNumber() if isnan(y): return NAN if y == 0: return 1 if isnan(x): return NAN if abs(x) > 1 and y == INFINITY: return INFINITY if abs(x) > 1 and y == -INFINITY: return 0 if abs(x) == 1 and isinf(y): return NAN if abs(x) < 1 and y == INFINITY: return 0 if abs(x) < 1 and y == -INFINITY: return INFINITY if x == INFINITY and y > 0: return INFINITY if x == INFINITY and y < 0: return 0 if x == -INFINITY and y > 0 and isodd(y): return -INFINITY if x == -INFINITY and y > 0 and not isodd(y): return INFINITY if x == -INFINITY and y < 0 and isodd(y): return -0.0 if x == -INFINITY and y < 0 and not isodd(y): return 0 if eq_signed_zero(x, 0.0) and y > 0: return 0 if eq_signed_zero(x, 0.0) and y < 0: return INFINITY if eq_signed_zero(x, -0.0) and y > 0 and isodd(y): return -0.0 if eq_signed_zero(x, -0.0) and y > 0 and not isodd(y): return +0 if eq_signed_zero(x, -0.0) and y < 0 and isodd(y): return -INFINITY if eq_signed_zero(x, -0.0) and y < 0 and not isodd(y): return INFINITY if x < 0 and not isinstance(y, int): return NAN try: return math.pow(x, y) except OverflowError: return INFINITY
def js_eval(ctx): from rpython.rlib.parsing.parsing import ParseError from rpython.rlib.parsing.deterministic import LexerError from js.astbuilder import parse_to_ast from js.jscode import ast_to_bytecode from js.jsobj import W_String from js.functions import JsEvalCode from js.execution_context import EvalExecutionContext from js.astbuilder import FakeParseError from js.exception import JsSyntaxError args = ctx.argv() x = get_arg(args, 0) if not isinstance(x, W_String): from js.completion import NormalCompletion return NormalCompletion(value=x) src = x.to_string() try: ast = parse_to_ast(src) except ParseError, e: #error = e.errorinformation.failure_reasons #error_lineno = e.source_pos.lineno #error_pos = e.source_pos.columnno #raise JsSyntaxError(msg = unicode(error), src = unicode(src), line = error_lineno, column = error_pos) raise JsSyntaxError()
def parse_float(this, args): from js.runistr import encode_unicode_utf8 from js.constants import num_lit_rexp string = get_arg(args, 0) input_string = string.to_string() trimmed_string = _strip(input_string) str_trimmed_string = encode_unicode_utf8(trimmed_string) match_data = num_lit_rexp.match(str_trimmed_string) if match_data is not None: number_string = match_data.group() else: number_string = '' if number_string == 'Infinity' or number_string == '+Infinity': return INFINITY elif number_string == '-Infinity': return -INFINITY try: number = float(number_string) return number except ValueError: pass return NAN
def sort(this, args): obj = this length = this.get(u'length').ToUInt32() comparefn = get_arg(args, 0) # TODO check if implementation defined # sorts need to be in-place, lets do some very non-fancy bubble sort for starters while True: swapped = False for i in xrange(1, length): x = unicode(str(i - 1)) y = unicode(str(i)) comp = sort_compare(obj, x, y, comparefn) if comp == 1: tmp_x = obj.get(x) tmp_y = obj.get(y) obj.put(x, tmp_y) obj.put(y, tmp_x) swapped = True if not swapped: break return obj
def parse_float(this, args): from js.runistr import encode_unicode_utf8 from js.constants import num_lit_rexp string = get_arg(args, 0) input_string = string.to_string() trimmed_string = _strip(input_string) str_trimmed_string = encode_unicode_utf8(trimmed_string) match_data = num_lit_rexp.match(str_trimmed_string) if match_data is not None: number_string = match_data.group() else: number_string = '' if number_string == 'Infinity' or number_string == '+Infinity': return INFINITY elif number_string == '-Infinity': return -INFINITY try: number = float(number_string) return number except ValueError: pass return NAN
def js_eval(ctx): from rpython.rlib.parsing.parsing import ParseError from rpython.rlib.parsing.deterministic import LexerError from js.astbuilder import parse_to_ast from js.jscode import ast_to_bytecode from js.jsobj import W_String from js.functions import JsEvalCode from js.execution_context import EvalExecutionContext from js.astbuilder import FakeParseError from js.exception import JsSyntaxError args = ctx.argv() x = get_arg(args, 0) if not isinstance(x, W_String): from js.completion import NormalCompletion return NormalCompletion(value=x) src = x.to_string() try: ast = parse_to_ast(src) except ParseError, e: #error = e.errorinformation.failure_reasons #error_lineno = e.source_pos.lineno #error_pos = e.source_pos.columnno #raise JsSyntaxError(msg = unicode(error), src = unicode(src), line = error_lineno, column = error_pos) raise JsSyntaxError()
def set_time(this, args): _assert_date(this) from js.jsobj import W_DateObject assert isinstance(this, W_DateObject) arg0 = get_arg(args, 0) this._primitive_value_ = arg0 return arg0
def set_time(this, args): _assert_date(this) from js.jsobj import W_DateObject assert isinstance(this, W_DateObject) arg0 = get_arg(args, 0) this._primitive_value_ = arg0 return arg0
def sort(this, args): obj = this length = this.get(u'length').ToUInt32() comparefn = get_arg(args, 0) # TODO check if implementation defined # sorts need to be in-place, lets do some very non-fancy bubble sort for starters while True: swapped = False for i in xrange(1, length): x = unicode(str(i - 1)) y = unicode(str(i)) comp = sort_compare(obj, x, y, comparefn) if comp == 1: tmp_x = obj.get(x) tmp_y = obj.get(y) obj.put(x, tmp_y) obj.put(y, tmp_x) swapped = True if not swapped: break return obj
def floor(this, args): arg0 = get_arg(args, 0) x = arg0.ToNumber() if isnan(x): return NAN return math.floor(x)
def js_abs(this, args): arg0 = get_arg(args, 0) x = arg0.ToNumber() if isnan(x): return NAN return abs(x)
def js_cos(this, args): arg0 = get_arg(args, 0) x = arg0.ToNumber() if isnan(x) or isinf(x): return NAN return math.cos(x)
def js_abs(this, args): arg0 = get_arg(args, 0) x = arg0.ToNumber() if isnan(x): return NAN return abs(x)
def floor(this, args): arg0 = get_arg(args, 0) x = arg0.ToNumber() if isnan(x): return NAN return math.floor(x)
def js_cos(this, args): arg0 = get_arg(args, 0) x = arg0.ToNumber() if isnan(x) or isinf(x): return NAN return math.cos(x)
def last_index_of(this, args): obj = this elem = get_arg(args, 0) length = this.get(u'length').ToUInt32() from_index = length if len(args) > 1: findex = get_arg(args, 1).ToInt32() if findex < 0: from_index = length + findex else: from_index = findex from js.jsobj import W_IntNumber for i in xrange(from_index, -1, -1): y = obj.get(unicode(str(i))) if elem == y: return W_IntNumber(i) return W_IntNumber(-1)
def last_index_of(this, args): obj = this elem = get_arg(args, 0) length = this.get(u'length').ToUInt32() from_index = length if len(args) > 1: findex = get_arg(args, 1).ToInt32() if findex < 0: from_index = length + findex else: from_index = findex from js.jsobj import W_IntNumber for i in xrange(from_index, -1, -1): y = obj.get(unicode(i)) if elem == y: return W_IntNumber(i) return W_IntNumber(-1)
def js_asin(this, args): arg0 = get_arg(args, 0) x = arg0.ToNumber() if isnan(x) or isinf(x): return NAN if x > 1 or x < -1: return NAN return math.asin(x)
def for_each(this, args): obj = this length = this.get(u'length').ToUInt32() callback = get_arg(args, 0) from js.jsobj import W_BasicFunction assert isinstance(callback, W_BasicFunction) for i in xrange(length): x = obj.get(unicode(str(i))) callback.Call(args=[x], this=newundefined())
def for_each(this, args): obj = this length = this.get(u'length').ToUInt32() callback = get_arg(args, 0) from js.jsobj import W_BasicFunction assert isinstance(callback, W_BasicFunction) for i in xrange(length): x = obj.get(unicode(str(i))) callback.Call(args=[x], this=newundefined())
def js_tan(this, args): arg0 = get_arg(args, 0) x = arg0.ToNumber() if isnan(x) or isinf(x): return NAN if x < 0: return NAN return math.tan(x)
def js_tan(this, args): arg0 = get_arg(args, 0) x = arg0.ToNumber() if isnan(x) or isinf(x): return NAN if x < 0: return NAN return math.tan(x)
def js_asin(this, args): arg0 = get_arg(args, 0) x = arg0.ToNumber() if isnan(x) or isinf(x): return NAN if x > 1 or x < -1: return NAN return math.asin(x)
def char_at(this, args): pos = get_arg(args, 0) position = pos.ToInt32() this.check_object_coercible() string = this.to_string() size = len(string) if position < 0 or position >= size: return u'' return string[position]
def set_year(this, args): arg0 = get_arg(args, 0) year = arg0.ToInteger() if isnan(year) or year < 0 or year > 99: this.set_primitive_value(_w(NAN)) return NAN y = year + 1900 c = JsDateChange() c.set_year(y) change_wdate(this, c) return y
def js_call(ctx): func = ctx.this_binding() args = ctx.argv() if not func.is_callable(): raise JsTypeError(u'') this_arg = get_arg(args, 0) arg_list = args[1:] res = func.Call(args=arg_list, this=this_arg, calling_context=ctx) compl = NormalCompletion(value=_w(res)) return compl
def char_code_at(this, args): pos = get_arg(args, 0) this.check_object_coercible() string = this.to_string() position = pos.ToInt32() size = len(string) if position < 0 or position >= size: return NAN char = string[position] return ord(char)
def char_at(this, args): pos = get_arg(args, 0) position = pos.ToInt32() this.check_object_coercible() string = this.to_string() size = len(string) if position < 0 or position >= size: return u'' return string[position]
def set_year(this, args): arg0 = get_arg(args, 0) year = arg0.ToInteger() if isnan(year) or year < 0 or year > 99: this.set_primitive_value(_w(NAN)) return NAN y = year + 1900 c = JsDateChange() c.set_year(y) change_wdate(this, c) return y
def char_code_at(this, args): pos = get_arg(args, 0) this.check_object_coercible() string = this.to_string() position = pos.ToInt32() size = len(string) if position < 0 or position >= size: return NAN char = string[position] return ord(char)
def js_sqrt(this, args): arg0 = get_arg(args, 0) x = arg0.ToNumber() if isnan(x): return NAN if x < 0: return NAN if isinf(x): return INFINITY return math.sqrt(x)
def js_atan(this, args): arg0 = get_arg(args, 0) x = arg0.ToNumber() if isnan(x): return NAN if x == INFINITY: return math.pi / 2 if x == -INFINITY: return -math.pi / 2 return math.atan(x)
def js_exp(this, args): arg0 = get_arg(args, 0) x = arg0.ToNumber() if isnan(x): return NAN if x == INFINITY: return INFINITY if x == -INFINITY: return 0 return math.exp(x)
def js_ceil(this, args): arg0 = get_arg(args, 0) x = arg0.ToNumber() if isnan(x): return NAN if x == INFINITY: return INFINITY if x == -INFINITY: return -INFINITY return math.ceil(x)
def js_exp(this, args): arg0 = get_arg(args, 0) x = arg0.ToNumber() if isnan(x): return NAN if x == INFINITY: return INFINITY if x == -INFINITY: return 0 return math.exp(x)
def js_ceil(this, args): arg0 = get_arg(args, 0) x = arg0.ToNumber() if isnan(x): return NAN if x == INFINITY: return INFINITY if x == -INFINITY: return -INFINITY return math.ceil(x)
def js_atan(this, args): arg0 = get_arg(args, 0) x = arg0.ToNumber() if isnan(x): return NAN if x == INFINITY: return math.pi / 2 if x == -INFINITY: return -math.pi / 2 return math.atan(x)
def js_sqrt(this, args): arg0 = get_arg(args, 0) x = arg0.ToNumber() if isnan(x): return NAN if x < 0: return NAN if isinf(x): return INFINITY return math.sqrt(x)
def js_log(this, args): arg0 = get_arg(args, 0) x = arg0.ToNumber() if isnan(x): return NAN if x < 0: return NAN if x == 0: return -INFINITY if x == INFINITY: return INFINITY return math.log(x)
def js_log(this, args): arg0 = get_arg(args, 0) x = arg0.ToNumber() if isnan(x): return NAN if x < 0: return NAN if x == 0: return -INFINITY if x == INFINITY: return INFINITY return math.log(x)
def js_load(ctx): from js.interpreter import load_file from js.jscode import ast_to_bytecode from js.functions import JsEvalCode from js.execution_context import EvalExecutionContext args = ctx.argv() f = get_arg(args, 0) filename = f.to_string() ast = load_file(filename) symbol_map = ast.symbol_map code = ast_to_bytecode(ast, symbol_map) f = JsEvalCode(code) calling_context = ctx._calling_context_ ctx = EvalExecutionContext(f, calling_context=calling_context) f.run(ctx)
def js_load(ctx): from js.interpreter import load_file from js.jscode import ast_to_bytecode from js.functions import JsEvalCode from js.execution_context import EvalExecutionContext args = ctx.argv() f = get_arg(args, 0) filename = f.to_string() ast = load_file(filename) symbol_map = ast.symbol_map code = ast_to_bytecode(ast, symbol_map) f = JsEvalCode(code) calling_context = ctx._calling_context_ ctx = EvalExecutionContext(f, calling_context=calling_context) f.run(ctx)
def Call(self, args=[], this=None, calling_context=None): from js.object_space import isnull_or_undefined from js.builtins import get_arg value = get_arg(args, 0) if isinstance(value, W_BasicObject): return value if isinstance(value, W_String): return value.ToObject() if isinstance(value, W_Boolean): return value.ToObject() if isinstance(value, W_Number): return value.ToObject() assert isnull_or_undefined(value) from js.object_space import object_space obj = object_space.new_obj() return obj
def unescape(this, args): string = get_arg(args, 0) r1 = string.to_string() r2 = len(r1) r = u'' k = 0 hexchars = u'0123456789abcdef' while k != r2: c = r1[k] if c == u'%': # 8. 9. 10. if (k > r2 - 6) or (r1[k + 1] != u'u') or ( not len(r1) == 6 and _string_match_chars(r1[k + 2:k + 6], hexchars)): # got step 14 if k > r2 - 3: # 14. pass # goto step 18 else: if not _string_match_chars(r1[k + 1:k + 3], hexchars): # 15. pass # goto step 18 else: # 16 hex_numeral = u'00' + r1[k + 1:k + 3] number = int(str(hex_numeral), 16) c = unichr(number) #17 k += 2 else: # 11. hex_numeral = r1[k + 2:k + 6] number = int(str(hex_numeral), 16) c = unichr(number) # 12. k += 5 # step 18 r += c k += 1 return r
def js_round(this, args): arg0 = get_arg(args, 0) x = arg0.ToNumber() if isnan(x): return x if x == 0: return x if x > 0 and x < 0.5: return 0 if x < 0 and x >= -0.5: return -0.0 if isinf(x): return x return math.floor(x + 0.5)