Skip to content

Commit 036b140

Browse files
authored
Merge branch 'master' into immutable
2 parents 0146260 + 7306b7f commit 036b140

File tree

14 files changed

+62
-102
lines changed

14 files changed

+62
-102
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* Runtime/wasm: support jsoo_env and keep track of backtrace status (#1881)
2020
* Runtime/wasm: support unmarshaling compressed data (#1898)
2121
* Runtime/wasm: make resuming a continuation more efficient in Wasm (#1892)
22+
* Runtime/wasm: use imported string constants for JavaScript strings (#2022)
2223
* Compiler: improve performance of Javascript linking
2324
* Compiler: remove empty blocks (#1934)
2425
* Ppx: explicitly disallow polymorphic method (#1897)

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ let run
484484
then Some (Filename.temp_file unit_name ".wasm.map")
485485
else None)
486486
@@ fun opt_input_sourcemap ->
487-
let strings, fragments =
487+
let fragments =
488488
output
489489
code
490490
~wat_file:
@@ -500,7 +500,7 @@ let run
500500
~input_file
501501
~output_file:tmp_wasm_file
502502
();
503-
{ Link.unit_name; unit_info; strings; fragments }
503+
{ Link.unit_name; unit_info; fragments }
504504
in
505505
cont unit_data unit_name tmp_wasm_file opt_tmp_map_file
506506
in

compiler/lib-wasm/code_generation.ml

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ type context =
5353
; mutable dummy_funs : Var.t IntMap.t
5454
; mutable cps_dummy_funs : Var.t IntMap.t
5555
; mutable init_code : W.instruction list
56-
; mutable string_count : int
57-
; mutable strings : string list
58-
; mutable string_index : int StringMap.t
5956
; mutable fragments : Javascript.expression StringMap.t
6057
; mutable globalized_variables : Var.Set.t
6158
; value_type : W.value_type
@@ -78,9 +75,6 @@ let make_context ~value_type =
7875
; dummy_funs = IntMap.empty
7976
; cps_dummy_funs = IntMap.empty
8077
; init_code = []
81-
; string_count = 0
82-
; strings = []
83-
; string_index = StringMap.empty
8478
; fragments = StringMap.empty
8579
; globalized_variables = Var.Set.empty
8680
; value_type
@@ -254,16 +248,6 @@ let register_init_code code st =
254248
st.context.init_code <- st'.instrs @ st.context.init_code;
255249
(), st
256250

257-
let register_string s st =
258-
let context = st.context in
259-
try StringMap.find s context.string_index, st
260-
with Not_found ->
261-
let n = context.string_count in
262-
context.string_count <- 1 + context.string_count;
263-
context.strings <- s :: context.strings;
264-
context.string_index <- StringMap.add s n context.string_index;
265-
n, st
266-
267251
let register_fragment name f st =
268252
let context = st.context in
269253
if not (StringMap.mem name context.fragments)
@@ -476,7 +460,8 @@ let rec is_smi e =
476460
| Br_on_cast_fail _
477461
| Br_on_null _
478462
| Try _
479-
| ExternConvertAny _ -> false
463+
| ExternConvertAny _
464+
| AnyConvertExtern _ -> false
480465
| BinOp ((F32 _ | F64 _), _, _) | RefTest _ | RefEq _ -> true
481466
| IfExpr (_, _, ift, iff) -> is_smi ift && is_smi iff
482467

compiler/lib-wasm/code_generation.mli

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@ type context =
3737
; mutable dummy_funs : Code.Var.t Stdlib.IntMap.t
3838
; mutable cps_dummy_funs : Code.Var.t Stdlib.IntMap.t
3939
; mutable init_code : Wasm_ast.instruction list
40-
; mutable string_count : int
41-
; mutable strings : string list
42-
; mutable string_index : int StringMap.t
4340
; mutable fragments : Javascript.expression StringMap.t
4441
; mutable globalized_variables : Code.Var.Set.t
4542
; value_type : Wasm_ast.value_type
@@ -178,8 +175,6 @@ val register_init_code : unit t -> unit t
178175

179176
val init_code : context -> unit t
180177

181-
val register_string : string -> int t
182-
183178
val register_fragment : string -> (unit -> Javascript.expression) -> unit t
184179

185180
val get_context : context t

compiler/lib-wasm/gc_target.ml

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,8 @@ module Value = struct
514514
| StructGet (_, _, _, e')
515515
| RefCast (_, e')
516516
| RefTest (_, e')
517-
| ExternConvertAny e' -> effect_free e'
517+
| ExternConvertAny e'
518+
| AnyConvertExtern e' -> effect_free e'
518519
| BinOp (_, e1, e2)
519520
| ArrayNew (_, e1, e2)
520521
| ArrayNewData (_, _, e1, e2)
@@ -912,20 +913,12 @@ module Constant = struct
912913
let* () = register_global name { mut = false; typ = Type.value } c in
913914
return (W.GlobalGet name)
914915

915-
let str_js_utf8 s =
916+
let byte_string s =
916917
let b = Buffer.create (String.length s) in
917918
String.iter s ~f:(function
918-
| '\\' -> Buffer.add_string b "\\\\"
919-
| c -> Buffer.add_char b c);
920-
Buffer.contents b
921-
922-
let str_js_byte s =
923-
let b = Buffer.create (String.length s) in
924-
String.iter s ~f:(function
925-
| '\\' -> Buffer.add_string b "\\\\"
926919
| '\128' .. '\255' as c ->
927-
Buffer.add_string b "\\x";
928-
Buffer.add_char_hex b c
920+
Buffer.add_char b (Char.chr (0xC2 lor (Char.code c lsr 6)));
921+
Buffer.add_char b (Char.chr (0x80 lor (Char.code c land 0x3F)))
929922
| c -> Buffer.add_char b c);
930923
Buffer.contents b
931924

@@ -989,22 +982,18 @@ module Constant = struct
989982
| NativeString s ->
990983
let s =
991984
match s with
992-
| Utf (Utf8 s) -> str_js_utf8 s
993-
| Byte s -> str_js_byte s
985+
| Utf (Utf8 s) -> s
986+
| Byte s -> byte_string s
994987
in
995-
let* i = register_string s in
996988
let* x =
997-
let* name = unit_name in
998989
register_import
999-
~import_module:
1000-
(match name with
1001-
| None -> "strings"
1002-
| Some name -> name ^ ".strings")
1003-
~name:(string_of_int i)
1004-
(Global { mut = false; typ = Ref { nullable = false; typ = Any } })
990+
~import_module:"str"
991+
~name:s
992+
(Global { mut = false; typ = Ref { nullable = false; typ = Extern } })
1005993
in
1006994
let* ty = Type.js_type in
1007-
return (Const_named ("str_" ^ s), W.StructNew (ty, [ GlobalGet x ]))
995+
return
996+
(Const_named ("str_" ^ s), W.StructNew (ty, [ AnyConvertExtern (GlobalGet x) ]))
1008997
| String s ->
1009998
let* ty = Type.string_type in
1010999
if String.length s >= string_length_threshold

compiler/lib-wasm/generate.ml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,12 +1217,7 @@ module Generate (Target : Target_sig.S) = struct
12171217
in
12181218
global_context.init_code <- [];
12191219
global_context.other_fields <- List.rev_append functions global_context.other_fields;
1220-
let js_code =
1221-
List.rev global_context.strings, StringMap.bindings global_context.fragments
1222-
in
1223-
global_context.string_count <- 0;
1224-
global_context.strings <- [];
1225-
global_context.string_index <- StringMap.empty;
1220+
let js_code = StringMap.bindings global_context.fragments in
12261221
global_context.fragments <- StringMap.empty;
12271222
toplevel_name, js_code
12281223

compiler/lib-wasm/generate.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ val f :
2727
-> live_vars:int array
2828
-> in_cps:Effects.in_cps
2929
-> deadcode_sentinal:Code.Var.t
30-
-> Wasm_ast.var * (string list * (string * Javascript.expression) list)
30+
-> Wasm_ast.var * (string * Javascript.expression) list
3131

3232
val add_start_function : context:Code_generation.context -> Wasm_ast.var -> unit
3333

compiler/lib-wasm/initialize_locals.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ let rec scan_expression ctx e =
4747
| RefTest (_, e')
4848
| Br_on_cast (_, _, _, e')
4949
| Br_on_cast_fail (_, _, _, e')
50-
| Br_on_null (_, e') -> scan_expression ctx e'
51-
| ExternConvertAny e' -> scan_expression ctx e'
50+
| Br_on_null (_, e')
51+
| ExternConvertAny e'
52+
| AnyConvertExtern e' -> scan_expression ctx e'
5253
| BinOp (_, e', e'')
5354
| ArrayNew (_, e', e'')
5455
| ArrayNewData (_, _, e', e'')

compiler/lib-wasm/link.ml

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -314,22 +314,17 @@ let trim_semi s =
314314
type unit_data =
315315
{ unit_name : string
316316
; unit_info : Unit_info.t
317-
; strings : string list
318317
; fragments : (string * Javascript.expression) list
319318
}
320319

321320
let info_to_sexp ~predefined_exceptions ~build_info ~unit_data =
322321
let add nm skip v rem = if skip then rem else Sexp.List (Atom nm :: v) :: rem in
323322
let units =
324323
List.map
325-
~f:(fun { unit_name; unit_info; strings; fragments } ->
324+
~f:(fun { unit_name; unit_info; fragments } ->
326325
Sexp.List
327326
(Unit_info.to_sexp unit_info
328327
|> add "name" false [ Atom unit_name ]
329-
|> add
330-
"strings"
331-
(List.is_empty strings)
332-
(List.map ~f:(fun s -> Sexp.Atom s) strings)
333328
|> add
334329
"fragments"
335330
(List.is_empty fragments)
@@ -366,9 +361,6 @@ let info_from_sexp info =
366361
let unit_name =
367362
u |> member "name" |> Option.value ~default:[] |> single string
368363
in
369-
let strings =
370-
u |> member "strings" |> Option.value ~default:[] |> List.map ~f:string
371-
in
372364
let fragments =
373365
u
374366
|> member "fragments"
@@ -383,7 +375,7 @@ let info_from_sexp info =
383375
, let lex = Parse_js.Lexer.of_string (to_string e) in
384376
Parse_js.parse_expr lex ))*)
385377
in
386-
{ unit_name; unit_info; strings; fragments })
378+
{ unit_name; unit_info; fragments })
387379
in
388380
build_info, predefined_exceptions, unit_data
389381

@@ -444,28 +436,13 @@ let build_runtime_arguments
444436
let generated_js =
445437
List.concat
446438
@@ List.map
447-
~f:(fun (unit_name, (strings, fragments)) ->
439+
~f:(fun (unit_name, fragments) ->
448440
let name s =
449441
match unit_name with
450442
| None -> s
451443
| Some nm -> nm ^ "." ^ s
452444
in
453-
let strings =
454-
if List.is_empty strings
455-
then []
456-
else
457-
[ ( name "strings"
458-
, Javascript.EArr
459-
(List.map
460-
~f:(fun s ->
461-
Javascript.Element (EStr (Utf8_string.of_string_exn s)))
462-
strings) )
463-
]
464-
in
465-
let fragments =
466-
if List.is_empty fragments then [] else [ name "fragments", obj fragments ]
467-
in
468-
strings @ fragments)
445+
if List.is_empty fragments then [] else [ name "fragments", obj fragments ])
469446
generated_js
470447
in
471448
let generated_js =
@@ -821,8 +798,8 @@ let link ~output_file ~linkall ~enable_source_maps ~files =
821798
let generated_js =
822799
List.concat
823800
@@ List.map files ~f:(fun (_, (_, units)) ->
824-
List.map units ~f:(fun { unit_name; strings; fragments; _ } ->
825-
Some unit_name, (strings, fragments)))
801+
List.map units ~f:(fun { unit_name; fragments; _ } ->
802+
Some unit_name, fragments))
826803
in
827804
let runtime_args =
828805
let js =

compiler/lib-wasm/link.mli

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ end
3838
type unit_data =
3939
{ unit_name : string
4040
; unit_info : Unit_info.t
41-
; strings : string list
4241
; fragments : (string * Javascript.expression) list
4342
}
4443

@@ -55,8 +54,7 @@ val build_runtime_arguments :
5554
-> separate_compilation:bool
5655
-> missing_primitives:string list
5756
-> wasm_dir:string
58-
-> generated_js:
59-
(string option * (string list * (string * Javascript.expression) list)) list
57+
-> generated_js:(string option * (string * Javascript.expression) list) list
6058
-> unit
6159
-> Javascript.expression
6260

0 commit comments

Comments
 (0)