Skip to content

Commit 8398222

Browse files
committed
Compiler: fix #330
1 parent 02a97c6 commit 8398222

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

compiler/generate.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,15 +1095,15 @@ and translate_expr ctx queue loc _x e level : _ * J.statement_list =
10951095
in
10961096
(J.ENew (cc, if args = [] then None else Some args),
10971097
or_p pc prop, queue)
1098-
| Extern "caml_js_get", [Pv o; Pc (String f | IString f)] ->
1098+
| Extern "caml_js_get", [Pv o; Pc (String f | IString f)] when J.is_ident f ->
10991099
let ((po, co), queue) = access_queue queue o in
11001100
(J.EDot (co, f), or_p po mutable_p, queue)
1101-
| Extern "caml_js_set", [Pv o; Pc (String f | IString f); v] ->
1101+
| Extern "caml_js_set", [Pv o; Pc (String f | IString f); v] when J.is_ident f ->
11021102
let ((po, co), queue) = access_queue queue o in
11031103
let ((pv, cv), queue) = access_queue' ~ctx queue v in
11041104
(J.EBin (J.Eq, J.EDot (co, f), cv),
11051105
or_p (or_p po pv) mutator_p, queue)
1106-
| Extern "caml_js_delete", [Pv o; Pc (String f | IString f)] ->
1106+
| Extern "caml_js_delete", [Pv o; Pc (String f | IString f)] when J.is_ident f ->
11071107
let ((po, co), queue) = access_queue queue o in
11081108
(J.EUn(J.Delete, J.EDot (co, f)), or_p po mutator_p, queue)
11091109
| Extern "%overrideMod", [Pc (String m | IString m);Pc (String f | IString f)] ->

compiler/javascript.ml

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,21 +105,21 @@ and statement =
105105
Block of block
106106
| Variable_statement of variable_declaration list
107107
| Empty_statement
108-
| Expression_statement of expression
109-
| If_statement of expression * (statement * location) * (statement * location) option
110-
| Do_while_statement of (statement * location) * expression
108+
| Expression_statement of expression
109+
| If_statement of expression * (statement * location) * (statement * location) option
110+
| Do_while_statement of (statement * location) * expression
111111
| While_statement of expression * (statement * location)
112112
| For_statement of (expression option,variable_declaration list) either * expression option * expression option * (statement * location)
113113
| ForIn_statement of (expression,variable_declaration) either * expression * (statement * location)
114-
| Continue_statement of Label.t option
115-
| Break_statement of Label.t option
116-
| Return_statement of expression option
114+
| Continue_statement of Label.t option
115+
| Break_statement of Label.t option
116+
| Return_statement of expression option
117117
(* | With_statement of expression * statement *)
118118
| Labelled_statement of Label.t * (statement * location)
119119
| Switch_statement of
120120
expression * case_clause list * statement_list option * case_clause list
121-
| Throw_statement of expression
122-
| Try_statement of block * (ident * block) option * block option
121+
| Throw_statement of expression
122+
| Try_statement of block * (ident * block) option * block option
123123
| Debugger_statement
124124

125125
and ('left,'right) either =
@@ -205,5 +205,23 @@ let string_of_number v =
205205
then s2
206206
else Printf.sprintf "%.18g" v
207207

208+
let is_ident =
209+
let l = Array.init 256 (fun i ->
210+
let c = Char.chr i in
211+
if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c = '_' || c = '$'
212+
then 1
213+
else if (c >= '0' && c <='9')
214+
then 2
215+
else 0
216+
) in
217+
fun s ->
218+
try
219+
for i = 0 to String.length s - 1 do
220+
let code = l.(Char.code(s.[i])) in
221+
if i = 0 then assert (code = 1) else assert (code >= 1)
222+
done;
223+
true
224+
with _ -> false
225+
208226
module IdentSet = Set.Make(struct type t = ident let compare = compare_ident end)
209227
module IdentMap = Map.Make(struct type t = ident let compare = compare_ident end)

compiler/javascript.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,6 @@ and source_element =
157157

158158
val compare_ident : ident -> ident -> int
159159
val string_of_number : float -> string
160-
160+
val is_ident : string -> bool
161161
module IdentSet : Set.S with type elt = ident
162162
module IdentMap : Map.S with type key = ident

0 commit comments

Comments
 (0)