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); }
// 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 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); }
/// <summary> /// Adds a JS file and associated source map to the mapper. /// </summary> /// <param name="fileName">The filename of a transpiled target file (NOT the source file).</param> /// <param name="mapJson">The JSON text of the V3 source map.</param> public void AddSource(string fileName, string mapJson) { using (var mapReader = new StreamReader(mapJson.ToStream())) { var map = m_parser.ParseSourceMap(mapReader); this.m_maps[fileName] = map; } }
public void ParseSourceMap_NullInputStream_ReturnsNull() { // Arrange SourceMapParser sourceMapParser = new SourceMapParser(); StreamReader input = null; // Act SourceMap output = sourceMapParser.ParseSourceMap(input); // Assert Assert.Null(output); }
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()); }
private async Task <SourceMap> GetMapFromUrl(string url) { var jsOriginalSource = await httpClient.GetStringAsync(url); var match = mappingUrlRegex.Match(jsOriginalSource); if (!match.Success) { log.Error($"failed to find sourcemap URL in JS file: {url}"); return(null); } var sourceMapRelativeUri = match.Groups[1].Value; var sourceMapUrl = new Uri(new Uri(url), sourceMapRelativeUri); var sourceMapStream = await httpClient.GetStreamAsync(sourceMapUrl); return(sourceMapParser.ParseSourceMap(new StreamReader(sourceMapStream))); }
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]); }
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> /// 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)); }
public SourceMapStore(ISourceMapProvider sourceMapProvider) { _sourceMapProvider = sourceMapProvider; _sourceMapParser = new SourceMapParser(); _sourceMapCache = new KeyValueCache <string, SourceMap>(sourceCodeUrl => _sourceMapParser.ParseSourceMap(_sourceMapProvider.GetSourceMapContentsForCallstackUrl(sourceCodeUrl))); }