componentized/directory

componentized:constants

interface dev

Create custom wasm components that export constant values.

Interfaces

  • atomics
  • config-values — Constant config values for the `wasi:config/store` interface.
  • factory — Creates components whose exported functions return constant values.
  • math — Common mathematical constants and sequences.
  • random — Pseudorandom number generators.
  • u8 — Constants for the `u8` unsigned integer type.
  • u16 — Constants for the `u16` unsigned integer type.
  • u32 — Constants for the `u32` unsigned integer type.
  • u64 — Constants for the `u64` unsigned integer type.
  • s8 — Constants for the `s8` signed integer type.
  • s16 — Constants for the `s16` signed integer type.
  • s32 — Constants for the `s32` signed integer type.
  • s64 — Constants for the `s64` signed integer type.
  • f32 — Constants for the `f32` floating point type.
  • f64 — Constants for the `f64` floating point type.

Worlds

WIT

package componentized:constants@0.2.1-dev;

interface atomics {
  /// Incrementing integers. Stream closes when it reaches max int.
  /// @expression |v| v + 1
  incrementer: async func(seed: u64) -> stream<u64>;

  /// Decrementing integers. Stream closes when it reaches zero.
  /// @expression |v| v - 1
  decrementer: async func(seed: u64) -> stream<u64>;
}

/// Constant config values for the `wasi:config/store` interface.
interface config-values {
  /// Values to be exposed via the `wasi:config/store` interface.
  ///
  /// Either use this interface with custom override values, or duplicate
  /// this interface updating the default values defined below.
  /// @value []
  values: func() -> list<tuple<string, string>>;
}

/// Creates components whose exported functions return constant values.
interface factory {
  use componentized:component/types@0.1.0.{wasm, error-code};
  use componentized:component/wit@0.1.0.{wit, wit-source};

  /// Values replacing those from the WIT. The overrides are a record with
  /// an optional field per world export. Interface exports are named by the
  /// interface and hold a nested record with a field per function.
  variant overrides {
    /// The record as WAVE source, `//` line comments are allowed.
    wave(string),
  }

  /// Creates a component implementing `world` from `wit`.
  ///
  /// Every function the world exports must take no parameters and return a
  /// value. Values can't reach maps, handles, futures, streams or error
  /// contexts, though result types may include them in branches the value
  /// doesn't take, e.g. `none` for an `option<own<r>>`. A function may
  /// return a stream, written as a list of its items, which must be an
  /// async function unless the stream is empty. Each function's value is a
  /// WAVE expression following a `@value` tag in its doc comment. A stream
  /// may continue with items generated by a Rust closure following an
  /// `@expression` tag, e.g. `|a, b| a + b`, from the items before them,
  /// starting from either its `@value` or the function's arguments.
  ///
  /// - `world`: the world to implement, required when the WIT package
  ///   defines more than one world.
  /// - `overrides`: values replacing those from the WIT.
  create: async func(wit: wit-source, %world: option<string>, overrides: option<overrides>) -> result<wasm, error-code>;
}

/// Common mathematical constants and sequences.
interface math {
  /// Euler's number (e)
  /// @value 2.718281828459045
  e: func() -> f64;

  /// The Euler-Mascheroni constant (γ)
  /// @value 0.5772156649015329
  euler-gamma: func() -> f64;

  /// 1/π
  /// @value 0.3183098861837907
  frac-n1-pi: func() -> f64;

  /// 1/sqrt(2)
  /// @value 0.7071067811865476
  frac-n1-sqrt-n2: func() -> f64;

  /// 2/π
  /// @value 0.6366197723675814
  frac-n2-pi: func() -> f64;

  /// 2/sqrt(π)
  /// @value 1.1283791670955126
  frac-n2-sqrt-pi: func() -> f64;

  /// π/2
  /// @value 1.5707963267948966
  frac-pi-n2: func() -> f64;

  /// π/3
  /// @value 1.0471975511965979
  frac-pi-n3: func() -> f64;

  /// π/4
  /// @value 0.7853981633974483
  frac-pi-n4: func() -> f64;

  /// π/6
  /// @value 0.5235987755982989
  frac-pi-n6: func() -> f64;

  /// π/8
  /// @value 0.39269908169872414
  frac-pi-n8: func() -> f64;

  /// The golden ratio (φ)
  /// @value 1.618033988749895
  golden-ratio: func() -> f64;

  /// ln(2)
  /// @value 0.6931471805599453
  ln-n2: func() -> f64;

  /// ln(10)
  /// @value 2.302585092994046
  ln-n10: func() -> f64;

  /// log2(10)
  /// @value 3.321928094887362
  log2-n10: func() -> f64;

  /// log2(e)
  /// @value 1.4426950408889634
  log2-e: func() -> f64;

  /// log10(2)
  /// @value 0.3010299956639812
  log10-n2: func() -> f64;

  /// log10(e)
  /// @value 0.4342944819032518
  log10-e: func() -> f64;

  /// Archimedes' constant (π)
  /// @value 3.141592653589793
  pi: func() -> f64;

  /// sqrt(2)
  /// @value 1.4142135623730951
  sqrt-n2: func() -> f64;

  /// The full circle constant (τ)
  /// @value 6.283185307179586
  tau: func() -> f64;

  /// Odd numbers. Stream closes when it reaches max int.
  /// @value [1]
  /// @expression |v| v + 2
  odd: async func() -> stream<u64>;

  /// Even numbers. Stream closes when it reaches max int.
  /// @value [0]
  /// @expression |v| v + 2
  even: async func() -> stream<u64>;

  /// Arithmetic sequence. Stream closes when it reaches max int.
  /// @expression || n + d * $i
  arithmetic: async func(n: u64, d: u64) -> stream<u64>;

  /// Geometric sequence. Stream closes when it reaches max int.
  /// @expression || n * r ** $i
  geometric: async func(n: u64, r: u64) -> stream<u64>;

  /// Fibonacci sequence. Stream closes when it reaches max int.
  /// @value [1, 1]
  /// @expression |a, b| a + b
  fibonacci: async func() -> stream<u64>;

  /// Factorial sequence. Stream closes when it reaches max int.
  /// @value [1, 1]
  /// @expression |v| v * $i
  factorial: async func() -> stream<u64>;

  /// Harmonic sequence. Stream closes when it reaches min-positive.
  /// @expression || n / ($i + 1) as f64
  harmonic: async func(n: f64) -> stream<f64>;

  /// Figurate sequence. Stream closes when it reaches max int.
  /// @expression || $i ** n
  figurate: async func(n: u64) -> stream<u64>;

  /// Prime numbers, each found by trial division from the previous one.
  /// Stream closes when it reaches max int.
  /// @value [2]
  /// @expression |v| {
  ///     let mut n = v + 1;
  ///     let mut d: u64 = 2;
  ///     while d * d <= n {
  ///         if n % d == 0 {
  ///             n += 1;
  ///             d = 2;
  ///         } else {
  ///             d += 1;
  ///         }
  ///     }
  ///     n
  /// }
  prime: async func() -> stream<u64>;
}

/// Pseudorandom number generators.
interface random {
  /// Pseudorandom numbers from SplitMix64, each mixed from the seed and its
  /// position in the stream. The same seed always gives the same numbers.
  /// Not cryptographically secure. The stream is unbounded.
  ///
  /// @expression || {
  ///     let z = seed +% ($i +% 1) *% 0x9E3779B97F4A7C15;
  ///     let z = (z ^ z >> 30) *% 0xBF58476D1CE4E5B9;
  ///     let z = (z ^ z >> 27) *% 0x94D049BB133111EB;
  ///     z ^ z >> 31
  /// }
  seeded-random: async func(seed: u64) -> stream<u64>;

  /// Pseudorandom bytes, each 8 bytes one of `seeded-random`'s numbers for the
  /// same seed, little endian. Not cryptographically secure. The stream is
  /// unbounded.
  ///
  /// @expression || {
  ///     let z = seed +% ($i / 8 +% 1) *% 0x9E3779B97F4A7C15;
  ///     let z = (z ^ z >> 30) *% 0xBF58476D1CE4E5B9;
  ///     let z = (z ^ z >> 27) *% 0x94D049BB133111EB;
  ///     (z ^ z >> 31).to_le_bytes()
  /// }
  seeded-random-bytes: async func(seed: u64) -> stream<u8>;
}

/// Constants for the `u8` unsigned integer type.
interface %u8 {
  /// u8 max value
  /// @value 255
  max: func() -> u8;

  /// u8 min value
  /// @value 0
  min: func() -> u8;

  /// The size of `u8` in bits.
  /// @value 8
  bits: func() -> u32;
}

/// Constants for the `u16` unsigned integer type.
interface %u16 {
  /// u16 max value
  /// @value 65535
  max: func() -> u16;

  /// u16 min value
  /// @value 0
  min: func() -> u16;

  /// The size of `u16` in bits.
  /// @value 16
  bits: func() -> u32;
}

/// Constants for the `u32` unsigned integer type.
interface %u32 {
  /// u32 max value
  /// @value 4294967295
  max: func() -> u32;

  /// u32 min value
  /// @value 0
  min: func() -> u32;

  /// The size of `u32` in bits.
  /// @value 32
  bits: func() -> u32;
}

/// Constants for the `u64` unsigned integer type.
interface %u64 {
  /// u64 max value
  /// @value 18446744073709551615
  max: func() -> u64;

  /// u64 min value
  /// @value 0
  min: func() -> u64;

  /// The size of `u64` in bits.
  /// @value 64
  bits: func() -> u32;
}

/// Constants for the `s8` signed integer type.
interface %s8 {
  /// s8 max value
  /// @value 127
  max: func() -> s8;

  /// s8 min value
  /// @value -128
  min: func() -> s8;

  /// The size of `s8` in bits.
  /// @value 8
  bits: func() -> u32;
}

/// Constants for the `s16` signed integer type.
interface %s16 {
  /// s16 max value
  /// @value 32767
  max: func() -> s16;

  /// s16 min value
  /// @value -32768
  min: func() -> s16;

  /// The size of `s16` in bits.
  /// @value 16
  bits: func() -> u32;
}

/// Constants for the `s32` signed integer type.
interface %s32 {
  /// s32 max value
  /// @value 2147483647
  max: func() -> s32;

  /// s32 min value
  /// @value -2147483648
  min: func() -> s32;

  /// The size of `s32` in bits.
  /// @value 32
  bits: func() -> u32;
}

/// Constants for the `s64` signed integer type.
interface %s64 {
  /// s64 max value
  /// @value 9223372036854775807
  max: func() -> s64;

  /// s64 min value
  /// @value -9223372036854775808
  min: func() -> s64;

  /// The size of `s64` in bits.
  /// @value 64
  bits: func() -> u32;
}

/// Constants for the `f32` floating point type.
interface %f32 {
  /// The radix or base of the internal representation of `f32`.
  /// @value 2
  radix: func() -> u32;

  /// Number of significant digits in base 2.
  /// @value 24
  mantissa-digits: func() -> u32;

  /// Approximate number of significant digits in base 10.
  /// @value 6
  digits: func() -> u32;

  /// Machine epsilon value for `f32`, the difference between `1.0` and the next larger representable number.
  /// @value 1.19209290e-07
  epsilon: func() -> f32;

  /// Smallest finite `f32` value.
  /// @value -3.40282347e+38
  min: func() -> f32;

  /// Smallest positive normal `f32` value.
  /// @value 1.17549435e-38
  min-positive: func() -> f32;

  /// Largest finite `f32` value.
  /// @value 3.40282347e+38
  max: func() -> f32;

  /// One greater than the minimum possible normal power of 2 exponent.
  /// @value -125
  min-exp: func() -> s32;

  /// Maximum possible power of 2 exponent.
  /// @value 128
  max-exp: func() -> s32;

  /// Minimum *x* for which 10<sup>*x*</sup> is normal.
  /// @value -37
  min-n10-exp: func() -> s32;

  /// Maximum *x* for which 10<sup>*x*</sup> is normal.
  /// @value 38
  max-n10-exp: func() -> s32;

  /// Not a Number (NaN).
  /// @value nan
  nan: func() -> f32;

  /// Infinity (∞).
  /// @value inf
  infinity: func() -> f32;

  /// Negative infinity (−∞).
  /// @value -inf
  neg-infinity: func() -> f32;
}

/// Constants for the `f64` floating point type.
interface %f64 {
  /// The radix or base of the internal representation of `f64`.
  /// @value 2
  radix: func() -> u32;

  /// Number of significant digits in base 2.
  /// @value 53
  mantissa-digits: func() -> u32;

  /// Approximate number of significant digits in base 10.
  /// @value 15
  digits: func() -> u32;

  /// Machine epsilon value for `f64`, the difference between `1.0` and the next larger representable number.
  /// @value 2.2204460492503131e-16
  epsilon: func() -> f64;

  /// Smallest finite `f64` value.
  /// @value -1.7976931348623157e+308
  min: func() -> f64;

  /// Smallest positive normal `f64` value.
  /// @value 2.2250738585072014e-308
  min-positive: func() -> f64;

  /// Largest finite `f64` value.
  /// @value 1.7976931348623157e+308
  max: func() -> f64;

  /// One greater than the minimum possible normal power of 2 exponent.
  /// @value -1021
  min-exp: func() -> s32;

  /// Maximum possible power of 2 exponent.
  /// @value 1024
  max-exp: func() -> s32;

  /// Minimum *x* for which 10<sup>*x*</sup> is normal.
  /// @value -307
  min-n10-exp: func() -> s32;

  /// Maximum *x* for which 10<sup>*x*</sup> is normal.
  /// @value 308
  max-n10-exp: func() -> s32;

  /// Not a Number (NaN).
  /// @value nan
  nan: func() -> f64;

  /// Infinity (∞).
  /// @value inf
  infinity: func() -> f64;

  /// Negative infinity (−∞).
  /// @value -inf
  neg-infinity: func() -> f64;
}

world config {
  import config-values;

  export wasi:config/store@0.2.0-rc.1;
}
world imports {
  import config-values;
  import componentized:component/types@0.1.0;
  import componentized:component/wit@0.1.0;
  import factory;
  import %u8;
  import %u16;
  import %u32;
  import %u64;
  import %s8;
  import %s16;
  import %s32;
  import %s64;
  import %f32;
  import %f64;
}