-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathboot.sml
327 lines (279 loc) · 12.8 KB
/
boot.sml
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
(* -*- mode: sml; mode: font-lock; tab-width: 4; insert-tabs-mode: nil; indent-tabs-mode: nil -*- *)
(*
* The following licensing terms and conditions apply and must be
* accepted in order to use the Reference Implementation:
*
* 1. This Reference Implementation is made available to all
* interested persons on the same terms as Ecma makes available its
* standards and technical reports, as set forth at
* http://www.ecma-international.org/publications/.
*
* 2. All liability and responsibility for any use of this Reference
* Implementation rests with the user, and not with any of the parties
* who contribute to, or who own or hold any copyright in, this Reference
* Implementation.
*
* 3. THIS REFERENCE IMPLEMENTATION IS PROVIDED BY THE COPYRIGHT
* HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* End of Terms and Conditions
*
* Copyright (c) 2007 Adobe Systems Inc., The Mozilla Foundation, Opera
* Software ASA, and others.
*)
(* The ES4 "boot environment". *)
structure Boot = struct
(* Local tracing machinery *)
val doTrace = ref false
fun trace ss = if (!doTrace) then LogErr.log ("[boot] " :: ss) else ()
fun error ss = LogErr.hostError ss
fun instantiateRootClass (fullName:Ast.NAME) (prog:Ast.PROGRAM) :
(Ast.CLS * Mach.CLS_CLOSURE * Mach.OBJ * Ast.PROGRAM)
=
let
val globalObj = Eval.getGlobalObject ()
val globalRegs = Eval.getInitialRegs ()
val _ = trace ["fetching ", LogErr.name fullName, " class definition"];
val fix = Fixture.getFixture (valOf (#fixtures prog)) (Ast.PropName fullName)
val cls = case fix of
Ast.ClassFixture cls => cls
| _ => error [LogErr.name fullName, " did not resolve to a class fixture"]
val _ = trace ["allocating class ", LogErr.name fullName];
val closure = Eval.newClsClosure (#scope globalRegs) cls
val obj = Mach.newObj (Mach.ClassTag Name.intrinsic_Class) Mach.Null (SOME (Mach.Class closure))
val classRegs = Eval.extendScopeReg globalRegs obj Mach.InstanceScope
val _ = trace ["allocating class fixtures for ", LogErr.name fullName];
val Ast.Cls { classFixtures, ... } = cls
val _ = Eval.allocObjFixtures classRegs obj NONE classFixtures
val _ = trace ["binding class ", LogErr.name fullName];
val Mach.Obj { props, ... } = globalObj
val _ = Mach.addProp props fullName
{ ty = Name.typename Name.intrinsic_Class,
state = Mach.ValProp (Mach.Object obj),
attrs = { dontDelete = true,
dontEnum = true,
readOnly = true,
isFixed = true } }
(*
* We return a "residual" program for each root class, to evaluate after
* all other classes load, typically because the packages & block of the
* root prog involves init statements that hit classes like "string"
* or "boolean" that are not yet defined.
*)
val residualProg = { packages = (#packages prog),
fixtures = SOME [],
block = (#block prog) }
in
(cls, closure, obj, residualProg)
end
fun completeClassFixtures name classObj =
let
(*
* Now the weird / feedbacky part: we go find the class "Class" and allocate
* its instance fixtures on the object we just built. For non-root classes
* this happens automatically because they're *instances* of class "Class",
* but the object we build only *says* it's an instance of class "Class"; it
* hasn't actually run through any sort of normal construction protocol for
* class "Class".
*
* Note that we do this *after* we bound the object to a position in the
* global object, because we want this tying-the-knot trick to work when
* we're defining class "Class" itself, and we won't be able to find it
* by name until just now.
*)
val globalRegs = Eval.getInitialRegs ()
val classRegs = Eval.extendScopeReg globalRegs classObj Mach.InstanceScope
val classClass = Eval.findVal (#scope globalRegs) Name.intrinsic_Class
val Ast.Cls { instanceFixtures, ... } = (#cls (Mach.needClass classClass))
in
Eval.allocObjFixtures classRegs classObj (SOME classObj) instanceFixtures
end
fun runObjectConstructorOnGlobalObject objClass objClassObj objClassClosure =
let
val _ = trace ["running Object constructor on global object"];
val globalObj = Eval.getGlobalObject ()
val globalRegs = Eval.getInitialRegs ()
val Ast.Cls { instanceFixtures, ...} = objClass
val objClassRegs = Eval.extendScopeReg globalRegs objClassObj Mach.InstanceScope
val _ = Eval.allocObjFixtures objClassRegs globalObj (SOME globalObj) instanceFixtures
in
Eval.initializeAndConstruct objClassClosure objClassObj objClassRegs [] globalObj
end
fun loadFile f =
let
val _ = trace ["parsing boot file ", f]
val p = Parser.parseFile f
val _ = trace ["defining boot file ", f]
in
Defn.defProgram p
end
fun loadFiles fs =
let
fun parse f =
(trace ["parsing boot file ", f];
(f, Parser.parseFile f))
fun def (f,p) =
(trace ["defining boot file ", f];
(f, Defn.defProgram p))
in
map def (map parse fs)
end
fun verifyFiles fs =
let
fun ver (f, p) =
(trace ["verifying boot file ", f];
(f, Verify.verifyProgram p))
in
map ver fs
end
fun evalFiles fs =
let
fun eval (f, p) =
(trace ["evaluating boot file ", f];
Eval.evalTopProgram p)
in
map eval fs
end
fun printProp ((n:Ast.NAME), (p:Mach.PROP)) =
let
val ps = case (#state p) of
Mach.TypeVarProp => "[typeVar]"
| Mach.TypeProp => "[type]"
| Mach.UninitProp => "[uninit]"
| Mach.ValProp _ => "[val]"
| Mach.VirtualValProp _ => "[virtual val]"
| Mach.MethodProp _ => "[method]"
| Mach.NativeFunctionProp _ => "[native function]"
| Mach.NamespaceProp _ => "[namespace]"
| Mach.ValListProp _ => "[val list]"
in
trace [LogErr.name n, " -> ", ps]
end
(*
fun printFixture ((n:Ast.FIXTURE_NAME), (f:Ast.FIXTURE)) =
let
val fs = case f of
Ast.NamespaceFixture _ => "[namespace]"
| Ast.ClassFixture _ => "[class]"
| Ast.InterfaceFixture _ => "[interface]"
| Ast.TypeVarFixture => "[typeVar]"
| Ast.TypeFixture _ => "[type]"
| Ast.MethodFixture _ => "[method]"
| Ast.ValFixture _ => "[val]"
| Ast.VirtualValFixture _ => "[virtualVal]"
in
case n of
Ast.TempName n => trace ["temp #", Int.toString n, " -> ", fs]
| Ast.PropName n => trace [LogErr.name n, " -> ", fs]
end
*)
fun describeGlobal _ =
if !doTrace
then
(trace ["global object contents:"];
case Eval.getGlobalObject () of
Mach.Obj {props, ...} =>
NameMap.appi printProp (!props);
trace ["top fixture contents:"];
Fixture.printTopFixtures (!Defn.topFixtures))
(* List.app printFixture (!Defn.topFixtures)) *)
else ()
fun boot _ =
let
val _ = trace ["resetting top fixtures"];
val _ = Defn.resetTopFixtures ()
val _ = trace ["allocating global object"];
val globalObj = Mach.newObj (Mach.ClassTag Name.nons_Object) Mach.Null NONE
val _ = trace ["installing global object"];
val _ = Eval.resetGlobal globalObj
val _ = Eval.booting := true
(* Allocate any standard anonymous user namespaces like magic and meta. *)
val _ = Eval.allocScopeFixtures (Eval.getInitialRegs()) (Fixture.getTopFixtures (!Defn.topFixtures))
(*
* We have to do a small bit of delicate work here because the global object
* needs to get installed as the root scope *inbetween* the moment of its
* allocation and the execution of its (class Object) constructor body.
*
* There is no provision for this in the standard object-construction
* protocol Eval.constructClassInstance, so we inline it here.
*
* There are also other 3 "root" classes that require special processing
* during startup to avoid feedback loops in their definition.
*)
val _ = Native.registerNatives ();
val objProg = loadFile "builtins/Object.es"
val clsProg = loadFile "builtins/Class.es"
val funProg = loadFile "builtins/Function.es"
val otherProgs = loadFiles ["builtins/Namespace.es",
"builtins/Magic.es",
"builtins/Internal.es",
"builtins/Conversions.es",
"builtins/String.es",
"builtins/string_primitive.es",
"builtins/Name.es",
"builtins/Interface.es",
"builtins/Error.es",
"builtins/EvalError.es",
"builtins/RangeError.es",
"builtins/ReferenceError.es",
"builtins/SyntaxError.es",
"builtins/TypeError.es",
"builtins/URIError.es",
"builtins/Boolean.es",
"builtins/boolean_primitive.es",
"builtins/Number.es",
"builtins/double.es",
"builtins/int.es",
"builtins/uint.es",
"builtins/decimal.es",
"builtins/Numeric.es",
"builtins/Math.es",
"builtins/Global.es",
"builtins/Array.es", (* before Date *)
"builtins/ByteArray.es",
"builtins/Shell.es", (* before RegExp, for debugging *)
"builtins/UnicodeClasses.es",
"builtins/UnicodeCasemapping.es",
"builtins/UnicodeTbl.es",
"builtins/Unicode.es",
"builtins/RegExpCompiler.es",
"builtins/RegExpEvaluator.es",
"builtins/RegExp.es",
"builtins/Date.es",
"builtins/JSON.es"
]
val objProg = Verify.verifyProgram objProg
val clsProg = Verify.verifyProgram clsProg
val funProg = Verify.verifyProgram funProg
val otherProgs = verifyFiles otherProgs
val (objClass, objClassClosure, objClassObj, residualObjectProg) = instantiateRootClass Name.nons_Object objProg
val _ = runObjectConstructorOnGlobalObject objClass objClassObj objClassClosure
val (_, _, classClassObj, residualClassProg) = instantiateRootClass Name.intrinsic_Class clsProg
val (_, _, funClassObj, residualFunProg) = instantiateRootClass Name.nons_Function funProg
val globalRegs = Eval.getInitialRegs ()
in
completeClassFixtures Name.nons_Object objClassObj;
completeClassFixtures Name.intrinsic_Class classClassObj;
completeClassFixtures Name.nons_Function funClassObj;
Eval.initClassPrototype globalRegs objClassObj;
Eval.initClassPrototype globalRegs classClassObj;
Eval.initClassPrototype globalRegs funClassObj;
evalFiles otherProgs;
Eval.evalTopProgram residualObjectProg;
Eval.evalTopProgram residualClassProg;
Eval.evalTopProgram residualFunProg;
Eval.booting := false;
Eval.resetStack ();
describeGlobal ()
end
end