From df18361ab554e00f7b102e30a1c7e252439f594a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Ole=C5=9B?= Date: Wed, 20 Jan 2021 23:14:01 +0100 Subject: [PATCH 1/2] add `reportDiagnostic` to APIOptions in asc --- .gitignore | 3 ++- NOTICE | 1 + cli/asc.d.ts | 36 +++++++++++++++++++++++++- cli/asc.js | 72 +++++++++++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 106 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 57082f26a3..2efb8995ec 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ out/ raw/ .history *.backup -.vscode \ No newline at end of file +.vscode +.idea diff --git a/NOTICE b/NOTICE index e43810f24e..2f95c0cc0e 100644 --- a/NOTICE +++ b/NOTICE @@ -29,6 +29,7 @@ under the licensing terms detailed in LICENSE: * Valeria Viana Gusmao * Gabor Greif * Martin Fredriksson +* Piotr OleÅ› Portions of this software are derived from third-party works licensed under the following terms: diff --git a/cli/asc.d.ts b/cli/asc.d.ts index 7957ac7c2f..ecf3c9589c 100644 --- a/cli/asc.d.ts +++ b/cli/asc.d.ts @@ -55,6 +55,38 @@ export interface MemoryStream extends OutputStream { toString(): string; } +/** A subset of Source interface from assemblyscript package */ +export interface Source { + /** Normalized path with file extension. */ + normalizedPath: string; +} + +/** A subset of Range interface from assemblyscript package */ +export interface Range { + /** Range start char index in a source */ + start: number; + /** Range end char index in a source */ + end: number; + source: Source; +} + +/** A subset of DiagnosticMessage interface from assemblyscript package */ +export interface DiagnosticMessage { + /** Message code. */ + code: number; + /** Message category. */ + category: number; + /** Message text. */ + message: string; + /** Respective source range, if any. */ + range: Range | null; + /** Related range, if any. */ + relatedRange: Range | null; +} + +/** A function that handles diagnostic */ +type DiagnosticReporter = (diagnostic: DiagnosticMessage) => void; + /** Compiler options. */ export interface CompilerOptions { /** Prints just the compiler's version and exits. */ @@ -157,6 +189,8 @@ export interface APIOptions { writeFile?: (filename: string, contents: Uint8Array, baseDir: string) => void; /** Lists all files within a directory. */ listFiles?: (dirname: string, baseDir: string) => string[] | null; + /** Handles diagnostic messages. */ + reportDiagnostic?: DiagnosticReporter; } /** Convenience function that parses and compiles source strings directly. */ @@ -176,7 +210,7 @@ export function main(argv: string[], options: APIOptions, callback?: (err: Error export function main(argv: string[], callback?: (err: Error | null) => number): number; /** Checks diagnostics emitted so far for errors. */ -export function checkDiagnostics(emitter: Record, stderr?: OutputStream): boolean; +export function checkDiagnostics(emitter: Record, stderr?: OutputStream, reportDiagnostic?: DiagnosticReporter): boolean; /** An object of stats for the current task. */ export interface Stats { diff --git a/cli/asc.js b/cli/asc.js index 12d6019eb2..fe21fc77cb 100644 --- a/cli/asc.js +++ b/cli/asc.js @@ -127,7 +127,7 @@ function loadAssemblyScriptWasm(binaryPath) { return exports; } -var assemblyscript, __newString, __getString, __pin, __unpin, __collect; +var assemblyscript, __newString, __getString, __pin, __unpin, __collect, __wrapDiagnosticMessage, __wrapRange, __wrapSource; function loadAssemblyScript() { const wasmArg = process.argv.findIndex(arg => arg == "--wasm"); @@ -140,6 +140,9 @@ function loadAssemblyScript() { __pin = assemblyscript.__pin; __unpin = assemblyscript.__unpin; __collect = assemblyscript.__collect; + __wrapDiagnosticMessage = assemblyscript.DiagnosticMessage.wrap; + __wrapRange = assemblyscript.Range.wrap; + __wrapSource = assemblyscript.Source.wrap; } else { assemblyscript = loadAssemblyScriptJS(); __newString = str => str; @@ -147,6 +150,9 @@ function loadAssemblyScript() { __pin = ptr => ptr; __unpin = ptr => undefined; __collect = incremental => undefined; + __wrapDiagnosticMessage = ptr => ptr; + __wrapRange = ptr => ptr; + __wrapSource = ptr => ptr; } } loadAssemblyScript(); @@ -700,7 +706,7 @@ exports.main = function main(argv, options, callback) { }); } } - var numErrors = checkDiagnostics(program, stderr); + var numErrors = checkDiagnostics(program, stderr, options.reportDiagnostic); if (numErrors) { const err = Error(numErrors + " parse error(s)"); err.stack = err.message; // omit stack @@ -814,7 +820,7 @@ exports.main = function main(argv, options, callback) { }; } }); - var numErrors = checkDiagnostics(program, stderr); + var numErrors = checkDiagnostics(program, stderr, options.reportDiagnostic); if (numErrors) { if (module) module.dispose(); const err = Error(numErrors + " compile error(s)"); @@ -1153,7 +1159,7 @@ function getAsconfig(file, baseDir, readFile) { exports.getAsconfig = getAsconfig; /** Checks diagnostics emitted so far for errors. */ -function checkDiagnostics(program, stderr) { +function checkDiagnostics(program, stderr, reportDiagnostic) { var numErrors = 0; do { let diagnosticPtr = assemblyscript.nextDiagnostic(program); @@ -1165,6 +1171,64 @@ function checkDiagnostics(program, stderr) { EOL + EOL ); } + if (reportDiagnostic) { + const diagnostic = __wrapDiagnosticMessage(diagnosticPtr); + __pin(diagnostic.message); + __pin(diagnostic.range); + __pin(diagnostic.relatedRange); + const range = diagnostic.range ? __wrapRange(diagnostic.range) : null; + const relatedRange = diagnostic.relatedRange ? __wrapRange(diagnostic.relatedRange) : null; + if (range) { + __pin(range.source); + } + if (relatedRange) { + __pin(relatedRange.source); + } + const rangeSource = range ? __wrapSource(range.source) : null; + const relatedRangeSource = relatedRange ? __wrapSource(relatedRange.source) : null; + if (rangeSource) { + __pin(rangeSource.normalizedPath); + } + if (relatedRangeSource) { + __pin(relatedRangeSource.normalizedPath); + } + + reportDiagnostic({ + message: __getString(diagnostic.message), + code: diagnostic.code, + category: diagnostic.category, + range: range ? { + start: range.start, + end: range.end, + source: rangeSource ? { + normalizedPath: __getString(rangeSource.normalizedPath) + } : null, + } : null, + relatedRange: relatedRange ? { + start: relatedRange.start, + end: relatedRange.end, + source: relatedRangeSource ? { + normalizedPath: __getString(relatedRangeSource.normalizedPath) + } : null + } : null + }); + + if (relatedRangeSource) { + __unpin(relatedRangeSource.normalizedPath); + } + if (rangeSource) { + __unpin(rangeSource.normalizedPath); + } + if (relatedRange) { + __unpin(relatedRange.source); + } + if (range) { + __unpin(range.source); + } + __unpin(diagnostic.message); + __unpin(diagnostic.range); + __unpin(diagnostic.relatedRange); + } if (assemblyscript.isError(diagnosticPtr)) ++numErrors; __unpin(diagnosticPtr); } while (true); From cab1e12b39feba14c757049a40bdefb07502c67e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Ole=C5=9B?= Date: Fri, 22 Jan 2021 18:00:19 +0100 Subject: [PATCH 2/2] apply comments from pull request regarding reportDiagnostic feature --- cli/asc.js | 63 +++++++++++++++++------------------------------------- 1 file changed, 20 insertions(+), 43 deletions(-) diff --git a/cli/asc.js b/cli/asc.js index fe21fc77cb..cab931f77f 100644 --- a/cli/asc.js +++ b/cli/asc.js @@ -127,7 +127,21 @@ function loadAssemblyScriptWasm(binaryPath) { return exports; } -var assemblyscript, __newString, __getString, __pin, __unpin, __collect, __wrapDiagnosticMessage, __wrapRange, __wrapSource; +/** + * Wraps object in WASM environment, returns untouched otherwise. + * @template T + * @param {number | T} ptrOrObj Pointer in WASM environment, object otherwise + * @param {typeof T} type Object type that provides wrap method in WASM environment + * @returns {T | null} + */ +function __wrap(ptrOrObj, type) { + if (typeof ptrOrObj === "number") { + return ptrOrObj === 0 ? null : type.wrap(ptrOrObj); + } + return ptrOrObj; +} + +var assemblyscript, __newString, __getString, __pin, __unpin, __collect; function loadAssemblyScript() { const wasmArg = process.argv.findIndex(arg => arg == "--wasm"); @@ -140,9 +154,6 @@ function loadAssemblyScript() { __pin = assemblyscript.__pin; __unpin = assemblyscript.__unpin; __collect = assemblyscript.__collect; - __wrapDiagnosticMessage = assemblyscript.DiagnosticMessage.wrap; - __wrapRange = assemblyscript.Range.wrap; - __wrapSource = assemblyscript.Source.wrap; } else { assemblyscript = loadAssemblyScriptJS(); __newString = str => str; @@ -150,9 +161,6 @@ function loadAssemblyScript() { __pin = ptr => ptr; __unpin = ptr => undefined; __collect = incremental => undefined; - __wrapDiagnosticMessage = ptr => ptr; - __wrapRange = ptr => ptr; - __wrapSource = ptr => ptr; } } loadAssemblyScript(); @@ -1172,26 +1180,11 @@ function checkDiagnostics(program, stderr, reportDiagnostic) { ); } if (reportDiagnostic) { - const diagnostic = __wrapDiagnosticMessage(diagnosticPtr); - __pin(diagnostic.message); - __pin(diagnostic.range); - __pin(diagnostic.relatedRange); - const range = diagnostic.range ? __wrapRange(diagnostic.range) : null; - const relatedRange = diagnostic.relatedRange ? __wrapRange(diagnostic.relatedRange) : null; - if (range) { - __pin(range.source); - } - if (relatedRange) { - __pin(relatedRange.source); - } - const rangeSource = range ? __wrapSource(range.source) : null; - const relatedRangeSource = relatedRange ? __wrapSource(relatedRange.source) : null; - if (rangeSource) { - __pin(rangeSource.normalizedPath); - } - if (relatedRangeSource) { - __pin(relatedRangeSource.normalizedPath); - } + const diagnostic = __wrap(diagnosticPtr, assemblyscript.DiagnosticMessage); + const range = __wrap(diagnostic.range, assemblyscript.Range); + const relatedRange = __wrap(diagnostic.relatedRange, assemblyscript.Range); + const rangeSource = range ? __wrap(range.source, assemblyscript.Source) : null; + const relatedRangeSource = relatedRange ? __wrap(relatedRange.source, assemblyscript.Source) : null; reportDiagnostic({ message: __getString(diagnostic.message), @@ -1212,22 +1205,6 @@ function checkDiagnostics(program, stderr, reportDiagnostic) { } : null } : null }); - - if (relatedRangeSource) { - __unpin(relatedRangeSource.normalizedPath); - } - if (rangeSource) { - __unpin(rangeSource.normalizedPath); - } - if (relatedRange) { - __unpin(relatedRange.source); - } - if (range) { - __unpin(range.source); - } - __unpin(diagnostic.message); - __unpin(diagnostic.range); - __unpin(diagnostic.relatedRange); } if (assemblyscript.isError(diagnosticPtr)) ++numErrors; __unpin(diagnosticPtr);