yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Ellie Hermaszewskaformatf65d756bf

master
4.8 KiB191 linesraw
1// test-process-main.cpp
2
3#include "../../source/core/slang-http.h"
4#include "../../source/core/slang-io.h"
5#include "../../source/core/slang-process-util.h"
6#include "../../source/core/slang-string-util.h"
7#include "../../source/core/slang-string.h"
8#include "../../source/core/slang-test-tool-util.h"
9#include "slang-com-helper.h"
10
11#include <stdio.h>
12#include <stdlib.h>
13#include <string.h>
14
15namespace TestProcess
16{
17using namespace Slang;
18
19static void _makeStdStreamsUnbuffered()
20{
21    // Make these streams unbuffered
22    setvbuf(stdout, nullptr, _IONBF, 0);
23    setvbuf(stderr, nullptr, _IONBF, 0);
24}
25
26static SlangResult _outputCount(int argc, const char* const* argv)
27{
28    if (argc < 3)
29    {
30        return SLANG_FAIL;
31    }
32    // Get the count
33    const Index count = stringToInt(argv[2]);
34
35    // If we want to crash
36    Index crashIndex = -1;
37    if (argc > 3)
38    {
39        // When we crash, we want to make sure everything is flushed...
40        _makeStdStreamsUnbuffered();
41        crashIndex = stringToInt(argv[3]);
42    }
43
44    FILE* fileOut = stdout;
45
46    StringBuilder buf;
47    for (Index i = 0; i < count; ++i)
48    {
49        buf.clear();
50        buf << i << "\n";
51
52        fwrite(buf.getBuffer(), 1, buf.getLength(), fileOut);
53
54        if (i == crashIndex)
55        {
56            // Use to simulate a crash.
57            SLANG_BREAKPOINT(0);
58            throw;
59        }
60    }
61
62    // NOTE! There is no flush here, we want to see if everything works if we just stream out.
63    return SLANG_OK;
64}
65
66static SlangResult _outputReflect()
67{
68    // Read lines from std input.
69    // When hit line with just 'end', terminate
70
71    // Get in as Stream
72
73    RefPtr<Stream> stdinStream;
74    Process::getStdStream(StdStreamType::In, stdinStream);
75
76    FILE* fileOut = stdout;
77
78    List<Byte> buffer;
79
80    Index startIndex = 0;
81
82    while (true)
83    {
84        SLANG_RETURN_ON_FAIL(StreamUtil::read(stdinStream, 0, buffer));
85
86        while (true)
87        {
88            UnownedStringSlice slice(
89                (const char*)buffer.begin() + startIndex,
90                (const char*)buffer.end());
91            UnownedStringSlice line;
92
93            if (!StringUtil::extractLine(slice, line) || slice.begin() == nullptr)
94            {
95                break;
96            }
97
98            // Process the line
99            if (line == UnownedStringSlice::fromLiteral("end"))
100            {
101                return SLANG_OK;
102            }
103
104            // Write the text to the output stream
105            fwrite(line.begin(), 1, line.getLength(), fileOut);
106            fputc('\n', fileOut);
107
108            // Move the start index forward
109            const Index newStartIndex = slice.begin()
110                                            ? Index(slice.begin() - (const char*)buffer.getBuffer())
111                                            : buffer.getCount();
112            SLANG_ASSERT(newStartIndex > startIndex);
113            startIndex = newStartIndex;
114        }
115    }
116}
117
118static SlangResult _httpReflect(int argc, const char* const* argv)
119{
120    SLANG_UNUSED(argc);
121    SLANG_UNUSED(argv);
122
123    RefPtr<Stream> stdinStream, stdoutStream;
124
125    Process::getStdStream(StdStreamType::In, stdinStream);
126    Process::getStdStream(StdStreamType::Out, stdoutStream);
127
128    RefPtr<BufferedReadStream> readStream(new BufferedReadStream(stdinStream));
129
130    RefPtr<HTTPPacketConnection> connection = new HTTPPacketConnection(readStream, stdoutStream);
131
132    while (connection->isActive())
133    {
134        // Block waiting for content (or error/closed)
135        SLANG_RETURN_ON_FAIL(connection->waitForResult());
136
137        // If we have content do something with it
138        if (connection->hasContent())
139        {
140            auto content = connection->getContent();
141
142            // If it just holds 'end' then we are done
143            const UnownedStringSlice slice((const char*)content.begin(), content.getCount());
144
145            if (slice == UnownedStringSlice::fromLiteral("end"))
146            {
147                break;
148            }
149
150            // Else reflect it back
151            SLANG_RETURN_ON_FAIL(connection->write(content.begin(), content.getCount()));
152
153            // Consume that content/packet
154            connection->consumeContent();
155        }
156    }
157
158    return SLANG_OK;
159}
160
161static SlangResult execute(int argc, const char* const* argv)
162{
163    if (argc < 2)
164    {
165        return SLANG_FAIL;
166    }
167
168    // Get the tool name
169    const String toolName = argv[1];
170    if (toolName == "reflect")
171    {
172        return _outputReflect();
173    }
174    else if (toolName == "count")
175    {
176        return _outputCount(argc, argv);
177    }
178    else if (toolName == "http-reflect")
179    {
180        return _httpReflect(argc, argv);
181    }
182    return SLANG_E_NOT_AVAILABLE;
183}
184
185} // namespace TestProcess
186
187int main(int argc, const char* const* argv)
188{
189    SlangResult res = TestProcess::execute(argc, argv);
190    return (int)Slang::TestToolUtil::getReturnCode(res);
191}