Skip to content

Commit 97b257f

Browse files
author
Hongbo Zhang
committed
snapshot
1 parent 3a45f86 commit 97b257f

File tree

2 files changed

+108
-24
lines changed

2 files changed

+108
-24
lines changed

jscomp/bin/bs_ppx.ml

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(** Bundled by bspack 08/19-14:58 *)
1+
(** Bundled by bspack 08/19-15:31 *)
22
module String_map : sig
33
#1 "string_map.mli"
44
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -4351,6 +4351,51 @@ type ffi =
43514351

43524352
type prim = Primitive.description
43534353

4354+
let valid_js_char =
4355+
let a = Array.init 256 (fun i ->
4356+
let c = Char.chr i in
4357+
(c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c = '_' || c = '$'
4358+
) in
4359+
(fun c -> Array.unsafe_get a (Char.code c))
4360+
4361+
let valid_first_js_char =
4362+
let a = Array.init 256 (fun i ->
4363+
let c = Char.chr i in
4364+
(c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c = '_' || c = '$'
4365+
) in
4366+
(fun c -> Array.unsafe_get a (Char.code c))
4367+
4368+
(** Approximation could be improved *)
4369+
let valid_ident (s : string) =
4370+
let len = String.length s in
4371+
valid_js_char s.[0] && valid_first_js_char s.[0] &&
4372+
(let module E = struct exception E end in
4373+
try
4374+
for i = 1 to len - 1 do
4375+
if not (valid_js_char (String.unsafe_get s i)) then
4376+
raise E.E
4377+
done ;
4378+
true
4379+
with E.E -> false )
4380+
4381+
let valid_global_name ?loc txt =
4382+
let error () =
4383+
Location.raise_errorf ?loc "Not a valid name %s" txt in
4384+
if txt = "" then
4385+
error ()
4386+
else
4387+
let v = Ext_string.split_by ~keep_empty:true (fun x -> x = '.') txt in
4388+
List.iter
4389+
(fun s ->
4390+
if not (valid_ident s) then error ()
4391+
) v
4392+
4393+
let valid_method_name ?loc txt =
4394+
let error () =
4395+
Location.raise_errorf ?loc "Not a valid name %s" txt in
4396+
if not (valid_ident txt) then error ()
4397+
4398+
43544399
let check_external_module_name ?loc x =
43554400
match x with
43564401
| {bundle = ""; _ } | {bind_name = Some ""} ->
@@ -4364,13 +4409,12 @@ let check_external_module_name_opt ?loc x =
43644409

43654410
let check_ffi ?loc ffi =
43664411
match ffi with
4367-
| Js_global {txt = ""}
4368-
| Js_send {name = ""}
4369-
| Js_set ""
4370-
| Js_get ""
4371-
-> Location.raise_errorf ?loc "empty name encountered"
4372-
| Js_global _ | Js_send _ | Js_set _ | Js_get _
4373-
| Obj_create _
4412+
| Js_global {txt} -> valid_global_name ?loc txt
4413+
| Js_send {name }
4414+
| Js_set name
4415+
| Js_get name
4416+
-> valid_method_name ?loc name
4417+
| Obj_create _ -> ()
43744418
| Js_get_index | Js_set_index
43754419
-> ()
43764420

@@ -4380,10 +4424,8 @@ let check_ffi ?loc ffi =
43804424
| Js_new {external_module_name ; txt = name}
43814425
| Js_call {external_module_name ; txt = {name ; _}}
43824426
->
4383-
check_external_module_name_opt ?loc external_module_name ;
4384-
if name = "" then
4385-
Location.raise_errorf ?loc "empty name in externals"
4386-
4427+
check_external_module_name_opt ?loc external_module_name ;
4428+
valid_global_name ?loc name
43874429

43884430

43894431
(**

jscomp/bin/compiler.ml

Lines changed: 54 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
(** Bundled by bspack 08/19-14:58 *)
1+
(** Bundled by bspack 08/19-15:31 *)
22
module String_map : sig
33
#1 "string_map.mli"
44
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -4351,6 +4351,51 @@ type ffi =
43514351

43524352
type prim = Primitive.description
43534353

4354+
let valid_js_char =
4355+
let a = Array.init 256 (fun i ->
4356+
let c = Char.chr i in
4357+
(c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c = '_' || c = '$'
4358+
) in
4359+
(fun c -> Array.unsafe_get a (Char.code c))
4360+
4361+
let valid_first_js_char =
4362+
let a = Array.init 256 (fun i ->
4363+
let c = Char.chr i in
4364+
(c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c = '_' || c = '$'
4365+
) in
4366+
(fun c -> Array.unsafe_get a (Char.code c))
4367+
4368+
(** Approximation could be improved *)
4369+
let valid_ident (s : string) =
4370+
let len = String.length s in
4371+
valid_js_char s.[0] && valid_first_js_char s.[0] &&
4372+
(let module E = struct exception E end in
4373+
try
4374+
for i = 1 to len - 1 do
4375+
if not (valid_js_char (String.unsafe_get s i)) then
4376+
raise E.E
4377+
done ;
4378+
true
4379+
with E.E -> false )
4380+
4381+
let valid_global_name ?loc txt =
4382+
let error () =
4383+
Location.raise_errorf ?loc "Not a valid name %s" txt in
4384+
if txt = "" then
4385+
error ()
4386+
else
4387+
let v = Ext_string.split_by ~keep_empty:true (fun x -> x = '.') txt in
4388+
List.iter
4389+
(fun s ->
4390+
if not (valid_ident s) then error ()
4391+
) v
4392+
4393+
let valid_method_name ?loc txt =
4394+
let error () =
4395+
Location.raise_errorf ?loc "Not a valid name %s" txt in
4396+
if not (valid_ident txt) then error ()
4397+
4398+
43544399
let check_external_module_name ?loc x =
43554400
match x with
43564401
| {bundle = ""; _ } | {bind_name = Some ""} ->
@@ -4364,13 +4409,12 @@ let check_external_module_name_opt ?loc x =
43644409

43654410
let check_ffi ?loc ffi =
43664411
match ffi with
4367-
| Js_global {txt = ""}
4368-
| Js_send {name = ""}
4369-
| Js_set ""
4370-
| Js_get ""
4371-
-> Location.raise_errorf ?loc "empty name encountered"
4372-
| Js_global _ | Js_send _ | Js_set _ | Js_get _
4373-
| Obj_create _
4412+
| Js_global {txt} -> valid_global_name ?loc txt
4413+
| Js_send {name }
4414+
| Js_set name
4415+
| Js_get name
4416+
-> valid_method_name ?loc name
4417+
| Obj_create _ -> ()
43744418
| Js_get_index | Js_set_index
43754419
-> ()
43764420

@@ -4380,10 +4424,8 @@ let check_ffi ?loc ffi =
43804424
| Js_new {external_module_name ; txt = name}
43814425
| Js_call {external_module_name ; txt = {name ; _}}
43824426
->
4383-
check_external_module_name_opt ?loc external_module_name ;
4384-
if name = "" then
4385-
Location.raise_errorf ?loc "empty name in externals"
4386-
4427+
check_external_module_name_opt ?loc external_module_name ;
4428+
valid_global_name ?loc name
43874429

43884430

43894431
(**

0 commit comments

Comments
 (0)