Skip to content

Commit 6b1af98

Browse files
authored
Merge pull request #4384 from BuckleScript/tweak_lazy_encoding
tweak lazy encoding
2 parents 193699b + 9e6e453 commit 6b1af98

34 files changed

+174
-134
lines changed

jscomp/core/lam_convert.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,18 +278,18 @@ let lam_prim ~primitive:( p : Lambda.primitive) ~args loc : Lam.t =
278278
begin match args with
279279
| [Lvar _ | Lconst _ | Lfunction _ as result ] ->
280280
let args =
281-
[ Lam.const (Const_string "done") ;
281+
[ Lam.const Const_js_true ;
282282
result
283283
] in
284-
prim ~primitive:(Pmakeblock (tag,Blk_record [|"RE_LAZY";"value"|],Mutable)) ~args loc
284+
prim ~primitive:(Pmakeblock (tag,Blk_record [|"RE_LAZY_DONE";"value"|],Mutable)) ~args loc
285285
| [computation] ->
286286
let args =
287-
[ Lam.const (Const_string "todo") ;
287+
[ Lam.const Const_js_false ;
288288
(* FIXME: arity 0 does not get proper supported*)
289289
prim ~primitive:(Pjs_fn_make 0) ~args:[Lam.function_ ~arity:1 ~params:[Ident.create "param"] ~body:computation]
290290
loc
291291
] in
292-
prim ~primitive:(Pmakeblock (tag,Blk_record [|"RE_LAZY";"value"|],Mutable)) ~args loc
292+
prim ~primitive:(Pmakeblock (tag,Blk_record [|"RE_LAZY_DONE";"value"|],Mutable)) ~args loc
293293

294294
| _ -> assert false
295295
end

jscomp/main/builtin_cmj_datasets.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(* 41e7f158a42bd8f0a232242d88578e11 *)
1+
(* 22680a52333cb2d8603ed9cb8b316240 *)
22
let module_names : string array = Obj.magic (
33
"Gc" (* 220 *),
44
"Js" (* 23 *),
@@ -151,7 +151,7 @@ let module_data : string array = Obj.magic (
151151
(* Sys *)"\132\149\166\190\000\000\000\139\000\000\000 \000\000\000n\000\000\000d\160\224\176&cygwin\144@\144\146C\176*getenv_opt\144\160\160A@@@\176*set_signal\144\160\160B@@@\176+catch_break\144\160\160A@@@\1767enable_runtime_warnings\144\160\160A@@@\1768runtime_warnings_enabled\144\160\160A@@@A",
152152
(* Belt *)"\132\149\166\190\000\000\000\003\000\000\000\001\000\000\000\003\000\000\000\003\160\128A",
153153
(* Char *)"\132\149\166\190\000\000\000\223\000\000\000<\000\000\000\197\000\000\000\186\160\b\000\000 \000\176#chr\144\160\160A@@@\176%equal\144\160\160B@@@\176'compare\144\160\160B@@\144\180B\160\176\001\004\001\"c1@\160\176\001\004\002\"c2@@\151\176I\160\144\004\t\160\144\004\b@\176\1922stdlib-406/char.ml\000K\001\n\001\001\n\021\192\004\002\000K\001\n\001\001\n&@\176'escaped\144\160\160A@@@\176)lowercase\144\160\160A@@@\176)uppercase\144\160\160A@@@\176/lowercase_ascii\144\160\160A@@@\176/uppercase_ascii\144\160\160A@@@A",
154-
(* Lazy *)"\132\149\166\190\000\000\001$\000\000\000O\000\000\001\002\000\000\000\241\160\240\176&is_val\144\160\160A@@@\176(from_fun\144\160\160A@@\144\180A\160\176\001\003\239!f@@\151\176\176\001\000\246\146\160'RE_LAZY%valueA\160\146\146$todo\160\180@@\147\192\144\004\018\160\146A@\176\1922stdlib-406/lazy.ml|\001\t\175\001\t\197\192\004\002|\001\t\175\001\t\203@@@\004\004\176(from_val\144\160\160A@@\144\180A\160\176\001\003\241!v@@\151\176\176\001\000\246\146\160'RE_LAZY%valueA\160\146\146$done\160\144\004\015@\176\192\004\027~\001\t\205\001\t\227\192\004\028~\001\t\205\001\t\228@\176)force_val\144\160\160A@@@\176+lazy_is_val\144\004D@\176-lazy_from_fun\144\004B@\176-lazy_from_val\144\004%@A",
154+
(* Lazy *)"\132\149\166\190\000\000\001$\000\000\000K\000\000\000\252\000\000\000\235\160\240\176&is_val\144\160\160A@@@\176(from_fun\144\160\160A@@\144\180A\160\176\001\003\239!f@@\151\176\176\001\000\246\146\160,RE_LAZY_DONE%valueA\160\146C\160\180@@\147\192\144\004\016\160\146A@\176\1922stdlib-406/lazy.ml|\001\t\175\001\t\197\192\004\002|\001\t\175\001\t\203@@@\004\004\176(from_val\144\160\160A@@\144\180A\160\176\001\003\241!v@@\151\176\176\001\000\246\146\160,RE_LAZY_DONE%valueA\160\146B\160\144\004\r@\176\192\004\025~\001\t\205\001\t\227\192\004\026~\001\t\205\001\t\228@\176)force_val\144\160\160A@@@\176+lazy_is_val\144\004@@\176-lazy_from_fun\144\004>@\176-lazy_from_val\144\004#@A",
155155
(* List *)"\132\149\166\190\000\000\003\134\000\000\001\020\000\000\003\135\000\000\003T\160\b\000\000\208\000\176\"hd\144\160\160A@@@\176\"tl\144\160\160A@@@\176#map\144\160\160B@@@\176#mem\144\160\160B@@@\176#nth\144\160\160B@@@\176#rev\144\160\160A@@@\176$assq\144\160\160B@@@\176$cons\144\160\160B@@\144\180B\160\176\001\003\241!a@\160\176\001\003\242!l@@\151\176\176@\160\"::A@\160\144\004\012\160\144\004\011@\176\1922stdlib-406/list.mlX\001\004\193\001\004\208\192\004\002X\001\004\193\001\004\212@\176$find\144\160\160B@@@\176$init\144\160\160B@@@\176$iter\144\160\160B@@@\176$map2\144\160\160C@@@\176$mapi\144\160\160B@@@\176$memq\144\160\160B@@@\176$sort\144\160\160B@@@\176%assoc\144\160\160B@@@\176%iter2\144\160\160C@@@\176%iteri\144\160\160B@@@\176%merge\144\160\160C@@@\176%split\144\160\160A@@@\176&append\144\160\160B@@@\176&concat\144\160\160A@@@\176&exists\144\160\160B@@@\176&filter\144\160\160A\160A@@@\176&length\144\160\160A@@@\176'combine\144\160\160B@@@\176'exists2\144\160\160C@@@\176'flatten\144\004\031@\176'for_all\144\160\160B@@@\176'nth_opt\144\160\160B@@@\176'rev_map\144\160\160B@@@\176(assq_opt\144\160\160B@@@\176(find_all\144\004,@\176(find_opt\144\160\160B@@@\176(for_all2\144\160\160C@@@\176(mem_assq\144\160\160B@@@\176(rev_map2\144\160\160C@@@\176)assoc_opt\144\160\160B@@@\176)fast_sort\144\004u@\176)fold_left\144\160\160C@@@\176)mem_assoc\144\160\160B@@@\176)partition\144\160\160B@@@\176)sort_uniq\144\160\160B@@@\176*fold_left2\144\160\160D@@@\176*fold_right\144\160\160C@@@\176*rev_append\144\160\160B@@@\176+fold_right2\144\160\160D@@@\176+remove_assq\144\160\160B@@@\176+stable_sort\144\004\165@\176,remove_assoc\144\160\160B@@@\176/compare_lengths\144\160\160B@@@\1763compare_length_with\144\160\160B@@@A",
156156
(* Node *)"\132\149\166\190\000\000\000\016\000\000\000\007\000\000\000\020\000\000\000\019\160\144\176$test\144\160\160A@@@A",
157157
(* Sort *)"\132\149\166\190\000\000\000,\000\000\000\017\000\000\0004\000\000\0001\160\176\176$list\144\160\160B@@@\176%array\144\160\160B@@@\176%merge\144\160\160C@@@A",

jscomp/runtime/release.ninja

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ build runtime/caml_chrome_debugger.cmj : cc_cmi runtime/caml_chrome_debugger.ml
2323
build runtime/caml_chrome_debugger.cmi : cc runtime/caml_chrome_debugger.mli | runtime/bs_stdlib_mini.cmi runtime/caml_obj_extern.cmj runtime/js.cmi runtime/js.cmj
2424
build runtime/caml_float.cmj : cc_cmi runtime/caml_float.ml | runtime/caml_float.cmi runtime/caml_float_extern.cmj
2525
build runtime/caml_float.cmi : cc runtime/caml_float.mli | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
26-
build runtime/caml_format.cmj : cc_cmi runtime/caml_format.ml | runtime/caml_char.cmj runtime/caml_float.cmj runtime/caml_float_extern.cmj runtime/caml_format.cmi runtime/caml_int32.cmj runtime/caml_int64.cmj runtime/caml_int64_extern.cmj runtime/caml_nativeint_extern.cmj runtime/caml_string_extern.cmj
26+
build runtime/caml_format.cmj : cc_cmi runtime/caml_format.ml | runtime/caml_char.cmj runtime/caml_float.cmj runtime/caml_float_extern.cmj runtime/caml_format.cmi runtime/caml_int64.cmj runtime/caml_int64_extern.cmj runtime/caml_nativeint_extern.cmj runtime/caml_string_extern.cmj
2727
build runtime/caml_format.cmi : cc runtime/caml_format.mli | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
2828
build runtime/caml_gc.cmj : cc_cmi runtime/caml_gc.ml | runtime/caml_gc.cmi
2929
build runtime/caml_gc.cmi : cc runtime/caml_gc.mli | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
3030
build runtime/caml_hash.cmj : cc_cmi runtime/caml_hash.ml | runtime/caml_hash.cmi runtime/caml_hash_primitive.cmj runtime/caml_nativeint_extern.cmj runtime/caml_obj_extern.cmj runtime/js.cmj
3131
build runtime/caml_hash.cmi : cc runtime/caml_hash.mli | runtime/bs_stdlib_mini.cmi runtime/caml_obj_extern.cmj runtime/js.cmi runtime/js.cmj
32-
build runtime/caml_hash_primitive.cmj : cc_cmi runtime/caml_hash_primitive.ml | runtime/caml_char.cmj runtime/caml_hash_primitive.cmi runtime/caml_int32.cmj runtime/caml_nativeint_extern.cmj runtime/caml_string_extern.cmj
32+
build runtime/caml_hash_primitive.cmj : cc_cmi runtime/caml_hash_primitive.ml | runtime/caml_char.cmj runtime/caml_hash_primitive.cmi runtime/caml_nativeint_extern.cmj runtime/caml_string_extern.cmj
3333
build runtime/caml_hash_primitive.cmi : cc runtime/caml_hash_primitive.mli | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
3434
build runtime/caml_int32.cmj : cc_cmi runtime/caml_int32.ml | runtime/caml_int32.cmi runtime/caml_nativeint_extern.cmj
3535
build runtime/caml_int32.cmi : cc runtime/caml_int32.mli | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
36-
build runtime/caml_int64.cmj : cc_cmi runtime/caml_int64.ml | runtime/caml_float.cmj runtime/caml_float_extern.cmj runtime/caml_int32.cmj runtime/caml_int64.cmi runtime/caml_nativeint_extern.cmj runtime/caml_primitive.cmj runtime/caml_string_extern.cmj runtime/js.cmj
36+
build runtime/caml_int64.cmj : cc_cmi runtime/caml_int64.ml | runtime/caml_float.cmj runtime/caml_float_extern.cmj runtime/caml_int64.cmi runtime/caml_nativeint_extern.cmj runtime/caml_primitive.cmj runtime/caml_string_extern.cmj runtime/js.cmj
3737
build runtime/caml_int64.cmi : cc runtime/caml_int64.mli | runtime/bs_stdlib_mini.cmi runtime/js.cmi runtime/js.cmj
3838
build runtime/caml_io.cmj : cc_cmi runtime/caml_io.ml | runtime/caml_io.cmi runtime/caml_string_extern.cmj runtime/caml_undefined_extern.cmj runtime/curry.cmj runtime/js.cmj
3939
build runtime/caml_io.cmi : cc runtime/caml_io.mli | runtime/bs_stdlib_mini.cmi runtime/caml_undefined_extern.cmj runtime/js.cmi runtime/js.cmj

jscomp/stdlib-406/camlinternalLazy.ml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,22 @@
2222
* along with this program; if not, write to the Free Software
2323
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
2424

25-
25+
[@@@bs.config { flags = [|"-bs-no-cross-module-opt" |]}]
26+
2627
(* Internals of forcing lazy values. *)
2728
type 'a t = {
28-
mutable tag : string [@bs.as "RE_LAZY"] ;
29+
mutable tag : bool [@bs.as "RE_LAZY_DONE"] ;
2930
(* Invariant: name *)
3031
mutable value : 'a (* its type is ['a] or [unit -> 'a ] *)
3132
}
3233

33-
let%private status_done = "done" (* used to be forward_tag in native *)
34+
3435
external%private fnToVal : (unit -> 'a [@bs]) -> 'a = "%identity"
3536
external%private valToFn : 'a -> (unit -> 'a [@bs]) = "%identity"
3637
external%private castToConcrete : 'a lazy_t -> 'a t = "%identity"
3738

3839
let is_val (type a ) (l : a lazy_t) : bool =
39-
((castToConcrete l ).tag = status_done)
40+
(castToConcrete l ).tag
4041

4142

4243

@@ -46,7 +47,7 @@ let%private forward_with_closure (type a ) (blk : a t) (closure : unit -> a [@bs
4647
let result = closure () [@bs] in
4748
(* do set_field BEFORE set_tag *)
4849
blk.value <- result;
49-
blk.tag<- status_done;
50+
blk.tag<- true;
5051
result
5152

5253

@@ -73,15 +74,15 @@ let%private force_val_lazy_block (type a ) (blk : a t) : a =
7374

7475
let force (type a ) (lzv : a lazy_t) : a =
7576
let lzv = (castToConcrete lzv : _ t) in
76-
if lzv.tag = status_done then lzv.value else
77+
if lzv.tag then lzv.value else
7778
force_lazy_block lzv
7879

7980

8081

8182

8283
let force_val (type a) (lzv : a lazy_t) : a =
8384
let lzv : _ t = castToConcrete lzv in
84-
if lzv.tag = status_done then lzv.value else
85+
if lzv.tag then lzv.value else
8586
force_val_lazy_block lzv
8687

8788

jscomp/test/build.ninja

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ build test/label_uncurry.cmi test/label_uncurry.cmj : cc test/label_uncurry.ml |
405405
build test/large_obj_test.cmi test/large_obj_test.cmj : cc test/large_obj_test.ml | $stdlib
406406
build test/large_record_duplication_test.cmi test/large_record_duplication_test.cmj : cc test/large_record_duplication_test.ml | test/mt.cmj $stdlib
407407
build test/largest_int_flow.cmi test/largest_int_flow.cmj : cc test/largest_int_flow.ml | $stdlib
408+
build test/lazy_demo.cmi test/lazy_demo.cmj : cc test/lazy_demo.re | $stdlib
408409
build test/lazy_test.cmi test/lazy_test.cmj : cc test/lazy_test.ml | test/mt.cmj $stdlib
409410
build test/lexer_test.cmi test/lexer_test.cmj : cc test/lexer_test.ml | test/arith_lexer.cmj test/arith_parser.cmj test/arith_syntax.cmj test/mt.cmj test/number_lexer.cmj $stdlib
410411
build test/lib_js_test.cmi test/lib_js_test.cmj : cc test/lib_js_test.ml | test/mt.cmj $stdlib

jscomp/test/ext_filename_test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var node_parent = "..";
2525
var node_current = ".";
2626

2727
var cwd = {
28-
RE_LAZY: "todo",
28+
RE_LAZY_DONE: false,
2929
value: (function () {
3030
return Caml_sys.caml_sys_getcwd(undefined);
3131
})
@@ -217,7 +217,7 @@ function find_package_json_dir(cwd) {
217217
}
218218

219219
var package_dir = {
220-
RE_LAZY: "todo",
220+
RE_LAZY_DONE: false,
221221
value: (function () {
222222
var cwd$1 = CamlinternalLazy.force(cwd);
223223
return find_root_filename(cwd$1, Test_literals.bsconfig_json);

jscomp/test/gpr_3697_test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var CamlinternalLazy = require("../../lib/js/camlinternalLazy.js");
44

55
function fix(param) {
66
return /* Fix */[{
7-
RE_LAZY: "todo",
7+
RE_LAZY_DONE: false,
88
value: (function () {
99
return fix(undefined);
1010
})

jscomp/test/hamming_test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ function pr(param) {
119119

120120
function map(f, l) {
121121
return {
122-
RE_LAZY: "todo",
122+
RE_LAZY_DONE: false,
123123
value: (function () {
124124
var match = CamlinternalLazy.force(l);
125125
return /* Cons */[
@@ -132,7 +132,7 @@ function map(f, l) {
132132

133133
function merge(cmp, l1, l2) {
134134
return {
135-
RE_LAZY: "todo",
135+
RE_LAZY_DONE: false,
136136
value: (function () {
137137
var match = CamlinternalLazy.force(l1);
138138
var match$1 = CamlinternalLazy.force(l2);
@@ -184,7 +184,7 @@ function iter_interval(f, _l, _param) {
184184
}
185185

186186
var hamming = {
187-
RE_LAZY: "todo",
187+
RE_LAZY_DONE: false,
188188
value: (function () {
189189
return /* Cons */[
190190
nn1,
@@ -194,21 +194,21 @@ var hamming = {
194194
};
195195

196196
var ham2 = {
197-
RE_LAZY: "todo",
197+
RE_LAZY_DONE: false,
198198
value: (function () {
199199
return CamlinternalLazy.force(map(x2, hamming));
200200
})
201201
};
202202

203203
var ham3 = {
204-
RE_LAZY: "todo",
204+
RE_LAZY_DONE: false,
205205
value: (function () {
206206
return CamlinternalLazy.force(map(x3, hamming));
207207
})
208208
};
209209

210210
var ham5 = {
211-
RE_LAZY: "todo",
211+
RE_LAZY_DONE: false,
212212
value: (function () {
213213
return CamlinternalLazy.force(map(x5, hamming));
214214
})

jscomp/test/lazy_demo.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
3+
var CamlinternalLazy = require("../../lib/js/camlinternalLazy.js");
4+
5+
var lazy1 = {
6+
RE_LAZY_DONE: false,
7+
value: (function () {
8+
console.log("Hello, lazy");
9+
return 1;
10+
})
11+
};
12+
13+
var lazy2 = {
14+
RE_LAZY_DONE: true,
15+
value: 3
16+
};
17+
18+
console.log(lazy1, lazy2);
19+
20+
var la = CamlinternalLazy.force(lazy1);
21+
22+
var lb = CamlinternalLazy.force(lazy2);
23+
24+
console.log(la, lb);
25+
26+
exports.lazy1 = lazy1;
27+
exports.lazy2 = lazy2;
28+
exports.la = la;
29+
exports.lb = lb;
30+
/* Not a pure module */

jscomp/test/lazy_demo.re

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
let lazy1 = lazy {
2+
"Hello, lazy" -> Js.log;
3+
1
4+
};
5+
6+
let lazy2 = lazy 3 ;
7+
8+
Js.log2 (lazy1, lazy2);
9+
10+
let (lazy la, lazy lb) = (lazy1, lazy2);
11+
12+
Js.log2 (la, lb);

0 commit comments

Comments
 (0)