File tree Expand file tree Collapse file tree 5 files changed +62
-8
lines changed Expand file tree Collapse file tree 5 files changed +62
-8
lines changed Original file line number Diff line number Diff line change 11.PHONY : build-wasm
22
3+ #  --reference-types
4+ 
35build-wasm :
4- 	wasm-pack build --target nodejs bindings/lz-str-wasm
6+ 	wasm-pack build --target nodejs bindings/lz-str-wasm
7+ 	cd  bindings/lz-str-wasm &&  python inject-inline-js.py
8+ 	
9+ build-wasm-browser :
10+ 	wasm-pack build --target web bindings/lz-str-wasm 
11+ 	cd  bindings/lz-str-wasm &&  python inject-inline-js.py
Original file line number Diff line number Diff line change @@ -12,4 +12,7 @@ crate-type = ["cdylib", "rlib"]
1212[dependencies ]
1313js-sys  = " 0.3.47" 
1414lz-str  = { path  = " ../.." 
15- wasm-bindgen  = " 0.2.70" 
15+ wasm-bindgen  = " 0.2.70" 
16+ 
17+ [package .metadata .wasm-pack .profile .release ]
18+ wasm-opt  = [ ' -O4' 
Original file line number Diff line number Diff line change 1+ if  __name__  ==  "__main__" :
2+     js_file_str  =  None 
3+     with  open ('pkg/lz_str_wasm.js' , 'r' ) as  file :
4+         js_file_str  =  file .read ()
5+         
6+     inline_js  =  None 
7+     with  open ('src/inline.js' , 'r' ) as  file :
8+         inline_js  =  file .read ()
9+       
10+     js_file_str  +=  '\n '  +  inline_js 
11+     with  open ('pkg/lz_str_wasm.js' , 'w' ) as  file :
12+         file .write (js_file_str )
Original file line number Diff line number Diff line change 1+ const  CHUNK_SIZE  =  10_000 ; 
2+ 
3+ function  convertUint16ArrayToString ( array )  { 
4+     let  ret  =  '' ; 
5+     let  num_chunks  =  Math . ceil ( array . length  /  CHUNK_SIZE ) ; 
6+     for ( let  i  =  0 ;  i  <  array . length ;  i  +=  CHUNK_SIZE )  { 
7+         let  end_index  =  Math . min ( i  +  CHUNK_SIZE ,  i  +  array . length ) ; 
8+         ret  +=  String . fromCharCode ( ...array . subarray ( i ,  end_index ) ) ; 
9+     } 
10+     
11+     return  ret ; 
12+ } 
Original file line number Diff line number Diff line change 11use  js_sys:: JsString ; 
2+ use  js_sys:: Uint16Array ; 
23use  wasm_bindgen:: prelude:: * ; 
34use  wasm_bindgen:: JsCast ; 
45
6+ #[ wasm_bindgen]  
7+ extern  "C"  { 
8+     #[ wasm_bindgen( js_name = "convertUint16ArrayToString" ) ]  
9+     fn  convert_uint16_array_to_string ( array :  & Uint16Array )  -> JsString ; 
10+ } 
11+ 
512/// Compress a [`JsString`]. 
613#[ wasm_bindgen]  
7- pub  fn  compress ( data :  & JsValue )  -> JsString  { 
8-     let  data:  & JsString  = data. dyn_ref :: < JsString > ( ) . expect ( "invalid `JsString`" ) ; 
14+ pub  fn  compress ( data :  & JsValue )  -> JsValue  { 
15+     let  data:  & JsString  = match  data. dyn_ref :: < JsString > ( )  { 
16+         Some ( data)  => data, 
17+         None  => { 
18+             return  JsValue :: NULL ; 
19+         } 
20+     } ; 
921    let  data:  Vec < u16 >  = data. iter ( ) . collect ( ) ; 
1022    let  compressed = lz_str:: compress ( & data) ; 
11-     JsString :: from_char_code ( & compressed) 
23+     let  array:  Uint16Array  = compressed. as_slice ( ) . into ( ) ; 
24+     convert_uint16_array_to_string ( & array) . into ( ) 
1225} 
1326
1427/// Decompress a [`JsString`]. 
1528#[ wasm_bindgen]  
1629pub  fn  decompress ( data :  & JsValue )  -> JsValue  { 
17-     let  data:  & JsString  = data. dyn_ref :: < JsString > ( ) . expect ( "invalid `JsString`" ) ; 
30+     let  data:  & JsString  = match  data. dyn_ref :: < JsString > ( )  { 
31+         Some ( data)  => data, 
32+         None  => { 
33+             return  JsValue :: NULL ; 
34+         } 
35+     } ; 
1836    let  data:  Vec < u16 >  = data. iter ( ) . collect ( ) ; 
1937    lz_str:: decompress ( & data) 
20-         . map ( |s| JsString :: from_char_code ( & s) ) 
21-         . map ( Into :: into) 
38+         . map ( |decompressed| { 
39+             let  array:  Uint16Array  = decompressed. as_slice ( ) . into ( ) ; 
40+             convert_uint16_array_to_string ( & array) . into ( ) 
41+         } ) 
2242        . unwrap_or ( JsValue :: NULL ) 
2343} 
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments