/
importer.vala
114 lines (93 loc) · 3.63 KB
/
importer.vala
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
using namespace v8;
using namespace GI;
public class Importer {
private Importer() {}
public static init(ObjectTemplate global) {
HandleScope hs = new HandleScope();
imports = new Persistent(new ObjectTemplate(), HandleType.OBJECT_TEMPLATE);
NamedPropertyData imports_npd = { Importer.imports_get_property, null };
imports.set_named_property_handler(imports_npd);
imports_gi = new Persistent(new ObjectTemplate(), HandleType.OBJECT_TEMPLATE);
NamedPropertyData imports_gi_npd = { Importer.imports_gi_get_property, null }
imports_gi.set_named_property_handler(imports_gi_npd);
imports_gi_versions = new Persistent(new v8.Object(), HandleType.OBJECT);
gi_hash = new HashTable<string, Handle>();
global.set(new String("imports", -1), imports.new_instance());
}
private static Handle imports_get_property(Handle property, AccessorInfo info) {
String.Utf8Value prop_str = new String.Utf8Value(property);
if (!strcmp(prop_str.chars(), "gi"))
return imports_gi;
if (!strcmp(prop_str.chars(), "searchPath"))
return NULL;
if (!strcmp(prop_str.chars(), "toString"))
return NULL;
}
private static Handle imports_gi_get_property(Handle property, AccessorInfo info) {
String.Utf8Value prop_str = new String.Utf8Value(property);
if (!strcmp(prop_str.chars(), "versions"))
return imports_gi_versions;
if (!strcmp(prop_str.chars(), "toString"))
return NULL;
if (!strcmp(prop_str.chars(), "valueOf")
return NULL;
Value ret;
TryCatch tc = new TryCatch();
ret = imports_gi_do_namespace(prop_str.chars());
/*if (ret.is_empty()) {
String.Utf8Value msg = new String.Utf8Value(tc.exception());
stderr.printf("Unable to import namespace: %s\n", msg.chars());
}
*/
return ret;
}
private static Value imports_gi_do_namespace(unowned string namespace) {
v8.Object namespace_obj;
HandleScope hs = new HandleScope();
if (namespace_obj = gi_hash.lookup(namespace)) {
/* TODO: log */
return namespace_obj;
}
String version = imports_gi_get_version(namespace);
String.Utf8Value version_v = new String.Utf8Value(version);
try {
Repository.require(null, namespace, version, 0);
uint n = Repository.get_n_infos(null, namespace);
BaseInfo info;
for (int i = 0; i < n; i++) {
InfoType info_type;
info = Repository.get_info(null, namespace, i);
info_type = info.get_type();
/*switch (info_type) {
case InfoType.FUNCTION:
FunctionInfo func_info = (FunctionInfo) info;
stdout.printf("%s\n", func_info.get_symbol());
break;
case InfoType.OBJECT:
ObjectInfo obj_info = (ObjectInfo) info;
stdout.printf("%s\n", obj_info.get_type_name());
break;
case InfoType.ENUM:
case InfoType.FLAGS:
EnumInfo enum_info = (EnumInfo) info;
default:
break;
}*/
stdout.printf("%s\n", info.get_name());
}
} catch (Error e) {
stderr.printf("Error: %s\n", e.message);
return v8.undefined();
}
return v8.null();
}
private static String imports_gi_get_version(unowned string namespace) {
HandleScope hs = new HandleScope();
Value ret = imports_gi_versions.get_with_key(new String(namespace, -1));
return hs.close(ret, HandleType.VALUE);
}
private static ObjectTemplate imports;
private static ObjectTemplate imports_gi;
private static v8.Object imports_gi_versions;
private static HashTable<string, Handle> gi_hash;
}