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
|
// slang-artifact-handler-impl.cpp
#include "slang-artifact-handler-impl.h"
#include "slang-artifact-impl.h"
#include "slang-artifact-representation-impl.h"
#include "slang-artifact-desc-util.h"
#include "slang-artifact-helper.h"
#include "../core/slang-castable-list-impl.h"
#include "../core/slang-file-system.h"
#include "../core/slang-io.h"
#include "../core/slang-shared-library.h"
namespace Slang {
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!! DefaultArtifactHandler !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
/* static */DefaultArtifactHandler DefaultArtifactHandler::g_singleton;
SlangResult DefaultArtifactHandler::queryInterface(SlangUUID const& uuid, void** outObject)
{
if (auto ptr = getInterface(uuid))
{
addRef();
*outObject = static_cast<IArtifactHandler*>(this);
return SLANG_OK;
}
return SLANG_E_NO_INTERFACE;
}
void* DefaultArtifactHandler::castAs(const Guid& guid)
{
if (auto ptr = getInterface(guid))
{
return ptr;
}
return getObject(guid);
}
void* DefaultArtifactHandler::getInterface(const Guid& uuid)
{
if (uuid == ISlangUnknown::getTypeGuid() ||
uuid == ICastable::getTypeGuid() ||
uuid == IArtifactHandler::getTypeGuid())
{
return static_cast<IArtifactHandler*>(this);
}
return nullptr;
}
void* DefaultArtifactHandler::getObject(const Guid& uuid)
{
SLANG_UNUSED(uuid);
return nullptr;
}
SlangResult DefaultArtifactHandler::_addRepresentation(IArtifact* artifact, ArtifactKeep keep, ISlangUnknown* rep, ICastable** outCastable)
{
SLANG_ASSERT(rep);
// See if it implements ICastable
{
ComPtr<ICastable> castable;
if (SLANG_SUCCEEDED(rep->queryInterface(ICastable::getTypeGuid(), (void**)castable.writeRef())) && castable)
{
return _addRepresentation(artifact, keep, castable, outCastable);
}
}
// We have to wrap
ComPtr<IUnknownCastableAdapter> adapter(new UnknownCastableAdapter(rep));
return _addRepresentation(artifact, keep, adapter, outCastable);
}
SlangResult DefaultArtifactHandler::_addRepresentation(IArtifact* artifact, ArtifactKeep keep, ICastable* castable, ICastable** outCastable)
{
SLANG_ASSERT(castable);
if (canKeep(keep))
{
artifact->addRepresentation(castable);
}
castable->addRef();
*outCastable = castable;
return SLANG_OK;
}
SlangResult DefaultArtifactHandler::expandChildren(IArtifactContainer* container)
{
SlangResult res = container->getExpandChildrenResult();
if (res != SLANG_E_UNINITIALIZED)
{
// It's already expanded
return res;
}
// For the generic container type, we just expand as empty
const auto desc = container->getDesc();
if (desc.kind == ArtifactKind::Container)
{
container->setChildren(nullptr, 0);
return SLANG_OK;
}
// TODO(JS):
// Proper implementation should (for example) be able to expand a Zip file etc.
return SLANG_E_NOT_IMPLEMENTED;
}
SlangResult DefaultArtifactHandler::getOrCreateRepresentation(IArtifact* artifact, const Guid& guid, ArtifactKeep keep, ICastable** outCastable)
{
const auto reps = artifact->getRepresentations();
// See if we already have a rep of this type
for (ICastable* rep : reps)
{
if (rep->castAs(guid))
{
rep->addRef();
*outCastable = rep;
return SLANG_OK;
}
}
// We can ask each representation if they can do the conversion to the type, if they can we just use that
for (ICastable* castable : reps)
{
if (auto rep = as<IArtifactRepresentation>(castable))
{
ComPtr<ICastable> created;
if (SLANG_SUCCEEDED(rep->createRepresentation(guid, created.writeRef())))
{
SLANG_ASSERT(created);
// Add the rep
return _addRepresentation(artifact, keep, created, outCastable);
}
}
}
// Special case shared library
if (guid == ISlangSharedLibrary::getTypeGuid())
{
ComPtr<ISlangSharedLibrary> sharedLib;
SLANG_RETURN_ON_FAIL(_loadSharedLibrary(artifact, keep, sharedLib.writeRef()));
return _addRepresentation(artifact, keep, sharedLib, outCastable);
}
return SLANG_E_NOT_AVAILABLE;
}
static bool _isFileSystemFile(ICastable* castable, void* data)
{
if (auto fileRep = as<IFileArtifactRepresentation>(castable))
{
ISlangMutableFileSystem* fileSystem = (ISlangMutableFileSystem*)data;
if (fileRep->getFileSystem() == fileSystem)
{
return true;
}
}
return false;
}
SlangResult DefaultArtifactHandler::getOrCreateFileRepresentation(IArtifact* artifact, ArtifactKeep keep, ISlangMutableFileSystem* fileSystem, IFileArtifactRepresentation** outFileRep)
{
// See if we already have it
if (auto fileRep = as<IFileArtifactRepresentation>(artifact->findRepresentationWithPredicate(&_isFileSystemFile, fileSystem)))
{
fileRep->addRef();
*outFileRep = fileRep;
return SLANG_OK;
}
auto helper = DefaultArtifactHelper::getSingleton();
// If we are going to access as a file we need to be able to write it, and to do that we need a blob
ComPtr<ISlangBlob> blob;
SLANG_RETURN_ON_FAIL(artifact->loadBlob(getIntermediateKeep(keep), blob.writeRef()));
// Okay we need to store as a temporary. Get a lock file.
ComPtr<IFileArtifactRepresentation> lockFile;
SLANG_RETURN_ON_FAIL(helper->createLockFile(artifact->getName(), fileSystem, lockFile.writeRef()));
// Now we need the appropriate name for this item
ComPtr<ISlangBlob> pathBlob;
SLANG_RETURN_ON_FAIL(helper->calcArtifactPath(artifact->getDesc(), lockFile->getPath(), pathBlob.writeRef()));
const auto path = StringUtil::getString(pathBlob);
// Write the contents
SLANG_RETURN_ON_FAIL(File::writeAllBytes(path, blob->getBufferPointer(), blob->getBufferSize()));
ComPtr<IFileArtifactRepresentation> fileRep;
// TODO(JS): This path comparison is perhaps not perfect, in that it assumes the path is not changed
// in any way. For example an impl of calcArtifactPath that changed slashes or used a canonical path
// might mean the lock file and the rep have the same path.
// As it stands calcArtifactPath impl doesn't do that, but that is perhaps somewhatfragile
// If the paths are identical, we can just use the lock file for the rep
if (UnownedStringSlice(lockFile->getPath()) == path.getUnownedSlice())
{
fileRep.swap(lockFile);
}
else
{
// Create a new rep that references the lock file
fileRep = new FileArtifactRepresentation(IFileArtifactRepresentation::Kind::Owned, path, lockFile, lockFile->getFileSystem());
}
// Create the rep
if (canKeep(keep))
{
artifact->addRepresentation(fileRep);
}
// Return the file
*outFileRep = fileRep.detach();
return SLANG_OK;
}
SlangResult DefaultArtifactHandler::_loadSharedLibrary(IArtifact* artifact, ArtifactKeep keep, ISlangSharedLibrary** outSharedLibrary)
{
// If it is 'shared library' for a CPU like thing, we can try and load it
const auto desc = artifact->getDesc();
if ((isDerivedFrom(desc.kind, ArtifactKind::HostCallable) ||
isDerivedFrom(desc.kind, ArtifactKind::SharedLibrary)) &&
isDerivedFrom(desc.payload, ArtifactPayload::CPULike))
{
// Get as a file represenation on the OS file system
ComPtr<IFileArtifactRepresentation> fileRep;
SLANG_RETURN_ON_FAIL(artifact->requireFile(ArtifactKeep::No, nullptr, fileRep.writeRef()));
// We requested on the OS file system, just check that's what we got...
SLANG_ASSERT(fileRep->getFileSystem() == nullptr);
// Try loading the shared library
SharedLibrary::Handle handle;
if (SLANG_FAILED(SharedLibrary::loadWithPlatformPath(fileRep->getPath(), handle)))
{
return SLANG_FAIL;
}
// The ScopeSharedLibrary will keep the fileRep in scope as long as is needed
auto sharedLibrary = new ScopeSharedLibrary(handle, fileRep);
if (canKeep(keep))
{
// We want to keep the fileRep, as that is necessary for the sharedLibrary to even work
artifact->addRepresentation(fileRep);
// Keep the shared library
artifact->addRepresentation(sharedLibrary);
}
// Output
sharedLibrary->addRef();
*outSharedLibrary = sharedLibrary;
return SLANG_OK;
}
return SLANG_FAIL;
}
} // namespace Slang
|