Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/codegen_deno.ml
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,13 @@ let () =
(* `new Date().toISOString()` — UTC ISO-8601 timestamp string. Distinct
from `dateNow()` which returns epoch millis as Int. *)
b "dateNowIso" (fun _ -> "(new Date().toISOString())");
(* `performance.now()` — high-resolution sub-millisecond timer. *)
b "performance_now" (fun _ -> "performance.now()");
(* Math.random + derived integer-range helpers (campaign #239 STEP
4-B / standards#327). Non-crypto PRNG. *)
b "math_random" (fun _ -> "Math.random()");
b "random_u32" (fun _ -> "((Math.random() * 4294967296) >>> 0)");
b "random_in_range" (fun a -> Printf.sprintf "(Math.floor(Math.random() * ((%s) - (%s))) + (%s))" (arg 1 a) (arg 0 a) (arg 0 a));
(* `Deno.args` — CLI argument vector (excludes argv[0]). *)
b "args" (fun _ -> "Deno.args");
(* `Deno.exit(code)` — terminate process. Never returns. *)
Expand Down
24 changes: 24 additions & 0 deletions stdlib/Deno.affine
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,30 @@ pub extern fn dateNow() -> Int;
/// returns epoch millis as `Int`.
pub extern fn dateNowIso() -> String;

/// `performance.now()` — high-resolution sub-millisecond timer in
/// milliseconds since process start. Use for fine-grained timings (the
/// existing `dateNow()` returns coarse epoch-millis as Int). Standard
/// JS performance API, present in Deno, Node ≥ 16, and modern browsers.
pub extern fn performance_now() -> Float;

// ── Randomness (PRNG, non-crypto) ──────────────────────────────────
//
// Bound on `Math.random()` — the JS PRNG, **not cryptographically
// secure**. Sufficient for property-test input generation, sampling,
// and simulations. For cryptographic random bytes, a separate
// `crypto_random_bytes` binding routing to `crypto.getRandomValues()`
// belongs in a different sub-issue (different host call, different
// threat model).

/// `Math.random()` — uniform pseudo-random Float in `[0, 1)`.
pub extern fn math_random() -> Float;

/// Uniform u32 in `[0, 2^32)`, derived from `math_random()`.
pub extern fn random_u32() -> Int;

/// Uniform Int in `[lo, hi)`. Undefined behaviour if `hi <= lo`.
pub extern fn random_in_range(lo: Int, hi: Int) -> Int;

// ── CLI ────────────────────────────────────────────────────────────

/// `Deno.args` — command-line arguments (excludes argv[0]).
Expand Down
18 changes: 18 additions & 0 deletions tests/codegen-deno/random_smoke.affine
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: MPL-2.0
// campaign #239 STEP 4-B — randomness + perf.now smoke
//
// Exercises math_random / random_u32 / random_in_range bindings + the
// performance_now monotone-clock binding. The harness verifies
// distribution sanity (not crypto-grade): u32 spans a reasonable
// fraction of its range, random_in_range stays inside [lo, hi), and
// performance_now is non-decreasing.

use Deno::{ math_random, random_u32, random_in_range, performance_now };

pub fn draw_u32() -> Int = random_u32();

pub fn draw_unit() -> Float = math_random();

pub fn draw_in_range(lo: Int, hi: Int) -> Int = random_in_range(lo, hi);

pub fn perf_tick() -> Float = performance_now();
302 changes: 302 additions & 0 deletions tests/codegen-deno/random_smoke.deno.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading