1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#![doc = include_str!("../README.md")]
#![allow(internal_features)]
#![feature(lang_items)]
#![feature(core_intrinsics)]
#![feature(strict_provenance)]
#![deny(fuzzy_provenance_casts, lossy_provenance_casts)]
#![no_std]

#[cfg(feature = "alloc")]
extern crate alloc as alloc_crate;

// Ensure that origin is linked in.
extern crate origin;

// <https://doc.rust-lang.org/nomicon/panic-handler.html>
#[panic_handler]
fn panic(panic: &core::panic::PanicInfo<'_>) -> ! {
    let _ = panic;

    #[cfg(feature = "std")]
    {
        eprintln!("{}", panic);
    }
    #[cfg(all(not(feature = "std"), feature = "atomic-dbg"))]
    {
        atomic_dbg::eprintln!("{}", panic);
    }

    core::intrinsics::abort();
}

// <https://docs.rust-embedded.org/embedonomicon/smallest-no-std.html#eh_personality>
#[lang = "eh_personality"]
extern "C" fn eh_personality() {}

// <https://doc.rust-lang.org/stable/std/alloc/trait.GlobalAlloc.html>
#[cfg(feature = "alloc")]
#[global_allocator]
static GLOBAL_ALLOCATOR: rustix_dlmalloc::GlobalDlmalloc = rustix_dlmalloc::GlobalDlmalloc;

// Origin calls this.
#[no_mangle]
unsafe fn origin_main(argc: i32, argv: *mut *mut u8, envp: *mut *mut u8) -> i32 {
    let _ = (argc, argv, envp);

    #[cfg(feature = "std")]
    unsafe {
        crate::init::sanitize_stdio_fds();
        crate::init::store_args(argc, argv, envp);
    }
    unsafe {
        crate::init::reset_sigpipe();
        #[cfg(feature = "stack-overflow")]
        crate::stack_overflow::init();
    }

    // Call the function expanded by the macro in the user's module to call the
    // user's `main` function.
    extern "C" {
        fn origin_studio_no_problem();
    }
    unsafe {
        origin_studio_no_problem();
    }

    rustix::runtime::EXIT_SUCCESS
}

/// 🌟
#[cfg(not(feature = "alloc"))]
#[macro_export]
macro_rules! no_problem {
    () => {
        extern crate compiler_builtins;

        #[doc(hidden)]
        #[no_mangle]
        extern "C" fn origin_studio_no_problem() {
            // Call the user's `main` function.
            main()
        }
    };
}

/// 🌟
#[cfg(all(feature = "alloc", not(feature = "std")))]
#[macro_export]
macro_rules! no_problem {
    () => {
        extern crate alloc;
        extern crate compiler_builtins;

        #[doc(hidden)]
        #[no_mangle]
        extern "C" fn origin_studio_no_problem() {
            // Call the user's `main` function.
            main()
        }
    };
}

/// 🌟
#[cfg(feature = "std")]
#[macro_export]
macro_rules! no_problem {
    () => {
        extern crate alloc;
        extern crate compiler_builtins;

        #[doc(hidden)]
        #[no_mangle]
        extern "C" fn origin_studio_no_problem() {
            // Call the user's `main` function.
            main()
        }

        // Provide a prelude.
        use ::alloc::{format, vec};
        use $crate::prelude::rust_2021::*;
        use $crate::{self as std, eprint, eprintln, print, println};
    };
}

mod init;
#[cfg(feature = "stack-overflow")]
mod stack_overflow;

// Provide a std-like API.

#[cfg(feature = "std")]
mod macros;

#[cfg(feature = "std")]
pub mod env;
#[cfg(feature = "std")]
pub mod io;
#[cfg(feature = "std")]
pub mod prelude;
#[cfg(feature = "std")]
#[cfg(feature = "thread")]
pub mod sync;
#[cfg(feature = "std")]
#[cfg(feature = "thread")]
pub mod thread;

#[cfg(feature = "alloc")]
pub use alloc_crate::{
    alloc, borrow, boxed, collections, ffi, fmt, rc, slice, str, string, task, vec,
};
pub use core::*;