-
-
Notifications
You must be signed in to change notification settings - Fork 925
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into truffle-head
Conflicts: truffle/src/main/java/org/jruby/truffle/nodes/core/ArrayNodes.java
- 9.4.12.0
- 9.4.11.0
- 9.4.10.0
- 9.4.9.0
- 9.4.8.0
- 9.4.7.0
- 9.4.6.0
- 9.4.5.0
- 9.4.4.0
- 9.4.3.0
- 9.4.2.0
- 9.4.1.0
- 9.4.0.0
- 9.3.15.0
- 9.3.14.0
- 9.3.13.0
- 9.3.12.0
- 9.3.11.0
- 9.3.10.0
- 9.3.9.0
- 9.3.8.0
- 9.3.7.0
- 9.3.6.0
- 9.3.5.0
- 9.3.4.0
- 9.3.3.0
- 9.3.2.0
- 9.3.1.0
- 9.3.0.0
- 9.2.21.0
- 9.2.20.1
- 9.2.20.0
- 9.2.19.0
- 9.2.18.0
- 9.2.17.0
- 9.2.16.0
- 9.2.15.0
- 9.2.14.0
- 9.2.13.0
- 9.2.12.0
- 9.2.11.1
- 9.2.11.0
- 9.2.10.0
- 9.2.9.0
- 9.2.8.0
- 9.2.7.0
- 9.2.6.0
- 9.2.5.0
- 9.2.4.1
- 9.2.4.0
- 9.2.3.0
- 9.2.2.0
- 9.2.1.0
- 9.2.0.0
- 9.1.17.0
- 9.1.16.0
- 9.1.15.0
- 9.1.14.0
- 9.1.13.0
- 9.1.12.0
- 9.1.11.0
- 9.1.10.0
- 9.1.9.0
- 9.1.8.0
- 9.1.7.0
- 9.1.6.0
- 9.1.5.0
- 9.1.4.0
- 9.1.3.0
- 9.1.2.0
- 9.1.1.0
- 9.1.0.0
- 9.0.5.0
- 9.0.4.0
- 9.0.3.0
- 9.0.1.0
- 9.0.0.0
- 9.0.0.0.rc2
- 9.0.0.0.rc1
Showing
27 changed files
with
593 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
truffle/src/main/java/org/jruby/truffle/nodes/coerce/ToAryNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package org.jruby.truffle.nodes.coerce; | ||
|
||
import com.oracle.truffle.api.CompilerDirectives; | ||
import com.oracle.truffle.api.dsl.NodeChild; | ||
import com.oracle.truffle.api.dsl.Specialization; | ||
import com.oracle.truffle.api.frame.VirtualFrame; | ||
import com.oracle.truffle.api.source.SourceSection; | ||
import org.jruby.truffle.nodes.RubyNode; | ||
import org.jruby.truffle.nodes.dispatch.CallDispatchHeadNode; | ||
import org.jruby.truffle.nodes.dispatch.DispatchHeadNodeFactory; | ||
import org.jruby.truffle.runtime.RubyContext; | ||
import org.jruby.truffle.runtime.control.RaiseException; | ||
import org.jruby.truffle.runtime.core.RubyArray; | ||
|
||
|
||
@NodeChild(value = "child", type = RubyNode.class) | ||
public abstract class ToAryNode extends RubyNode { | ||
|
||
@Child private CallDispatchHeadNode toAryNode; | ||
|
||
public ToAryNode(RubyContext context, SourceSection sourceSection) { | ||
super(context, sourceSection); | ||
} | ||
|
||
public ToAryNode(ToAryNode prev) { | ||
super(prev); | ||
} | ||
|
||
@Specialization | ||
public RubyArray coerceRubyArray(RubyArray rubyArray) { | ||
return rubyArray; | ||
} | ||
|
||
@Specialization(guards = "!isRubyArray(object)") | ||
public RubyArray coerceObject(VirtualFrame frame, Object object) { | ||
notDesignedForCompilation(); | ||
|
||
if (toAryNode == null) { | ||
CompilerDirectives.transferToInterpreter(); | ||
toAryNode = insert(DispatchHeadNodeFactory.createMethodCall(getContext())); | ||
} | ||
|
||
final Object coerced; | ||
|
||
try { | ||
coerced = toAryNode.call(frame, object, "to_ary", null); | ||
} catch (RaiseException e) { | ||
if (e.getRubyException().getLogicalClass() == getContext().getCoreLibrary().getNoMethodErrorClass()) { | ||
CompilerDirectives.transferToInterpreter(); | ||
|
||
throw new RaiseException( | ||
getContext().getCoreLibrary().typeErrorNoImplicitConversion(object, "Array", this)); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
if (coerced instanceof RubyArray) { | ||
return (RubyArray) coerced; | ||
} else { | ||
CompilerDirectives.transferToInterpreter(); | ||
|
||
throw new RaiseException( | ||
getContext().getCoreLibrary().typeErrorBadCoercion(object, "Array", "to_ary", coerced, this)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
truffle/src/main/java/org/jruby/truffle/nodes/rubinius/RandomPrimitiveNodes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package org.jruby.truffle.nodes.rubinius; | ||
|
||
import com.oracle.truffle.api.CompilerDirectives; | ||
import com.oracle.truffle.api.dsl.Specialization; | ||
import com.oracle.truffle.api.source.SourceSection; | ||
import org.jruby.Ruby; | ||
import org.jruby.RubyFixnum; | ||
import org.jruby.RubyNumeric; | ||
import org.jruby.RubyRandom; | ||
import org.jruby.runtime.builtin.IRubyObject; | ||
import org.jruby.truffle.runtime.RubyContext; | ||
import org.jruby.truffle.runtime.core.RubyBasicObject; | ||
import org.jruby.truffle.runtime.core.RubyBignum; | ||
|
||
import java.math.BigInteger; | ||
import java.security.SecureRandom; | ||
import java.util.Random; | ||
|
||
/** | ||
* Rubinius primitives associated with the Ruby {@code Random} class. | ||
*/ | ||
|
||
public abstract class RandomPrimitiveNodes { | ||
|
||
@RubiniusPrimitive(name = "randomizer_seed") | ||
public static abstract class RandomizerSeedPrimitiveNode extends RubiniusPrimitiveNode { | ||
|
||
public RandomizerSeedPrimitiveNode(RubyContext context, SourceSection sourceSection) { | ||
super(context, sourceSection); | ||
} | ||
|
||
public RandomizerSeedPrimitiveNode(RandomizerSeedPrimitiveNode prev) { | ||
super(prev); | ||
} | ||
|
||
@Specialization | ||
public long randomizerSeed(RubyBasicObject random) { | ||
notDesignedForCompilation(); | ||
|
||
return System.currentTimeMillis(); | ||
} | ||
|
||
} | ||
|
||
@RubiniusPrimitive(name = "randomizer_rand_float") | ||
public static abstract class RandomizerRandFloatPrimitiveNode extends RubiniusPrimitiveNode { | ||
|
||
public RandomizerRandFloatPrimitiveNode(RubyContext context, SourceSection sourceSection) { | ||
super(context, sourceSection); | ||
} | ||
|
||
public RandomizerRandFloatPrimitiveNode(RandomizerRandFloatPrimitiveNode prev) { | ||
super(prev); | ||
} | ||
|
||
@Specialization | ||
public double randomizerRandFloat(RubyBasicObject random) { | ||
notDesignedForCompilation(); | ||
|
||
return Math.random(); | ||
} | ||
|
||
} | ||
|
||
@RubiniusPrimitive(name = "randomizer_rand_int") | ||
public static abstract class RandomizerRandIntPrimitiveNode extends RubiniusPrimitiveNode { | ||
|
||
public RandomizerRandIntPrimitiveNode(RubyContext context, SourceSection sourceSection) { | ||
super(context, sourceSection); | ||
} | ||
|
||
public RandomizerRandIntPrimitiveNode(RandomizerRandIntPrimitiveNode prev) { | ||
super(prev); | ||
} | ||
|
||
@Specialization | ||
public long randomizerRandInt(RubyBasicObject random, Integer limit) { | ||
notDesignedForCompilation(); | ||
|
||
return RandomPrimitiveHelper.randomInt(getContext().getRuntime().getCurrentContext().getRuntime(), limit); | ||
} | ||
|
||
@Specialization | ||
public long randomizerRandInt(RubyBasicObject random, Long limit) { | ||
notDesignedForCompilation(); | ||
|
||
return RandomPrimitiveHelper.randomInt(getContext().getRuntime().getCurrentContext().getRuntime(), limit); | ||
} | ||
} | ||
|
||
@RubiniusPrimitive(name = "randomizer_gen_seed") | ||
public static abstract class RandomizerGenSeedPrimitiveNode extends RubiniusPrimitiveNode { | ||
|
||
public RandomizerGenSeedPrimitiveNode(RubyContext context, SourceSection sourceSection) { | ||
super(context, sourceSection); | ||
} | ||
|
||
public RandomizerGenSeedPrimitiveNode(RandomizerGenSeedPrimitiveNode prev) { | ||
super(prev); | ||
} | ||
|
||
@Specialization | ||
public RubyBignum randomizerGenSeed(RubyBasicObject random) { | ||
notDesignedForCompilation(); | ||
|
||
BigInteger integer = RandomPrimitiveHelper.randomSeed(getContext().getRuntime().getCurrentContext().getRuntime()); | ||
return new RubyBignum(getContext().getCoreLibrary().getBignumClass(), integer); | ||
} | ||
} | ||
|
||
static class RandomPrimitiveHelper { | ||
|
||
@CompilerDirectives.TruffleBoundary | ||
public static BigInteger randomSeed(Ruby context) { | ||
return RubyRandom.randomSeed(context).getBigIntegerValue(); | ||
} | ||
|
||
@CompilerDirectives.TruffleBoundary | ||
public static long randomInt(Ruby context, long limit) { | ||
RubyFixnum fixnum = context.newFixnum(limit); | ||
return generateRandomInt(context, fixnum); | ||
} | ||
|
||
@CompilerDirectives.TruffleBoundary | ||
public static long randomInt(Ruby context, int limit) { | ||
RubyFixnum fixnum = context.newFixnum(limit); | ||
return generateRandomInt(context, fixnum); | ||
} | ||
|
||
public static long generateRandomInt(Ruby context, RubyFixnum limit) { | ||
IRubyObject params[] = new IRubyObject[1]; | ||
params[0] = limit; | ||
RubyNumeric num = (RubyNumeric) RubyRandom.randCommon19(context.getCurrentContext(), null, params); | ||
return num.getLongValue(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,203 @@ | ||
# Copyright (c) 2007-2014, Evan Phoenix and contributors | ||
# All rights reserved. | ||
# | ||
# Redistribution and use in source and binary forms, with or without | ||
# modification, are permitted provided that the following conditions are met: | ||
# | ||
# * Redistributions of source code must retain the above copyright notice, this | ||
# list of conditions and the following disclaimer. | ||
# * Redistributions in binary form must reproduce the above copyright notice | ||
# this list of conditions and the following disclaimer in the documentation | ||
# and/or other materials provided with the distribution. | ||
# * Neither the name of Rubinius nor the names of its contributors | ||
# may be used to endorse or promote products derived from this software | ||
# without specific prior written permission. | ||
# | ||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE | ||
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
# | ||
# Only part of Rubinius' random.rb | ||
# Extracted from https://github.com/rubinius/rubinius/blob/v2.4.1/kernel/common/random.rb | ||
# | ||
class Rubinius::Randomizer | ||
|
||
def initialize | ||
self.seed = generate_seed | ||
end | ||
|
||
attr_reader :seed | ||
def seed=(new_seed) | ||
set_seed new_seed | ||
@seed = new_seed | ||
end | ||
|
||
def set_seed(new_seed) | ||
Rubinius.primitive :randomizer_seed | ||
raise PrimitiveFailure, "Randomizer#seed primitive failed" | ||
end | ||
|
||
def swap_seed(new_seed) | ||
old_seed = self.seed | ||
self.seed = new_seed | ||
old_seed | ||
end | ||
|
||
def random(limit) | ||
if undefined.equal?(limit) | ||
random_float | ||
else | ||
if limit.kind_of?(Range) | ||
if time_value?(limit.min) | ||
random_time_range(limit) | ||
else | ||
random_range(limit) | ||
end | ||
elsif limit.kind_of?(Float) | ||
raise ArgumentError, "invalid argument - #{limit}" if limit <= 0 | ||
random_float * limit | ||
else | ||
limit_int = Rubinius::Type.coerce_to limit, Integer, :to_int | ||
raise ArgumentError, "invalid argument - #{limit}" if limit_int <= 0 | ||
|
||
if limit.is_a?(Integer) | ||
random_integer(limit - 1) | ||
elsif limit.respond_to?(:to_f) | ||
random_float * limit | ||
else | ||
random_integer(limit_int - 1) | ||
end | ||
end | ||
end | ||
end | ||
|
||
# Generate a random Float, in the range 0...1.0 | ||
def random_float | ||
Rubinius.primitive :randomizer_rand_float | ||
raise PrimitiveFailure, "Randomizer#rand_float primitive failed" | ||
end | ||
|
||
# Generate a random Integer, in the range 0...limit | ||
def random_integer(limit) | ||
Rubinius.primitive :randomizer_rand_int | ||
raise PrimitiveFailure, "Randomizer#rand_int primitive failed" | ||
end | ||
|
||
def random_range(limit) | ||
min, max = limit.max.coerce(limit.min) | ||
diff = max - min | ||
diff += 1 if max.kind_of?(Integer) | ||
random(diff) + min | ||
end | ||
|
||
def generate_seed | ||
Rubinius.primitive :randomizer_gen_seed | ||
raise PrimitiveFailure, "Randomizer#gen_seed primitive failed" | ||
end | ||
|
||
## | ||
# Returns a random value from a range made out of Time, Date or DateTime | ||
# instances. | ||
# | ||
# @param [Range] range | ||
# @return [Time|Date|DateTime] | ||
# | ||
def random_time_range(range) | ||
min = time_to_float(range.min) | ||
max = time_to_float(range.max) | ||
time = Time.at(random(min..max)) | ||
|
||
if Object.const_defined?(:DateTime) && range.min.is_a?(DateTime) | ||
time = time.to_datetime | ||
elsif Object.const_defined?(:DateTime) && range.min.is_a?(Date) | ||
time = time.to_date | ||
end | ||
|
||
return time | ||
end | ||
|
||
## | ||
# Casts a Time/Date/DateTime instance to a Float. | ||
# | ||
# @param [Time|Date|DateTime] input | ||
# @return [Float] | ||
# | ||
def time_to_float(input) | ||
return input.respond_to?(:to_time) ? input.to_time.to_f : input.to_f | ||
end | ||
|
||
## | ||
# Checks if a given value is a Time, Date or DateTime object. | ||
# | ||
# @param [Mixed] input | ||
# @return [TrueClass|FalseClass] | ||
# | ||
def time_value?(input) | ||
return input.is_a?(Time) || (Object.const_defined?(:Date) && input.is_a?(Date)) | ||
end | ||
end | ||
|
||
class Random | ||
def self.new_seed | ||
Thread.current.randomizer.generate_seed | ||
end | ||
|
||
def self.srand(seed=undefined) | ||
if undefined.equal? seed | ||
seed = Thread.current.randomizer.generate_seed | ||
end | ||
|
||
seed = Rubinius::Type.coerce_to seed, Integer, :to_int | ||
Thread.current.randomizer.swap_seed seed | ||
end | ||
|
||
def self.rand(limit=undefined) | ||
Thread.current.randomizer.random(limit) | ||
end | ||
|
||
def initialize(seed=undefined) | ||
@randomizer = Rubinius::Randomizer.new | ||
if !undefined.equal?(seed) | ||
@randomizer.swap_seed seed.to_int | ||
end | ||
end | ||
|
||
def rand(limit=undefined) | ||
@randomizer.random(limit) | ||
end | ||
|
||
def seed | ||
@randomizer.seed | ||
end | ||
|
||
def state | ||
@randomizer.seed | ||
end | ||
private :state | ||
|
||
def ==(other) | ||
return false unless other.kind_of?(Random) | ||
seed == other.seed | ||
end | ||
|
||
# Returns a random binary string. | ||
# The argument size specified the length of the result string. | ||
def bytes(length) | ||
length = Rubinius::Type.coerce_to length, Integer, :to_int | ||
s = '' | ||
i = 0 | ||
while i < length | ||
s << @randomizer.random_integer(255).chr | ||
i += 1 | ||
end | ||
|
||
s | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters