Skip to content

Commit

Permalink
implement Lazy with CamlinternalLazy
Browse files Browse the repository at this point in the history
  • Loading branch information
namenu committed Aug 20, 2023
1 parent cfafe56 commit 26acdbe
Show file tree
Hide file tree
Showing 31 changed files with 210 additions and 853 deletions.
2 changes: 1 addition & 1 deletion jscomp/runtime/caml_module.res
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ let init_mod = (loc: (string, int, int), shape: shape) => {
let rec loop = (shape: shape, struct_: Obj.t, idx) =>
switch shape {
| Function => set_field(struct_, idx, Obj.magic(undef_module))
| Lazy => set_field(struct_, idx, Obj.magic(lazy undef_module))
| Lazy => set_field(struct_, idx, Obj.magic(undef_module))
| Class =>
set_field(
struct_,
Expand Down
17 changes: 17 additions & 0 deletions jscomp/stdlib-406/camlinternalLazy.res
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type t<'a> = {
%%private(external fnToVal: ((. unit) => 'a) => 'a = "%identity")
%%private(external valToFn: 'a => (. unit) => 'a = "%identity")
%%private(external castToConcrete: lazy_t<'a> => t<'a> = "%identity")
%%private(external castFromConcrete: t<'a> => lazy_t<'a> = "%identity")

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

Expand Down Expand Up @@ -90,3 +91,19 @@ let force_val = (type a, lzv: lazy_t<a>): a => {
force_val_lazy_block(lzv)
}
}

let from_fun = (type a, closure: (. unit) => a): lazy_t<a> => {
let blk = {
tag: false,
value: fnToVal(closure),
}
castFromConcrete(blk)
}

let from_val = (type a, value: a): lazy_t<a> => {
let blk = {
tag: true,
value: value,
}
castFromConcrete(blk)
}
4 changes: 4 additions & 0 deletions jscomp/stdlib-406/camlinternalLazy.resi
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ let force: lazy_t<'a> => 'a
let force_val: lazy_t<'a> => 'a

let is_val: lazy_t<'a> => bool

let from_fun: ((. unit) => 'a) => lazy_t<'a>

let from_val: 'a => lazy_t<'a>
2 changes: 1 addition & 1 deletion jscomp/stdlib-406/hashtbl.res
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ let randomized = ref(randomized_default)
let randomize = () => randomized := true
let is_randomized = () => randomized.contents

let prng = lazy Random.State.make_self_init()
let prng = Lazy.from_fun(() => Random.State.make_self_init())

/* Creating a fresh, empty table */

Expand Down
4 changes: 2 additions & 2 deletions jscomp/stdlib-406/lazy.res
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ external force: t<'a> => 'a = "%lazy_force"

let force_val = CamlinternalLazy.force_val

let from_fun = f => lazy f()
let from_fun = f => CamlinternalLazy.from_fun((. ) => f())

let from_val = v => lazy v
let from_val = v => CamlinternalLazy.from_val(v)

let is_val = CamlinternalLazy.is_val

Expand Down
8 changes: 4 additions & 4 deletions jscomp/stdlib-406/stream.res
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,13 @@ let iapp = (i, s) => Some({count: 0, data: Sapp(data(i), data(s))})
let icons = (i, s) => Some({count: 0, data: Scons(i, data(s))})
let ising = i => Some({count: 0, data: Scons(i, Sempty)})

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

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

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

/* For debugging use */

Expand Down
3 changes: 1 addition & 2 deletions jscomp/test/build.ninja

Large diffs are not rendered by default.

14 changes: 4 additions & 10 deletions jscomp/test/ext_filename_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions jscomp/test/ext_filename_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type t = [
| #Dir(string)
]

let cwd = lazy Sys.getcwd()
let cwd = Lazy.from_fun(() => Sys.getcwd())

let \"//" = Filename.concat

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

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

let package_dir = lazy find_package_json_dir(Lazy.force(cwd))
let package_dir = Lazy.from_fun(() => find_package_json_dir(Lazy.force(cwd)))

let module_name_of_file = file =>
String.capitalize_ascii(\"@@"(Filename.chop_extension, Filename.basename(file)))
Expand Down
9 changes: 3 additions & 6 deletions jscomp/test/gpr_3697_test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions jscomp/test/gpr_3697_test.res
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
type rec t<'a> = Fix(lazy_t<t<'a>>)

let rec fix = () => Fix(lazy fix())
let rec fix = () => Fix(Lazy.from_fun(fix))

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

let unfix = p =>
while true {
p :=
switch p.contents {
| Fix(lazy h) => h
| Fix(h) => Lazy.force(h)
}
}
/* ;; unfixLeak (fix ()) */
Expand Down
Loading

0 comments on commit 26acdbe

Please sign in to comment.