1------------------------------------------------------------------------------
2--
3--  LGI Lua-side core.
4--
5--  Copyright (c) 2010, 2011 Pavel Holejsovsky
6--  Licensed under the MIT license:
7--  http://www.opensource.org/licenses/mit-license.php
8--
9------------------------------------------------------------------------------
10
11local assert, require, pcall, setmetatable, pairs, type, error, tostring,
12_VERSION, jit
13   = assert, require, pcall, setmetatable, pairs, type, error, tostring,
14_VERSION, rawget(_G, 'jit')
15
16local package = require 'package'
17
18-- Require core lgi utilities, used during bootstrap.
19local core = require 'lgi.core'
20
21-- Create lgi table, containing the module.
22local lgi = { _NAME = 'lgi', _VERSION = require 'lgi.version' }
23
24-- Forward selected core methods into external interface.
25for _, name in pairs { 'yield', 'lock', 'enter', 'leave' } do
26   lgi[name] = core[name]
27end
28
29-- If global package 'bytes' does not exist (i.e. not provided
30-- externally), use our internal (although incomplete) implementation.
31local ok, bytes = pcall(require, 'bytes')
32if not ok or not bytes then
33   package.loaded.bytes = core.bytes
34end
35
36-- Prepare logging support.  'log' is module-exported table, containing all
37-- functionality related to logging wrapped around GLib g_log facility.
38lgi.log = require 'lgi.log'
39
40-- For the rest of bootstrap, prepare logging to lgi domain.
41local log = lgi.log.domain('lgi')
42
43-- Repository, table with all loaded namespaces.  Its metatable takes care of
44-- loading on-demand.  Created by C-side bootstrap.
45local repo = core.repo
46
47local namespace = require 'lgi.namespace'
48lgi.require = namespace.require
49
50-- Install 'lgi.package' method.
51lgi.package = require('lgi.package').ensure
52
53-- Add assert override, which accepts not only text message but any
54-- kind of error object.
55function lgi.assert(cond, ...)
56   if cond then return cond, ... end
57   local err = ...
58   if _VERSION == 'Lua 5.1' and not jit then
59      -- Lua 5.1 does not support displaying message of non-string
60      -- errors on the commandline interpreter, so better stringize
61      -- the error message right now.
62      err = tostring(err)
63   end
64   error(err, 2)
65end
66
67-- Install metatable into repo table, so that on-demand loading works.
68setmetatable(repo, { __index = function(_, name)
69				  return lgi.require(name)
70			       end })
71
72-- Create lazy-loading components for base gobject entities.
73assert(core.gi.require ('GLib', '2.0'))
74assert(core.gi.require ('GObject', '2.0'))
75repo.GObject._precondition = {}
76for _, name in pairs { 'Type', 'Value', 'Closure', 'Object' } do
77   repo.GObject._precondition[name] = 'GObject-' .. name
78end
79repo.GObject._precondition.InitiallyUnowned = 'GObject-Object'
80
81-- Create lazy-loading components for GLib primitives.
82repo.GLib._precondition = {}
83repo.GLib._precondition.Error = 'GLib-Error'
84repo.GLib._precondition.Bytes = 'GLib-Bytes'
85repo.GLib._precondition.Timer = 'GLib-Timer'
86repo.GLib._precondition.MarkupParser = 'GLib-Markup'
87repo.GLib._precondition.MarkupParseContext = 'GLib-Markup'
88repo.GLib._precondition.Source = 'GLib-Source'
89repo.GLib._precondition.SourceFuncs = 'GLib-Source'
90for _, name in pairs { 'Variant', 'VariantType', 'VariantBuilder' } do
91   repo.GLib._precondition[name] = 'GLib-Variant'
92end
93
94-- Access to module proxies the whole repo, so that lgi.'namespace'
95-- notation works.
96return setmetatable(lgi, { __index = repo })
97