From 25edde2eb4f65e5d798a39499978e70baa200d98 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Bennet=20Ble=C3=9Fmann?= Date: Sat, 26 Jul 2025 21:02:42 +0200 Subject: [PATCH] replace filter by tfiltert and fix Pipe{Reader,Writer} streams --- src/lib/lists.pl | 10 +--------- src/lib/process.pl | 19 ++++++++++++------- src/machine/streams.rs | 4 ++++ src/macros.rs | 14 ++++++++++++++ 4 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/lib/lists.pl b/src/lib/lists.pl index 9c972f01..3d1cc6a2 100644 --- a/src/lib/lists.pl +++ b/src/lib/lists.pl @@ -7,7 +7,7 @@ List manipulation predicates maplist/3, maplist/4, maplist/5, maplist/6, maplist/7, maplist/8, maplist/9, same_length/2, nth0/3, nth0/4, nth1/3, nth1/4, sum_list/2, transpose/2, list_to_set/2, list_max/2, - list_min/2, permutation/2, filter/3]). + list_min/2, permutation/2]). /* Author: Mark Thom, Jan Wielemaker, and Richard O'Keefe Copyright (c) 2018-2021, Mark Thom @@ -538,11 +538,3 @@ perm([], []). perm(List, [First|Perm]) :- select(First, List, Rest), perm(Rest, Perm). - - -%% filter(+Predicate, ?Xs1 ?Xs2). -% -% Succeeds if Xs2 is the list of elements X from Xs1 for which call(Pred, X) succeeds. -% -filter(_, [], []). -filter(Pred, [X1|XS1], XS) :- call(Pred, X1) -> filter(Pred, XS1, XS2), XS = [X1|XS2] ; filter(Pred, XS1, XS). \ No newline at end of file diff --git a/src/lib/process.pl b/src/lib/process.pl index 6f682ac8..d02713e9 100644 --- a/src/lib/process.pl +++ b/src/lib/process.pl @@ -9,7 +9,8 @@ :- use_module(library(error)). :- use_module(library(iso_ext)). -:- use_module(library(lists), [member/2, maplist/2, filter/3]). +:- use_module(library(lists), [member/2, maplist/2]). +:- use_module(library(reif), [tfilter/3]). %% process_create(+Exe, +Args:list, +Options). @@ -148,7 +149,7 @@ must_be_known_options(Valid, Found, [X|XS]) :- check_options([], _). check_options([X | XS], Options) :- (Kinds, Pred, Default, Choice) = X, - filter(process:find_option(Kinds), Options, Solutions), + tfilter(process:find_option(Kinds), Options, Solutions), ( Solutions = [] -> Choice = Default; Solutions = [Provided] -> call(Pred, Provided), Choice = Provided ; @@ -156,11 +157,11 @@ check_options([X | XS], Options) :- ), check_options(XS, Options). -find_option([Kind|_], Found) :- Found =.. [Kind,_]. -find_option([_|Kinds], Found) :- find_option(Kinds, Found). +find_option(Names, Found, T) :- (functor(Found, Name, 1), member(Name, Names)) -> T = true ; T = false. -valid_stdio(IO) :- IO =.. [_, Arg], +valid_stdio(IO) :- arg(1, IO, Arg), ( + var(Arg) -> instantiation_error(process_create/3) ; valid_stdio_(Arg) -> true ; domain_error(process_create_option, Arg, process_create/3) ). @@ -170,11 +171,15 @@ valid_stdio_(null). valid_stdio_(pipe(Stream)) :- must_be(var, Stream). valid_stdio_(file(Path)) :- must_be(chars, Path). -valid_env(env(E)) :- ( +valid_env(env(E)) :- + must_be(list, E), + ( valid_env_(E) -> true ; domain_error(process_create_option, env(E), process_create/3) ). -valid_env(environment(E)) :- ( +valid_env(environment(E)) :- + must_be(list, E), + ( valid_env_(E) -> true ; domain_error(process_create_option, environment(E), process_create/3) ). diff --git a/src/machine/streams.rs b/src/machine/streams.rs index 871ea617..596b09de 100644 --- a/src/machine/streams.rs +++ b/src/machine/streams.rs @@ -694,6 +694,8 @@ impl Stream { ArenaHeaderTag::InputChannelStream => { Stream::InputChannel(unsafe { ptr.as_typed_ptr() }) } + ArenaHeaderTag::PipeReader => Stream::PipeReader(unsafe { ptr.as_typed_ptr() }), + ArenaHeaderTag::PipeWriter => Stream::PipeWriter(unsafe { ptr.as_typed_ptr() }), _ => unreachable!(), } } @@ -1601,6 +1603,7 @@ impl Stream { | Stream::Readline(_) | Stream::StaticString(_) | Stream::InputFile(..) + | Stream::PipeReader(_) | Stream::Null(_) => true, _ => false, } @@ -1619,6 +1622,7 @@ impl Stream { | Stream::Byte(_) | Stream::OutputFile(..) | Stream::Callback(_) + | Stream::PipeWriter(_) | Stream::Null(_) => true, _ => false, } diff --git a/src/macros.rs b/src/macros.rs index 10467419..79470f18 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -218,6 +218,18 @@ macro_rules! match_untyped_arena_ptr_pat_body { #[allow(unused_braces)] $code }}; + ($ptr:ident, PipeReader, $listener:ident, $code:expr) => {{ + #[allow(unused_mut)] + let mut $listener = unsafe { $ptr.as_typed_ptr::() }; + #[allow(unused_braces)] + $code + }}; + ($ptr:ident, PipeWriter, $listener:ident, $code:expr) => {{ + #[allow(unused_mut)] + let mut $listener = unsafe { $ptr.as_typed_ptr::() }; + #[allow(unused_braces)] + $code + }}; ($ptr:ident, ChildProcess, $listener:ident, $code:expr) => {{ #[allow(unused_mut)] let mut $listener = unsafe { $ptr.as_typed_ptr::() }; @@ -246,6 +258,8 @@ macro_rules! match_untyped_arena_ptr_pat { | ArenaHeaderTag::InputChannelStream | ArenaHeaderTag::StandardOutputStream | ArenaHeaderTag::StandardErrorStream + | ArenaHeaderTag::PipeReader + | ArenaHeaderTag::PipeWriter }; ($tag:ident) => { ArenaHeaderTag::$tag -- 2.54.0