mozilla's nodeconf talk

40
mozilla SpiderNode , V8Monkey , and You 1 Thursday, May 5, 2011

Upload: brendan-eich

Post on 08-May-2015

4.907 views

Category:

Technology


1 download

DESCRIPTION

Mozilla's NodeConf talk: SpiderNode, V8Monkey, and You

TRANSCRIPT

Page 2: Mozilla's NodeConf talk

mozilla

What?

2

Thursday, May 5, 2011

Page 3: Mozilla's NodeConf talk

mozilla

What?

2

In which we put a v8 API on top of spidermonkey without futzing with a separate build system. Prep work for spidernode. (from an autoupdated unofficial mozilla-central clone)

Thursday, May 5, 2011

Page 4: Mozilla's NodeConf talk

mozilla

Why?

3

Thursday, May 5, 2011

Page 5: Mozilla's NodeConf talk

mozilla

Why?

• We at Mozilla <3 V8 (we <3 WebKit/JavaScriptCore too; Chakra not so much)

3

Thursday, May 5, 2011

Page 6: Mozilla's NodeConf talk

mozilla

Why?

• We at Mozilla <3 V8 (we <3 WebKit/JavaScriptCore too; Chakra not so much)

• SpiderMonkey (born 1996, reborn 2008 [TraceMonkey], 2010 [JaegerMonkey], 2011 [TypeInferenceMonkey], 2012 [IonMonkey] -- “My afterlife is *so* boring!” - Heathers) really needs a better API

3

Thursday, May 5, 2011

Page 7: Mozilla's NodeConf talk

mozilla

Why?

• We at Mozilla <3 V8 (we <3 WebKit/JavaScriptCore too; Chakra not so much)

• SpiderMonkey (born 1996, reborn 2008 [TraceMonkey], 2010 [JaegerMonkey], 2011 [TypeInferenceMonkey], 2012 [IonMonkey] -- “My afterlife is *so* boring!” - Heathers) really needs a better API

• V8 has a nice “C++ the good parts” API

3

Thursday, May 5, 2011

Page 8: Mozilla's NodeConf talk

mozilla

Why?

• We at Mozilla <3 V8 (we <3 WebKit/JavaScriptCore too; Chakra not so much)

• SpiderMonkey (born 1996, reborn 2008 [TraceMonkey], 2010 [JaegerMonkey], 2011 [TypeInferenceMonkey], 2012 [IonMonkey] -- “My afterlife is *so* boring!” - Heathers) really needs a better API

• V8 has a nice “C++ the good parts” API

• Template types, RAII storage class auto helpers, GC all the way down

3

Thursday, May 5, 2011

Page 9: Mozilla's NodeConf talk

mozilla

Why?

• We at Mozilla <3 V8 (we <3 WebKit/JavaScriptCore too; Chakra not so much)

• SpiderMonkey (born 1996, reborn 2008 [TraceMonkey], 2010 [JaegerMonkey], 2011 [TypeInferenceMonkey], 2012 [IonMonkey] -- “My afterlife is *so* boring!” - Heathers) really needs a better API

• V8 has a nice “C++ the good parts” API

• Template types, RAII storage class auto helpers, GC all the way down

• JS the language is evolving -- Harmony coming in ES.next

3

Thursday, May 5, 2011

Page 10: Mozilla's NodeConf talk

mozilla

Why?

• We at Mozilla <3 V8 (we <3 WebKit/JavaScriptCore too; Chakra not so much)

• SpiderMonkey (born 1996, reborn 2008 [TraceMonkey], 2010 [JaegerMonkey], 2011 [TypeInferenceMonkey], 2012 [IonMonkey] -- “My afterlife is *so* boring!” - Heathers) really needs a better API

• V8 has a nice “C++ the good parts” API

• Template types, RAII storage class auto helpers, GC all the way down

• JS the language is evolving -- Harmony coming in ES.next

• Node is a great testbed for new JS features

3

Thursday, May 5, 2011

Page 11: Mozilla's NodeConf talk

mozilla

Approved for ES.next

4

Thursday, May 5, 2011

Page 12: Mozilla's NodeConf talk

mozilla

Approved for ES.next

• let, const, function in block scope

4

Thursday, May 5, 2011

Page 13: Mozilla's NodeConf talk

mozilla

Approved for ES.next

• let, const, function in block scope

• destructuring: let {x, y} = pt; let [s, v, o] = triple()

4

Thursday, May 5, 2011

Page 14: Mozilla's NodeConf talk

mozilla

Approved for ES.next

• let, const, function in block scope

• destructuring: let {x, y} = pt; let [s, v, o] = triple()

• parameter default values: function f(x, y=1, z=0) {...}

4

Thursday, May 5, 2011

Page 15: Mozilla's NodeConf talk

mozilla

Approved for ES.next

• let, const, function in block scope

• destructuring: let {x, y} = pt; let [s, v, o] = triple()

• parameter default values: function f(x, y=1, z=0) {...}

• rest, spread: function g(i, j, ...r) { return r.slice(i, j); } let a = [0,1,2,3], o = new any_constructor(...a)

4

Thursday, May 5, 2011

Page 16: Mozilla's NodeConf talk

mozilla

Approved for ES.next

• let, const, function in block scope

• destructuring: let {x, y} = pt; let [s, v, o] = triple()

• parameter default values: function f(x, y=1, z=0) {...}

• rest, spread: function g(i, j, ...r) { return r.slice(i, j); } let a = [0,1,2,3], o = new any_constructor(...a)

• proxies, weak maps: Proxy.create(handler, proto), new WeakMap

4

Thursday, May 5, 2011

Page 17: Mozilla's NodeConf talk

mozilla

Approved for ES.next

• let, const, function in block scope

• destructuring: let {x, y} = pt; let [s, v, o] = triple()

• parameter default values: function f(x, y=1, z=0) {...}

• rest, spread: function g(i, j, ...r) { return r.slice(i, j); } let a = [0,1,2,3], o = new any_constructor(...a)

• proxies, weak maps: Proxy.create(handler, proto), new WeakMap

• modules: module M { export function fast_sin(x) {...} }

4

Thursday, May 5, 2011

Page 18: Mozilla's NodeConf talk

mozilla

Approved for ES.next

• let, const, function in block scope

• destructuring: let {x, y} = pt; let [s, v, o] = triple()

• parameter default values: function f(x, y=1, z=0) {...}

• rest, spread: function g(i, j, ...r) { return r.slice(i, j); } let a = [0,1,2,3], o = new any_constructor(...a)

• proxies, weak maps: Proxy.create(handler, proto), new WeakMap

• modules: module M { export function fast_sin(x) {...} }

• iterators, generators: function* gen() { yield 1; yield 2; }

4

Thursday, May 5, 2011

Page 19: Mozilla's NodeConf talk

mozilla

Approved for ES.next

• let, const, function in block scope

• destructuring: let {x, y} = pt; let [s, v, o] = triple()

• parameter default values: function f(x, y=1, z=0) {...}

• rest, spread: function g(i, j, ...r) { return r.slice(i, j); } let a = [0,1,2,3], o = new any_constructor(...a)

• proxies, weak maps: Proxy.create(handler, proto), new WeakMap

• modules: module M { export function fast_sin(x) {...} }

• iterators, generators: function* gen() { yield 1; yield 2; }

• comprehensions: return [a+b for (a in A) for (b in B)]

4

Thursday, May 5, 2011

Page 20: Mozilla's NodeConf talk

mozilla

Yet more approved for ES.next

5

Thursday, May 5, 2011

Page 21: Mozilla's NodeConf talk

mozilla

Yet more approved for ES.next

• Binary data:

5

Thursday, May 5, 2011

Page 22: Mozilla's NodeConf talk

mozilla

Yet more approved for ES.next

• Binary data:

• const Point2D = new StructType({ x: uint32, y: uint32 }), Color = new StructType({ r: uint8, g: uint8, b: uint8 }), Pixel = new StructType({ point: Point2D, color: Color });

5

Thursday, May 5, 2011

Page 23: Mozilla's NodeConf talk

mozilla

Yet more approved for ES.next

• Binary data:

• const Point2D = new StructType({ x: uint32, y: uint32 }), Color = new StructType({ r: uint8, g: uint8, b: uint8 }), Pixel = new StructType({ point: Point2D, color: Color });

• const Triangle = new ArrayType(Pixel, 3);

5

Thursday, May 5, 2011

Page 24: Mozilla's NodeConf talk

mozilla

Yet more approved for ES.next

• Binary data:

• const Point2D = new StructType({ x: uint32, y: uint32 }), Color = new StructType({ r: uint8, g: uint8, b: uint8 }), Pixel = new StructType({ point: Point2D, color: Color });

• const Triangle = new ArrayType(Pixel, 3);

• new Triangle([{ point: { x: 0, y: 0 }, color: { r: 255, g: 255, b: 255 } }, { point: { x: 5, y: 5 }, color: { r: 128, g: 0, b: 0 } }, { point: { x: 10, y: 0 }, color: { r: 0, g: 0, b: 128 } }]);

5

Thursday, May 5, 2011

Page 25: Mozilla's NodeConf talk

mozilla

Hot, but not yet in Harmony

6

Thursday, May 5, 2011

Page 26: Mozilla's NodeConf talk

mozilla

Hot, but not yet in Harmony

• Arrow function syntax, instead of λ, ƒ, or # (want to save # for later)

6

Thursday, May 5, 2011

Page 27: Mozilla's NodeConf talk

mozilla

Hot, but not yet in Harmony

• Arrow function syntax, instead of λ, ƒ, or # (want to save # for later)

• Just like CoffeeScript: let identity = (x) -> x

6

Thursday, May 5, 2011

Page 28: Mozilla's NodeConf talk

mozilla

Hot, but not yet in Harmony

• Arrow function syntax, instead of λ, ƒ, or # (want to save # for later)

• Just like CoffeeScript: let identity = (x) -> x

• Expression body: const square = (x) -> (x * x)

6

Thursday, May 5, 2011

Page 29: Mozilla's NodeConf talk

mozilla

Hot, but not yet in Harmony

• Arrow function syntax, instead of λ, ƒ, or # (want to save # for later)

• Just like CoffeeScript: let identity = (x) -> x

• Expression body: const square = (x) -> (x * x)

• Statement body: let countUsed = (str) -> { if (str in usedWords) usedWords[str]++; else usedWords[str] = 1; }

6

Thursday, May 5, 2011

Page 30: Mozilla's NodeConf talk

mozilla

Hot, but not yet in Harmony

• Arrow function syntax, instead of λ, ƒ, or # (want to save # for later)

• Just like CoffeeScript: let identity = (x) -> x

• Expression body: const square = (x) -> (x * x)

• Statement body: let countUsed = (str) -> { if (str in usedWords) usedWords[str]++; else usedWords[str] = 1; }

• Fat arrow too: callback = (msg) => ( this.vmail.push(msg) )

6

Thursday, May 5, 2011

Page 31: Mozilla's NodeConf talk

mozilla

Hot, but not yet in Harmony

• Arrow function syntax, instead of λ, ƒ, or # (want to save # for later)

• Just like CoffeeScript: let identity = (x) -> x

• Expression body: const square = (x) -> (x * x)

• Statement body: let countUsed = (str) -> { if (str in usedWords) usedWords[str]++; else usedWords[str] = 1; }

• Fat arrow too: callback = (msg) => ( this.vmail.push(msg) )

• Binding forms: let f() -> “writable” const K() -> “readonly”

6

Thursday, May 5, 2011

Page 32: Mozilla's NodeConf talk

mozilla

What else?

7

Thursday, May 5, 2011

Page 33: Mozilla's NodeConf talk

mozilla

What else?

• CoffeeScript classes, for prototypal inheritance sugar

• Or a different classes as closure pattern sugar proposal?

• Or (and this is somewhat Coffee-like) extended object initialisers?

7

Thursday, May 5, 2011

Page 34: Mozilla's NodeConf talk

mozilla

What else?

• CoffeeScript classes, for prototypal inheritance sugar

• Or a different classes as closure pattern sugar proposal?

• Or (and this is somewhat Coffee-like) extended object initialisers?

• Coffee’s @foo for this.foo

• Or some private names or “soft fields” @ usage?

7

Thursday, May 5, 2011

Page 35: Mozilla's NodeConf talk

mozilla

What else?

• CoffeeScript classes, for prototypal inheritance sugar

• Or a different classes as closure pattern sugar proposal?

• Or (and this is somewhat Coffee-like) extended object initialisers?

• Coffee’s @foo for this.foo

• Or some private names or “soft fields” @ usage?

• Paren-free syntax: if x > y return x while i < n { a.push(i++); }

7

Thursday, May 5, 2011

Page 36: Mozilla's NodeConf talk

mozilla

What else?

• CoffeeScript classes, for prototypal inheritance sugar

• Or a different classes as closure pattern sugar proposal?

• Or (and this is somewhat Coffee-like) extended object initialisers?

• Coffee’s @foo for this.foo

• Or some private names or “soft fields” @ usage?

• Paren-free syntax: if x > y return x while i < n { a.push(i++); }

• More operators: ?? ??= div mod divmod is isnt

7

Thursday, May 5, 2011

Page 37: Mozilla's NodeConf talk

mozilla

Demo: generators for callback-free i/o

• https://github.com/dherman/taskjs

var {task} = require('./taskjs/lib/task.js');var fs = require('fs');function readFile(path) { return new NodeReadFile(path);}function NodeReadFile(path) { this.path = path; var wait = this; fs.readFile(path, function (err, data) { if (err) { wait.throw(err); } else { wait.return(data); } });}

8

Thursday, May 5, 2011

Page 38: Mozilla's NodeConf talk

mozilla

Demo: generators, continued

• NodeReadFile.prototype = new task.Wait();

task.spawn(function () { try { var data = yield readFile('gen.js'); console.log(data.toString('ascii')); } catch (e) { console.log(e); }});

• You have to write yield a bit (don’t forget it!)

• But you don’t have to write function(){...} nests

9

Thursday, May 5, 2011

Page 39: Mozilla's NodeConf talk

mozilla

Even shorter generator anti-nesting demo

• let {Wait, spawn, choose} = require('./taskjs/lib/task.js').task;let newRequest = new Wait();require('http').createServer(function (req, res) { newRequest.return([req, res]);}).listen(10337, "127.0.0.1");spawn(function () { let i = 0; while (true) { let [req, res] = yield newRequest; if (req.url === '/') { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World: ' + i + '\n'); i++; } }})console.log('Server running at http://127.0.0.1:10337/');

10

Thursday, May 5, 2011

Page 40: Mozilla's NodeConf talk

mozilla

Thanks, contact, more demos, Q&A

• Thanks to @robarnold @sdwilsh @zpao @john_h_ford and @andreasgal

• irc.mozilla.org #spidernode

[email protected] (mailman subscribe request)

• More demos

• NodeChat running at SSID spidernode 169.254.64.209

• Questions?

11

Thursday, May 5, 2011