#![allow(unexpected_cfgs)]
use bun_collections::StringArrayHashMap;
use bun_wyhash::Wyhash11;
use crate::{Expr, Ref};
pub struct Runtime;
impl Runtime {
pub fn source_code() -> &'static [u8] {
bun_core::runtime_embed_file!(Codegen, "runtime.out.js").as_bytes()
}
pub fn version_hash() -> u32 {
let hash = Wyhash11::hash(0, Self::source_code());
hash as u32 }
}
#[derive(Clone)]
pub enum ReplaceableExport {
Delete,
Replace(Expr),
Inject { name: Box<[u8]>, value: Expr },
}
impl ReplaceableExport {
#[inline]
pub fn is_replace(&self) -> bool {
matches!(self, Self::Replace(_))
}
}
#[derive(Default)]
pub struct ReplaceableExportMap {
pub entries: StringArrayHashMap<ReplaceableExport>,
}
impl core::ops::Deref for ReplaceableExportMap {
type Target = StringArrayHashMap<ReplaceableExport>;
#[inline]
fn deref(&self) -> &Self::Target {
&self.entries
}
}
impl core::ops::DerefMut for ReplaceableExportMap {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.entries
}
}
impl ReplaceableExportMap {
#[inline]
pub fn count(&self) -> usize {
self.entries.count()
}
#[inline]
pub fn get_ptr(&self, key: &[u8]) -> Option<&ReplaceableExport> {
self.entries.get(key)
}
#[inline]
pub fn get_ptr_mut(&mut self, key: &[u8]) -> Option<&mut ReplaceableExport> {
self.entries.get_ptr_mut(key)
}
#[inline]
pub fn contains(&self, key: &[u8]) -> bool {
self.entries.contains(key)
}
}
#[derive(Clone, Copy, PartialEq, Eq, Default)]
pub enum ServerComponentsMode {
#[default]
None,
WrapAnonServerFunctions,
WrapExportsForClientReference,
WrapExportsForServerReference,
ClientSide,
}
impl ServerComponentsMode {
pub fn is_server_side(self) -> bool {
matches!(
self,
Self::WrapExportsForServerReference | Self::WrapAnonServerFunctions
)
}
pub fn wraps_exports(self) -> bool {
matches!(
self,
Self::WrapExportsForClientReference | Self::WrapExportsForServerReference
)
}
#[inline]
pub fn is_enabled(self) -> bool {
!matches!(self, Self::None)
}
}
#[derive(Default, Clone, Copy)]
pub struct Names;
impl Names {
pub const ACTIVATE_FUNCTION: &'static [u8] = b"activate";
}
#[allow(non_snake_case)]
#[derive(Default, Clone)]
pub struct Imports {
pub __name: Option<Ref>,
pub __require: Option<Ref>,
pub __export: Option<Ref>,
pub __reExport: Option<Ref>,
pub __exportValue: Option<Ref>,
pub __exportDefault: Option<Ref>,
pub __merge: Option<Ref>,
pub __legacyDecorateClassTS: Option<Ref>,
pub __legacyDecorateParamTS: Option<Ref>,
pub __legacyMetadataTS: Option<Ref>,
pub __publicField: Option<Ref>,
pub __privateIn: Option<Ref>,
pub __privateGet: Option<Ref>,
pub __privateAdd: Option<Ref>,
pub __privateSet: Option<Ref>,
pub __privateMethod: Option<Ref>,
pub __decoratorStart: Option<Ref>,
pub __decoratorMetadata: Option<Ref>,
pub __runInitializers: Option<Ref>,
pub __decorateElement: Option<Ref>,
pub dollar_dollar_typeof: Option<Ref>,
pub __using: Option<Ref>,
pub __callDispose: Option<Ref>,
pub __jsonParse: Option<Ref>,
pub __promiseAll: Option<Ref>,
}
impl Imports {
pub const ALL: [&'static [u8]; 25] = [
b"__name",
b"__require",
b"__export",
b"__reExport",
b"__exportValue",
b"__exportDefault",
b"__merge",
b"__legacyDecorateClassTS",
b"__legacyDecorateParamTS",
b"__legacyMetadataTS",
b"__publicField",
b"__privateIn",
b"__privateGet",
b"__privateAdd",
b"__privateSet",
b"__privateMethod",
b"__decoratorStart",
b"__decoratorMetadata",
b"__runInitializers",
b"__decorateElement",
b"$$typeof",
b"__using",
b"__callDispose",
b"__jsonParse",
b"__promiseAll",
];
#[cfg_attr(not(test), allow(dead_code))]
const ALL_SORTED: [&'static [u8]; 25] = [
b"$$typeof",
b"__callDispose",
b"__decorateElement",
b"__decoratorMetadata",
b"__decoratorStart",
b"__export",
b"__exportDefault",
b"__exportValue",
b"__jsonParse",
b"__legacyDecorateClassTS",
b"__legacyDecorateParamTS",
b"__legacyMetadataTS",
b"__merge",
b"__name",
b"__privateAdd",
b"__privateGet",
b"__privateIn",
b"__privateMethod",
b"__privateSet",
b"__promiseAll",
b"__publicField",
b"__reExport",
b"__require",
b"__runInitializers",
b"__using",
];
pub const ALL_SORTED_INDEX: [usize; 25] = [
13, 22, 5, 21, 7, 6, 12, 9, 10, 11, 20, 16, 15, 14, 18, 17, 4, 3, 23, 2, 0, 24, 1, 8, 19, ];
pub const NAME: &'static [u8] = b"bun:wrap";
pub const ALT_NAME: &'static [u8] = b"bun:wrap";
#[inline]
fn field(&self, i: usize) -> Option<Ref> {
match i {
0 => self.__name,
1 => self.__require,
2 => self.__export,
3 => self.__reExport,
4 => self.__exportValue,
5 => self.__exportDefault,
6 => self.__merge,
7 => self.__legacyDecorateClassTS,
8 => self.__legacyDecorateParamTS,
9 => self.__legacyMetadataTS,
10 => self.__publicField,
11 => self.__privateIn,
12 => self.__privateGet,
13 => self.__privateAdd,
14 => self.__privateSet,
15 => self.__privateMethod,
16 => self.__decoratorStart,
17 => self.__decoratorMetadata,
18 => self.__runInitializers,
19 => self.__decorateElement,
20 => self.dollar_dollar_typeof,
21 => self.__using,
22 => self.__callDispose,
23 => self.__jsonParse,
24 => self.__promiseAll,
_ => None,
}
}
#[inline]
fn field_mut(&mut self, i: usize) -> Option<&mut Option<Ref>> {
match i {
0 => Some(&mut self.__name),
1 => Some(&mut self.__require),
2 => Some(&mut self.__export),
3 => Some(&mut self.__reExport),
4 => Some(&mut self.__exportValue),
5 => Some(&mut self.__exportDefault),
6 => Some(&mut self.__merge),
7 => Some(&mut self.__legacyDecorateClassTS),
8 => Some(&mut self.__legacyDecorateParamTS),
9 => Some(&mut self.__legacyMetadataTS),
10 => Some(&mut self.__publicField),
11 => Some(&mut self.__privateIn),
12 => Some(&mut self.__privateGet),
13 => Some(&mut self.__privateAdd),
14 => Some(&mut self.__privateSet),
15 => Some(&mut self.__privateMethod),
16 => Some(&mut self.__decoratorStart),
17 => Some(&mut self.__decoratorMetadata),
18 => Some(&mut self.__runInitializers),
19 => Some(&mut self.__decorateElement),
20 => Some(&mut self.dollar_dollar_typeof),
21 => Some(&mut self.__using),
22 => Some(&mut self.__callDispose),
23 => Some(&mut self.__jsonParse),
24 => Some(&mut self.__promiseAll),
_ => None,
}
}
pub fn iter(&self) -> ImportsIterator<'_> {
ImportsIterator {
i: 0,
runtime_imports: self,
}
}
pub fn contains(&self, key: &[u8]) -> bool {
Self::ALL
.iter()
.position(|&k| k == key)
.and_then(|i| self.field(i))
.is_some()
}
pub fn has_any(&self) -> bool {
for i in 0..Self::ALL.len() {
if self.field(i).is_some() {
return true;
}
}
false
}
pub fn put(&mut self, key: &[u8], ref_: Ref) {
if let Some(i) = Self::ALL.iter().position(|&k| k == key) {
if let Some(slot) = self.field_mut(i) {
*slot = Some(ref_);
}
}
}
pub fn at(&self, key: &[u8]) -> Option<Ref> {
Self::ALL
.iter()
.position(|&k| k == key)
.and_then(|i| self.field(i))
}
pub fn get(&self, key: usize) -> Option<Ref> {
if key < Self::ALL.len() {
self.field(key)
} else {
None
}
}
pub fn count(&self) -> usize {
let mut n: usize = 0;
for i in 0..Self::ALL.len() {
if self.field(i).is_some() {
n += 1;
}
}
n
}
}
pub struct ImportsIterator<'a> {
pub i: usize,
pub runtime_imports: &'a Imports,
}
#[derive(Clone, Copy)]
pub struct ImportsIteratorEntry {
pub key: u16,
pub value: Ref,
}
impl ImportsIterator<'_> {
pub fn next(&mut self) -> Option<ImportsIteratorEntry> {
while self.i < Imports::ALL.len() {
let t = self.i;
self.i += 1; if let Some(val) = self.runtime_imports.field(t) {
return Some(ImportsIteratorEntry {
key: u16::try_from(t).expect("int cast"),
value: val,
});
}
}
None
}
}
#[cfg(test)]
mod tests {
use super::Imports;
#[test]
fn all_sorted_matches_zig_comptime() {
let mut list = Imports::ALL;
list.sort_unstable();
assert_eq!(
list,
Imports::ALL_SORTED,
"ALL_SORTED drifted from sorted(ALL)"
);
let mut out = [0usize; Imports::ALL.len()];
for (i, name) in Imports::ALL.iter().enumerate() {
for (j, cmp) in list.iter().enumerate() {
if name == cmp {
out[i] = j;
break;
}
}
}
assert_eq!(
out,
Imports::ALL_SORTED_INDEX,
"ALL_SORTED_INDEX drifted from derivation"
);
}
}