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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use cfg_if::cfg_if;
cfg_if! {
if #[cfg(target_arch = "wasm32")]{
use js_sys::{Function, Object};
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(extends = Object)]
#[derive(Debug)]
type __NodeJsNodeWebkitInfo__;
#[wasm_bindgen(method, getter)]
fn is_node_js(this: &__NodeJsNodeWebkitInfo__) -> bool;
#[wasm_bindgen(method, getter)]
fn is_node_webkit(this: &__NodeJsNodeWebkitInfo__) -> bool;
}
static mut DETECT: Option<(bool,bool)> = None;
fn detect() -> (bool, bool) {
unsafe { DETECT }.unwrap_or_else(||{
let result = Function::new_no_args("
let is_node_js = (
typeof process === 'object' &&
typeof process.versions === 'object' &&
typeof process.versions.node !== 'undefined'
);
let is_node_webkit = false;
if(is_node_js) {
is_node_webkit = (typeof nw !== 'undefined' && typeof nw.Window !== 'undefined');
}
return {
is_node_js,
is_node_webkit
}
").call0(&JsValue::undefined());
let flags = match result {
Ok(value) => {
if value.is_undefined() {
(false, false)
} else {
let info: __NodeJsNodeWebkitInfo__ = value.into();
(info.is_node_js(), info.is_node_webkit())
}
}
Err(_) => {
(false, false)
}
};
unsafe { DETECT = Some(flags) };
flags
})
}
pub fn is_node() -> bool {
detect().0
}
pub fn is_nw() -> bool {
detect().1
}
pub fn is_web()->bool{
!is_node()
}
}else{
pub fn is_node() -> bool {
false
}
pub fn is_nw() -> bool {
false
}
pub fn is_web()->bool{
false
}
}
}
pub fn is_solana() -> bool {
cfg_if! {
if #[cfg(target_os = "solana")]{
true
}else{
false
}
}
}
pub fn is_wasm() -> bool {
cfg_if! {
if #[cfg(target_arch = "wasm32")]{
true
}else{
false
}
}
}
pub fn is_native() -> bool {
cfg_if! {
if #[cfg(any(target_os = "solana", target_arch = "wasm32"))] {
false
}else{
true
}
}
}
#[derive(Debug)]
pub enum Runtime {
Native,
Solana,
NW,
Node,
Web,
}
impl From<&Runtime> for String {
fn from(value: &Runtime) -> Self {
match value {
Runtime::Native => "Native",
Runtime::Solana => "Solana",
Runtime::NW => "NW",
Runtime::Node => "Node",
Runtime::Web => "Web",
}
.to_string()
}
}
impl std::fmt::Display for Runtime {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let str: String = self.into();
f.write_str(&str)
}
}
impl Runtime {
pub fn get() -> Self {
if is_solana() {
Runtime::Solana
} else if is_wasm() {
if is_nw() {
Runtime::NW
} else if is_node() {
Runtime::Node
} else {
Runtime::Web
}
} else {
Runtime::Native
}
}
}