Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Random core class and random functions in Kernel #384

Merged
merged 3 commits into from
Nov 25, 2019

Conversation

lopopolo
Copy link
Member

This commit implements all APIs of Random except for the internal
private method Random#state and Random::Formatter.

Random is implemented with the rand crate's not cryptographically
secure SmallRng. For cryptographically secure APIs like
Random::urandom, the re-seeding rand::thread_rng is used.

Random is gated behind a crate feature.

To support the Random::DEFAULT interpreter-widre PRNG, the interpreter
stores a Random on the State and injects a special backend to allow
the DEFAULT constant to target this State-owned Random.

Random is implemented with multiple backends and has a small API which
would make supporting a future CRuby algorithm possible. The Rand
backend is generic over the rand::Rng implementation.

This commit passes 30 ruby/specs and fails 14:

$ ./scripts/spec.rb artichoke core random
Passed 30, skipped 0, not implemented 0, failed 14 specs.

The specs that fail fall into the following categories:

  • Core class not implemented: Bignum, Struct, Complex, Rational.
  • Private methods not implemented.
  • PRNG is not the same implementation as the one in CRuby.
  • Encoding not implemented, particularly Encoding::ASCII-8BIT.

This PR does some small refactorings:

  • Implement Value::implicitly_convert_to_int to simplify arg handling
    in random trampoline. All of Artichoke is not fully migrated.
  • Move Float type alias from float converter to types module.

This PR implements the Random core class and Kernel random
functions. This PR is a partial implementation of GH-34.

This commit implements all APIs of `Random` except for the internal
private method `Random#state` and `Random::Formatter`.

Random is implemented with the `rand` crate's not cryptographically
secure `SmallRng`. For cryptographically secure APIs like
`Random::urandom`, the re-seeding `rand::thread_rng` is used.

`Random` is gated behind a crate feature.

To support the `Random::DEFAULT` interpreter-widre PRNG, the interpreter
stores a `Random` on the `State` and injects a special backend to allow
the `DEFAULT` constant to target this `State`-owned `Random`.

`Random` is implemented with multiple backends and has a small API which
would make supporting a future CRuby algorithm possible. The `Rand`
backend is generic over the `rand::Rng` implementation.

This commit passes 30 ruby/specs and fails 14:

    $ ./scripts/spec.rb artichoke core random
    Passed 30, skipped 0, not implemented 0, failed 14 specs.

The specs that fail fall into the following categories:

- Core class not implemented: Bignum, Struct, Complex, Rational.
- Private methods not implemented.
- PRNG is not the same implementation as the one in CRuby.
- `Encoding` not implemented, particularly `Encoding::ASCII-8BIT`.

This PR does some small refactorings:

- Implement `Value::implicitly_convert_to_int` to simplify arg handling
  in `random` trampoline. All of Artichoke is not fully migrated.
- Move `Float` type alias from float converter to `types` module.

This PR implements the `Random` core class and `Kernel` random
functions. This PR is a partial implementation of GH-34.
@lopopolo lopopolo added A-ruby-core Area: Ruby Core types. A-deps Area: Source and library dependencies. labels Nov 24, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-deps Area: Source and library dependencies. A-ruby-core Area: Ruby Core types.
Development

Successfully merging this pull request may close these issues.

None yet

1 participant