diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2021-10-04 14:15:51 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-04 14:15:51 -0400 |
| commit | 97bb82ebcdf8f1391b9d93b5a8d7b1dfc4e88e52 (patch) | |
| tree | f120ba282cbea96d23ed179737984a4610d3b520 /source/slang | |
| parent | b3dfe383c6d31ff3dbd76dcfb32de8d536382f3e (diff) | |
Removing exceptions from core/compiler-core (#1953)
* #include an absolute path didn't work - because paths were taken to always be relative.
* Refactor Stream. Working on all tests.
* Split out CharEncode.
* Make method names lower camel.
m_prefix in Writer/Reader
* Tidy up around CharEncode interface.
* Small improvements around encode/decode.
* Better use of types.
* Remove readLine from TextReader.
* Remove exceptions from Stream/Text handling.
* Fix some typos.
* Fix tabbing.
* Fix missing override.
* Remove remaining exception throw/catch via using signal mechanism.
* Remove exceptions that are not used anymore.
* Document the Stream interface.
* Remove index for decoding 'get byte' function.
* Fix CharReader -> ByteReader.
Diffstat (limited to 'source/slang')
| -rw-r--r-- | source/slang/slang-api.cpp | 19 | ||||
| -rw-r--r-- | source/slang/slang-compiler.cpp | 13 | ||||
| -rw-r--r-- | source/slang/slang-options.cpp | 3 | ||||
| -rw-r--r-- | source/slang/slang-repro.cpp | 26 | ||||
| -rw-r--r-- | source/slang/slang-serialize-types.cpp | 20 | ||||
| -rw-r--r-- | source/slang/slang-serialize.cpp | 17 | ||||
| -rw-r--r-- | source/slang/slang.cpp | 7 |
7 files changed, 44 insertions, 61 deletions
diff --git a/source/slang/slang-api.cpp b/source/slang/slang-api.cpp index a00d993e3..5ee384e4a 100644 --- a/source/slang/slang-api.cpp +++ b/source/slang/slang-api.cpp @@ -64,18 +64,15 @@ SlangResult trySaveStdLibToCache( Slang::ComPtr<ISlangBlob> stdLibBlobPtr; SLANG_RETURN_ON_FAIL( globalSession->saveStdLib(SLANG_ARCHIVE_TYPE_RIFF_LZ4, stdLibBlobPtr.writeRef())); - try - { - Slang::FileStream fileStream(cacheFilename, Slang::FileMode::Create); - fileStream.write(&dllTimestamp, sizeof(dllTimestamp)); - fileStream.write(stdLibBlobPtr->getBufferPointer(), stdLibBlobPtr->getBufferSize()); - return SLANG_OK; - } - catch (...) - { - } + + Slang::FileStream fileStream; + SLANG_RETURN_ON_FAIL(fileStream.init(cacheFilename, Slang::FileMode::Create)); + + SLANG_RETURN_ON_FAIL(fileStream.write(&dllTimestamp, sizeof(dllTimestamp))); + SLANG_RETURN_ON_FAIL(fileStream.write(stdLibBlobPtr->getBufferPointer(), stdLibBlobPtr->getBufferSize())) } - return SLANG_FAIL; + + return SLANG_OK; } SLANG_API SlangResult slang_createGlobalSession( diff --git a/source/slang/slang-compiler.cpp b/source/slang/slang-compiler.cpp index c6c41b099..a73289852 100644 --- a/source/slang/slang-compiler.cpp +++ b/source/slang/slang-compiler.cpp @@ -2176,16 +2176,9 @@ namespace Slang return SLANG_OK; } - FileStream stream(fileName, FileMode::Create, FileAccess::Write, FileShare::ReadWrite); - try - { - stream.write(m_containerBlob->getBufferPointer(), m_containerBlob->getBufferSize()); - } - catch (const IOException&) - { - // Unable to write - return SLANG_FAIL; - } + FileStream stream; + SLANG_RETURN_ON_FAIL(stream.init(fileName, FileMode::Create, FileAccess::Write, FileShare::ReadWrite)); + SLANG_RETURN_ON_FAIL(stream.write(m_containerBlob->getBufferPointer(), m_containerBlob->getBufferSize())); return SLANG_OK; } diff --git a/source/slang/slang-options.cpp b/source/slang/slang-options.cpp index 90c5a3896..1c7d8fbc2 100644 --- a/source/slang/slang-options.cpp +++ b/source/slang/slang-options.cpp @@ -1151,7 +1151,8 @@ struct OptionsParser SLANG_RETURN_ON_FAIL(reader.expectArg(referenceModuleName)); // We need to deserialize and add the modules - FileStream fileStream(referenceModuleName.value, FileMode::Open, FileAccess::Read, FileShare::ReadWrite); + FileStream fileStream; + SLANG_RETURN_ON_FAIL(fileStream.init(referenceModuleName.value, FileMode::Open, FileAccess::Read, FileShare::ReadWrite)); // TODO: probably near an error when we can't open the file? diff --git a/source/slang/slang-repro.cpp b/source/slang/slang-repro.cpp index 4b3148278..67feebe43 100644 --- a/source/slang/slang-repro.cpp +++ b/source/slang/slang-repro.cpp @@ -1082,22 +1082,15 @@ struct LoadContext /* static */SlangResult ReproUtil::saveState(EndToEndCompileRequest* request, const String& filename) { - RefPtr<Stream> stream(new FileStream(filename, FileMode::Create, FileAccess::Write, FileShare::ReadWrite)); + RefPtr<FileStream> stream(new FileStream); + SLANG_RETURN_ON_FAIL(stream->init(filename, FileMode::Create, FileAccess::Write, FileShare::ReadWrite)); return saveState(request, stream); } /* static */ SlangResult ReproUtil::loadState(const String& filename, List<uint8_t>& outBuffer) { - RefPtr<Stream> stream; - try - { - stream = new FileStream(filename, FileMode::Open, FileAccess::Read, FileShare::ReadWrite); - } - catch (const IOException&) - { - return SLANG_FAIL; - } - + RefPtr<FileStream> stream = new FileStream; + SLANG_RETURN_ON_FAIL(stream->init(filename, FileMode::Open, FileAccess::Read, FileShare::ReadWrite)); return loadState(stream, outBuffer); } @@ -1564,6 +1557,8 @@ static SlangResult _findFirstSourcePath(EndToEndCompileRequest* request, String& String sourceFileName = Path::getFileName(sourcePath); String sourceBaseName = Path::getFileNameWithoutExt(sourceFileName); + RefPtr<FileStream> stream = new FileStream; + // Okay we need a unique number to make sure the name is unique const int maxTries = 100; for (int triesCount = 0; triesCount < maxTries; ++triesCount) @@ -1578,15 +1573,12 @@ static SlangResult _findFirstSourcePath(EndToEndCompileRequest* request, String& outFileName = builder; // We could have clashes, as we use ticks, we should get to a point where the clashes stop - try + if (SLANG_SUCCEEDED(stream->init(builder, FileMode::CreateNew, FileAccess::Write, FileShare::WriteOnly))) { - outStream = new FileStream(builder, FileMode::CreateNew, FileAccess::Write, FileShare::WriteOnly); + outStream = stream; return SLANG_OK; } - catch (const IOException&) - { - } - + // TODO(JS): // Might make sense to sleep here - but don't seem to have cross platform func for that yet. } diff --git a/source/slang/slang-serialize-types.cpp b/source/slang/slang-serialize-types.cpp index fc839afc1..6c4512b1d 100644 --- a/source/slang/slang-serialize-types.cpp +++ b/source/slang/slang-serialize-types.cpp @@ -14,10 +14,10 @@ namespace Slang { namespace { // anonymous -struct CharReader +struct ByteReader { - char operator()(int pos) const { SLANG_UNUSED(pos); return *m_pos++; } - CharReader(const char* pos) :m_pos(pos) {} + Byte operator()() const { return Byte(*m_pos++); } + ByteReader(const char* pos) :m_pos(pos) {} mutable const char* m_pos; }; @@ -37,11 +37,15 @@ struct CharReader stringTable.clear(); for (const auto& slice : slices) { + // TODO(JS): + // This is a bit of a hack. We need to store the string length, along with the string contents. We don't want to write + // the size as (say) uint32, because most strings are short. So we just save off the length as a utf8 encoding. + // As it stands this *does* have an arguable problem because encoding isn't of the full 32 bits. const int len = int(slice.getLength()); // We need to write into the the string array char prefixBytes[6]; - const int numPrefixBytes = EncodeUnicodePointToUTF8(prefixBytes, len); + const int numPrefixBytes = encodeUnicodePointToUTF8(len, prefixBytes); const Index baseIndex = stringTable.getCount(); stringTable.setCount(baseIndex + numPrefixBytes + len); @@ -61,8 +65,8 @@ struct CharReader while (cur < end) { - CharReader reader(cur); - const int len = GetUnicodePointFromUTF8(reader); + ByteReader reader(cur); + const int len = getUnicodePointFromUTF8(reader); slicesOut.add(UnownedStringSlice(reader.m_pos, len)); cur = reader.m_pos + len; } @@ -87,8 +91,8 @@ struct CharReader while (cur < end) { - CharReader reader(cur); - const int len = GetUnicodePointFromUTF8(reader); + ByteReader reader(cur); + const int len = getUnicodePointFromUTF8(reader); outPool.add(UnownedStringSlice(reader.m_pos, len)); cur = reader.m_pos + len; } diff --git a/source/slang/slang-serialize.cpp b/source/slang/slang-serialize.cpp index 4f8fdc546..a7074df1c 100644 --- a/source/slang/slang-serialize.cpp +++ b/source/slang/slang-serialize.cpp @@ -506,20 +506,13 @@ SlangResult SerialWriter::write(Stream* stream) // MAX_ALIGNMENT, and so < MAX_ALIGNMENT is the most extra bytes we can write SLANG_ASSERT( alignmentFixSize < SerialInfo::MAX_ALIGNMENT); - try + SLANG_RETURN_ON_FAIL(stream->write(entry, entrySize)); + // If we needed to fix so that subsequent alignment is right, write out extra bytes here + if (alignmentFixSize) { - stream->write(entry, entrySize); - // If we needed to fix so that subsequent alignment is right, write out extra bytes here - if (alignmentFixSize) - { - stream->write(s_fixBuffer, alignmentFixSize); - } + SLANG_RETURN_ON_FAIL(stream->write(s_fixBuffer, alignmentFixSize)); } - catch (const IOException&) - { - return SLANG_FAIL; - } - + // Onto next offset = nextOffset; entry = next; diff --git a/source/slang/slang.cpp b/source/slang/slang.cpp index 0fc5f6122..6fe778435 100644 --- a/source/slang/slang.cpp +++ b/source/slang/slang.cpp @@ -269,11 +269,14 @@ SlangResult Session::compileStdLib(slang::CompileStdLibFlags compileFlags) { String fileName("stdlib-doc.md"); - StreamWriter writer(new FileStream(fileName, FileMode::Create)); + RefPtr<FileStream> stream = new FileStream; + SLANG_RETURN_ON_FAIL(stream->init(fileName, FileMode::Create)); + StreamWriter writer; + SLANG_RETURN_ON_FAIL(writer.init(stream)); for (auto& docString : docStrings) { - writer.Write(docString); + SLANG_RETURN_ON_FAIL(writer.write(docString)); } } } |
