1 / 33

Specifying JavaScript in ML

Specifying JavaScript in ML. Dave Herman Cormac Flanagan. JavaScript: the “j” in Ajax. JavaScript is not Java-Lite. JavaScript is almost nothing like Java! Multi-paradigm Functional! Lexically-scoped! Prototypes, no classes (until now…) Dynamically typed (until now…). Outline.

tieve
Download Presentation

Specifying JavaScript in ML

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Specifying JavaScript in ML Dave Herman Cormac Flanagan

  2. JavaScript: the “j” in Ajax http://www.ecmascript.org

  3. JavaScript is not Java-Lite. JavaScript is almost nothing like Java! • Multi-paradigm • Functional! Lexically-scoped! • Prototypes, no classes (until now…) • Dynamically typed (until now…) http://www.ecmascript.org

  4. Outline • How we got here • Specifying JavaScript with ML • The evolution of JavaScript • Status report http://www.ecmascript.org

  5. I. Testimonial: coming to ML

  6. Standardized JavaScript • Many implementations exist (multiple browsers, JVM, Flash, …) • Highly competitive market • First standardized at Ecma Int’l in ’97 http://www.ecmascript.org

  7. Existing JavaScript specifications Grammar production: ConditionalExpression→LogicalORExpression?AssignmentExpression:AssignmentExpression Evaluation: • Evaluate LogicalORExpression. • Call GetValue(Result(1)). • Call ToBoolean(Result(2)). • If Result(3) is false, go to step 8. • Evaluate the first AssignmentExpression. • Call GetValue(Result(5)). • Return Result(6). • Evaluate the second AssignmentExpression. • Call GetValue(Result(8)). • Return Result(9). Any bugs in there? Who knows? http://www.ecmascript.org

  8. Existing JavaScript specifications From: Brendan Eich Date: 12/2/2005 To: Dave Herman Subject: Specifying JavaScript semantics ...In my view, we TG1’ers desperately need a machine- checkable specification... Others in TG1 may agree in principle, but are not wild about taking too much time to develop a checkable spec. So we are looking for help... http://www.ecmascript.org

  9. Meeting in the middle informal prose,pseudocode definitionalinterpreter feasible formal methods,mechanized frameworks precise,accurate http://www.ecmascript.org

  10. One interpreter, several artifacts • Read—literate source • Run, test—executable • Prove theorems—source code (maybe) http://www.ecmascript.org

  11. An executable spec in SML/NJ • First-level sanity checks • Tests: JS2 standard library,JS2 → Flash VM compiler • Biggest benefit: Mozilla/Adobe regression test suites for JavaScript 1.x http://www.ecmascript.org

  12. II. Specifying JavaScript with SML

  13. Interpreter style • No gratuitous optimization • No gratuitous mutation (duh!) • Modularize language feature encodings (“keep the monster in a box”) • Big-step, impure, direct style eval : EXPR * ENV -> VAL http://www.ecmascript.org

  14. Exploiting ML: algebraic datatypes datatype EXPR = UnaryExpr of (UNOP * EXPR) | BinaryExpr of (BINOP * EXPR * EXPR) | TernaryExpr of (EXPR * EXPR * EXPR) | LiteralExpr of LITERAL | CallExpr of { func: EXPR, actuals: EXPR list } | ... and STMT = ExprStmt of EXPR | ForStmt of FOR_STMT | WhileStmt of WHILE_STMT | IfStmt of { cnd: EXPR, thn: STMT, els: STMT } | ... http://www.ecmascript.org

  15. Exploiting ML: refs datatype VAL = Object of OBJ | Null | Undef and OBJ = Obj of { ident: int, tag: VAL_TAG, proto: VAL, props: PROP_BINDINGS } and VAL_TAG = ObjectTag of FIELD_TYPE list | ArrayTag of TYPE_EXPR list | FunctionTag of FUNC_TYPE | ClassTag of NAME withtype PROP = { ty: TYPE_EXPR, attrs: ATTRS, state: VAL } and PROP_BINDINGS = (NAME * PROP) list ref http://www.ecmascript.org

  16. Exploiting ML: exceptions • Non-local jumps in JS: return, throw, tail calls • Tail calls via modified trampoline: exception TailCallException of (unit -> VAL) • Non-tail function call protocol: [[expr]]handle TailCallException thunk => thunk() http://www.ecmascript.org

  17. Exploiting (S)ML(/NJ): callcc • JavaScript generators (coroutines) • yield instead of return: suspend current procedure activation as an object function nats() { var i = 0; while (true) { yield i; i++; } } var gen = nats(); print(gen.next()); // 0 print(gen.next()); // 1 print(gen.next()); // 2 http://www.ecmascript.org

  18. Exploiting (S)ML(/NJ): callcc • yield captures a (delimited) continuation • Alternatives: threads, delimited continuations, CPS • Benefit of callcc: localizing the encoding http://www.ecmascript.org

  19. Exploiting ML: modules datatype OBJ = Obj of { ... props: PROP_BINDINGS ... } withtype PROP = { ty: TYPE_EXPR, attrs: ATTRS, state: VAL } and PROP_BINDINGS = (NAME * PROP) list ref http://www.ecmascript.org

  20. Exploiting ML: modules datatype OBJ = Obj of { ... props: PROP_BINDINGS ... } withtype PROP = { ty: TYPE_EXPR, attrs: ATTRS, state: VAL } and PROP_BINDINGS = (PROP NameMap.map) ref structure NameMap = BinaryMapFn (NameKey); (* structure NameMap = SplayMapFn (NameKey); *) (* structure NameMap = ListMapFn (NameKey); *) http://www.ecmascript.org

  21. Limitations of code • Underspecification: sort must be nlog(n) • Overspecification: type(x) reveals concrete implementation of interface • Conclusion: prose is still necessary http://www.ecmascript.org

  22. Frustrations with SML • Non-nested withtype declarations • Deciphering type inference errors • No recursive modules http://www.ecmascript.org

  23. III. The evolution of JavaScript Typed functional programming for the web

  24. JavaScript 1.x/ECMAScript Edition 3 • Dynamically typed • Mostly well-behaved scoping • Prototypes inspired by Self • Objects: (string × value) table http://www.ecmascript.org

  25. JavaScript 2.0/ECMAScript Edition 4 • “Opt-in” static typing • Improved lexical scoping • Prototypes and classes • Objects: ((key × string) × value) table • Generous syntactic sugar • Advanced control constructs (generators) http://www.ecmascript.org

  26. Gradual typing • Conservative extension of dynamically typed JavaScript 1.x • Optional type annotations • Optional static type checker • Careful interface between typed and untyped code http://www.ecmascript.org

  27. Type system • Nominal types (Java, C#) • Structural types type thunk = function():int; function foo(f:thunk):[int] { ... } var obj : { m:function(thunk):[int] } = { m:foo }; • Type “dynamic”: var image : * = read(filename); switch type (image) { ... } • Parameterized types http://www.ecmascript.org

  28. IV. Status report

  29. Status report • Feature freeze, coding and writing • ~25 KLOC mostly pure SML, ~12 KLOC JS2 standard libraries • SML/NJ, proof-of-concept ports to SML.NET and MLton • Release: 2008 (tentatively) http://www.ecmascript.org

  30. Steal this source code • Read, run, test  • Prove theorems? • Industry-scale case study for research • Experimental language extensions • Development tools, IDE’s • Inference, migration tools • …what else? Have at it:www.ecmascript.org/download.php http://www.ecmascript.org

  31. Thank you! http://www.ecmascript.org http://www.ecmascript.org

  32. What, no monads? datatype RESULT = RETURN of VAL | EXCEPTION of VAL type cont = RESULT * STORE -> RESULT * STORE type comp = STORE * cont -> RESULT * STORE eval : EXPR * ENV -> comp Didn’t ML already do this for us? http://www.ecmascript.org

  33. New functional programming idioms • More expression forms, including function bodies (down with statements!) • Better-behaved lexical scoping (let, controlling with, eval) • Destructuring binding ≈ pattern matching http://www.ecmascript.org

More Related