@@ -805,6 +805,30 @@ impl<'a> Context<'a> {
805
805
self . global ( "let WASM_VECTOR_LEN = 0;" ) ;
806
806
}
807
807
808
+ fn expose_encode_as_ascii ( & mut self ) {
809
+ if !self . should_write_global ( "encode_as_ascii" ) {
810
+ return ;
811
+ }
812
+
813
+ self . expose_uint8_memory ( ) ;
814
+
815
+ self . global ( "
816
+ function encodeAsAscii(arg, ptr, len) {
817
+ let offset = 0;
818
+
819
+ const mem = getUint8Memory();
820
+
821
+ for (; offset < len; offset++) {
822
+ const code = arg.charCodeAt(offset);
823
+ if (code > 0x7F) break;
824
+ mem[ptr + offset] = code;
825
+ }
826
+
827
+ return offset;
828
+ }
829
+ " ) ;
830
+ }
831
+
808
832
fn expose_pass_string_to_wasm ( & mut self ) -> Result < ( ) , Error > {
809
833
if !self . should_write_global ( "pass_string_to_wasm" ) {
810
834
return Ok ( ( ) ) ;
@@ -846,6 +870,8 @@ impl<'a> Context<'a> {
846
870
self . expose_text_encoder ( ) ?;
847
871
self . expose_uint8_memory ( ) ;
848
872
873
+ self . expose_encode_as_ascii ( ) ;
874
+
849
875
// A fast path that directly writes char codes into WASM memory as long
850
876
// as it finds only ASCII characters.
851
877
//
@@ -856,30 +882,25 @@ impl<'a> Context<'a> {
856
882
// expensive in mainstream engines than staying in the JS, and
857
883
// charCodeAt on ASCII strings is usually optimised to raw bytes.
858
884
let start_encoding_as_ascii = format ! (
859
- "
885
+ "\
860
886
{}
861
- let size = arg.length;
862
- let ptr = wasm.__wbindgen_malloc(size);
863
- let offset = 0;
864
- {{
865
- const mem = getUint8Memory();
866
- for (; offset < arg.length; offset++) {{
867
- const code = arg.charCodeAt(offset);
868
- if (code > 0x7F) break;
869
- mem[ptr + offset] = code;
870
- }}
871
- }}
887
+ const len = arg.length;
888
+ let ptr = wasm.__wbindgen_malloc(len);
889
+ const offset = encodeAsAscii(arg, ptr, len);
872
890
" ,
873
891
debug
874
892
) ;
875
893
876
894
// The first implementation we have for this is to use
877
895
// `TextEncoder#encode` which has been around for quite some time.
878
896
let use_encode = format ! (
879
- "
897
+ "\
880
898
{}
881
- if (offset !== arg.length) {{
882
- const buf = cachedTextEncoder.encode(arg.slice(offset));
899
+ if (offset !== len) {{
900
+ if (offset !== 0) {{
901
+ arg = arg.slice(offset);
902
+ }}
903
+ const buf = cachedTextEncoder.encode(arg);
883
904
ptr = wasm.__wbindgen_realloc(ptr, size, size = offset + buf.length);
884
905
getUint8Memory().set(buf, ptr + offset);
885
906
offset += buf.length;
@@ -894,11 +915,13 @@ impl<'a> Context<'a> {
894
915
// newer and isn't implemented everywhere yet. It's more efficient,
895
916
// however, becaues it allows us to elide an intermediate allocation.
896
917
let use_encode_into = format ! (
897
- "
918
+ "\
898
919
{}
899
- if (offset !== arg.length) {{
900
- arg = arg.slice(offset);
901
- ptr = wasm.__wbindgen_realloc(ptr, size, size = offset + arg.length * 3);
920
+ if (offset !== len) {{
921
+ if (offset !== 0) {{
922
+ arg = arg.slice(offset);
923
+ }}
924
+ ptr = wasm.__wbindgen_realloc(ptr, size, size = offset + len * 3);
902
925
const view = getUint8Memory().subarray(ptr + offset, ptr + size);
903
926
const ret = cachedTextEncoder.encodeInto(arg, view);
904
927
{}
@@ -909,7 +932,7 @@ impl<'a> Context<'a> {
909
932
" ,
910
933
start_encoding_as_ascii,
911
934
if self . config. debug {
912
- "if (ret.read != arg.length ) throw new Error('failed to pass whole string');"
935
+ "if (ret.read != len ) throw new Error('failed to pass whole string');"
913
936
} else {
914
937
""
915
938
} ,
@@ -932,12 +955,9 @@ impl<'a> Context<'a> {
932
955
self . require_internal_export ( "__wbindgen_realloc" ) ?;
933
956
self . global ( & format ! (
934
957
"
935
- let passStringToWasm;
936
- if (typeof cachedTextEncoder.encodeInto === 'function') {{
937
- passStringToWasm = function(arg) {{ {} }};
938
- }} else {{
939
- passStringToWasm = function(arg) {{ {} }};
940
- }}
958
+ const passStringToWasm = (typeof cachedTextEncoder.encodeInto === 'function'
959
+ ? function (arg) {{ {} }}
960
+ : function (arg) {{ {} }});
941
961
" ,
942
962
use_encode_into, use_encode,
943
963
) ) ;
0 commit comments