summaryrefslogtreecommitdiffstats
path: root/source
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
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')
-rw-r--r--source/slang-wasm/slang-wasm-bindings.cpp11
-rw-r--r--source/slang-wasm/slang-wasm.cpp42
-rw-r--r--source/slang-wasm/slang-wasm.h11
-rw-r--r--source/slang/slang-check-decl.cpp2
4 files changed, 63 insertions, 3 deletions
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_<slang::wgsl::Module, base<slang::wgsl::ComponentType>>("Module")
.function(
@@ -214,4 +218,7 @@ EMSCRIPTEN_BINDINGS(slang)
"createLanguageServer",
&slang::wgsl::lsp::createLanguageServer,
return_value_policy::take_ownership());
-}
+
+ class_<slang::wgsl::HashedString>("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<std::string, SlangCompileTarget> 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<int, std::string> 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<GenericTypeParamDecl>(decl) || as<GenericValueParamDecl>(decl) ||
+ if (as<GenericTypeParamDeclBase>(decl) || as<GenericValueParamDecl>(decl) ||
as<GenericTypeConstraintDecl>(decl))
{
auto genericDecl = as<GenericDecl>(decl->parentDecl);