22//! implements bytecode structure.
33
44use bitflags:: bitflags;
5+ use itertools:: Itertools ;
56use num_bigint:: BigInt ;
67use num_complex:: Complex64 ;
78use serde:: { Deserialize , Serialize } ;
@@ -40,9 +41,9 @@ pub struct CodeObject {
4041 pub flags : CodeFlags ,
4142 pub posonlyarg_count : usize , // Number of positional-only arguments
4243 pub arg_names : Vec < String > , // Names of positional arguments
43- pub varargs : Varargs , // *args or *
44+ pub varargs_name : Option < String > , // *args or *
4445 pub kwonlyarg_names : Vec < String > ,
45- pub varkeywords : Varargs , // **kwargs or **
46+ pub varkeywords_name : Option < String > , // **kwargs or **
4647 pub source_path : String ,
4748 pub first_line_number : usize ,
4849 pub obj_name : String , // Name of the object that created this code object
@@ -57,6 +58,8 @@ bitflags! {
5758 const NEW_LOCALS = 0x08 ;
5859 const IS_GENERATOR = 0x10 ;
5960 const IS_COROUTINE = 0x20 ;
61+ const HAS_VARARGS = 0x40 ;
62+ const HAS_VARKEYWORDS = 0x80 ;
6063 }
6164}
6265
@@ -70,6 +73,8 @@ impl CodeFlags {
7073 pub const NAME_MAPPING : & ' static [ ( & ' static str , CodeFlags ) ] = & [
7174 ( "GENERATOR" , CodeFlags :: IS_GENERATOR ) ,
7275 ( "COROUTINE" , CodeFlags :: IS_COROUTINE ) ,
76+ ( "VARARGS" , CodeFlags :: HAS_VARARGS ) ,
77+ ( "VARKEYWORDS" , CodeFlags :: HAS_VARKEYWORDS ) ,
7378 ] ;
7479}
7580
@@ -352,13 +357,6 @@ pub enum UnaryOperator {
352357 Plus ,
353358}
354359
355- #[ derive( Debug , Clone , PartialEq , Serialize , Deserialize ) ]
356- pub enum Varargs {
357- None ,
358- Unnamed ,
359- Named ( String ) ,
360- }
361-
362360/*
363361Maintain a stack of blocks on the VM.
364362pub enum BlockType {
@@ -373,9 +371,9 @@ impl CodeObject {
373371 flags : CodeFlags ,
374372 posonlyarg_count : usize ,
375373 arg_names : Vec < String > ,
376- varargs : Varargs ,
374+ varargs_name : Option < String > ,
377375 kwonlyarg_names : Vec < String > ,
378- varkeywords : Varargs ,
376+ varkeywords_name : Option < String > ,
379377 source_path : String ,
380378 first_line_number : usize ,
381379 obj_name : String ,
@@ -387,9 +385,9 @@ impl CodeObject {
387385 flags,
388386 posonlyarg_count,
389387 arg_names,
390- varargs ,
388+ varargs_name ,
391389 kwonlyarg_names,
392- varkeywords ,
390+ varkeywords_name ,
393391 source_path,
394392 first_line_number,
395393 obj_name,
@@ -418,6 +416,29 @@ impl CodeObject {
418416 } )
419417 }
420418
419+ pub fn varnames ( & self ) -> impl Iterator < Item = & str > + ' _ {
420+ self . arg_names
421+ . iter ( )
422+ . map ( String :: as_str)
423+ . chain ( self . kwonlyarg_names . iter ( ) . map ( String :: as_str) )
424+ . chain (
425+ self . instructions
426+ . iter ( )
427+ . filter_map ( |i| match i {
428+ Instruction :: LoadName {
429+ name,
430+ scope : NameScope :: Local ,
431+ }
432+ | Instruction :: StoreName {
433+ name,
434+ scope : NameScope :: Local ,
435+ } => Some ( name. as_str ( ) ) ,
436+ _ => None ,
437+ } )
438+ . unique ( ) ,
439+ )
440+ }
441+
421442 fn display_inner (
422443 & self ,
423444 f : & mut fmt:: Formatter ,
0 commit comments