platform

Minimal native platform detection for MoonBit packages.

moonbit
native
platform
ffi
moon add justjavac/platform@0.1.2
Download zip
Author
Version
0.1.2
License
MIT
Last updated
2 months ago
Downloads
40
README

#justjavac/platform

coverage linux macos windows

justjavac/platform is a small native-only MoonBit package for detecting basic platform identity at compile time. It reports the operating system and CPU architecture that the current native binary was built for, then provides a few convenience helpers for common platform-specific strings.

The package is intentionally narrow: it does not inspect environment variables, read files, spawn shell commands, or call desktop services. All values come from native compiler macros through a tiny C stub.

#Quick Start

let current = @platform.current()
println(current.target())
println(current.os_name())
println(current.arch_name())

#justjavac/platform

Minimal native platform detection for Windows, Linux, and macOS.

///|
test {
let current = @platform.current()
inspect(current.os_name() != "", content="true")
inspect(current.arch_name() != "", content="true")
}

#
Arch

pub(all) enum Arch {
X86
X64
Arm
Arm64
Riscv64
UnknownArch
} derive(Eq,
Debug
)

CPU architectures recognized by the native platform detector.

Arch describes the processor family that the native binary was compiled for. The names returned by Arch::name are stable lowercase target labels, so they are suitable for logs, cache keys, and combined target strings. Targets outside the known set are represented as UnknownArch.
impl Show for Arch

#
Arch::is_known

fn Arch::is_known(self : Arch) -> Bool

Return whether this architecture is known.

This is a lightweight guard for code that wants architecture-specific behavior only when the detector recognized the target. UnknownArch returns false; every other Arch value returns true.

#
Arch::name

fn Arch::name(self : Arch) -> String

Return a stable lowercase architecture name.

The returned strings are x86, x86_64, arm, aarch64, riscv64, or unknown. They are intended for logs, target identifiers, filenames, and combined target strings.

#
Info

pub(all) struct Info {
os : Os
arch : Arch
} derive(Eq,
Debug
)

Core platform facts for the current native binary.

Info keeps the operating system and architecture together so callers can pass a single value around instead of querying both dimensions repeatedly. The values are derived from compile-time C macros. No filesystem, environment, shell, or desktop APIs are touched, which makes the result deterministic for a given compiled binary.
impl Show for Info

#
Info::arch_name

fn Info::arch_name(self : Info) -> String

Return the architecture name for this platform info.

This delegates to Arch::name on the contained arch value and returns the same stable lowercase strings used by the architecture enum.

#
Info::executable_suffix

fn Info::executable_suffix(self : Info) -> String

Return the executable suffix for this platform info.

This delegates to Os::executable_suffix on the contained os value, so Windows returns .exe and all other values return an empty string.

#
Info::family

fn Info::family(self : Info) -> String

Return the broad operating-system family for this platform info.

This delegates to Os::family on the contained os value and returns windows, unix, or unknown.

#
Info::is_known

fn Info::is_known(self : Info) -> Bool

Return whether both OS and architecture were detected.

This returns true only when neither field is unknown. Use it when callers need to reject or special-case targets that the package cannot identify precisely.

#
Info::is_unix

fn Info::is_unix(self : Info) -> Bool

Return whether this platform info represents a Unix-like OS.

This delegates to Os::is_unix on the contained os value. macOS and Linux return true; Windows and unknown targets return false.

#
Info::is_windows

fn Info::is_windows(self : Info) -> Bool

Return whether this platform info represents Windows.

This delegates to Os::is_windows on the contained os value. It is a convenient shorthand when code already carries an Info value.

#
Info::line_ending

fn Info::line_ending(self : Info) -> String

Return the line ending convention for this platform info.

This delegates to Os::line_ending on the contained os value, so Windows returns CRLF and all other values return LF.

#
Info::os_name

fn Info::os_name(self : Info) -> String

Return the operating-system name for this platform info.

This delegates to Os::name on the contained os value and returns the same stable lowercase strings: windows, macos, linux, or unknown.

#
Info::path_separator

fn Info::path_separator(self : Info) -> String

Return the path separator for this platform info.

This delegates to Os::path_separator on the contained os value, so Windows returns \ and all other values return /.

#
Info::target

fn Info::target(self : Info) -> String

Return a compact os-arch target string.

The result combines Info::os_name and Info::arch_name, for example windows-x86_64, linux-aarch64, or unknown-unknown. It is suitable for diagnostics, cache keys, and simple platform labels.
pub(all) enum Os {
Windows
MacOS
Linux
UnknownOs
} derive(Eq,
Debug
)

Operating systems recognized by the native platform detector.

Os is a compact enum for platform branches that usually need different filenames, separators, line endings, or feature choices. The detector currently distinguishes Windows, macOS, Linux, and UnknownOs for every other native target. Prefer the helper methods on Os when you need stable lowercase names or coarse family checks.
impl Show for Os

#
Os::executable_suffix

fn Os::executable_suffix(self : Os) -> String

Return the executable suffix used by this operating system.

Windows returns .exe; all other values return an empty string. The method is intentionally simple and mirrors the common convention needed when composing executable filenames.

#
Os::family

fn Os::family(self : Os) -> String

Return the broad operating-system family.

Windows maps to windows, macOS and Linux map to unix, and unknown targets map to unknown. This is useful when code only needs to choose between Windows-style and Unix-style conventions.

#
Os::is_known

fn Os::is_known(self : Os) -> Bool

Return whether this operating system is known.

This is a lightweight guard for code that wants to use specific platform behavior only when the detector recognized the target. UnknownOs returns false; every other Os value returns true.

#
Os::is_unix

fn Os::is_unix(self : Os) -> Bool

Return whether this operating system is Unix-like.

macOS and Linux return true. Windows and unknown targets return false, which keeps fallback behavior conservative on targets the package does not explicitly recognize.

#
Os::is_windows

fn Os::is_windows(self : Os) -> Bool

Return whether this operating system is Windows.

Use this helper instead of comparing names when choosing Windows-specific paths, executable suffixes, or command-line behavior.

#
Os::line_ending

fn Os::line_ending(self : Os) -> String

Return the line ending convention used by this operating system.

Windows returns CRLF and all other values return LF. Unknown targets use LF as the package's conservative default.

#
Os::name

fn Os::name(self : Os) -> String

Return a stable lowercase operating-system name.

The returned strings are windows, macos, linux, or unknown. They are intended for logs, target identifiers, filenames, and other places where the enum constructor spelling would be too presentation-oriented.

#
Os::path_separator

fn Os::path_separator(self : Os) -> String

Return the path separator used by this operating system.

Windows returns \; all other values return /. Unknown targets use the Unix-style separator as the package's conservative default.

#
arch

fn arch() -> Arch

Return the CPU architecture detected for this native binary.

The value comes from native compile-time macros through the bundled C stub. Known targets include x86, x86_64, arm, aarch64, and riscv64. Every other target returns UnknownArch so callers can decide their own fallback.

#
current

fn current() -> Info

Return the complete platform identity for this native binary.

This is the preferred entry point when you need both OS and architecture, because the returned Info value can produce names, families, target strings, and OS conventions without repeating the top-level queries.
fn os() -> Os

Return the operating system detected for this native binary.

The value comes from native compile-time macros through the bundled C stub. A known target returns one of Windows, MacOS, or Linux; every other target returns UnknownOs so callers can decide their own fallback.

Source Files

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io