@@ -6,8 +6,8 @@ use std::{
66use anyhow:: { bail, Context as _, Error , Result } ;
77use clap:: Args ;
88use std:: str:: FromStr ;
9- use wac_graph:: { CompositionGraph , EncodeOptions , NodeId , PackageId } ;
10- use wac_types:: { Package , SubtypeChecker } ;
9+ use wac_graph:: { CompositionGraph , EncodeOptions } ;
10+ use wac_types:: Package ;
1111
1212#[ cfg( feature = "registry" ) ]
1313use warg_client:: FileSystemClient ;
@@ -151,7 +151,6 @@ impl PlugCommand {
151151
152152 let socket = Package :: from_bytes ( "socket" , None , socket, graph. types_mut ( ) ) ?;
153153 let socket = graph. register_package ( socket) ?;
154- let socket_instantiation = graph. instantiate ( socket) ;
155154
156155 // Collect the plugs by their names
157156 let mut plugs_by_name = std:: collections:: HashMap :: < _ , Vec < _ > > :: new ( ) ;
@@ -169,6 +168,8 @@ impl PlugCommand {
169168 plugs_by_name. entry ( name) . or_default ( ) . push ( plug) ;
170169 }
171170
171+ let mut plug_packages = Vec :: new ( ) ;
172+
172173 // Plug each plug into the socket.
173174 for ( name, plug_refs) in plugs_by_name {
174175 for ( i, plug_ref) in plug_refs. iter ( ) . enumerate ( ) {
@@ -221,37 +222,23 @@ impl PlugCommand {
221222 use core:: fmt:: Write ;
222223 write ! ( & mut name, "{i}" ) . unwrap ( ) ;
223224 }
224- plug_into_socket ( & name, & path, socket, socket_instantiation, & mut graph) ?;
225+
226+ let plug_package = Package :: from_file ( & name, None , & path, graph. types_mut ( ) ) ?;
227+ let package_id = graph. register_package ( plug_package) ?;
228+ plug_packages. push ( package_id) ;
225229 }
226230 }
227231
228- // Check we've actually done any plugging.
229- if graph
230- . get_instantiation_arguments ( socket_instantiation)
231- . next ( )
232- . is_none ( )
233- {
234- bail ! ( "the socket component had no matching imports for the plugs that were provided" )
235- }
232+ wac_graph:: plug ( & mut graph, plug_packages, socket) ?;
236233
237- // Export all exports from the socket component.
238- for name in graph. types ( ) [ graph[ socket] . ty ( ) ]
239- . exports
240- . keys ( )
241- . cloned ( )
242- . collect :: < Vec < _ > > ( )
243- {
244- let export = graph. alias_instance_export ( socket_instantiation, & name) ?;
245- graph. export ( export, & name) ?;
246- }
234+ let mut bytes = graph. encode ( EncodeOptions :: default ( ) ) ?;
247235
248236 let binary_output_to_terminal =
249237 !self . wat && self . output . is_none ( ) && std:: io:: stdout ( ) . is_terminal ( ) ;
250238 if binary_output_to_terminal {
251239 bail ! ( "cannot print binary wasm output to a terminal; pass the `-t` flag to print the text format instead" ) ;
252240 }
253241
254- let mut bytes = graph. encode ( EncodeOptions :: default ( ) ) ?;
255242 if self . wat {
256243 bytes = wasmprinter:: print_bytes ( & bytes)
257244 . context ( "failed to convert binary wasm output to text" ) ?
@@ -278,39 +265,3 @@ impl PlugCommand {
278265 Ok ( ( ) )
279266 }
280267}
281-
282- /// Take the exports of the plug component and plug them into the socket component.
283- fn plug_into_socket (
284- name : & str ,
285- plug_path : & std:: path:: Path ,
286- socket : PackageId ,
287- socket_instantiation : NodeId ,
288- graph : & mut CompositionGraph ,
289- ) -> Result < ( ) , anyhow:: Error > {
290- let plug = Package :: from_file ( name, None , plug_path, graph. types_mut ( ) ) ?;
291- let plug = graph. register_package ( plug) ?;
292-
293- let mut plugs = Vec :: new ( ) ;
294- let mut cache = Default :: default ( ) ;
295- let mut checker = SubtypeChecker :: new ( & mut cache) ;
296- for ( name, plug_ty) in & graph. types ( ) [ graph[ plug] . ty ( ) ] . exports {
297- if let Some ( socket_ty) = graph. types ( ) [ graph[ socket] . ty ( ) ] . imports . get ( name) {
298- if checker
299- . is_subtype ( * plug_ty, graph. types ( ) , * socket_ty, graph. types ( ) )
300- . is_ok ( )
301- {
302- plugs. push ( name. clone ( ) ) ;
303- }
304- }
305- }
306-
307- // Instantiate the plug component
308- let mut plug_instantiation = None ;
309- for plug_name in plugs {
310- log:: debug!( "using export `{plug_name}` for plug" ) ;
311- let plug_instantiation = * plug_instantiation. get_or_insert_with ( || graph. instantiate ( plug) ) ;
312- let export = graph. alias_instance_export ( plug_instantiation, & plug_name) ?;
313- graph. set_instantiation_argument ( socket_instantiation, & plug_name, export) ?;
314- }
315- Ok ( ( ) )
316- }
0 commit comments