Zezombye / overpy

High-level language for the Overwatch Workshop with support for compilation and decompilation.
GNU General Public License v3.0
175 stars 25 forks source link

Add "sourceURL" in javascript eval for better macro error handling. #357

Closed nathan130200 closed 3 months ago

nathan130200 commented 5 months ago

I don't know if eval engine used in overpy will accept //# sourceURL=C:\path\to\my\file.js comment to help us understand the code better in JS macros or even add VS Code panel to output where the code is breaking. All errors thrown in that JS file will be automatically output in error stack trace based in //# sourceURL=.

I've tried edit overpy code to add an output panel to extension, but for some weird reason the compilation and VSCode itself start lagging alot.

I had noticed this sourceURL comment string when i used to eval some JS code to generate some enum classes for my csharp project.

Waiting for the debugger to disconnect...
c:\Users\Nathan\Documents\GitHub\XmppSharp\src\XmppSharp\Protocol\IqType.cs.typedef:72
throw new Error("Test");
      ^

Error: Test
    at eval (c:\Users\Nathan\Documents\GitHub\XmppSharp\src\XmppSharp\Protocol\IqType.cs.typedef:72:7)
    at eval (c:\Users\Nathan\Documents\GitHub\XmppSharp\src\XmppSharp\Protocol\IqType.cs.typedef:75:3)
    at file:///c:/Users/Nathan/Documents/GitHub/XmppSharp/src/XmppSharp/Protocol/_GenerateEnums.mjs:17:23
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25)

Node.js v18.17.1

The source

import * as fs from 'fs';
import * as path from 'path';

const baseDir = process.cwd();

const entries = fs.readdirSync(baseDir, {
    recursive: false
});

for (let file of entries) {
    let fileName = path.join(baseDir, file);
    let ofs = fileName.indexOf('.typedef');

    if (ofs != -1) {
        var outFileName = fileName.substring(0, ofs);

        var typeDef = eval(`//# sourceURL=${fileName}
(function(){
    ${fs.readFileSync(fileName).toString('utf-8')}
})()`);

        console.log(typeDef);
    }
}