Skip to content

Commit 1d95151

Browse files
author
Hongbo Zhang
committed
add recursive module runtime support first
1 parent f6fcd2a commit 1d95151

File tree

5 files changed

+182
-34
lines changed

5 files changed

+182
-34
lines changed

jscomp/runtime/.runtimedepend

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ caml_lexer.cmj : caml_lexer.cmi
5454
caml_lexer.cmx : caml_lexer.cmi
5555
caml_md5.cmj : js.cmj caml_md5.cmi
5656
caml_md5.cmx : js.cmx caml_md5.cmi
57+
caml_module.cmj :
58+
caml_module.cmx :
5759
caml_obj.cmj : js.cmj caml_obj.cmi
5860
caml_obj.cmx : js.cmx caml_obj.cmi
5961
caml_oo.cmj : js.cmj caml_oo.cmi
@@ -112,6 +114,8 @@ caml_lexer.cmo : caml_lexer.cmi
112114
caml_lexer.cmj : caml_lexer.cmi
113115
caml_md5.cmo : js.cmo caml_md5.cmi
114116
caml_md5.cmj : js.cmj caml_md5.cmi
117+
caml_module.cmo :
118+
caml_module.cmj :
115119
caml_obj.cmo : js.cmo caml_obj.cmi
116120
caml_obj.cmj : js.cmj caml_obj.cmi
117121
caml_oo.cmo : js.cmo caml_oo.cmi

jscomp/runtime/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ OTHERS= caml_array caml_string \
88
caml_float caml_lexer caml_parser caml_primitive\
99
caml_format caml_md5 caml_queue caml_hash caml_weak\
1010
caml_backtrace caml_int32 caml_gc typed_array \
11-
js_primitive caml_basic caml_oo curry
11+
js_primitive caml_basic caml_oo curry caml_module
1212

1313
SOURCE_LIST= $(OTHERS) caml_builtin_exceptions block js
1414

jscomp/runtime/caml_module.ml

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
(** Note that we have to provide a drop in replacement, since compiler internally will
26+
spit out ("CamlinternalMod".[init_mod|update_mod] unless we intercept it
27+
in the lambda layer
28+
*)
29+
let init_mod (loc : string * int * int) (shape : CamlinternalMod.shape) =
30+
let undef_module _ = raise (Undefined_recursive_module loc) in
31+
let rec loop (shape : CamlinternalMod.shape) (struct_ : Obj.t array) idx =
32+
match shape with
33+
| Function -> struct_.(idx)<-(Obj.magic undef_module)
34+
| Lazy -> struct_.(idx)<- (Obj.magic (lazy undef_module))
35+
| Class -> struct_.(idx)<- (Obj.magic (CamlinternalOO.dummy_class loc))
36+
| Module comps
37+
->
38+
let v = (Obj.magic [||]) in
39+
struct_.(idx)<- v ;
40+
let len = Array.length comps in
41+
for i = 0 to len - 1 do
42+
loop comps.(i) v i
43+
done
44+
| Value v ->
45+
struct_.(idx) <- v in
46+
let res = (Obj.magic [||] : Obj.t array) in
47+
loop shape res 0 ;
48+
res.(0)
49+
50+
external caml_update_dummy : Obj.t -> Obj.t -> unit = "caml_update_dummy"
51+
(* Note the [shape] passed between [init_mod] and [update_mod] is always the same
52+
and we assume [module] is encoded as an array
53+
*)
54+
let update_mod (shape : CamlinternalMod.shape) (o : Obj.t) (n : Obj.t) : unit =
55+
let rec aux (shape : CamlinternalMod.shape) o n parent i =
56+
match shape with
57+
| Function
58+
-> Obj.set_field parent i n
59+
60+
| Lazy
61+
| Class ->
62+
caml_update_dummy o n
63+
| Module comps
64+
->
65+
for i = 0 to Array.length comps - 1 do
66+
aux comps.(i) (Obj.field o i) (Obj.field n i) o i
67+
done
68+
| Value _ -> () in
69+
match shape with
70+
| Module comps ->
71+
for i = 0 to Array.length comps - 1 do
72+
aux comps.(i) (Obj.field o i) (Obj.field n i) o i
73+
done
74+
| _ -> assert false

jscomp/runtime/runtime.mllib

-33
This file was deleted.

lib/js/caml_module.js

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// GENERATED CODE BY BUCKLESCRIPT VERSION 0.6.1 , PLEASE EDIT WITH CARE
2+
'use strict';
3+
4+
var Caml_builtin_exceptions = require("./caml_builtin_exceptions");
5+
var Caml_obj = require("./caml_obj");
6+
var CamlinternalOO = require("./camlinternalOO");
7+
8+
function init_mod(loc, shape) {
9+
var undef_module = function () {
10+
throw [
11+
Caml_builtin_exceptions.undefined_recursive_module,
12+
loc
13+
];
14+
};
15+
var loop = function (shape, struct_, idx) {
16+
if (typeof shape === "number") {
17+
switch (shape) {
18+
case 0 :
19+
case 1 :
20+
struct_[idx] = undef_module;
21+
return /* () */0;
22+
case 2 :
23+
struct_[idx] = CamlinternalOO.dummy_class(loc);
24+
return /* () */0;
25+
26+
}
27+
}
28+
else if (shape.tag) {
29+
struct_[idx] = shape[0];
30+
return /* () */0;
31+
}
32+
else {
33+
var comps = shape[0];
34+
var v = /* array */[];
35+
struct_[idx] = v;
36+
var len = comps.length;
37+
for(var i = 0 ,i_finish = len - 1 | 0; i <= i_finish; ++i){
38+
loop(comps[i], v, i);
39+
}
40+
return /* () */0;
41+
}
42+
};
43+
var res = /* array */[];
44+
loop(shape, res, 0);
45+
return res[0];
46+
}
47+
48+
function update_mod(shape, o, n) {
49+
var aux = function (shape, o, n, parent, i) {
50+
if (typeof shape === "number") {
51+
switch (shape) {
52+
case 0 :
53+
parent[i] = n;
54+
return /* () */0;
55+
case 1 :
56+
case 2 :
57+
return Caml_obj.caml_update_dummy(o, n);
58+
59+
}
60+
}
61+
else if (shape.tag) {
62+
return /* () */0;
63+
}
64+
else {
65+
var comps = shape[0];
66+
for(var i$1 = 0 ,i_finish = comps.length - 1 | 0; i$1 <= i_finish; ++i$1){
67+
aux(comps[i$1], o[i$1], n[i$1], o, i$1);
68+
}
69+
return /* () */0;
70+
}
71+
};
72+
if (typeof shape === "number") {
73+
throw [
74+
Caml_builtin_exceptions.assert_failure,
75+
[
76+
"caml_module.ml",
77+
74,
78+
10
79+
]
80+
];
81+
}
82+
else if (shape.tag) {
83+
throw [
84+
Caml_builtin_exceptions.assert_failure,
85+
[
86+
"caml_module.ml",
87+
74,
88+
10
89+
]
90+
];
91+
}
92+
else {
93+
var comps = shape[0];
94+
for(var i = 0 ,i_finish = comps.length - 1 | 0; i <= i_finish; ++i){
95+
aux(comps[i], o[i], n[i], o, i);
96+
}
97+
return /* () */0;
98+
}
99+
}
100+
101+
exports.init_mod = init_mod;
102+
exports.update_mod = update_mod;
103+
/* No side effect */

0 commit comments

Comments
 (0)