-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathxvar.lua
More file actions
2073 lines (1722 loc) · 45.8 KB
/
xvar.lua
File metadata and controls
2073 lines (1722 loc) · 45.8 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
--author cs
--04nycs@gmail.com
--
--https://github.com/ThinEureka/xvar
--created on Apri 10, 2024
--
local xvar = {}
local rawget = rawget
local rawset = rawset
local table = table
local table_insert = table.insert
local table_remove = table.remove
local setmetatable = setmetatable
local xpcall = xpcall
local type = type
local pairs = pairs
local ipairs = ipairs
local select = select
local __err_xs = {}
local __numXs = 0
-- g_allXars = {}
local __x_trace = true
-- this function assumes we're using tabMachine context stack
local function on_error(error)
local strArray = {}
local x = __err_xs[__numXs]
table_insert(strArray, error)
if x ~= nil then
local desc = xvar.desc(x, true)
table_insert(strArray, desc)
end
table_insert(strArray, "tabStack {")
for i = 1, g_getCurStackNum() do
local context = __contextStack[i].context
if context ~= nil then
table_insert(strArray, context:getDetailedPath())
end
end
table_insert(strArray, "}")
table_insert(strArray, debug.traceback("", 1))
local strError = table.concat(strArray, "\n")
printError(strError)
if fabric and fabric.getInstance and fabric:getInstance() then
fabric:getInstance():reportCustomException(strError)
end
end
xvar.pcall = function(f, x, ...)
local x_trace = __x_trace
local numXs
if x_trace then
numXs = __numXs + 1
if numXs > #__err_xs then
table_insert(__err_xs, x)
else
__err_xs[numXs] = x
end
__numXs = numXs
end
local stat, result = xpcall(f, on_error, ...)
if x_trace then
__err_xs[numXs] = false --place holder
__numXs = numXs - 1
end
return stat,result
end
local xvar_err_nil = { DEBUG_NAME = "xvar_err_nil" }
local meta_err_nil = {
__index = function(t, key)
assert(false, "accessing xvar.err_nil")
end,
__newindex = function(t, key)
assert(false, "accessing xvar.err_nil")
end,
}
setmetatable(xvar_err_nil, meta_err_nil)
local xvar_volatile = 0
local x_operators = nil
local __land = nil
local __lor = nil
local __lxor = nil
local __lnot = nil
if __xCallbacks == nil then
__xCallbacks = {}
__xCallbackSize = 0
end
local __xCallbacks = __xCallbacks
local pcall_xvar_validate = nil
local builtin_binary_ops = {
--builit in binary
__add = function(op1, op2)
if op1 == nil or op2 == nil or
op1 == xvar_err_nil or op2 == xvar_err_nil then
return xvar_err_nil
end
return op1 + op2
end,
__sub = function(op1, op2)
if op1 == nil or op2 == nil or
op1 == xvar_err_nil or op2 == xvar_err_nil then
return xvar_err_nil
end
return op1 - op2
end,
__mul = function(op1, op2)
if op1 == nil or op2 == nil or
op1 == xvar_err_nil or op2 == xvar_err_nil then
return xvar_err_nil
end
return op1 * op2
end,
__div = function(op1, op2)
if op1 == nil or op2 == nil or
op1 == xvar_err_nil or op2 == xvar_err_nil then
return xvar_err_nil
end
return op1 / op2
end,
__mod = function(op1, op2)
if op1 == nil or op2 == nil or
op1 == xvar_err_nil or op2 == xvar_err_nil then
return xvar_err_nil
end
return op1 % op2
end,
__pow = function(op1, op2)
if op1 == nil or op2 == nil or
op1 == xvar_err_nil or op2 == xvar_err_nil then
return xvar_err_nil
end
return op1 ^ op2
end,
__idiv = function(op1, op2)
if op1 == nil or op2 == nil or
op1 == xvar_err_nil or op2 == xvar_err_nil then
return xvar_err_nil
end
return op1 // op2
end,
-- __band = function(op1, op2)
-- if op1 == xvar_err_nil or op2 == xvar_err_nil then
-- return xvar_err_nil
-- end
--
--
-- return op1 & op2
-- end,
--
-- __bor = function(op1, op2)
-- if op1 == xvar_err_nil or op2 == xvar_err_nil then
-- return xvar_err_nil
-- end
--
-- return op1 | op2
-- end,
--
-- __bxor = function(op1, op2)
-- if op1 == xvar_err_nil or op2 == xvar_err_nil then
-- return xvar_err_nil
-- end
--
-- return op1 ~ op2
-- end,
__concat = function(op1, op2)
if op1 == nil or op1 == xvar_err_nil or
op2 == nil or op2 == xvar_err_nil then
return xvar_err_nil
end
return op1 .. op2
end,
-- __eq = function(op1, op2)
-- return op1 == op2
-- end,
--
-- __lt = function(op1, op2)
-- return op1 < op2
-- end,
--
-- __le = function(op1, op2)
-- return op1 <= op2
-- end,
__shl = function(op1, op2)
if op1 == nil or op2 == nil or
op1 == xvar_err_nil or op2 == xvar_err_nil then
return xvar_err_nil
end
return op1 << op2
end,
__shr = function(op1, op2)
if op1 == nil or op2 == nil or
op1 == xvar_err_nil or op2 == xvar_err_nil then
return xvar_err_nil
end
return op1 >> op2
end,
}
local builtin_unary_ops = {
--built unary
__len = function(op1)
if op1 == nil or op1 == xvar_err_nil then
return xvar_err_nil
end
if rawget(op1, "__xop") == nil then
return #op1
end
if rawget(op1, "__xdirty") then
pcall_xvar_validate(op1)
end
return #(rawget(op1, "__xvalue"))
end,
__unm = function(op1)
if op1 == nil or op1 == xvar_err_nil then
return xvar_err_nil
end
return -op1
end
}
local __index = function(op1, op2)
if op1 == nil or op2 == nil or
op1 == xvar_err_nil or op2 == xvar_err_nil then
return xvar_err_nil
end
return op1[op2]
end
local __eq = function(op1, op2)
if op1 == xvar_err_nil or op2 == xvar_err_nil then
return xvar_err_nil
end
return op1 == op2
end
local __noteq = function(op1, op2)
if op1 == xvar_err_nil or op2 == xvar_err_nil then
return xvar_err_nil
end
return op1 ~= op2
end
local __lt = function(op1, op2)
if op1 == nil or op2 == nil or
op1 == xvar_err_nil or op2 == xvar_err_nil then
return xvar_err_nil
end
return op1 < op2
end
local __le = function(op1, op2)
if op1 == nil or op2 == nil or
op1 == xvar_err_nil or op2 == xvar_err_nil then
return xvar_err_nil
end
return op1 <= op2
end
local __gt = function(op1, op2)
if op1 == nil or op2 == nil or
op1 == xvar_err_nil or op2 == xvar_err_nil then
return xvar_err_nil
end
return op1 > op2
end
local __ge = function(op1, op2)
if op1 == nil or op2 == nil or
op1 == xvar_err_nil or op2 == xvar_err_nil then
return xvar_err_nil
end
return op1 >= op2
end
local __filter = function(op1, op2)
if op1 == nil or
op1 == xvar_err_nil or op2 == xvar_err_nil then
return xvar_err_nil
end
local t = {}
for k, v in pairs(op1) do
if op2 == nil or op2(v) then
t[k] = v
end
end
return t
end
local __sort = function(op1, op2)
if op1 == nil or
op1 == xvar_err_nil or op2 == xvar_err_nil then
return xvar_err_nil
end
local t = {}
for k, v in pairs(op1) do
t[k] = v
end
if op2 ~= nil then
table.sort(t, op2)
end
return t
end
local __extend = function(op1, op2)
if op1 == nil or op2 == nil or
op1 == xvar_err_nil or op2 == xvar_err_nil then
return xvar_err_nil
end
local t = {}
for k, v in pairs(op1) do
t[k] = v
end
for k, v in pairs(op2) do
t[k] = v
end
return t
end
local __find = function(op1, op2)
if op1 == nil or op2 == nil or
op1 == xvar_err_nil or op2 == xvar_err_nil then
return xvar_err_nil
end
for k, v in pairs(op1) do
if op2(v, k) then
return v
end
end
end
local __indexof = function(op1, op2)
if op1 == nil or op2 == nil or
op1 == xvar_err_nil or op2 == xvar_err_nil then
return xvar_err_nil
end
for k, v in pairs(op1) do
if v == op2 then
return k
end
end
end
local __x_index = nil
local meta_xvar = nil
--private:
local xvar_setDirty = nil
local xvar_collectDirty = nil
local xvar_collectDebugMsg = nil
local xvar_addSink = nil
local xvar_removeSink = nil
local xvar_removeFromSources = nil
local xvar_validate = nil
-- local pcall_xvar_validate = nil
--public:
local xvar_isDirty = nil
local xvar_isVolatile = nil
local xvar_setVolatile = nil
local xvar_reset = nil
local xvar_addDirtyCallback = nil
local xvar_removeDirtyCallback = nil
local xvar_removeAllDirtyCallbacks = nil
local xvar_removeDirtyCallbackByKey = nil
local xvar_readonly = nil
local xvar_getCollectDebugMsg = nil
local xvar_op0 = nil
local xvar_op1 = nil
local xvar_op2 = nil
local xvar_opn = nil
local xvar_opx = nil
local xvar_ops = nil
local xvar_fns = nil
local __table = nil
local function setDebugInfo(x, fn, f, loc)
local opName = nil
local isBuiltInOp = false
if f ~= nil then
opName = g_xvarOpNames[f]
if opName ~= nil then
isBuiltInOp = true
end
end
if not isBuiltInOp then
if fn == nil then
local xop = rawget(x, "__xop")
if xop == 0 then
fn = "f0"
elseif xop == -1 then
fn = "fx"
elseif xop == -2 then
fn = "fs"
else
fn = "fn"
end
end
opName = fn
end
local file
local line
if loc ~= nil then
local info = debug.getinfo(1 + loc)
file = info.short_src
line = info.currentline
else
if f == nil then
file = "xvar.lua"
line = 0
else
local info = debug.getinfo(f)
file = info.short_src
line = info.linedefined
end
end
local xName = opName .. " " .. file .. ":" .. line
rawset(x, "__xname", xName)
end
xvar_op0 = function(c)
local x = {}
setmetatable(x, meta_xvar)
rawset(x, "__xop", 0)
rawset(x,"__xvalue", c)
-- rawset(x, "__xdirty", true)
if g_xvarDebug then
setDebugInfo(x, "f0", nil, 2)
end
return x
end
xvar_opn = function(f, ...)
local x = {}
setmetatable(x, meta_xvar)
-- table_insert(g_allXars, x)
local num = select("#", ...)
rawset(x,"__xop", num)
rawset(x,"__xf", f)
local op_xs = {}
for index = 1, num do
local p = select(index, ...)
local op_x
if type(p) ~= "table" or rawget(p, "__xop") == nil then
op_x = xvar_op0(p)
else
op_x = p
xvar_addSink(op_x, x)
end
table_insert(op_xs, op_x)
end
rawset(x,"__op_xs", op_xs)
rawset(x, "__xdirty", true)
if g_xvarDebug then
setDebugInfo(x, "fn", f, 2)
end
return x
end
xvar_op1 = xvar_opn
xvar_op2 = xvar_opn
xvar_opx = function(f)
local x = {}
rawset(x,"__xop", -1)
setmetatable(x, meta_xvar)
-- table_insert(g_allXars, x)
rawset(x,"__xf", f)
rawset(x, "__xdirty", true)
if g_xvarDebug then
setDebugInfo(x, "fx", f, 2)
end
return x
end
xvar_ops = function(f, structure, op_xs)
local x = {}
setmetatable(x, meta_xvar)
-- table_insert(g_allXars, x)
rawset(x,"__xop", -2)
rawset(x,"__xf", f)
local num = #op_xs
for index = 1, num do
local op_x = op_xs[index]
xvar_addSink(op_x, x)
end
rawset(x, "__op_xs", op_xs)
rawset(x,"__xstructue", structure)
rawset(x, "__xdirty", true)
if g_xvarDebug then
setDebugInfo(x, "fs", f, 2)
end
return x
end
xvar_readonly = function(x, isReadOnly)
rawset(x, "__xreadonly", isReadOnly)
end
--private
xvar_collectDebugMsg = function(x, msg)
if (not msg) then
msg = debug.traceback("", 1):gsub("\n", "#")
end
local callbacks = rawget(x, "__xdirtycallbacks")
if callbacks ~= nil then
local msgs = rawget(x, "__xdebugmsgs")
if (not msgs) then
msgs = {}
rawset(x, "__xdebugmsgs", msgs)
end
table_insert(msgs, msg)
end
local sinks = rawget(x, "__xsinks")
if sinks ~= nil then
for sink, _ in pairs(sinks) do
xvar_collectDebugMsg(sink, msg)
end
end
end
xvar_setDirty = function(x, location)
local xCallbacks = __xCallbacks
local beginSize = __xCallbackSize
xvar_collectDirty(x, location)
local endSize = __xCallbackSize
--TODO: a global enableSorting option can be added
for i = beginSize + 2, endSize do
local xCallback1 = xCallbacks[i]
local j = i - 1
while j >= beginSize + 1 and xCallbacks[j].callbackId > xCallback1.callbackId do
xCallbacks[j + 1] = xCallbacks[j]
j = j - 1
end
xCallbacks[j + 1] = xCallback1
end
for index = beginSize + 1, endSize do
-- callbackArray[index](xArray[index])
local xCallback = xCallbacks[index]
local x = xCallback.x
local callback = xCallback.callback
xCallback.x = false
xCallback.callback = false
--if g_xvarDebug then
-- --table_insert(__err_xs, x)
--end
xpcall(callback, on_error, x)
--if g_xvarDebug then
-- --table_remove(__err_xs)
--end
end
__xCallbackSize = beginSize
end
xvar_collectDirty = function(x, location, excludeX)
if not excludeX then
rawset(x, "__xdirty", true)
rawset(x, "__xdirty_location", location)
end
local callbacks = rawget(x, "__xdirtycallbacks")
if callbacks ~= nil then
for callbackId, callback in pairs(callbacks) do
local newSize = __xCallbackSize + 1
__xCallbackSize = newSize
if newSize > #__xCallbacks then
table_insert(__xCallbacks, {})
end
local xCallback = __xCallbacks[newSize]
xCallback.x = x
xCallback.callback = callback
xCallback.callbackId = callbackId
end
end
local sinks = rawget(x, "__xsinks")
if sinks ~= nil then
for sink, _ in pairs(sinks) do
if not rawget(sink, "__xdirty") then
if location == nil then
xvar_collectDirty(sink, nil)
else
local xSourceLocation = rawget(sink, "__xsourcelocation")
if xSourceLocation == nil then
xvar_collectDirty(sink, nil)
else
local miss = false
local isValue = false
local op_xs = rawget(sink, "__op_xs")
local x_i
for i, j in pairs(xSourceLocation) do
if i == 0 then
x_i = rawget(sink, "__xvalue")
else
x_i = op_xs[i]
end
local x_j = op_xs[j]
if x_i == x then
if i == 0 then
isValue = true
else
miss = true
if not rawget(x_j, "__xdirty") then
if rawget(x_j, "__xvalue") == location then
miss = false
end
end
end
break
end
end
if not miss then
if isValue then
xvar_collectDirty(sink, location, true)
else
xvar_collectDirty(sink, nil)
end
end
end
end
end
end
end
end
local meta_sinks = {__mode = "k"}
xvar_addSink = function(x, sink)
local sinks = rawget(x, "__xsinks")
if sinks == nil then
sinks = {}
setmetatable(sinks, meta_sinks)
rawset(x, "__xsinks", sinks)
end
sinks[sink] = true
end
xvar_removeSink = function(x, sink)
local sinks = rawget(x, "__xsinks")
if sinks == nil then
return
end
sinks[sink] = nil
end
xvar_removeFromSources = function(x)
local value = rawget(x, "__xvalue")
if type(value) == "table" then
if rawget(value, "__xop") ~= nil then
xvar_removeSink(value, x)
end
end
-- local xop = rawget(x, "__xop")
-- if xop <= 0 then
-- return
-- end
local op_xs = rawget(x, "__op_xs")
if op_xs ~= nil then
for _, op_x in ipairs(op_xs) do
xvar_removeSink(op_x, x)
end
end
end
--public:
xvar_isDirty = function(x)
return rawget(x, "__xdirty")
end
xvar_isVolatile = function(x)
return rawget(x, "__xvolatile")
end
xvar_setVolatile = function(x)
return rawset(x, "__xvolatile", true)
end
xvar_getCollectDebugMsg = function(x)
return rawget(x, "__xdebugmsgs")
end
pcall_xvar_validate = function(x)
--if g_xvarDebug then
-- --table_insert(__err_xs, x)
--end
--local stat = xpcall(xvar_validate, on_error, x)
local stat, result = xvar.pcall(xvar_validate, x, x)
--if g_xvarDebug then
-- --table_remove(__err_xs)
--end
if not stat then
rawset(x, "__xvalue", xvar_err_nil)
rawset(x, "__xdirty", false)
end
return result
end
--private:
xvar_validate = function(x)
local xop = rawget(x, "__xop")
if xop == 0 then
rawset(x, "__xdirty", false)
return
end
local oldValue = rawget(x, "__xvalue")
local value = nil
local op_xs = rawget(x, "__op_xs")
local f = rawget(x, "__xf")
if xop > 0 then
for _, op_x in ipairs(op_xs) do
if rawget(op_x, "__xdirty") then
pcall_xvar_validate(op_x)
end
end
if xop == 1 then
value = f(rawget(op_xs[1], "__xvalue"))
elseif xop == 2 then
value = f(rawget(op_xs[1], "__xvalue"), rawget(op_xs[2], "__xvalue"))
elseif xop == 3 then
value = f(rawget(op_xs[1], "__xvalue"), rawget(op_xs[2], "__xvalue"),
rawget(op_xs[3], "__xvalue"))
elseif xop == 4 then
value = f(rawget(op_xs[1], "__xvalue"), rawget(op_xs[2], "__xvalue"),
rawget(op_xs[3], "__xvalue"), rawget(op_xs[4], "__xvalue"))
else
local ops = {}
for index, op_x in ipairs(op_xs) do
local op = rawget(op_x, "__xvalue")
ops[index] = op
end
value = f(table.unpack(ops, 1, #op_xs))
end
elseif xop == -1 then
value = f()
elseif xop == -2 then
for _, op_x in ipairs(op_xs) do
if rawget(op_x, "__xdirty") then
pcall_xvar_validate(op_x)
end
end
local ops = {}
for index, op_x in ipairs(op_xs) do
local op = rawget(op_x, "__xvalue")
ops[index] = op
end
value = f(rawget(x, "__xstructue"), ops)
end
if oldValue ~= value then
if type(oldValue) == "table"then
if rawget(oldValue, "__xop") ~= nil then
xvar_removeSink(oldValue, x)
end
end
if type(value) == "table" then
if rawget(value, "__xop") ~= nil then
if rawget(value, "__xdirty") then
pcall_xvar_validate(value)
end
xvar_addSink(value, x)
end
end
end
rawset(x, "__xvalue", value)
rawset(x, "__xdirty", false)
rawset(x, "__xdirty_location", false)
return
end
xvar_reset = function(x, v)
if x == v then
return
end
xvar_removeFromSources(x)
local xop = nil
local vIsXvar = true
if type(v) ~= "table" then
vIsXvar = false
else
xop = rawget(v, "__xop")
if xop == nil then
vIsXvar = false
end
end
if not vIsXvar then
-- rawset(x, "__xop", 0)
rawset(x, "__xvalue", v)
rawset(x, "__op_xs", nil)
rawset(x, "__xf", nil)
if not rawget(x, "__xdirty") then
xvar_setDirty(x)
end
else
rawset(x, "__xop", xop)
rawset(x, "__xf", rawget(v, "__xf"))
local op_xs = rawget(v, "__op_xs")
if op_xs ~= nil then
for _, op_x in ipairs(op_xs) do
xvar_addSink(op_x, x)
end
end
rawset(x, "__op_xs", op_xs)
-- rawset(x, "__xsinks", rawget(v, "__xsinks"))
rawset(x, "__xvalue", rawget(v, "__xvalue"))
rawset(x,"__xstructue", rawget(v, "__xstructue"))
if not rawget(x, "__xdirty") then
xvar_setDirty(x)
end
end
--if (g_xvarDebug) then
-- --xvar_collectDebugMsg(x)
--end
end
local xvar_call_back_id = 0
xvar_addDirtyCallback = function(x, callback)
local callbacks = rawget(x, "__xdirtycallbacks")
if callbacks == nil then
callbacks = {}
rawset(x, "__xdirtycallbacks", callbacks)
end
xvar_call_back_id = xvar_call_back_id + 1
local callbackId = xvar_call_back_id
callbacks[callbackId] = callback
if rawget(x, "__xdirty") then
--if g_xvarDebug then
-- --table_insert(__err_xs, x)
--end
xpcall(callback, on_error, x)
--if g_xvarDebug then
-- --table_remove(__err_xs)
--end
end
return callbackId
end
xvar_removeDirtyCallback = function(x, key)
local callbacks = rawget(x, "__xdirtycallbacks")
if callbacks == nil then
return
end
callbacks[key] = nil
end
xvar_removeAllDirtyCallbacks = function(x, callback)
rawset(x, "__xdirtycallbacks", nil)
end
-- local xvar_index = {
-- get = function(x, key)
-- return xvar_op2(__index, x, key)
-- end,
-- }
--
local sourceLocationIndex = {[1] = 2, [0] = 2}
__x_index = function(x, key)
local y = xvar_op2(__index, x, key)
rawset(y, "__xsourcelocation", sourceLocationIndex)
return y
end
meta_xvar = {
__index = function(x, key)
if key == "x_index" then
return __x_index
end
if rawget(x, "__xdirty") then
pcall_xvar_validate(x)
end
local value = rawget(x, "__xvalue")
return value[key]
end,
--new index
__newindex = function(x, k, v)
local xreadonly = rawget(x, "__xreadonly")
if (xreadonly) then
assert(false, "donot to modify xvar data direct")
return
end
local xop = rawget(x, "__xop")
if xop ~= 0 then
assert(false)
end
local xvalue = rawget(x, "__xvalue")
if (xvalue[k] == v) then
return
end
rawset(xvalue, k, v)
rawset(x, "__xvolatile", true)
xvar_setDirty(x, k)
--if (g_xvarDebug) then
-- --xvar_collectDebugMsg(x)
--end
end,
--get value