// use map file to get TS source file pos from JS file pos private static SourcePos FindTypeScriptLineNumber(string soureMapFile, SourcePos jsOneBasedSourcePos) { // convert to zero-based coords SourcePosition jsZeroBasedSourcePos = jsOneBasedSourcePos.GetZeroBasedPosition(); // Parse the source map from file SourceMapParser parser = new SourceMapParser(); SourceMap sourceMap; using (FileStream stream = new FileStream(soureMapFile, FileMode.Open)) { sourceMap = parser.ParseSourceMap(new StreamReader(stream)); } // look for JS source position in map entries // if we can't find the exact location in the map file, we get a null result, so to safeguard against this happening, // we'll look 10 chars either side of the specfied column, and take the closest match if we can't find an exact match // if that also fails, we return notFound = [0,0] = line 1, column 1 // (column is not currently used, but required to get the mapped line number) MappingEntry[] mapEntry = new MappingEntry[3]; for (int delta = 0; delta <= 10; delta++) { SourcePosition posBefore = jsZeroBasedSourcePos; posBefore.ZeroBasedColumnNumber -= delta; SourcePosition posAfter = jsZeroBasedSourcePos; posAfter.ZeroBasedColumnNumber += delta; mapEntry[0] = sourceMap.GetMappingEntryForGeneratedSourcePosition(jsZeroBasedSourcePos); mapEntry[1] = sourceMap.GetMappingEntryForGeneratedSourcePosition(posBefore); mapEntry[2] = sourceMap.GetMappingEntryForGeneratedSourcePosition(posAfter); if (mapEntry[0] != null || mapEntry[1] != null || mapEntry[2] != null) { break; } } MappingEntry tsMapEnt = null; for (int i = 0; i < 3; i++) { if (mapEntry[i] != null) { tsMapEnt = mapEntry[i]; break; } } if (tsMapEnt == null) { Log($"Unable to map Javascript source pos to TypeScript source file, mapping file was: {soureMapFile}"); return(SourcePos.Notfound); } else { Log($"Successfully mapped Javascript source pos to TypeScript pos using mapping file: {soureMapFile}"); return(new SourcePos(tsMapEnt.OriginalSourcePosition)); } }
private SourceMap GetSourceMap(JSContext ctx, string fileName) { SourceMap sourceMap; if (!_sourceMaps.TryGetValue(fileName, out sourceMap)) { try { var runtime = ScriptEngine.GetRuntime(ctx); var fileResolver = runtime.GetPathResolver(); var fileSystem = runtime.GetFileSystem(); string searchPath; string resolvedPath; if (fileResolver.ResolvePath(fileSystem, fileName + ".map", out searchPath, out resolvedPath)) { var fileContent = fileSystem.ReadAllBytes(resolvedPath); if (fileContent != null && fileContent.Length > 0) { using (var stream = new MemoryStream(fileContent)) { var parser = new SourceMapParser(); var reader = new StreamReader(stream); sourceMap = parser.ParseSourceMap(reader); // Debug.Log($"[SourceMapHelper] parse sourceMap: {sourceMap.File} ({resolvedPath})"); } } } } finally { _sourceMaps[fileName] = sourceMap; } } return(sourceMap); }
private static SourceMap GetSourceMap(IntPtr ctx, string fileName) { SourceMap sourceMap; if (!_sourceMaps.TryGetValue(fileName, out sourceMap)) { try { var fileResolver = DuktapeVM.GetVM(ctx).fileResolver; var fileContent = fileResolver.ReadAllBytes(fileName + ".map"); if (fileContent != null && fileContent.Length > 0) { using (var stream = new MemoryStream(fileContent)) { var parser = new SourceMapParser(); var reader = new StreamReader(stream); sourceMap = parser.ParseSourceMap(reader); // Debug.Log($"[SourceMapHelper] parse sourceMap: {sourceMap.File} ({resolvedPath})"); } } } finally { _sourceMaps[fileName] = sourceMap; } } return(sourceMap); }
private static SourceMap GetSourceMap(IntPtr ctx, string fileName) { SourceMap sourceMap; if (!_sourceMaps.TryGetValue(fileName, out sourceMap)) { try { var vm = DuktapeVM.GetVM(ctx); var resolvedPath = vm.ResolvePath(fileName + ".map"); if (resolvedPath != null) { using (var stream = File.OpenRead(resolvedPath)) { var parser = new SourceMapParser(); var reader = new StreamReader(stream); sourceMap = parser.ParseSourceMap(reader); // Debug.Log($"[SourceMapHelper] parse sourceMap: {sourceMap.File} ({resolvedPath})"); } } } finally { _sourceMaps[fileName] = sourceMap; } } return(sourceMap); }
public void Parse_SourcesContentContainsOriginalSources() { var map = SourceMapParser.Parse(JsonSerializer.Serialize(Helper.TestMapWithSourcesContent)); Assert.Equal(2, map.SourcesContent.Count); Assert.Equal(" ONE.foo = function (bar) {\n return baz(bar);\n };", map.SourcesContent[0]); Assert.Equal(" TWO.inc = function (n) {\n return n + 1;\n };", map.SourcesContent[1]); }
/// <summary> /// Creates a StackTraceDeminifier that only deminifies the method names. StackTrace deminifiers created with this method will use significantly less memory during runtime than the /// </summary> /// <param name="sourceMapProvider">Consumers of the API should implement this interface, which provides the source map for a given JavaScript file. Throws ArgumentNullException if the parameter is set to null.</param> /// <param name="generatedCodeProvider">Consumers of the API should implement this interface, which provides the contents of a JavaScript file. Throws ArgumentNullException if the parameter is set to null.</param> /// <param name="stackTraceParser">Consumers of the API should implement this interface, which provides a parser for the stacktrace. Throws ArgumentNullException if the parameter is set to null.</param> public static StackTraceDeminifier GetMethodNameOnlyStackTraceDeminfier(ISourceMapProvider sourceMapProvider, ISourceCodeProvider generatedCodeProvider, IStackTraceParser stackTraceParser) { ValidateArguments(sourceMapProvider, generatedCodeProvider, stackTraceParser); SourceMapParser sourceMapParser = new SourceMapParser(); IStackFrameDeminifier stackFrameDeminifier = new MethodNameStackFrameDeminifier(new FunctionMapStore(generatedCodeProvider, (url) => sourceMapParser.ParseSourceMap(sourceMapProvider.GetSourceMapContentsForCallstackUrl(url))), new FunctionMapConsumer()); return(new StackTraceDeminifier(stackFrameDeminifier, stackTraceParser)); }
string ProcessWithSourceMaps(IEnumerable <AssetContent> assets) { var results = assets.Select(a => new { asset = a, result = this.reactEnvironment.ExecuteWithBabel <JavaScriptWithSourceMap>("ReactNET_transform_sourcemap", a.Contents, babelConfig, a.Path) }).ToArray(); var offset = 0; var map = new SourcemapToolkit.SourcemapParser.SourceMap(); var sourceMapParser = new SourceMapParser(); var outputBuilder = new StringBuilder(); foreach (var result in results) { var json = result.result.SourceMap.ToJson(); using (var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(json))) { using (var streamReader = new StreamReader(memoryStream)) { var sourceMap = sourceMapParser.ParseSourceMap(streamReader); foreach (var name in sourceMap.Names) { map.Names.Add(name); } map.Sources.Add(result.asset.Path.ToLower()); foreach (var mappingEntry in sourceMap.ParsedMappings) { if (mappingEntry.OriginalSourcePosition == null) { continue; } map.ParsedMappings.Add(mappingEntry); mappingEntry.OriginalFileName = result.asset.Path.ToLower(); mappingEntry.GeneratedSourcePosition.ZeroBasedLineNumber += offset; } offset += result.result.Code.Split('\n').Length; } } outputBuilder.AppendLine(result.result.Code); } var sourceMapGenerator = new SourceMapGenerator(); var generateSourceMapInlineComment = sourceMapGenerator.GenerateSourceMapInlineComment(map); outputBuilder.AppendLine(generateSourceMapInlineComment); return(outputBuilder.ToString()); }
public void ParseSourceMap_NullInputStream_ReturnsNull() { // Arrange SourceMapParser sourceMapParser = new SourceMapParser(); StreamReader input = null; // Act SourceMap output = sourceMapParser.ParseSourceMap(input); // Assert Assert.Null(output); }
/// <summary> /// Parses an encoded sourcemap string and matches each entry to an AST Node /// </summary> static (SourceMapEntry[] SourceMapEntries, AstNode[] AstNodes) ParseSourceMap(string encodedSourceMap, Dictionary <int, AstNode[]> astDict) { var sourceMapItems = SourceMapParser.Parse(encodedSourceMap); var astNodes = new List <AstNode>(); for (var i = 0; i < sourceMapItems.Length; i++) { var sourceMap = sourceMapItems[i]; var node = MatchSourceMapEntry(sourceMap, astDict); if (node != null) { astNodes.Add(node); } } return(sourceMapItems, astNodes.ToArray()); }
// [Fact] public void Parse_MapsMappingTokensBackExactly() { var map = SourceMapParser.Parse(JsonSerializer.Serialize(Helper.TestMap)); Helper.AssertMapping(1, 1, "/the/root/one.js", 1, 1, null, map); Helper.AssertMapping(1, 5, "/the/root/one.js", 1, 5, null, map); Helper.AssertMapping(1, 9, "/the/root/one.js", 1, 11, null, map); Helper.AssertMapping(1, 18, "/the/root/one.js", 1, 21, "bar", map); Helper.AssertMapping(1, 21, "/the/root/one.js", 2, 3, null, map); Helper.AssertMapping(1, 28, "/the/root/one.js", 2, 10, "baz", map); Helper.AssertMapping(1, 32, "/the/root/one.js", 2, 14, "bar", map); Helper.AssertMapping(2, 1, "/the/root/two.js", 1, 1, null, map); Helper.AssertMapping(2, 5, "/the/root/two.js", 1, 5, null, map); Helper.AssertMapping(2, 9, "/the/root/two.js", 1, 11, null, map); Helper.AssertMapping(2, 18, "/the/root/two.js", 1, 21, "n", map); Helper.AssertMapping(2, 21, "/the/root/two.js", 2, 3, null, map); Helper.AssertMapping(2, 28, "/the/root/two.js", 2, 10, "n", map); }
public void ParseSourceMap_SimpleSourceMap_CorrectlyParsed() { // Arrange SourceMapParser sourceMapParser = new SourceMapParser(); string input = "{ \"version\":3, \"file\":\"CommonIntl\", \"lineCount\":65, \"mappings\":\"AACAA,aAAA,CAAc\", \"sources\":[\"input/CommonIntl.js\"], \"names\":[\"CommonStrings\",\"afrikaans\"]}"; // Act SourceMap output = sourceMapParser.ParseSourceMap(UnitTestUtils.StreamReaderFromString(input)); // Assert Assert.Equal(3, output.Version); Assert.Equal("CommonIntl", output.File); Assert.Equal("AACAA,aAAA,CAAc", output.Mappings); Assert.Equal(1, output.Sources.Count); Assert.Equal("input/CommonIntl.js", output.Sources[0]); Assert.Equal(2, output.Names.Count); Assert.Equal("CommonStrings", output.Names[0]); Assert.Equal("afrikaans", output.Names[1]); }
public void ApplyMappingSegment_CorrectlyUpdatesState() { var state = new MappingParserState { GeneratedLineNumber = 1, GeneratedColumnNumber = 2, SourcesListIndex = 3, OriginalLineNumber = 4, OriginalColumnNumber = 5, NamesListIndex = 6 }; var segmentFields = new List <int> { 4 }; // Only column number SourceMapParser.ApplyMappingSegment(segmentFields, ref state); Assert.Equal(1, state.GeneratedLineNumber); Assert.Equal(6, state.GeneratedColumnNumber); Assert.Equal(3, state.SourcesListIndex); Assert.Equal(4, state.OriginalLineNumber); Assert.Equal(5, state.OriginalColumnNumber); Assert.Equal(6, state.NamesListIndex); segmentFields = new List <int> { 1, 5, 4, 3 }; SourceMapParser.ApplyMappingSegment(segmentFields, ref state); Assert.Equal(1, state.GeneratedLineNumber); Assert.Equal(7, state.GeneratedColumnNumber); Assert.Equal(8, state.SourcesListIndex); Assert.Equal(8, state.OriginalLineNumber); Assert.Equal(8, state.OriginalColumnNumber); Assert.Equal(6, state.NamesListIndex); segmentFields = new List <int> { 1, 1, 1, 1, 3 }; SourceMapParser.ApplyMappingSegment(segmentFields, ref state); Assert.Equal(1, state.GeneratedLineNumber); Assert.Equal(8, state.GeneratedColumnNumber); Assert.Equal(9, state.SourcesListIndex); Assert.Equal(9, state.OriginalLineNumber); Assert.Equal(9, state.OriginalColumnNumber); Assert.Equal(9, state.NamesListIndex); }
public void Parse_SourceMap_ContainsOriginalSources() { SourceMap map; map = SourceMapParser.Parse(JsonSerializer.Serialize(Helper.TestMap)); Assert.Equal("/the/root/one.js", map.Sources[0]); Assert.Equal("/the/root/two.js", map.Sources[1]); Assert.Equal(2, map.Sources.Count); map = SourceMapParser.Parse(JsonSerializer.Serialize(Helper.TestMapNoSourceRoot)); Assert.Equal("one.js", map.Sources[0]); Assert.Equal("two.js", map.Sources[1]); Assert.Equal(2, map.Sources.Count); map = SourceMapParser.Parse(JsonSerializer.Serialize(Helper.TestMapEmptySourceRoot)); Assert.Equal("one.js", map.Sources[0]); Assert.Equal("two.js", map.Sources[1]); Assert.Equal(2, map.Sources.Count); }
static void Main() { // Module can be used to create, read, modify, and write WebAssembly files. SourceMapParser parser = new SourceMapParser(); SourceMap sourceMap; using (FileStream stream = new FileStream(@"../../fac.wasm.map", FileMode.Open)) { sourceMap = parser.ParseSourceMap(new StreamReader(stream)); } var parsedMapping = sourceMap.ParsedMappings; var module = Module.ReadFromBinary("../../fac.wasm"); var nameSection = module.CustomSections.FirstOrDefault(cs => cs.Name == "name"); var NameSection = new NameSection(nameSection); var actorScriptSection = module.CustomSections.FirstOrDefault(cs => cs.Name == "actorScript"); ActorScript.Meta.actorScriptSection = new ActorScriptSection(actorScriptSection); // We now have enough for a usable WASM file, which we could save with module.WriteToBinary(). // Below, we show how the Compile feature can be used for .NET-based execution. // For stream-based compilation, WebAssembly.Compile should be used. // var instanceCreator = module.Compile<Sample>(); var instanceCreator = Compile.FromBinary <Sample>(@"../../fac.wasm"); // Instances should be wrapped in a "using" block for automatic disposal. using (var instance = instanceCreator()) { // FYI, instanceCreator can be used multiple times to create independent instances. //Console.WriteLine(instance.Exports.Fac(1));// Binary 0, result 0 } // Automatically release the WebAssembly instance here. }
/// <summary> /// Constructs a new SourceMapper. /// </summary> public SourceMapper() { this.m_parser = new SourceMapParser(); this.m_maps = new Dictionary <string, SourceMap>(); }
public void Parse_CanParseGeneratedSourcemaps(string path) { var map = SourceMapParser.Parse(File.ReadAllText(path)); Assert.NotNull(map); }
public void Parse_ParsesSuccessfully() { var result = SourceMapParser.Parse(JsonSerializer.Serialize(Helper.TestMap)); Assert.NotNull(result); }
public void ParseMappings_CorrectlyParsesSingleMapping() { var result = SourceMapParser.ParseMappings(Helper.TestMapWithSourcesContent.MappingsString, Helper.TestMapWithSourcesContent.Names, Helper.TestMapWithSourcesContent.Sources); Assert.Equal(13, result.Count); }
public SourceMapStore(ISourceMapProvider sourceMapProvider) { _sourceMapProvider = sourceMapProvider; _sourceMapParser = new SourceMapParser(); _sourceMapCache = new KeyValueCache <string, SourceMap>(sourceCodeUrl => _sourceMapParser.ParseSourceMap(_sourceMapProvider.GetSourceMapContentsForCallstackUrl(sourceCodeUrl))); }