]> git.gir.st - VimFx.git/blob - extension/bootstrap.coffee
VimFx v0.27.1
[VimFx.git] / extension / bootstrap.coffee
1 # This file boots the main VimFx process, as well as each frame script. It tries
2 # to do the minimum amount of things to run main.coffee, or main-frame.coffee
3 # for frame scripts. It defines a few global variables, and sets up a
4 # Node.js-style `require` module loader.
5
6 # `bootstrap.js` files of different add-ons do _not_ share scope. However, frame
7 # scripts for the same `<browser>` but from different add-ons _do_ share scope.
8 # In order not to pollute that global scope in frame scripts, everything is done
9 # in an IIFE here, and the `global` variable is handled with care.
10
11 do (global = this) ->
12
13 {classes: Cc, interfaces: Ci, utils: Cu} = Components
14 ADDON_PATH = do -> # @echo ADDON_PATH
15 HOMEPAGE = do -> # @echo HOMEPAGE
16 IS_FRAME_SCRIPT = (typeof content != 'undefined')
17 BUILD_TIME = do -> # @echo BUILD_TIME
18 REQUIRE_DATA = do -> # @echo REQUIRE_DATA
19
20 shutdownHandlers = []
21
22 dirname = (uri) -> uri.split('/')[...-1].join('/') or '.'
23
24 require = (path, moduleRoot = '.', dir = '.') ->
25 unless path[0] == '.'
26 # Allow `require('module/lib/foo')` in additon to `require('module')`.
27 [match, name, subPath] = path.match(///^ ([^/]+) (?: /(.+) )? ///)
28 base = REQUIRE_DATA[moduleRoot]?[name] ? moduleRoot
29 dir = "#{base}/node_modules/#{name}"
30 main = REQUIRE_DATA[dir]?['']
31 path = subPath ? main ? 'index'
32 moduleRoot = dir
33
34 prefix = "#{ADDON_PATH}/content"
35 uri = "#{prefix}/#{dir}/#{path}.js"
36 normalizedUri = Services.io.newURI(uri, null, null).spec
37 currentDir = dirname(".#{normalizedUri[prefix.length..]}")
38
39 unless require.scopes[normalizedUri]?
40 module = {
41 exports: {}
42 onShutdown: (fn) -> shutdownHandlers.push(fn)
43 }
44 require.scopes[normalizedUri] = scope = {
45 require: (path) -> require.call(null, path, moduleRoot, currentDir)
46 module, exports: module.exports
47 Cc, Ci, Cu, Services
48 ADDON_PATH, BUILD_TIME, HOMEPAGE
49 IS_FRAME_SCRIPT
50 FRAME_SCRIPT_ENVIRONMENT: if IS_FRAME_SCRIPT then global else null
51 }
52 Services.scriptloader.loadSubScript(normalizedUri, scope, 'UTF-8')
53
54 return require.scopes[normalizedUri].module.exports
55
56 require.scopes = {}
57
58 startup = (args...) ->
59 main = if IS_FRAME_SCRIPT then './lib/main-frame' else './lib/main'
60 require(main)(args...)
61
62 shutdown = ->
63 for shutdownHandler in shutdownHandlers
64 try
65 shutdownHandler()
66 catch error
67 Cu.reportError(error)
68 shutdownHandlers = []
69
70 # Release everything in `require`d modules. This must be done _after_ all
71 # shutdownHandlers, since they use variables in these scopes.
72 for path, scope of require.scopes
73 for name of scope
74 scope[name] = null
75 require.scopes = {}
76
77 if IS_FRAME_SCRIPT
78 messageManager = require('./lib/message-manager')
79
80 # Tell the main process that this frame script was created, and ask if
81 # anything should be done in this frame.
82 messageManager.send('tabCreated', null, (ok) ->
83 # After dragging a tab from one window to another, `content` might have
84 # been set to `null` by Firefox when this runs. If so, simply return.
85 return unless ok and content?
86
87 startup()
88
89 messageManager.listenOnce('shutdown', shutdown)
90 )
91 else
92 global.startup = startup
93 global.shutdown = shutdown
94 global.install = ->
95 global.uninstall = ->
Imprint / Impressum