forked from dherman/es4
-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval.sml
5373 lines (4800 loc) · 183 KB
/
eval.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(* -*- mode: sml; mode: font-lock; tab-width: 4; insert-tabs-mode: nil; indent-tabs-mode: nil -*- *)
structure Control = Control (type RESULT = Mach.GENERATOR_SIGNAL);
structure Eval = struct
(*
* 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.
*)
open Name
open Ast
open Mach
open LogErr
fun log (ss:string list) = LogErr.log ("[eval] " :: ss)
fun getObjId (obj:OBJECT)
: OBJECT_IDENTIFIER =
let
val Object { ident, ... } = obj
in
ident
end
val doTrace = ref false
val doTraceConstruct = ref false
fun fmtName n = if (!doTrace orelse !doTraceConstruct) then LogErr.name n else ""
fun fmtType n = if (!doTrace orelse !doTraceConstruct) then LogErr.ty n else ""
fun fmtNameExpr n = if (!doTrace orelse !doTraceConstruct) then LogErr.nameExpr n else ""
fun fmtObjId obj = if (!doTrace orelse !doTraceConstruct) then (Int.toString (getObjId obj)) else ""
fun trace (ss:string list) =
if (!doTrace) then log ss else ()
fun traceConstruct (ss:string list) =
if (!doTraceConstruct) then log ss else ()
fun error (regs:REGS)
(ss:string list) =
(LogErr.log ("[stack] " :: [stackString (stackOf regs)]);
evalError ss)
fun normalize (regs:REGS)
(ty:TYPE)
: TYPE =
let
val { scope, rootFixtureMap, ... } = regs
val fixtureMaps = getFixtureMaps regs scope
in
Type.normalize fixtureMaps ty
end
fun evalTy (regs:REGS)
(ty:TYPE)
: TYPE = normalize regs ty
(* Exceptions for object-language control transfer. *)
exception ContinueException of (IDENTIFIER option)
exception BreakException of (IDENTIFIER option)
exception ThrowException of VALUE
exception ReturnException of VALUE
exception StopIterationException
exception InternalError
fun throwExn (v:VALUE)
: 'a =
raise (ThrowException v)
infix 4 <*;
fun tsub <* tsup = Type.compatibleSubtype tsub tsup
fun mathOp (v:VALUE)
(decimalFn:(Decimal.DEC -> 'a) option)
(doubleFn:(Real64.real -> 'a) option)
(default:'a)
: 'a =
let
fun fnOrDefault fo v = case fo of
NONE => default
| SOME f => f v
in
if isDecimal v
then fnOrDefault decimalFn (needDecimal v)
else
if isDouble v
then fnOrDefault doubleFn (needDouble v)
else default
end
(*
Extend a scope object (p) with object (ob) of kind (kind)
*)
fun extendScope (p:SCOPE)
(ob:OBJECT)
(kind:SCOPE_KIND)
: SCOPE =
let
val parent = SOME p
val temps = ref []
in
Scope { parent = parent,
object = ob,
temps = temps,
kind = kind }
end
(*
Extend the scope in registers (r) with object (ob) of kind (kind)
*)
fun extendScopeReg (r:REGS)
(ob:OBJECT)
(kind:SCOPE_KIND)
: REGS =
let
val { scope, this, thisFun, thisGenerator, global, rootFixtureMap, aux } = r
val scope = extendScope scope ob kind
in
{ scope = scope,
this = this,
thisFun = thisFun,
thisGenerator = thisGenerator,
global = global,
rootFixtureMap = rootFixtureMap,
aux = aux }
end
fun withThis (r:REGS)
(newThis:OBJECT)
: REGS =
let
val { scope, this, thisFun, thisGenerator, global, rootFixtureMap, aux } = r
in
{ scope = scope,
this = newThis,
thisFun = thisFun,
thisGenerator = thisGenerator,
global = global,
rootFixtureMap = rootFixtureMap,
aux = aux }
end
fun withThisFun (r:REGS)
(newThisFun:OBJECT option)
: REGS =
let
val { scope, this, thisFun, thisGenerator, global, rootFixtureMap, aux } = r
in
{ scope = scope,
this = this,
thisFun = newThisFun,
thisGenerator = thisGenerator,
global = global,
rootFixtureMap = rootFixtureMap,
aux = aux }
end
fun withThisGenerator (r:REGS)
(newThisGenerator:OBJECT option)
: REGS =
let
val { scope, this, thisFun, thisGenerator, global, rootFixtureMap, aux } = r
in
{ scope = scope,
this = this,
thisFun = thisFun,
thisGenerator = newThisGenerator,
global = global,
rootFixtureMap = rootFixtureMap,
aux = aux }
end
fun withScope (r:REGS)
(newScope:SCOPE)
: REGS =
let
val { scope, this, thisFun, thisGenerator, global, rootFixtureMap, aux } = r
in
{ scope = newScope,
this = this,
thisFun = thisFun,
thisGenerator = thisGenerator,
global = global,
rootFixtureMap = rootFixtureMap,
aux = aux }
end
fun withRootFixtureMap (r:REGS)
(rootFixtureMap:FIXTURE_MAP)
: REGS =
let
val { scope, this, thisFun, thisGenerator, global, aux, ... } = r
in
{ scope = scope,
this = this,
thisFun = thisFun,
thisGenerator = thisGenerator,
global = global,
rootFixtureMap = rootFixtureMap,
aux = aux }
end
fun slotObjId (regs:REGS)
(slotFunc:REGS -> (OBJECT option) ref)
: OBJECT_IDENTIFIER =
let
val slot = slotFunc regs
val slotVal = !slot
in
case slotVal of
SOME obj => getObjId obj
| NONE => ~1
end
fun getScopeObj (scope:SCOPE)
: OBJECT =
let
val Scope { object, ... } = scope
in
object
end
fun getScopeId (scope:SCOPE)
: OBJECT_IDENTIFIER =
let
val scopeObj = getScopeObj scope
in
getObjId scopeObj
end
fun getScopeTemps (scope:SCOPE)
: TEMPS =
let
val Scope { temps, ... } = scope
in
temps
end
fun getTemps (regs:REGS)
: TEMPS =
let
val { scope, ... } = regs
in
getScopeTemps scope
end
(*
* A small number of functions do not fully evaluate to VALUE
* values, but instead to REFs; these are temporary artifacts of
* evaluation, not first-class values in the language.
*)
type REF = (OBJECT * NAME)
datatype NUMBER_TYPE = DoubleNum | DecimalNum
fun promoteToCommon (regs:REGS)
(a:NUMBER_TYPE)
(b:NUMBER_TYPE)
: NUMBER_TYPE =
if a = b
then a
else DecimalNum
val specialBindings = [
(* NB: there are order constraints on this list.
*
* This list dictates the order of reification during booting, which
* interacts with the wiring up of shared prototypes: if X.__proto__ is
* supposed to be initialized form Y.__proto__, then Y needs to precede
* X in this list.
*)
(public_Function, getFunctionClassSlot),
(public_Object, getObjectClassSlot),
(public_Array, getArrayClassSlot),
(intrinsic_Type, getTypeInterfaceSlot),
(ES4_Namespace, getNamespaceClassSlot),
(ES4_string, getStringClassSlot),
(public_String, getStringWrapperClassSlot),
(ES4_double, getDoubleClassSlot),
(ES4_decimal, getDecimalClassSlot),
(public_Number, getNumberClassSlot),
(ES4_boolean, getBooleanClassSlot),
(public_Boolean, getBooleanWrapperClassSlot),
(helper_GeneratorImpl, getGeneratorClassSlot),
(helper_Arguments, getArgumentsClassSlot)
]
(* Fundamental object methods *)
fun allocTemps (regs:REGS)
(temps:TEMPS)
(f:FIXTURE_MAP)
: unit =
let
fun allocFixture (n, f) =
case n of
TempName t => allocTemp regs f t temps
| PropName pn => ()
in
List.app allocFixture f
end
and allocScopeTemps (regs:REGS)
(fixtureMap:FIXTURE_MAP)
: unit =
let
val { scope, ... } = regs
val Scope { object, temps, ... } = scope
fun allocTempFromFixture (n, f) =
case n of
TempName t => allocTemp regs f t temps
| PropName pn => ()
in
List.app allocTempFromFixture fixtureMap
end
and allocTemp (regs:REGS)
(f:FIXTURE)
(t:int)
(temps:TEMPS)
: unit =
let
val ty = case f of
ValFixture { ty, ... } => ty
| _ => error regs ["allocating non-value temporary"]
val tmps = !temps
val tlen = List.length tmps
val emptyTmp = (AnyType, UninitTemp)
in
if t = tlen
then
let
val newTemps = tmps @ [emptyTmp]
in
traceConstruct ["allocating temp #", Int.toString t];
temps := newTemps
end
else
if t < tlen
then
let
val prefix = List.take (tmps, t-1)
val suffix = List.drop (tmps, t)
val tempTy = evalTy regs ty
val tempCell = (tempTy, UninitTemp)
val newTemps = prefix @ (tempCell :: suffix)
in
traceConstruct ["reallocating temp #", Int.toString t, " of ", Int.toString tlen];
temps := newTemps
end
else
let
val prefixLen = t-tlen
val prefix = List.tabulate (prefixLen, (fn _ => emptyTmp))
val newTemps = prefix @ (emptyTmp :: tmps)
in
traceConstruct ["extending temps to cover temp #", Int.toString t];
temps := newTemps
end
end
and asArrayIndex (v:VALUE)
: Word32.word =
case v of
ObjectValue (Object { tag, ... }) =>
(case tag of
PrimitiveTag (DoublePrimitive d) =>
if isIntegral d
andalso 0.0 <= d
andalso d < 4294967295.0
then
doubleToWord d
else
0wxFFFFFFFF
| PrimitiveTag (DecimalPrimitive d) =>
0wxFFFFFFFF (* FIXME *)
| _ => 0wxFFFFFFFF)
| _ => 0wxFFFFFFFF
and hasOwnProperty (regs : REGS)
(obj : OBJECT)
(n : NAME)
: bool =
let
val Object { propertyMap, ... } = obj
in
if Fixture.hasFixture (getFixtureMap regs obj) (PropName n)
then true
else
if hasFixedProp propertyMap n then
true
else
if hasFixedProp propertyMap meta_has then
let
val v = evalNamedMethodCall regs obj meta_has [newName regs n]
in
toBoolean v
end
handle ThrowException e =>
let
val ty = typeOfVal regs e
val defaultBehaviorClassTy =
instanceType regs helper_DefaultBehaviorClass []
in
if ty <* defaultBehaviorClassTy then
hasProp propertyMap n
else
throwExn e
end
else
hasProp propertyMap n
end
and hasProperty (regs:REGS)
(obj:OBJECT)
(n:NAME)
: bool =
if hasOwnProperty regs obj n
then true
else
let
val Object { proto, ... } = obj
in
case proto of
ObjectValue p => hasProperty regs p n
| _ => false
end
and defaultValueForType (regs:REGS)
(ty:TYPE)
: VALUE option =
case evalTy regs ty of
AnyType => SOME UndefinedValue
| NullType => SOME NullValue
| UndefinedType => SOME UndefinedValue
| UnionType [] => NONE
| UnionType ts =>
let
fun firstType [] = NONE
| firstType (x::xs) =
(case defaultValueForType regs x of
NONE => firstType xs
| other => other)
fun firstSimpleType [] = firstType ts
| firstSimpleType ((AnyType)::xs) = SOME UndefinedValue
| firstSimpleType ((NullType)::xs) = SOME NullValue
| firstSimpleType ((UndefinedType)::xs) = SOME UndefinedValue
| firstSimpleType (x::xs) = firstSimpleType xs
in
firstSimpleType ts
end
| ArrayType _ => SOME NullValue
| RecordType _ => SOME NullValue
| AppType (base, _) => defaultValueForType regs base
| InstanceType (Class {name, ...}) =>
(* We cannot go via the class obj id because we may be booting! *)
if nameEq name Name.ES4_double
then SOME (newDouble regs 0.0)
else
if nameEq name Name.ES4_string
then SOME (newString regs Ustring.empty)
else
if nameEq name Name.ES4_boolean
then SOME (newBoolean regs false)
else
if nameEq name Name.ES4_decimal
then SOME (newDecimal regs Decimal.zero)
else NONE
| _ => NONE
and isNumericName (name:NAME) =
let
val { ns, id } = name
in
(ns = Name.publicNS) andalso
List.all (fn x => (#"0" <= x) andalso (x <= #"9")) (String.explode (Ustring.toAscii id))
end
(*
* *Similar to* ES-262-3 8.7.1 GetValue(V), there's
* no Reference type in ES4.
*
* Note that ES-262-3 8.6.2.1 [[Get]](P) is folded into the
* name resolution algorithm. There is no recursive-get
* operation on a single object + prototype chain.
*)
and getPropertyValue (regs:REGS)
(obj:OBJECT)
(name:NAME)
: VALUE =
getPropertyValueOrVirtual regs obj name true
and getPropertyValueOrVirtual (regs:REGS)
(obj:OBJECT)
(name:NAME)
(doVirtual:bool)
: VALUE =
let
(* INFORMATIVE *) val _ = trace ["getting property ", fmtName name, " on obj #", fmtObjId obj]
val Object { propertyMap, tag, ... } = obj
in
case findProp propertyMap name of
SOME {state=(ValueProperty v), ...}
=> v
| SOME {state=(VirtualProperty { getter, ... }), ...}
=> if doVirtual
then
case getter of
SOME g => invokeFuncClosure (withThis regs obj) g NONE []
| _ => UndefinedValue
else
UndefinedValue
| NONE =>
case Fixture.findFixture (getFixtureMap regs obj) (PropName name) of
SOME fixture
=>
(* INFORMATIVE *) (trace ["getValueOrVirtual reifying fixture ", fmtName name];
(* LPAREN *)reifyFixture regs obj name fixture;
getPropertyValueOrVirtual regs obj name doVirtual)
| NONE =>
case (isNumericName name, tag) of
(true, ArrayTag (_, SOME defaultType))
=> let
val defaultVal = defaultValueForType regs defaultType
in
case defaultVal of
NONE => throwExn (newTypeErr (* LDOTS_RPAREN *)
(* BEGIN_INFORMATIVE *)
regs ["default type for property ",
LogErr.name name,
" has no default value "])
(* END_INFORMATIVE *)
| SOME dv
=> (setPropertyValueOrVirtual regs obj name dv false;
dv)
end
| _
=> if doVirtual andalso
Fixture.hasFixture (getFixtureMap regs obj) (PropName meta_get)
then
(trace ["running meta::get(\"", (Ustring.toAscii (#id name)), "\") catchall on obj #", fmtObjId obj];(* INFORMATIVE *)
evalNamedMethodCall regs obj meta_get [newString regs (#id name)]
handle ThrowException e =>
let
val ty = typeOfVal regs e
val defaultBehaviorClassTy =
instanceType regs helper_DefaultBehaviorClass []
in
if ty <* defaultBehaviorClassTy then
getPropertyValueOrVirtual regs obj name false
else
throwExn e
end
)(* INFORMATIVE *)
else
if isDynamic regs obj
then UndefinedValue
else throwExn (newRefErr (* LDOTS_RPAREN *)
(* BEGIN_INFORMATIVE *)
regs
["attempting to get nonexistent property ",
LogErr.name name,
" from non-dynamic object"]) (* END_INFORMATIVE *)
end
and reifyFixture (regs:REGS)
(obj:OBJECT)
(name:NAME)
(fixture:FIXTURE)
: unit =
(* LDOTS *)
let
val Object { propertyMap, tag, ... } = obj
fun reifiedFixture ty newPropState writable =
let
val attrs = { removable = false,
enumerable = false,
fixed = true,
writable = writable }
val newProp = { state = newPropState,
ty = ty,
attrs = attrs }
in
addProp propertyMap name newProp;
trace ["reified fixture ", fmtName name ]
end
in
case fixture of
(NamespaceFixture ns) =>
reifiedFixture (instanceType regs ES4_Namespace []) (ValueProperty (newNamespace regs ns)) ReadOnly
| (ClassFixture c) =>
reifiedFixture (ClassType c) (ValueProperty (newClass regs c)) ReadOnly
| (InterfaceFixture i) =>
reifiedFixture (instanceType regs helper_InterfaceTypeImpl []) (ValueProperty (newInterface regs i)) ReadOnly
| (MethodFixture { func, ty, writable, inheritedFrom, ... }) =>
let
val Func { native, ... } = func
val v = if native
then (newNativeFunction regs (getNativeFunction name))
else
let
val scope = case (tag, inheritedFrom) of
(NoTag, _) => (#scope regs)
| (_, SOME class) => getObjectScope regs obj (SOME (InstanceType class))
| (_, NONE) => getObjectScope regs obj NONE
in
newFunctionFromFunc (withScope regs scope) scope func
end
in
reifiedFixture ty (ValueProperty v) ReadOnly
end
| (ValFixture {ty, writable}) =>
let
val defaultValueOption = defaultValueForType regs ty
in
case defaultValueOption of
NONE => throwExn (newRefErr regs ["attempting to reify fixture w/o default value: ",
LogErr.name name])
| SOME v => reifiedFixture ty (ValueProperty v) (if writable
then Writable
else WriteOnce)
end
| (VirtualValFixture { ty, getter, setter }) =>
let
(* FIXME: inherit scopes like in MethodFixture *)
fun scope NONE = getObjectScope regs obj NONE
| scope (SOME class) = getObjectScope regs obj (SOME (InstanceType class))
fun makeClosureOption NONE = NONE
| makeClosureOption (SOME (f, inheritedFrom)) =
SOME (newFunClosure (scope inheritedFrom) f (SOME obj))
in
reifiedFixture ty (VirtualProperty { getter = makeClosureOption getter,
setter = makeClosureOption setter })
ReadOnly
end
| _ => throwExn (newRefErr regs ["attempting to reify unknown kind of fixture",
LogErr.name name])
end
and newTypeOpFailure (regs:REGS)
(prefix:string)
(v:VALUE)
(tyExpr:TYPE)
: VALUE =
newTypeErr regs [prefix, ": val=", approx v,
" type=", ty (typeOfVal regs v),
" wanted=", ty tyExpr]
(*
throwTypeErr regs [prefix, ": val=", approx v,
" type=", ty (typeOfVal regs v),
" wanted=", ty tyExpr]
*)
and checkAndConvert (regs:REGS)
(v:VALUE)
(ty:TYPE)
: VALUE =
let
val tyExpr = evalTy regs ty
in
if evalOperatorIs regs v tyExpr
then v
else
let
val (instanceType:TYPE) =
case Type.findSpecialConversion (typeOfVal regs v) tyExpr of
NONE => throwExn (newTypeOpFailure regs "incompatible types w/o conversion" v tyExpr)
| SOME n => n
val (classTy:TYPE) = AstQuery.needInstanceType instanceType
val (classObj:OBJECT) = getClassObjOfInstanceType regs classTy
(* FIXME: this will call back on itself! *)
val converted = evalNamedMethodCall regs classObj meta_invoke [v]
in
typeCheck regs converted tyExpr
end
end
and getObjTag (obj:OBJECT)
: TAG =
let
val Object { tag, ... } = obj
in
tag
end
and isDynamic (regs:REGS)
(obj:OBJECT)
: bool =
let
fun typeIsDynamic (UnionType tys) = List.exists typeIsDynamic tys
| typeIsDynamic (ArrayType _) = true
| typeIsDynamic (FunctionType _) = true
| typeIsDynamic (RecordType _) = true
| typeIsDynamic (NonNullType t) = typeIsDynamic t
| typeIsDynamic (ClassType _) = true
| typeIsDynamic (InstanceType (Class { dynamic, ... })) = dynamic
| typeIsDynamic _ = false
in
typeIsDynamic (typeOfVal regs (ObjectValue obj))
end
and badPropAccess (regs:REGS)
(accessKind:string)
(propName:NAME)
(existingPropState:PROPERTY_STATE)
: unit =
let
val existingPropKind =
case existingPropState of
ValueProperty _ => "value"
| VirtualProperty _ => "virtual"
in
throwExn (newTypeErr regs ["bad property ", accessKind,
" on ", existingPropKind,
" ", name propName])
end
and writeProperty (regs:REGS)
(propertyMap:PROPERTY_MAP)
(name:NAME)
(v:VALUE)
(ty:TYPE)
(removable:BOOLEAN)
(enumerable:BOOLEAN)
(fixed:BOOLEAN)
(writable:WRITABILITY)
: unit =
let
val newProp = { state = ValueProperty (checkAndConvert regs v ty),
ty = ty,
attrs = { removable = removable,
enumerable = enumerable,
fixed = fixed,
writable = writable } }
in
if hasProp propertyMap name
then updateProp propertyMap name newProp
else addProp propertyMap name newProp
end
and setPropertyValueOrVirtual (regs:REGS)
(obj:OBJECT)
(name:NAME)
(v:VALUE)
(doVirtual:bool)
: unit =
let
val Object { propertyMap, tag, ... } = obj
in
case findProp propertyMap name of
SOME existingProp =>
let
val { state, attrs, ty, ... } = existingProp
val { removable, enumerable, fixed, writable } = attrs
fun writeExisting _ = writeProperty regs propertyMap name v ty
removable enumerable fixed
(case writable of
ReadOnly => ReadOnly
| WriteOnce => ReadOnly
| Writable => Writable)
in
case state of
ValueProperty _
=> writeExisting ()
| VirtualProperty { setter, ... }
=>
if doVirtual
then
case setter of
NONE => ()
| SOME s => (invokeFuncClosure (withThis regs obj) s
NONE [v]; ())
else
if writable = ReadOnly
then throwExn (newTypeErr (* LDOTS_RPAREN *)
(* INFORMATIVE *)regs ["non-virtual setPropertyValue on read-only method: ", LogErr.name name])
else writeExisting ()
end
| NONE =>
case Fixture.findFixture (getFixtureMap regs obj) (PropName name) of
SOME (ValFixture {ty, writable})
=> writeProperty regs propertyMap name v ty
false false true
(if writable
then Writable
else ReadOnly)
| SOME f
=> (trace ["setPropertyValueOrVirtual reifying fixture ", fmtName name]; (* INFORMATIVE *)
(* LPAREN *)reifyFixture regs obj name f;
setPropertyValueOrVirtual regs obj name v doVirtual)
| NONE
=>
case (isNumericName name, tag) of
(true, ArrayTag (_, SOME defaultType))
=> writeProperty regs propertyMap name v defaultType true true
false Writable
| _
=>
if
doVirtual andalso
Fixture.hasFixture (getFixtureMap regs obj)
(PropName meta_set)
then
(trace ["running meta::set(\"", (Ustring.toAscii (#id name)), (* INFORMATIVE *)
"\", ", approx v, ") catchall on obj #", fmtObjId obj]; (* INFORMATIVE *)
(* LPAREN *)(evalNamedMethodCall regs obj meta_set
[newString regs (#id name), v]; ())
handle ThrowException e =>
let
val ty = typeOfVal regs e
val defaultBehaviorClassTy =
instanceType regs helper_DefaultBehaviorClass []
in
if ty <* defaultBehaviorClassTy then
setPropertyValueOrVirtual regs obj name v false
else
throwExn e
end
)(* INFORMATIVE *)
else
if isDynamic regs obj
then writeProperty regs propertyMap name v AnyType true true
false Writable
else throwExn (newTypeErr (* LDOTS_RPAREN *)
(* INFORMATIVE *) regs ["attempting to add dynamic property to non-dynamic object"])
end
and setPropertyValue (regs:REGS)
(base:OBJECT)
(name:NAME)
(v:VALUE)
: unit =
setPropertyValueOrVirtual regs base name v true
and defValue (regs:REGS)
(base:OBJECT)
(name:NAME)
(v:VALUE)
: unit =
setPropertyValueOrVirtual regs base name v false
and deletePropertyValueOrVirtual (regs:REGS)
(obj:OBJECT)
(name:NAME)
(doVirtual:bool)
: VALUE =
let
val Object { propertyMap, tag, ... } = obj
val existingProp = findProp propertyMap name
in
case existingProp of
SOME { attrs = { fixed = true, ...}, ...}
=> newBoolean regs false
| _
=> if
doVirtual andalso
Fixture.hasFixture (getFixtureMap regs obj) (PropName meta_delete)
then
(trace ["running meta::delete(\"", (Ustring.toAscii (#id name)), (* INFORMATIVE *)
"\") catchall on obj #", fmtObjId obj]; (* INFORMATIVE *)
(* LPAREN *)(evalNamedMethodCall regs obj meta_delete
[newString regs (#id name)])
handle ThrowException e =>
let
val ty = typeOfVal regs e
val defaultBehaviorClassTy =
instanceType regs helper_DefaultBehaviorClass []
in
if ty <* defaultBehaviorClassTy then
deletePropertyValueOrVirtual regs obj name false
else
throwExn e
end
)(* INFORMATIVE *)
else
case existingProp of
SOME { attrs = { removable = true, ... }, ... }
=> (delProp propertyMap name;
newBoolean regs true)
| _
=> newBoolean regs false
end
and deletePropertyValue (regs:REGS)
(base:OBJECT)
(name:NAME)
: VALUE =
deletePropertyValueOrVirtual regs base name true
and instantiateGlobalClass (regs:REGS)
(n:NAME)
(args:VALUE list)
: VALUE =
let
val _ = traceConstruct ["instantiating global class ", fmtName n];
val (cls:VALUE) = getPropertyValue regs (#global regs) n
in
case cls of
ObjectValue ob => evalNewObj regs ob args
| _ => error regs ["global class name ", name n,
" did not resolve to object"]
end
and newExn (regs:REGS)
(name:NAME)
(args:string list)
: VALUE =
if isBooting regs
then error regs ["trapped ThrowException during boot: ",
LogErr.name name, "(", String.concat args, ")"]
else instantiateGlobalClass
regs name
[((newString regs) o Ustring.fromString o String.concat) args]
and newTypeErr (regs:REGS)
(args:string list)