Skip to content

Commit b3d2462

Browse files
authored
Merge pull request #618 from bloomberg/external_object
add first class js object support so people can create js object
2 parents 29bc22b + 4412f8e commit b3d2462

21 files changed

+531
-17
lines changed

jscomp/syntax/ast_attributes.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ let process_attributes_rev (attrs : t) =
101101
st, attr::acc
102102
) ( `Nothing, []) attrs
103103

104-
let process_class_type_decl_rev attrs =
104+
let process_bs attrs =
105105
List.fold_left (fun (st, acc) (({txt; loc}, _) as attr : attr) ->
106106
match txt, st with
107107
| "bs", _

jscomp/syntax/ast_attributes.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ val process_method_attributes_rev :
3535
val process_attributes_rev :
3636
t -> [ `Meth_callback | `Nothing | `Uncurry | `Method ] * t
3737

38-
val process_class_type_decl_rev :
38+
val process_bs :
3939
t -> [ `Nothing | `Has] * t
4040

4141
val process_external : t -> bool

jscomp/syntax/ast_core_type.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,6 @@ let from_labels ~loc tyvars (labels : string list)
150150
(fun label tyvar acc -> Typ.arrow ~loc label tyvar acc) labels tyvars result_type
151151

152152

153+
let make_obj ~loc xs =
154+
Ast_comb.to_js_type loc @@
155+
Ast_helper.Typ.object_ ~loc xs Closed

jscomp/syntax/ast_core_type.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,7 @@ val string_type : t -> arg_type
5757
val from_labels :
5858
loc:Location.t -> t list -> string list -> t
5959

60+
val make_obj :
61+
loc:Location.t ->
62+
(string * Parsetree.attributes * t) list ->
63+
t

jscomp/syntax/ast_exp.ml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
type t = Parsetree.expression

jscomp/syntax/ast_exp.mli

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
type t = Parsetree.expression

jscomp/syntax/ast_external.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ val create_local_external : Location.t ->
2929
pval_type:Parsetree.core_type ->
3030
?local_module_name:string ->
3131
?local_fun_name:string ->
32-
(Asttypes.label * Parsetree.expression) list -> Parsetree.expression_desc
32+
(string * Parsetree.expression) list -> Parsetree.expression_desc
3333

3434
val local_extern_cont :
3535
Location.t ->

jscomp/syntax/ast_external_attributes.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,7 @@ let handle_attributes
433433
| Obj_create arg_labels , {ptyp_desc = Ptyp_any; _}
434434
->
435435
let result =
436-
Ast_comb.to_js_type loc @@
437-
Ast_helper.Typ.object_ ~loc (
436+
Ast_core_type.make_obj ~loc (
438437
List.fold_right2 (fun arg label acc ->
439438
match arg, label with
440439
| (_, ty), Ast_core_type.Label s
@@ -451,7 +450,7 @@ let handle_attributes
451450
| _ -> assert false
452451
end
453452
| (_, _), Ast_core_type.Empty -> acc
454-
) arg_types_ty arg_labels []) Closed in
453+
) arg_types_ty arg_labels []) in
455454
Ast_core_type.replace_result type_annotation result
456455
| _, _ -> type_annotation) ,
457456
(match ffi , prim_name with

jscomp/syntax/ast_pat.ml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
26+
type t = Parsetree.pattern
27+
28+
29+
let is_unit_cont ~yes ~no (p : t) =
30+
match p with
31+
| {ppat_desc = Ppat_construct({txt = Lident "()"}, None)}
32+
-> yes
33+
| _ -> no
34+
35+
36+
(** [arity_of_fun pat e] tells the arity of
37+
expression [fun pat -> e]
38+
*)
39+
let arity_of_fun
40+
(pat : Parsetree.pattern)
41+
(e : Parsetree.expression) =
42+
let rec aux (e : Parsetree.expression) =
43+
match e.pexp_desc with
44+
| Pexp_fun ("", None, pat, e) ->
45+
1 + aux e
46+
| Pexp_fun _
47+
-> Location.raise_errorf
48+
~loc:e.pexp_loc "Lable is not allowed in JS object"
49+
| _ -> 0 in
50+
is_unit_cont ~yes:0 ~no:1 pat + aux e

jscomp/syntax/ast_pat.mli

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
type t = Parsetree.pattern
26+
27+
val is_unit_cont : yes:'a -> no:'a -> t -> 'a
28+
29+
(** [arity_of_fun pat e] tells the arity of
30+
expression [fun pat -> e]*)
31+
val arity_of_fun : t -> Parsetree.expression -> int

0 commit comments

Comments
 (0)