pub mod core;
pub mod helpers;
use crate::class::BridgedClassDef;
use crate::env::Environment;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use std::sync::{Arc, LazyLock, Mutex, OnceLock};
static PACKAGE_LIBRARIES: LazyLock<Mutex<HashMap<String, Vec<Arc<BridgedClassDef>>>>> =
LazyLock::new(|| Mutex::new(HashMap::new()));
static MAIN_SCRIPT_PATH: OnceLock<String> = OnceLock::new();
pub fn set_main_script_path(path: &str) {
let _ = MAIN_SCRIPT_PATH.set(path.to_string());
}
pub fn main_script_path() -> Option<String> {
MAIN_SCRIPT_PATH.get().cloned()
}
pub fn register_package_library(uri: &str, classes: Vec<Arc<BridgedClassDef>>) {
let mut reg = PACKAGE_LIBRARIES.lock().unwrap();
let entry = reg.entry(uri.to_string()).or_default();
for c in classes {
if !entry.iter().any(|e| e.name == c.name) {
entry.push(c);
}
}
}
pub fn register_module_library(uri: &str, classes: Vec<Arc<BridgedClassDef>>) {
register_package_library(uri, classes);
}
pub fn module_library_environment(uri: &str) -> Option<Rc<RefCell<Environment>>> {
let classes = crate::state::current()
.module_libraries
.borrow()
.get(uri)
.cloned()
.or_else(|| PACKAGE_LIBRARIES.lock().unwrap().get(uri).cloned())?;
let env = Environment::new();
for bc in classes {
crate::log::debug(&format!(
"Registering module class '{}' from '{uri}'",
bc.name
));
Environment::define_bridge(&env, bc.clone());
}
Some(env)
}
static GLOBAL_DART_MODULES: LazyLock<Mutex<HashMap<String, String>>> =
LazyLock::new(|| Mutex::new(HashMap::new()));
pub fn register_dart_module(name: &str, source: &str) {
GLOBAL_DART_MODULES
.lock()
.unwrap()
.insert(name.to_string(), source.to_string());
}
pub fn dart_module_source(name: &str) -> Option<String> {
GLOBAL_DART_MODULES.lock().unwrap().get(name).cloned()
}
pub fn package_library_environment(uri: &str) -> Option<Rc<RefCell<Environment>>> {
module_library_environment(uri)
}
pub fn load_library(uri: &str) -> Option<Rc<RefCell<Environment>>> {
let state = crate::state::current();
if let Some(env) = state.libraries.borrow().get(uri).cloned() {
return Some(env);
}
let env = match uri {
"dart:core" => {
let env = Environment::new();
core::load_core(&env);
env
}
"dart:math" => {
#[cfg(feature = "math")]
{
let env = Environment::new();
math::load(&env);
env
}
#[cfg(not(feature = "math"))]
{
return None;
}
}
"dart:convert" => {
#[cfg(feature = "convert")]
{
let env = Environment::new();
convert::load(&env);
env
}
#[cfg(not(feature = "convert"))]
{
return None;
}
}
"dart:collection" => {
#[cfg(feature = "collection")]
{
let env = Environment::new();
collection::load(&env);
env
}
#[cfg(not(feature = "collection"))]
{
return None;
}
}
"dart:typed_data" => {
#[cfg(feature = "typed_data")]
{
let env = Environment::new();
typed_data::load(&env);
env
}
#[cfg(not(feature = "typed_data"))]
{
return None;
}
}
"dart:async" => {
#[cfg(feature = "async")]
{
let env = Environment::new();
async_lib::load(&env);
stream_lib::load(&env);
env
}
#[cfg(not(feature = "async"))]
{
return None;
}
}
"dart:io" => {
#[cfg(feature = "io")]
{
let env = Environment::new();
io::load(&env);
process::load(&env);
http::load(&env);
convert::load(&env);
env
}
#[cfg(not(feature = "io"))]
{
return None;
}
}
"dart:isolate" => {
#[cfg(feature = "isolate")]
{
let env = Environment::new();
isolate::load(&env);
env
}
#[cfg(not(feature = "isolate"))]
{
return None;
}
}
_ => return None,
};
state
.libraries
.borrow_mut()
.insert(uri.to_string(), env.clone());
Some(env)
}
pub mod async_lib;
#[cfg(feature = "collection")]
pub mod collection;
#[cfg(feature = "convert")]
pub mod convert;
#[cfg(feature = "io")]
pub mod http;
#[cfg(feature = "io")]
pub mod io;
#[cfg(feature = "isolate")]
pub mod isolate;
#[cfg(feature = "math")]
pub mod math;
#[cfg(feature = "io")]
pub mod process;
pub mod stream_lib;
#[cfg(feature = "typed_data")]
pub mod typed_data;