forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathframe.rs
1972 lines (1828 loc) · 73.2 KB
/
frame.rs
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
use crate::common::{boxvec::BoxVec, lock::PyMutex};
use crate::{
builtins::{
asyncgenerator::PyAsyncGenWrappedValue,
function::{PyCell, PyCellRef, PyFunction},
tuple::{PyTuple, PyTupleTyped},
PyBaseExceptionRef, PyCode, PyCoroutine, PyDict, PyDictRef, PyGenerator, PyList, PySet,
PySlice, PyStr, PyStrInterned, PyStrRef, PyTraceback, PyType,
},
bytecode,
convert::{IntoObject, ToPyResult},
coroutine::Coro,
exceptions::ExceptionCtor,
function::{ArgMapping, Either, FuncArgs},
protocol::{PyIter, PyIterReturn},
scope::Scope,
source_code::SourceLocation,
stdlib::builtins,
vm::{Context, PyMethod},
AsObject, Py, PyObject, PyObjectRef, PyPayload, PyRef, PyResult, TryFromObject, VirtualMachine,
};
use indexmap::IndexMap;
use itertools::Itertools;
#[cfg(feature = "threading")]
use std::sync::atomic;
use std::{fmt, iter::zip};
#[derive(Clone, Debug)]
struct Block {
/// The type of block.
typ: BlockType,
/// The level of the value stack when the block was entered.
level: usize,
}
#[derive(Clone, Debug)]
enum BlockType {
Loop,
TryExcept {
handler: bytecode::Label,
},
Finally {
handler: bytecode::Label,
},
/// Active finally sequence
FinallyHandler {
reason: Option<UnwindReason>,
prev_exc: Option<PyBaseExceptionRef>,
},
ExceptHandler {
prev_exc: Option<PyBaseExceptionRef>,
},
}
pub type FrameRef = PyRef<Frame>;
/// The reason why we might be unwinding a block.
/// This could be return of function, exception being
/// raised, a break or continue being hit, etc..
#[derive(Clone, Debug)]
enum UnwindReason {
/// We are returning a value from a return statement.
Returning { value: PyObjectRef },
/// We hit an exception, so unwind any try-except and finally blocks. The exception should be
/// on top of the vm exception stack.
Raising { exception: PyBaseExceptionRef },
// NoWorries,
/// We are unwinding blocks, since we hit break
Break { target: bytecode::Label },
/// We are unwinding blocks since we hit a continue statements.
Continue { target: bytecode::Label },
}
#[derive(Debug)]
struct FrameState {
// We need 1 stack per frame
/// The main data frame of the stack machine
stack: BoxVec<PyObjectRef>,
/// Block frames, for controlling loops and exceptions
blocks: Vec<Block>,
/// index of last instruction ran
#[cfg(feature = "threading")]
lasti: u32,
}
#[cfg(feature = "threading")]
type Lasti = atomic::AtomicU32;
#[cfg(not(feature = "threading"))]
type Lasti = std::cell::Cell<u32>;
#[pyclass(module = false, name = "frame")]
pub struct Frame {
pub code: PyRef<PyCode>,
pub fastlocals: PyMutex<Box<[Option<PyObjectRef>]>>,
pub(crate) cells_frees: Box<[PyCellRef]>,
pub locals: ArgMapping,
pub globals: PyDictRef,
pub builtins: PyDictRef,
// on feature=threading, this is a duplicate of FrameState.lasti, but it's faster to do an
// atomic store than it is to do a fetch_add, for every instruction executed
/// index of last instruction ran
pub lasti: Lasti,
/// tracer function for this frame (usually is None)
pub trace: PyMutex<PyObjectRef>,
state: PyMutex<FrameState>,
// member
pub trace_lines: PyMutex<bool>,
pub temporary_refs: PyMutex<Vec<PyObjectRef>>,
}
impl PyPayload for Frame {
fn class(ctx: &Context) -> &'static Py<PyType> {
ctx.types.frame_type
}
}
// Running a frame can result in one of the below:
pub enum ExecutionResult {
Return(PyObjectRef),
Yield(PyObjectRef),
}
/// A valid execution result, or an exception
type FrameResult = PyResult<Option<ExecutionResult>>;
impl Frame {
pub(crate) fn new(
code: PyRef<PyCode>,
scope: Scope,
builtins: PyDictRef,
closure: &[PyCellRef],
vm: &VirtualMachine,
) -> Frame {
let cells_frees = std::iter::repeat_with(|| PyCell::default().into_ref(&vm.ctx))
.take(code.cellvars.len())
.chain(closure.iter().cloned())
.collect();
let state = FrameState {
stack: BoxVec::new(code.max_stackdepth as usize),
blocks: Vec::new(),
#[cfg(feature = "threading")]
lasti: 0,
};
Frame {
fastlocals: PyMutex::new(vec![None; code.varnames.len()].into_boxed_slice()),
cells_frees,
locals: scope.locals,
globals: scope.globals,
builtins,
code,
lasti: Lasti::new(0),
state: PyMutex::new(state),
trace: PyMutex::new(vm.ctx.none()),
trace_lines: PyMutex::new(true),
temporary_refs: PyMutex::new(vec![]),
}
}
pub fn current_location(&self) -> SourceLocation {
self.code.locations[self.lasti() as usize - 1]
}
pub fn lasti(&self) -> u32 {
#[cfg(feature = "threading")]
{
self.lasti.load(atomic::Ordering::Relaxed)
}
#[cfg(not(feature = "threading"))]
{
self.lasti.get()
}
}
pub fn locals(&self, vm: &VirtualMachine) -> PyResult<ArgMapping> {
let locals = &self.locals;
let code = &**self.code;
let map = &code.varnames;
let j = std::cmp::min(map.len(), code.varnames.len());
if !code.varnames.is_empty() {
let fastlocals = self.fastlocals.lock();
for (&k, v) in zip(&map[..j], &**fastlocals) {
match locals.mapping().ass_subscript(k, v.clone(), vm) {
Ok(()) => {}
Err(e) if e.fast_isinstance(vm.ctx.exceptions.key_error) => {}
Err(e) => return Err(e),
}
}
}
if !code.cellvars.is_empty() || !code.freevars.is_empty() {
let map_to_dict = |keys: &[&PyStrInterned], values: &[PyCellRef]| {
for (&k, v) in zip(keys, values) {
if let Some(value) = v.get() {
locals.mapping().ass_subscript(k, Some(value), vm)?;
} else {
match locals.mapping().ass_subscript(k, None, vm) {
Ok(()) => {}
Err(e) if e.fast_isinstance(vm.ctx.exceptions.key_error) => {}
Err(e) => return Err(e),
}
}
}
Ok(())
};
map_to_dict(&code.cellvars, &self.cells_frees)?;
if code.flags.contains(bytecode::CodeFlags::IS_OPTIMIZED) {
map_to_dict(&code.freevars, &self.cells_frees[code.cellvars.len()..])?;
}
}
Ok(locals.clone())
}
}
impl Py<Frame> {
#[inline(always)]
fn with_exec<R>(&self, f: impl FnOnce(ExecutingFrame) -> R) -> R {
let mut state = self.state.lock();
let exec = ExecutingFrame {
code: &self.code,
fastlocals: &self.fastlocals,
cells_frees: &self.cells_frees,
locals: &self.locals,
globals: &self.globals,
builtins: &self.builtins,
lasti: &self.lasti,
object: self,
state: &mut state,
};
f(exec)
}
// #[cfg_attr(feature = "flame-it", flame("Frame"))]
pub fn run(&self, vm: &VirtualMachine) -> PyResult<ExecutionResult> {
self.with_exec(|mut exec| exec.run(vm))
}
pub(crate) fn resume(
&self,
value: Option<PyObjectRef>,
vm: &VirtualMachine,
) -> PyResult<ExecutionResult> {
self.with_exec(|mut exec| {
if let Some(value) = value {
exec.push_value(value)
}
exec.run(vm)
})
}
pub(crate) fn gen_throw(
&self,
vm: &VirtualMachine,
exc_type: PyObjectRef,
exc_val: PyObjectRef,
exc_tb: PyObjectRef,
) -> PyResult<ExecutionResult> {
self.with_exec(|mut exec| exec.gen_throw(vm, exc_type, exc_val, exc_tb))
}
pub fn yield_from_target(&self) -> Option<PyObjectRef> {
self.with_exec(|exec| exec.yield_from_target().map(PyObject::to_owned))
}
pub fn is_internal_frame(&self) -> bool {
let code = self.f_code();
let filename = code.co_filename();
filename.as_str().contains("importlib") && filename.as_str().contains("_bootstrap")
}
pub fn next_external_frame(&self, vm: &VirtualMachine) -> Option<FrameRef> {
self.f_back(vm).map(|mut back| loop {
back = if let Some(back) = back.to_owned().f_back(vm) {
back
} else {
break back;
};
if !back.is_internal_frame() {
break back;
}
})
}
}
/// An executing frame; essentially just a struct to combine the immutable data outside the mutex
/// with the mutable data inside
struct ExecutingFrame<'a> {
code: &'a PyRef<PyCode>,
fastlocals: &'a PyMutex<Box<[Option<PyObjectRef>]>>,
cells_frees: &'a [PyCellRef],
locals: &'a ArgMapping,
globals: &'a PyDictRef,
builtins: &'a PyDictRef,
object: &'a Py<Frame>,
lasti: &'a Lasti,
state: &'a mut FrameState,
}
impl fmt::Debug for ExecutingFrame<'_> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("ExecutingFrame")
.field("code", self.code)
// .field("scope", self.scope)
.field("state", self.state)
.finish()
}
}
impl ExecutingFrame<'_> {
#[inline(always)]
fn update_lasti(&mut self, f: impl FnOnce(&mut u32)) {
#[cfg(feature = "threading")]
{
f(&mut self.state.lasti);
self.lasti
.store(self.state.lasti, atomic::Ordering::Relaxed);
}
#[cfg(not(feature = "threading"))]
{
let mut lasti = self.lasti.get();
f(&mut lasti);
self.lasti.set(lasti);
}
}
#[inline(always)]
fn lasti(&self) -> u32 {
#[cfg(feature = "threading")]
{
self.state.lasti
}
#[cfg(not(feature = "threading"))]
{
self.lasti.get()
}
}
fn run(&mut self, vm: &VirtualMachine) -> PyResult<ExecutionResult> {
flame_guard!(format!("Frame::run({})", self.code.obj_name));
// Execute until return or exception:
let instrs = &self.code.instructions;
let mut arg_state = bytecode::OpArgState::default();
loop {
let idx = self.lasti() as usize;
self.update_lasti(|i| *i += 1);
let bytecode::CodeUnit { op, arg } = instrs[idx];
let arg = arg_state.extend(arg);
let mut do_extend_arg = false;
let result = self.execute_instruction(op, arg, &mut do_extend_arg, vm);
match result {
Ok(None) => {}
Ok(Some(value)) => {
break Ok(value);
}
// Instruction raised an exception
Err(exception) => {
#[cold]
fn handle_exception(
frame: &mut ExecutingFrame,
exception: PyBaseExceptionRef,
idx: usize,
vm: &VirtualMachine,
) -> FrameResult {
// 1. Extract traceback from exception's '__traceback__' attr.
// 2. Add new entry with current execution position (filename, lineno, code_object) to traceback.
// 3. Unwind block stack till appropriate handler is found.
let loc = frame.code.locations[idx];
let next = exception.traceback();
let new_traceback =
PyTraceback::new(next, frame.object.to_owned(), frame.lasti(), loc.row);
vm_trace!("Adding to traceback: {:?} {:?}", new_traceback, loc.row());
exception.set_traceback(Some(new_traceback.into_ref(&vm.ctx)));
vm.contextualize_exception(&exception);
frame.unwind_blocks(vm, UnwindReason::Raising { exception })
}
match handle_exception(self, exception, idx, vm) {
Ok(None) => {}
Ok(Some(result)) => break Ok(result),
// TODO: append line number to traceback?
// traceback.append();
Err(exception) => break Err(exception),
}
}
}
if !do_extend_arg {
arg_state.reset()
}
}
}
fn yield_from_target(&self) -> Option<&PyObject> {
if let Some(bytecode::CodeUnit {
op: bytecode::Instruction::YieldFrom,
..
}) = self.code.instructions.get(self.lasti() as usize)
{
Some(self.last_value_ref())
} else {
None
}
}
/// Ok(Err(e)) means that an error occurred while calling throw() and the generator should try
/// sending it
fn gen_throw(
&mut self,
vm: &VirtualMachine,
exc_type: PyObjectRef,
exc_val: PyObjectRef,
exc_tb: PyObjectRef,
) -> PyResult<ExecutionResult> {
if let Some(gen) = self.yield_from_target() {
// borrow checker shenanigans - we only need to use exc_type/val/tb if the following
// variable is Some
let thrower = if let Some(coro) = self.builtin_coro(gen) {
Some(Either::A(coro))
} else {
vm.get_attribute_opt(gen.to_owned(), "throw")?
.map(Either::B)
};
if let Some(thrower) = thrower {
let ret = match thrower {
Either::A(coro) => coro
.throw(gen, exc_type, exc_val, exc_tb, vm)
.to_pyresult(vm), // FIXME:
Either::B(meth) => meth.call((exc_type, exc_val, exc_tb), vm),
};
return ret.map(ExecutionResult::Yield).or_else(|err| {
self.pop_value();
self.update_lasti(|i| *i += 1);
if err.fast_isinstance(vm.ctx.exceptions.stop_iteration) {
let val = vm.unwrap_or_none(err.get_arg(0));
self.push_value(val);
self.run(vm)
} else {
let (ty, val, tb) = vm.split_exception(err);
self.gen_throw(vm, ty, val, tb)
}
});
}
}
let exception = vm.normalize_exception(exc_type, exc_val, exc_tb)?;
match self.unwind_blocks(vm, UnwindReason::Raising { exception }) {
Ok(None) => self.run(vm),
Ok(Some(result)) => Ok(result),
Err(exception) => Err(exception),
}
}
fn unbound_cell_exception(&self, i: usize, vm: &VirtualMachine) -> PyBaseExceptionRef {
if let Some(&name) = self.code.cellvars.get(i) {
vm.new_exception_msg(
vm.ctx.exceptions.unbound_local_error.to_owned(),
format!("local variable '{name}' referenced before assignment"),
)
} else {
let name = self.code.freevars[i - self.code.cellvars.len()];
vm.new_name_error(
format!("free variable '{name}' referenced before assignment in enclosing scope"),
name.to_owned(),
)
}
}
/// Execute a single instruction.
#[inline(always)]
fn execute_instruction(
&mut self,
instruction: bytecode::Instruction,
arg: bytecode::OpArg,
extend_arg: &mut bool,
vm: &VirtualMachine,
) -> FrameResult {
vm.check_signals()?;
flame_guard!(format!("Frame::execute_instruction({:?})", instruction));
#[cfg(feature = "vm-tracing-logging")]
{
trace!("=======");
/* TODO:
for frame in self.frames.iter() {
trace!(" {:?}", frame);
}
*/
trace!(" {:#?}", self);
trace!(" Executing op code: {:?}", instruction);
trace!("=======");
}
#[cold]
fn name_error(name: &'static PyStrInterned, vm: &VirtualMachine) -> PyBaseExceptionRef {
vm.new_name_error(format!("name '{name}' is not defined"), name.to_owned())
}
match instruction {
bytecode::Instruction::LoadConst { idx } => {
self.push_value(self.code.constants[idx.get(arg) as usize].clone().into());
Ok(None)
}
bytecode::Instruction::ImportName { idx } => {
self.import(vm, Some(self.code.names[idx.get(arg) as usize]))?;
Ok(None)
}
bytecode::Instruction::ImportNameless => {
self.import(vm, None)?;
Ok(None)
}
bytecode::Instruction::ImportStar => {
self.import_star(vm)?;
Ok(None)
}
bytecode::Instruction::ImportFrom { idx } => {
let obj = self.import_from(vm, idx.get(arg))?;
self.push_value(obj);
Ok(None)
}
bytecode::Instruction::LoadFast(idx) => {
#[cold]
fn reference_error(
varname: &'static PyStrInterned,
vm: &VirtualMachine,
) -> PyBaseExceptionRef {
vm.new_exception_msg(
vm.ctx.exceptions.unbound_local_error.to_owned(),
format!("local variable '{varname}' referenced before assignment",),
)
}
let idx = idx.get(arg) as usize;
let x = self.fastlocals.lock()[idx]
.clone()
.ok_or_else(|| reference_error(self.code.varnames[idx], vm))?;
self.push_value(x);
Ok(None)
}
bytecode::Instruction::LoadNameAny(idx) => {
let name = self.code.names[idx.get(arg) as usize];
let result = self.locals.mapping().subscript(name, vm);
match result {
Ok(x) => self.push_value(x),
Err(e) if e.fast_isinstance(vm.ctx.exceptions.key_error) => {
self.push_value(self.load_global_or_builtin(name, vm)?);
}
Err(e) => return Err(e),
}
Ok(None)
}
bytecode::Instruction::LoadGlobal(idx) => {
let name = &self.code.names[idx.get(arg) as usize];
let x = self.load_global_or_builtin(name, vm)?;
self.push_value(x);
Ok(None)
}
bytecode::Instruction::LoadDeref(i) => {
let i = i.get(arg) as usize;
let x = self.cells_frees[i]
.get()
.ok_or_else(|| self.unbound_cell_exception(i, vm))?;
self.push_value(x);
Ok(None)
}
bytecode::Instruction::LoadClassDeref(i) => {
let i = i.get(arg) as usize;
let name = self.code.freevars[i - self.code.cellvars.len()];
let value = self.locals.mapping().subscript(name, vm).ok();
self.push_value(match value {
Some(v) => v,
None => self.cells_frees[i]
.get()
.ok_or_else(|| self.unbound_cell_exception(i, vm))?,
});
Ok(None)
}
bytecode::Instruction::StoreFast(idx) => {
let value = self.pop_value();
self.fastlocals.lock()[idx.get(arg) as usize] = Some(value);
Ok(None)
}
bytecode::Instruction::StoreLocal(idx) => {
let name = self.code.names[idx.get(arg) as usize];
let value = self.pop_value();
self.locals.mapping().ass_subscript(name, Some(value), vm)?;
Ok(None)
}
bytecode::Instruction::StoreGlobal(idx) => {
let value = self.pop_value();
self.globals
.set_item(self.code.names[idx.get(arg) as usize], value, vm)?;
Ok(None)
}
bytecode::Instruction::StoreDeref(i) => {
let value = self.pop_value();
self.cells_frees[i.get(arg) as usize].set(Some(value));
Ok(None)
}
bytecode::Instruction::DeleteFast(idx) => {
let mut fastlocals = self.fastlocals.lock();
let idx = idx.get(arg) as usize;
if fastlocals[idx].is_none() {
return Err(vm.new_exception_msg(
vm.ctx.exceptions.unbound_local_error.to_owned(),
format!(
"local variable '{}' referenced before assignment",
self.code.varnames[idx]
),
));
}
fastlocals[idx] = None;
Ok(None)
}
bytecode::Instruction::DeleteLocal(idx) => {
let name = self.code.names[idx.get(arg) as usize];
let res = self.locals.mapping().ass_subscript(name, None, vm);
match res {
Ok(()) => {}
Err(e) if e.fast_isinstance(vm.ctx.exceptions.key_error) => {
return Err(name_error(name, vm))
}
Err(e) => return Err(e),
}
Ok(None)
}
bytecode::Instruction::DeleteGlobal(idx) => {
let name = self.code.names[idx.get(arg) as usize];
match self.globals.del_item(name, vm) {
Ok(()) => {}
Err(e) if e.fast_isinstance(vm.ctx.exceptions.key_error) => {
return Err(name_error(name, vm))
}
Err(e) => return Err(e),
}
Ok(None)
}
bytecode::Instruction::DeleteDeref(i) => {
self.cells_frees[i.get(arg) as usize].set(None);
Ok(None)
}
bytecode::Instruction::LoadClosure(i) => {
let value = self.cells_frees[i.get(arg) as usize].clone();
self.push_value(value.into());
Ok(None)
}
bytecode::Instruction::Subscript => self.execute_subscript(vm),
bytecode::Instruction::StoreSubscript => self.execute_store_subscript(vm),
bytecode::Instruction::DeleteSubscript => self.execute_delete_subscript(vm),
bytecode::Instruction::Pop => {
// Pop value from stack and ignore.
self.pop_value();
Ok(None)
}
bytecode::Instruction::Duplicate => {
// Duplicate top of stack
let value = self.last_value();
self.push_value(value);
Ok(None)
}
bytecode::Instruction::Duplicate2 => {
// Duplicate top 2 of stack
let top = self.last_value();
let second_to_top = self.nth_value(1).to_owned();
self.push_value(second_to_top);
self.push_value(top);
Ok(None)
}
// splitting the instructions like this offloads the cost of "dynamic" dispatch (on the
// amount to rotate) to the opcode dispatcher, and generates optimized code for the
// concrete cases we actually have
bytecode::Instruction::Rotate2 => self.execute_rotate(2),
bytecode::Instruction::Rotate3 => self.execute_rotate(3),
bytecode::Instruction::BuildString { size } => {
let s = self
.pop_multiple(size.get(arg) as usize)
.as_slice()
.iter()
.map(|pyobj| pyobj.payload::<PyStr>().unwrap().as_ref())
.collect::<String>();
let str_obj = vm.ctx.new_str(s);
self.push_value(str_obj.into());
Ok(None)
}
bytecode::Instruction::BuildList { size } => {
let elements = self.pop_multiple(size.get(arg) as usize).collect();
let list_obj = vm.ctx.new_list(elements);
self.push_value(list_obj.into());
Ok(None)
}
bytecode::Instruction::BuildListUnpack { size } => {
let elements = self.unpack_elements(vm, size.get(arg) as usize)?;
let list_obj = vm.ctx.new_list(elements);
self.push_value(list_obj.into());
Ok(None)
}
bytecode::Instruction::BuildSet { size } => {
let set = PySet::new_ref(&vm.ctx);
{
for element in self.pop_multiple(size.get(arg) as usize) {
set.add(element, vm)?;
}
}
self.push_value(set.into());
Ok(None)
}
bytecode::Instruction::BuildSetUnpack { size } => {
let set = PySet::new_ref(&vm.ctx);
{
for element in self.pop_multiple(size.get(arg) as usize) {
vm.map_iterable_object(&element, |x| set.add(x, vm))??;
}
}
self.push_value(set.into());
Ok(None)
}
bytecode::Instruction::BuildTuple { size } => {
let elements = self.pop_multiple(size.get(arg) as usize).collect();
let list_obj = vm.ctx.new_tuple(elements);
self.push_value(list_obj.into());
Ok(None)
}
bytecode::Instruction::BuildTupleUnpack { size } => {
let elements = self.unpack_elements(vm, size.get(arg) as usize)?;
let list_obj = vm.ctx.new_tuple(elements);
self.push_value(list_obj.into());
Ok(None)
}
bytecode::Instruction::BuildMap { size } => self.execute_build_map(vm, size.get(arg)),
bytecode::Instruction::BuildMapForCall { size } => {
self.execute_build_map_for_call(vm, size.get(arg))
}
bytecode::Instruction::DictUpdate => {
let other = self.pop_value();
let dict = self
.last_value_ref()
.downcast_ref::<PyDict>()
.expect("exact dict expected");
dict.merge_object(other, vm)?;
Ok(None)
}
bytecode::Instruction::BuildSlice { step } => {
self.execute_build_slice(vm, step.get(arg))
}
bytecode::Instruction::ListAppend { i } => {
let item = self.pop_value();
let obj = self.nth_value(i.get(arg));
let list: &Py<PyList> = unsafe {
// SAFETY: trust compiler
obj.downcast_unchecked_ref()
};
list.append(item);
Ok(None)
}
bytecode::Instruction::SetAdd { i } => {
let item = self.pop_value();
let obj = self.nth_value(i.get(arg));
let set: &Py<PySet> = unsafe {
// SAFETY: trust compiler
obj.downcast_unchecked_ref()
};
set.add(item, vm)?;
Ok(None)
}
bytecode::Instruction::MapAdd { i } => {
let value = self.pop_value();
let key = self.pop_value();
let obj = self.nth_value(i.get(arg));
let dict: &Py<PyDict> = unsafe {
// SAFETY: trust compiler
obj.downcast_unchecked_ref()
};
dict.set_item(&*key, value, vm)?;
Ok(None)
}
bytecode::Instruction::BinaryOperation { op } => self.execute_binop(vm, op.get(arg)),
bytecode::Instruction::BinaryOperationInplace { op } => {
self.execute_binop_inplace(vm, op.get(arg))
}
bytecode::Instruction::LoadAttr { idx } => self.load_attr(vm, idx.get(arg)),
bytecode::Instruction::StoreAttr { idx } => self.store_attr(vm, idx.get(arg)),
bytecode::Instruction::DeleteAttr { idx } => self.delete_attr(vm, idx.get(arg)),
bytecode::Instruction::UnaryOperation { op } => self.execute_unop(vm, op.get(arg)),
bytecode::Instruction::TestOperation { op } => self.execute_test(vm, op.get(arg)),
bytecode::Instruction::CompareOperation { op } => self.execute_compare(vm, op.get(arg)),
bytecode::Instruction::ReturnValue => {
let value = self.pop_value();
self.unwind_blocks(vm, UnwindReason::Returning { value })
}
bytecode::Instruction::YieldValue => {
let value = self.pop_value();
let value = if self.code.flags.contains(bytecode::CodeFlags::IS_COROUTINE) {
PyAsyncGenWrappedValue(value).into_pyobject(vm)
} else {
value
};
Ok(Some(ExecutionResult::Yield(value)))
}
bytecode::Instruction::YieldFrom => self.execute_yield_from(vm),
bytecode::Instruction::SetupAnnotation => self.setup_annotations(vm),
bytecode::Instruction::SetupLoop => {
self.push_block(BlockType::Loop);
Ok(None)
}
bytecode::Instruction::SetupExcept { handler } => {
self.push_block(BlockType::TryExcept {
handler: handler.get(arg),
});
Ok(None)
}
bytecode::Instruction::SetupFinally { handler } => {
self.push_block(BlockType::Finally {
handler: handler.get(arg),
});
Ok(None)
}
bytecode::Instruction::EnterFinally => {
self.push_block(BlockType::FinallyHandler {
reason: None,
prev_exc: vm.current_exception(),
});
Ok(None)
}
bytecode::Instruction::EndFinally => {
// Pop the finally handler from the stack, and recall
// what was the reason we were in this finally clause.
let block = self.pop_block();
if let BlockType::FinallyHandler { reason, prev_exc } = block.typ {
vm.set_exception(prev_exc);
if let Some(reason) = reason {
self.unwind_blocks(vm, reason)
} else {
Ok(None)
}
} else {
self.fatal(
"Block type must be finally handler when reaching EndFinally instruction!",
);
}
}
bytecode::Instruction::SetupWith { end } => {
let context_manager = self.pop_value();
let error_string = || -> String {
format!(
"'{:.200}' object does not support the context manager protocol",
context_manager.class().name(),
)
};
let enter_res = vm
.get_special_method(&context_manager, identifier!(vm, __enter__))?
.ok_or_else(|| vm.new_type_error(error_string()))?
.invoke((), vm)?;
let exit = context_manager
.get_attr(identifier!(vm, __exit__), vm)
.map_err(|_exc| {
vm.new_type_error({
format!("'{} (missed __exit__ method)", error_string())
})
})?;
self.push_value(exit);
self.push_block(BlockType::Finally {
handler: end.get(arg),
});
self.push_value(enter_res);
Ok(None)
}
bytecode::Instruction::BeforeAsyncWith => {
let mgr = self.pop_value();
let error_string = || -> String {
format!(
"'{:.200}' object does not support the asynchronous context manager protocol",
mgr.class().name(),
)
};
let aenter_res = vm
.get_special_method(&mgr, identifier!(vm, __aenter__))?
.ok_or_else(|| vm.new_type_error(error_string()))?
.invoke((), vm)?;
let aexit = mgr
.get_attr(identifier!(vm, __aexit__), vm)
.map_err(|_exc| {
vm.new_type_error({
format!("'{} (missed __aexit__ method)", error_string())
})
})?;
self.push_value(aexit);
self.push_value(aenter_res);
Ok(None)
}
bytecode::Instruction::SetupAsyncWith { end } => {
let enter_res = self.pop_value();
self.push_block(BlockType::Finally {
handler: end.get(arg),
});
self.push_value(enter_res);
Ok(None)
}
bytecode::Instruction::WithCleanupStart => {
let block = self.current_block().unwrap();
let reason = match block.typ {
BlockType::FinallyHandler { reason, .. } => reason,
_ => self.fatal("WithCleanupStart expects a FinallyHandler block on stack"),
};
let exc = match reason {
Some(UnwindReason::Raising { exception }) => Some(exception),
_ => None,
};
let exit = self.pop_value();
let args = if let Some(exc) = exc {
vm.split_exception(exc)
} else {
(vm.ctx.none(), vm.ctx.none(), vm.ctx.none())
};
let exit_res = exit.call(args, vm)?;
self.push_value(exit_res);
Ok(None)
}
bytecode::Instruction::WithCleanupFinish => {
let block = self.pop_block();
let (reason, prev_exc) = match block.typ {
BlockType::FinallyHandler { reason, prev_exc } => (reason, prev_exc),
_ => self.fatal("WithCleanupFinish expects a FinallyHandler block on stack"),
};
vm.set_exception(prev_exc);
let suppress_exception = self.pop_value().try_to_bool(vm)?;
if suppress_exception {
Ok(None)
} else if let Some(reason) = reason {
self.unwind_blocks(vm, reason)
} else {
Ok(None)
}
}
bytecode::Instruction::PopBlock => {
self.pop_block();
Ok(None)
}
bytecode::Instruction::GetIter => {
let iterated_obj = self.pop_value();
let iter_obj = iterated_obj.get_iter(vm)?;
self.push_value(iter_obj.into());
Ok(None)
}
bytecode::Instruction::GetAwaitable => {
let awaited_obj = self.pop_value();
let awaitable = if awaited_obj.payload_is::<PyCoroutine>() {
awaited_obj
} else {
let await_method = vm.get_method_or_type_error(
awaited_obj.clone(),
identifier!(vm, __await__),
|| {
format!(
"object {} can't be used in 'await' expression",
awaited_obj.class().name(),
)
},
)?;
await_method.call((), vm)?
};
self.push_value(awaitable);
Ok(None)
}
bytecode::Instruction::GetAIter => {
let aiterable = self.pop_value();
let aiter = vm.call_special_method(&aiterable, identifier!(vm, __aiter__), ())?;
self.push_value(aiter);
Ok(None)
}
bytecode::Instruction::GetANext => {
let aiter = self.last_value();
let awaitable = vm.call_special_method(&aiter, identifier!(vm, __anext__), ())?;
let awaitable = if awaitable.payload_is::<PyCoroutine>() {
awaitable
} else {
vm.call_special_method(&awaitable, identifier!(vm, __await__), ())?
};
self.push_value(awaitable);
Ok(None)
}