|
| 1 | +/* |
| 2 | + * Copyright 2024 WebAssembly Community Group participants |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "parser/wat-parser.h" |
| 18 | +#include "print-test.h" |
| 19 | +#include "wasm-binary.h" |
| 20 | +#include "gtest/gtest.h" |
| 21 | + |
| 22 | +using namespace wasm; |
| 23 | + |
| 24 | +using BinaryReaderTest = PrintTest; |
| 25 | + |
| 26 | +// Check that debug location parsers can handle single-segment mappings. |
| 27 | +TEST_F(BinaryReaderTest, SourceMappingSingleSegment) { |
| 28 | + auto moduleText = "(module)"; |
| 29 | + Module module; |
| 30 | + parseWast(module, moduleText); |
| 31 | + |
| 32 | + BufferWithRandomAccess buffer; |
| 33 | + WasmBinaryWriter(&module, buffer, PassOptions()); |
| 34 | + auto moduleBytes = buffer.getAsChars(); |
| 35 | + |
| 36 | + // A single-segment mapping starting at offset 0. |
| 37 | + std::string sourceMap = R"( |
| 38 | + { |
| 39 | + "version": 3, |
| 40 | + "sources": [], |
| 41 | + "names": [], |
| 42 | + "mappings": "A" |
| 43 | + } |
| 44 | + )"; |
| 45 | + std::stringstream sourceMapStream(sourceMap); |
| 46 | + |
| 47 | + // Test `readSourceMapHeader` (only check for errors, as there is no mapping |
| 48 | + // to print). |
| 49 | + { |
| 50 | + Module module; |
| 51 | + WasmBinaryReader binaryReader(module, FeatureSet::All, moduleBytes); |
| 52 | + binaryReader.setDebugLocations(&sourceMapStream); |
| 53 | + binaryReader.readSourceMapHeader(); |
| 54 | + } |
| 55 | + |
| 56 | + // Test `readNextDebugLocation`. |
| 57 | + { |
| 58 | + Module module; |
| 59 | + WasmBinaryReader binaryReader(module, FeatureSet::All, moduleBytes); |
| 60 | + binaryReader.setDebugLocations(&sourceMapStream); |
| 61 | + binaryReader.readNextDebugLocation(); |
| 62 | + } |
| 63 | +} |
0 commit comments