inf 212 analysis of prog. langslopes/teaching/inf212w15/...dynamically-typed languages simple...

25
INF 212 ANALYSIS OF PROG. LANGS PLUGINS Instructors: Crista Lopes Copyright © Instructors.

Upload: others

Post on 29-May-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: INF 212 ANALYSIS OF PROG. LANGSlopes/teaching/inf212W15/...Dynamically-typed languages Simple Statically-typed languages Not so simple Discuss Binary components -- Python 1 #!/usr/bin/env

INF 212 ANALYSIS OF PROG. LANGS PLUGINS Instructors: Crista Lopes Copyright © Instructors.

Page 2: INF 212 ANALYSIS OF PROG. LANGSlopes/teaching/inf212W15/...Dynamically-typed languages Simple Statically-typed languages Not so simple Discuss Binary components -- Python 1 #!/usr/bin/env

Modules as conceptual units

Page 3: INF 212 ANALYSIS OF PROG. LANGSlopes/teaching/inf212W15/...Dynamically-typed languages Simple Statically-typed languages Not so simple Discuss Binary components -- Python 1 #!/usr/bin/env

Modules as physical components

Page 4: INF 212 ANALYSIS OF PROG. LANGSlopes/teaching/inf212W15/...Dynamically-typed languages Simple Statically-typed languages Not so simple Discuss Binary components -- Python 1 #!/usr/bin/env

Software modules as physical components

Source components Get the source, make it yours. Simple.

Binary components Java: jar files .NET: DLL files C/C++: so files ... Not so simple

Page 5: INF 212 ANALYSIS OF PROG. LANGSlopes/teaching/inf212W15/...Dynamically-typed languages Simple Statically-typed languages Not so simple Discuss Binary components -- Python 1 #!/usr/bin/env

Source vs. Binary

Discuss

Page 6: INF 212 ANALYSIS OF PROG. LANGSlopes/teaching/inf212W15/...Dynamically-typed languages Simple Statically-typed languages Not so simple Discuss Binary components -- Python 1 #!/usr/bin/env

Linking binary components

3 steps 1. Independent compilation

2. Dynamic Loading

3. Instantiation of classes

Page 7: INF 212 ANALYSIS OF PROG. LANGSlopes/teaching/inf212W15/...Dynamically-typed languages Simple Statically-typed languages Not so simple Discuss Binary components -- Python 1 #!/usr/bin/env

Linking binary components

Dynamically-typed languages Simple

Statically-typed languages Not so simple

Discuss

Page 8: INF 212 ANALYSIS OF PROG. LANGSlopes/teaching/inf212W15/...Dynamically-typed languages Simple Statically-typed languages Not so simple Discuss Binary components -- Python 1 #!/usr/bin/env

Binary components -- Python

1 #!/usr/bin/env python 2 import sys, ConfigParser, imp 3 4 def load_plugins(): 5 config = ConfigParser.ConfigParser() 6 config.read("config.ini") 7 words_plugin = config.get("Plugins", "words") 8 frequencies_plugin = config.get("Plugins", "frequencies") 9 global tfwords, tffreqs 10 tfwords = imp.load_compiled(’tfwords’, words_plugin) 11 tffreqs = imp.load_compiled(’tffreqs’, frequencies_plugin) 12 13 load_plugins() 14 word_freqs = tffreqs.top25(tfwords.extract_words(sys.argv[1])) 15 16 for (w, c) in word_freqs: 17 print w, ’ - ’, c

Page 9: INF 212 ANALYSIS OF PROG. LANGSlopes/teaching/inf212W15/...Dynamically-typed languages Simple Statically-typed languages Not so simple Discuss Binary components -- Python 1 #!/usr/bin/env

Binary components – Python

Python No need to worry about types during independent

compilation

Page 10: INF 212 ANALYSIS OF PROG. LANGSlopes/teaching/inf212W15/...Dynamically-typed languages Simple Statically-typed languages Not so simple Discuss Binary components -- Python 1 #!/usr/bin/env

Binary components – Typed

class TFApp { static void main(String[] args) { HashMap<String, int> wordFreqs; wordFreqs = tffreqs.top25(tfwords.extract_words(sys.argv[0])); } }

Types?

Page 11: INF 212 ANALYSIS OF PROG. LANGSlopes/teaching/inf212W15/...Dynamically-typed languages Simple Statically-typed languages Not so simple Discuss Binary components -- Python 1 #!/usr/bin/env

Binary components – Typed

interface TFWords { public List<String> extractWords(string path); } interface TFFreqs { public HashMap<String, int> top25(List<String> words); }

Types ok!

class TFApp { static void main(String[] args) { HashMap<String, int> wordFreqs; TFWords tfwords; //= ??? TFFreqs tffreqs ;//= ??? wordFreqs = tffreqs.top25(tfwords.extract_words(sys.argv[0])); } }

Page 12: INF 212 ANALYSIS OF PROG. LANGSlopes/teaching/inf212W15/...Dynamically-typed languages Simple Statically-typed languages Not so simple Discuss Binary components -- Python 1 #!/usr/bin/env

Binary components – Typed

<<class>> TFApplication

<<interface>> TFWords

<<interface>> TFFreqs

<<class>> Words1

<<class>> Words2

<<class>> Freqs1

<<class>> Freqs2

How do we partition into jars?

Page 13: INF 212 ANALYSIS OF PROG. LANGSlopes/teaching/inf212W15/...Dynamically-typed languages Simple Statically-typed languages Not so simple Discuss Binary components -- Python 1 #!/usr/bin/env

Physical modularization 1 – Typed

<<class>> TFApplication

<<interface>> TFWords

<<interface>> TFFreqs

<<class>> Words1

<<class>> Words2

<<class>> Freqs1

<<class>> Freqs2

pack1

pack2

pack3

pack4

pack5

Compilation dependencies: pack1? pack2? same

Page 14: INF 212 ANALYSIS OF PROG. LANGSlopes/teaching/inf212W15/...Dynamically-typed languages Simple Statically-typed languages Not so simple Discuss Binary components -- Python 1 #!/usr/bin/env

Physical modularization 2 – Java

<<class>> TFApplication

<<interface>> TFWords

<<interface>> TFFreqs

<<class>> Words1

<<class>> Words2

<<class>> Freqs1

<<class>> Freqs2

pack1

pack2

pack3

pack4

pack5

pack6

Compilation dependencies: pack1? pack2? pack3?

Page 15: INF 212 ANALYSIS OF PROG. LANGSlopes/teaching/inf212W15/...Dynamically-typed languages Simple Statically-typed languages Not so simple Discuss Binary components -- Python 1 #!/usr/bin/env

Physical modularization 3 – Typed

<<class>> TFApplication

<<interface>> TFWords

<<interface>> TFFreqs

<<class>> Words1

<<class>> Words2

<<class>> Freqs1

<<class>> Freqs2

Compilation dependencies: pack1? pack2? pack3?

pack1

pack2

pack3

Page 16: INF 212 ANALYSIS OF PROG. LANGSlopes/teaching/inf212W15/...Dynamically-typed languages Simple Statically-typed languages Not so simple Discuss Binary components -- Python 1 #!/usr/bin/env

Physical modularization 4 – Typed

<<class>> TFApplication

<<interface>> TFWords

<<interface>> TFFreqs

<<class>> Words1

<<class>> Words2

<<class>> Freqs1

<<class>> Freqs2

Compilation dependencies: pack1? pack2? pack3?

pack1

pack3

pack4 pack2

Page 17: INF 212 ANALYSIS OF PROG. LANGSlopes/teaching/inf212W15/...Dynamically-typed languages Simple Statically-typed languages Not so simple Discuss Binary components -- Python 1 #!/usr/bin/env

Physical modularization 5 – Typed

<<class>> TFApplication

<<interface>> TFWords

<<interface>> TFFreqs

<<class>> Words1

<<class>> Words2

<<class>> Freqs1

<<class>> Freqs2

????? Compilation dependencies: pack1? pack2?

pack1

pack2

Page 18: INF 212 ANALYSIS OF PROG. LANGSlopes/teaching/inf212W15/...Dynamically-typed languages Simple Statically-typed languages Not so simple Discuss Binary components -- Python 1 #!/usr/bin/env

Linking binary components

3 steps Independent compilation

Dynamic Loading

Instantiation of classes

Page 19: INF 212 ANALYSIS OF PROG. LANGSlopes/teaching/inf212W15/...Dynamically-typed languages Simple Statically-typed languages Not so simple Discuss Binary components -- Python 1 #!/usr/bin/env

Binary components – Typed

interface TFWords { public List<String> extractWords(string path); } interface TFFreqs { public HashMap<String, int> top25(List<String> words); }

class TFApp { static void main(String[] args) { HashMap<String, int> wordFreqs; TFWords tfwords; //= ??? TFFreqs tffreqs ;//= ??? wordFreqs = tffreqs.top25(tfwords.extract_words(sys.argv[0])); } }

Page 20: INF 212 ANALYSIS OF PROG. LANGSlopes/teaching/inf212W15/...Dynamically-typed languages Simple Statically-typed languages Not so simple Discuss Binary components -- Python 1 #!/usr/bin/env

Binary components – Typed

class TFApp { static void main(String[] args) { HashMap<String, int> wordFreqs; TFWords tfwords = new Words1(); TFFreqs tffreqs = new Freqs1(); wordFreqs = tffreqs.top25(tfwords.extract_words(sys.argv[0])); } }

????? Coupling between physical components! TFApp needs one of the other components in order to compile!

Page 21: INF 212 ANALYSIS OF PROG. LANGSlopes/teaching/inf212W15/...Dynamically-typed languages Simple Statically-typed languages Not so simple Discuss Binary components -- Python 1 #!/usr/bin/env

Binary components – Typed

class TFApp { static void main(String[] args) { HashMap<String, int> wordFreqs; TFWords tfwords = create instance dynamically (“...”); TFFreqs tffreqs = create instance dynamically (“...”); wordFreqs = tffreqs.top25(tfwords.extract_words(sys.argv[0])); } }

Given by .ini file

You need to research how to do it in your language of choice

Page 22: INF 212 ANALYSIS OF PROG. LANGSlopes/teaching/inf212W15/...Dynamically-typed languages Simple Statically-typed languages Not so simple Discuss Binary components -- Python 1 #!/usr/bin/env

Dynamic loading of libraries

Java: ClassLoader class

.NET Assembly class

Raw C++, Linux: dlopen, dlsym, dlclose

Raw C++, Win32 LoadLibrary()

Page 23: INF 212 ANALYSIS OF PROG. LANGSlopes/teaching/inf212W15/...Dynamically-typed languages Simple Statically-typed languages Not so simple Discuss Binary components -- Python 1 #!/usr/bin/env

Linking binary components

3 steps Independent compilation

Dynamic Loading

Instantiation of classes

Page 24: INF 212 ANALYSIS OF PROG. LANGSlopes/teaching/inf212W15/...Dynamically-typed languages Simple Statically-typed languages Not so simple Discuss Binary components -- Python 1 #!/usr/bin/env

Instantiation

Java Class.forName(“...”).newInstance();

.NET Activator.CreateInstance(type)

Raw C++ ?? Factory pattern in linked lib, maybe??

Page 25: INF 212 ANALYSIS OF PROG. LANGSlopes/teaching/inf212W15/...Dynamically-typed languages Simple Statically-typed languages Not so simple Discuss Binary components -- Python 1 #!/usr/bin/env

Linking binary components

3 steps Independent compilation

Dynamic Loading

Instantiation of classes