-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathvtkWrapJavaScriptClass.c
More file actions
262 lines (231 loc) · 7.57 KB
/
Copy pathvtkWrapJavaScriptClass.c
File metadata and controls
262 lines (231 loc) · 7.57 KB
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkWrapJavaScriptClass.c
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkWrapJavaScriptClass.h"
#include "vtkParseData.h"
#include "vtkWrap.h"
#include "vtkWrapJavaScriptConstant.h"
#include "vtkWrapJavaScriptEnum.h"
#include "vtkWrapJavaScriptMethod.h"
#include <string.h>
// NOLINTBEGIN(bugprone-unsafe-functions)
#ifdef NDEBUG
#define DLOG(...)
#else
#define DLOG(...) printf(__VA_ARGS__);
#endif
static int vtkWrapJavaScript_IsSpecialTypeWrappable(const ClassInfo* data);
/* -------------------------------------------------------------------- */
/* For classes that aren't derived from vtkObjectBase, check to see if
* they are wrappable */
int vtkWrapJavaScript_IsSpecialTypeWrappable(const ClassInfo* data)
{
/* wrapping templates is only possible after template instantiation */
if (data->Template)
{
return 0;
}
/* restrict wrapping to classes that have a "vtk" prefix */
if (strncmp(data->Name, "vtk", 3) != 0)
{
return 0;
}
return 1;
}
/* -------------------------------------------------------------------- */
/* get the true superclass */
const char* vtkWrapJavaScript_GetSuperClass(
ClassInfo* data, const HierarchyInfo* hinfo, const char** supermodule)
{
const char* supername = NULL;
const char* module = NULL;
HierarchyEntry* entry;
int i;
/* if there are multiple superclasses, we just need the relevant one */
for (i = 0; i < data->NumberOfSuperClasses; i++)
{
supername = data->SuperClasses[i];
if (vtkWrap_IsClassWrapped(hinfo, supername))
{
if (vtkWrap_IsVTKObjectBaseType(hinfo, data->Name))
{
/* if class derived from vtkObjectBase, then only accept a
superclass that is also a vtkObjectBase */
if (vtkWrap_IsVTKObjectBaseType(hinfo, supername))
{
break;
}
}
else
{
break;
}
}
supername = NULL;
}
if (supermodule)
{
*supermodule = NULL;
if (hinfo && supername)
{
/* get superclass module and check against our own */
entry = vtkParseHierarchy_FindEntry(hinfo, data->Name);
if (entry)
{
module = entry->Module;
}
entry = vtkParseHierarchy_FindEntry(hinfo, supername);
if (entry && (!module || strcmp(entry->Module, module) != 0))
{
*supermodule = entry->Module;
}
}
}
return supername;
}
/* -------------------------------------------------------------------- */
/* Create the docstring for a class, and print it to fp */
void vtkWrapJavaScript_ClassDoc(
FILE* fp, FileInfo* file_info, ClassInfo* data, HierarchyInfo* hinfo, int is_vtkobject)
{
(void)fp;
(void)file_info;
(void)data;
(void)hinfo;
(void)is_vtkobject;
}
/* -------------------------------------------------------------------- */
/* Wrap one class */
int vtkWrapJavaScript_WrapOneClass(FILE* fp, const char* module, const char* classname,
ClassInfo* data, FileInfo* file_info, HierarchyInfo* hinfo, int is_vtkobject)
{
int class_has_new = 0;
/* recursive handling of templated classes */
if (data->Template)
{
// return vtkWrapJavaScript_WrapTemplatedClass()
return 0;
}
/* verify wrappability */
if (!is_vtkobject && !vtkWrapJavaScript_IsSpecialTypeWrappable(data))
{
return 0;
}
/* check for New() function */
for (int i = 0; i < data->NumberOfFunctions; i++)
{
FunctionInfo* func = data->Functions[i];
if (func->IsDeprecated)
{
// skip deprecated member functions.
continue;
}
if (func->Name && !func->IsExcluded && func->Access == VTK_ACCESS_PUBLIC &&
strncmp("New", func->Name, 3) == 0 && func->NumberOfParameters == 0 &&
!vtkWrap_IsInheritedMethod(data, func))
{
class_has_new = 1;
}
}
if (data->IsAbstract)
{
DLOG("%s abstract class is not fully supported.\n", classname);
}
/* create any enum types defined in the class */
vtkWrapJavaScript_GenerateEnumTypes(fp, module, classname, " ", data);
if (is_vtkobject || class_has_new)
{
// add destructor
fprintf(fp,
"template<> void emscripten::internal::raw_destructor<%s>(%s * ptr){ ptr->Delete(); }",
classname, classname);
}
fprintf(fp, "\nEMSCRIPTEN_BINDINGS(%s_class) {", classname);
for (int i = 0; i < data->NumberOfEnums; ++i)
{
if (!data->Enums[i]->IsExcluded && data->Enums[i]->Access == VTK_ACCESS_PUBLIC)
{
EnumInfo* enumInfo = data->Enums[i];
int found = 0;
/* check to make sure there won't be a name conflict between an
enum type and some other class member, it happens specifically
for vtkImplicitBoolean which has a variable and enum type both
with the name OperationType */
for (int j = 0; j < data->NumberOfVariables && !found; j++)
{
found = (strcmp(data->Variables[j]->Name, enumInfo->Name) == 0);
}
if (!found)
{
// Scoped C++/C style enums need to be `using`'d.
fprintf(fp, "\n using %s=%s::%s;", enumInfo->Name, classname, enumInfo->Name);
}
}
}
const char* supermodule = NULL;
const char* supername = vtkWrapJavaScript_GetSuperClass(data, hinfo, &supermodule);
const char* indent = " ";
if (supername == NULL)
{
fprintf(fp, "\n%semscripten::class_<%s>(\"%s\")", indent, classname, classname);
}
else
{
fprintf(fp, "\n%semscripten::class_<%s, emscripten::base<%s>>(\"%s\")", indent, classname,
supername, classname);
}
// no constructors for abstract classes.
if (!data->IsAbstract)
{
if (is_vtkobject && class_has_new)
{
fprintf(fp, "\n%s%s.smart_ptr<vtkSmartPointer<%s>>(\"vtkSmartPointer<%s>\")", indent, indent,
classname, classname);
fprintf(fp, "\n%s%s.constructor(&vtk::MakeAvtkSmartPointer<%s>)", indent, indent, classname);
}
else
{
// check if class has constructors and destructors
int hasPublicConstructor = 0;
int hasPublicDestructor = 0;
for (int i = 0; i < data->NumberOfFunctions; ++i)
{
FunctionInfo* theFunc = data->Functions[i];
if (theFunc->IsDeprecated)
{
// skip deprecated member functions.
continue;
}
if (!theFunc->IsPublic)
{
continue;
}
// TODO: handle constructors with arguments.
hasPublicConstructor |=
(vtkWrap_IsConstructor(data, theFunc) && theFunc->NumberOfParameters == 0);
hasPublicDestructor |= vtkWrap_IsDestructor(data, theFunc);
}
if (hasPublicConstructor && hasPublicDestructor)
{
// use std::shared_ptr
fprintf(fp, "\n%s%s.smart_ptr<std::shared_ptr<%s>>(\"std::shared_ptr<%s>\")", indent,
indent, classname, classname);
fprintf(fp, "\n%s%s.constructor(&std::make_shared<%s>)", indent, indent, classname);
}
}
}
/* now output all the methods which are wrappable */
vtkWrapJavaScript_GenerateMethods(fp, classname, data, file_info, hinfo, indent);
fprintf(fp, ";\n}\n");
/* create any constant types defined in the class */
vtkWrapJavaScript_GenerateConstants(fp, module, classname, " ", data);
return 1;
}
// NOLINTEND(bugprone-unsafe-functions)