CS263: Runtime Systems Lecture: High-level language virtual machines Today: A Review of Object-oriented features Chandra Krintz UCSB Computer Science Department
Virtual machines (VMs) Terminology Aka managed runtime environments (MREs) Aka [high-level] language runtime (runtime) Vernacular Java: JVMs,.Net: Common language runtime (CLR), Python/others: runtime
Java Classfiles Architecture-independent Format called bytecode Dynamically loaded / executed by a Java Virtual Machine File.java class cls1 { } class cls2 { } source compiler (javac File.java) cls1.class Java bytecode architecture-independent architecture-dependent Java bytecode cls2.class
Java Classfiles Architecture-independent Format called bytecode Dynamically loaded / executed by a Java Virtual Machine File.java class cls1 { } class cls2 { } source compiler (javac File.java) cls1.class Java bytecode architecture-independent architecture-dependent static compiler (Ex: g++) executable code linkable code linker executable code Java bytecode cls2.class
Java Classfiles Architecture-independent Format called bytecode Dynamically loaded / executed by a Java Virtual Machine File.java class cls1 { } class cls2 { } source compiler (javac File.java) cls1.class Java bytecode architecture-independent architecture-dependent Java Virtual Machine Translation from bytecode to native machine code executable code Java bytecode cls2.class Runtime environment program loading/verif. memory management thread/synchronization optimization
Java Classfiles Architecture-independent Format called bytecode Dynamically loaded / executed by a Java Virtual Machine File.java class cls1 { } class cls2 { } source compiler (javac File.java) cls1.class Java bytecode architecture-independent architecture-dependent Java Virtual Machine Interpreter and/or dynamic compiler executable code Java bytecode cls2.class Runtime environment program loading/verif. memory management thread/synchronization optimization
Java Classfiles Architecture-independent Format called bytecode Dynamically loaded / executed by a Java Virtual Machine File.java class cls1 { } class cls2 { } source compiler (javac File.java) cls1.class Java bytecode architecture-independent architecture-dependent Java Virtual Machine Interpreter and/or dynamic compiler executable code static compiler (Ex: g++) executable code linkable code linker executable code Java bytecode cls2.class Runtime environment program loading/verif. memory management thread/synchronization optimization
C# Architecture-independent Format called bytecode Dynamically loaded / executed by a common language runtime (e.g. mono) File.cs class cls1 { } class cls2 { } source compiler (mcs File.cs) cls1.exe MSIL bytecode architecture-independent architecture-dependent Common Language Runtime Translation from bytecode to native machine code executable code MSIL bytecode cls2.exe Runtime environment program loading/verif. memory management thread/synchronization optimization
Python Architecture-independent Format called bytecode Dynamically loaded / executed by a python runtime (e.g. cpython) File.py Non-class code class cls1: source compiler (pyc File.py) cls1.pyc Python bytecode Or source code! architecture-independent architecture-dependent Python Runtime Translation from bytecode to native machine code executable code Runtime environment program loading/verif. memory management thread/synchronization optimization
Python Architecture-independent Format called bytecode Dynamically loaded / executed by a python runtime (e.g. cpython) File.py Non-class code class cls1: source compiler (pyc File.py) cls1.pyc Python bytecode architecture-independent architecture-dependent High-Level Lang. Runtime Translation from bytecode to native machine code executable code Runtime environment program loading/verif. memory management thread/synchronization optimization
OO Review Things you should already know (study/review if not) Static vs dynamic typing Type-safety, memory safety; vs type-inference Class Object Class/object members (aka attributes) Fields (data) Methods (code) Static fields vs instance fields Static methods vs instances methods Inheritance Dynamic dispatch of virtual instance methods Dynamically typed OO language features
Static vs Dynamic Typing STATIC Variable Name Type is associated with variable and known at compile time (C, C++, Java, C#, all below) -- via explict specification or type inference (as in Scala, C# v3+, ML, Swift, Go) DYNAMIC Variable Name Object is of Type Type Object is of Type Type is associated with value and not known until runtime when the code executes
Static vs Dynamic Typing STATIC Variable Name Type is associated with variable and known at compile time -- via explict specification or type inference (as in Scala, C# v3+, ML, Swift, Go) DYNAMIC Variable Name runtime Object is of Type Type runtime Object is of Type Type is associated with value and not known until runtime when the code executes
Static vs Dynamic Typing STATIC Variable Name Type is associated with variable and known at compile time -- via explict specification or type inference (as in Scala, C# v3+, ML, Swift, Go) DYNAMIC Variable Name runtime In the code, known ahead of runtime runtime Object Type is of Type Object is of Type Type is associated with value and not known until runtime when the code executes
Static vs Dynamic Typing Variable Name STATIC Type is associated with variable and known at compile time -- via explict specification or type inference (as in Scala, C# v3+, ML, Swift, Go) DYNAMIC Variable Name Object is of Type Type Must match (be compatible) Type Safe if this and operations on types are checked (statically if possible, dynamically required for some operations) and legal according to the type system. A type system generally tries to guarantee that operations are not used with values for which that operation does not make sense. Object is of Type Type is associated with value and not known until runtime when the code executes Checked at runtime
Example Languages Statically typed, not type safe or memory safe C, C++ Type-safe, memory safe, statically typed Java, C# & the.net languages, Scala, ML, Go, Swift If you don t specify the type, the compiler can infer it in many cases Memory safe = there is no way to access a memory location except through an object reference Cannot walk through memory arbitrarily Cannot dereference an object that is NULL Dynamically typed (most are sandboxed) Python, Ruby, Lua Javascript, PHP Perl, OS shell scripts
Java Classes/Objects class MyMainClass { public static void main(string argv[]) { MyClass A = new MyClass(); } } Object = An instance of a class def. Class members Methods and fields Same for all objects of that class Declared using the static modifier class MyClass { int field1; int field2; static int field3; } void meth1 (int arg1) { } static void meth2(int arg1){ } MyClass() { } 18
Java Classes/Objects class MyMainClass { public static void main(string argv[]) { MyClass A = new MyClass(); } } Object = An instance of a class def. Class members Methods and fields Same for all objects of that class Declared using the static modifier class MyClass { int field1; int field2; static int field3; } void meth1 (int arg1) { } static void meth2(int arg1){ } MyClass() { } Instance members = non-static members (each object has own copy) Methods and fields Created & allocated when new is called Initialized when the constructor is called (MyClass() { } above) 19
Type-safe Object Conversions (Java/C#) A variable with a class type Can hold Objects of its own type or of subtypes of its own type Source compiler checks this statically (at compile time) Child c = new Parent(); // is not allowed class Parent { int field1; } class Child extends Parent { int field2; } Child c = new Child(); ((Parent)c).field1 = 2;// OK Child has Parent parts via inheritance c.field1 = 2; //also works (is the same as above in this case) 20
Inheritance and Static Fields & Methods Most languages (Java, C++, Python (fields only), others) Class/static fields and methods (Should be) accessed using the Class Name Parent.staticMethod(); Parent.staticField = 2; If accessed via an object, compiler uses the static (declared) or casted type of variable, & replaces the access with one like above. Parent obj = new Parent(); obj.staticfield = 2; //is same as above Members are inherited by subclasses: Child extends Parent System.err.println(Child.staticField); // output: 2
Inheritance and Static Fields & Methods Most languages (Java, C++, Python (fields only), others) Class/static fields and methods If Child has a field/method with the same name (e.g. staticfield) Both are available (Child hides parent s field): Child obj=new Child(); obj.staticfield = 2; Accessible via a cast ((Parent)obj).staticField = 4; or directly thru the parent s class name: Parent.staticField = 7; Hidden NOT overridden
Inheritance and Instance Fields Statically typed languages (Java, C++, C#, ) Instance fields An object instance is required for access Accessible from an object Within the object or in a lookup table referenced in the object Fields in parent class are inherited by subclass Those with same names are hidden by subclass but still available! Use casts or assign into a variable that has the type of the parent Child inherits from Parent Child obj = new Child(); obj.field1 = 7; //field1 is in both Parent/Child ((Parent)obj).field1 = 4; //field1 inher from Parent
Type-safe Object Conversions (Java/C#) A variable with a class type Can hold Objects of its own type or of subtypes of its own type Source compiler checks this statically (at compile time) Child c = new Parent(); // is not allowed Can be casted To a supertype The compiler checks compatibility at compile time To a subtype The compiler inserts runtime check to check the underlying object type when this code executes Checks that the underlying object type is 24 the one to which the cast is converting class Parent { int field1; } class Child extends Parent { int field1; } Parent p = Child c = ((Child)p).field1 = 7; ((Parent)c).field1 = 2;
Inheritance and Instance Fields Statically typed languages (Java, C++, C#, ) Instance fields An object instance is required for access Accessible from an object Within the object or in a lookup table referenced in the object Fields in parent class are inherited by subclass Those with same names are hidden by subclass but still available! Use casts or assign into a variable that has the type of the parent Child inherits from Parent Child obj = new Child(); obj.field1 = 7; //field1 is in both Parent/Child ((Parent)obj).field1 = 4; //field1 inher from Parent
Inheritance and Instance Fields Statically typed languages (Java, C++, C#, ) Instance fields An object instance is required for access Accessible from an object Within the object or in a lookup table referenced in the object Fields in parent class are inherited by subclass Those with same names are hidden by subclass but still available! Cast or assign into a variable that has the type of the parent Child inherits from Parent Parent pobj = getit(); Child obj = new Child(); pobj.field1 = 2; //field1 inher. from Parent obj.field1 = 7; //field1 is in both Parent/Child ((Child)pobj).field1 = 1; //error or which field1? ((Parent)obj).field1 = 4; //field1 inher from Parent //how does this work in Java? pobj = new Parent(); Hidden NOT overridden ((Child)pobj).field1 = 1; //what about this?
Inheritance and Instance Methods Most languages (Java, C++, Python, Others) Instance methods An object instance is required for access Accessible from object (lookup table referred to by object) Methods in parent class are inherited by subclass For methods in subclass with same signature (name/type) as one in parent = Polymorphic There are 2 choices: Static dispatch Dynamic dispatch
Inheritance and Instance Methods Instance methods Methods in parent class are inherited by subclass For methods in subclass with same signature (name/type) as one in parent = Polymorphic There are 2 choices: Static dispatch - Make them like fields and have the child hide them (making them still accessible) we use the static/casted type determine which method to call C++,C# use this Parent obj = new Child(); obj.instancemethod(); //instancemethod is defined in both //calls which one? ((Child)obj).instanceMethod(); //calls which one?
Inheritance and Instance Methods Instance methods Methods in parent class are inherited by subclass For methods in subclass with same signature (name/type) as one in parent = Polymorphic There are 2 choices: Static dispatch - Make them like fields and have the child hide them (making them still accessible) we use the static/casted type determine which method to call C++,C# use this Parent obj = new Child(); obj.instancemethod(); //instancemethod is defined in both //calls Parent s ((Child)obj).instanceMethod(); //calls Child s
Inheritance and Instance Methods Instance methods Methods in parent class are inherited by subclass For methods in subclass with same signature (name/type) as one in parent = Polymorphic There are 2 choices: Dynamic dispatch Look up the underlying object type at runtime to determine which method to call All instance methods in Java and Python use this Instance methods marked virtual in C++ and C# use this Parent obj = new Child(); obj.instancemethod(); ((Parent)obj).instanceMethod(); //instancemethod is defined in both //calls which one? //calls which one?
Inheritance and Instance Methods Instance methods Methods in parent class are inherited by subclass For methods in subclass with same signature (name/type) as one in parent = Polymorphic There are 2 choices: Dynamic dispatch Look up the underlying object type at runtime to determine which method to call All instance methods in Java and Python use this Instance methods marked virtual in C++ and C# use this Parent obj = new Child(); obj.instancemethod(); ((Parent)obj).instanceMethod(); //instancemethod is defined in both //calls Child s //calls Child s
Dynamically Typed OO Languages Python, Ruby, Javascript, Lua, Attributes/properties (fields and methods) are defined on the fly upon first write/definition Object == unordered set of key-value pairs Lookup of attributes is by name for every access Most OO languages employ class-based inheritance Javascript and Self use prototype inheritance Prototypes define a set of properties; prototypes are objects Objects can inherit their properties from a prototype (ie other objects) And then change them (diverge from prototype) Hidden classes (or maps) are used by the runtime to track which properties an object has any (control) point in the executing program