Merge pull request #1967 from rujialiu/optional-features

Allow users to disable optional features
This commit is contained in:
Mark Thom
2023-08-21 11:42:03 -06:00
committed by GitHub
12 changed files with 292 additions and 28 deletions

20
Cargo.lock generated
View File

@@ -742,8 +742,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427" checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"js-sys",
"libc", "libc",
"wasi 0.11.0+wasi-snapshot-preview1", "wasi 0.11.0+wasi-snapshot-preview1",
"wasm-bindgen",
] ]
[[package]] [[package]]
@@ -1811,6 +1813,22 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "ring-wasi"
version = "0.16.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db1418b2535ed5e71a9fc73d3fede8596792fd7cb4b4a0f8ecf412cfddaaedd4"
dependencies = [
"cc",
"getrandom",
"libc",
"once_cell",
"spin",
"untrusted",
"web-sys",
"winapi",
]
[[package]] [[package]]
name = "ripemd160" name = "ripemd160"
version = "0.8.0" version = "0.8.0"
@@ -1944,6 +1962,7 @@ dependencies = [
"divrem", "divrem",
"futures", "futures",
"fxhash", "fxhash",
"getrandom",
"git-version", "git-version",
"hostname", "hostname",
"http-body-util", "http-body-util",
@@ -1965,6 +1984,7 @@ dependencies = [
"ref_thread_local", "ref_thread_local",
"reqwest", "reqwest",
"ring", "ring",
"ring-wasi",
"ripemd160", "ripemd160",
"roxmltree", "roxmltree",
"rustyline", "rustyline",

View File

@@ -13,6 +13,12 @@ build = "build/main.rs"
rust-version = "1.63" rust-version = "1.63"
[features] [features]
default = ["ffi", "repl", "hostname", "tls", "http"]
ffi = ["dep:libffi"]
repl = ["dep:crossterm", "dep:ctrlc", "dep:rustyline"]
hostname = ["dep:hostname"]
tls = ["dep:native-tls"]
http = ["dep:hyper", "dep:reqwest"]
[build-dependencies] [build-dependencies]
indexmap = "1.0.2" indexmap = "1.0.2"
@@ -29,28 +35,22 @@ walkdir = "2"
bit-set = "0.5.3" bit-set = "0.5.3"
bitvec = "1" bitvec = "1"
cpu-time = "1.0.0" cpu-time = "1.0.0"
crossterm = "0.20.0"
dirs-next = "2.0.0" dirs-next = "2.0.0"
divrem = "0.1.0" divrem = "0.1.0"
fxhash = "0.2.1" fxhash = "0.2.1"
git-version = "0.3.4" git-version = "0.3.4"
hostname = "0.3.1"
indexmap = "1.0.2" indexmap = "1.0.2"
lazy_static = "1.4.0" lazy_static = "1.4.0"
lexical = "5.2.2" lexical = "5.2.2"
libc = "0.2.62" libc = "0.2.62"
modular-bitfield = "0.11.2" modular-bitfield = "0.11.2"
ctrlc = "3.2.2"
ordered-float = "2.6.0" ordered-float = "2.6.0"
phf = { version = "0.9", features = ["macros"] } phf = { version = "0.9", features = ["macros"] }
ref_thread_local = "0.0.0" ref_thread_local = "0.0.0"
rustyline = "12.0.0"
ring = "0.16.13"
ripemd160 = "0.8.0" ripemd160 = "0.8.0"
sha3 = "0.8.2" sha3 = "0.8.2"
blake2 = "0.8.1" blake2 = "0.8.1"
crrl = "0.6.0" crrl = "0.6.0"
native-tls = "0.2.4"
chrono = "0.4.11" chrono = "0.4.11"
select = "0.6.0" select = "0.6.0"
roxmltree = "0.11.0" roxmltree = "0.11.0"
@@ -58,18 +58,35 @@ base64 = "0.12.3"
smallvec = "1.8.0" smallvec = "1.8.0"
static_assertions = "1.1.0" static_assertions = "1.1.0"
ryu = "1.0.9" ryu = "1.0.9"
hyper = { version = "1.0.0-rc.3", features = ["full"] }
tokio = { version = "1.28.2", features = ["full"] }
futures = "0.3" futures = "0.3"
libloading = "0.7" libloading = "0.7"
derive_deref = "1.1.1" derive_deref = "1.1.1"
http-body-util = "0.1.0-rc.2" http-body-util = "0.1.0-rc.2"
bytes = "1" bytes = "1"
reqwest = { version = "0.11.18", features = ["blocking"] }
dashu = { git = "https://github.com/coasys/dashu.git" } dashu = { git = "https://github.com/coasys/dashu.git" }
libffi = { git = "https://github.com/coasys/libffi-rs.git", branch = "windows-space" }
rand = "0.8.5" rand = "0.8.5"
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
libffi = { git = "https://github.com/coasys/libffi-rs.git", branch = "windows-space", optional = true }
hostname = { version = "0.3.1", optional = true }
crossterm = { version = "0.20.0", optional = true }
ctrlc = { version = "3.2.2", optional = true }
rustyline = { version = "12.0.0", optional = true }
native-tls = { version = "0.2.4", optional = true }
hyper = { version = "=1.0.0-rc.3", features = ["full"], optional = true }
reqwest = { version = "0.11.18", features = ["blocking"], optional = true }
tokio = { version = "1.28.2", features = ["full"] }
[target.'cfg(target_arch = "wasm32")'.dependencies]
getrandom = { version = "0.2.10", features = ["js"] }
tokio = { version = "1.28.2", features = ["sync", "macros", "io-util", "rt", "time"] }
[target.'cfg(target_os = "wasi")'.dependencies]
ring-wasi = { version = "0.16.25" }
[target.'cfg(not(target_os = "wasi"))'.dependencies]
ring = { version = "0.16.13" }
[dev-dependencies] [dev-dependencies]
assert_cmd = "1.0.3" assert_cmd = "1.0.3"
predicates-core = "1.0.2" predicates-core = "1.0.2"

View File

@@ -1,3 +1,4 @@
#[cfg(feature = "http")]
use crate::http::{HttpListener, HttpResponse}; use crate::http::{HttpListener, HttpResponse};
use crate::machine::loader::LiveLoadState; use crate::machine::loader::LiveLoadState;
use crate::machine::machine_indices::*; use crate::machine::machine_indices::*;
@@ -566,6 +567,7 @@ impl ArenaAllocated for TcpListener {
} }
} }
#[cfg(feature = "http")]
impl ArenaAllocated for HttpListener { impl ArenaAllocated for HttpListener {
type PtrToAllocated = TypedArenaPtr<HttpListener>; type PtrToAllocated = TypedArenaPtr<HttpListener>;
@@ -588,6 +590,7 @@ impl ArenaAllocated for HttpListener {
} }
} }
#[cfg(feature = "http")]
impl ArenaAllocated for HttpResponse { impl ArenaAllocated for HttpResponse {
type PtrToAllocated = TypedArenaPtr<HttpResponse>; type PtrToAllocated = TypedArenaPtr<HttpResponse>;
@@ -695,12 +698,15 @@ unsafe fn drop_slab_in_place(value: &mut AllocSlab) {
ptr::drop_in_place(value.payload_offset::<StreamLayout<CharReader<NamedTcpStream>>>()); ptr::drop_in_place(value.payload_offset::<StreamLayout<CharReader<NamedTcpStream>>>());
} }
ArenaHeaderTag::NamedTlsStream => { ArenaHeaderTag::NamedTlsStream => {
#[cfg(feature = "tls")]
ptr::drop_in_place(value.payload_offset::<StreamLayout<CharReader<NamedTlsStream>>>()); ptr::drop_in_place(value.payload_offset::<StreamLayout<CharReader<NamedTlsStream>>>());
} }
ArenaHeaderTag::HttpReadStream => { ArenaHeaderTag::HttpReadStream => {
#[cfg(feature = "http")]
ptr::drop_in_place(value.payload_offset::<StreamLayout<CharReader<HttpReadStream>>>()); ptr::drop_in_place(value.payload_offset::<StreamLayout<CharReader<HttpReadStream>>>());
} }
ArenaHeaderTag::HttpWriteStream => { ArenaHeaderTag::HttpWriteStream => {
#[cfg(feature = "http")]
ptr::drop_in_place(value.payload_offset::<StreamLayout<CharReader<HttpWriteStream>>>()); ptr::drop_in_place(value.payload_offset::<StreamLayout<CharReader<HttpWriteStream>>>());
} }
ArenaHeaderTag::ReadlineStream => { ArenaHeaderTag::ReadlineStream => {
@@ -724,9 +730,11 @@ unsafe fn drop_slab_in_place(value: &mut AllocSlab) {
ptr::drop_in_place(value.payload_offset::<TcpListener>()); ptr::drop_in_place(value.payload_offset::<TcpListener>());
} }
ArenaHeaderTag::HttpListener => { ArenaHeaderTag::HttpListener => {
#[cfg(feature = "http")]
ptr::drop_in_place(value.payload_offset::<HttpListener>()); ptr::drop_in_place(value.payload_offset::<HttpListener>());
} }
ArenaHeaderTag::HttpResponse => { ArenaHeaderTag::HttpResponse => {
#[cfg(feature = "http")]
ptr::drop_in_place(value.payload_offset::<HttpResponse>()); ptr::drop_in_place(value.payload_offset::<HttpResponse>());
} }
ArenaHeaderTag::StandardOutputStream => { ArenaHeaderTag::StandardOutputStream => {

View File

@@ -2,6 +2,7 @@ fn main() -> std::process::ExitCode {
use std::sync::atomic::Ordering; use std::sync::atomic::Ordering;
use scryer_prolog::*; use scryer_prolog::*;
#[cfg(feature = "repl")]
ctrlc::set_handler(move || { ctrlc::set_handler(move || {
scryer_prolog::machine::INTERRUPT.store(true, Ordering::Relaxed); scryer_prolog::machine::INTERRUPT.store(true, Ordering::Relaxed);
}).unwrap(); }).unwrap();

View File

@@ -15,11 +15,13 @@ mod allocator;
mod arithmetic; mod arithmetic;
pub mod codegen; pub mod codegen;
mod debray_allocator; mod debray_allocator;
#[cfg(feature = "ffi")]
mod ffi; mod ffi;
mod variable_records; mod variable_records;
mod forms; mod forms;
mod heap_iter; mod heap_iter;
pub mod heap_print; pub mod heap_print;
#[cfg(feature = "http")]
mod http; mod http;
mod indexing; mod indexing;
#[macro_use] #[macro_use]
@@ -30,6 +32,7 @@ mod iterators;
pub mod machine; pub mod machine;
mod raw_block; mod raw_block;
pub mod read; pub mod read;
#[cfg(feature = "repl")]
mod repl_helper; mod repl_helper;
mod targets; mod targets;
pub mod types; pub mod types;

View File

@@ -4242,58 +4242,72 @@ impl Machine {
step_or_fail!(self, self.machine_st.p = self.machine_st.cp); step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
} }
&Instruction::CallHttpOpen => { &Instruction::CallHttpOpen => {
#[cfg(feature = "http")]
try_or_throw!(self.machine_st, self.http_open()); try_or_throw!(self.machine_st, self.http_open());
step_or_fail!(self, self.machine_st.p += 1); step_or_fail!(self, self.machine_st.p += 1);
} }
&Instruction::ExecuteHttpOpen => { &Instruction::ExecuteHttpOpen => {
#[cfg(feature = "http")]
try_or_throw!(self.machine_st, self.http_open()); try_or_throw!(self.machine_st, self.http_open());
step_or_fail!(self, self.machine_st.p = self.machine_st.cp); step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
} }
&Instruction::CallHttpListen => { &Instruction::CallHttpListen => {
#[cfg(feature = "http")]
try_or_throw!(self.machine_st, self.http_listen()); try_or_throw!(self.machine_st, self.http_listen());
step_or_fail!(self, self.machine_st.p += 1); step_or_fail!(self, self.machine_st.p += 1);
} }
&Instruction::ExecuteHttpListen => { &Instruction::ExecuteHttpListen => {
#[cfg(feature = "http")]
try_or_throw!(self.machine_st, self.http_listen()); try_or_throw!(self.machine_st, self.http_listen());
step_or_fail!(self, self.machine_st.p = self.machine_st.cp); step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
} }
&Instruction::CallHttpAccept => { &Instruction::CallHttpAccept => {
#[cfg(feature = "http")]
try_or_throw!(self.machine_st, self.http_accept()); try_or_throw!(self.machine_st, self.http_accept());
step_or_fail!(self, self.machine_st.p += 1); step_or_fail!(self, self.machine_st.p += 1);
} }
&Instruction::ExecuteHttpAccept => { &Instruction::ExecuteHttpAccept => {
#[cfg(feature = "http")]
try_or_throw!(self.machine_st, self.http_accept()); try_or_throw!(self.machine_st, self.http_accept());
step_or_fail!(self, self.machine_st.p = self.machine_st.cp); step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
} }
&Instruction::CallHttpAnswer => { &Instruction::CallHttpAnswer => {
#[cfg(feature = "http")]
try_or_throw!(self.machine_st, self.http_answer()); try_or_throw!(self.machine_st, self.http_answer());
step_or_fail!(self, self.machine_st.p += 1); step_or_fail!(self, self.machine_st.p += 1);
} }
&Instruction::ExecuteHttpAnswer => { &Instruction::ExecuteHttpAnswer => {
#[cfg(feature = "http")]
try_or_throw!(self.machine_st, self.http_answer()); try_or_throw!(self.machine_st, self.http_answer());
step_or_fail!(self, self.machine_st.p = self.machine_st.cp); step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
} }
&Instruction::CallLoadForeignLib => { &Instruction::CallLoadForeignLib => {
#[cfg(feature = "ffi")]
try_or_throw!(self.machine_st, self.load_foreign_lib()); try_or_throw!(self.machine_st, self.load_foreign_lib());
step_or_fail!(self, self.machine_st.p += 1); step_or_fail!(self, self.machine_st.p += 1);
} }
&Instruction::ExecuteLoadForeignLib => { &Instruction::ExecuteLoadForeignLib => {
#[cfg(feature = "ffi")]
try_or_throw!(self.machine_st, self.load_foreign_lib()); try_or_throw!(self.machine_st, self.load_foreign_lib());
step_or_fail!(self, self.machine_st.p = self.machine_st.cp); step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
} }
&Instruction::CallForeignCall => { &Instruction::CallForeignCall => {
#[cfg(feature = "ffi")]
try_or_throw!(self.machine_st, self.foreign_call()); try_or_throw!(self.machine_st, self.foreign_call());
step_or_fail!(self, self.machine_st.p += 1); step_or_fail!(self, self.machine_st.p += 1);
} }
&Instruction::ExecuteForeignCall => { &Instruction::ExecuteForeignCall => {
#[cfg(feature = "ffi")]
try_or_throw!(self.machine_st, self.foreign_call()); try_or_throw!(self.machine_st, self.foreign_call());
step_or_fail!(self, self.machine_st.p = self.machine_st.cp); step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
} }
&Instruction::CallDefineForeignStruct => { &Instruction::CallDefineForeignStruct => {
#[cfg(feature = "ffi")]
try_or_throw!(self.machine_st, self.define_foreign_struct()); try_or_throw!(self.machine_st, self.define_foreign_struct());
step_or_fail!(self, self.machine_st.p += 1); step_or_fail!(self, self.machine_st.p += 1);
} }
&Instruction::ExecuteDefineForeignStruct => { &Instruction::ExecuteDefineForeignStruct => {
#[cfg(feature = "ffi")]
try_or_throw!(self.machine_st, self.define_foreign_struct()); try_or_throw!(self.machine_st, self.define_foreign_struct());
step_or_fail!(self, self.machine_st.p = self.machine_st.cp); step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
} }
@@ -4462,18 +4476,22 @@ impl Machine {
self.machine_st.p = self.machine_st.cp; self.machine_st.p = self.machine_st.cp;
} }
&Instruction::CallTLSAcceptClient => { &Instruction::CallTLSAcceptClient => {
#[cfg(feature = "tls")]
try_or_throw!(self.machine_st, self.tls_accept_client()); try_or_throw!(self.machine_st, self.tls_accept_client());
step_or_fail!(self, self.machine_st.p += 1); step_or_fail!(self, self.machine_st.p += 1);
} }
&Instruction::ExecuteTLSAcceptClient => { &Instruction::ExecuteTLSAcceptClient => {
#[cfg(feature = "tls")]
try_or_throw!(self.machine_st, self.tls_accept_client()); try_or_throw!(self.machine_st, self.tls_accept_client());
step_or_fail!(self, self.machine_st.p = self.machine_st.cp); step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
} }
&Instruction::CallTLSClientConnect => { &Instruction::CallTLSClientConnect => {
#[cfg(feature = "tls")]
try_or_throw!(self.machine_st, self.tls_client_connect()); try_or_throw!(self.machine_st, self.tls_client_connect());
step_or_fail!(self, self.machine_st.p += 1); step_or_fail!(self, self.machine_st.p += 1);
} }
&Instruction::ExecuteTLSClientConnect => { &Instruction::ExecuteTLSClientConnect => {
#[cfg(feature = "tls")]
try_or_throw!(self.machine_st, self.tls_client_connect()); try_or_throw!(self.machine_st, self.tls_client_connect());
step_or_fail!(self, self.machine_st.p = self.machine_st.cp); step_or_fail!(self, self.machine_st.p = self.machine_st.cp);
} }

View File

@@ -2,6 +2,7 @@ use crate::arena::*;
use crate::atom_table::*; use crate::atom_table::*;
use crate::parser::ast::*; use crate::parser::ast::*;
#[cfg(feature = "ffi")]
use crate::ffi::FFIError; use crate::ffi::FFIError;
use crate::forms::*; use crate::forms::*;
use crate::machine::heap::*; use crate::machine::heap::*;
@@ -538,6 +539,7 @@ impl MachineState {
} }
} }
#[cfg(feature = "ffi")]
pub(super) fn ffi_error(&mut self, err: FFIError) -> MachineError { pub(super) fn ffi_error(&mut self, err: FFIError) -> MachineError {
let error_atom = match err { let error_atom = match err {
FFIError::ValueCast => atom!("value_cast"), FFIError::ValueCast => atom!("value_cast"),

View File

@@ -244,6 +244,7 @@ impl Machine {
user_error, user_error,
load_contexts: vec![], load_contexts: vec![],
runtime, runtime,
#[cfg(feature = "ffi")]
foreign_function_table: Default::default(), foreign_function_table: Default::default(),
}; };

View File

@@ -28,6 +28,7 @@ use crate::arena::*;
use crate::arithmetic::*; use crate::arithmetic::*;
use crate::atom_table::*; use crate::atom_table::*;
use crate::forms::*; use crate::forms::*;
#[cfg(feature = "ffi")]
use crate::ffi::ForeignFunctionTable; use crate::ffi::ForeignFunctionTable;
use crate::instructions::*; use crate::instructions::*;
use crate::machine::args::*; use crate::machine::args::*;
@@ -68,6 +69,7 @@ pub struct Machine {
pub(super) user_error: Stream, pub(super) user_error: Stream,
pub(super) load_contexts: Vec<LoadContext>, pub(super) load_contexts: Vec<LoadContext>,
pub(super) runtime: Runtime, pub(super) runtime: Runtime,
#[cfg(feature = "ffi")]
pub(super) foreign_function_table: ForeignFunctionTable, pub(super) foreign_function_table: ForeignFunctionTable,
} }
@@ -425,8 +427,14 @@ impl Machine {
let user_output = Stream::stdout(&mut machine_st.arena); let user_output = Stream::stdout(&mut machine_st.arena);
let user_error = Stream::stderr(&mut machine_st.arena); let user_error = Stream::stderr(&mut machine_st.arena);
#[cfg(not(target_os = "wasi"))]
let runtime = tokio::runtime::Runtime::new() let runtime = tokio::runtime::Runtime::new()
.unwrap(); .unwrap();
#[cfg(target_os = "wasi")]
let runtime = tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.unwrap();
let mut wam = Machine { let mut wam = Machine {
machine_st, machine_st,
@@ -437,6 +445,7 @@ impl Machine {
user_error, user_error,
load_contexts: vec![], load_contexts: vec![],
runtime, runtime,
#[cfg(feature = "ffi")]
foreign_function_table: Default::default(), foreign_function_table: Default::default(),
}; };

View File

@@ -9,6 +9,7 @@ use crate::machine::machine_errors::*;
use crate::machine::machine_indices::*; use crate::machine::machine_indices::*;
use crate::machine::machine_state::*; use crate::machine::machine_state::*;
use crate::types::*; use crate::types::*;
#[cfg(feature = "http")]
use crate::http::HttpResponse; use crate::http::HttpResponse;
pub use modular_bitfield::prelude::*; pub use modular_bitfield::prelude::*;
@@ -26,6 +27,7 @@ use std::net::{TcpStream, Shutdown};
use std::ops::{Deref, DerefMut}; use std::ops::{Deref, DerefMut};
use std::ptr; use std::ptr;
#[cfg(feature = "tls")]
use native_tls::TlsStream; use native_tls::TlsStream;
#[derive(Debug, BitfieldSpecifier, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, BitfieldSpecifier, Clone, Copy, PartialEq, Eq, Hash)]
@@ -232,12 +234,14 @@ impl Write for NamedTcpStream {
} }
} }
#[cfg(feature = "tls")]
#[derive(Debug)] #[derive(Debug)]
pub struct NamedTlsStream { pub struct NamedTlsStream {
address: Atom, address: Atom,
tls_stream: TlsStream<Stream>, tls_stream: TlsStream<Stream>,
} }
#[cfg(feature = "tls")]
impl Read for NamedTlsStream { impl Read for NamedTlsStream {
#[inline] #[inline]
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
@@ -245,6 +249,7 @@ impl Read for NamedTlsStream {
} }
} }
#[cfg(feature = "tls")]
impl Write for NamedTlsStream { impl Write for NamedTlsStream {
#[inline] #[inline]
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
@@ -257,17 +262,20 @@ impl Write for NamedTlsStream {
} }
} }
#[cfg(feature = "http")]
pub struct HttpReadStream { pub struct HttpReadStream {
url: Atom, url: Atom,
body_reader: Box<dyn BufRead>, body_reader: Box<dyn BufRead>,
} }
#[cfg(feature = "http")]
impl Debug for HttpReadStream { impl Debug for HttpReadStream {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Http Read Stream [{}]", self.url.as_str()) write!(f, "Http Read Stream [{}]", self.url.as_str())
} }
} }
#[cfg(feature = "http")]
impl Read for HttpReadStream { impl Read for HttpReadStream {
#[inline] #[inline]
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
@@ -275,6 +283,7 @@ impl Read for HttpReadStream {
} }
} }
#[cfg(feature = "http")]
pub struct HttpWriteStream { pub struct HttpWriteStream {
status_code: u16, status_code: u16,
headers: hyper::HeaderMap, headers: hyper::HeaderMap,
@@ -282,12 +291,14 @@ pub struct HttpWriteStream {
buffer: Vec<u8>, buffer: Vec<u8>,
} }
#[cfg(feature = "http")]
impl Debug for HttpWriteStream { impl Debug for HttpWriteStream {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Http Write Stream") write!(f, "Http Write Stream")
} }
} }
#[cfg(feature = "http")]
impl Write for HttpWriteStream { impl Write for HttpWriteStream {
#[inline] #[inline]
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> { fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
@@ -453,8 +464,11 @@ arena_allocated_impl_for_stream!(CharReader<ByteStream>, ByteStream);
arena_allocated_impl_for_stream!(CharReader<InputFileStream>, InputFileStream); arena_allocated_impl_for_stream!(CharReader<InputFileStream>, InputFileStream);
arena_allocated_impl_for_stream!(OutputFileStream, OutputFileStream); arena_allocated_impl_for_stream!(OutputFileStream, OutputFileStream);
arena_allocated_impl_for_stream!(CharReader<NamedTcpStream>, NamedTcpStream); arena_allocated_impl_for_stream!(CharReader<NamedTcpStream>, NamedTcpStream);
#[cfg(feature = "tls")]
arena_allocated_impl_for_stream!(CharReader<NamedTlsStream>, NamedTlsStream); arena_allocated_impl_for_stream!(CharReader<NamedTlsStream>, NamedTlsStream);
#[cfg(feature = "http")]
arena_allocated_impl_for_stream!(CharReader<HttpReadStream>, HttpReadStream); arena_allocated_impl_for_stream!(CharReader<HttpReadStream>, HttpReadStream);
#[cfg(feature = "http")]
arena_allocated_impl_for_stream!(CharReader<HttpWriteStream>, HttpWriteStream); arena_allocated_impl_for_stream!(CharReader<HttpWriteStream>, HttpWriteStream);
arena_allocated_impl_for_stream!(ReadlineStream, ReadlineStream); arena_allocated_impl_for_stream!(ReadlineStream, ReadlineStream);
arena_allocated_impl_for_stream!(StaticStringStream, StaticStringStream); arena_allocated_impl_for_stream!(StaticStringStream, StaticStringStream);
@@ -468,8 +482,11 @@ pub enum Stream {
OutputFile(TypedArenaPtr<StreamLayout<OutputFileStream>>), OutputFile(TypedArenaPtr<StreamLayout<OutputFileStream>>),
StaticString(TypedArenaPtr<StreamLayout<StaticStringStream>>), StaticString(TypedArenaPtr<StreamLayout<StaticStringStream>>),
NamedTcp(TypedArenaPtr<StreamLayout<CharReader<NamedTcpStream>>>), NamedTcp(TypedArenaPtr<StreamLayout<CharReader<NamedTcpStream>>>),
#[cfg(feature = "tls")]
NamedTls(TypedArenaPtr<StreamLayout<CharReader<NamedTlsStream>>>), NamedTls(TypedArenaPtr<StreamLayout<CharReader<NamedTlsStream>>>),
#[cfg(feature = "http")]
HttpRead(TypedArenaPtr<StreamLayout<CharReader<HttpReadStream>>>), HttpRead(TypedArenaPtr<StreamLayout<CharReader<HttpReadStream>>>),
#[cfg(feature = "http")]
HttpWrite(TypedArenaPtr<StreamLayout<CharReader<HttpWriteStream>>>), HttpWrite(TypedArenaPtr<StreamLayout<CharReader<HttpWriteStream>>>),
Null(StreamOptions), Null(StreamOptions),
Readline(TypedArenaPtr<StreamLayout<ReadlineStream>>), Readline(TypedArenaPtr<StreamLayout<ReadlineStream>>),
@@ -524,8 +541,11 @@ impl Stream {
Stream::OutputFile(TypedArenaPtr::new(ptr as *mut _)) Stream::OutputFile(TypedArenaPtr::new(ptr as *mut _))
} }
ArenaHeaderTag::NamedTcpStream => Stream::NamedTcp(TypedArenaPtr::new(ptr as *mut _)), ArenaHeaderTag::NamedTcpStream => Stream::NamedTcp(TypedArenaPtr::new(ptr as *mut _)),
#[cfg(feature = "tls")]
ArenaHeaderTag::NamedTlsStream => Stream::NamedTls(TypedArenaPtr::new(ptr as *mut _)), ArenaHeaderTag::NamedTlsStream => Stream::NamedTls(TypedArenaPtr::new(ptr as *mut _)),
#[cfg(feature = "http")]
ArenaHeaderTag::HttpReadStream => Stream::HttpRead(TypedArenaPtr::new(ptr as *mut _)), ArenaHeaderTag::HttpReadStream => Stream::HttpRead(TypedArenaPtr::new(ptr as *mut _)),
#[cfg(feature = "http")]
ArenaHeaderTag::HttpWriteStream => Stream::HttpWrite(TypedArenaPtr::new(ptr as *mut _)), ArenaHeaderTag::HttpWriteStream => Stream::HttpWrite(TypedArenaPtr::new(ptr as *mut _)),
ArenaHeaderTag::ReadlineStream => Stream::Readline(TypedArenaPtr::new(ptr as *mut _)), ArenaHeaderTag::ReadlineStream => Stream::Readline(TypedArenaPtr::new(ptr as *mut _)),
ArenaHeaderTag::StaticStringStream => { ArenaHeaderTag::StaticStringStream => {
@@ -578,8 +598,11 @@ impl Stream {
Stream::OutputFile(ptr) => ptr.header_ptr(), Stream::OutputFile(ptr) => ptr.header_ptr(),
Stream::StaticString(ptr) => ptr.header_ptr(), Stream::StaticString(ptr) => ptr.header_ptr(),
Stream::NamedTcp(ptr) => ptr.header_ptr(), Stream::NamedTcp(ptr) => ptr.header_ptr(),
#[cfg(feature = "tls")]
Stream::NamedTls(ptr) => ptr.header_ptr(), Stream::NamedTls(ptr) => ptr.header_ptr(),
#[cfg(feature = "http")]
Stream::HttpRead(ptr) => ptr.header_ptr(), Stream::HttpRead(ptr) => ptr.header_ptr(),
#[cfg(feature = "http")]
Stream::HttpWrite(ptr) => ptr.header_ptr(), Stream::HttpWrite(ptr) => ptr.header_ptr(),
Stream::Null(_) => ptr::null(), Stream::Null(_) => ptr::null(),
Stream::Readline(ptr) => ptr.header_ptr(), Stream::Readline(ptr) => ptr.header_ptr(),
@@ -595,8 +618,11 @@ impl Stream {
Stream::OutputFile(ref ptr) => &ptr.options, Stream::OutputFile(ref ptr) => &ptr.options,
Stream::StaticString(ref ptr) => &ptr.options, Stream::StaticString(ref ptr) => &ptr.options,
Stream::NamedTcp(ref ptr) => &ptr.options, Stream::NamedTcp(ref ptr) => &ptr.options,
#[cfg(feature = "tls")]
Stream::NamedTls(ref ptr) => &ptr.options, Stream::NamedTls(ref ptr) => &ptr.options,
#[cfg(feature = "http")]
Stream::HttpRead(ref ptr) => &ptr.options, Stream::HttpRead(ref ptr) => &ptr.options,
#[cfg(feature = "http")]
Stream::HttpWrite(ref ptr) => &ptr.options, Stream::HttpWrite(ref ptr) => &ptr.options,
Stream::Null(ref options) => options, Stream::Null(ref options) => options,
Stream::Readline(ref ptr) => &ptr.options, Stream::Readline(ref ptr) => &ptr.options,
@@ -612,8 +638,11 @@ impl Stream {
Stream::OutputFile(ref mut ptr) => &mut ptr.options, Stream::OutputFile(ref mut ptr) => &mut ptr.options,
Stream::StaticString(ref mut ptr) => &mut ptr.options, Stream::StaticString(ref mut ptr) => &mut ptr.options,
Stream::NamedTcp(ref mut ptr) => &mut ptr.options, Stream::NamedTcp(ref mut ptr) => &mut ptr.options,
#[cfg(feature = "tls")]
Stream::NamedTls(ref mut ptr) => &mut ptr.options, Stream::NamedTls(ref mut ptr) => &mut ptr.options,
#[cfg(feature = "http")]
Stream::HttpRead(ref mut ptr) => &mut ptr.options, Stream::HttpRead(ref mut ptr) => &mut ptr.options,
#[cfg(feature = "http")]
Stream::HttpWrite(ref mut ptr) => &mut ptr.options, Stream::HttpWrite(ref mut ptr) => &mut ptr.options,
Stream::Null(ref mut options) => options, Stream::Null(ref mut options) => options,
Stream::Readline(ref mut ptr) => &mut ptr.options, Stream::Readline(ref mut ptr) => &mut ptr.options,
@@ -630,8 +659,11 @@ impl Stream {
Stream::OutputFile(ptr) => ptr.lines_read += incr_num_lines_read, Stream::OutputFile(ptr) => ptr.lines_read += incr_num_lines_read,
Stream::StaticString(ptr) => ptr.lines_read += incr_num_lines_read, Stream::StaticString(ptr) => ptr.lines_read += incr_num_lines_read,
Stream::NamedTcp(ptr) => ptr.lines_read += incr_num_lines_read, Stream::NamedTcp(ptr) => ptr.lines_read += incr_num_lines_read,
#[cfg(feature = "tls")]
Stream::NamedTls(ptr) => ptr.lines_read += incr_num_lines_read, Stream::NamedTls(ptr) => ptr.lines_read += incr_num_lines_read,
#[cfg(feature = "http")]
Stream::HttpRead(ptr) => ptr.lines_read += incr_num_lines_read, Stream::HttpRead(ptr) => ptr.lines_read += incr_num_lines_read,
#[cfg(feature = "http")]
Stream::HttpWrite(_) => {} Stream::HttpWrite(_) => {}
Stream::Null(_) => {} Stream::Null(_) => {}
Stream::Readline(ptr) => ptr.lines_read += incr_num_lines_read, Stream::Readline(ptr) => ptr.lines_read += incr_num_lines_read,
@@ -648,8 +680,11 @@ impl Stream {
Stream::OutputFile(ptr) => ptr.lines_read = value, Stream::OutputFile(ptr) => ptr.lines_read = value,
Stream::StaticString(ptr) => ptr.lines_read = value, Stream::StaticString(ptr) => ptr.lines_read = value,
Stream::NamedTcp(ptr) => ptr.lines_read = value, Stream::NamedTcp(ptr) => ptr.lines_read = value,
#[cfg(feature = "tls")]
Stream::NamedTls(ptr) => ptr.lines_read = value, Stream::NamedTls(ptr) => ptr.lines_read = value,
#[cfg(feature = "http")]
Stream::HttpRead(ptr) => ptr.lines_read = value, Stream::HttpRead(ptr) => ptr.lines_read = value,
#[cfg(feature = "http")]
Stream::HttpWrite(_) => {} Stream::HttpWrite(_) => {}
Stream::Null(_) => {} Stream::Null(_) => {}
Stream::Readline(ptr) => ptr.lines_read = value, Stream::Readline(ptr) => ptr.lines_read = value,
@@ -666,8 +701,11 @@ impl Stream {
Stream::OutputFile(ptr) => ptr.lines_read, Stream::OutputFile(ptr) => ptr.lines_read,
Stream::StaticString(ptr) => ptr.lines_read, Stream::StaticString(ptr) => ptr.lines_read,
Stream::NamedTcp(ptr) => ptr.lines_read, Stream::NamedTcp(ptr) => ptr.lines_read,
#[cfg(feature = "tls")]
Stream::NamedTls(ptr) => ptr.lines_read, Stream::NamedTls(ptr) => ptr.lines_read,
#[cfg(feature = "http")]
Stream::HttpRead(ptr) => ptr.lines_read, Stream::HttpRead(ptr) => ptr.lines_read,
#[cfg(feature = "http")]
Stream::HttpWrite(_) => 0, Stream::HttpWrite(_) => 0,
Stream::Null(_) => 0, Stream::Null(_) => 0,
Stream::Readline(ptr) => ptr.lines_read, Stream::Readline(ptr) => ptr.lines_read,
@@ -682,15 +720,21 @@ impl CharRead for Stream {
match self { match self {
Stream::InputFile(file) => (*file).peek_char(), Stream::InputFile(file) => (*file).peek_char(),
Stream::NamedTcp(tcp_stream) => (*tcp_stream).peek_char(), Stream::NamedTcp(tcp_stream) => (*tcp_stream).peek_char(),
#[cfg(feature = "tls")]
Stream::NamedTls(tls_stream) => (*tls_stream).peek_char(), Stream::NamedTls(tls_stream) => (*tls_stream).peek_char(),
#[cfg(feature = "http")]
Stream::HttpRead(http_stream) => (*http_stream).peek_char(), Stream::HttpRead(http_stream) => (*http_stream).peek_char(),
Stream::Readline(rl_stream) => (*rl_stream).peek_char(), Stream::Readline(rl_stream) => (*rl_stream).peek_char(),
Stream::StaticString(src) => (*src).peek_char(), Stream::StaticString(src) => (*src).peek_char(),
Stream::Byte(cursor) => (*cursor).peek_char(), Stream::Byte(cursor) => (*cursor).peek_char(),
#[cfg(feature = "http")]
Stream::HttpWrite(_) => Some(Err(std::io::Error::new(
ErrorKind::PermissionDenied,
StreamError::ReadFromOutputStream,
))),
Stream::OutputFile(_) | Stream::OutputFile(_) |
Stream::StandardError(_) | Stream::StandardError(_) |
Stream::StandardOutput(_) | Stream::StandardOutput(_) |
Stream::HttpWrite(_) |
Stream::Null(_) => Some(Err(std::io::Error::new( Stream::Null(_) => Some(Err(std::io::Error::new(
ErrorKind::PermissionDenied, ErrorKind::PermissionDenied,
StreamError::ReadFromOutputStream, StreamError::ReadFromOutputStream,
@@ -702,15 +746,21 @@ impl CharRead for Stream {
match self { match self {
Stream::InputFile(file) => (*file).read_char(), Stream::InputFile(file) => (*file).read_char(),
Stream::NamedTcp(tcp_stream) => (*tcp_stream).read_char(), Stream::NamedTcp(tcp_stream) => (*tcp_stream).read_char(),
#[cfg(feature = "tls")]
Stream::NamedTls(tls_stream) => (*tls_stream).read_char(), Stream::NamedTls(tls_stream) => (*tls_stream).read_char(),
#[cfg(feature = "http")]
Stream::HttpRead(http_stream) => (*http_stream).read_char(), Stream::HttpRead(http_stream) => (*http_stream).read_char(),
Stream::Readline(rl_stream) => (*rl_stream).read_char(), Stream::Readline(rl_stream) => (*rl_stream).read_char(),
Stream::StaticString(src) => (*src).read_char(), Stream::StaticString(src) => (*src).read_char(),
Stream::Byte(cursor) => (*cursor).read_char(), Stream::Byte(cursor) => (*cursor).read_char(),
#[cfg(feature = "http")]
Stream::HttpWrite(_) => Some(Err(std::io::Error::new(
ErrorKind::PermissionDenied,
StreamError::ReadFromOutputStream,
))),
Stream::OutputFile(_) | Stream::OutputFile(_) |
Stream::StandardError(_) | Stream::StandardError(_) |
Stream::StandardOutput(_) | Stream::StandardOutput(_) |
Stream::HttpWrite(_) |
Stream::Null(_) => Some(Err(std::io::Error::new( Stream::Null(_) => Some(Err(std::io::Error::new(
ErrorKind::PermissionDenied, ErrorKind::PermissionDenied,
StreamError::ReadFromOutputStream, StreamError::ReadFromOutputStream,
@@ -722,15 +772,18 @@ impl CharRead for Stream {
match self { match self {
Stream::InputFile(file) => file.put_back_char(c), Stream::InputFile(file) => file.put_back_char(c),
Stream::NamedTcp(tcp_stream) => tcp_stream.put_back_char(c), Stream::NamedTcp(tcp_stream) => tcp_stream.put_back_char(c),
#[cfg(feature = "tls")]
Stream::NamedTls(tls_stream) => tls_stream.put_back_char(c), Stream::NamedTls(tls_stream) => tls_stream.put_back_char(c),
#[cfg(feature = "http")]
Stream::HttpRead(http_stream) => http_stream.put_back_char(c), Stream::HttpRead(http_stream) => http_stream.put_back_char(c),
Stream::Readline(rl_stream) => rl_stream.put_back_char(c), Stream::Readline(rl_stream) => rl_stream.put_back_char(c),
Stream::StaticString(src) => src.put_back_char(c), Stream::StaticString(src) => src.put_back_char(c),
Stream::Byte(cursor) => cursor.put_back_char(c), Stream::Byte(cursor) => cursor.put_back_char(c),
#[cfg(feature = "http")]
Stream::HttpWrite(_) => {}
Stream::OutputFile(_) | Stream::OutputFile(_) |
Stream::StandardError(_) | Stream::StandardError(_) |
Stream::StandardOutput(_) | Stream::StandardOutput(_) |
Stream::HttpWrite(_) |
Stream::Null(_) => {} Stream::Null(_) => {}
} }
} }
@@ -739,15 +792,18 @@ impl CharRead for Stream {
match self { match self {
Stream::InputFile(ref mut file) => file.consume(nread), Stream::InputFile(ref mut file) => file.consume(nread),
Stream::NamedTcp(ref mut tcp_stream) => tcp_stream.consume(nread), Stream::NamedTcp(ref mut tcp_stream) => tcp_stream.consume(nread),
#[cfg(feature = "tls")]
Stream::NamedTls(ref mut tls_stream) => tls_stream.consume(nread), Stream::NamedTls(ref mut tls_stream) => tls_stream.consume(nread),
#[cfg(feature = "http")]
Stream::HttpRead(ref mut http_stream) => http_stream.consume(nread), Stream::HttpRead(ref mut http_stream) => http_stream.consume(nread),
Stream::Readline(ref mut rl_stream) => rl_stream.consume(nread), Stream::Readline(ref mut rl_stream) => rl_stream.consume(nread),
Stream::StaticString(ref mut src) => src.consume(nread), Stream::StaticString(ref mut src) => src.consume(nread),
Stream::Byte(ref mut cursor) => cursor.consume(nread), Stream::Byte(ref mut cursor) => cursor.consume(nread),
#[cfg(feature = "http")]
Stream::HttpWrite(_) => {}
Stream::OutputFile(_) | Stream::OutputFile(_) |
Stream::StandardError(_) | Stream::StandardError(_) |
Stream::StandardOutput(_) | Stream::StandardOutput(_) |
Stream::HttpWrite(_) |
Stream::Null(_) => {} Stream::Null(_) => {}
} }
} }
@@ -759,15 +815,21 @@ impl Read for Stream {
let bytes_read = match self { let bytes_read = match self {
Stream::InputFile(file) => (*file).read(buf), Stream::InputFile(file) => (*file).read(buf),
Stream::NamedTcp(tcp_stream) => (*tcp_stream).read(buf), Stream::NamedTcp(tcp_stream) => (*tcp_stream).read(buf),
#[cfg(feature = "tls")]
Stream::NamedTls(tls_stream) => (*tls_stream).read(buf), Stream::NamedTls(tls_stream) => (*tls_stream).read(buf),
#[cfg(feature = "http")]
Stream::HttpRead(http_stream) => (*http_stream).read(buf), Stream::HttpRead(http_stream) => (*http_stream).read(buf),
Stream::Readline(rl_stream) => (*rl_stream).read(buf), Stream::Readline(rl_stream) => (*rl_stream).read(buf),
Stream::StaticString(src) => (*src).read(buf), Stream::StaticString(src) => (*src).read(buf),
Stream::Byte(cursor) => (*cursor).read(buf), Stream::Byte(cursor) => (*cursor).read(buf),
#[cfg(feature = "http")]
Stream::HttpWrite(_) => Err(std::io::Error::new(
ErrorKind::PermissionDenied,
StreamError::ReadFromOutputStream,
)),
Stream::OutputFile(_) Stream::OutputFile(_)
| Stream::StandardError(_) | Stream::StandardError(_)
| Stream::StandardOutput(_) | Stream::StandardOutput(_)
| Stream::HttpWrite(_)
| Stream::Null(_) => Err(std::io::Error::new( | Stream::Null(_) => Err(std::io::Error::new(
ErrorKind::PermissionDenied, ErrorKind::PermissionDenied,
StreamError::ReadFromOutputStream, StreamError::ReadFromOutputStream,
@@ -783,12 +845,18 @@ impl Write for Stream {
match self { match self {
Stream::OutputFile(ref mut file) => file.write(buf), Stream::OutputFile(ref mut file) => file.write(buf),
Stream::NamedTcp(ref mut tcp_stream) => tcp_stream.get_mut().write(buf), Stream::NamedTcp(ref mut tcp_stream) => tcp_stream.get_mut().write(buf),
#[cfg(feature = "tls")]
Stream::NamedTls(ref mut tls_stream) => tls_stream.get_mut().write(buf), Stream::NamedTls(ref mut tls_stream) => tls_stream.get_mut().write(buf),
Stream::Byte(ref mut cursor) => cursor.get_mut().write(buf), Stream::Byte(ref mut cursor) => cursor.get_mut().write(buf),
Stream::StandardOutput(stream) => stream.write(buf), Stream::StandardOutput(stream) => stream.write(buf),
Stream::StandardError(stream) => stream.write(buf), Stream::StandardError(stream) => stream.write(buf),
#[cfg(feature = "http")]
Stream::HttpWrite(ref mut stream) => stream.get_mut().write(buf), Stream::HttpWrite(ref mut stream) => stream.get_mut().write(buf),
Stream::HttpRead(_) | #[cfg(feature = "http")]
Stream::HttpRead(_) => Err(std::io::Error::new(
ErrorKind::PermissionDenied,
StreamError::WriteToInputStream,
)),
Stream::StaticString(_) | Stream::StaticString(_) |
Stream::Readline(_) | Stream::Readline(_) |
Stream::InputFile(..) | Stream::InputFile(..) |
@@ -803,12 +871,18 @@ impl Write for Stream {
match self { match self {
Stream::OutputFile(ref mut file) => file.stream.flush(), Stream::OutputFile(ref mut file) => file.stream.flush(),
Stream::NamedTcp(ref mut tcp_stream) => tcp_stream.stream.get_mut().flush(), Stream::NamedTcp(ref mut tcp_stream) => tcp_stream.stream.get_mut().flush(),
#[cfg(feature = "tls")]
Stream::NamedTls(ref mut tls_stream) => tls_stream.stream.get_mut().flush(), Stream::NamedTls(ref mut tls_stream) => tls_stream.stream.get_mut().flush(),
Stream::Byte(ref mut cursor) => cursor.stream.get_mut().flush(), Stream::Byte(ref mut cursor) => cursor.stream.get_mut().flush(),
Stream::StandardError(stream) => stream.stream.flush(), Stream::StandardError(stream) => stream.stream.flush(),
Stream::StandardOutput(stream) => stream.stream.flush(), Stream::StandardOutput(stream) => stream.stream.flush(),
#[cfg(feature = "http")]
Stream::HttpWrite(ref mut stream) => stream.stream.get_mut().flush(), Stream::HttpWrite(ref mut stream) => stream.stream.get_mut().flush(),
Stream::HttpRead(_) | #[cfg(feature = "http")]
Stream::HttpRead(_) => Err(std::io::Error::new(
ErrorKind::PermissionDenied,
StreamError::FlushToInputStream,
)),
Stream::StaticString(_) | Stream::StaticString(_) |
Stream::Readline(_) | Stream::Readline(_) |
Stream::InputFile(_) | Stream::InputFile(_) |
@@ -913,7 +987,12 @@ impl Stream {
Stream::InputFile(file_stream) => { Stream::InputFile(file_stream) => {
file_stream.position() file_stream.position()
} }
Stream::NamedTcp(..) | Stream::NamedTls(..) | Stream::Readline(..) => { #[cfg(feature = "tls")]
Stream::NamedTls(..) => {
Some(0)
}
Stream::NamedTcp(..)
| Stream::Readline(..) => {
Some(0) Some(0)
} }
_ => None, _ => None,
@@ -951,8 +1030,11 @@ impl Stream {
Stream::OutputFile(stream) => stream.past_end_of_stream, Stream::OutputFile(stream) => stream.past_end_of_stream,
Stream::StaticString(stream) => stream.past_end_of_stream, Stream::StaticString(stream) => stream.past_end_of_stream,
Stream::NamedTcp(stream) => stream.past_end_of_stream, Stream::NamedTcp(stream) => stream.past_end_of_stream,
#[cfg(feature = "tls")]
Stream::NamedTls(stream) => stream.past_end_of_stream, Stream::NamedTls(stream) => stream.past_end_of_stream,
#[cfg(feature = "http")]
Stream::HttpRead(stream) => stream.past_end_of_stream, Stream::HttpRead(stream) => stream.past_end_of_stream,
#[cfg(feature = "http")]
Stream::HttpWrite(stream) => stream.past_end_of_stream, Stream::HttpWrite(stream) => stream.past_end_of_stream,
Stream::Null(_) => false, Stream::Null(_) => false,
Stream::Readline(stream) => stream.past_end_of_stream, Stream::Readline(stream) => stream.past_end_of_stream,
@@ -974,8 +1056,11 @@ impl Stream {
Stream::OutputFile(stream) => stream.past_end_of_stream = value, Stream::OutputFile(stream) => stream.past_end_of_stream = value,
Stream::StaticString(stream) => stream.past_end_of_stream = value, Stream::StaticString(stream) => stream.past_end_of_stream = value,
Stream::NamedTcp(stream) => stream.past_end_of_stream = value, Stream::NamedTcp(stream) => stream.past_end_of_stream = value,
#[cfg(feature = "tls")]
Stream::NamedTls(stream) => stream.past_end_of_stream = value, Stream::NamedTls(stream) => stream.past_end_of_stream = value,
#[cfg(feature = "http")]
Stream::HttpRead(stream) => stream.past_end_of_stream = value, Stream::HttpRead(stream) => stream.past_end_of_stream = value,
#[cfg(feature = "http")]
Stream::HttpWrite(stream) => stream.past_end_of_stream = value, Stream::HttpWrite(stream) => stream.past_end_of_stream = value,
Stream::Null(_) => {} Stream::Null(_) => {}
Stream::Readline(stream) => stream.past_end_of_stream = value, Stream::Readline(stream) => stream.past_end_of_stream = value,
@@ -1054,6 +1139,7 @@ impl Stream {
Stream::InputFile(file) => Some(file.stream.get_ref().file_name), Stream::InputFile(file) => Some(file.stream.get_ref().file_name),
Stream::OutputFile(file) => Some(file.stream.file_name), Stream::OutputFile(file) => Some(file.stream.file_name),
Stream::NamedTcp(tcp) => Some(tcp.stream.get_ref().address), Stream::NamedTcp(tcp) => Some(tcp.stream.get_ref().address),
#[cfg(feature = "tls")]
Stream::NamedTls(tls) => Some(tls.stream.get_ref().address), Stream::NamedTls(tls) => Some(tls.stream.get_ref().address),
_ => None, _ => None,
} }
@@ -1062,14 +1148,19 @@ impl Stream {
#[inline] #[inline]
pub(crate) fn mode(&self) -> Atom { pub(crate) fn mode(&self) -> Atom {
match self { match self {
#[cfg(feature = "http")]
Stream::HttpRead(_) => atom!("read"),
#[cfg(feature = "tls")]
Stream::NamedTls(..) => atom!("read_append"),
Stream::Byte(_) Stream::Byte(_)
| Stream::Readline(_) | Stream::Readline(_)
| Stream::StaticString(_) | Stream::StaticString(_)
| Stream::HttpRead(_)
| Stream::InputFile(..) => atom!("read"), | Stream::InputFile(..) => atom!("read"),
Stream::NamedTcp(..) | Stream::NamedTls(..) => atom!("read_append"), Stream::NamedTcp(..) => atom!("read_append"),
Stream::OutputFile(file) if file.is_append => atom!("append"), Stream::OutputFile(file) if file.is_append => atom!("append"),
Stream::OutputFile(_) | Stream::StandardError(_) | Stream::StandardOutput(_) | Stream::HttpWrite(_) => atom!("write"), #[cfg(feature = "http")]
Stream::HttpWrite(_) => atom!("write"),
Stream::OutputFile(_) | Stream::StandardError(_) | Stream::StandardOutput(_) => atom!("write"),
Stream::Null(_) => atom!(""), Stream::Null(_) => atom!(""),
} }
} }
@@ -1108,6 +1199,7 @@ impl Stream {
)) ))
} }
#[cfg(feature = "tls")]
#[inline] #[inline]
pub(crate) fn from_tls_stream( pub(crate) fn from_tls_stream(
address: Atom, address: Atom,
@@ -1123,6 +1215,7 @@ impl Stream {
)) ))
} }
#[cfg(feature = "http")]
#[inline] #[inline]
pub(crate) fn from_http_stream( pub(crate) fn from_http_stream(
url: Atom, url: Atom,
@@ -1138,6 +1231,7 @@ impl Stream {
)) ))
} }
#[cfg(feature = "http")]
#[inline] #[inline]
pub(crate) fn from_http_sender( pub(crate) fn from_http_sender(
response: TypedArenaPtr<HttpResponse>, response: TypedArenaPtr<HttpResponse>,
@@ -1189,9 +1283,11 @@ impl Stream {
Stream::NamedTcp(ref mut tcp_stream) => { Stream::NamedTcp(ref mut tcp_stream) => {
tcp_stream.inner_mut().tcp_stream.shutdown(Shutdown::Both) tcp_stream.inner_mut().tcp_stream.shutdown(Shutdown::Both)
}, },
#[cfg(feature = "tls")]
Stream::NamedTls(ref mut tls_stream) => { Stream::NamedTls(ref mut tls_stream) => {
tls_stream.inner_mut().tls_stream.shutdown() tls_stream.inner_mut().tls_stream.shutdown()
} }
#[cfg(feature = "http")]
Stream::HttpRead(ref mut http_stream) => { Stream::HttpRead(ref mut http_stream) => {
unsafe { unsafe {
http_stream.set_tag(ArenaHeaderTag::Dropped); http_stream.set_tag(ArenaHeaderTag::Dropped);
@@ -1200,6 +1296,7 @@ impl Stream {
Ok(()) Ok(())
} }
#[cfg(feature = "http")]
Stream::HttpWrite(ref mut http_stream) => { Stream::HttpWrite(ref mut http_stream) => {
unsafe { unsafe {
http_stream.set_tag(ArenaHeaderTag::Dropped); http_stream.set_tag(ArenaHeaderTag::Dropped);
@@ -1242,9 +1339,11 @@ impl Stream {
#[inline] #[inline]
pub(crate) fn is_input_stream(&self) -> bool { pub(crate) fn is_input_stream(&self) -> bool {
match self { match self {
#[cfg(feature = "tls")]
Stream::NamedTls(..) => true,
#[cfg(feature = "http")]
Stream::HttpRead(..) => true,
Stream::NamedTcp(..) Stream::NamedTcp(..)
| Stream::NamedTls(..)
| Stream::HttpRead(..)
| Stream::Byte(_) | Stream::Byte(_)
| Stream::Readline(_) | Stream::Readline(_)
| Stream::StaticString(_) | Stream::StaticString(_)
@@ -1256,11 +1355,13 @@ impl Stream {
#[inline] #[inline]
pub(crate) fn is_output_stream(&self) -> bool { pub(crate) fn is_output_stream(&self) -> bool {
match self { match self {
#[cfg(feature = "tls")]
Stream::NamedTls(..) => true,
#[cfg(feature = "http")]
Stream::HttpWrite(..) => true,
Stream::StandardError(_) Stream::StandardError(_)
| Stream::StandardOutput(_) | Stream::StandardOutput(_)
| Stream::NamedTcp(..) | Stream::NamedTcp(..)
| Stream::NamedTls(..)
| Stream::HttpWrite(..)
| Stream::Byte(_) | Stream::Byte(_)
| Stream::OutputFile(..) => true, | Stream::OutputFile(..) => true,
_ => false, _ => false,

View File

@@ -7,9 +7,11 @@ use lazy_static::lazy_static;
use crate::arena::*; use crate::arena::*;
use crate::atom_table::*; use crate::atom_table::*;
use crate::forms::*; use crate::forms::*;
#[cfg(feature = "ffi")]
use crate::ffi::*; use crate::ffi::*;
use crate::heap_iter::*; use crate::heap_iter::*;
use crate::heap_print::*; use crate::heap_print::*;
#[cfg(feature = "http")]
use crate::http::{HttpService, HttpListener, HttpResponse}; use crate::http::{HttpService, HttpListener, HttpResponse};
use crate::instructions::*; use crate::instructions::*;
use crate::machine; use crate::machine;
@@ -44,6 +46,7 @@ use std::cmp::Ordering;
use std::collections::BTreeSet; use std::collections::BTreeSet;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::env; use std::env;
#[cfg(feature = "ffi")]
use std::ffi::CString; use std::ffi::CString;
use std::fs; use std::fs;
use std::hash::{BuildHasher, BuildHasherDefault}; use std::hash::{BuildHasher, BuildHasherDefault};
@@ -57,10 +60,13 @@ use std::process;
use std::str::FromStr; use std::str::FromStr;
use chrono::{offset::Local, DateTime}; use chrono::{offset::Local, DateTime};
#[cfg(not(target_os = "wasi"))]
use cpu_time::ProcessTime; use cpu_time::ProcessTime;
use std::time::{Duration, SystemTime}; use std::time::{Duration, SystemTime};
#[cfg(feature = "repl")]
use crossterm::event::{read, Event, KeyCode, KeyEvent, KeyModifiers}; use crossterm::event::{read, Event, KeyCode, KeyEvent, KeyModifiers};
#[cfg(feature = "repl")]
use crossterm::terminal::{disable_raw_mode, enable_raw_mode}; use crossterm::terminal::{disable_raw_mode, enable_raw_mode};
use blake2::{Blake2b, Blake2s}; use blake2::{Blake2b, Blake2s};
@@ -74,19 +80,25 @@ use sha3::{Sha3_224, Sha3_256, Sha3_384, Sha3_512};
use crrl::{secp256k1, x25519}; use crrl::{secp256k1, x25519};
#[cfg(feature = "tls")]
use native_tls::{TlsConnector,TlsAcceptor,Identity}; use native_tls::{TlsConnector,TlsAcceptor,Identity};
use base64; use base64;
use roxmltree; use roxmltree;
use select; use select;
#[cfg(feature = "http")]
use hyper::server::conn::http1; use hyper::server::conn::http1;
#[cfg(feature = "http")]
use hyper::header::{HeaderValue, HeaderName}; use hyper::header::{HeaderValue, HeaderName};
#[cfg(feature = "http")]
use hyper::{HeaderMap, Method}; use hyper::{HeaderMap, Method};
use http_body_util::BodyExt; use http_body_util::BodyExt;
use bytes::Buf; use bytes::Buf;
#[cfg(feature = "http")]
use reqwest::Url; use reqwest::Url;
#[cfg(feature = "repl")]
pub(crate) fn get_key() -> KeyEvent { pub(crate) fn get_key() -> KeyEvent {
let key; let key;
enable_raw_mode().expect("failed to enable raw mode"); enable_raw_mode().expect("failed to enable raw mode");
@@ -741,7 +753,7 @@ impl MachineState {
}; };
if let Some(max_steps) = max_steps_n { if let Some(max_steps) = max_steps_n {
if max_steps.abs() as usize <= 1 << 63 { if max_steps.abs() as u64 <= 1 << 63 {
if max_steps >= 0 { if max_steps >= 0 {
max_old = max_steps; max_old = max_steps;
} else { } else {
@@ -1764,6 +1776,7 @@ impl Machine {
#[inline(always)] #[inline(always)]
pub(crate) fn current_hostname(&mut self) { pub(crate) fn current_hostname(&mut self) {
#[cfg(feature = "hostname")]
match hostname::get().ok() { match hostname::get().ok() {
Some(host) => match host.to_str() { Some(host) => match host.to_str() {
Some(host) => { Some(host) => {
@@ -3679,6 +3692,7 @@ impl Machine {
Ok(()) Ok(())
} }
#[cfg(feature = "repl")]
#[inline(always)] #[inline(always)]
pub(crate) fn get_single_char(&mut self) -> CallResult { pub(crate) fn get_single_char(&mut self) -> CallResult {
let ctrl_c = KeyEvent { let ctrl_c = KeyEvent {
@@ -3702,7 +3716,28 @@ impl Machine {
KeyCode::Char(c) => c, KeyCode::Char(c) => c,
_ => unreachable!(), _ => unreachable!(),
}; };
let a1 = self.deref_register(1);
self.machine_st.unify_char(
c,
a1,
);
Ok(())
}
#[cfg(not(feature = "repl"))]
#[inline(always)]
pub(crate) fn get_single_char(&mut self) -> CallResult {
let mut buffer = [0; 1];
// is there a better way?
if std::io::stdin().read(&mut buffer).is_err() {
let stub = functor_stub(atom!("get_single_char"), 1);
let err = self.machine_st.interrupt_error();
let err = self.machine_st.error_form(err, stub);
return Err(err);
}
let c = buffer[0] as char;
let a1 = self.deref_register(1); let a1 = self.deref_register(1);
self.machine_st.unify_char( self.machine_st.unify_char(
c, c,
@@ -4158,6 +4193,7 @@ impl Machine {
self.machine_st.fail = result; self.machine_st.fail = result;
} }
#[cfg(not(target_os = "wasi"))]
#[inline(always)] #[inline(always)]
pub(crate) fn cpu_now(&mut self) { pub(crate) fn cpu_now(&mut self) {
let secs = ProcessTime::now().as_duration().as_secs_f64(); let secs = ProcessTime::now().as_duration().as_secs_f64();
@@ -4166,6 +4202,12 @@ impl Machine {
self.machine_st.unify_f64(secs, self.machine_st.registers[1]); self.machine_st.unify_f64(secs, self.machine_st.registers[1]);
} }
#[cfg(target_os = "wasi")]
#[inline(always)]
pub(crate) fn cpu_now(&mut self) {
// TODO
}
#[inline(always)] #[inline(always)]
pub(crate) fn det_length_rundown(&mut self) -> CallResult { pub(crate) fn det_length_rundown(&mut self) -> CallResult {
let stub_gen = || functor_stub(atom!("length"), 2); let stub_gen = || functor_stub(atom!("length"), 2);
@@ -4198,6 +4240,7 @@ impl Machine {
Ok(()) Ok(())
} }
#[cfg(feature = "http")]
#[inline(always)] #[inline(always)]
pub(crate) fn http_open(&mut self) -> CallResult { pub(crate) fn http_open(&mut self) -> CallResult {
let address_sink = self.deref_register(1); let address_sink = self.deref_register(1);
@@ -4316,6 +4359,7 @@ impl Machine {
Ok(()) Ok(())
} }
#[cfg(feature = "http")]
#[inline(always)] #[inline(always)]
pub(crate) fn http_listen(&mut self) -> CallResult { pub(crate) fn http_listen(&mut self) -> CallResult {
let address_sink = self.deref_register(1); let address_sink = self.deref_register(1);
@@ -4364,6 +4408,7 @@ impl Machine {
Ok(()) Ok(())
} }
#[cfg(feature = "http")]
#[inline(always)] #[inline(always)]
pub(crate) fn http_accept(&mut self) -> CallResult { pub(crate) fn http_accept(&mut self) -> CallResult {
let culprit = self.deref_register(1); let culprit = self.deref_register(1);
@@ -4447,6 +4492,7 @@ impl Machine {
Ok(()) Ok(())
} }
#[cfg(feature = "http")]
#[inline(always)] #[inline(always)]
pub(crate) fn http_answer(&mut self) -> CallResult { pub(crate) fn http_answer(&mut self) -> CallResult {
let culprit = self.deref_register(1); let culprit = self.deref_register(1);
@@ -4513,6 +4559,7 @@ impl Machine {
Ok(()) Ok(())
} }
#[cfg(feature = "ffi")]
#[inline(always)] #[inline(always)]
pub(crate) fn load_foreign_lib(&mut self) -> CallResult { pub(crate) fn load_foreign_lib(&mut self) -> CallResult {
let library_name = self.deref_register(1); let library_name = self.deref_register(1);
@@ -4559,6 +4606,7 @@ impl Machine {
Ok(()) Ok(())
} }
#[cfg(feature = "ffi")]
#[inline(always)] #[inline(always)]
pub(crate) fn foreign_call(&mut self) -> CallResult { pub(crate) fn foreign_call(&mut self) -> CallResult {
let function_name = self.deref_register(1); let function_name = self.deref_register(1);
@@ -4634,6 +4682,7 @@ impl Machine {
Ok(()) Ok(())
} }
#[cfg(feature = "ffi")]
fn build_struct(&mut self, name: &str, mut args: Vec<Value>) -> HeapCellValue { fn build_struct(&mut self, name: &str, mut args: Vec<Value>) -> HeapCellValue {
args.insert(0, Value::CString(CString::new(name).unwrap())); args.insert(0, Value::CString(CString::new(name).unwrap()));
let cells: Vec<_> = args.into_iter() let cells: Vec<_> = args.into_iter()
@@ -4654,6 +4703,7 @@ impl Machine {
) )
} }
#[cfg(feature = "ffi")]
#[inline(always)] #[inline(always)]
pub(crate) fn define_foreign_struct(&mut self) -> CallResult { pub(crate) fn define_foreign_struct(&mut self) -> CallResult {
let struct_name = self.deref_register(1); let struct_name = self.deref_register(1);
@@ -6233,6 +6283,7 @@ impl Machine {
Ok(()) Ok(())
} }
#[cfg(feature = "tls")]
#[inline(always)] #[inline(always)]
pub(crate) fn tls_client_connect(&mut self) -> CallResult { pub(crate) fn tls_client_connect(&mut self) -> CallResult {
if let Some(hostname) = self.machine_st.value_to_str_like(self.machine_st.registers[1]) { if let Some(hostname) = self.machine_st.value_to_str_like(self.machine_st.registers[1]) {
@@ -6270,6 +6321,7 @@ impl Machine {
} }
} }
#[cfg(feature = "tls")]
#[inline(always)] #[inline(always)]
pub(crate) fn tls_accept_client(&mut self) -> CallResult { pub(crate) fn tls_accept_client(&mut self) -> CallResult {
let pkcs12 = self.string_encoding_bytes(self.machine_st.registers[1], atom!("octet")); let pkcs12 = self.string_encoding_bytes(self.machine_st.registers[1], atom!("octet"));

View File

@@ -10,14 +10,19 @@ use crate::machine::machine_indices::*;
use crate::machine::machine_state::MachineState; use crate::machine::machine_state::MachineState;
use crate::machine::streams::*; use crate::machine::streams::*;
use crate::parser::char_reader::*; use crate::parser::char_reader::*;
#[cfg(feature = "repl")]
use crate::repl_helper::Helper; use crate::repl_helper::Helper;
use crate::types::*; use crate::types::*;
use fxhash::FxBuildHasher; use fxhash::FxBuildHasher;
use indexmap::IndexSet; use indexmap::IndexSet;
#[cfg(feature = "repl")]
use rustyline::error::ReadlineError; use rustyline::error::ReadlineError;
#[cfg(feature = "repl")]
use rustyline::history::DefaultHistory; use rustyline::history::DefaultHistory;
#[cfg(feature = "repl")]
use rustyline::{Config, Editor}; use rustyline::{Config, Editor};
use std::collections::VecDeque; use std::collections::VecDeque;
@@ -102,12 +107,14 @@ fn get_prompt() -> &'static str {
#[derive(Debug)] #[derive(Debug)]
pub struct ReadlineStream { pub struct ReadlineStream {
#[cfg(feature = "repl")]
rl: Editor<Helper, DefaultHistory>, rl: Editor<Helper, DefaultHistory>,
pending_input: CharReader<Cursor<String>>, pending_input: CharReader<Cursor<String>>,
add_history: bool, add_history: bool,
} }
impl ReadlineStream { impl ReadlineStream {
#[cfg(feature = "repl")]
#[inline] #[inline]
pub fn new(pending_input: &str, add_history: bool) -> Self { pub fn new(pending_input: &str, add_history: bool) -> Self {
let config = Config::builder() let config = Config::builder()
@@ -133,11 +140,25 @@ impl ReadlineStream {
} }
} }
#[cfg(not(feature = "repl"))]
#[inline]
pub fn new(pending_input: &str, add_history: bool) -> Self {
ReadlineStream {
pending_input: CharReader::new(Cursor::new(pending_input.to_owned())),
add_history: add_history,
}
}
#[cfg(feature = "repl")]
pub fn set_atoms_for_completion(&mut self, atoms: *const IndexSet<Atom>) { pub fn set_atoms_for_completion(&mut self, atoms: *const IndexSet<Atom>) {
let helper = self.rl.helper_mut().unwrap(); let helper = self.rl.helper_mut().unwrap();
helper.atoms = atoms; helper.atoms = atoms;
} }
#[cfg(not(feature = "repl"))]
pub fn set_atoms_for_completion(&mut self, atoms: *const IndexSet<Atom>) {
}
#[inline] #[inline]
pub fn reset(&mut self) { pub fn reset(&mut self) {
self.pending_input.reset_buffer(); self.pending_input.reset_buffer();
@@ -148,6 +169,7 @@ impl ReadlineStream {
pending_input.set_position(0); pending_input.set_position(0);
} }
#[cfg(feature = "repl")]
fn call_readline(&mut self) -> std::io::Result<usize> { fn call_readline(&mut self) -> std::io::Result<usize> {
match self.rl.readline(get_prompt()) { match self.rl.readline(get_prompt()) {
Ok(text) => { Ok(text) => {
@@ -175,6 +197,12 @@ impl ReadlineStream {
} }
} }
#[cfg(not(feature = "repl"))]
fn call_readline(&mut self) -> std::io::Result<usize> {
Ok(0)
}
#[cfg(feature = "repl")]
fn save_history(&mut self) { fn save_history(&mut self) {
if !self.add_history { if !self.add_history {
return; return;
@@ -191,6 +219,10 @@ impl ReadlineStream {
} }
} }
#[cfg(not(feature = "repl"))]
fn save_history(&mut self) {
}
#[inline] #[inline]
pub(crate) fn peek_byte(&mut self) -> std::io::Result<u8> { pub(crate) fn peek_byte(&mut self) -> std::io::Result<u8> {
let bytes = self.pending_input.refresh_buffer()?; let bytes = self.pending_input.refresh_buffer()?;