Skip to content

Commit fef665e

Browse files
authored
Merge pull request #4428 from BuckleScript/fix_4426
fix #4426
2 parents c0e4bb6 + 139b5f4 commit fef665e

16 files changed

+179
-26
lines changed

jscomp/syntax/ast_literal.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ let type_int64 =
134134
Ast_helper.Typ.mk
135135
(Ptyp_constr ({ txt = Lident "int64"; loc = Location.none}, []))
136136

137+
let type_float =
138+
Ast_helper.Typ.mk
139+
(Ptyp_constr ({ txt = Lident "float"; loc = Location.none}, []))
140+
137141
let type_any ?loc () =
138142
match loc with
139143
| None -> No_loc.type_any

jscomp/syntax/ast_literal.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ val type_string : core_type_lit
6262
val type_bool : core_type_lit
6363
val type_int : core_type_lit
6464
val type_int64 : Parsetree.core_type
65+
val type_float : Parsetree.core_type
6566
val type_any : core_type_lit
6667

6768
val pat_unit : pattern_lit

jscomp/syntax/bs_builtin_ppx.ml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,14 @@ let signature_item_mapper (self : mapper) (sigi : Parsetree.signature_item) =
259259
value_desc with
260260
pval_prim = External_ffi_types.inline_int64_primitive s;
261261
pval_attributes = [];
262-
} }
262+
} }
263+
| Pexp_constant (Pconst_float(s,None)) ->
264+
Bs_ast_invariant.warn_discarded_unused_attributes pval_attributes;
265+
{sigi with psig_desc = Psig_value {
266+
value_desc with
267+
pval_prim = External_ffi_types.inline_float_primitive s;
268+
pval_attributes = [];
269+
} }
263270
| Pexp_construct ({txt = Lident ("true" | "false" as txt)}, None)
264271
->
265272
Bs_ast_invariant.warn_discarded_unused_attributes pval_attributes;
@@ -338,6 +345,16 @@ let structure_item_mapper (self : mapper) (str : Parsetree.structure_item) =
338345
pval_attributes = [];
339346
pval_prim = External_ffi_types.inline_int64_primitive s
340347
} }
348+
| Pexp_constant(Pconst_float (s, None))
349+
->
350+
Bs_ast_invariant.warn_discarded_unused_attributes pvb_attributes;
351+
{str with pstr_desc = Pstr_primitive {
352+
pval_name = pval_name ;
353+
pval_type = Ast_literal.type_float;
354+
pval_loc = pvb_loc;
355+
pval_attributes = [];
356+
pval_prim = External_ffi_types.inline_float_primitive s
357+
} }
341358
| Pexp_construct ({txt = Lident ("true" | "false" as txt) },None) ->
342359
Bs_ast_invariant.warn_discarded_unused_attributes pvb_attributes;
343360
{str with pstr_desc = Pstr_primitive {

jscomp/syntax/external_ffi_types.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,11 @@ let inline_int64_primitive (i : int64) : string list =
316316
(Const_int64 i))
317317
]
318318

319+
let inline_float_primitive (i : string) : string list =
320+
["";
321+
to_string
322+
(Ffi_inline_const (Const_float i))
323+
]
319324
let rec ffi_bs_aux acc (params : External_arg_spec.params) =
320325
match params with
321326
| {arg_type = Nothing; arg_label = Arg_empty}

jscomp/syntax/external_ffi_types.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ val inline_int64_primitive :
141141
int64 ->
142142
string list
143143

144+
val inline_float_primitive :
145+
string -> string list
146+
144147
val ffi_bs:
145148
External_arg_spec.params ->
146149
return_wrapper ->

jscomp/test/inline_const.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33

44
var N = {};
55

6-
function N1($star) {
7-
return {};
8-
}
6+
console.log([
7+
3e-6,
8+
3e-6
9+
]);
910

1011
var x = true;
1112

13+
function N1(funarg) {
14+
return {};
15+
}
16+
1217
var h = "hello";
1318

1419
var hh = "hellohello";
@@ -18,4 +23,4 @@ exports.N = N;
1823
exports.N1 = N1;
1924
exports.h = h;
2025
exports.hh = hh;
21-
/* No side effect */
26+
/* Not a pure module */

jscomp/test/inline_const.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ end
1919

2020
module N1 = functor () -> struct
2121
let f4 = {j|中文|j} [@@bs.inline]
22+
let xx = 3e-6 [@@bs.inline]
23+
let xx0 = 3e-6
2224
end
2325
let h = f
2426

@@ -41,3 +43,5 @@ let f9 = 100L [@@bs.inline]
4143

4244
let v = 100L [@@bs.inline]
4345
let u = 1L [@@bs.inline]
46+
47+
let () = Js.log (xx,xx0)

jscomp/test/inline_const.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ end
1818
module N1 : functor () -> sig
1919
val f4 : string
2020
[@@bs.inline {j|中文|j}]
21+
val xx : float [@@bs.inline 3e-6]
2122
end
2223

2324
val h : string

jscomp/test/inline_const_test.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
var Mt = require("./mt.js");
44
var Int64 = require("../../lib/js/int64.js");
55
var Caml_int64 = require("../../lib/js/caml_int64.js");
6+
var Inline_const = require("./inline_const.js");
67

78
var suites = {
89
contents: /* [] */0
@@ -28,28 +29,32 @@ var f3 = "中文";
2829

2930
var f4 = "中文";
3031

31-
eq("File \"inline_const_test.ml\", line 27, characters 5-12", f, "hello");
32+
eq("File \"inline_const_test.ml\", line 29, characters 5-12", f, "hello");
3233

33-
eq("File \"inline_const_test.ml\", line 28, characters 5-12", f1, "a");
34+
eq("File \"inline_const_test.ml\", line 30, characters 5-12", f1, "a");
3435

35-
eq("File \"inline_const_test.ml\", line 29, characters 5-12", f2, "中文");
36+
eq("File \"inline_const_test.ml\", line 31, characters 5-12", f2, "中文");
3637

37-
eq("File \"inline_const_test.ml\", line 30, characters 5-12", f3, "中文");
38+
eq("File \"inline_const_test.ml\", line 32, characters 5-12", f3, "中文");
3839

39-
eq("File \"inline_const_test.ml\", line 31, characters 5-12", f4, "中文");
40+
eq("File \"inline_const_test.ml\", line 33, characters 5-12", f4, "中文");
4041

41-
eq("File \"inline_const_test.ml\", line 32, characters 5-12", true, true);
42+
eq("File \"inline_const_test.ml\", line 34, characters 5-12", true, true);
4243

43-
eq("File \"inline_const_test.ml\", line 33, characters 5-12", 1, 1);
44+
eq("File \"inline_const_test.ml\", line 35, characters 5-12", 1, 1);
45+
46+
eq("File \"inline_const_test.ml\", line 36, characters 5-12", 3e-6, 0.000003);
4447

4548
var h = Caml_int64.add(Caml_int64.add(Caml_int64.mk(100, 0), Int64.one), Caml_int64.one);
4649

47-
Mt.from_pair_suites("File \"inline_const_test.ml\", line 41, characters 22-29", suites.contents);
50+
Mt.from_pair_suites("File \"inline_const_test.ml\", line 43, characters 22-29", suites.contents);
4851

4952
var f5 = true;
5053

5154
var f6 = 1;
5255

56+
var f7 = 3e-6;
57+
5358
exports.suites = suites;
5459
exports.test_id = test_id;
5560
exports.eq = eq;
@@ -61,5 +66,6 @@ exports.f3 = f3;
6166
exports.f4 = f4;
6267
exports.f5 = f5;
6368
exports.f6 = f6;
69+
exports.f7 = f7;
6470
exports.h = h;
6571
/* Not a pure module */

jscomp/test/inline_const_test.ml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ let f ,
1010
f3,
1111
f4,
1212
f5,
13-
f6
13+
f6,
14+
f7
1415
=
1516

1617
Inline_const.(
@@ -20,7 +21,8 @@ let f ,
2021
N.f3,
2122
H.f4,
2223
f5,
23-
f6
24+
f6,
25+
H.xx
2426
)
2527

2628
let () =
@@ -30,8 +32,8 @@ let () =
3032
eq __LOC__ f3 {j|中文|j};
3133
eq __LOC__ f4 {j|中文|j};
3234
eq __LOC__ f5 true;
33-
eq __LOC__ f6 1
34-
35+
eq __LOC__ f6 1 ;
36+
eq __LOC__ f7 0.000003
3537

3638
let h =
3739
let open Inline_const in

0 commit comments

Comments
 (0)