yum-mirror/slang

Making it easier to work with shaders

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

Ellie HermaszewskaCorrect include dir for libslang (#5539)7b570feed

master
3.1 KiB124 linesraw
1
2#include "../../source/compiler-core/slang-json-lexer.h"
3#include "../../source/compiler-core/slang-json-native.h"
4#include "../../source/compiler-core/slang-json-parser.h"
5#include "../../source/compiler-core/slang-json-source-map-util.h"
6#include "../../source/compiler-core/slang-json-value.h"
7#include "../../source/compiler-core/slang-source-map.h"
8#include "../../source/core/slang-rtti-info.h"
9#include "../../source/core/slang-string-escape-util.h"
10#include "unit-test/slang-unit-test.h"
11
12using namespace Slang;
13
14static SlangResult _read(
15    JSONContainer* container,
16    const String& json,
17    DiagnosticSink* sink,
18    SourceMap& outSourceMap)
19{
20    auto sourceManager = sink->getSourceManager();
21
22    JSONValue rootValue;
23    {
24        // Now need to parse as JSON
25        SourceFile* sourceFile =
26            sourceManager->createSourceFileWithString(PathInfo::makeUnknown(), json);
27        SourceView* sourceView = sourceManager->createSourceView(sourceFile, nullptr, SourceLoc());
28
29        JSONLexer lexer;
30        lexer.init(sourceView, sink);
31
32        JSONBuilder builder(container);
33
34        JSONParser parser;
35        SLANG_RETURN_ON_FAIL(parser.parse(&lexer, sourceView, &builder, sink));
36
37        rootValue = builder.getRootValue();
38    }
39
40    SLANG_RETURN_ON_FAIL(JSONSourceMapUtil::decode(container, rootValue, sink, outSourceMap));
41
42    return SLANG_OK;
43}
44
45static SlangResult _write(
46    JSONContainer* container,
47    const SourceMap& sourceMap,
48    DiagnosticSink* sink,
49    String& out)
50{
51    // Write it out
52    JSONValue jsonValue;
53
54    SLANG_RETURN_ON_FAIL(JSONSourceMapUtil::encode(sourceMap, container, sink, jsonValue));
55
56    // Convert into a string
57    JSONWriter writer(JSONWriter::IndentationStyle::Allman);
58    container->traverseRecursively(jsonValue, &writer);
59
60    out = writer.getBuilder();
61    return SLANG_OK;
62}
63
64static SlangResult _check()
65{
66    SourceManager sourceManager;
67    sourceManager.initialize(nullptr, nullptr);
68    DiagnosticSink sink(&sourceManager, nullptr);
69
70    const char jsonSource[] = R"(
71{
72        "version" : 3,
73        "file" : "out.js",
74        "sourceRoot" : "",
75        "sources" : ["foo.js", "bar.js"],
76        "sourcesContent" : [null, null],
77        "names" : ["src", "maps", "are", "fun"],
78        "mappings" : "A,AAAB;;ABCEG;" 
79}
80)";
81
82    RefPtr<JSONContainer> container = new JSONContainer(&sourceManager);
83
84    SourceMap sourceMap;
85    SLANG_RETURN_ON_FAIL(_read(container, jsonSource, &sink, sourceMap));
86
87    String json;
88    SLANG_RETURN_ON_FAIL(_write(container, sourceMap, &sink, json));
89
90    SourceMap readSourceMap;
91    SLANG_RETURN_ON_FAIL(_read(container, json, &sink, readSourceMap));
92
93    if (readSourceMap != sourceMap)
94    {
95        return SLANG_FAIL;
96    }
97
98    // Lets try copy construction
99    {
100        SourceMap copy(sourceMap);
101
102        if (copy != sourceMap)
103        {
104            return SLANG_FAIL;
105        }
106    }
107
108    // Lets try assignment
109    {
110        SourceMap assign;
111        assign = sourceMap;
112        if (assign != sourceMap)
113        {
114            return SLANG_FAIL;
115        }
116    }
117
118    return SLANG_OK;
119}
120
121SLANG_UNIT_TEST(sourceMap)
122{
123    SLANG_CHECK(SLANG_SUCCEEDED(_check()));
124}