Skip to content

Commit 4128eb4

Browse files
committed
switch to another representation
1 parent df77396 commit 4128eb4

File tree

152 files changed

+9861
-9766
lines changed

Some content is hidden

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

152 files changed

+9861
-9766
lines changed

jscomp/core/js_dump.ml

+6-2
Original file line numberDiff line numberDiff line change
@@ -794,8 +794,12 @@ and
794794
(* | Caml_uninitialized_obj (tag, size)
795795
-> (* FIXME *)
796796
expression_desc cxt l f (Object [Length, size ; Tag, tag]) *)
797-
| Optional_block (e,_) ->
798-
expression_desc cxt l f (Caml_block([e],Immutable,E.zero_int_literal, Blk_na))
797+
| Optional_block (e,identity) ->
798+
if identity then
799+
expression l cxt f e
800+
else
801+
expression l cxt f
802+
(E.runtime_call Js_runtime_modules.js_primitive "some" [e])
799803
| Caml_block( el, mutable_flag, tag, tag_info)
800804
->
801805
(* Note that, if we ignore more than tag [0] we loose some information

jscomp/core/js_exp_make.ml

+6-6
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ let merge_outer_comment comment (e : t ) =
155155
comment
156156
= Some (comment ^ sep ^ s)}
157157

158-
let some_comment = Some "Some"
158+
let some_comment = None
159159

160160
let optional_block e : J.expression =
161161
{ expression_desc = Optional_block (e,false) ;
@@ -915,13 +915,13 @@ let rec int_comp (cmp : Lambda.comparison) ?comment (e0 : t) (e1 : t) =
915915
{fn with expression_desc =
916916
Var(Qualified (ident,Runtime, Some "caml_equal"))
917917
} , args, call_info)}
918-
| Ceq, Optional_block _, Number _
919-
| Ceq, Number _, Optional_block _
918+
| Ceq, Optional_block _, Undefined
919+
| Ceq, Undefined, Optional_block _
920920
-> caml_false
921921
| Ceq, _, _ -> int_equal e0 e1
922-
(* -FIXME: it should not be called [int_comp] *)
923-
| Cneq, Optional_block _, Number _
924-
| Cneq, Number _, Optional_block _
922+
923+
| Cneq, Optional_block _, Undefined
924+
| Cneq, Undefined , Optional_block _
925925
| Cneq, Caml_block _ , Number _
926926
| Cneq, Number _, Caml_block _
927927
-> caml_true

jscomp/core/js_fold_basic.ml

+7-1
Original file line numberDiff line numberDiff line change
@@ -107,13 +107,19 @@ class count_hard_dependencies =
107107
(Ident.create_persistent Js_runtime_modules.curry));
108108
super#expression x
109109
| {expression_desc = Caml_block(_,_, tag, tag_info); _}
110-
-> (* -FIXME: Optional_block *)
110+
->
111111
if needBlockRuntime tag tag_info then
112112
add_lam_module_ident hard_dependencies
113113
(Lam_module_ident.of_runtime
114114
(Ident.create_persistent Js_runtime_modules.block))
115115
;
116116
super#expression x
117+
| {expression_desc = Optional_block (_,false)} ->
118+
add_lam_module_ident hard_dependencies
119+
(Lam_module_ident.of_runtime
120+
(Ident.create_persistent Js_runtime_modules.js_primitive))
121+
;
122+
super#expression x
117123
| _ -> super#expression x
118124
method get_hard_dependencies = hard_dependencies
119125
end

jscomp/core/js_of_lam_option.ml

+7-5
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,10 @@ type option_unwrap_time =
4242
]}
4343
*)
4444
let none : J.expression =
45-
(* -FIXME *)
46-
{expression_desc = Number (Int {i = 0l; c = None}); comment = Some "None" }
45+
E.undefined
4746

48-
let is_none_static (arg : J.expression_desc ) =
49-
match arg with Number _ -> true | _ -> false
47+
48+
let is_none_static (arg : J.expression_desc ) = arg = Undefined
5049

5150
let is_not_none (e : J.expression) : J.expression =
5251
let desc = e.expression_desc in
@@ -60,7 +59,8 @@ let val_from_option (arg : J.expression) =
6059
match arg.expression_desc with
6160
| Optional_block (x,_) -> x
6261
| _ ->
63-
E.index arg 0l (* -FIXME *)
62+
E.runtime_call Js_runtime_modules.js_primitive
63+
"valFromOption" [arg]
6464
(**
6565
Invrariant:
6666
- optional encoding
@@ -87,6 +87,7 @@ let get_default_undefined_from_optional
8787
-> x (* invariant: option encoding *)
8888
| _ ->
8989
if Js_analyzer.is_okay_to_duplicate arg then
90+
(* FIXME: no need do such inlining*)
9091
E.econd (is_not_none arg )
9192
(val_from_option arg) E.undefined
9293
else
@@ -101,6 +102,7 @@ let get_default_undefined (arg : J.expression) : J.expression =
101102
Js_of_lam_polyvar.get_field x
102103
(* invariant: option encoding *)
103104
| _ ->
105+
(* FIXME: no need do such inlining*)
104106
if Js_analyzer.is_okay_to_duplicate arg then
105107
E.econd (is_not_none arg)
106108
(Js_of_lam_polyvar.get_field (val_from_option arg)) E.undefined

jscomp/core/js_pass_flatten_and_mark_dead.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ let subst_map name = object (self)
196196
List.fold_left
197197
(fun (i,e, acc) (x : J.expression) ->
198198
match x.expression_desc with
199-
| J.Var _ | Number _ | Str _ | J.Bool _
199+
| J.Var _ | Number _ | Str _ | J.Bool _ | Undefined
200200
-> (* TODO: check the optimization *)
201201
(i + 1, x :: e, acc)
202202
| _ ->
@@ -247,7 +247,7 @@ let subst_map name = object (self)
247247
turn a runtime crash into compile time crash : )
248248
*)
249249
begin match List.nth ls (Int32.to_int i) with
250-
| {expression_desc = J.Var _ | Number _ | Str _ } as x
250+
| {expression_desc = J.Var _ | Number _ | Str _ | Undefined} as x
251251
-> x
252252
| exception _ ->
253253
begin

jscomp/core/lam.ml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1203,8 +1203,8 @@ let unit : t =
12031203

12041204

12051205
let lam_none : constant =
1206-
(* -FIXME *)
1207-
(Const_pointer(0, Pt_constructor "None"))
1206+
Const_js_undefined
1207+
12081208

12091209
(* let assert_false_unit : t =
12101210
Lconst (Const_pointer( 0, Pt_constructor "impossible branch")) *)

jscomp/core/lam_compile_primitive.ml

+1-2
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,7 @@ let translate loc
193193
end
194194
| Pval_from_option_not_nest ->
195195
begin match args with
196-
| [ e ] ->
197-
Js_of_lam_option.val_from_option e
196+
| [ e ] -> e
198197
| _ -> assert false
199198
end
200199
| Pfield (i, fld_info) ->

jscomp/runtime/.depend

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ caml_backtrace.cmj : caml_backtrace.cmi
2323
caml_int32.cmj : caml_int32.cmi
2424
caml_gc.cmj : caml_gc.cmi
2525
js_typed_array.cmj : js.cmj
26-
js_primitive.cmj : js_undefined.cmj js_null.cmj js.cmj js_primitive.cmi
26+
js_primitive.cmj : js.cmj js_primitive.cmi
2727
caml_basic.cmj : js_undefined.cmj caml_basic.cmi
2828
caml_oo.cmj : bs_obj.cmj caml_oo.cmi
2929
curry.cmj : caml_array.cmj

jscomp/runtime/.extradepend

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
caml_array.cmj: caml_builtin_exceptions.cmj
22
caml_backtrace.cmj: caml_builtin_exceptions.cmj
3+
caml_basic.cmj: js_primitive.cmj
34
caml_bytes.cmj: caml_builtin_exceptions.cmj
45
caml_format.cmj: curry.cmj caml_int32.cmj caml_int64.cmj caml_utils.cmj caml_builtin_exceptions.cmj
56
caml_hash.cmj: caml_queue.cmj caml_hash_primitive.cmj caml_builtin_exceptions.cmj
@@ -9,11 +10,13 @@ caml_int64.cmj: caml_int32.cmj caml_utils.cmj caml_primitive.cmj caml_builtin_ex
910
caml_io.cmj: curry.cmj caml_builtin_exceptions.cmj
1011
caml_lexer.cmj: caml_builtin_exceptions.cmj
1112
caml_module.cmj: caml_obj.cmj caml_builtin_exceptions.cmj
12-
caml_obj.cmj: block.cmj caml_primitive.cmj caml_builtin_exceptions.cmj
13+
caml_obj.cmj: block.cmj js_primitive.cmj caml_primitive.cmj caml_builtin_exceptions.cmj
1314
caml_oo.cmj: caml_array.cmj caml_builtin_exceptions.cmj
1415
caml_oo_curry.cmj: curry.cmj caml_oo.cmj
1516
caml_string.cmj: caml_builtin_exceptions.cmj
1617
caml_sys.cmj: caml_builtin_exceptions.cmj
1718
caml_weak.cmj: caml_obj.cmj caml_array.cmj js_primitive.cmj
1819
curry.cmj: caml_array.cmj
1920
js_exn.cmj: caml_exceptions.cmj
21+
js_null.cmj: js_primitive.cmj
22+
js_undefined.cmj: js_primitive.cmj

jscomp/runtime/js_primitive.ml

+28-24
Original file line numberDiff line numberDiff line change
@@ -22,53 +22,57 @@
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+
let undefinedHeader = [| |]
2526

27+
let some ( x : Obj.t) : Obj.t =
28+
if Obj.magic x = None then
29+
Obj.repr (undefinedHeader, 0)
30+
else
31+
if x != Obj.repr Js.null && fst (Obj.magic x ) == Obj.repr undefinedHeader then
32+
Obj.repr (undefinedHeader, snd (Obj.magic x) + 1)
33+
else x
2634

27-
let nullable_to_opt ( x : 'a Js.null_undefined) =
35+
let nullable_to_opt (type t) ( x : t Js.null_undefined) : t option =
2836
if (Obj.magic x) == Js.null || (Obj.magic x) == Js.undefined then
2937
None
30-
else Some (Obj.magic x : 'a)
38+
else Obj.magic (some (Obj.magic x : 'a))
3139

32-
let undefined_to_opt ( x : 'a Js.undefined) =
40+
let undefined_to_opt (type t) ( x : t Js.undefined) : t option =
3341
if (Obj.magic x) == Js.undefined then None
34-
else Some (Obj.magic x : 'a)
42+
else Obj.magic (some (Obj.magic x : 'a))
3543

36-
let null_to_opt ( x : 'a Js.null) =
44+
let null_to_opt (type t ) ( x : t Js.null) : t option =
3745
if (Obj.magic x) == Js.null then None
38-
else Some (Obj.magic x : 'a)
46+
else Obj.magic (some (Obj.magic x : 'a) )
47+
48+
(* external valFromOption : 'a option -> 'a =
49+
"#val_from_option" *)
3950

40-
external valFromOption : 'a option -> 'a =
41-
"#val_from_option"
4251

43-
let undefinedHeader = [| |]
4452

4553
(** The input is already of [Some] form, [x] is not None,
4654
make sure [x[0]] will not throw *)
4755
let valFromOption (x : Obj.t) : Obj.t =
48-
if x != Obj.repr Js_null.empty && fst (Obj.magic x) == Obj.repr undefinedHeader
56+
if x != Obj.repr Js.null && fst (Obj.magic x) == Obj.repr undefinedHeader
4957
then
5058
let depth : int = snd (Obj.magic x) in
5159
if depth = 0 then Obj.magic None
5260
else Obj.magic (undefinedHeader, depth - 1)
5361
else Obj.magic x
5462

55-
let some ( x : Obj.t) : Obj.t =
56-
if Obj.magic x = None then
57-
Obj.repr (undefinedHeader, 0)
58-
else
59-
if x != Obj.repr Js_null.empty && fst (Obj.magic x ) == Obj.repr undefinedHeader then
60-
Obj.repr (undefinedHeader, snd (Obj.magic x) + 1)
61-
else x
62-
6363

64-
let option_get (x : 'a option) : 'a Js_undefined.t =
65-
match x with
64+
let option_get (x : 'a option) : 'a Js.undefined =
65+
if x = None then Js.undefined
66+
else Obj.magic (valFromOption (Obj.repr x))
67+
(* match x with
6668
| None -> Js_undefined.empty
67-
| Some x -> Js_undefined.return x
69+
| Some x -> Js_undefined.return x *)
6870

6971
(** [input] is optional polymorphic variant *)
70-
let option_get_unwrap (x : 'a option) : _ Js_undefined.t =
71-
match x with
72+
let option_get_unwrap (x : 'a option) : _ Js.undefined =
73+
if x = None then Js.undefined
74+
else Obj.magic (Obj.field (Obj.repr (valFromOption (Obj.repr x))) 1 )
75+
(* match x with
7276
| None -> Js.undefined
73-
| Some x -> Js_undefined.return (Obj.field (Obj.repr x) 1)
77+
| Some x -> Js_undefined.return (Obj.field (Obj.repr x) 1) *)
7478

jscomp/test/a_string_test.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ var suites_000 = /* tuple */[
99
"split",
1010
(function () {
1111
return /* Eq */Block.__(0, [
12-
Ext_string_test.split([true], "hihi", /* "i" */105),
12+
Ext_string_test.split(true, "hihi", /* "i" */105),
1313
/* :: */[
1414
"h",
1515
/* :: */[
@@ -29,7 +29,7 @@ var suites_001 = /* :: */[
2929
"split_non_empty",
3030
(function () {
3131
return /* Eq */Block.__(0, [
32-
Ext_string_test.split(/* None */0, "hihi", /* "i" */105),
32+
Ext_string_test.split(undefined, "hihi", /* "i" */105),
3333
/* :: */[
3434
"h",
3535
/* :: */[
@@ -45,7 +45,7 @@ var suites_001 = /* :: */[
4545
"split_empty",
4646
(function () {
4747
return /* Eq */Block.__(0, [
48-
Ext_string_test.split([true], "", /* "i" */105),
48+
Ext_string_test.split(true, "", /* "i" */105),
4949
/* [] */0
5050
]);
5151
})
@@ -55,7 +55,7 @@ var suites_001 = /* :: */[
5555
"split_normal",
5656
(function () {
5757
return /* Eq */Block.__(0, [
58-
Ext_string_test.split([true], "h i i", /* " " */32),
58+
Ext_string_test.split(true, "h i i", /* " " */32),
5959
/* :: */[
6060
"h",
6161
/* :: */[
@@ -76,7 +76,7 @@ var suites_001 = /* :: */[
7676
return /* Eq */Block.__(0, [
7777
List.filter((function (s) {
7878
return s !== "";
79-
}))(Ext_string_test.split_by(/* None */0, (function (x) {
79+
}))(Ext_string_test.split_by(undefined, (function (x) {
8080
if (x === /* " " */32) {
8181
return true;
8282
} else {

jscomp/test/ast_abstract_test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,9 @@ function zToJs(param) {
187187

188188
function zFromJs(param) {
189189
if (param <= 2 && 0 <= param) {
190-
return [param - 0 | 0];
190+
return param - 0 | 0;
191191
} else {
192-
return /* None */0;
192+
return undefined;
193193
}
194194
}
195195

0 commit comments

Comments
 (0)