-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.ml
259 lines (228 loc) · 6.32 KB
/
common.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
(*
* Haxe Compiler
* Copyright (c)2005-2008 Nicolas Cannasse
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)
open Type
type package_rule =
| Forbidden
| Directory of string
| Remap of string
type platform =
| Cross
| Flash
| Js
| Neko
| Flash9
| Php
| Cpp
type pos = Ast.pos
type basic_types = {
mutable tvoid : t;
mutable tint : t;
mutable tfloat : t;
mutable tbool : t;
mutable tnull : t -> t;
mutable tstring : t;
mutable tarray : t -> t;
}
type context = {
(* config *)
version : int;
mutable display : bool;
mutable debug : bool;
mutable verbose : bool;
mutable foptimize : bool;
mutable dead_code_elimination : bool;
mutable platform : platform;
mutable std_path : string list;
mutable class_path : string list;
mutable main_class : Type.path option;
mutable defines : (string,unit) PMap.t;
mutable package_rules : (string,package_rule) PMap.t;
mutable error : string -> pos -> unit;
mutable warning : string -> pos -> unit;
mutable load_extern_type : (path -> pos -> Ast.package option) list; (* allow finding types which are not in sources *)
mutable filters : (unit -> unit) list;
mutable defines_signature : string option;
(* add input format option to be able to choose another parser *)
mutable input_format: string;
mutable as3_mode: bool;
(* output *)
mutable file : string;
mutable flash_version : float;
mutable modules : Type.module_def list;
mutable main : Type.texpr option;
mutable types : Type.module_type list;
mutable resources : (string,string) Hashtbl.t;
mutable neko_libs : string list;
mutable php_front : string option;
mutable php_lib : string option;
mutable php_prefix : string option;
mutable swf_libs : (string * (unit -> Swf.swf) * (unit -> ((string list * string),As3hl.hl_class) Hashtbl.t)) list;
mutable js_gen : (unit -> unit) option;
(* typing *)
mutable basic : basic_types;
}
type global_cache = {
cache_version : int;
mutable cache_file : string option;
mutable cached_haxelib : (string list, string list) Hashtbl.t;
mutable cached_files : (string, float * Ast.package) Hashtbl.t;
}
exception Abort of string * Ast.pos
let display_default = ref false
let cache_version = 1
let global_cache : global_cache option ref = ref None
let create_cache() =
{
cache_version = cache_version;
cache_file = None;
cached_files = Hashtbl.create 0;
cached_haxelib = Hashtbl.create 0;
}
let create v =
let m = Type.mk_mono() in
{
version = v;
debug = false;
display = !display_default;
verbose = false;
foptimize = true;
dead_code_elimination = false;
platform = Cross;
std_path = [];
class_path = [];
main_class = None;
defines = PMap.add "true" () (if !display_default then PMap.add "display" () PMap.empty else PMap.empty);
package_rules = PMap.empty;
file = "";
types = [];
filters = [];
modules = [];
main = None;
flash_version = 10.;
resources = Hashtbl.create 0;
php_front = None;
php_lib = None;
swf_libs = [];
neko_libs = [];
php_prefix = None;
js_gen = None;
load_extern_type = [];
defines_signature = None;
(* default parser is haxe *)
input_format="hx";
as3_mode=false;
warning = (fun _ _ -> assert false);
error = (fun _ _ -> assert false);
basic = {
tvoid = m;
tint = m;
tfloat = m;
tbool = m;
tnull = (fun _ -> assert false);
tstring = m;
tarray = (fun _ -> assert false);
};
}
let clone com =
let t = com.basic in
{ com with basic = { t with tvoid = t.tvoid } }
let platforms = [
Flash;
Js;
Neko;
Flash9;
Php;
Cpp
]
let platform_name = function
| Cross -> "cross"
| Flash -> "flash"
| Js -> "js"
| Neko -> "neko"
| Flash9 -> "flash9"
| Php -> "php"
| Cpp -> "cpp"
let flash_versions = List.map (fun v ->
let maj = int_of_float v in
let min = int_of_float (mod_float (v *. 10.) 10.) in
v, string_of_int maj ^ (if min = 0 then "" else "_" ^ string_of_int min)
) [9.;10.;10.1;10.2;10.3;11.;11.1;11.2]
let defined ctx v = PMap.mem v ctx.defines
let define ctx v =
ctx.defines <- PMap.add v () ctx.defines;
let v = String.concat "_" (ExtString.String.nsplit v "-") in
ctx.defines <- PMap.add v () ctx.defines;
ctx.defines_signature <- None
let init_platform com pf =
com.platform <- pf;
let name = platform_name pf in
let forbid acc p = if p = name || PMap.mem p acc then acc else PMap.add p Forbidden acc in
com.package_rules <- List.fold_left forbid com.package_rules (List.map platform_name platforms);
define com name
let error msg p = raise (Abort (msg,p))
let platform ctx p = ctx.platform = p
let add_filter ctx f =
ctx.filters <- f :: ctx.filters
let find_file ctx f =
let rec loop = function
| [] -> raise Not_found
| p :: l ->
let file = p ^ f in
if Sys.file_exists file then
file
else
loop l
in
loop ctx.class_path
let get_full_path f = try Extc.get_full_path f with _ -> f
(* ------------------------- TIMERS ----------------------------- *)
type timer_infos = {
name : string;
mutable start : float list;
mutable total : float;
}
let get_time = Unix.gettimeofday
let htimers = Hashtbl.create 0
let new_timer name =
try
let t = Hashtbl.find htimers name in
t.start <- get_time() :: t.start;
t
with Not_found ->
let t = { name = name; start = [get_time()]; total = 0.; } in
Hashtbl.add htimers name t;
t
let curtime = ref []
let close t =
let start = (match t.start with
| [] -> assert false
| s :: l -> t.start <- l; s
) in
let dt = get_time() -. start in
t.total <- t.total +. dt;
curtime := List.tl !curtime;
List.iter (fun ct -> ct.start <- List.map (fun t -> t +. dt) ct.start) !curtime
let timer name =
let t = new_timer name in
curtime := t :: !curtime;
(function() -> close t)
let rec close_time() =
match !curtime with
| [] -> ()
| t :: _ -> close t