<<

Contents

Softwaretechnik Lecture 19: Specification with Types Specification with Types Excursion: Scripting Languages Peter Thiemann Ultra-Brief JavaScript Tutorial

Universit¨at Freiburg, Germany Thesis

SS 2011

Peter Thiemann (Univ. Freiburg) Softwaretechnik SWT 1 / 21 Peter Thiemann (Univ. Freiburg) Softwaretechnik SWT 2 / 21 Specification with Types Excursion: Scripting Languages Specification with Types Excursion: Scripting Languages Scripting Languages

I Lightweight programming languages evolved from command languages

I Lightweight data structures Excursion to a World Without Types: hashmap (object), strings Scripting Languages I Lightweight syntax familiar, no semicolon, (often not well specified), . . .

I Lightweight typing dynamic, weak, duck typing

I Lightweight metaprogramming

I Lightweight implementation interpreted, few tools

Peter Thiemann (Univ. Freiburg) Softwaretechnik SWT 3 / 21 Peter Thiemann (Univ. Freiburg) Softwaretechnik SWT 4 / 21 I Incompatible DOM implementations in Web browsers programming recipes instead of techniques ⇒ platform independent libraries like JQuery ⇒

Specification with Types Excursion: Scripting Languages Specification with Types Excursion: Scripting Languages JavaScript, a Typical Scripting Language JavaScript, Technically

I Java-style syntax I Initially developed by Brendan Eich of Netscape Corp. I Object-based imperative language node.onmouseout = I Standardized as ECMAScript (ECMA-262 Edition 5.1) I no classes, but prototype concept function (ev) { I Application areas (scripting targets) I objects are hashtables init(); state++; I client-side web scripting (dynamic HTML, SVG, XUL) I First-class functions node.className = I server-side scripting (Whitebeam, Helma, Cocoon, iPlanet) I a functional language "highlight-" I animation scripting (diablo, dim3, k3d) I Weak, dynamic + state; I and many more Slogan: Any type can be converted to ev.stopPropagation(); any other reasonable type };

Peter Thiemann (Univ. Freiburg) Softwaretechnik SWT 5 / 21 Peter Thiemann (Univ. Freiburg) Softwaretechnik SWT 6 / 21 Specification with Types Excursion: Scripting Languages Specification with Types Excursion: Scripting Languages Problems with JavaScript Specific Problems with JavaScript

Symptomatic for other scripting languages I Most popular applications I No module system I client-side scripting I No namespace management I AJAX I No interface descriptions I Dynamic modification of page content via DOM interface I No static type system I DOM = document object model I No application specific datatypes I W3C standard interface for accessing and modifying XML primitive datatypes, strings, hashtables I Mainly used in web browers

I Type conversions are sometimes surprising “A scripting language should never throw an exception [the script should just continue]” (Rob Pike, Google) Limited to small applications ⇒

Peter Thiemann (Univ. Freiburg) Softwaretechnik SWT 7 / 21 Peter Thiemann (Univ. Freiburg) Softwaretechnik SWT 8 / 21 Rule 2: Every value has a type. For most reasonable combinations, values can be converted from one type to another type. Rule 3: Rule 3: Types include , boolean, number, string, object, and function. Types include null, boolean, number, string, object, and function. Rule 4: Rule 4: ‘Undefined’ is a value (and a type). ‘Undefined’ is a value (and a type).

Specification with Types Excursion: Scripting Languages Specification with Types Excursion: Scripting Languages Specific Problems with JavaScript Can You Write Reliable Programs in JavaScript?

I Most popular applications I client-side scripting I Struggle with the lack of e.g. a module system I AJAX I Ad-hoc structuring of large programs I Dynamic modification of page content via DOM interface I Naming conventions I Working in a team I DOM = document object model I W3C standard interface for accessing and modifying XML I Work around DOM incompatibilities I Mainly used in web browers I Use existing JavaScript frameworks (widgets, networking) I Incompatible DOM implementations in Web browsers I Frameworks are also incompatible programming recipes instead of techniques I Wonder about unexpected results ⇒ platform independent libraries like JQuery ⇒

Peter Thiemann (Univ. Freiburg) Softwaretechnik SWT 8 / 21 Peter Thiemann (Univ. Freiburg) Softwaretechnik SWT 9 / 21 Specification with Types Ultra-Brief JavaScript Tutorial Specification with Types Ultra-Brief JavaScript Tutorial An Ultra-Brief JavaScript Tutorial An Ultra-Brief JavaScript Tutorial

Rule 1: Rule 1: JavaScript is object-based. An object is a hash table that maps named JavaScript is object-based. An object is a hash table that maps named properties to values. properties to values. Rule 2: Every value has a type. For most reasonable combinations, values can be converted from one type to another type.

Peter Thiemann (Univ. Freiburg) Softwaretechnik SWT 10 / 21 Peter Thiemann (Univ. Freiburg) Softwaretechnik SWT 10 / 21 Rule 4: ‘Undefined’ is a value (and a type).

Specification with Types Ultra-Brief JavaScript Tutorial Specification with Types Ultra-Brief JavaScript Tutorial An Ultra-Brief JavaScript Tutorial An Ultra-Brief JavaScript Tutorial

Rule 1: Rule 1: JavaScript is object-based. An object is a hash table that maps named JavaScript is object-based. An object is a hash table that maps named properties to values. properties to values. Rule 2: Rule 2: Every value has a type. For most reasonable combinations, values can be Every value has a type. For most reasonable combinations, values can be converted from one type to another type. converted from one type to another type. Rule 3: Rule 3: Types include null, boolean, number, string, object, and function. Types include null, boolean, number, string, object, and function. Rule 4: ‘Undefined’ is a value (and a type).

Peter Thiemann (Univ. Freiburg) Softwaretechnik SWT 10 / 21 Peter Thiemann (Univ. Freiburg) Softwaretechnik SWT 10 / 21 Specification with Types Ultra-Brief JavaScript Tutorial Specification with Types Ultra-Brief JavaScript Tutorial Some Quick Questions Answers

Let’s define an object obj: js> var obj = x:1 { } js> obj.x js> var obj = x: 1 { } 1 What are the values/outputs of js> obj.y js> print(obj.y) obj.x I undefined I obj.y js> obj.y.z I print(obj.y) js: "", line 12: uncaught JavaScript exception:

I obj.y.z ConversionError: The undefined value has no properties. (; line 12)

Peter Thiemann (Univ. Freiburg) Softwaretechnik SWT 11 / 21 Peter Thiemann (Univ. Freiburg) Softwaretechnik SWT 12 / 21 Wrapper objects for booleans

js> flag = new Bool(false); js> result = flag ? true : false;

What is the value of result?

Specification with Types Ultra-Brief JavaScript Tutorial Specification with Types Ultra-Brief JavaScript Tutorial Weak, Dynamic Types in JavaScript II Weak, Dynamic Types in JavaScript II

Rule 5: Rule 5: An object is really a dynamic mapping from strings to values. An object is really a dynamic mapping from strings to values. js> var x = "x" js> var x = "x" js> obj[x] js> obj[x] 1 1 js> obj.undefined = "gotcha" js> obj.undefined = "gotcha" gotcha gotcha js> obj[obj.y] js> obj[obj.y] == obj[undefined] What is the effect/result of the last expression? == obj["undefined"] == obj.undefined == "gotcha" == "gotcha"

Peter Thiemann (Univ. Freiburg) Softwaretechnik SWT 13 / 21 Peter Thiemann (Univ. Freiburg) Softwaretechnik SWT 14 / 21 Specification with Types Ultra-Brief JavaScript Tutorial Specification with Types Ultra-Brief JavaScript Tutorial Weak, Dynamic Types in JavaScript III Weak, Dynamic Types in JavaScript III

Wrapper objects for numbers Recall Rule 2: Every value has a type. For most reasonable combinations, values can be js> m = new Number (17); n = new Number (4) converted from one type to another type. js> m+n 21 js> var a = 17 js> a.x = 42 42 js> a.x

What is the effect/result of the last expression?

Peter Thiemann (Univ. Freiburg) Softwaretechnik SWT 15 / 21 Peter Thiemann (Univ. Freiburg) Softwaretechnik SWT 16 / 21 Specification with Types Ultra-Brief JavaScript Tutorial Specification with Types Ultra-Brief JavaScript Tutorial Weak, Dynamic Types in JavaScript III Weak, Dynamic Types in JavaScript IV

Wrapper objects for numbers Rule 6: Functions are first-class, but behave differently when used as methods or as constructors. js> m = new Number (17); n = new Number (4) js> m+n js> function f () return this.x 21 { } js> f() x Wrapper objects for booleans js> obj.f = f function f() return this.x; { } js> flag = new Bool(false); js> obj.f() js> result = flag ? true : false; 1 js> new f() What is the value of result? [object Object]

Peter Thiemann (Univ. Freiburg) Softwaretechnik SWT 16 / 21 Peter Thiemann (Univ. Freiburg) Softwaretechnik SWT 17 / 21 Specification with Types Ultra-Brief JavaScript Tutorial Specification with Types Ultra-Brief JavaScript Tutorial Distinguishing Absence and Undefinedness I Distinguishing Absence and Undefinedness II

Rule 7: js> obju = u : .xx The with construct puts its argument object on top of the current { {} } [object Object] environment stack. js> objv = v : .xx { {} } [object Object] js> u = "defined" js> print(obju.u) defined undefined js> with (obju) print(u) js> print(objv.u) undefined undefined js> with (objv) print(u) defined

Peter Thiemann (Univ. Freiburg) Softwaretechnik SWT 18 / 21 Peter Thiemann (Univ. Freiburg) Softwaretechnik SWT 19 / 21 Specification with Types Ultra-Brief JavaScript Tutorial Specification with Types Thesis Distinguishing Absence and Undefinedness III Thesis

Rule 8: The for construct has an in operator to range over all defined indexes. I Common errors such as I using non-objects as objects js> for (i in obju) print(i) e.g. using numbers as functions u I invoking non-existing methods js> for (i in objv) print(i) I accessing non-existing fields v I surprising conversions js> delete objv.v can all be caught by a true Static Type System js> for (i in objv) print(i) I and much more. js> delete objv.v true

Peter Thiemann (Univ. Freiburg) Softwaretechnik SWT 20 / 21 Peter Thiemann (Univ. Freiburg) Softwaretechnik SWT 21 / 21