summaryrefslogtreecommitdiffstats
path: root/examples/cpu-com-example/main.cpp
diff options
context:
space:
mode:
authorjsmall-nvidia <jsmall@nvidia.com>2022-06-08 10:23:01 -0400
committerGitHub <noreply@github.com>2022-06-08 10:23:01 -0400
commit8e6e884eca5b33218a8cb2714266fb6ed4548d75 (patch)
treea9c8aee79a71450a64e6660da7266b6a45da0264 /examples/cpu-com-example/main.cpp
parent01d0154ae90f5c587321d39b8fd8f82e2764f360 (diff)
Actual global support (#2262)
* #include an absolute path didn't work - because paths were taken to always be relative. * Use TerminatedUnownedStringSlice for literals in output C++. * Remove Escape/Unescape functions used in slang-token-reader.cpp Add target type of 'host-cpp' etc to map to the target types. * Fix some corner cases around string encoding. * Added unit test for string escaping. Fixed some assorted escaping bugs. * Updated test output. * Added decode test. * Stop using hex output, to get around 'greedy' aspect. Use octal instead. * Added HostHostCallable Small changes to use ArtifactDesc/Info instead of large switches. * Fix C++ emit to handle arbitrary function export. * Add options handling for callable without an output being specified. * Can compile with COM interface. Added example using com interface. * Use the IR Ptr type instead of hack in C++ emit for interfaces. * Fix issue with outputting the COM call when ptr is used. * Fix crash issue on compilation failure. * Add support for __global. * Added `ActualGlobalRate` Added special handling around globals and COM interfaces. Tested out in cpu-com-example. * Fix typo in NodeBase. * Support for accessing globals by name working. * Check that actual global initialization is working. * Refactor the com replacement such that it doesn't need a cache or do anything special with GlobalVar. * Remove context. Only create replacement if needed. * Split out COM host-callable into a unit-test. * host-callable com testing on C++and llvm. * Comment around the COM ptr replacement. * Disable com test on vs 32 bit. Fix C++ prelude * Disable 32 bit targets testing com host-callable. * Use JSON parsing to locate VS version. * Need platform detection in C++prelude. * Fix com host callable test for LLVM. * Work around for not being able to include "targetConditionals.h"
Diffstat (limited to 'examples/cpu-com-example/main.cpp')
-rw-r--r--examples/cpu-com-example/main.cpp56
1 files changed, 41 insertions, 15 deletions
diff --git a/examples/cpu-com-example/main.cpp b/examples/cpu-com-example/main.cpp
index 2e9eeed79..dedbf69f5 100644
--- a/examples/cpu-com-example/main.cpp
+++ b/examples/cpu-com-example/main.cpp
@@ -7,7 +7,6 @@
#include <slang-com-ptr.h>
#include <slang-com-helper.h>
-
// This includes a useful small function for setting up the prelude (described more further below).
#include "../../source/core/slang-test-tool-util.h"
@@ -21,8 +20,9 @@ using namespace Slang;
class IDoThings : public ISlangUnknown
{
public:
- virtual int SLANG_MCALL doThing(int a, int b) = 0;
- virtual int SLANG_MCALL calcHash(const char* in) = 0;
+ virtual SLANG_NO_THROW int SLANG_MCALL doThing(int a, int b) = 0;
+ virtual SLANG_NO_THROW int SLANG_MCALL calcHash(const char* in) = 0;
+ virtual SLANG_NO_THROW void SLANG_MCALL printMessage(const char* in) = 0;
};
static int _calcHash(const char* in)
@@ -36,7 +36,7 @@ static int _calcHash(const char* in)
return hash;
}
-class DoThings :public IDoThings
+class DoThings : public IDoThings
{
public:
// We don't need queryInterface for this impl, or ref counting
@@ -45,17 +45,21 @@ public:
virtual SLANG_NO_THROW uint32_t SLANG_MCALL release() SLANG_OVERRIDE { return 1; }
// IDoThings
- virtual int SLANG_MCALL doThing(int a, int b) SLANG_OVERRIDE { return a + b + 1; }
- virtual int SLANG_MCALL calcHash(const char* in) SLANG_OVERRIDE { return (int)_calcHash(in); }
+ virtual SLANG_NO_THROW int SLANG_MCALL doThing(int a, int b) SLANG_OVERRIDE { return a + b + 1; }
+ virtual SLANG_NO_THROW int SLANG_MCALL calcHash(const char* in) SLANG_OVERRIDE { return (int)_calcHash(in); }
+ virtual SLANG_NO_THROW void SLANG_MCALL printMessage(const char* in) SLANG_OVERRIDE { printf("%s\n", in); }
};
static SlangResult _innerMain(int argc, char** argv)
{
+ // NOTE! This example only works if `slang-llvm` or a C++ compiler that Slang supports is available.
+
// Create the session
ComPtr<slang::IGlobalSession> slangSession;
slangSession.attach(spCreateSession(NULL));
// Set up the prelude
+ // NOTE: This isn't strictly necessary, as preludes are embedded in the binary.
TestToolUtil::setSessionDefaultPreludeFromExePath(argv[0], slangSession);
// Create a compile request
@@ -93,6 +97,19 @@ static SlangResult _innerMain(int argc, char** argv)
ComPtr<ISlangSharedLibrary> sharedLibrary;
SLANG_RETURN_ON_FAIL(request->getTargetHostCallable(0, sharedLibrary.writeRef()));
+ DoThings doThings;
+
+ {
+ auto doThingsPtr = (IDoThings**)sharedLibrary->findSymbolAddressByName("globalDoThings");
+ if (!doThingsPtr)
+ {
+ return SLANG_FAIL;
+ }
+ // Set the global interface
+ *doThingsPtr = &doThings;
+ }
+
+ // Test a free function
{
typedef const char* (*Func)(const char*);
Func func = (Func)sharedLibrary->findFuncByName("getString");
@@ -107,25 +124,34 @@ static SlangResult _innerMain(int argc, char** argv)
SLANG_ASSERT(text == returnedText);
}
- {
- typedef int (*Func)(const char* text, IDoThings* doThings);
+ // Test hash
+ {
+ typedef int (*Func)(const char* text);
Func func = (Func)sharedLibrary->findFuncByName("calcHash");
-
if (!func)
{
return SLANG_FAIL;
}
- DoThings doThings;
-
String text("Hello");
-
- const int hash = func(text.getBuffer(), &doThings);
-
+ const int hash = func(text.getBuffer());
SLANG_ASSERT(hash == _calcHash(text.getBuffer()));
}
-
+
+ // Test printing
+ {
+ typedef void (*Func)(const char* text);
+
+ Func func = (Func)sharedLibrary->findFuncByName("printMessage");
+
+ if (!func)
+ {
+ return SLANG_FAIL;
+ }
+ func("Hello World!");
+ }
+
return SLANG_OK;
}