-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopt.view.ts
306 lines (252 loc) · 6.62 KB
/
opt.view.ts
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
namespace $.$$ {
type File = {
uri: string;
code: string;
points: (Fun | NativeCall | InlinedFun)[];
};
type Fun = {
type: 'Fun';
pos: number;
name: string;
source: {uri: string; start: number; end: number};
optimizationCount: number;
asm: string;
reasons: string[];
optimized: boolean;
points: undefined
};
type NativeCall = {
type: 'NativeCall';
reasons: string[];
pos: number;
points: undefined
};
type InlinedFun = {
type: 'InlinedFun';
points: (Fun | NativeCall | InlinedFun)[];
name: string;
pos: number;
reasons: undefined;
source: {uri: string; start: number; end: number};
};
export class $hyoo_js_opt extends $.$hyoo_js_opt {
@ $mol_mem
data( next = [] as File[] ) {
return next
}
@ $mol_mem
files() {
const raw = this.data()
const files = new Map< string, File >()
for( const file of raw ) {
files.set( file.uri, file )
}
return files
}
@ $mol_mem
menu_content() {
return [ ... this.files().keys() ].map( uri => this.File( uri ) )
}
file_uri( uri: string ) {
return uri
}
file() {
const uri = this.$.$mol_state_arg.value( 'file' )
if( !uri ) return null
return this.files().get( uri ) ?? null
}
@ $mol_mem
inline_path() {
const path = this.$.$mol_state_arg.value( 'inline' )
if( !path ) return []
return path.split( ',' ).map( Number )
}
@ $mol_mem_key
point( deep: number ): InlinedFun | Fun {
if( !deep ) {
const file = this.file()!
return {
type: 'InlinedFun',
points: file.points,
name: file.uri,
pos: 0,
reasons: undefined,
source: { uri: file.uri, start: 0, end: file.code.length },
}
}
return this.point( deep - 1 ).points![ this.inline_path()[ deep - 1 ] ] as InlinedFun | Fun
}
script_title( deep: number ) {
if( !deep ) return this.file()!.uri
const point = this.point( deep )
return point.name || `Inlined #${ this.inline_path()[ deep - 1 ]}`
}
script( deep: number ) {
const point = this.point( deep )
if( 'asm' in point ) {
console.log(point)
return point.asm
}
const source = this.script_source( deep )
return this.files().get( source.uri )!.code
}
@ $mol_mem_key
script_source( deep: number ) {
const point = this.point( deep )
if( 'asm' in point ) return { uri: 'ASM', start: 0, end: point.asm.length }
return point.source
}
@ $mol_mem_key
points( deep: number ) {
return this.point( deep ).points ?? []
}
script_path( deep: number ) {
return this.inline_path().slice( 0, deep )
}
@ $mol_mem
pages() {
return [
this.Menu_page(),
... this.file() ? [
this.Script(0),
... this.inline_path().map( (_,i)=> this.Script(i+1) ),
] : [],
]
}
}
export class $hyoo_js_opt_script extends $.$hyoo_js_opt_script {
points() {
return super.points() as readonly ( Fun | NativeCall | InlinedFun )[]
}
@ $mol_mem
filters() {
const points = this.points()
const next = {} as Record< string, string >
if( points.some( point => point.type === 'Fun' ) ) {
next[ 'Opt' ] = 'Opt'
}
if( points.some( point => point.type === 'InlinedFun' ) ) {
next[ 'Inlined' ] = 'Inlined'
}
for( const point of this.points() ) {
for( const reason of ( point.reasons ?? [] ) ) {
if( !reason ) continue
next[ reason ] = reason
}
}
return next
}
@ $mol_mem
points_followers() {
return this.points().map( ( point, index )=> {
switch( point.type ) {
case 'InlinedFun': return this.Inline( index )
case 'NativeCall': return this.Native( index )
case 'Fun': return this.Func( index )
}
} )
}
@ $mol_mem
points_followers_filtered() {
const points = this.points()
return this.points_followers().filter( (_, index ) => {
const point = points[ index ]
if( point.type === 'Fun' ) return this.filter_enabled( 'Opt' )
if( point.type === 'InlinedFun' ) return this.filter_enabled( 'Inlined' )
return point.reasons.some( reason => this.filter_enabled( reason ) )
} )
}
@ $mol_mem
body() {
return [
this.Code(),
... this.points_followers_filtered(),
]
}
@ $mol_mem
Foot() {
if( !this.points().length ) return null as any
return super.Foot()
}
code() {
const source = this.source()
return this.script().slice( source.start, source.end )
}
@ $mol_mem_key
point_pos( index: number ) {
return this.Code().find_pos( this.points()[ index ].pos - this.source().start )
}
Point_anchor( index: number ) {
return this.point_pos( index )!.token
}
@ $mol_mem_key
point_offset( index: number ) {
const pos = this.point_pos( index )!
const text = pos.token.haystack()
return [ pos.offset / text.length, .1 ]
}
@ $mol_mem_key
inline_arg( index: number ) {
return {
inline: [ ... this.path(), index ].join( ',' )
}
}
@ $mol_mem
close_arg() {
const path = this.path()
return path.length
? {
inline: this.path().slice( 0, -1 ).join( ',' )
}
: {
inline: null,
file: null,
}
}
@ $mol_mem_key
point_hint( index: number ) {
const point = this.points()[ index ]
return [
... point.type === 'Fun'
? [ `${ point.optimized ? 'Optimized' : 'Deoptimized' } after ${ point.optimizationCount } attempts` ]
: [],
... point.reasons!
].join( '\n' )
}
func_attempts( index: number ) {
return ( this.points()[ index ] as Fun ).optimizationCount
}
func_optimized( index: number ) {
return ( this.points()[ index ] as Fun ).optimized
}
@ $mol_mem_key
inline_current( index: number ) {
const current = this.$.$mol_state_arg.value( 'inline' ) ?? ''
const self = this.inline_arg( index ).inline
return current === self || current.startsWith( self + ',' )
}
@ $mol_mem
jump_rows() {
const lines = new Set<$mol_text_code_line>()
const followers = this.points_followers_filtered()
for( let i =0; i < followers.length; ++ i ) {
const anchor = followers[i].Anchor()
const line = ( $mol_owning_get( anchor ) as $mol_wire_atom<any,any,any> ).host
lines.add( line )
}
return [ ... lines ]
}
@ $mol_mem
jump( next?: number ) {
const lines = this.jump_rows()
if( next === undefined ) return lines.length
if( next > lines.length ) next = 1
if( next < 1 ) next = lines.length
if( next ) this.Code().ensure_visible( lines[ next - 1 ] )
return next
}
filter_enabled( id: string, next?: boolean ) {
return this.$.$mol_state_local.value( `filter_enabled(${id})`, next ) ?? super.filter_enabled( id )
}
}
}