devcon1 - boojs

27
Yet another Javascript transpiler Iván – DrSlump – Montes @drslump www.pollinimini.net github.com/drslump Tuesday, November 27, 12

Upload: ivan-montes

Post on 28-Jan-2015

104 views

Category:

Technology


0 download

DESCRIPTION

Code examples: https://gist.github.com/4167417 Async/Await demo: http://jsfiddle.net/drslump/PrgQc/

TRANSCRIPT

Page 1: DEVCON1 - BooJs

Yet  another  Javascript  transpiler

Iván  –  DrSlump  –  Montes@drslump  -­‐  www.pollinimini.net  -­‐  github.com/drslump

Tuesday, November 27, 12

Page 2: DEVCON1 - BooJs

Javascript  as  Virtual  Machine

Tuesday, November 27, 12

Page 3: DEVCON1 - BooJs

JS is the x86 of the web.

The point is JS is about as low as we can go. But it also has higher-level facilities.

Brendan  EichJavaScript’s  creator

Tuesday, November 27, 12

Page 4: DEVCON1 - BooJs

Douglas  CrockfordJavaScript  evangelist

JavaScript is the VM of the web. We had always thought that Java’s JVM would be the VM of the web, but it turns out that it’s JavaScript.

Tuesday, November 27, 12

Page 5: DEVCON1 - BooJs

JonnycatY  Combinator  user

The JavaScript we've got now is the assembly language of the client-side. We can't easily change it, but we have to start building better tools on top of it.

Tuesday, November 27, 12

Page 6: DEVCON1 - BooJs

A  Popular  Trend

Tuesday, November 27, 12

Page 7: DEVCON1 - BooJs

Open  source  project  with  a  huge  communitySyntax  sugar,  more  a  pre-­‐processor  than  a  compilerFocused  on  having  a  very  concise  syntax

grade = (student) -> if student.excellentWork "A+" else if student.okayStuff if student.triedHard then "B" else "B-" else "C"

eldest = if 24 > 21 then "Liz" else "Ike"

Tuesday, November 27, 12

Page 8: DEVCON1 - BooJs

Backed  by  GoogleAnnotated  Javascript  codeMore  a  staOc  analysis  tool  than  a  compiler

/** * A shape. * @interface */function Shape() {};Shape.prototype.draw = function() {};

/** * @constructor * @implements {Shape} */function Square() {};Square.prototype.draw = function() {  ...};

Tuesday, November 27, 12

Page 9: DEVCON1 - BooJs

Backed  by  MicrosoPType  extensions  to  JavaScript  syntaxFocused  on  being  IDE  friendly

interface Person { firstname: string; lastname: string;}

function greeter(person : Person) { return "Hello, " + person.firstname + " " + person.lastname;}

var user = {firstname: "Jane", lastname: "User"};

document.body.innerHTML = greeter(user);

Tuesday, November 27, 12

Page 10: DEVCON1 - BooJs

• Javascript  syntax  is  geRng  dated  (Harmony  in  the  works  to  somehow  remedy  that)

• Browser  war  has  focused  on  Javascript  engines,  huge  performance  boost  in  latest  years.

• Programmers  with  many  different  backgrounds  using  Javascript  to  develop/deploy  complex  products.

• Much  improved  debugging  tools  (Firebug,  SourceMaps,  OpOmizers,  …)

• User  friendly  write-­‐compile-­‐test  workflows    

Why  so  many?

Tuesday, November 27, 12

Page 11: DEVCON1 - BooJs

• The  standard  is  finally  coming  along  but  not  expected  to  be  finished  unOl  mid  2013.  Browser  vendors  are  not  too  far  from  having  a  working  implementaOon  though.

• The  browser  market  is  hugely  fragmented,  mobile  devices  will  probably  held  back  early  adopOon  this  Ome,  although  we  will  conOnue  to  blame  MicrosoP  :-­‐)

• Harmony  is  being  driven  to  be  highly  performant  on  current  engines  not  that  much  on  improving  developers  performance.

• Harmony  will  be  a  good  step  forward  but  it  won’t  render  fuOle  the  JavaScript  as  Virtual  Machine  approach.

Won’t  Harmony  save  JavaScript?

Tuesday, November 27, 12

Page 12: DEVCON1 - BooJs

The  Boo  Language

Tuesday, November 27, 12

Page 13: DEVCON1 - BooJs

Boo is an object oriented statically typed programming language ... with a python inspired syntax and a special focus on language and compiler extensibility.

Rodrigo  B.  De  OliveiraBoo’s  creator

Tuesday, November 27, 12

Page 14: DEVCON1 - BooJs

import System

class Person: public name as string public age as int

def constructor(name as string): .name = name age = 0

def birthday(p as Person): p.age += 1

# Create an instanceme = Person('DrSlump', age: 32)birthday(me)print "$(me.name) celebrated his $(me.age) birthday"

Tuesday, November 27, 12

Page 15: DEVCON1 - BooJs

Readable  and  expressive  syntaxPython  inspired

DSL  oriented

StaOcally  typedType  inference

Duck  typing

ExtensibleMacros  and  Meta  methods  (AST  manipulaOon)

Open  compiler  design  (Pipelines)

Tuesday, November 27, 12

Page 16: DEVCON1 - BooJs

Language  Features.Net  Type  System  (classes,  value  types,  interfaces,  ...)

Slicing  syntax,  Variadic  arguments,  IniOalizers

FuncOons  are  first  class  ciOzens

Closures

Events,  Generators,  Expression  modifiers

Method  and  Operators  Overloading

Tuesday, November 27, 12

Page 17: DEVCON1 - BooJs

Extensibility  and  Meta  programmingSyntacOc  Macros  and  Afributes

Meta  methods  (macros  aPer  type  resoluOon)

Pafern  Matching

Custom  pipelines

Compiler  as  a  service

Tuesday, November 27, 12

Page 18: DEVCON1 - BooJs

BooJsboo·jay·es /bʊ ˈdʒeɪ ɛs/

Tuesday, November 27, 12

Page 19: DEVCON1 - BooJs

Why  did  you  do  it?More  bang  for  the  buck

If  we  are  going  to  require  a  compilaOon  workflow,  lets  have  a  really  powerful  compiler,  not  just  syntacOc  sugar.

An  almost  perfect  type  systemA  staOcally  typed  language  with  the  look  and  feel  of  a  scripOng  language  is  a  great  combinaOon,  specially  for  medium  to  large  code  bases.  It’s  just  one  small  step  short  from  latent  typing,  which  I  hope  to  support  in  the  future.

It  is  FUN!Programming  is  fun  ergo  programming  compilers  is  twice  as  fun!

Tuesday, November 27, 12

Page 20: DEVCON1 - BooJs

Boo JavaScriptFirst-­‐class  funcEons  ✓  ✓

FuncEon  expressions  ✓  ✓Closures  ✓  ✓

Scope Lexical  (funcOon  and  types) Lexical  (funcOon)

Type  system StaOc  and  Dynamic  (strong) Dynamic  (weak)

Variadic  funcEons  ✓  ✓  (via  arguments)

Inheritance Class/Type  based Prototypal

Generators  ✓ ✘

List  comprehensions  ✓ ✘

Iterators  ✓ ✘

ProperEes  ✓ ✘

Destructuring  ✓ ✘

Method  overloading  ✓ ✘

Operator  overloading  ✓ ✘

Macros  ✓ ✘

PaLern  Matching  ✓ ✘

Comparison  with  Javascript

Tuesday, November 27, 12

Page 21: DEVCON1 - BooJs

ImplementaOonLeverages  Boo’s  open  compiler  design

Implemented  using  the  Boo  language

Runs  on  .Net  4.0  and  Mono  2.1.x

Over  700  unit  tests,  automated  with  Travis-­‐CI

Generates  a  Mozilla  AST,  prefy  printed  to  JS  1.5

SourceMaps  support  

JS  type  system  and  runOme  (no  .NET  emulaOon!)

Lightweight  runOme  library  (≈4kb  gzipped)

Tuesday, November 27, 12

Page 22: DEVCON1 - BooJs

ApplicaOon  structureEach  file  is  a  Module

Each  Module  has  a  single  Namespace

Modules  are  organized  in  Assemblies

Only  one  of  the  modules  can  be  the  entry  point

External  Namespaces  and  Types  can  be  imported

Each  assembly  generates  an  output  Javascript  file

An  AMD  style  loader  is  used  to  resolve  dependencies

Tuesday, November 27, 12

Page 23: DEVCON1 - BooJs

Using  exisOng  JavaScript  codeMacros  and  afributes  to  model  external  Javascript

Standard  APIs  like  the  DOM/jQuery  are  provided

Support  for  prototypal  and  Module  Pafern  styles

Extern  type  definiOons  do  not  generate  any  code

`backOck  quoted  code`  is  embedded  unmodified

Global  macro  to  access  exisOng  symbols

Duck  typing  allows  to  use  pracOcally  everything!

Tuesday, November 27, 12

Page 24: DEVCON1 - BooJs

Code  example

Tuesday, November 27, 12

Page 25: DEVCON1 - BooJs

Tuesday, November 27, 12

Page 26: DEVCON1 - BooJs

The limits of my language are the limits of my world.

Ludwig  WifgensteinPhilosopher  (1889-­‐1951)

Tuesday, November 27, 12

Page 27: DEVCON1 - BooJs

Related  materialBooJs  project  page

Boo  website

Tuesday, November 27, 12