From 181fd1f3c9c4b047c1947096e7b3f8e5bc2314c3 Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Tue, 18 Apr 2023 12:36:06 -0400 Subject: On demand SourceMap JSON serialization (#2811) * #include an absolute path didn't work - because paths were taken to always be relative. * Moved JSON source map writing logic to JSONSourceMapUtil. * Use ArtifactHandler to read/write SourceMaps. Use ObjectCastableAdapter to hold SourceMap Only serialize SourceMap <-> JSON on demand. --- .../compiler-core/slang-artifact-handler-impl.cpp | 49 +++++++++++++++++++++- 1 file changed, 47 insertions(+), 2 deletions(-) (limited to 'source/compiler-core/slang-artifact-handler-impl.cpp') diff --git a/source/compiler-core/slang-artifact-handler-impl.cpp b/source/compiler-core/slang-artifact-handler-impl.cpp index 985aa2337..1a8e8374d 100644 --- a/source/compiler-core/slang-artifact-handler-impl.cpp +++ b/source/compiler-core/slang-artifact-handler-impl.cpp @@ -17,6 +17,10 @@ #include "../core/slang-io.h" #include "../core/slang-shared-library.h" +#include "slang-source-map.h" + +#include "slang-json-source-map-util.h" + namespace Slang { /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!! DefaultArtifactHandler !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ @@ -126,6 +130,25 @@ SlangResult DefaultArtifactHandler::expandChildren(IArtifact* container) return SLANG_OK; } +static SlangResult _loadSourceMap(IArtifact* artifact, ArtifactKeep intermediateKeep, RefPtr& outSourceMap) +{ + const auto desc = artifact->getDesc(); + if (isDerivedFrom(desc.kind, ArtifactKind::Json) && + isDerivedFrom(desc.payload, ArtifactPayload::SourceMap)) + { + ComPtr blob; + SLANG_RETURN_ON_FAIL(artifact->loadBlob(intermediateKeep, blob.writeRef())); + + RefPtr sourceMap; + SLANG_RETURN_ON_FAIL(JSONSourceMapUtil::read(blob, sourceMap)); + + outSourceMap = sourceMap; + return SLANG_OK; + } + + return SLANG_FAIL; +} + SlangResult DefaultArtifactHandler::getOrCreateRepresentation(IArtifact* artifact, const Guid& guid, ArtifactKeep keep, ICastable** outCastable) { const auto reps = artifact->getRepresentations(); @@ -156,8 +179,16 @@ SlangResult DefaultArtifactHandler::getOrCreateRepresentation(IArtifact* artifac } } - // Special case shared library - if (guid == ISlangSharedLibrary::getTypeGuid()) + // Special cases + if (guid == SourceMap::getTypeGuid()) + { + // Blob -> SourceMap + RefPtr sourceMap; + SLANG_RETURN_ON_FAIL(_loadSourceMap(artifact, getIntermediateKeep(keep), sourceMap)); + ComPtr castable(new ObjectCastableAdapter(sourceMap)); + return _addRepresentation(artifact, keep, castable, outCastable); + } + else if (guid == ISlangSharedLibrary::getTypeGuid()) { ComPtr sharedLib; SLANG_RETURN_ON_FAIL(_loadSharedLibrary(artifact, sharedLib.writeRef())); @@ -169,6 +200,20 @@ SlangResult DefaultArtifactHandler::getOrCreateRepresentation(IArtifact* artifac SLANG_RETURN_ON_FAIL(_createOSFile(artifact, getIntermediateKeep(keep), fileRep.writeRef())); return _addRepresentation(artifact, keep, fileRep, outCastable); } + else if (guid == ISlangBlob::getTypeGuid()) + { + for (ICastable* rep : reps) + { + if (SourceMap* sourceMap = as(rep)) + { + // SourceMap -> Blob + ComPtr blob; + SLANG_RETURN_ON_FAIL(JSONSourceMapUtil::write(sourceMap, blob)); + // Add the rep + return _addRepresentation(artifact, keep, blob, outCastable); + } + } + } return SLANG_E_NOT_AVAILABLE; } -- cgit v1.2.3