From 582d10d5e160afeac11b2ba5a5ba1698e4935cef Mon Sep 17 00:00:00 2001 From: kaizhangNV <149626564+kaizhangNV@users.noreply.github.com> Date: Thu, 31 Oct 2024 14:29:11 -0500 Subject: 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 --- source/slang-wasm/slang-wasm-bindings.cpp | 11 ++++++-- source/slang-wasm/slang-wasm.cpp | 42 +++++++++++++++++++++++++++++++ source/slang-wasm/slang-wasm.h | 11 ++++++++ source/slang/slang-check-decl.cpp | 2 +- 4 files changed, 63 insertions(+), 3 deletions(-) (limited to 'source') diff --git a/source/slang-wasm/slang-wasm-bindings.cpp b/source/slang-wasm/slang-wasm-bindings.cpp index 151211a67..326ef9ff2 100644 --- a/source/slang-wasm/slang-wasm-bindings.cpp +++ b/source/slang-wasm/slang-wasm-bindings.cpp @@ -42,7 +42,11 @@ EMSCRIPTEN_BINDINGS(slang) .function("getEntryPointCode", &slang::wgsl::ComponentType::getEntryPointCode) .function("getEntryPointCodeBlob", &slang::wgsl::ComponentType::getEntryPointCodeBlob) .function("getTargetCodeBlob", &slang::wgsl::ComponentType::getTargetCodeBlob) - .function("getTargetCode", &slang::wgsl::ComponentType::getTargetCode); + .function("getTargetCode", &slang::wgsl::ComponentType::getTargetCode) + .function( + "loadStrings", + &slang::wgsl::ComponentType::loadStrings, + return_value_policy::take_ownership()); class_>("Module") .function( @@ -214,4 +218,7 @@ EMSCRIPTEN_BINDINGS(slang) "createLanguageServer", &slang::wgsl::lsp::createLanguageServer, return_value_policy::take_ownership()); -} + + class_("HashedString") + .function("getString", &slang::wgsl::HashedString::getString); +}; 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) diff --git a/source/slang-wasm/slang-wasm.h b/source/slang-wasm/slang-wasm.h index ea45d516b..c82976659 100644 --- a/source/slang-wasm/slang-wasm.h +++ b/source/slang-wasm/slang-wasm.h @@ -38,6 +38,16 @@ private: std::unordered_map m_compileTargetMap; }; +class HashedString +{ +public: + std::string getString(uint32_t hash) { return m_hashedStrings[(int)hash]; } + void insertString(int hash, const std::string& str) { m_hashedStrings[hash] = str; } + +private: + std::unordered_map m_hashedStrings; +}; + CompileTargets* getCompileTargets(); class ComponentType @@ -57,6 +67,7 @@ public: slang::IComponentType* interface() const { return m_interface; } + HashedString* loadStrings(); virtual ~ComponentType() = default; private: diff --git a/source/slang/slang-check-decl.cpp b/source/slang/slang-check-decl.cpp index 5b3f692be..ba91bbefa 100644 --- a/source/slang/slang-check-decl.cpp +++ b/source/slang/slang-check-decl.cpp @@ -12168,7 +12168,7 @@ void SemanticsDeclCapabilityVisitor::visitInheritanceDecl(InheritanceDecl* inher DeclVisibility getDeclVisibility(Decl* decl) { - if (as(decl) || as(decl) || + if (as(decl) || as(decl) || as(decl)) { auto genericDecl = as(decl->parentDecl); -- cgit v1.2.3