-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathstate.py
75 lines (61 loc) · 2.11 KB
/
state.py
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
import eel
import random as rnd, copy as cpy, pickle as pkl, io, algorecord as rec
execution = {}
class Jam:
def state(self, n):
state_at_step_n = State()
state_dict, _ = get_state(n)
for var, val in state_dict.items():
setattr(state_at_step_n, var, val)
return state_at_step_n
def visits(self, n):
if 0 <= n < len(execution['visits']):
return execution['visits'][n]
else:
return []
def line(self, s):
return execution['lines'][s]
def stack(self, n):
return [] # TODO: This needs some refactoring in the C++ module to work...
class State:
pass
def execution_start():
execution['lines'] = []
execution['visits'] = []
def execution_report(data):
shift = len(execution['lines'])
execution['lines'] += data['line']
for i, l in enumerate(data['line']):
if l > len(execution['visits']):
for _ in range(len(execution['visits']), l):
execution['visits'].append([])
execution['visits'][l - 1].append(i + shift);
@eel.expose
def get_all_variables():
var_names = [str(s) for s in rec.get_all_variables()] # weird bugs here...
return sorted(var_names)
def get_state(step):
# Could move this whole function into C++ ext for performance...
milestone = rec.get_milestone(step)
obj = {0: None}
bytes_io = io.BytesIO(milestone['pickle_bytes'].getvalue())
unpickler = pkl.Unpickler(bytes_io)
for obj_id in milestone['pickle_order']:
try:
obj[obj_id] = unpickler.load()
except:
obj[obj_id] = '<pickle error>'
state = {}
for s, op, o, k, v in milestone['assignments']:
# This is a sort of mini VM that re-executes all assignments
if s <= step:
if v in obj:
if op == 0: # o = v
state[obj[o]] = obj[v]
elif op == 1: # o[k] = v
obj[o][obj[k]] = obj[v]
elif op == 2: # o.k = v
setattr(obj[o], obj[k], obj[v])
else:
break
return state, {}