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-
26- type element = string
27-
28- let rec binarySearchAux (arr : element array ) (lo : int ) (hi : int ) key : bool =
29- let mid = (lo + hi)/ 2 in
30- let midVal = Array. unsafe_get arr mid in
31- if key = midVal then true
32- else if key < midVal then (* a[lo] =< key < a[mid] <= a[hi] *)
33- if hi = mid then
34- (Array. unsafe_get arr lo) = key
35- else binarySearchAux arr lo mid key
36- else (* a[lo] =< a[mid] < key <= a[hi] *)
37- if lo = mid then
38- (Array. unsafe_get arr hi) = key
39- else binarySearchAux arr mid hi key
40-
41- let binarySearch (key : element ) (sorted : element array ) : bool =
42- let len = Array. length sorted in
43- if len = 0 then false
44- else
45- let lo = Array. unsafe_get sorted 0 in
46- if key < lo then false
47- else
48- let hi = Array. unsafe_get sorted (len - 1 ) in
49- if key > hi then false
50- else binarySearchAux sorted 0 (len - 1 ) key
25+ module SSet = Set. Make (String )
5126
5227(* * Words that can never be identifier's name.
5328
5429 See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#reserved_words
5530 *)
56- let sorted_js_keywords = [|
57- " await" ;
31+ let js_keywords = SSet. of_list [
5832 " break" ;
5933 " case" ;
6034 " catch" ;
@@ -66,28 +40,19 @@ let sorted_js_keywords = [|
6640 " delete" ;
6741 " do" ;
6842 " else" ;
69- " enum" ;
7043 " export" ;
7144 " extends" ;
7245 " false" ;
7346 " finally" ;
7447 " for" ;
7548 " function" ;
7649 " if" ;
77- " implements" ;
7850 " import" ;
7951 " in" ;
8052 " instanceof" ;
81- " interface" ;
82- " let" ;
8353 " new" ;
8454 " null" ;
85- " package" ;
86- " private" ;
87- " protected" ;
88- " public" ;
8955 " return" ;
90- " static" ;
9156 " super" ;
9257 " switch" ;
9358 " this" ;
@@ -99,10 +64,23 @@ let sorted_js_keywords = [|
9964 " void" ;
10065 " while" ;
10166 " with" ;
67+ (* The following are also reserved in strict context, including ESM *)
68+ " let" ;
69+ " static" ;
10270 " yield" ;
103- |]
71+ (* `await` is reserved in async context, including ESM *)
72+ " await" ;
73+ (* Future reserved words *)
74+ " enum" ;
75+ " implements" ;
76+ " interface" ;
77+ " package" ;
78+ " private" ;
79+ " protected" ;
80+ " public" ;
81+ ]
10482
105- let is_js_keyword s = binarySearch s sorted_js_keywords
83+ let is_js_keyword s = SSet. exists ( fun x -> x = s) js_keywords
10684
10785(* * Identifiers with special meanings.
10886
@@ -112,7 +90,7 @@ let is_js_keyword s = binarySearch s sorted_js_keywords
11290
11391 However, these names are actually used with no problems today. Preventing this can be annoying.
11492 *)
115- let sorted_js_special_words = [|
93+ let js_special_words = SSet. of_list [
11694 " arguments" ;
11795 " as" ;
11896 " async" ;
@@ -121,12 +99,15 @@ let sorted_js_special_words = [|
12199 " get" ;
122100 " of" ;
123101 " set" ;
124- | ]
102+ ]
125103
126- let is_js_special_word s = binarySearch s sorted_js_special_words
104+ let is_js_special_word s = SSet. exists ( fun x -> x = s) js_special_words
127105
128106(* * Identifier names _might_ need to care about *)
129- let sorted_js_globals = [|
107+ let js_globals = SSet. of_list [
108+ (* JavaScript standards built-ins
109+ See https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects
110+ *)
130111 " AggregateError" ;
131112 " Array" ;
132113 " ArrayBuffer" ;
@@ -139,11 +120,14 @@ let sorted_js_globals = [|
139120 " BigInt64Array" ;
140121 " BigUint64Array" ;
141122 " Boolean" ;
142- " Bun" ;
143123 " DataView" ;
144124 " Date" ;
145- " Deno" ;
125+ " decodeURI" ;
126+ " decodeURIComponent" ;
127+ " encodeURI" ;
128+ " encodeURIComponent" ;
146129 " Error" ;
130+ " eval" ;
147131 " EvalError" ;
148132 " FinalizationRegistry" ;
149133 " Float16Array" ;
@@ -152,18 +136,23 @@ let sorted_js_globals = [|
152136 " Function" ;
153137 " Generator" ;
154138 " GeneratorFunction" ;
139+ " globalThis" ;
155140 " Infinity" ;
156141 " Int16Array" ;
157142 " Int32Array" ;
158143 " Int8Array" ;
159144 " Intl" ;
145+ " isFinite" ;
146+ " isNaN" ;
160147 " Iterator" ;
161148 " JSON" ;
162149 " Map" ;
163150 " Math" ;
164151 " NaN" ;
165152 " Number" ;
166153 " Object" ;
154+ " parseFloat" ;
155+ " parseInt" ;
167156 " Promise" ;
168157 " Proxy" ;
169158 " RangeError" ;
@@ -175,32 +164,49 @@ let sorted_js_globals = [|
175164 " String" ;
176165 " Symbol" ;
177166 " SyntaxError" ;
178- " TypeError" ;
179167 " TypedArray" ;
180- " URIError " ;
168+ " TypeError " ;
181169 " Uint16Array" ;
182170 " Uint32Array" ;
183171 " Uint8Array" ;
184172 " Uint8ClampedArray" ;
173+ " undefined" ;
174+ " URIError" ;
185175 " WeakMap" ;
186176 " WeakRef" ;
187177 " WeakSet" ;
178+
179+ (* A few of the HTML standard globals
180+
181+ See https://developer.mozilla.org/en-US/docs/Web/API/Window
182+ See https://developer.mozilla.org/en-US/docs/Web/API/WorkerGlobalScope
183+
184+ But we don't actually need to protect these names.
185+
186+ "window";
187+ "self";
188+ "document";
189+ "location";
190+ "navigator";
191+ "origin";
192+ *)
193+
194+ (* A few of the Node.js globals
195+
196+ Specifically related to the CommonJS module system
197+ They cannot be redeclared in nested scope.
198+ *)
188199 " __dirname" ;
189200 " __filename" ;
190- " decodeURI" ;
191- " decodeURIComponent" ;
192- " encodeURI" ;
193- " encodeURIComponent" ;
194- " eval" ;
195- " exports" ;
196- " globalThis" ;
197- " isFinite" ;
198- " isNaN" ;
199- " module" ;
200- " parseFloat" ;
201- " parseInt" ;
202201 " require" ;
203- " undefined" ;
204- |]
202+ " module" ;
203+ " exports" ;
204+
205+ (* Bun's global namespace *)
206+ " Bun" ;
207+
208+ (* Deno's global namespace *)
209+ " Deno" ;
210+ ]
205211
206- let is_js_global s = binarySearch s sorted_js_globals
212+ let is_js_global s = SSet. exists ( fun x -> x = s) js_globals
0 commit comments