Compare commits
1 Commits
86953dd9d3
...
f50324f7c9
| Author | SHA1 | Date | |
|---|---|---|---|
|
f50324f7c9
|
2
Justfile
2
Justfile
@@ -12,4 +12,4 @@
|
|||||||
|
|
||||||
[no-exit-message]
|
[no-exit-message]
|
||||||
@evalr expr:
|
@evalr expr:
|
||||||
RUST_LOG=info cargo run --bin eval --release -- '{{expr}}'
|
RUST_LOG=silent cargo run --bin eval --release -- '{{expr}}'
|
||||||
|
|||||||
@@ -31,7 +31,6 @@ export const hasAttr =
|
|||||||
(set: NixValue): boolean =>
|
(set: NixValue): boolean =>
|
||||||
Object.hasOwn(forceAttrs(set), forceStringValue(s));
|
Object.hasOwn(forceAttrs(set), forceStringValue(s));
|
||||||
|
|
||||||
let counter = 0;
|
|
||||||
export const mapAttrs =
|
export const mapAttrs =
|
||||||
(f: NixValue) =>
|
(f: NixValue) =>
|
||||||
(attrs: NixValue): NixAttrs => {
|
(attrs: NixValue): NixAttrs => {
|
||||||
@@ -39,8 +38,7 @@ export const mapAttrs =
|
|||||||
const forcedF = forceFunction(f);
|
const forcedF = forceFunction(f);
|
||||||
const newAttrs: NixAttrs = {};
|
const newAttrs: NixAttrs = {};
|
||||||
for (const key in forcedAttrs) {
|
for (const key in forcedAttrs) {
|
||||||
newAttrs[key] = createThunk(() => forceFunction(forcedF(key))(forcedAttrs[key]), `created by mapAttrs (${counter})`);
|
newAttrs[key] = createThunk(() => forceFunction(forcedF(key))(forcedAttrs[key]), "created by mapAttrs");
|
||||||
counter += 1;
|
|
||||||
}
|
}
|
||||||
return newAttrs;
|
return newAttrs;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -351,6 +351,18 @@ export const derivationStrict = (args: NixValue): NixAttrs => {
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const specialAttrs = new Set([
|
||||||
|
"name",
|
||||||
|
"builder",
|
||||||
|
"system",
|
||||||
|
"args",
|
||||||
|
"outputs",
|
||||||
|
"__structuredAttrs",
|
||||||
|
"__ignoreNulls",
|
||||||
|
"__contentAddressed",
|
||||||
|
"impure",
|
||||||
|
]);
|
||||||
|
|
||||||
export const derivation = (args: NixValue): NixAttrs => {
|
export const derivation = (args: NixValue): NixAttrs => {
|
||||||
const attrs = forceAttrs(args);
|
const attrs = forceAttrs(args);
|
||||||
const strict = derivationStrict(args);
|
const strict = derivationStrict(args);
|
||||||
@@ -364,18 +376,6 @@ export const derivation = (args: NixValue): NixAttrs => {
|
|||||||
const ignoreNulls = "__ignoreNulls" in attrs ? force(attrs.__ignoreNulls) === true : false;
|
const ignoreNulls = "__ignoreNulls" in attrs ? force(attrs.__ignoreNulls) === true : false;
|
||||||
const drvArgs = extractArgs(attrs, collectedContext);
|
const drvArgs = extractArgs(attrs, collectedContext);
|
||||||
|
|
||||||
const specialAttrs = new Set([
|
|
||||||
"name",
|
|
||||||
"builder",
|
|
||||||
"system",
|
|
||||||
"args",
|
|
||||||
"outputs",
|
|
||||||
"__structuredAttrs",
|
|
||||||
"__ignoreNulls",
|
|
||||||
"__contentAddressed",
|
|
||||||
"impure",
|
|
||||||
]);
|
|
||||||
|
|
||||||
const baseAttrs: NixAttrs = {
|
const baseAttrs: NixAttrs = {
|
||||||
type: "derivation",
|
type: "derivation",
|
||||||
drvPath: strict.drvPath,
|
drvPath: strict.drvPath,
|
||||||
|
|||||||
@@ -18,7 +18,8 @@ import * as misc from "./misc";
|
|||||||
import * as derivation from "./derivation";
|
import * as derivation from "./derivation";
|
||||||
|
|
||||||
import type { NixValue } from "../types";
|
import type { NixValue } from "../types";
|
||||||
import { createThunk, force } from "../thunk";
|
import { createThunk, force, isThunk } from "../thunk";
|
||||||
|
import { getTos } from "../helpers";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Symbol used to mark functions as primops (primitive operations)
|
* Symbol used to mark functions as primops (primitive operations)
|
||||||
@@ -263,4 +264,9 @@ export const builtins: any = {
|
|||||||
nixPath: [],
|
nixPath: [],
|
||||||
nixVersion: "2.31.2",
|
nixVersion: "2.31.2",
|
||||||
storeDir: "INVALID_PATH",
|
storeDir: "INVALID_PATH",
|
||||||
|
|
||||||
|
__traceCaller: (e: NixValue) => {
|
||||||
|
console.log(`traceCaller: ${getTos()}`)
|
||||||
|
return e
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -12,7 +12,8 @@ import { getPathValue } from "../path";
|
|||||||
import type { NixStringContext, StringWithContext } from "../string-context";
|
import type { NixStringContext, StringWithContext } from "../string-context";
|
||||||
import { mkStringWithContext } from "../string-context";
|
import { mkStringWithContext } from "../string-context";
|
||||||
import { isPath } from "./type-check";
|
import { isPath } from "./type-check";
|
||||||
import { getCorepkg } from "../corepkgs";
|
|
||||||
|
const importCache = new Map<string, NixValue>();
|
||||||
|
|
||||||
export const importFunc = (path: NixValue): NixValue => {
|
export const importFunc = (path: NixValue): NixValue => {
|
||||||
const context: NixStringContext = new Set();
|
const context: NixStringContext = new Set();
|
||||||
@@ -30,9 +31,17 @@ Dependency tracking for imported derivations may be incomplete.`,
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const cached = importCache.get(pathStr);
|
||||||
|
if (cached !== undefined) {
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
|
|
||||||
// Call Rust op - returns JS code string
|
// Call Rust op - returns JS code string
|
||||||
const code = Deno.core.ops.op_import(pathStr);
|
const code = Deno.core.ops.op_import(pathStr);
|
||||||
return Function(`return (${code})`)();
|
const result = Function(`return (${code})`)();
|
||||||
|
|
||||||
|
importCache.set(pathStr, result);
|
||||||
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const scopedImport =
|
export const scopedImport =
|
||||||
@@ -452,13 +461,8 @@ export const findFile =
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (lookupPathStr.startsWith("nix/")) {
|
if (lookupPathStr.startsWith("nix/")) {
|
||||||
const corepkgName = lookupPathStr.substring(4);
|
|
||||||
const corepkgContent = getCorepkg(corepkgName);
|
|
||||||
|
|
||||||
if (corepkgContent !== undefined) {
|
|
||||||
// FIXME: special path type
|
// FIXME: special path type
|
||||||
return { [IS_PATH]: true, value: `<nix/${corepkgName}>` };
|
return { [IS_PATH]: true, value: `<${lookupPathStr}>` };
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
throw new CatchableError(`file '${lookupPathStr}' was not found in the Nix search path`);
|
throw new CatchableError(`file '${lookupPathStr}' was not found in the Nix search path`);
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
* Miscellaneous builtin functions
|
* Miscellaneous builtin functions
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { createThunk, force } from "../thunk";
|
import { force } from "../thunk";
|
||||||
import { CatchableError } from "../types";
|
import { CatchableError } from "../types";
|
||||||
import type { NixAttrs, NixBool, NixStrictValue, NixValue } from "../types";
|
import type { NixAttrs, NixBool, NixStrictValue, NixValue } from "../types";
|
||||||
import { forceList, forceAttrs, forceFunction, forceStringValue, forceString, forceStringNoCtx } from "../type-assert";
|
import { forceList, forceAttrs, forceFunction, forceStringValue, forceString, forceStringNoCtx } from "../type-assert";
|
||||||
@@ -20,7 +20,8 @@ import {
|
|||||||
export const addErrorContext =
|
export const addErrorContext =
|
||||||
(e1: NixValue) =>
|
(e1: NixValue) =>
|
||||||
(e2: NixValue): NixValue => {
|
(e2: NixValue): NixValue => {
|
||||||
console.log("[WARNING]: addErrorContext not implemented");
|
// FIXME:
|
||||||
|
// console.log("[WARNING]: addErrorContext not implemented");
|
||||||
return e2;
|
return e2;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,73 +0,0 @@
|
|||||||
export const FETCHURL_NIX = `{
|
|
||||||
system ? "", # obsolete
|
|
||||||
url,
|
|
||||||
hash ? "", # an SRI hash
|
|
||||||
|
|
||||||
# Legacy hash specification
|
|
||||||
md5 ? "",
|
|
||||||
sha1 ? "",
|
|
||||||
sha256 ? "",
|
|
||||||
sha512 ? "",
|
|
||||||
outputHash ?
|
|
||||||
if hash != "" then
|
|
||||||
hash
|
|
||||||
else if sha512 != "" then
|
|
||||||
sha512
|
|
||||||
else if sha1 != "" then
|
|
||||||
sha1
|
|
||||||
else if md5 != "" then
|
|
||||||
md5
|
|
||||||
else
|
|
||||||
sha256,
|
|
||||||
outputHashAlgo ?
|
|
||||||
if hash != "" then
|
|
||||||
""
|
|
||||||
else if sha512 != "" then
|
|
||||||
"sha512"
|
|
||||||
else if sha1 != "" then
|
|
||||||
"sha1"
|
|
||||||
else if md5 != "" then
|
|
||||||
"md5"
|
|
||||||
else
|
|
||||||
"sha256",
|
|
||||||
|
|
||||||
executable ? false,
|
|
||||||
unpack ? false,
|
|
||||||
name ? baseNameOf (toString url),
|
|
||||||
impure ? false,
|
|
||||||
}:
|
|
||||||
|
|
||||||
derivation (
|
|
||||||
{
|
|
||||||
builder = "builtin:fetchurl";
|
|
||||||
|
|
||||||
# New-style output content requirements.
|
|
||||||
outputHashMode = if unpack || executable then "recursive" else "flat";
|
|
||||||
|
|
||||||
inherit
|
|
||||||
name
|
|
||||||
url
|
|
||||||
executable
|
|
||||||
unpack
|
|
||||||
;
|
|
||||||
|
|
||||||
system = "builtin";
|
|
||||||
|
|
||||||
# No need to double the amount of network traffic
|
|
||||||
preferLocalBuild = true;
|
|
||||||
|
|
||||||
# This attribute does nothing; it's here to avoid changing evaluation results.
|
|
||||||
impureEnvVars = [
|
|
||||||
"http_proxy"
|
|
||||||
"https_proxy"
|
|
||||||
"ftp_proxy"
|
|
||||||
"all_proxy"
|
|
||||||
"no_proxy"
|
|
||||||
];
|
|
||||||
|
|
||||||
# To make "nix-prefetch-url" work.
|
|
||||||
urls = [ url ];
|
|
||||||
}
|
|
||||||
// (if impure then { __impure = true; } else { inherit outputHashAlgo outputHash; })
|
|
||||||
)
|
|
||||||
`;
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
import { FETCHURL_NIX } from "./fetchurl.nix";
|
|
||||||
|
|
||||||
export const COREPKGS: Record<string, string> = {
|
|
||||||
"fetchurl.nix": FETCHURL_NIX,
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getCorepkg = (name: string): string | undefined => {
|
|
||||||
return COREPKGS[name];
|
|
||||||
};
|
|
||||||
@@ -19,12 +19,10 @@ interface StackFrame {
|
|||||||
const callStack: StackFrame[] = [];
|
const callStack: StackFrame[] = [];
|
||||||
const MAX_STACK_DEPTH = 1000;
|
const MAX_STACK_DEPTH = 1000;
|
||||||
|
|
||||||
export const STACK_TRACE = { enabled: false };
|
|
||||||
|
|
||||||
function enrichError(error: unknown): Error {
|
function enrichError(error: unknown): Error {
|
||||||
const err = error instanceof Error ? error : new Error(String(error));
|
const err = error instanceof Error ? error : new Error(String(error));
|
||||||
|
|
||||||
if (!STACK_TRACE.enabled || callStack.length === 0) {
|
if (callStack.length === 0) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,13 +36,17 @@ function enrichError(error: unknown): Error {
|
|||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const getTos = (): string => {
|
||||||
|
const tos = callStack[callStack.length - 2];
|
||||||
|
const { file, line, column } = Deno.core.ops.op_decode_span(tos.span);
|
||||||
|
return `${tos.message} at ${file}:${line}:${column}`;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Push an error context onto the stack
|
* Push an error context onto the stack
|
||||||
* Used for tracking evaluation context (e.g., "while evaluating the condition")
|
* Used for tracking evaluation context (e.g., "while evaluating the condition")
|
||||||
*/
|
*/
|
||||||
export const pushContext = (message: string, span: string): void => {
|
export const pushContext = (message: string, span: string): void => {
|
||||||
if (!STACK_TRACE.enabled) return;
|
|
||||||
|
|
||||||
if (callStack.length >= MAX_STACK_DEPTH) {
|
if (callStack.length >= MAX_STACK_DEPTH) {
|
||||||
callStack.shift();
|
callStack.shift();
|
||||||
}
|
}
|
||||||
@@ -55,7 +57,6 @@ export const pushContext = (message: string, span: string): void => {
|
|||||||
* Pop an error context from the stack
|
* Pop an error context from the stack
|
||||||
*/
|
*/
|
||||||
export const popContext = (): void => {
|
export const popContext = (): void => {
|
||||||
if (!STACK_TRACE.enabled) return;
|
|
||||||
callStack.pop();
|
callStack.pop();
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -64,10 +65,6 @@ export const popContext = (): void => {
|
|||||||
* Automatically pushes context before execution and pops after
|
* Automatically pushes context before execution and pops after
|
||||||
*/
|
*/
|
||||||
export const withContext = <T>(message: string, span: string, fn: () => T): T => {
|
export const withContext = <T>(message: string, span: string, fn: () => T): T => {
|
||||||
if (!STACK_TRACE.enabled) {
|
|
||||||
return fn();
|
|
||||||
}
|
|
||||||
|
|
||||||
pushContext(message, span);
|
pushContext(message, span);
|
||||||
try {
|
try {
|
||||||
return fn();
|
return fn();
|
||||||
@@ -183,7 +180,7 @@ export const resolvePath = (currentDir: string, path: NixValue): NixPath => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const select = (obj: NixValue, attrpath: NixValue[], span?: string): NixValue => {
|
export const select = (obj: NixValue, attrpath: NixValue[], span?: string): NixValue => {
|
||||||
if (STACK_TRACE.enabled && span) {
|
if (span) {
|
||||||
const pathStrings = attrpath.map((a) => forceStringValue(a));
|
const pathStrings = attrpath.map((a) => forceStringValue(a));
|
||||||
const path = pathStrings.join(".");
|
const path = pathStrings.join(".");
|
||||||
const message = path ? `while selecting attribute [${path}]` : "while selecting attribute";
|
const message = path ? `while selecting attribute [${path}]` : "while selecting attribute";
|
||||||
@@ -229,7 +226,7 @@ export const selectWithDefault = (
|
|||||||
default_val: NixValue,
|
default_val: NixValue,
|
||||||
span?: string,
|
span?: string,
|
||||||
): NixValue => {
|
): NixValue => {
|
||||||
if (STACK_TRACE.enabled && span) {
|
if (span) {
|
||||||
const pathStrings = attrpath.map((a) => forceStringValue(a));
|
const pathStrings = attrpath.map((a) => forceStringValue(a));
|
||||||
const path = pathStrings.join(".");
|
const path = pathStrings.join(".");
|
||||||
const message = path ? `while selecting attribute [${path}]` : "while selecting attribute";
|
const message = path ? `while selecting attribute [${path}]` : "while selecting attribute";
|
||||||
@@ -337,7 +334,7 @@ export const validateParams = (
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const call = (func: NixValue, arg: NixValue, span?: string): NixValue => {
|
export const call = (func: NixValue, arg: NixValue, span?: string): NixValue => {
|
||||||
if (STACK_TRACE.enabled && span) {
|
if (span) {
|
||||||
if (callStack.length >= MAX_STACK_DEPTH) {
|
if (callStack.length >= MAX_STACK_DEPTH) {
|
||||||
callStack.shift();
|
callStack.shift();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ import {
|
|||||||
concatStringsWithContext,
|
concatStringsWithContext,
|
||||||
call,
|
call,
|
||||||
assert,
|
assert,
|
||||||
STACK_TRACE,
|
|
||||||
pushContext,
|
pushContext,
|
||||||
popContext,
|
popContext,
|
||||||
withContext,
|
withContext,
|
||||||
@@ -41,7 +40,6 @@ export const Nix = {
|
|||||||
HAS_CONTEXT,
|
HAS_CONTEXT,
|
||||||
IS_PATH,
|
IS_PATH,
|
||||||
DEBUG_THUNKS,
|
DEBUG_THUNKS,
|
||||||
STACK_TRACE,
|
|
||||||
|
|
||||||
assert,
|
assert,
|
||||||
call,
|
call,
|
||||||
|
|||||||
@@ -224,15 +224,17 @@ export const op = {
|
|||||||
const attrsA = av as NixAttrs;
|
const attrsA = av as NixAttrs;
|
||||||
const attrsB = bv as NixAttrs;
|
const attrsB = bv as NixAttrs;
|
||||||
|
|
||||||
// If both denote a derivation (type = "derivation"), compare their outPaths
|
// Derivation comparison: compare outPaths only
|
||||||
const isDerivationA = "type" in attrsA && force(attrsA.type) === "derivation";
|
// Safe to force 'type' because it's always a string literal, never a computed value
|
||||||
const isDerivationB = "type" in attrsB && force(attrsB.type) === "derivation";
|
if ("type" in attrsA && "type" in attrsB) {
|
||||||
|
const typeValA = force(attrsA.type);
|
||||||
if (isDerivationA && isDerivationB) {
|
const typeValB = force(attrsB.type);
|
||||||
|
if (typeValA === "derivation" && typeValB === "derivation") {
|
||||||
if ("outPath" in attrsA && "outPath" in attrsB) {
|
if ("outPath" in attrsA && "outPath" in attrsB) {
|
||||||
return op.eq(attrsA.outPath, attrsB.outPath);
|
return op.eq(attrsA.outPath, attrsB.outPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Otherwise, compare attributes one by one
|
// Otherwise, compare attributes one by one
|
||||||
const keysA = Object.keys(attrsA).sort();
|
const keysA = Object.keys(attrsA).sort();
|
||||||
|
|||||||
@@ -12,8 +12,9 @@ import type { NixValue, NixThunkInterface, NixStrictValue } from "./types";
|
|||||||
export const IS_THUNK = Symbol("is_thunk");
|
export const IS_THUNK = Symbol("is_thunk");
|
||||||
|
|
||||||
const forceStack: NixThunk[] = [];
|
const forceStack: NixThunk[] = [];
|
||||||
|
const MAX_FORCE_DEPTH = 1000;
|
||||||
|
|
||||||
export const DEBUG_THUNKS = { enabled: false };
|
export const DEBUG_THUNKS = { enabled: true };
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* NixThunk class - represents a lazy, unevaluated expression
|
* NixThunk class - represents a lazy, unevaluated expression
|
||||||
@@ -97,14 +98,29 @@ export const force = (value: NixValue): NixStrictValue => {
|
|||||||
|
|
||||||
if (DEBUG_THUNKS.enabled) {
|
if (DEBUG_THUNKS.enabled) {
|
||||||
forceStack.push(thunk);
|
forceStack.push(thunk);
|
||||||
|
if (forceStack.length > MAX_FORCE_DEPTH) {
|
||||||
|
let msg = `force depth exceeded ${MAX_FORCE_DEPTH} at ${thunk}\n`;
|
||||||
|
msg += "Force chain (most recent first):\n";
|
||||||
|
for (let i = forceStack.length - 1; i >= Math.max(0, forceStack.length - 20); i--) {
|
||||||
|
const t = forceStack[i];
|
||||||
|
msg += ` ${i + 1}. ${t}`;
|
||||||
|
msg += "\n";
|
||||||
|
}
|
||||||
|
throw new Error(msg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const result = force(func());
|
const result = force(func());
|
||||||
thunk.result = result;
|
thunk.result = result;
|
||||||
return result;
|
return result;
|
||||||
|
} catch (e) {
|
||||||
|
thunk.func = func;
|
||||||
|
throw e;
|
||||||
} finally {
|
} finally {
|
||||||
|
if (DEBUG_THUNKS.enabled) {
|
||||||
forceStack.pop();
|
forceStack.pop();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -11,9 +11,6 @@ pub(crate) fn compile(expr: &Ir, ctx: &impl CodegenContext) -> String {
|
|||||||
if std::env::var("NIX_JS_DEBUG_THUNKS").is_ok() {
|
if std::env::var("NIX_JS_DEBUG_THUNKS").is_ok() {
|
||||||
debug_flags.push("Nix.DEBUG_THUNKS.enabled=true");
|
debug_flags.push("Nix.DEBUG_THUNKS.enabled=true");
|
||||||
}
|
}
|
||||||
if std::env::var("NIX_JS_STACK_TRACE").is_ok() {
|
|
||||||
debug_flags.push("Nix.STACK_TRACE.enabled=true");
|
|
||||||
}
|
|
||||||
let debug_prefix = if debug_flags.is_empty() {
|
let debug_prefix = if debug_flags.is_empty() {
|
||||||
String::new()
|
String::new()
|
||||||
} else {
|
} else {
|
||||||
@@ -97,17 +94,11 @@ impl<Ctx: CodegenContext> Compile<Ctx> for Ir {
|
|||||||
let cond_code = ctx.get_ir(cond).compile(ctx);
|
let cond_code = ctx.get_ir(cond).compile(ctx);
|
||||||
let consq = ctx.get_ir(consq).compile(ctx);
|
let consq = ctx.get_ir(consq).compile(ctx);
|
||||||
let alter = ctx.get_ir(alter).compile(ctx);
|
let alter = ctx.get_ir(alter).compile(ctx);
|
||||||
|
|
||||||
// Only add context tracking if STACK_TRACE is enabled
|
|
||||||
if std::env::var("NIX_JS_STACK_TRACE").is_ok() {
|
|
||||||
let cond_span = encode_span(ctx.get_ir(cond).span(), ctx);
|
let cond_span = encode_span(ctx.get_ir(cond).span(), ctx);
|
||||||
format!(
|
format!(
|
||||||
"(Nix.withContext(\"while evaluating a branch condition\",{},()=>Nix.forceBool({})))?({}):({})",
|
"(Nix.withContext(\"while evaluating a branch condition\",{},()=>Nix.forceBool({})))?({}):({})",
|
||||||
cond_span, cond_code, consq, alter
|
cond_span, cond_code, consq, alter
|
||||||
)
|
)
|
||||||
} else {
|
|
||||||
format!("Nix.forceBool({cond_code})?({consq}):({alter})")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Ir::BinOp(x) => x.compile(ctx),
|
Ir::BinOp(x) => x.compile(ctx),
|
||||||
Ir::UnOp(x) => x.compile(ctx),
|
Ir::UnOp(x) => x.compile(ctx),
|
||||||
@@ -149,9 +140,6 @@ impl<Ctx: CodegenContext> Compile<Ctx> for Ir {
|
|||||||
}) => {
|
}) => {
|
||||||
let assertion_code = ctx.get_ir(assertion).compile(ctx);
|
let assertion_code = ctx.get_ir(assertion).compile(ctx);
|
||||||
let expr = ctx.get_ir(expr).compile(ctx);
|
let expr = ctx.get_ir(expr).compile(ctx);
|
||||||
|
|
||||||
// Only add context tracking if STACK_TRACE is enabled
|
|
||||||
if std::env::var("NIX_JS_STACK_TRACE").is_ok() {
|
|
||||||
let assertion_span = encode_span(ctx.get_ir(assertion).span(), ctx);
|
let assertion_span = encode_span(ctx.get_ir(assertion).span(), ctx);
|
||||||
let span = encode_span(span, ctx);
|
let span = encode_span(span, ctx);
|
||||||
format!(
|
format!(
|
||||||
@@ -162,14 +150,6 @@ impl<Ctx: CodegenContext> Compile<Ctx> for Ir {
|
|||||||
assertion_raw.escape_quote(),
|
assertion_raw.escape_quote(),
|
||||||
span
|
span
|
||||||
)
|
)
|
||||||
} else {
|
|
||||||
format!(
|
|
||||||
"Nix.assert({},{},{})",
|
|
||||||
assertion_code,
|
|
||||||
expr,
|
|
||||||
assertion_raw.escape_quote()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Ir::CurPos(cur_pos) => {
|
Ir::CurPos(cur_pos) => {
|
||||||
let span_str = encode_span(cur_pos.span, ctx);
|
let span_str = encode_span(cur_pos.span, ctx);
|
||||||
@@ -186,20 +166,12 @@ impl<Ctx: CodegenContext> Compile<Ctx> for BinOp {
|
|||||||
let lhs = ctx.get_ir(self.lhs).compile(ctx);
|
let lhs = ctx.get_ir(self.lhs).compile(ctx);
|
||||||
let rhs = ctx.get_ir(self.rhs).compile(ctx);
|
let rhs = ctx.get_ir(self.rhs).compile(ctx);
|
||||||
|
|
||||||
// Only add context tracking if STACK_TRACE is enabled
|
|
||||||
let stack_trace_enabled = std::env::var("NIX_JS_STACK_TRACE").is_ok();
|
|
||||||
|
|
||||||
// Helper to wrap operation with context (only if enabled)
|
|
||||||
let with_ctx = |op_name: &str, op_call: String| {
|
let with_ctx = |op_name: &str, op_call: String| {
|
||||||
if stack_trace_enabled {
|
|
||||||
let span = encode_span(self.span, ctx);
|
let span = encode_span(self.span, ctx);
|
||||||
format!(
|
format!(
|
||||||
"Nix.withContext(\"while evaluating the {} operator\",{},()=>({}))",
|
"Nix.withContext(\"while evaluating the {} operator\",{},()=>({}))",
|
||||||
op_name, span, op_call
|
op_name, span, op_call
|
||||||
)
|
)
|
||||||
} else {
|
|
||||||
op_call
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
match self.kind {
|
match self.kind {
|
||||||
@@ -308,13 +280,9 @@ impl<Ctx: CodegenContext> Compile<Ctx> for Let {
|
|||||||
let info = &self.binding_sccs;
|
let info = &self.binding_sccs;
|
||||||
let mut js_statements = Vec::new();
|
let mut js_statements = Vec::new();
|
||||||
|
|
||||||
for (scc_exprs, is_recursive) in info.sccs.iter() {
|
for (scc_exprs, _is_recursive) in info.sccs.iter() {
|
||||||
for &expr in scc_exprs {
|
for &expr in scc_exprs {
|
||||||
let value = if *is_recursive {
|
let value = unwrap_thunk(ctx.get_ir(expr), ctx);
|
||||||
ctx.get_ir(expr).compile(ctx)
|
|
||||||
} else {
|
|
||||||
unwrap_thunk(ctx.get_ir(expr), ctx)
|
|
||||||
};
|
|
||||||
js_statements.push(format!("const expr{}={}", expr.0, value));
|
js_statements.push(format!("const expr{}={}", expr.0, value));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -351,21 +319,16 @@ impl<Ctx: CodegenContext> Compile<Ctx> for AttrSet {
|
|||||||
fn compile(&self, ctx: &Ctx) -> String {
|
fn compile(&self, ctx: &Ctx) -> String {
|
||||||
let mut attrs = Vec::new();
|
let mut attrs = Vec::new();
|
||||||
let mut attr_positions = Vec::new();
|
let mut attr_positions = Vec::new();
|
||||||
let stack_trace_enabled = std::env::var("NIX_JS_STACK_TRACE").is_ok();
|
|
||||||
|
|
||||||
for (&sym, &(expr, attr_span)) in &self.stcs {
|
for (&sym, &(expr, attr_span)) in &self.stcs {
|
||||||
let key = ctx.get_sym(sym);
|
let key = ctx.get_sym(sym);
|
||||||
let value_code = ctx.get_ir(expr).compile(ctx);
|
let value_code = ctx.get_ir(expr).compile(ctx);
|
||||||
|
|
||||||
let value = if stack_trace_enabled {
|
|
||||||
let value_span = encode_span(ctx.get_ir(expr).span(), ctx);
|
let value_span = encode_span(ctx.get_ir(expr).span(), ctx);
|
||||||
format!(
|
let value = format!(
|
||||||
"Nix.withContext(\"while evaluating the attribute '{}'\",{},()=>({}))",
|
"Nix.withContext(\"while evaluating the attribute '{}'\",{},()=>({}))",
|
||||||
key, value_span, value_code
|
key, value_span, value_code
|
||||||
)
|
);
|
||||||
} else {
|
|
||||||
value_code
|
|
||||||
};
|
|
||||||
attrs.push(format!("{}:{}", key.escape_quote(), value));
|
attrs.push(format!("{}:{}", key.escape_quote(), value));
|
||||||
|
|
||||||
let attr_pos_str = encode_span(attr_span, ctx);
|
let attr_pos_str = encode_span(attr_span, ctx);
|
||||||
@@ -381,15 +344,11 @@ impl<Ctx: CodegenContext> Compile<Ctx> for AttrSet {
|
|||||||
let val_expr = ctx.get_ir(*val);
|
let val_expr = ctx.get_ir(*val);
|
||||||
let val = val_expr.compile(ctx);
|
let val = val_expr.compile(ctx);
|
||||||
let span = val_expr.span();
|
let span = val_expr.span();
|
||||||
let val = if stack_trace_enabled {
|
|
||||||
let span = encode_span(span, ctx);
|
let span = encode_span(span, ctx);
|
||||||
format!(
|
let val = format!(
|
||||||
"Nix.withContext(\"while evaluating a dynamic attribute\",{},()=>({}))",
|
"Nix.withContext(\"while evaluating a dynamic attribute\",{},()=>({}))",
|
||||||
span, val
|
span, val
|
||||||
)
|
);
|
||||||
} else {
|
|
||||||
val
|
|
||||||
};
|
|
||||||
let dyn_span_str = encode_span(*attr_span, ctx);
|
let dyn_span_str = encode_span(*attr_span, ctx);
|
||||||
(key, val, dyn_span_str)
|
(key, val, dyn_span_str)
|
||||||
})
|
})
|
||||||
@@ -416,23 +375,17 @@ impl<Ctx: CodegenContext> Compile<Ctx> for AttrSet {
|
|||||||
|
|
||||||
impl<Ctx: CodegenContext> Compile<Ctx> for List {
|
impl<Ctx: CodegenContext> Compile<Ctx> for List {
|
||||||
fn compile(&self, ctx: &Ctx) -> String {
|
fn compile(&self, ctx: &Ctx) -> String {
|
||||||
let stack_trace_enabled = std::env::var("NIX_JS_STACK_TRACE").is_ok();
|
|
||||||
|
|
||||||
let list = self
|
let list = self
|
||||||
.items
|
.items
|
||||||
.iter()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.map(|(idx, item)| {
|
.map(|(idx, item)| {
|
||||||
let item_code = ctx.get_ir(*item).compile(ctx);
|
let item_code = ctx.get_ir(*item).compile(ctx);
|
||||||
if stack_trace_enabled {
|
|
||||||
let item_span = encode_span(ctx.get_ir(*item).span(), ctx);
|
let item_span = encode_span(ctx.get_ir(*item).span(), ctx);
|
||||||
format!(
|
format!(
|
||||||
"Nix.withContext(\"while evaluating list element {}\",{},()=>({}))",
|
"Nix.withContext(\"while evaluating list element {}\",{},()=>({}))",
|
||||||
idx, item_span, item_code
|
idx, item_span, item_code
|
||||||
)
|
)
|
||||||
} else {
|
|
||||||
item_code
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.join(",");
|
.join(",");
|
||||||
format!("[{list}]")
|
format!("[{list}]")
|
||||||
@@ -441,22 +394,16 @@ impl<Ctx: CodegenContext> Compile<Ctx> for List {
|
|||||||
|
|
||||||
impl<Ctx: CodegenContext> Compile<Ctx> for ConcatStrings {
|
impl<Ctx: CodegenContext> Compile<Ctx> for ConcatStrings {
|
||||||
fn compile(&self, ctx: &Ctx) -> String {
|
fn compile(&self, ctx: &Ctx) -> String {
|
||||||
let stack_trace_enabled = std::env::var("NIX_JS_STACK_TRACE").is_ok();
|
|
||||||
|
|
||||||
let parts: Vec<String> = self
|
let parts: Vec<String> = self
|
||||||
.parts
|
.parts
|
||||||
.iter()
|
.iter()
|
||||||
.map(|part| {
|
.map(|part| {
|
||||||
let part_code = ctx.get_ir(*part).compile(ctx);
|
let part_code = ctx.get_ir(*part).compile(ctx);
|
||||||
if stack_trace_enabled {
|
|
||||||
let part_span = encode_span(ctx.get_ir(*part).span(), ctx);
|
let part_span = encode_span(ctx.get_ir(*part).span(), ctx);
|
||||||
format!(
|
format!(
|
||||||
"Nix.withContext(\"while evaluating a path segment\",{},()=>({}))",
|
"Nix.withContext(\"while evaluating a path segment\",{},()=>({}))",
|
||||||
part_span, part_code
|
part_span, part_code
|
||||||
)
|
)
|
||||||
} else {
|
|
||||||
part_code
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
|||||||
@@ -214,8 +214,8 @@ pub struct StackFrame {
|
|||||||
pub src: NamedSource<Arc<str>>,
|
pub src: NamedSource<Arc<str>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
const MAX_STACK_FRAMES: usize = 10;
|
const MAX_STACK_FRAMES: usize = 20;
|
||||||
const FRAMES_AT_START: usize = 5;
|
const FRAMES_AT_START: usize = 15;
|
||||||
const FRAMES_AT_END: usize = 5;
|
const FRAMES_AT_END: usize = 5;
|
||||||
|
|
||||||
pub(crate) fn parse_js_error(error: Box<JsError>, ctx: &impl RuntimeContext) -> Error {
|
pub(crate) fn parse_js_error(error: Box<JsError>, ctx: &impl RuntimeContext) -> Error {
|
||||||
@@ -234,7 +234,11 @@ pub(crate) fn parse_js_error(error: Box<JsError>, ctx: &impl RuntimeContext) ->
|
|||||||
} else {
|
} else {
|
||||||
(None, None, Vec::new())
|
(None, None, Vec::new())
|
||||||
};
|
};
|
||||||
let stack_trace = truncate_stack_trace(frames);
|
let stack_trace = if std::env::var("NIX_JS_STACK_TRACE").is_ok() {
|
||||||
|
truncate_stack_trace(frames)
|
||||||
|
} else {
|
||||||
|
Vec::new()
|
||||||
|
};
|
||||||
let message = error.get_message().to_string();
|
let message = error.get_message().to_string();
|
||||||
let js_backtrace = error.stack.map(|stack| {
|
let js_backtrace = error.stack.map(|stack| {
|
||||||
stack
|
stack
|
||||||
@@ -272,7 +276,7 @@ fn parse_frames(stack: &str, ctx: &impl RuntimeContext) -> Vec<NixStackFrame> {
|
|||||||
let mut frames = Vec::new();
|
let mut frames = Vec::new();
|
||||||
|
|
||||||
for line in stack.lines() {
|
for line in stack.lines() {
|
||||||
// Format: NIX_STACK_FRAME:start:end[:extra_data]
|
// Format: NIX_STACK_FRAME:source_id:start:end[:extra_data]
|
||||||
let Some(rest) = line.strip_prefix("NIX_STACK_FRAME:") else {
|
let Some(rest) = line.strip_prefix("NIX_STACK_FRAME:") else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -314,7 +314,8 @@ where
|
|||||||
ctx.set_current_binding(Some(slot));
|
ctx.set_current_binding(Some(slot));
|
||||||
|
|
||||||
let default = if let Some(default) = default {
|
let default = if let Some(default) = default {
|
||||||
Some(default.clone().downgrade(ctx)?)
|
let default = default.clone().downgrade(ctx)?;
|
||||||
|
Some(ctx.maybe_thunk(default))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user