Skip to content

Commit bc5b79d

Browse files
author
Hongbo Zhang
committed
bug fix 631
1 parent 0073b81 commit bc5b79d

File tree

9 files changed

+53
-9
lines changed

9 files changed

+53
-9
lines changed

jscomp/js_exp_make.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,16 @@ let index ?comment (e0 : t) e1 : t =
314314
List.nth l (Int32.to_int e1) (* Float i -- should not appear here *)
315315
| _ -> { expression_desc = Access (e0, int ?comment e1); comment = None}
316316

317+
318+
let index_addr ?comment ~yes ~no (e0 : t) e1 : t =
319+
match e0.expression_desc with
320+
| Array (l,_mutable_flag) when no_side_effect e0 ->
321+
no
322+
| Caml_block (l,_mutable_flag, _, _) when no_side_effect e0 ->
323+
no
324+
| _ ->
325+
yes ({ expression_desc = Access (e0, int ?comment e1); comment = None} : t)
326+
317327
let call ?comment ~info e0 args : t =
318328
{expression_desc = Call(e0,args,info); comment }
319329

jscomp/js_exp_make.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ val js_global_dot : ?comment:string -> string -> string -> t
145145

146146
val index : ?comment:string -> t -> Int32.t -> t
147147

148+
(** if the expression is a temporay block which has no side effect,
149+
write to it does not really make sense, optimize it away *)
150+
val index_addr : ?comment:string -> yes:(t -> t) -> no:t -> t -> Js_op.jsint -> t
148151

149152
val assign : binary_op
150153

jscomp/js_of_lam_block.ml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,16 @@ let field field_info e i =
5757
-> E.index ~comment:s e i
5858

5959

60-
let set_field field_info e i e0 =
61-
let v =
60+
61+
let set_field field_info e i e0 =
62+
let comment =
6263
match field_info with
6364
| Lambda.Fld_set_na
64-
->
65-
E.index e i
66-
| Fld_record_set s ->
67-
E.index ~comment:s e i in
68-
E.assign v e0
65+
-> None
66+
| Fld_record_set s -> Some (s)
67+
in (* see GPR#631*)
68+
E.index_addr ?comment e i ~no:e0 ~yes:(fun v -> E.assign v e0)
69+
6970

7071

7172

jscomp/lam_compile_group.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ let lambda_as_module
422422
(lam : Lambda.lambda) =
423423
begin
424424
Js_config.set_current_file filename ;
425-
Js_config.iset_debug_file "optional_ffi_test.ml";
425+
Js_config.iset_debug_file "jsoo_485_test.ml";
426426
let lambda_output = compile ~filename output_prefix false env sigs lam in
427427
let (//) = Filename.concat in
428428
let basename =

jscomp/test/.depend

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,10 @@ js_obj_test.cmj : mt.cmi
334334
js_obj_test.cmx : mt.cmx
335335
js_val.cmj :
336336
js_val.cmx :
337+
jsoo_485.cmj :
338+
jsoo_485.cmx :
339+
jsoo_485_test.cmj :
340+
jsoo_485_test.cmx :
337341
largest_int_flow.cmj :
338342
largest_int_flow.cmx :
339343
lazy_test.cmj : mt.cmi ../stdlib/lazy.cmi
@@ -1086,6 +1090,10 @@ js_obj_test.cmo : mt.cmi
10861090
js_obj_test.cmj : mt.cmj
10871091
js_val.cmo :
10881092
js_val.cmj :
1093+
jsoo_485.cmo :
1094+
jsoo_485.cmj :
1095+
jsoo_485_test.cmo :
1096+
jsoo_485_test.cmj :
10891097
largest_int_flow.cmo :
10901098
largest_int_flow.cmj :
10911099
lazy_test.cmo : mt.cmi ../stdlib/lazy.cmi

jscomp/test/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ OTHERS := a test_ari test_export2 test_internalOO test_obj_simple_ffi test_scope
5959
optional_ffi_test poly_variant_test \
6060
bs_rest_test infer_type_test fs_test module_as_function\
6161
test_case_set test_mutliple string_bound_get_test inline_string_test\
62-
ppx_this_obj_test unsafe_obj_external gpr_627_test
62+
ppx_this_obj_test unsafe_obj_external gpr_627_test jsoo_485_test
6363

6464

6565
SOURCE_LIST := js_dyn $(OTHERS)

jscomp/test/jsoo_485.ml

Whitespace-only changes.

jscomp/test/jsoo_485_test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
4+
function f() {
5+
/* Some */[3];
6+
return /* () */0;
7+
}
8+
9+
exports.f = f;
10+
/* Not a pure module */

jscomp/test/jsoo_485_test.ml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
let f () =
2+
(ref None) := Some 3
3+
4+
5+
(* Uncaught ReferenceError: Invalid left-hand side in assignment *)
6+
(*
7+
function f() {
8+
/* None */0 = /* Some */[3];
9+
return /* () */0;
10+
}*)
11+
12+
let () = f ()

0 commit comments

Comments
 (0)