Skip to content

Commit e939120

Browse files
authored
Merge pull request #3764 from BuckleScript/renaming
avoid leaking magic number to user when compiling lazy evaluation
2 parents da68a82 + 87f3f2a commit e939120

30 files changed

+1050
-1162
lines changed
File renamed without changes.
File renamed without changes.

jscomp/core/lam_analysis.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ let rec no_side_effects (lam : Lam.t) : bool =
6363
| "caml_obj_dup"
6464
| "caml_array_dup"
6565
| "caml_obj_block"
66+
| "caml_lazy_make"
6667
), _ -> true
6768
| "caml_ml_open_descriptor_in", [Lconst ( (Const_int 0))] -> true
6869
| "caml_ml_open_descriptor_out",

jscomp/core/lam_constant_convert.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,12 @@ let rec convert_constant ( const : Lambda.structured_constant) : Lam_constant.t
7777
| Blk_extension_slot ->
7878
let t : Lam_tag_info.t = Blk_extension_slot in
7979
Const_block (i,t, Ext_list.map xs convert_constant )
80+
| Blk_lazy_general
81+
| Blk_lazy_forward
8082
| Blk_na ->
8183
let t : Lam_tag_info.t = Blk_na in
8284
Const_block (i,t, Ext_list.map xs convert_constant )
85+
8386
#if OCAML_VERSION =~ ">4.03.0" then
8487
| Blk_record_inlined (s,ctor,ix) ->
8588
let t : Lam_tag_info.t = Blk_record_inlined (s, ctor,ix) in

jscomp/core/lam_convert.ml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ let lam_prim ~primitive:( p : Lambda.primitive) ~args loc : Lam.t =
219219
| Blk_record s ->
220220
let info : Lam_tag_info.t = Blk_record s in
221221
prim ~primitive:(Pmakeblock (tag,info,mutable_flag)) ~args loc
222+
222223
#if OCAML_VERSION =~ ">4.03.0" then
223224
| Blk_record_inlined (s,ctor,i) ->
224225
let info : Lam_tag_info.t = Blk_record_inlined (s, ctor,i) in
@@ -243,7 +244,13 @@ let lam_prim ~primitive:( p : Lambda.primitive) ~args loc : Lam.t =
243244
#end
244245
prim ~primitive:(Pmakeblock (tag,info,mutable_flag)) ~args loc
245246
)
246-
247+
| Blk_lazy_general
248+
->
249+
prim
250+
~primitive:(Pccall {prim_name="caml_lazy_make"; prim_arity = 1; prim_native_name = ""})
251+
~args loc
252+
| Blk_lazy_forward
253+
247254
| Blk_na ->
248255
let info : Lam_tag_info.t = Blk_na in
249256
prim ~primitive:(Pmakeblock (tag,info,mutable_flag)) ~args loc

jscomp/core/lam_dispatch_primitive.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,7 @@ let translate loc (prim_name : string)
737737
| "caml_update_dummy"
738738
| "caml_obj_truncate"
739739
| "caml_lazy_make_forward"
740+
| "caml_lazy_make"
740741
->
741742
call Js_runtime_modules.obj_runtime
742743

jscomp/main/bsb_helper_main.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ let link link_byte_or_native =
6262
end
6363
#end
6464
let () =
65-
Bsb_arg.parse_exn [
65+
Bsb_helper_arg.parse_exn [
6666
"-g", Set_int dev_group ,
6767
" Set the dev group (default to be 0)"
6868
;

jscomp/runtime/caml_obj.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ let caml_obj_truncate (x : Caml_obj_extern.t) (new_size : int) =
105105

106106
let caml_lazy_make_forward x = lazy x
107107

108+
let caml_lazy_make (fn : _ -> _) =
109+
let block = Caml_obj_extern.repr [|fn|] in
110+
Caml_obj_extern.set_tag block 246 (* Obj.lazy_tag*);
111+
block
112+
113+
108114
(**
109115
For the empty dummy object, whether it's
110116
[[]] or [{}] depends on how

jscomp/runtime/caml_obj.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ val caml_obj_truncate : Caml_obj_extern.t -> int -> unit
4141

4242
val caml_lazy_make_forward : 'a -> 'a lazy_t
4343

44+
val caml_lazy_make :
45+
(t -> t) ->
46+
t
47+
4448
val caml_update_dummy : Caml_obj_extern.t -> Caml_obj_extern.t -> unit
4549

4650

jscomp/test/ext_filename_test.js

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var Bytes = require("../../lib/js/bytes.js");
77
var Curry = require("../../lib/js/curry.js");
88
var Format = require("../../lib/js/format.js");
99
var $$String = require("../../lib/js/string.js");
10+
var Caml_obj = require("../../lib/js/caml_obj.js");
1011
var Caml_sys = require("../../lib/js/caml_sys.js");
1112
var Filename = require("../../lib/js/filename.js");
1213
var Caml_bytes = require("../../lib/js/caml_bytes.js");
@@ -25,9 +26,9 @@ var node_parent = "..";
2526

2627
var node_current = ".";
2728

28-
var cwd = Block.__(246, [(function (param) {
29+
var cwd = Caml_obj.caml_lazy_make((function (param) {
2930
return Caml_sys.caml_sys_getcwd(/* () */0);
30-
})]);
31+
}));
3132

3233
function path_as_directory(x) {
3334
if (x === "" || Ext_string_test.ends_with(x, Filename.dir_sep)) {
@@ -39,15 +40,7 @@ function path_as_directory(x) {
3940

4041
function absolute_path(s) {
4142
var s$1 = s;
42-
var s$2;
43-
if (Curry._1(Filename.is_relative, s$1)) {
44-
var tag = cwd.tag | 0;
45-
s$2 = Filename.concat(tag === 250 ? cwd[0] : (
46-
tag === 246 ? CamlinternalLazy.force_lazy_block(cwd) : cwd
47-
), s$1);
48-
} else {
49-
s$2 = s$1;
50-
}
43+
var s$2 = Curry._1(Filename.is_relative, s$1) ? Filename.concat(CamlinternalLazy.force(cwd), s$1) : s$1;
5144
var aux = function (_s) {
5245
while(true) {
5346
var s = _s;
@@ -234,13 +227,10 @@ function find_package_json_dir(cwd) {
234227
return find_root_filename(cwd, Test_literals.bsconfig_json);
235228
}
236229

237-
var package_dir = Block.__(246, [(function (param) {
238-
var tag = cwd.tag | 0;
239-
var cwd$1 = tag === 250 ? cwd[0] : (
240-
tag === 246 ? CamlinternalLazy.force_lazy_block(cwd) : cwd
241-
);
230+
var package_dir = Caml_obj.caml_lazy_make((function (param) {
231+
var cwd$1 = CamlinternalLazy.force(cwd);
242232
return find_root_filename(cwd$1, Test_literals.bsconfig_json);
243-
})]);
233+
}));
244234

245235
function module_name_of_file(file) {
246236
var s = Filename.chop_extension(Curry._1(Filename.basename, file));

0 commit comments

Comments
 (0)