-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtable.lua
More file actions
1369 lines (1044 loc) · 31.3 KB
/
Copy pathtable.lua
File metadata and controls
1369 lines (1044 loc) · 31.3 KB
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
local setmetatable = _G.setmetatable
local table = _G.table
local ipairs = _G.ipairs
local tostring = _G.tostring
local assert = _G.assert
local Union = require("nattlua.types.union").Union
local Nil = require("nattlua.types.symbol").Nil
local Number = require("nattlua.types.number").Number
local LNumber = require("nattlua.types.number").LNumber
local LString = require("nattlua.types.string").LString
local String = require("nattlua.types.string").String
local ConstString = require("nattlua.types.string").ConstString
local Tuple = require("nattlua.types.tuple").Tuple
local error_messages = require("nattlua.error_messages")
local context = require("nattlua.analyzer.context")
local mutation_solver = require("nattlua.analyzer.mutation_solver")
local table_sort = require("nattlua.other.sort")
local Any = require("nattlua.types.any").Any
local math_abs = math.abs
local math_huge = math.huge
local META = require("nattlua.types.base")()
--[[#local type TBaseType = META.TBaseType]]
--[[#type TTable = META.@Self]]
--[[#type TTable.suppress = boolean]]
--[[#type TTable.mutations = Map<|
TBaseType,
List<|{scope = TBaseType, value = TBaseType, contract = TBaseType, key = TBaseType}|>
|> | false]]
--[[#type TTable.LiteralDataCache = Map<|TBaseType, TBaseType|>]]
--[[#type TTable.PotentialSelf = TBaseType | false]]
--[[#type META.@Name = "TTable"]]
META.Type = "table"
META:GetSet("Data", nil--[[# as List<|{key = TBaseType, val = TBaseType}|>]])
META:GetSet("BaseTable", nil--[[# as TTable | false]])
META:GetSet("ReferenceId", nil--[[# as string | false]])
META:GetSet("Self", nil--[[# as false | TTable]])
META:GetSet("Contracts", nil--[[# as List<|TTable|>]])
META:GetSet("CreationScope", nil--[[# as any]])
META:GetSet("AnalyzerEnvironment", false--[[# as false | "runtime" | "typesystem"]])
META:GetSet("MutationLimit", 100)
do -- comes from tbl.@Name = "my name"
META:GetSet("Name", false--[[# as false | TBaseType]])
function META:SetName(name--[[#: TBaseType | false]])
if name then assert(name:IsLiteral()) end
self.Name = name
end
end
function META:GetName()
if not self.Name then
local meta = self:GetMetaTable()
if meta and meta ~= self then return meta:GetName() end
end
return self.Name
end
do
META:GetSet("MetaTable", false--[[# as TBaseType | false]])
function META:GetMetaTable()
local contract = self:GetContract()
if contract and contract.Type == "table" and contract.MetaTable then
return contract.MetaTable
end
return self.MetaTable
end
end
function META:SetSelf(tbl--[[#: TTable]])
tbl:SetMetaTable(self)
tbl:SetContract(tbl)
self.Self = tbl
end
function META:SetSelf2(tbl)
self.Self2 = tbl
end
function META:GetSelf2()
return self.Self2
end
function META.Equal(a--[[#: TBaseType]], b--[[#: TBaseType]], visited--[[#: Map<|TBaseType, boolean|>]])
if a.Type ~= b.Type then return false, "types differ" end
if a:IsUnique() then
return a:GetUniqueID() == b:GetUniqueID(), "unique ids match"
end
do
local contract = a:GetContract()
if contract and contract.Type == "table" and contract.Name then
if not b:GetContract() or not b:GetContract().Name then
return false, "contract name mismatch"
end
-- never called
return contract.Name:GetData() == b:GetContract().Name:GetData(),
"contract names match"
end
end
if a.Name then
if not b.Name then return false, "name property mismatch" end
return a.Name:GetData() == b.Name:GetData(), "names match"
end
visited = visited or {}
if visited[a] then return true, "circular reference detected" end
visited[a] = true
local adata = a:GetData()
local bdata = b:GetData()
if #adata ~= #bdata then return false, "table size mismatch" end
local matched = {}
for i = 1, #adata do
local akv = adata[i]
local ok = false
for i = 1, #bdata do
if not matched[i] then -- Skip already matched entries
if akv.key:Equal(bdata[i].key, visited) and akv.val:Equal(bdata[i].val, visited) then
ok = true
matched[i] = true
break
end
end
end
if not ok then return false, "table key-value mismatch" end
end
return true, "all table entries match"
end
function META:GetHash(visited)
if self:IsUnique() then return "{*" .. self:GetUniqueID() .. "*}" end
local contract = self:GetContract()
if contract and contract.Type == "table" and contract.Name then
return "{*" .. contract.Name:GetData() .. "*}"
end
if self.Name then return "{*" .. self.Name:GetData() .. "*}" end
visited = visited or {}
if visited[self] then return visited[self] end
visited[self] = "*circular*"
local data = self.Data
local entries = {}
for i = 1, #data do
table.insert(entries, data[i].key:GetHash(visited) .. "=" .. data[i].val:GetHash(visited))
end
table_sort(entries)
visited[self] = "{" .. table.concat(entries, ",") .. "}"
return visited[self]
end
local level = 0
function META:__tostring()
if self.suppress then return "current_table" end
self.suppress = true
do
local contract = self:GetContract()
if contract and contract.Name then -- never called
self.suppress = false
return tostring(contract.Name:GetData())
end
end
if self.Name then
self.suppress = false
return tostring(self.Name:GetData())
end
local meta = self:GetMetaTable()
if meta then
local func = meta:Get(ConstString("__tostring"))
if func then
local analyzer = context:GetCurrentAnalyzer()
if analyzer then
local str = analyzer:GetFirstValue(analyzer:Call(func, Tuple({self})))
if str and str.Type == "string" and str:IsLiteral() then
self.suppress = false
return tostring(str:GetData())
end
end
end
end
local s = {}
level = level + 1
local indent = ("\t"):rep(level)
if #self.Data <= 1 then indent = " " end
local contract = self:GetContract()
if contract and contract.Type == "table" and contract ~= self then
local contract_data = contract:GetData()
local contract_len = #contract_data
for i = 1, contract_len do
local keyval = contract_data[i]
local table_kv = self:FindKeyValExact(keyval.key)
local key = tostring(table_kv and table_kv.key or "nil")
local val = tostring(table_kv and table_kv.val or "nil")
local tkey = tostring(keyval.key)
local tval = tostring(keyval.val)
if key == tkey then
s[i] = indent .. "[" .. key .. "]"
else
s[i] = indent .. "[" .. key .. " as " .. tkey .. "]"
end
if val == tval then
s[i] = s[i] .. " = " .. val
else
s[i] = s[i] .. " = " .. val .. " as " .. tval
end
end
else
local data = self.Data
local len = #data
for i = 1, len do
local keyval = data[i]
local key, val = tostring(keyval.key), tostring(keyval.val)
s[i] = indent .. "[" .. key .. "]" .. " = " .. val
end
end
level = level - 1
self.suppress = false
if #self.Data <= 1 then return "{" .. table.concat(s, ",") .. " }" end
return "{\n" .. table.concat(s, ",\n") .. "\n" .. ("\t"):rep(level) .. "}"
end
function META:GetArrayLength()
local contract = self:GetContract()
if contract and contract ~= self then return contract:GetArrayLength() end
local len = 0
local data = self.Data
local data_len = #data
for i = 1, data_len do
local kv = data[i]
if kv.key:IsNumeric() then
if kv.key:IsLiteral() then
-- TODO: not very accurate
if kv.key.Type == "range" then return kv.key:Copy() end
if len + 1 == kv.key:GetData() then
len = kv.key:GetData()
else
break
end
else
return kv.key
end
end
end
return LNumber(len)
end
function META:FollowsContract(contract--[[#: TTable]])
if self.suppress then return true end
if self:GetContract() == contract then return true end
if not self.Data[1] and contract:CanBeEmpty() then return true end
for _, keyval in ipairs(contract:GetData()) do
local required_key = keyval.key
if required_key:IsLiteral() then
if not keyval.val:IsNil() then
local res, err = self:FindKeyValExact(required_key)
if not res and self:GetMetaTable() then
res, err = self:GetMetaTable():FindKeyValExact(required_key)
end
if not res then return res, err end
local ok, err = res.val:IsSubsetOf(keyval.val)
if not ok then
return false,
error_messages.because(error_messages.table_key(error_messages.subset(res.key, keyval.key)), err)
end
end
else
local found_anything = false
for _, keyval2 in ipairs(self.Data) do
if keyval2.key:IsSubsetOf(required_key) then
self.suppress = true
local ok, err = keyval2.val:IsSubsetOf(keyval.val)
self.suppress = false
found_anything = true
if not ok then
return false,
error_messages.because(error_messages.table_key(error_messages.subset(keyval2.key, keyval.key)), err)
end
end
end
if not found_anything then
return false, error_messages.table_index(self, required_key)
end
end
end
local data = self.Data
local len = #data
for i = 1, len do
local keyval = data[i]
if not keyval.val:IsNil() then
local res, err = contract:FindKeyValExact(keyval.key)
-- it's ok if the key is not found, as we're doing structural checking
if res then
-- if it is found, we make sure its type matches
local ok, err = keyval.val:IsSubsetOf(res.val)
if not ok then
return false,
error_messages.because(error_messages.table_value(error_messages.subset(res.val, keyval.val)), err)
end
end
end
end
return true
end
function META:CanBeEmpty()
local data = self.Data
local len = #data
for i = 1, len do
if not data[i].val:CanBeNil() then return false end
end
return true
end
function META:IsEmpty()
if
self:GetContract() and
self:GetContract() ~= self and
self:GetContract().Type == "table"
then
return self:GetContract():IsEmpty()
end
return not self.Data[1]
end
function META.IsSubsetOf(a--[[#: TBaseType]], b--[[#: TBaseType]])
if a.suppress then return true, "suppressed" end
if b.Type == "tuple" then b = b:GetWithNumber(1) end
if b.Type == "any" then return true, "b is any" end
if b.Type == "table" then
if a == b then return true, "same type" end
local ok, err = a:IsSameUniqueType(b)
if not ok then return ok, err end
end
if b.Type == "table" then
if b:GetMetaTable() and b:GetMetaTable() == a then
return true, "same metatable"
end
if a:IsEmpty() then
if b:CanBeEmpty() then return true, "can be empty" end
return false, error_messages.subset(a, b)
end
for _, bkeyval in ipairs(b:GetData()) do
local akeyval, reason = a:FindKeyValWide(bkeyval.key, true)
if not bkeyval.val:CanBeNil() then
if not akeyval then
if b.BaseTable and b.BaseTable == a then
akeyval = bkeyval
else
return akeyval, reason
end
end
a.suppress = true
local ok, err = akeyval.val:IsSubsetOf(bkeyval.val)
a.suppress = false
if not ok then
return false,
error_messages.because(error_messages.table_subset(bkeyval.key, akeyval.key, bkeyval.val, akeyval.val), err)
end
end
end
if b:IsNumericallyIndexed() and not a:IsNumericallyIndexed() then
return false, error_messages.subset(a, b)
end
return true, "all is equal"
elseif b.Type == "union" then
for _, obj in ipairs(b.Data) do
local ok, err = a:IsSubsetOf(obj)
if ok then return true, "a is subset of one in the union" end
end
return false, error_messages.subset(a, b)
end
return false, error_messages.subset(a, b)
end
function META:ContainsAllKeysIn(contract--[[#: TTable]])
for _, keyval in ipairs(contract:GetData()) do
if keyval.key:IsLiteral() then
local ok, err = self:FindKeyValExact(keyval.key)
if not ok then
if keyval.val:CanBeNil() then return true end
return false,
error_messages.because(error_messages.key_missing_contract(keyval.key, contract), err)
end
end
end
return true
end
local function get_hash(key)
if key.Type ~= "table" and key.Type ~= "function" and key.Type ~= "tuple" then
return key:GetHash()
end
return nil
end
local function write_cache(self, key, val)
local hash = get_hash(key)
if hash then self.LiteralDataCache[hash] = val end
end
local function read_cache(self, key)
local hash = get_hash(key)
if hash then
local val = self.LiteralDataCache[hash]
if val then return val end
return false, error_messages.table_index(self, key)
end
return nil
end
local function read_cache_no_error(self, key)
local hash = get_hash(key)
if hash then return self.LiteralDataCache[hash] end
return nil
end
local function AddKey(self, keyval, key, val)
if not keyval then
local keyval = {key = key, val = val}
table.insert(self.Data, keyval)
write_cache(self, key, keyval)
else
if keyval.key:IsLiteral() and keyval.key:Equal(key) then
keyval.val = val
else
keyval.val = Union({keyval.val, val})
end
end
end
function META:RemoveRedundantNilValues()
for i = #self.Data, 1, -1 do
local keyval = self.Data[i]
if
keyval.key.Type == "number" and
keyval.val.Type == "symbol" and
keyval.val:IsNil()
then
table.remove(self.Data, i)
write_cache(self, keyval.key, nil)
else
break
end
end
end
function META:Delete(key--[[#: TBaseType]])
for i = #self.Data, 1, -1 do
local keyval = self.Data[i]
if key:Equal(keyval.key) then
table.remove(self.Data, i)
write_cache(self, keyval.key, nil)
end
end
return true
end
do
function META:Insert(val--[[#: TBaseType]])
self.size = self.size or 1
self:Set(LNumber(self.size), val)
self.size = self.size + 1
end
function META:Concat(separator--[[#: TBaseType]])
if not self:IsLiteral() then return String() end
if
separator and
(
(
separator.Type ~= "string" or
not separator:IsLiteral()
)
and
(
separator.Type ~= "symbol" or
separator:IsBoolean()
)
)
then
return String()
end
local out = {}
for i, keyval in ipairs(self.Data) do
if not keyval.val:IsLiteral() or keyval.val.Type == "union" then
return String()
end
out[i] = keyval.val:GetData()
end
return table.concat(out, separator and separator:GetData() or nil)
end
function META:Remove(index--[[#: TBaseType]])
local index_num = index:GetData()
local removed_val = nil
local found_index = nil
for i = #self.Data, 1, -1 do
local keyval = self.Data[i]
if keyval.key.Type == "number" and keyval.key:IsLiteral() then
local key_num = keyval.key:GetData()
if key_num == index_num then
removed_val = keyval.val
found_index = i
table.remove(self.Data, i)
write_cache(self, keyval.key, nil)
break
end
end
end
if not found_index then
return false, error_messages.table_index(self, index)
end
for i, keyval in ipairs(self.Data) do
if keyval.key.Type == "number" and keyval.key:IsLiteral() then
local key_num = keyval.key:GetData()
if key_num > index_num then
write_cache(self, keyval.key, nil)
local new_key = LNumber(key_num - 1)
keyval.key = new_key
write_cache(self, new_key, keyval)
end
end
end
if self.size and self.size > index_num then self.size = self.size - 1 end
return removed_val
end
end
function META:GetValueUnion()
local union = Union()
for _, keyval in ipairs(self.Data) do
union:AddType(keyval.val:Copy())
end
return union
end
function META:HasKey(key--[[#: TBaseType]])
if read_cache(self, key) then return true end
for i, keyval in ipairs(self.Data) do
if key:Equal(keyval.key) or key:IsSubsetOf(keyval.key) then return true end
end
if self.BaseTable then if self.BaseTable:HasKey(key) then return true end end
return false
end
function META:FindKeyValExact(key--[[#: TBaseType]])
local keyval, reason = read_cache(self, key)
if keyval then return keyval, reason end
local reasons = {}
for i, keyval in ipairs(self.Data) do
local ok, reason = keyval.key:IsSubsetOf(key)
if ok then return keyval end
if i <= 20 then reasons[i] = reason end
end
if not reasons[1] then
reasons[1] = error_messages.because(error_messages.table_index(self, key), {"table is empty"})
end
return false,
error_messages.because(error_messages.table_index(self, key), reasons)
end
function META:FindKeyValWide(key--[[#: TBaseType]], reverse--[[#: boolean | nil]])
local keyval = read_cache(self, key)
if keyval then return keyval end
local reasons = {}
local data = self.Data
local len = #data
for i = 1, len do
local keyval = data[i]
if key:Equal(keyval.key) then return keyval end
local ok, reason
if reverse then
ok, reason = keyval.key:IsSubsetOf(key)
else
ok, reason = key:IsSubsetOf(keyval.key)
end
if ok then return keyval end
if i <= 20 then reasons[i] = reason end
end
if #reasons > 20 then reasons = {error_messages.table_index(self, key)} end
if self.BaseTable then
local ok, reason = self.BaseTable:FindKeyValWide(key)
if ok then return ok end
table.insert(reasons, reason)
end
if not reasons[1] then
reasons[1] = error_messages.because(error_messages.table_index(self, key), {"table is empty"})
end
return false,
error_messages.because(error_messages.table_index(self, key), reasons)
end
function META:Set(key--[[#: TBaseType]], val--[[#: TBaseType | nil]], no_delete--[[#: boolean | nil]])
if key.Type == "string" and key:IsLiteral() and key:GetData():sub(1, 1) == "@" then
if
context:GetCurrentAnalyzer() and
context:GetCurrentAnalyzer():GetCurrentAnalyzerEnvironment() == "typesystem"
then
assert(self["Set" .. key:GetData():sub(2)], key:GetData() .. " is not a function")(self, val)
return true
end
end
if key.Type == "symbol" and key:IsNil() then
return false, error_messages.invalid_table_index(key)
end
if key.Type == "number" and key:IsNan() then
return false, error_messages.invalid_table_index(key)
end
-- delete entry
if not no_delete and not self:GetContract() then
if (not val or (val.Type == "symbol" and val:IsNil())) then
return self:Delete(key)
end
end
if self:GetContract() and self:GetContract().Type == "table" then -- TODO
local keyval, reason = self:GetContract():FindKeyValWide(key)
if not keyval then return keyval, reason end
local keyval, reason = val:IsSubsetOf(keyval.val)
if not keyval then return keyval, reason end
end
-- if the key exists, check if we can replace it and maybe the value
local keyval, reason = self:FindKeyValWide(key)
AddKey(self, keyval, key, val)
return true
end
function META:SetExplicit(key--[[#: TBaseType]], val--[[#: TBaseType]])
if key.Type == "string" and key:IsLiteral() and key:GetData():sub(1, 1) == "@" then
local lua_key = "Set" .. key:GetData():sub(2)
assert(self[lua_key], lua_key .. " is not a function")(self, val)
return true
end
if key.Type == "symbol" and key:IsNil() then
return false, error_messages.table_key("is nil")
end
-- if the key exists, check if we can replace it and maybe the value
AddKey(self, read_cache_no_error(self, key), key, val)
return true
end
function META:Get(key--[[#: TBaseType | TString]])
if key.Type == "string" and key:IsLiteral() and key:GetData():sub(1, 1) == "@" then
if
context:GetCurrentAnalyzer() and
context:GetCurrentAnalyzer():GetCurrentAnalyzerEnvironment() == "typesystem"
then
return assert(self["Get" .. key:GetData():sub(2)], key:GetData() .. " is not a function")(self) or
Nil()
end
end
if key.Type == "union" then
if key:IsEmpty() then return false, error_messages.union_key_empty() end
local union = Union()
local errors = {}
for _, k in ipairs(key:GetData()) do
local obj, reason = self:Get(k)
if obj then
union:AddType(obj)
else
table.insert(errors, reason)
end
end
if union:GetCardinality() == 0 then return false, errors end
return union
end
if (key.Type == "string" or key.Type == "number") and not key:IsLiteral() then
local union = Union({Nil()})
local found_non_literal = false
for _, keyval in ipairs(self.Data) do
if keyval.key.Type == "union" then
for _, ukey in ipairs(keyval.key:GetData()) do
if ukey:IsSubsetOf(key) then union:AddType(keyval.val) end
end
elseif keyval.key.Type == key.Type or keyval.key.Type == "any" then
if keyval.key:IsLiteral() then
union:AddType(keyval.val)
else
found_non_literal = true
break
end
end
end
if not found_non_literal then return union end
end
if key.Type == "range" then
local union = Union()
local min, max = key:GetMin(), key:GetMax()
local len = math_abs(min - max)
if len == math_huge or len == -math_huge then
union:AddType(Nil())
for _, keyval in ipairs(self.Data) do
if keyval.key.Type == "number" then union:AddType(keyval.val) end
end
return union
end
if len > 100 then return Any() end
for i = min, max do
local res, reason = self:Get(LNumber(i))
if not res then res = Nil() end
union:AddType(res)
end
return union
end
if key.Type == "any" then
local union = Union({Nil()})
for _, keyval in ipairs(self.Data) do
union:AddType(keyval.val)
end
return union
end
local keyval, reason = self:FindKeyValWide(key)
if keyval then return keyval.val end
if self:GetContract() then
local keyval, reason = self:GetContract():FindKeyValWide(key)
if keyval then return keyval.val end
return false, reason
end
return false, reason
end
function META:IsNumericallyIndexed()
for _, keyval in ipairs(self.Data) do
if not keyval.key:IsNumeric() then return false end -- TODO, check if there are holes?
end
return true
end
function META:CopyLiteralness(from)
if from.Type ~= self.Type then return self end
if self:Equal(from) then return self end
local self = self:Copy()
for _, keyval_from in ipairs(from:GetData()) do
local keyval, reason = self:FindKeyValExact(keyval_from.key)
if keyval then
keyval.key = keyval.key:CopyLiteralness(keyval_from.key)
keyval.val = keyval.val:CopyLiteralness(keyval_from.val)
end
end
return self
end
function META:CopyLiteralness2(from)
if from.Type ~= self.Type then return self end
if self:Equal(from) then return self end
local ref_map = {[from] = self}
for _, keyval_from in ipairs(from:GetData()) do
local keyval, reason = self:FindKeyValExact(keyval_from.key)
if keyval then
self:Delete(keyval.key)
AddKey(self, nil, keyval_from.key:Copy(ref_map), keyval_from.val:Copy(ref_map))
end
end
return self
end
function META:CoerceUntypedFunctions(from--[[#: TTable]])
for _, kv in ipairs(self.Data) do
local kv_from, reason = from:FindKeyValWide(kv.key)
if not kv_from then return nil, reason end
if kv.val.Type == "function" and kv_from.val.Type == "function" then
kv.val:SetInputSignature(kv_from.val:GetInputSignature())
kv.val:SetOutputSignature(kv_from.val:GetOutputSignature())
kv.val:SetExplicitOutputSignature(true)
kv.val:SetExplicitInputSignature(true)
kv.val:SetCalled(false)
end
end
return true
end
local function copy_val(val, map, copy_tables)
if not val then return val end
if map[val] then
-- if the specific reference is already copied, just return it
return map[val]
end
map[val] = val:Copy(map, copy_tables)
return map[val]
end
function META:Copy(map--[[#: Map<|any, any|> | nil]], copy_tables)
map = map or {}
if map[self] then return map[self] end
local copy = META.New()
map[self] = copy -- map any lua references from self to this new copy
for i, keyval in ipairs(self.Data) do
local k = copy_val(keyval.key, map, copy_tables)
local v = copy_val(keyval.val, map, copy_tables)
copy.Data[i] = {key = k, val = v}
write_cache(copy, k, copy.Data[i])
end
copy:CopyInternalsFrom(self)
if self.Self then
local tbl = self.Self
local m, c = tbl.MetaTable, tbl.Contract
tbl.MetaTable = false
tbl.Contract = false
local tbl_copy = copy_val(self.Self, map, copy_tables)
tbl.MetaTable = m
tbl.Contract = c
copy:SetSelf(tbl_copy)