Skip to content

Commit f875fe9

Browse files
committed
Improved compilation of JavaScript literal objects
1 parent 1c10a9b commit f875fe9

File tree

6 files changed

+22
-5
lines changed

6 files changed

+22
-5
lines changed

compiler/annot_lexer.mll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ rule initial = parse
3030
| "mutable" {TA_Mutable }
3131
| "mutator" {TA_Mutator }
3232
| "shallow" {TA_Shallow}
33+
| "object_literal" {TA_Object_literal}
3334
| "Version" {TVersion}
3435
| ['a'-'z''A'-'Z''$''_']['a'-'z''A'-'Z''$''_''0'-'9']* {
3536
let x = Lexing.lexeme lexbuf in

compiler/annot_parser.mly

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121

2222
%token TProvides TRequires TVersion
23-
%token TA_Pure TA_Const TA_Mutable TA_Mutator TA_Shallow
23+
%token TA_Pure TA_Const TA_Mutable TA_Mutator TA_Shallow TA_Object_literal
2424
%token<string> TIdent TVNum
2525
%token TComma TSemi EOF EOL LE LT GE GT EQ LPARENT RPARENT
2626
%token<string> TOTHER
@@ -49,6 +49,7 @@ prim_annot:
4949
arg_annot:
5050
| TA_Const { `Const }
5151
| TA_Shallow { `Shallow_const}
52+
| TA_Object_literal { `Object_literal}
5253
| TA_Mutable { `Mutable}
5354

5455
op:

compiler/flow.ml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,23 @@ let expr_escape st _x e =
230230
| Expr (Block (_, a)) ->
231231
Array.iter (fun x -> block_escape st x) a
232232
| _ -> block_escape st v
233+
end
234+
| Pv v,`Object_literal ->
235+
begin match st.defs.(Var.idx v) with
236+
| Expr (Block (_, a)) ->
237+
Array.iter
238+
(fun x ->
239+
begin match st.defs.(Var.idx x) with
240+
| Expr (Block (_, [|k; v|])) ->
241+
block_escape st v
242+
| _ ->
243+
block_escape st x
244+
end)
245+
a
246+
| _ ->
247+
block_escape st v
233248
end;
234-
| Pv v, _ -> block_escape st v
249+
| Pv v, `Mutable -> block_escape st v
235250
end;
236251
loop ax kx in
237252
loop l ka

compiler/jsoo_primitive.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ let rec resolve nm = try resolve (Hashtbl.find aliases nm) with Not_found -> nm
2525
(****)
2626

2727
type kind = [ `Pure | `Mutable | `Mutator ]
28-
type kind_arg = [`Shallow_const | `Const | `Mutable]
28+
type kind_arg = [`Shallow_const | `Object_literal | `Const | `Mutable]
2929
type t = [
3030
| `Requires of Parse_info.t option * string list
3131
| `Provides of Parse_info.t option * string * kind * kind_arg list option

compiler/jsoo_primitive.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ val is_pure : string -> bool
2222
val exists : string -> bool
2323

2424
type kind = [ `Pure | `Mutable | `Mutator ]
25-
type kind_arg = [`Shallow_const | `Const | `Mutable]
25+
type kind_arg = [`Shallow_const | `Object_literal | `Const | `Mutable]
2626
type t =
2727
[ `Requires of Parse_info.t option * string list
2828
| `Provides of Parse_info.t option * string * kind * kind_arg list option

runtime/jslib_js_of_ocaml.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ function caml_pure_js_expr (s){
125125
js_print_stderr("caml_pure_js_expr: fallback to runtime evaluation");
126126
return eval(s.toString());}
127127

128-
//Provides: caml_js_object (shallow)
128+
//Provides: caml_js_object (object_literal)
129129
//Requires: MlString
130130
function caml_js_object (a) {
131131
var o = {};

0 commit comments

Comments
 (0)