@@ -11,6 +11,7 @@ use cilly::{
11
11
cil_root:: CILRoot ,
12
12
conv_usize,
13
13
ilasm_exporter:: ILASM_FLAVOUR ,
14
+ js_exporter:: JSExporter ,
14
15
ldc_i32,
15
16
method:: { Method , MethodType } ,
16
17
DotnetTypeRef , FnSig , IString , IlasmFlavour , Type ,
@@ -24,7 +25,7 @@ mod export;
24
25
use cilly:: libc_fns;
25
26
mod load;
26
27
mod patch;
27
- use std:: { collections:: HashMap , env, io:: Write } ;
28
+ use std:: { collections:: HashMap , env, io:: Write , path :: Path } ;
28
29
struct NativePastroughInfo {
29
30
defs : HashMap < IString , IString > ,
30
31
}
@@ -771,67 +772,112 @@ fn main() {
771
772
false ,
772
773
)
773
774
. unwrap ( ) ;
774
- return ;
775
- }
776
-
777
- // Run ILASM
778
- export:: export_assembly ( & final_assembly, output_file_path, is_lib)
775
+ } else if * JS_MODE {
776
+ type Exporter = cilly:: js_exporter:: JSExporter ;
777
+ use cilly:: asm_exporter:: AssemblyExporter ;
778
+ Exporter :: export_assembly (
779
+ Exporter :: default ( ) ,
780
+ & final_assembly,
781
+ output_file_path. as_ref ( ) ,
782
+ is_lib,
783
+ false ,
784
+ )
779
785
. expect ( "Assembly export faliure!" ) ;
780
- let path: std:: path:: PathBuf = output_file_path. into ( ) ;
781
- final_assembly
782
- . save_tmp ( & mut std:: fs:: File :: create ( path. with_extension ( "cilly" ) ) . unwrap ( ) )
783
- . unwrap ( ) ;
784
- // Run AOT compiler
785
- aot_compile_mode. compile ( output_file_path) ;
786
+ let path: std:: path:: PathBuf = output_file_path. into ( ) ;
787
+ final_assembly
788
+ . save_tmp ( & mut std:: fs:: File :: create ( path. with_extension ( "cilly" ) ) . unwrap ( ) )
789
+ . unwrap ( ) ;
790
+ // Run AOT compiler
791
+ aot_compile_mode. compile ( output_file_path) ;
786
792
787
- // Cargo integration
793
+ // Cargo integration
788
794
789
- if cargo_support {
790
- let bootstrap = format ! (
791
- include_str!( "dotnet_jumpstart.rs" ) ,
792
- exec_file = path. file_name( ) . unwrap( ) . to_string_lossy( ) ,
793
- has_native_companion = * NATIVE_PASSTROUGH ,
794
- has_pdb = match * ILASM_FLAVOUR {
795
- IlasmFlavour :: Clasic => false ,
796
- IlasmFlavour :: Modern => true ,
797
- } ,
798
- pdb_file = match * ILASM_FLAVOUR {
799
- IlasmFlavour :: Clasic => String :: new( ) ,
800
- IlasmFlavour :: Modern => format!(
801
- "{output_file_path}.pdb" ,
802
- output_file_path = path. file_name( ) . unwrap( ) . to_string_lossy( )
803
- ) ,
804
- } ,
805
- native_companion_file = if * NATIVE_PASSTROUGH {
806
- format!(
807
- "rust_native_{output_file_path}.so" ,
808
- output_file_path = file_stem( output_file_path)
809
- )
810
- } else {
811
- String :: new( )
812
- }
813
- ) ;
814
- let bootstrap_path = path. with_extension ( "rs" ) ;
815
- let mut bootstrap_file = std:: fs:: File :: create ( & bootstrap_path) . unwrap ( ) ;
816
- bootstrap_file. write_all ( bootstrap. as_bytes ( ) ) . unwrap ( ) ;
817
- let path = std:: env:: var ( "PATH" ) . unwrap ( ) ;
818
- let out = std:: process:: Command :: new ( "rustc" )
819
- . arg ( "-O" )
820
- . arg ( bootstrap_path)
821
- . arg ( "-o" )
822
- . arg ( output_file_path)
823
- . env_clear ( )
824
- . env ( "PATH" , path)
825
- . output ( )
795
+ if cargo_support {
796
+ let bootstrap = bootstrap_source ( & path, output_file_path, "node" ) ;
797
+ let bootstrap_path = path. with_extension ( "rs" ) ;
798
+ let mut bootstrap_file = std:: fs:: File :: create ( & bootstrap_path) . unwrap ( ) ;
799
+ bootstrap_file. write_all ( bootstrap. as_bytes ( ) ) . unwrap ( ) ;
800
+ let path = std:: env:: var ( "PATH" ) . unwrap ( ) ;
801
+ let out = std:: process:: Command :: new ( "rustc" )
802
+ . arg ( "-O" )
803
+ . arg ( bootstrap_path)
804
+ . arg ( "-o" )
805
+ . arg ( output_file_path)
806
+ . env_clear ( )
807
+ . env ( "PATH" , path)
808
+ . output ( )
809
+ . unwrap ( ) ;
810
+ assert ! (
811
+ out. stderr. is_empty( ) ,
812
+ "{}" ,
813
+ String :: from_utf8( out. stderr) . unwrap( )
814
+ ) ;
815
+ }
816
+ } else {
817
+ // Run ILASM
818
+ export:: export_assembly ( & final_assembly, output_file_path, is_lib)
819
+ . expect ( "Assembly export faliure!" ) ;
820
+ let path: std:: path:: PathBuf = output_file_path. into ( ) ;
821
+ final_assembly
822
+ . save_tmp ( & mut std:: fs:: File :: create ( path. with_extension ( "cilly" ) ) . unwrap ( ) )
826
823
. unwrap ( ) ;
827
- assert ! (
828
- out. stderr. is_empty( ) ,
829
- "{}" ,
830
- String :: from_utf8( out. stderr) . unwrap( )
831
- ) ;
824
+ // Run AOT compiler
825
+ aot_compile_mode. compile ( output_file_path) ;
826
+
827
+ // Cargo integration
828
+
829
+ if cargo_support {
830
+ let bootstrap = bootstrap_source ( & path, output_file_path, "dotnet" ) ;
831
+ let bootstrap_path = path. with_extension ( "rs" ) ;
832
+ let mut bootstrap_file = std:: fs:: File :: create ( & bootstrap_path) . unwrap ( ) ;
833
+ bootstrap_file. write_all ( bootstrap. as_bytes ( ) ) . unwrap ( ) ;
834
+ let path = std:: env:: var ( "PATH" ) . unwrap ( ) ;
835
+ let out = std:: process:: Command :: new ( "rustc" )
836
+ . arg ( "-O" )
837
+ . arg ( bootstrap_path)
838
+ . arg ( "-o" )
839
+ . arg ( output_file_path)
840
+ . env_clear ( )
841
+ . env ( "PATH" , path)
842
+ . output ( )
843
+ . unwrap ( ) ;
844
+ assert ! (
845
+ out. stderr. is_empty( ) ,
846
+ "{}" ,
847
+ String :: from_utf8( out. stderr) . unwrap( )
848
+ ) ;
849
+ }
832
850
}
851
+
833
852
//todo!();
834
853
}
854
+ fn bootstrap_source ( fpath : & Path , output_file_path : & str , jumpstart_cmd : & str ) -> String {
855
+ format ! (
856
+ include_str!( "dotnet_jumpstart.rs" ) ,
857
+ jumpstart_cmd = "dotnet" ,
858
+ exec_file = fpath. file_name( ) . unwrap( ) . to_string_lossy( ) ,
859
+ has_native_companion = * NATIVE_PASSTROUGH ,
860
+ has_pdb = match * ILASM_FLAVOUR {
861
+ IlasmFlavour :: Clasic => false ,
862
+ IlasmFlavour :: Modern => true ,
863
+ } ,
864
+ pdb_file = match * ILASM_FLAVOUR {
865
+ IlasmFlavour :: Clasic => String :: new( ) ,
866
+ IlasmFlavour :: Modern => format!(
867
+ "{output_file_path}.pdb" ,
868
+ output_file_path = fpath. file_name( ) . unwrap( ) . to_string_lossy( )
869
+ ) ,
870
+ } ,
871
+ native_companion_file = if * NATIVE_PASSTROUGH {
872
+ format!(
873
+ "rust_native_{output_file_path}.so" ,
874
+ output_file_path = file_stem( output_file_path)
875
+ )
876
+ } else {
877
+ String :: new( )
878
+ }
879
+ )
880
+ }
835
881
lazy_static ! {
836
882
#[ doc = "Tells the codegen compile linked static libraries into a shared library, which will be bundled with the .NET executable." ] pub static ref NATIVE_PASSTROUGH : bool = {
837
883
std:: env:: vars( ) . find_map( |( key, value) |if key == stringify!( NATIVE_PASSTROUGH ) {
@@ -865,6 +911,17 @@ lazy_static! {
865
911
} ) . unwrap_or( false )
866
912
} ;
867
913
}
914
+ lazy_static ! {
915
+ #[ doc = "Tells the codegen to emmit JS source files." ] pub static ref JS_MODE : bool = {
916
+ std:: env:: vars( ) . find_map( |( key, value) |if key == stringify!( JS_MODE ) {
917
+ Some ( value)
918
+ } else {
919
+ None
920
+ } ) . map( |value|match value. as_ref( ) {
921
+ "0" |"false" |"False" |"FALSE" => false , "1" |"true" |"True" |"TRUE" => true , _ => panic!( "Boolean enviroment variable {} has invalid value {}" , stringify!( C_MODE ) , value) ,
922
+ } ) . unwrap_or( false )
923
+ } ;
924
+ }
868
925
fn override_errno ( asm : & mut Assembly ) {
869
926
for method in asm. methods_mut ( ) {
870
927
if method. name ( ) . contains ( "errno" )
0 commit comments