-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheval.view.ts
235 lines (182 loc) · 4.88 KB
/
eval.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
namespace $.$$ {
export class $hyoo_js_eval extends $.$hyoo_js_eval {
@ $mol_mem
code( next?: string ) {
return this.$.$mol_state_arg.value( 'code', next ) ?? ''
}
@ $mol_mem
run( next?: boolean ) {
return this.$.$mol_state_arg.value( 'run', next?.valueOf && String( next ) ) === 'true'
}
submit() {
this.run( true )
}
@ $mol_mem
perf() {
const sources = encodeURIComponent(JSON.stringify([ this.code() ]))
return `https://perf.js.hyoo.ru/#!sources=${sources}`
}
@ $mol_mem
pages() {
return [
this.Menu_page(),
this.Code_page(),
... this.run() ? [ this.Result_page() ] : [],
]
}
@ $mol_mem
bookmark_list( next?: string[] ) {
return this.$.$mol_state_local.value( 'snippets', next ) ?? super.bookmark_list()
}
@ $mol_mem
bookmark( next?: boolean ) {
const prev = this.bookmark_list()
const code = this.code()
if( next === undefined ) {
return prev.includes( code )
} else {
const list = prev.filter( str => str !== code )
if( next ) list.unshift( code )
this.bookmark_list( list )
return next
}
}
@ $mol_mem
menu() {
return this.bookmark_list().map( (_, index )=> this.Menu_link( index ) )
}
menu_link_code( index: number ) {
return this.bookmark_list()[ index ]
}
@ $mol_mem_key
menu_link_title( index: number ) {
return this.bookmark_list()[ index ]
.replace( /\n[\s\S]*/, '' )
.replace( /^\/\/ +/, '' )
}
@ $mol_mem
code_enhanced() {
let code = this.code()
code = code.replaceAll(
/^([ \t]*)(?:const|var|let|class|function) +(\w+)/mig,
( found, indent, name )=> `__spy__( "${indent}${name} =", ()=>[ ${name} ] );${found}`
)
return code
}
@ $mol_mem
execute() {
if( !this.run() ) return []
this.code()
this.result([])
clearTimeout( this._defer_spy )
this.spy_queue.length = 0
const console = new Proxy( this.$.console, {
get: ( target, field: keyof Console )=> {
if( typeof target[ field ] !== 'function' ) return target[ field ]
return ( ... args: any[] )=> {
this.spy( `${String(field)}:`, ()=> [ ... args ] )
return ( target[ field ] as any )( ... args )
}
}
} )
const __spy__ = this.spy.bind( this )
let __res__: any[]
try {
__res__ = [ '=', eval( this.code_enhanced() ) ]
} catch( error ) {
__res__ = [ '=', error ]
}
__spy__( '=', ()=> __res__.slice(1) )
this.spy_run()
return __res__
}
@ $mol_mem
error_pos() {
const [ eq, val ] = this.execute()
if(!( val instanceof Error )) return null
const pos = val.stack!.match( /(?:<anonymous>| eval).*:(\d+:\d+)/ )
if( !pos ) return null
const [ line, col ] = pos[1].split( ':' ).map( Number )
const row = this.Code().View().Row( line )
const shift = this.code_enhanced().split('\n')[ line - 1 ]
?.match( /^\w*__spy__\( .*?\);/ )?.[0]?.length ?? 0
return row.find_pos( col - 1 - shift )
}
error_anchor() {
return this.error_pos()?.token
}
@ $mol_mem
error_offset() {
const pos = this.error_pos()!
return [ pos.offset / pos.token.haystack().length, 0 ]
}
error_message() {
return this.execute()[1]?.message
}
Error_mark() {
return this.run() ? super.Error_mark() : null as any
}
spy_queue = [] as [ string, ()=> any[] ][]
_defer_spy: any = 0
@ $mol_action
spy_run() {
if( !this.run() ) return
this.result([
... this.result(),
... this.spy_queue.splice(0).map( ([ name, task ])=> {
try {
return ( [ name ] as any[] ).concat( task() )
} catch( error ) {
if( error instanceof ReferenceError ) {
this.spy_queue.push([ name, task ])
if( !this._defer_spy ) {
this._defer_spy = setTimeout( ()=> {
this._defer_spy = 0
this.spy_run()
}, 100 )
}
} else {
return [ name, error ]
}
}
} ).filter( Boolean ),
])
}
spy( name: string, task: ()=> any[] ) {
this.spy_queue.push([ name, task ])
if( this.spy_queue.length > 1 ) return
Promise.resolve().then( ()=> this.spy_run() )
}
@ $mol_mem
result( next = [] as any[] ) {
return next
}
@ $mol_mem
rejection_listener() {
return new $mol_dom_listener(
window,
'unhandledrejection',
( event: PromiseRejectionEvent )=> {
this.spy( 'Unhandled', ()=> event.reason )
}
)
}
@ $mol_mem
logs() {
this.rejection_listener()
this.execute()
return this.result().map( (_,index)=> this.Log( index ) )
}
@ $mol_mem_key
log( index: number ) {
return this.result()[ index ]
}
html( next = '' ) {
const root = this.UI().dom_node() as HTMLElement
root.innerHTML = next
return new Proxy( ( query: string )=> root.querySelector( query ), {
get: ( $, id: string )=> $( '#' + id ),
} )
}
}
}