1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
GLib = imports.gi.GLib;
(function() {
/* Variant Sugar
* Provide commodities methods to convert Variant from/to javascript object.
* variant.toJS() : returns a javascript object representing the Variant
# @variant
* Variant.new(signature, value): returns a GVariant structure representing
* the javascript object @value
*
* This implementation uses json-glib's json <-> variant conversion facilities
* if presents and fallback to a javascript implementation otherwise.
* This javascript implementation is imported from gjs with modification for
* JSCore compatibility (Copyright 2011 Giovanni Campagna,
* see gjs/override/GLib.js for licence)
*/
// Use json-glib's json <-> variant conversion if present.
// Defaults to javascript code imported from gjs otherwise.
GLib.Variant.prototype.to_js = function(signature) {
return _toJS(signature, this);
}
var toVariant = _toVariant;
try {
JSONGLib = imports.gi.Json;
}
catch (e) {}
if (JSONGLib && JSONGLib.gvariant_serialize_data)
{
GLib.Variant.prototype.to_js = function(signature) {
return _toJSNative(signature, this);
}
toVariant = _toVariantNative;
}
GLib.Variant.new = function (value, sig) {
var signature = Array.prototype.slice.call(sig);
if (signature.length != 0)
throw new TypeError('Invalid GVariant signature (more than one single complete type)');
var variant = toVariant(signature, value);
return variant;
}
GLib.Variant.prototype.toString = function() {
return '[object variant of type "' + this.get_type_string() + '"]';
};
/// End Variant Sugar ///
}).apply();
function _toVariantNative(signature, object)
{
if (!object || object == '')
return null;
if (!signature || signature == '')
signature = null;
return JSONGLib.gvariant_deserialize_data (JSON.stringify (object),
-1, signature);
};
function _toJSNative(signature, variant)
{
if (!variant)
return;
var jsonStr = JSONGLib.gvariant_serialize_data (variant, signature);
if (!jsonStr)
return;
return JSON.parse (jsonStr);
};
// Code imported from gjs, modified for JSCore idoms.
// Copyright 2011 Giovanni Campagna (see gjs/override/GLib.js for licence)
const SIMPLE_TYPES = ['b', 'y', 'n', 'q', 'i', 'u', 'x', 't', 'h', 'd', 's', 'o', 'g'];
function _read_single_type(signature, forceSimple) {
var char = signature.shift();
var isSimple = false;
if (SIMPLE_TYPES.indexOf(char) == -1) {
if (forceSimple)
throw new TypeError('Invalid GVariant signature (a simple type was expected)');
} else
isSimple = true;
if (char == 'm' || char == 'a')
return [char].concat(_read_single_type(signature, false));
if (char == '{') {
var key = _read_single_type(signature, true);
var val = _read_single_type(signature, false);
var close = signature.shift();
if (close != '}')
throw new TypeError('Invalid GVariant signature for type DICT_ENTRY (expected "}"');
return [char].concat(key, val, close);
}
if (char == '(') {
var res = [char];
while (true) {
if (signature.length == 0)
throw new TypeError('Invalid GVariant signature for type TUPLE (expected ")")');
var next = signature[0];
if (next == ')') {
signature.shift();
return res.concat(next);
}
var el = _read_single_type(signature);
res = res.concat(el);
}
}
// Valid types are simple types, arrays, maybes, tuples, dictionary entries and variants
if (!isSimple && char != 'v')
throw new TypeError('Invalid GVariant signature (' + char + ' is not a valid type)');
return [char];
}
function _toVariant(signature, value) {
if (signature.length == 0)
throw new TypeError('GVariant signature cannot be empty');
var char = signature.shift();
switch (char) {
case 'b':
return GLib.Variant.new_boolean(value);
case 'y':
return GLib.Variant.new_byte(value);
case 'n':
return GLib.Variant.new_int16(value);
case 'q':
return GLib.Variant.new_uint16(value);
case 'i':
return GLib.Variant.new_int32(value);
case 'u':
return GLib.Variant.new_uint32(value);
case 'x':
return GLib.Variant.new_int64(value);
case 't':
return GLib.Variant.new_uint64(value);
case 'h':
return GLib.Variant.new_handle(value);
case 'd':
return GLib.Variant.new_double(value);
case 's':
return GLib.Variant.new_string(value);
case 'o':
return GLib.Variant.new_object_path(value);
case 'g':
return GLib.Variant.new_signature(value);
case 'v':
return GLib.Variant.new_variant(value);
case 'm':
if (value != null)
return GLib.Variant.new_maybe(null, _pack_variant(signature, value))
else
return GLib.Variant.new_maybe(GLib.VariantType.new(_read_single_type(signature, false).join('')), null);
case 'a':
var arrayType = _read_single_type(signature, false);
if (arrayType[0] == 's') {
// special case for array of strings
return GLib.Variant.new_strv(value, value.length);
}
if (arrayType[0] == 'y') {
// special case for array of bytes
return GLib.Variant.new_bytestring(value);
}
if (arrayType[0] == 'a' && arrayType[1] == 'y') {
// special case for array of array of bytes
return GLib.Variant.new_bytestring_array(value, value.length);
}
var arrayValue = [];
if (arrayType[0] == '{') {
// special case for dictionaries
for (var key in value) {
var copy = [].concat(arrayType);
var child = _pack_variant(copy, [key, value[key]]);
arrayValue.push(child);
}
} else {
for (var i = 0; i < value.length; i++) {
var copy = [].concat(arrayType);
var child = _pack_variant(copy, value[i]);
arrayValue.push(child);
}
}
return GLib.Variant.new_array(GLib.VariantType.new(arrayType.join('')), arrayValue, arrayValue.length);
case '(':
var children = [ ];
for (var i = 0; i < value.length; i++) {
var next = signature[0];
if (next == ')')
break;
children.push(_pack_variant(signature, value[i]));
}
if (signature[0] != ')')
throw new TypeError('Invalid GVariant signature for type TUPLE (expected ")")');
signature.shift();
return GLib.Variant.new_tuple(children, children.length);
case '{':
var key = _pack_variant(signature, value[0]);
var child = _pack_variant(signature, value[1]);
if (signature[0] != '}')
throw new TypeError('Invalid GVariant signature for type DICT_ENTRY (expected "}")');
signature.shift();
return GLib.Variant.new_dict_entry(key, child);
default:
throw new TypeError('Invalid GVariant signature (unexpected character ' + char + ')');
}
}
function _toJS(signature, variant) {
switch (String.fromCharCode(variant.classify())) {
case 'b':
return variant.get_boolean();
case 'y':
return variant.get_byte();
case 'n':
return variant.get_int16();
case 'q':
return variant.get_uint16();
case 'i':
return variant.get_int32();
case 'u':
return variant.get_uint32();
case 'x':
return variant.get_int64();
case 't':
return variant.get_uint64();
case 'h':
return variant.get_handle();
case 'd':
return variant.get_double();
case 'o':
case 'g':
case 's':
// g_variant_get_string has length as out argument
return variant.get_string();
case 'v':
return variant.get_variant();
case 'm':
var val = variant.get_maybe();
return _toJS(val);
case 'a':
if (variant.is_container()) {
// special case containers
var ret = { };
var nElements = variant.n_children();
for (var i = 0; i < nElements; i++) {
// always unpack the dictionary entry, and always unpack
// the key (or it cannot be added as a key)
var val = _toJS(variant.get_child_value(i));
var key = val[0].classify ? _toJS(val[0]) : val[0];
ret[key] = val[1] && val[1].classify ? _toJS(val[1]) : val[1]
}
return ret;
}
// fall through
case '(':
case '{':
var ret = [ ];
var nElements = variant.n_children();
for (var i = 0; i < nElements; i++) {
var val = variant.get_child_value(i);
ret.push(_toJS(val));
}
return ret;
}
throw new Error('Assertion failure: this code should not be reached');
}
|