yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
7b570feed
master
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 ( 15JSONContainer * container , 16const String & json , 17DiagnosticSink * sink , 18SourceMap & outSourceMap ) 19{ 20auto sourceManager = sink -> getSourceManager (); 21 22JSONValue rootValue ; 23 { 24// Now need to parse as JSON 25SourceFile * sourceFile = 26sourceManager -> createSourceFileWithString (PathInfo ::makeUnknown (),json ); 27SourceView * sourceView = sourceManager -> createSourceView (sourceFile ,nullptr ,SourceLoc ()); 28 29JSONLexer lexer ; 30lexer .init (sourceView ,sink ); 31 32JSONBuilder builder (container ); 33 34JSONParser parser ; 35SLANG_RETURN_ON_FAIL (parser .parse (& lexer ,sourceView ,& builder ,sink )); 36 37rootValue = builder .getRootValue (); 38 } 39 40SLANG_RETURN_ON_FAIL (JSONSourceMapUtil ::decode (container ,rootValue ,sink ,outSourceMap )); 41 42return SLANG_OK ; 43} 44 45static SlangResult _write ( 46JSONContainer * container , 47const SourceMap & sourceMap , 48DiagnosticSink * sink , 49String & out ) 50{ 51// Write it out 52JSONValue jsonValue ; 53 54SLANG_RETURN_ON_FAIL (JSONSourceMapUtil ::encode (sourceMap ,container ,sink ,jsonValue )); 55 56// Convert into a string 57JSONWriter writer (JSONWriter ::IndentationStyle ::Allman ); 58container -> traverseRecursively (jsonValue ,& writer ); 59 60out = writer .getBuilder (); 61return SLANG_OK ; 62} 63 64static SlangResult _check () 65{ 66SourceManager sourceManager ; 67sourceManager .initialize (nullptr ,nullptr ); 68DiagnosticSink sink (& sourceManager ,nullptr ); 69 70const 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 82RefPtr < JSONContainer > container = new JSONContainer (& sourceManager ); 83 84SourceMap sourceMap ; 85SLANG_RETURN_ON_FAIL (_read (container ,jsonSource ,& sink ,sourceMap )); 86 87String json ; 88SLANG_RETURN_ON_FAIL (_write (container ,sourceMap ,& sink ,json )); 89 90SourceMap readSourceMap ; 91SLANG_RETURN_ON_FAIL (_read (container ,json ,& sink ,readSourceMap )); 92 93if (readSourceMap != sourceMap ) 94 { 95return SLANG_FAIL ; 96 } 97 98// Lets try copy construction 99 { 100SourceMap copy (sourceMap ); 101 102if (copy != sourceMap ) 103 { 104return SLANG_FAIL ; 105 } 106 } 107 108// Lets try assignment 109 { 110SourceMap assign ; 111assign = sourceMap ; 112if (assign != sourceMap ) 113 { 114return SLANG_FAIL ; 115 } 116 } 117 118return SLANG_OK ; 119} 120 121SLANG_UNIT_TEST (sourceMap ) 122{ 123SLANG_CHECK (SLANG_SUCCEEDED (_check ())); 124}