Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
711: Add std.env  r=Marwes a=Marwes

Closes gluon-lang#701 

Co-authored-by: Markus Westerlind <marwes91@gmail.com>
  • Loading branch information
bors[bot] and Marwes committed Apr 13, 2019
2 parents c5dde76 + b561c8d commit b7dd3d6
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 22 deletions.
22 changes: 8 additions & 14 deletions src/lib.rs
Expand Up @@ -38,16 +38,9 @@ extern crate gluon_codegen;
pub extern crate gluon_vm as vm;

pub mod compiler_pipeline;
#[cfg(feature = "http")]
pub mod http;
#[macro_use]
pub mod import;
pub mod io;
pub mod process;
#[cfg(all(feature = "random", not(target_arch = "wasm32")))]
pub mod rand_bind;
#[cfg(feature = "regex")]
pub mod regex_bind;
pub mod std_lib;

pub use crate::vm::thread::{RootedThread, Thread};

Expand Down Expand Up @@ -801,8 +794,9 @@ impl VmBuilder {
add_extern_module(&vm, "std.channel.prim", crate::vm::channel::load_channel);
add_extern_module(&vm, "std.thread.prim", crate::vm::channel::load_thread);
add_extern_module(&vm, "std.debug.prim", crate::vm::debug::load);
add_extern_module(&vm, "std.io.prim", crate::io::load);
add_extern_module(&vm, "std.process.prim", crate::process::load);
add_extern_module(&vm, "std.io.prim", crate::std_lib::io::load);
add_extern_module(&vm, "std.process.prim", crate::std_lib::process::load);
add_extern_module(&vm, "std.env.prim", crate::std_lib::env::load);

add_extern_module_if!(
#[cfg(feature = "serialization")],
Expand All @@ -813,25 +807,25 @@ impl VmBuilder {
add_extern_module_if!(
#[cfg(feature = "regex")],
available_if = "gluon is compiled with the 'regex' feature",
args(&vm, "std.regex.prim", crate::regex_bind::load)
args(&vm, "std.regex.prim", crate::std_lib::regex::load)
);

add_extern_module_if!(
#[cfg(feature = "web")],
available_if = "gluon is compiled with the 'web' feature",
args(&vm, "std.http.prim_types", crate::http::load_types)
args(&vm, "std.http.prim_types", crate::std_lib::http::load_types)
);

add_extern_module_if!(
#[cfg(feature = "web")],
available_if = "gluon is compiled with the 'web' feature",
args(&vm, "std.http.prim", crate::http::load)
args(&vm, "std.http.prim", crate::std_lib::http::load)
);

add_extern_module_if!(
#[cfg(all(feature = "random", not(target_arch = "wasm32")))],
available_if = "gluon is compiled with the 'random' feature and is not targeting WASM",
args(&vm, "std.random.prim", crate::rand_bind::load)
args(&vm, "std.random.prim", crate::std_lib::random::load)
);

vm
Expand Down
9 changes: 9 additions & 0 deletions src/std_lib.rs
@@ -0,0 +1,9 @@
pub mod env;
#[cfg(feature = "http")]
pub mod http;
pub mod io;
pub mod process;
#[cfg(all(feature = "random", not(target_arch = "wasm32")))]
pub mod random;
#[cfg(feature = "regex")]
pub mod regex;
98 changes: 98 additions & 0 deletions src/std_lib/env.rs
@@ -0,0 +1,98 @@
//! Module containing bindings to rust's `std::env` module.
use crate::real_std::{
env,
path::{Path, PathBuf},
};

use crate::vm::{self, api::IO, thread::Thread, ExternModule};

fn args() -> IO<Vec<String>> {
IO::Value(env::args().collect())
}

fn current_dir() -> IO<PathBuf> {
env::current_dir().into()
}

fn current_exe() -> IO<PathBuf> {
env::current_exe().into()
}

fn join_paths(paths: Vec<&Path>) -> IO<PathBuf> {
env::join_paths(paths).map(PathBuf::from).into()
}

fn remove_var(var: &str) -> IO<()> {
IO::Value(env::remove_var(var))
}

fn set_current_dir(dir: &str) -> IO<()> {
env::set_current_dir(dir).into()
}

fn set_var(key: &str, value: &str) -> IO<()> {
IO::Value(env::set_var(key, value))
}

fn split_paths(path: &str) -> IO<Vec<PathBuf>> {
IO::Value(env::split_paths(path).collect())
}

fn temp_dir() -> IO<PathBuf> {
IO::Value(env::temp_dir())
}

fn var(key: &str) -> IO<String> {
env::var(key).into()
}

field_decl! { key, value }

type Entry = record_type! {
key => String,
value => String
};

fn vars() -> IO<Vec<Entry>> {
IO::Value(
env::vars()
.map(|(key, value)| record_no_decl! { key => key, value => value })
.collect(),
)
}

mod std {
pub mod env {
pub use crate::std_lib::env as prim;
}
}

pub fn load(vm: &Thread) -> vm::Result<ExternModule> {
use self::std;

ExternModule::new(
vm,
record! {
consts => record! {
arch => crate::real_std::env::consts::ARCH,
dll_extension => crate::real_std::env::consts::DLL_EXTENSION,
dll_prefix => crate::real_std::env::consts::DLL_PREFIX,
dll_suffix => crate::real_std::env::consts::DLL_SUFFIX,
exe_extension => crate::real_std::env::consts::EXE_EXTENSION,
family => crate::real_std::env::consts::FAMILY,
os => crate::real_std::env::consts::OS,
},
args => primitive!(0, std::env::prim::args),
current_dir => primitive!(0, std::env::prim::current_dir),
current_exe => primitive!(0, std::env::prim::current_exe),
join_paths => primitive!(1, std::env::prim::join_paths),
remove_var => primitive!(1, std::env::prim::remove_var),
set_current_dir => primitive!(1, std::env::prim::set_current_dir),
set_var => primitive!(2, std::env::prim::set_var),
split_paths => primitive!(1, std::env::prim::split_paths),
temp_dir => primitive!(0, std::env::prim::temp_dir),
var => primitive!(1, std::env::prim::var),
vars => primitive!(0, std::env::prim::vars),
},
)
}
2 changes: 1 addition & 1 deletion src/http.rs → src/std_lib/http.rs
Expand Up @@ -406,7 +406,7 @@ macro_rules! uri_binds {

mod std {
pub(crate) mod http {
pub(crate) use crate::http as prim;
pub(crate) use crate::std_lib::http as prim;
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/io.rs → src/std_lib/io.rs
Expand Up @@ -20,9 +20,7 @@ use crate::vm::thread::{RootedThread, Thread, ThreadInternal};
use crate::vm::types::*;
use crate::vm::{self, ExternModule, Result};

use crate::compiler_pipeline::*;

use super::{Compiler, Error};
use crate::{compiler_pipeline::*, Compiler, Error};

fn print(s: &str) -> IO<()> {
print!("{}", s);
Expand Down Expand Up @@ -334,7 +332,7 @@ fn load_script(

mod std {
pub mod io {
pub use crate::io as prim;
pub use crate::std_lib::io as prim;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/process.rs → src/std_lib/process.rs
Expand Up @@ -32,7 +32,7 @@ fn execute(create: CreateProcess) -> IO<Option<i32>> {

mod std {
pub mod process {
pub use crate::process as prim;
pub use crate::std_lib::process as prim;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/rand_bind.rs → src/std_lib/random.rs
Expand Up @@ -54,7 +54,7 @@ fn xor_shift_next(gen: &XorShiftRng) -> RngNext<XorShiftRng> {

mod std {
pub mod random {
pub use crate::rand_bind as prim;
pub use crate::std_lib::random as prim;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/regex_bind.rs → src/std_lib/regex.rs
Expand Up @@ -67,7 +67,7 @@ fn error_to_string(err: &Error) -> &str {

mod std {
pub mod regex {
pub use crate::regex_bind as prim;
pub use crate::std_lib::regex as prim;
}
}

Expand Down
6 changes: 6 additions & 0 deletions std/env.glu
@@ -0,0 +1,6 @@
//! Inspection and manipulation of the process's environment.

{
..
import! std.env.prim
}

0 comments on commit b7dd3d6

Please sign in to comment.