Skip to content
Permalink
Browse files

Cherry-pick some changes over from type-specialized-attrs.

  • Loading branch information
alex committed Jul 12, 2013
1 parent 97b50ea commit c5120b1a0c8997228e7aa4a999d73ad34452b7ec
@@ -6,7 +6,7 @@ def test_while_loop(self, topaz, tmpdir):
traces = self.run(topaz, tmpdir, """
i = 0
while i < 10000
i += 1
i += 1
end
""")
self.assert_matches(traces[0].loop, """
@@ -37,7 +37,7 @@ def test_ivar_while_loop(self, topaz, tmpdir):
traces = self.run(topaz, tmpdir, """
@i = 0
while @i < 10000
@i += 1
@i += 1
end
""")
self.assert_matches(traces[0].loop, """
@@ -76,7 +76,7 @@ def test_constant_string(self, topaz, tmpdir):
traces = self.run(topaz, tmpdir, """
i = 0
while i < 10000
i += "a".length
i += "a".length
end
""")
self.assert_matches(traces[0].loop, """
@@ -110,8 +110,8 @@ def test_method_missing(self, topaz, tmpdir):
traces = self.run(topaz, tmpdir, """
i = 0
while i < 10000
Array.try_convert(1)
i += 1
Array.try_convert(1)
i += 1
end
""")
self.assert_matches(traces[0].loop, """
@@ -13,8 +13,8 @@ def initialize
end
i = 0
while i < 10000
A.new
i += 1
A.new
i += 1
end
""")
self.assert_matches(traces[0].loop, """
@@ -49,6 +49,12 @@ def test_equal(self, space):
w_res = space.execute("return 2.4 == 2.3")
assert w_res is space.w_false

def test_equal_method(self, space):
w_res = space.execute("return 2.3.equal?(2.3)")
assert w_res is space.w_true
w_res = space.execute("return Float::NAN.equal?(Float::NAN)")
assert w_res is space.w_true

def test_hashability(self, space):
w_res = space.execute("return 1.0.hash == 1.0.hash")
assert w_res is space.w_true
@@ -408,3 +408,15 @@ class X
return [x.a, x.b]
""")
assert self.unwrap(space, w_res) == ["abc", 3.8]

def test_attribute_after_flag(self, space):
w_res = space.execute("""
class X
attr_accessor :a
end
x = X.new
x.taint
x.a = "abc"
return x.a
""")
assert space.str_w(w_res) == "abc"
@@ -6,21 +6,21 @@
from rpython.rlib.rarithmetic import ovfcheck_float_to_int
from rpython.rlib.rbigint import rbigint
from rpython.rlib.rfloat import (formatd, DTSF_ADD_DOT_0, DTSF_STR_PRECISION,
NAN, INFINITY, isfinite)
NAN, INFINITY, isfinite, round_away)

from topaz.error import RubyError
from topaz.module import ClassDef
from topaz.objects.exceptionobject import W_ArgumentError
from topaz.objects.objectobject import W_RootObject
from topaz.objects.numericobject import W_NumericObject


class W_FloatObject(W_NumericObject):
class W_FloatObject(W_RootObject):
_immutable_fields_ = ["floatvalue"]

classdef = ClassDef("Float", W_NumericObject.classdef)

def __init__(self, space, floatvalue):
W_NumericObject.__init__(self, space)
self.floatvalue = floatvalue

def __deepcopy__(self, memo):
@@ -141,6 +141,23 @@ def method_eq(self, space, w_other):
else:
raise

@classdef.method("equal?")
def method_equalp(self, space, w_other):
if space.is_kind_of(w_other, space.w_float):
other = space.float_w(w_other)
return space.newbool(
self.floatvalue == other or
(math.isnan(self.floatvalue) and math.isnan(other))
)

try:
return W_NumericObject.retry_binop_coercing(space, self, w_other, "equal?")
except RubyError as e:
if isinstance(e.w_value, W_ArgumentError):
return space.send(w_other, "equal?", [self])
else:
raise

@classdef.method("<=>")
def method_comparator(self, space, w_other):
if space.is_kind_of(w_other, space.w_numeric):
@@ -159,6 +176,13 @@ def method_comparator(self, space, w_other):
def method_hash(self, space):
return space.newint(compute_hash(self.floatvalue))

@classdef.method("coerce")
def method_coerce(self, space, w_other):
if space.getclass(w_other) is space.getclass(self):
return space.newarray([w_other, self])
else:
return space.newarray([space.send(self, "Float", [w_other]), self])

@classdef.method("abs")
def method_abs(self, space):
return space.newfloat(abs(self.floatvalue))
@@ -240,3 +264,11 @@ def method_finite(self, space):
@classdef.method("nan?")
def method_nan(self, space):
return space.newbool(math.isnan(self.floatvalue))

@classdef.method("round")
def method_round(self, space):
return space.newint(int(round_away(self.floatvalue)))

@classdef.method("quo")
def method_quo(self, space):
raise space.error(space.w_NotImplementedError, "Numeric#quo")

0 comments on commit c5120b1

Please sign in to comment.
You can’t perform that action at this time.