/
camlinternalOO.ml.patch
303 lines (283 loc) · 10.7 KB
/
camlinternalOO.ml.patch
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
--- camlinternalOO.ml.orig 2010-08-18 14:53:57.000000000 -0400
+++ camlinternalOO.ml 2010-08-19 15:43:41.000000000 -0400
@@ -1,3 +1,26 @@
+(*
+ * This file is part of ocamljs, OCaml to Javascript compiler
+ * Copyright (C) 2007-9 Skydeck, Inc
+ * Copyright (C) 2010 Jake Donham
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA
+ *)
+
+open Ocamljs.Inline
+
(***********************************************************************)
(* *)
(* Objective Caml *)
@@ -29,9 +52,15 @@
(**** Object copy ****)
let copy o =
- let o = (Obj.obj (Obj.dup (Obj.repr o))) in
- set_id o last_id;
- o
+ let c = << new $o$.constructor() >> in
+ <:stmt<
+ var s = $o$._s;
+ var i;
+ for (i=0; i < s; i++)
+ $c$[i] = $o$[i];
+ >>;
+ set_id c last_id;
+ c
(**** Compression options ****)
(* Parameters *)
@@ -69,21 +98,11 @@
type closure = item
type t = DummyA | DummyB | DummyC of int
type obj = t array
-external ret : (obj -> 'a) -> closure = "%identity"
+external ret : (obj -> 'a) -> closure = "$dummyargfun"
(**** Labels ****)
-let public_method_label s : tag =
- let accu = ref 0 in
- for i = 0 to String.length s - 1 do
- accu := 223 * !accu + Char.code s.[i]
- done;
- (* reduce to 31 bits *)
- accu := !accu land (1 lsl 31 - 1);
- (* make it signed for 64 bits architectures *)
- let tag = if !accu > 0x3FFFFFFF then !accu - (1 lsl 31) else !accu in
- (* Printf.eprintf "%s = %d\n" s tag; flush stderr; *)
- magic tag
+let public_method_label s : tag = failwith "unused"
(**** Sparse array ****)
@@ -106,7 +125,8 @@
label list * string list) list;
mutable hidden_meths: (label * item) list;
mutable vars: vars;
- mutable initializers: (obj -> unit) list }
+ mutable initializers: (obj -> unit) list;
+ mutable constructor: Obj.t }
let dummy_table =
{ methods = [| dummy_item |];
@@ -116,7 +136,8 @@
hidden_meths = [];
vars = Vars.empty;
initializers = [];
- size = 0 }
+ size = 0;
+ constructor = Obj.magic 0; }
let table_count = ref 0
@@ -129,21 +150,17 @@
if n <= 2 then n else
fit_size ((n+1)/2) * 2
-let new_table pub_labels =
+let new_table len =
incr table_count;
- let len = Array.length pub_labels in
- let methods = Array.create (len*2+2) dummy_met in
- methods.(0) <- magic len;
- methods.(1) <- magic (fit_size len * Sys.word_size / 8 - 1);
- for i = 0 to len - 1 do methods.(i*2+3) <- magic pub_labels.(i) done;
- { methods = methods;
+ { methods = Array.create len dummy_met;
methods_by_name = Meths.empty;
methods_by_label = Labs.empty;
previous_states = [];
hidden_meths = [];
vars = Vars.empty;
initializers = [];
- size = initial_object_size }
+ size = initial_object_size;
+ constructor = Obj.magic 0; }
let resize array new_size =
let old_size = Array.length array.methods in
@@ -300,13 +317,12 @@
*)
let create_table public_methods =
- if public_methods == magic 0 then new_table [||] else
+ if public_methods == magic 0 then new_table 0 else
(* [public_methods] must be in ascending order for bytecode *)
- let tags = Array.map public_method_label public_methods in
- let table = new_table tags in
+ let table = new_table (Array.length public_methods) in
Array.iteri
(fun i met ->
- let lab = i*2+2 in
+ let lab = i in
table.methods_by_name <- Meths.add met lab table.methods_by_name;
table.methods_by_label <- Labs.add lab true table.methods_by_label)
public_methods;
@@ -315,7 +331,15 @@
let init_class table =
inst_var_count := !inst_var_count + table.size - 1;
table.initializers <- List.rev table.initializers;
- resize table (3 + magic table.methods.(1) * 16 / Sys.word_size)
+ table.constructor <- << function () { } >>;
+ Meths.iter
+ (fun met lab ->
+ <:stmt< $table.constructor$.prototype[$met$] = $table.methods$[$lab$]; >>)
+ table.methods_by_name;
+ <:stmt<
+ $table.constructor$.prototype._m = $table.methods$;
+ $table.constructor$.prototype._s = $table.size$;
+ >>
let inherits cla vals virt_meths concr_meths (_, super, _, env) top =
narrow cla vals virt_meths concr_meths;
@@ -352,18 +376,14 @@
let create_object table =
(* XXX Appel de [obj_block] *)
- let obj = Obj.new_block Obj.object_tag table.size in
- (* XXX Appel de [caml_modify] *)
- Obj.set_field obj 0 (Obj.repr table.methods);
+ let obj = << new $table.constructor$() >> in
set_id obj last_id;
(Obj.obj obj)
let create_object_opt obj_0 table =
if (Obj.magic obj_0 : bool) then obj_0 else begin
(* XXX Appel de [obj_block] *)
- let obj = Obj.new_block Obj.object_tag table.size in
- (* XXX Appel de [caml_modify] *)
- Obj.set_field obj 0 (Obj.repr table.methods);
+ let obj = << new $table.constructor$() >> in
set_id obj last_id;
(Obj.obj obj)
end
@@ -371,7 +391,7 @@
let rec iter_f obj =
function
[] -> ()
- | f::l -> f obj; iter_f obj l
+ | f::l -> <:stmt< _m($f$, $obj$, []); >>; iter_f obj l
let run_initializers obj table =
let inits = table.initializers in
@@ -439,61 +459,63 @@
(**** builtin methods ****)
-let get_const x = ret (fun obj -> x)
-let get_var n = ret (fun obj -> Array.unsafe_get obj n)
+let get_const x =
+ (*
+ hack so if you say
+ let foo x = ...
+ method foo = foo
+ then the result can be called from Javascript with
+ o.foo(x)
+ instead of
+ o.foo()(x)
+ *)
+ << typeof $x$ === "function" ? $x$ : $ret (fun obj -> x)$ >>
+
+let get_var n = ret (fun obj -> Array.unsafe_get << this >> n)
let get_env e n =
ret (fun obj ->
- Array.unsafe_get (Obj.magic (Array.unsafe_get obj e) : obj) n)
-let get_meth n = ret (fun obj -> sendself obj n)
-let set_var n = ret (fun obj x -> Array.unsafe_set obj n x)
+ Array.unsafe_get (Obj.magic (Array.unsafe_get << this >> e) : obj) n)
+let get_meth n = ret (fun obj -> sendself << this >> n)
+let set_var n = ret (fun obj x -> Array.unsafe_set << this >> n x)
let app_const f x = ret (fun obj -> f x)
-let app_var f n = ret (fun obj -> f (Array.unsafe_get obj n))
+let app_var f n = ret (fun obj -> f (Array.unsafe_get << this >> n))
let app_env f e n =
ret (fun obj ->
- f (Array.unsafe_get (Obj.magic (Array.unsafe_get obj e) : obj) n))
-let app_meth f n = ret (fun obj -> f (sendself obj n))
+ f (Array.unsafe_get (Obj.magic (Array.unsafe_get << this >> e) : obj) n))
+let app_meth f n = ret (fun obj -> f (sendself << this >> n))
let app_const_const f x y = ret (fun obj -> f x y)
-let app_const_var f x n = ret (fun obj -> f x (Array.unsafe_get obj n))
-let app_const_meth f x n = ret (fun obj -> f x (sendself obj n))
-let app_var_const f n x = ret (fun obj -> f (Array.unsafe_get obj n) x)
-let app_meth_const f n x = ret (fun obj -> f (sendself obj n) x)
+let app_const_var f x n = ret (fun obj -> f x (Array.unsafe_get << this >> n))
+let app_const_meth f x n = ret (fun obj -> f x (sendself << this >> n))
+let app_var_const f n x = ret (fun obj -> f (Array.unsafe_get << this >> n) x)
+let app_meth_const f n x = ret (fun obj -> f (sendself << this >> n) x)
let app_const_env f x e n =
ret (fun obj ->
- f x (Array.unsafe_get (Obj.magic (Array.unsafe_get obj e) : obj) n))
+ f x (Array.unsafe_get (Obj.magic (Array.unsafe_get << this >> e) : obj) n))
let app_env_const f e n x =
ret (fun obj ->
- f (Array.unsafe_get (Obj.magic (Array.unsafe_get obj e) : obj) n) x)
-let meth_app_const n x = ret (fun obj -> (sendself obj n : _ -> _) x)
+ f (Array.unsafe_get (Obj.magic (Array.unsafe_get << this >> e) : obj) n) x)
+let meth_app_const n x = ret (fun obj -> (sendself << this >> n : _ -> _) x)
let meth_app_var n m =
- ret (fun obj -> (sendself obj n : _ -> _) (Array.unsafe_get obj m))
+ ret (fun obj -> (sendself << this >> n : _ -> _) (Array.unsafe_get << this >> m))
let meth_app_env n e m =
- ret (fun obj -> (sendself obj n : _ -> _)
- (Array.unsafe_get (Obj.magic (Array.unsafe_get obj e) : obj) m))
+ ret (fun obj -> (sendself << this >> n : _ -> _)
+ (Array.unsafe_get (Obj.magic (Array.unsafe_get << this >> e) : obj) m))
let meth_app_meth n m =
- ret (fun obj -> (sendself obj n : _ -> _) (sendself obj m))
-let send_const m x c =
- ret (fun obj -> sendcache x m (Array.unsafe_get obj 0) c)
-let send_var m n c =
+ ret (fun obj -> (sendself << this >> n : _ -> _) (sendself << this >> m))
+let send_const m x =
+ ret (fun obj -> send x m)
+let send_var m n =
ret (fun obj ->
- sendcache (Obj.magic (Array.unsafe_get obj n) : obj) m
- (Array.unsafe_get obj 0) c)
-let send_env m e n c =
+ send (Obj.magic (Array.unsafe_get << this >> n) : obj) m)
+let send_env m e n =
ret (fun obj ->
- sendcache
+ send
(Obj.magic (Array.unsafe_get
- (Obj.magic (Array.unsafe_get obj e) : obj) n) : obj)
- m (Array.unsafe_get obj 0) c)
-let send_meth m n c =
+ (Obj.magic (Array.unsafe_get << this >> e) : obj) n) : obj)
+ m)
+let send_meth m n =
ret (fun obj ->
- sendcache (sendself obj n) m (Array.unsafe_get obj 0) c)
-let new_cache table =
- let n = new_method table in
- let n =
- if n mod 2 = 0 || n > 2 + magic table.methods.(1) * 16 / Sys.word_size
- then n else new_method table
- in
- table.methods.(n) <- Obj.magic 0;
- n
+ send (sendself << this >> n) m)
type impl =
GetConst
@@ -560,14 +582,14 @@
| MethAppMeth ->
let n = next() and m = next() in meth_app_meth n m
| SendConst ->
- let m = next() and x = next() in send_const m x (new_cache table)
+ let m = next() and x = next() in send_const m x
| SendVar ->
- let m = next() and n = next () in send_var m n (new_cache table)
+ let m = next() and n = next () in send_var m n
| SendEnv ->
let m = next() and e = next() and n = next() in
- send_env m e n (new_cache table)
+ send_env m e n
| SendMeth ->
- let m = next() and n = next () in send_meth m n (new_cache table)
+ let m = next() and n = next () in send_meth m n
| Closure _ as clo -> magic clo
let set_methods table methods =