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 set_mutable_binding(self, identifier, value, strict): assert identifier is not None and isinstance(identifier, unicode) assert self.has_binding(identifier) if not self._is_mutable_binding(identifier): from js.exception import JsTypeError raise JsTypeError(u'immutable binding') self._set_binding(identifier, value)
def _idx_put(self, idx, v, throw): d = self._can_idx_put(idx) can_put = d.can_put own_desc = d.own inherited_desc = d.inherited prop = d.prop if not can_put: if throw: raise JsTypeError(u"can't put %s" % (str(idx), )) else: return if is_data_descriptor(own_desc): value_desc = PropertyDescriptor(value=v) self._define_own_idx_property(idx, value_desc, throw, own_desc, prop) return if own_desc is None: desc = inherited_desc else: desc = own_desc if is_accessor_descriptor(desc): setter = desc.setter assert setter is not None setter.Call(this=self, args=[v]) else: new_desc = DataPropertyDescriptor(v, True, True, True) self._define_own_idx_property(idx, new_desc, throw)
def eval(self, ctx): rval = ctx.stack_pop() lval = ctx.stack_pop() from js.jsobj import W_BasicObject if not isinstance(rval, W_BasicObject): raise JsTypeError(u'TypeError') res = rval.has_instance(lval) ctx.stack_append(_w(res))
def operation(self, ctx, left, right): from js.jsobj import W_BasicObject from js.object_space import newbool if not isinstance(right, W_BasicObject): raise JsTypeError(u"TypeError: fffuuu!") # + repr(right) name = left.to_string() has_name = right.has_property(name) return newbool(has_name)
def value_of(this, args): if isinstance(this, W_Boolean): b = this elif isinstance(this, W_BooleanObject): b = this.PrimitiveValue() else: raise JsTypeError(u'') return b
def to_string(this, args): if isinstance(this, W_String): s = this elif isinstance(this, W_StringObject): s = this.PrimitiveValue() else: raise JsTypeError(u'') return s.to_string()
def commonnew(ctx, obj, args): if not obj.is_callable(): msg = u'%s is not a constructor' % (obj.to_string()) raise JsTypeError(msg) from js.jsobj import W_BasicFunction assert isinstance(obj, W_BasicFunction) res = obj.Construct(args=args) return res
def value_of(this, args): if isinstance(this, W_Number): num = this elif isinstance(this, W_NumericObject): num = this.PrimitiveValue() else: raise JsTypeError(u'') return num.ToNumber()
def value_of(this, args): if isinstance(this, W_String): s = this elif isinstance(this, W_StringObject): s = this.PrimitiveValue() else: raise JsTypeError(u'') assert isinstance(s, W_String) return s
def to_string(this, args): if isinstance(this, W_Boolean): b = this elif isinstance(this, W_BooleanObject): b = this.PrimitiveValue() else: raise JsTypeError(u'') if b.to_boolean() is True: return u'true' else: return u'false'
def delete(self, p, throw=False): desc = self.get_own_property(p) if desc is None: return True if desc.configurable: self._del_prop(p) return True if throw is True: raise JsTypeError(u'') return False
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 common_call(ctx, funcobj, args, this, identifyer): if not funcobj.is_callable(): err = u"%s is not a callable (%s)" % (funcobj.to_string(), identifyer.to_string()) raise JsTypeError(err) from js.jsobj import W_List, W_BasicFunction assert isinstance(args, W_List) assert isinstance(funcobj, W_BasicFunction) argv = args.to_list() res = funcobj.Call(args=argv, this=this, calling_context=ctx) return res
def default_value(self, hint='Number'): if hint == 'String': res = self._default_value_string_() if res is None: res = self._default_value_number_() else: res = self._default_value_number_() if res is None: res = self._default_value_string_() if res is not None: return res raise JsTypeError(u'')
def to_string(this, args): if len(args) > 0: radix = args[0].ToInteger() if radix < 2 or radix > 36: raise JsRangeError if isinstance(this, W_Number): num = this elif isinstance(this, W_NumericObject): num = this.PrimitiveValue() else: raise JsTypeError(u'') # TODO radix, see 15.7.4.2 return num.to_string()
def emit(self, bytecode): w_object = self.w_object left_expr = self.left_expr body = self.body w_object.emit(bytecode) bytecode.emit('LOAD_ITERATOR') # load the "last" iterations result bytecode.emit('LOAD_UNDEFINED') precond = bytecode.emit_startloop_label() finish = bytecode.prealocate_endloop_label(True) bytecode.emit('JUMP_IF_ITERATOR_EMPTY', finish) # put next iterator value on stack bytecode.emit('NEXT_ITERATOR') # store iterrator value into approperiate place if isinstance(left_expr, This): raise JsException(u'Invalid left-hand side in for-in') if isinstance(left_expr, Identifier): name = left_expr.name index = left_expr.index bytecode.emit('STORE', index, name) bytecode.emit('POP') elif isinstance(left_expr, VariableDeclaration): name = left_expr.identifier index = left_expr.index bytecode.emit('STORE', index, name) bytecode.emit('POP') elif isinstance(left_expr, MemberDot): bytecode.emit('LOAD_STRINGCONSTANT', left_expr.name) left_expr.left.emit(bytecode) bytecode.emit('STORE_MEMBER') bytecode.emit('POP') elif isinstance(left_expr, Member): left_expr.expr.emit(bytecode) left_expr.left.emit(bytecode) bytecode.emit('STORE_MEMBER') bytecode.emit('POP') else: raise JsTypeError(u'unsupported') body.emit(bytecode) bytecode.emit('JUMP', precond) bytecode.emit_endloop_label(finish)
def has_instance(self, v): from js.object_space import isnull_or_undefined if not isinstance(v, W_BasicObject): return False o = self.get(u'prototype') if not isinstance(o, W_BasicObject): raise JsTypeError(u'has_instance') while True: assert isinstance(v, W_BasicObject) v = v.prototype() if isnull_or_undefined(v): return False if v == o: return True
def sort_compare(obj, j, k, comparefn=newundefined()): from js.object_space import isundefined j_string = j k_string = k has_j = obj.has_property(j) has_k = obj.has_property(k) if has_j is False and has_k is False: return 0 if has_j is False: return 1 if has_k is False: return -1 x = obj.get(j_string) y = obj.get(k_string) if isundefined(x) and isundefined(y): return 0 if isundefined(x): return 1 if isundefined(y): return -1 if not isundefined(comparefn): if not comparefn.is_callable(): from js.exception import JsTypeError raise JsTypeError(u'') from js.jsobj import W_BasicFunction assert isinstance(comparefn, W_BasicFunction) res = comparefn.Call(args=[x, y], this=newundefined()) return res.ToInteger() x_string = x.to_string() y_string = y.to_string() if x_string < y_string: return -1 if x_string > y_string: return 1 return 0
def put(self, p, v, throw=False): assert p is not None and isinstance(p, unicode) if not self.can_put(p): if throw: raise JsTypeError(u"can't put %s" % (p, )) else: return own_desc = self.get_own_property(p) if is_data_descriptor(own_desc) is True: value_desc = PropertyDescriptor(value=v) self.define_own_property(p, value_desc, throw) return desc = self.get_property(p) if is_accessor_descriptor(desc) is True: setter = desc.setter assert setter is not None setter.Call(this=self, args=[v]) else: new_desc = DataPropertyDescriptor(v, True, True, True) self.define_own_property(p, new_desc, throw)
def get(self, p): v = W_BasicObject.get(self, p) if p is u'caller' and isinstance(v, W__Function) and v.is_strict(): raise JsTypeError(u'') return v
def ToObject(self): raise JsTypeError(u'W_Root.ToObject')
def has_instance(self, other): raise JsTypeError(u'has_instance')
def check_object_coercible(self): raise JsTypeError(u'W_Undefined.check_object_coercible')
def _ireject(throw, idx): if throw: raise JsTypeError(unicode(str(idx))) return False
def reject(throw, msg=u''): if throw: raise JsTypeError(msg) return False
def Call(self, args=[], this=None, calling_context=None): if not isinstance(this, W_BasicObject): raise JsTypeError(u'') proto = args[0] this._prototype_ = proto
def ToObject(self): raise JsTypeError(u'W_Null.ToObject')
def check_object_coercible(self): raise JsTypeError(u'W_Null.check_object_coercible')
def ToObject(self): raise JsTypeError(u'W_Undefined.ToObject')