summaryrefslogtreecommitdiff
path: root/source/slang-wasm/slang-wasm.cpp
diff options
context:
space:
mode:
authorkaizhangNV <149626564+kaizhangNV@users.noreply.github.com>2024-10-31 14:29:11 -0500
committerGitHub <noreply@github.com>2024-10-31 12:29:11 -0700
commit582d10d5e160afeac11b2ba5a5ba1698e4935cef (patch)
tree332476ddfa2d0978307a40248acee3084c2a3584 /source/slang-wasm/slang-wasm.cpp
parent0f68de95b68510806598b17cba3b63cda429e199 (diff)
Add function of getting hash string to wasm binding (#5468)
* [wasm]: Add function to get string from hash * Fix bug on the visibility issue * Formatting
Diffstat (limited to 'source/slang-wasm/slang-wasm.cpp')
-rw-r--r--source/slang-wasm/slang-wasm.cpp42
1 files changed, 42 insertions, 0 deletions
diff --git a/source/slang-wasm/slang-wasm.cpp b/source/slang-wasm/slang-wasm.cpp
index f642c3ed6..be30f8394 100644
--- a/source/slang-wasm/slang-wasm.cpp
+++ b/source/slang-wasm/slang-wasm.cpp
@@ -344,6 +344,48 @@ emscripten::val ComponentType::getTargetCodeBlob(int targetIndex)
return emscripten::val(emscripten::typed_memory_view(kernelBlob->getBufferSize(), ptr));
}
+HashedString* ComponentType::loadStrings()
+{
+ slang::ProgramLayout* slangReflection = interface()->getLayout();
+ if (!slangReflection)
+ {
+ g_error.type = std::string("USER");
+ g_error.message = std::string("Failed to get reflection data");
+ return nullptr;
+ }
+
+ SlangUInt hashedStringCount = slangReflection->getHashedStringCount();
+ if (hashedStringCount == 0)
+ {
+ g_error.type = std::string("USER");
+ g_error.message = std::string("Warn: No reflection data found");
+ return nullptr;
+ }
+
+ size_t stringSize = 0;
+ HashedString* hashedStrings = new HashedString();
+ for (SlangUInt ii = 0; ii < hashedStringCount; ++ii)
+ {
+ // For each string we can fetch its bytes from the Slang
+ // reflection data.
+ //
+ size_t stringSize = 0;
+ char const* stringData = slangReflection->getHashedString(ii, &stringSize);
+
+ // Then we can compute the hash code for that string using
+ // another Slang API function.
+ //
+ // Note: the exact hashing algorithm that Slang uses for
+ // string literals is not currently documented, and may
+ // change in future releases of the compiler.
+ //
+ int hash = spComputeStringHash(stringData, stringSize);
+
+ hashedStrings->insertString(hash, std::string(stringData));
+ }
+ return hashedStrings;
+}
+
namespace lsp
{
Position translate(Slang::LanguageServerProtocol::Position p)