]> git.gir.st - VimFx.git/blob - extension/bootstrap.coffee
Merge pull request #512 from akhodakivskiy/multi-process
[VimFx.git] / extension / bootstrap.coffee
1 ###
2 # Copyright Anton Khodakivskiy 2012, 2013, 2014.
3 # Copyright Simon Lydell 2013, 2014.
4 #
5 # This file is part of VimFx.
6 #
7 # VimFx is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # VimFx is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with VimFx. If not, see <http://www.gnu.org/licenses/>.
19 ###
20
21 # This file boots the main VimFx process, as well as each frame script. It tries
22 # to do the minimum amount of things to run main.coffee, or main-frame.coffee
23 # for frame scripts. It defines a few global variables, and sets up a
24 # Node.js-style `require` module loader.
25
26 # Expose `Components` shortcuts a well as `Services`, `console` and
27 # `IS_FRAME_SCRIPT` in all modules. The `@`s are needed for frame scripts.
28 { classes: @Cc, interfaces: @Ci, utils: @Cu } = Components
29
30 Cu.import('resource://gre/modules/Services.jsm')
31 Cu.import('resource://gre/modules/devtools/Console.jsm')
32
33 @IS_FRAME_SCRIPT = (typeof content != 'undefined')
34
35 do (global = this) ->
36
37 if IS_FRAME_SCRIPT
38 [ global.__SCRIPT_URI_SPEC__ ] = sendSyncMessage('VimFx:tabCreated')
39 return if __SCRIPT_URI_SPEC__ == false
40 global.FRAME_SCRIPT_ENVIRONMENT = global
41
42 shutdownHandlers = []
43
44 createURI = (path, base = null) -> Services.io.newURI(path, null, base)
45 baseURI = createURI(__SCRIPT_URI_SPEC__)
46
47 # Everything up to the first `!` is the absolute path to the .xpi.
48 dirname = (uri) -> uri.match(///^ [^!]+!/ (.+) /[^/]+ $///)[1]
49
50 require = (path, moduleRoot = '.', dir = '.') ->
51 unless path[0] == '.'
52 # Allow `require('module/lib/foo')` in additon to `require('module')`.
53 [ match, name, subPath ] = path.match(///^ ([^/]+) (?: /(.+) )? ///)
54 base = require.data[moduleRoot]?[name] ? moduleRoot
55 dir = "#{ base }/node_modules/#{ name }"
56 main = require.data[dir]?['']
57 path = subPath ? main ? 'index'
58 moduleRoot = dir
59
60 fullPath = createURI("#{ dir }/#{ path }.js", baseURI).spec
61
62 unless require.scopes[fullPath]?
63 module =
64 exports: {}
65 onShutdown: Function::call.bind(Array::push, shutdownHandlers)
66 require.scopes[fullPath] = scope =
67 require: (path) -> require(path, moduleRoot, "./#{ dirname(fullPath) }")
68 module: module
69 exports: module.exports
70 Services.scriptloader.loadSubScript(fullPath, scope, 'UTF-8')
71
72 return require.scopes[fullPath].module.exports
73
74 require.scopes = {}
75 require.data = require('./require-data')
76
77 unless IS_FRAME_SCRIPT
78 # Set default prefs and apply migrations as early as possible.
79 { applyMigrations } = require('./lib/legacy')
80 migrations = require('./lib/migrations')
81 prefs = require('./lib/prefs')
82
83 prefs.default._init()
84 applyMigrations(migrations)
85
86 main = if IS_FRAME_SCRIPT then './lib/main-frame' else './lib/main'
87 global.startup = require(main)
88
89 global.shutdown = ->
90 require('./lib/message-manager').send('shutdown') unless IS_FRAME_SCRIPT
91
92 for shutdownHandler in shutdownHandlers
93 try
94 shutdownHandler()
95 catch error
96 Cu.reportError(error)
97 shutdownHandlers = null
98
99 # Release everything in `require`d modules. This must be done _after_ all
100 # shutdownHandlers, since they use variables in these scopes.
101 for path, scope of require.scopes
102 for name of scope
103 scope[name] = null
104 require.scopes = null
105
106 global.install = ->
107
108 global.uninstall = ->
109
110 if IS_FRAME_SCRIPT
111 require('./lib/message-manager').listenOnce('shutdown', shutdown)
112 startup()
Imprint / Impressum