imports are more flexible

This commit is contained in:
Olivier Gagnon 2021-12-23 15:55:58 -05:00
parent 16c51e8e8e
commit ed86577d6c
6 changed files with 23 additions and 14 deletions

14
dist/vendor.bundle.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -2,6 +2,7 @@ import { makeRuntimeRejectMsg } from "./NetscriptEvaluator";
import { ScriptUrl } from "./Script/ScriptUrl"; import { ScriptUrl } from "./Script/ScriptUrl";
import { WorkerScript } from "./Netscript/WorkerScript"; import { WorkerScript } from "./Netscript/WorkerScript";
import { Script } from "./Script/Script"; import { Script } from "./Script/Script";
import { areImportsEquals } from "./Terminal/DirectoryHelpers";
export const BlobsMap: { [key: string]: string } = {}; export const BlobsMap: { [key: string]: string } = {};
@ -117,11 +118,11 @@ function _getScriptUrls(script: Script, scripts: Script[], seen: Script[]): Scri
let transformedCode = script.code.replace( let transformedCode = script.code.replace(
/((?:from|import)\s+(?:'|"))(?:\.\/)?([^'"]+)('|")/g, /((?:from|import)\s+(?:'|"))(?:\.\/)?([^'"]+)('|")/g,
(unmodified, prefix, filename, suffix) => { (unmodified, prefix, filename, suffix) => {
const isAllowedImport = scripts.some((s) => s.filename == filename); const isAllowedImport = scripts.some((s) => areImportsEquals(s.filename, filename));
if (!isAllowedImport) return unmodified; if (!isAllowedImport) return unmodified;
// Find the corresponding script. // Find the corresponding script.
const [importedScript] = scripts.filter((s) => s.filename == filename); const [importedScript] = scripts.filter((s) => areImportsEquals(s.filename, filename));
// Try to get a URL for the requested script and its dependencies. // Try to get a URL for the requested script and its dependencies.
const urls = _getScriptUrls(importedScript, scripts, seen); const urls = _getScriptUrls(importedScript, scripts, seen);

@ -13,6 +13,7 @@ import { RamCalculationErrorCode } from "./RamCalculationErrorCodes";
import { RamCosts, RamCostConstants } from "../Netscript/RamCostGenerator"; import { RamCosts, RamCostConstants } from "../Netscript/RamCostGenerator";
import { Script } from "../Script/Script"; import { Script } from "../Script/Script";
import { WorkerScript } from "../Netscript/WorkerScript"; import { WorkerScript } from "../Netscript/WorkerScript";
import { areImportsEquals } from "../Terminal/DirectoryHelpers";
// These special strings are used to reference the presence of a given logical // These special strings are used to reference the presence of a given logical
// construct within a user script. // construct within a user script.
@ -106,8 +107,9 @@ async function parseOnlyRamCalculate(
let script = null; let script = null;
const fn = nextModule.startsWith("./") ? nextModule.slice(2) : nextModule; const fn = nextModule.startsWith("./") ? nextModule.slice(2) : nextModule;
for (const s of otherScripts) { for (const s of otherScripts) {
if (s.filename === fn) { if (areImportsEquals(s.filename, fn)) {
script = s; script = s;
console.log(`${s.filename} ${fn}`);
break; break;
} }
} }

@ -310,3 +310,9 @@ export function areFilesEqual(f0: string, f1: string): boolean {
if (!f1.startsWith("/")) f1 = "/" + f1; if (!f1.startsWith("/")) f1 = "/" + f1;
return f0 === f1; return f0 === f1;
} }
export function areImportsEquals(f0: string, f1: string): boolean {
if (!f0.endsWith(".ns") && !f0.endsWith(".js")) f0 = f0 + ".js";
if (!f1.endsWith(".ns") && !f1.endsWith(".js")) f1 = f1 + ".js";
return areFilesEqual(f0, f1);
}