Skip to content

Commit 1899e2a

Browse files
committed
implement Lazy with CamlinternalLazy
1 parent 186fd00 commit 1899e2a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+502
-1145
lines changed

jscomp/runtime/caml_module.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ let init_mod = (loc: (string, int, int), shape: shape) => {
5353
let rec loop = (shape: shape, struct_: Obj.t, idx) =>
5454
switch shape {
5555
| Function => set_field(struct_, idx, Obj.magic(undef_module))
56-
| Lazy => set_field(struct_, idx, Obj.magic(lazy undef_module))
56+
| Lazy => set_field(struct_, idx, Obj.magic(undef_module))
5757
| Class =>
5858
set_field(
5959
struct_,

jscomp/stdlib-406/camlinternalLazy.res

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type t<'a> = {
3535
%%private(external fnToVal: ((. unit) => 'a) => 'a = "%identity")
3636
%%private(external valToFn: 'a => (. unit) => 'a = "%identity")
3737
%%private(external castToConcrete: lazy_t<'a> => t<'a> = "%identity")
38+
%%private(external castFromConcrete: t<'a> => lazy_t<'a> = "%identity")
3839

3940
let is_val = (type a, l: lazy_t<a>): bool => castToConcrete(l).tag
4041

@@ -90,3 +91,19 @@ let force_val = (type a, lzv: lazy_t<a>): a => {
9091
force_val_lazy_block(lzv)
9192
}
9293
}
94+
95+
let from_fun = (type a, closure: (. unit) => a): lazy_t<a> => {
96+
let blk = {
97+
tag: false,
98+
value: fnToVal(closure),
99+
}
100+
castFromConcrete(blk)
101+
}
102+
103+
let from_val = (type a, value: a): lazy_t<a> => {
104+
let blk = {
105+
tag: true,
106+
value: value,
107+
}
108+
castFromConcrete(blk)
109+
}

jscomp/stdlib-406/camlinternalLazy.resi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ let force: lazy_t<'a> => 'a
2525
let force_val: lazy_t<'a> => 'a
2626

2727
let is_val: lazy_t<'a> => bool
28+
29+
let from_fun: ((. unit) => 'a) => lazy_t<'a>
30+
31+
let from_val: 'a => lazy_t<'a>

jscomp/stdlib-406/hashtbl.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ let randomized = ref(randomized_default)
5656
let randomize = () => randomized := true
5757
let is_randomized = () => randomized.contents
5858

59-
let prng = lazy Random.State.make_self_init()
59+
let prng = Lazy.from_fun(() => Random.State.make_self_init())
6060

6161
/* Creating a fresh, empty table */
6262

jscomp/stdlib-406/lazy.res

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ external force: t<'a> => 'a = "%lazy_force"
5555

5656
let force_val = CamlinternalLazy.force_val
5757

58-
let from_fun = f => lazy f()
58+
let from_fun = f => CamlinternalLazy.from_fun((. ) => f())
5959

60-
let from_val = v => lazy v
60+
let from_val = v => CamlinternalLazy.from_val(v)
6161

6262
let is_val = CamlinternalLazy.is_val
6363

jscomp/stdlib-406/stream.res

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,13 @@ let iapp = (i, s) => Some({count: 0, data: Sapp(data(i), data(s))})
218218
let icons = (i, s) => Some({count: 0, data: Scons(i, data(s))})
219219
let ising = i => Some({count: 0, data: Scons(i, Sempty)})
220220

221-
let lapp = (f, s) => Some({count: 0, data: Slazy(lazy Sapp(data(f()), data(s)))})
221+
let lapp = (f, s) => Some({count: 0, data: Slazy(Lazy.from_fun(() => Sapp(data(f()), data(s))))})
222222

223-
let lcons = (f, s) => Some({count: 0, data: Slazy(lazy Scons(f(), data(s)))})
224-
let lsing = f => Some({count: 0, data: Slazy(lazy Scons(f(), Sempty))})
223+
let lcons = (f, s) => Some({count: 0, data: Slazy(Lazy.from_fun(() => Scons(f(), data(s))))})
224+
let lsing = f => Some({count: 0, data: Slazy(Lazy.from_fun(() => Scons(f(), Sempty)))})
225225

226226
let sempty = None
227-
let slazy = f => Some({count: 0, data: Slazy(lazy data(f()))})
227+
let slazy = f => Some({count: 0, data: Slazy(Lazy.from_fun(() => data(f())))})
228228

229229
/* For debugging use */
230230

jscomp/test/build.ninja

Lines changed: 1 addition & 2 deletions
Large diffs are not rendered by default.

jscomp/test/demo_int_map.js

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/test/ext_filename_test.js

Lines changed: 4 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/test/ext_filename_test.res

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type t = [
3232
| #Dir(string)
3333
]
3434

35-
let cwd = lazy Sys.getcwd()
35+
let cwd = Lazy.from_fun(() => Sys.getcwd())
3636

3737
let \"//" = Filename.concat
3838

@@ -211,7 +211,7 @@ let rec find_root_filename = (~cwd, filename) =>
211211

212212
let find_package_json_dir = cwd => find_root_filename(~cwd, Test_literals.bsconfig_json)
213213

214-
let package_dir = lazy find_package_json_dir(Lazy.force(cwd))
214+
let package_dir = Lazy.from_fun(() => find_package_json_dir(Lazy.force(cwd)))
215215

216216
let module_name_of_file = file =>
217217
String.capitalize_ascii(\"@@"(Filename.chop_extension, Filename.basename(file)))

jscomp/test/gpr_3697_test.js

Lines changed: 3 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

jscomp/test/gpr_3697_test.res

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
type rec t<'a> = Fix(lazy_t<t<'a>>)
22

3-
let rec fix = () => Fix(lazy fix())
3+
let rec fix = () => Fix(Lazy.from_fun(fix))
44

55
let rec unfixLeak = (Fix(f)) => \"@@"(unfixLeak, Lazy.force(f))
66

77
let unfix = p =>
88
while true {
99
p :=
1010
switch p.contents {
11-
| Fix(lazy h) => h
11+
| Fix(h) => Lazy.force(h)
1212
}
1313
}
1414
/* ;; unfixLeak (fix ()) */

0 commit comments

Comments
 (0)