Slide 1

Slide 1 text

Practice on embedding Node.js into Atom Editor Cheng Zhao https://github.com/zcbenz

Slide 2

Slide 2 text

• Atom is a text editor written by GitHub
 https://github.com/atom/atom • Atom Shell is a desktop application shell based on Chromium and Node.js
 https://github.com/atom/atom- shell • Atom is built upon Atom Shell Atom Editor and Atom Shell

Slide 3

Slide 3 text

Atom Shell is a Node.js programmable Chromium browser

Slide 4

Slide 4 text

Chromium Browser Browser Window Window Window … Tray Menu Dialog Controlled by C++ Renderer HTML JavaScript (DOM) Renderer HTML JavaScript (DOM) Renderer HTML JavaScript (DOM) IPC

Slide 5

Slide 5 text

Atom Shell Browser Window Window Window … Tray Menu Dialog Controlled by Node.js Renderer HTML JavaScript (DOM) Node.js Renderer HTML JavaScript (DOM) Node.js Renderer HTML JavaScript (DOM) Node.js IPC

Slide 6

Slide 6 text

Node.js in Atom Shell • Atom Shell has embedded Node.js in both Chromium browser’s browser process and renderer process • JavaScript in browser process can do GUI related operations like creating windows and menus. • JavaScript in renderer process can use Node.js APIs in addition to standard DOM.

Slide 7

Slide 7 text

How Node.js is embedded into Atom Shell?

Slide 8

Slide 8 text

3 steps

Slide 9

Slide 9 text

1. Initialize V8 and everything 2. Load Node.js environment 3. Entering event loop

Slide 10

Slide 10 text

Step 2. Load Node.js env

Slide 11

Slide 11 text

Node.js <= 0.10.x main(int argc, char *argv[]) context = Context::New() SetupProcessObject(argc, argv); Load(process_l); uv_run(); Load environment Event loop Create context Program entrance

Slide 12

Slide 12 text

allows only one global context

Slide 13

Slide 13 text

multi-context patch http://strongloop.com/strongblog/whats-new-node-js-v0-12- multiple-context-execution/

Slide 14

Slide 14 text

Node.js >= 0.11.x main(int argc, char *argv[]) context = Context::New() env = CreateEnvironment(context) uv_run(); Load environment Event loop Create context Program entrance

Slide 15

Slide 15 text

multiple contexts in one process

Slide 16

Slide 16 text

Node.js in Atom Shell main(int argc, char *argv[]) Context::New() CreateEnvironment() pump->Run(); Load Node.js Event loop Create context Program entrance content::xxxxx(…) Init Chromium Context::New() CreateEnvironment()

Slide 17

Slide 17 text

Step 3. Entering event loop

Slide 18

Slide 18 text

Event Loop of Node.js timers, idle IO poll deal with IO events uv_check uv_prepare Main Thread process.nextTick produced events setImmediate produced events

Slide 19

Slide 19 text

Event Loop of Native GUI Programs Main Thread [NSApp run] WaitMessage() Main Thread Windows Mac OS X

Slide 20

Slide 20 text

2 event loops in 1 main thread

Slide 21

Slide 21 text

… Main Thread … … … … … main(int argc, char *argv[]) ?????

Slide 22

Slide 22 text

event loop integration https://github.com/atom/atom-shell/blob/master/atom/common/ node_bindings.cc

Slide 23

Slide 23 text

Event Loop of Atom Shell Main Thread Windows WaitMessage() New Thread IO poll 提 ⽰示主 线 程 有 Node.js事 件 timers, idle IO poll once deal with IO events uv_check uv_prepare

Slide 24

Slide 24 text

Questions?