Repository navigation
Expand file tree
/
Copy pathlib.rs
More file actions
104 lines (93 loc) · 2.62 KB
/
Copy pathlib.rs
File metadata and controls
104 lines (93 loc) · 2.62 KB
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
use napi::{bindgen_prelude::*, ScopedTask};
use napi_derive::napi;
use parse_ast::parse_ast;
use std::mem;
#[cfg(all(
not(all(target_os = "linux", target_arch = "loongarch64")),
not(all(target_os = "linux", target_arch = "riscv64", target_env = "musl")),
not(all(target_os = "linux", target_env = "ohos")),
not(all(target_os = "freebsd", target_arch = "aarch64")),
not(debug_assertions)
))]
#[global_allocator]
static ALLOC: mimalloc_safe::MiMalloc = mimalloc_safe::MiMalloc;
pub struct ParseTask {
pub code: String,
pub allow_return_outside_function: bool,
pub jsx: bool,
}
#[napi]
impl<'task> ScopedTask<'task> for ParseTask {
type Output = Vec<u8>;
type JsValue = BufferSlice<'task>;
fn compute(&mut self) -> Result<Self::Output> {
Ok(parse_ast(
mem::take(&mut self.code),
self.allow_return_outside_function,
self.jsx,
))
}
fn resolve(&mut self, env: &'task Env, output: Self::Output) -> Result<Self::JsValue> {
BufferSlice::from_data(env, output)
}
}
#[napi]
pub fn parse<'env>(
env: &'env Env,
code: String,
allow_return_outside_function: bool,
jsx: bool,
) -> Result<BufferSlice<'env>> {
BufferSlice::from_data(env, parse_ast(code, allow_return_outside_function, jsx))
}
#[napi]
pub fn parse_async(
code: String,
allow_return_outside_function: bool,
jsx: bool,
signal: Option<AbortSignal>,
) -> AsyncTask<ParseTask> {
AsyncTask::with_optional_signal(
ParseTask {
code,
allow_return_outside_function,
jsx,
},
signal,
)
}
#[napi]
pub fn xxhash_base64_url(input: &[u8]) -> String {
xxhash::xxhash_base64_url(input)
}
#[napi]
pub fn xxhash_base36(input: &[u8]) -> String {
xxhash::xxhash_base36(input)
}
#[napi]
pub fn xxhash_base16(input: &[u8]) -> String {
xxhash::xxhash_base16(input)
}
/// Manually writes the LLVM coverage profile data to disk.
///
/// On Linux, the LLVM profiler runtime's `atexit` handler does not fire for
/// shared libraries loaded via `dlopen` (as Node loads `.node` addons), so
/// coverage data is never flushed. This function calls
/// `__llvm_profile_initialize_file()` (to parse `LLVM_PROFILE_FILE`) and
/// `__llvm_profile_write_file()` directly so the test runner can invoke it
/// from a `process.on('exit')` handler.
///
/// Only compiled when `cfg(coverage)` is active (set by `cargo-llvm-cov
/// show-env`), so it has zero impact on production builds.
#[cfg(coverage)]
#[napi]
pub fn flush_llvm_coverage() {
extern "C" {
fn __llvm_profile_initialize_file();
fn __llvm_profile_write_file() -> std::os::raw::c_int;
}
unsafe {
__llvm_profile_initialize_file();
__llvm_profile_write_file();
}
}