Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return all values if runtime error #2657

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import { forwardRef } from "react";

import { SquiggleViewer } from "../index.js";
import { SquiggleOutput } from "../lib/hooks/useSquiggle.js";
import { getResultVariables, getResultValue } from "../lib/utility.js";
import {
getResultVariables,
getResultValue,
getResultError,
} from "../lib/utility.js";
import { CodeEditorHandle } from "./CodeEditor/index.js";
import { PartialPlaygroundSettings } from "./PlaygroundSettings.js";
import { SquiggleViewerHandle } from "./SquiggleViewer/index.js";
Expand Down Expand Up @@ -42,6 +46,7 @@ export const DynamicSquiggleViewer = forwardRef<SquiggleViewerHandle, Props>(
ref={viewerRef}
resultVariables={getResultVariables(squiggleOutput)}
resultItem={getResultValue(squiggleOutput)}
resultError={getResultError(squiggleOutput)}
editor={editor}
rootPathOverride={rootPathOverride}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
/** The output of squiggle's run */
resultVariables: result<SqDictValue, SqError>;
resultItem: result<SqValue, SqError> | undefined;
resultError: SqError | undefined;
editor?: CodeEditorHandle;
rootPathOverride?: SqValuePath;
} & PartialPlaygroundSettings;
Expand All @@ -39,9 +40,10 @@
SquiggleViewerHandle,
SquiggleViewerProps
>(function SquiggleViewerOuter(
{ resultVariables, resultItem, rootPathOverride },
{ resultVariables, resultItem, resultError, rootPathOverride },
ref
) {
console.log("ERROR", resultError);

Check warning on line 46 in packages/components/src/components/SquiggleViewer/index.tsx

View workflow job for this annotation

GitHub Actions / Build, test, lint

Unexpected console statement
const { focused, dispatch, getCalculator } = useViewerContext();
const unfocus = useUnfocus();
const focus = useFocus();
Expand Down Expand Up @@ -137,6 +139,7 @@
return (
<div>
{focusedNavigation}
{resultError && <SquiggleErrorAlert error={resultError} />}
{body()}
</div>
);
Expand All @@ -147,6 +150,7 @@
{
resultVariables,
resultItem,
resultError,
editor,
rootPathOverride,
...partialPlaygroundSettings
Expand All @@ -171,6 +175,7 @@
<SquiggleViewerOuter
resultVariables={resultVariables}
resultItem={resultItem}
resultError={resultError}
rootPathOverride={rootPathOverride}
ref={ref}
/>
Expand Down
3 changes: 3 additions & 0 deletions packages/components/src/lib/hooks/useSquiggle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
SqDict,
SqValue,
result,
SqRuntimeError,
} from "@quri/squiggle-lang";
import { WINDOW_VARIABLE_NAME } from "../constants.js";

Expand Down Expand Up @@ -38,6 +39,7 @@
exports: SqDict;
result: SqValue;
bindings: SqDict;
error: SqRuntimeError | undefined;
},
SqError
>;
Expand Down Expand Up @@ -101,6 +103,7 @@
project.setContinues(sourceId, continues);
await project.run(sourceId);
const output = project.getOutput(sourceId);
console.log("output", output);

Check warning on line 106 in packages/components/src/lib/hooks/useSquiggle.ts

View workflow job for this annotation

GitHub Actions / Build, test, lint

Unexpected console statement
const executionTime = Date.now() - startTime;

setSquiggleOutput({
Expand Down
11 changes: 11 additions & 0 deletions packages/components/src/lib/utility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
SqDictValue,
result,
resultMap,
SqRuntimeError,
} from "@quri/squiggle-lang";

import { SquiggleOutput } from "./hooks/useSquiggle.js";
Expand Down Expand Up @@ -59,6 +60,16 @@ export function getResultValue({
}
}

export function getResultError({
output,
}: SquiggleOutput): SqRuntimeError | undefined {
if (output.ok) {
return output.value.error;
} else {
return undefined;
}
}

export function getErrors(result: SquiggleOutput["output"]) {
if (!result.ok) {
return [result.value];
Expand Down
13 changes: 9 additions & 4 deletions packages/squiggle-lang/src/public/SqProject/ProjectItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type RunOutput = {
result: Value;
bindings: Bindings;
exports: Bindings;
error: SqError | undefined;
};

export type Import =
Expand Down Expand Up @@ -220,10 +221,13 @@ export class ProjectItem {
throw new Error("Expected Program expression");
}

const [result, contextAfterEvaluation] = evaluate(expression.value, {
...context,
evaluate: asyncEvaluate,
});
const [result, contextAfterEvaluation, error] = evaluate(
expression.value,
{
...context,
evaluate: asyncEvaluate,
}
);

const bindings = contextAfterEvaluation.stack.asBindings();
const exportNames = new Set(expression.value.value.exports);
Expand All @@ -232,6 +236,7 @@ export class ProjectItem {
result,
bindings,
exports,
error,
});
} catch (e: unknown) {
this.failRun(new SqRuntimeError(IRuntimeError.fromException(e)));
Expand Down
13 changes: 11 additions & 2 deletions packages/squiggle-lang/src/public/SqProject/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { ImmutableMap } from "../../utility/immutableMap.js";
import * as Result from "../../utility/result.js";
import { Value, vDict } from "../../value/index.js";

import { SqError, SqOtherError } from "../SqError.js";
import { SqError, SqOtherError, SqRuntimeError } from "../SqError.js";
import { SqDict } from "../SqValue/SqDict.js";
import { SqValue, wrapValue } from "../SqValue/index.js";
import { SqValueContext } from "../SqValueContext.js";
Expand All @@ -16,6 +16,7 @@ import { SqValuePath } from "../SqValuePath.js";
import { SqLinker } from "../SqLinker.js";
import { SqOutputResult } from "../types.js";
import { Import, ProjectItem, RunOutput } from "./ProjectItem.js";
import { IRuntimeError } from "../../errors/IError.js";

function getNeedToRunError() {
return new SqOtherError("Need to run");
Expand Down Expand Up @@ -267,7 +268,15 @@ export class SqProject {
)
);

return Result.Ok({ result, bindings, exports });
const error = internalOutputR.value.error;
return Result.Ok({
result,
bindings,
exports,
error: error
? new SqRuntimeError(IRuntimeError.fromException(error))
: undefined,
});
}

getResult(sourceId: string): Result.result<SqValue, SqError> {
Expand Down
3 changes: 2 additions & 1 deletion packages/squiggle-lang/src/public/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { result } from "../utility/result.js";
import { SqError } from "./SqError.js";
import { SqError, SqRuntimeError } from "./SqError.js";
import { SqValue } from "./SqValue/index.js";
import { SqDict } from "./SqValue/SqDict.js";

export type SqOutput = {
result: SqValue;
bindings: SqDict;
exports: SqDict;
error: SqRuntimeError | undefined;
};

export type SqOutputResult = result<SqOutput, SqError>;
40 changes: 25 additions & 15 deletions packages/squiggle-lang/src/reducer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
} from "../errors/messages.js";
import { compileAst } from "../expression/compile.js";
import { Expression } from "../expression/index.js";
import { SqError, SqRuntimeError } from "../index.js";
import { getStdLib } from "../library/index.js";
import { ImmutableMap } from "../utility/immutableMap.js";
import * as Result from "../utility/result.js";
Expand All @@ -33,13 +34,13 @@ import { UserDefinedLambdaParameter, UserDefinedLambda } from "./lambda.js";
export type ReducerFn = (
expression: Expression,
context: Context.ReducerContext
) => [Value, Context.ReducerContext];
) => [Value, Context.ReducerContext, SqError | undefined];

type SubReducerFn<T extends Expression["type"] = Expression["type"]> = (
expressionValue: Extract<Expression, { type: T }>["value"],
context: Context.ReducerContext,
ast: ASTNode
) => [Value, Context.ReducerContext];
) => [Value, Context.ReducerContext, SqError | undefined];

function throwFrom(
error: ErrorMessage,
Expand Down Expand Up @@ -104,7 +105,7 @@ const evaluateBlock: SubReducerFn<"Block"> = (statements, context) => {
currentContext
);
}
return [currentValue, context]; // throw away block's context
return [currentValue, context, undefined]; // throw away block's context
};

const evaluateProgram: SubReducerFn<"Program"> = (expressionValue, context) => {
Expand All @@ -113,12 +114,20 @@ const evaluateProgram: SubReducerFn<"Program"> = (expressionValue, context) => {
let currentValue: Value = vVoid();

for (const statement of expressionValue.statements) {
[currentValue, currentContext] = context.evaluate(
statement,
currentContext
);
try {
[currentValue, currentContext] = context.evaluate(
statement,
currentContext
);
} catch (e: unknown) {
return [
currentValue,
currentContext,
new SqRuntimeError(IRuntimeError.fromException(e)),
];
}
}
return [currentValue, currentContext];
return [currentValue, currentContext, undefined];
};

const evaluateArray: SubReducerFn<"Array"> = (expressionValue, context) => {
Expand All @@ -127,7 +136,7 @@ const evaluateArray: SubReducerFn<"Array"> = (expressionValue, context) => {
return value;
});
const value = vArray(values);
return [value, context];
return [value, context, undefined];
};

const evaluateDict: SubReducerFn<"Dict"> = (expressionValue, context, ast) => {
Expand All @@ -148,7 +157,7 @@ const evaluateDict: SubReducerFn<"Dict"> = (expressionValue, context, ast) => {
})
)
);
return [value, context];
return [value, context, undefined];
};

const evaluateAssign: SubReducerFn<"Assign"> = (expressionValue, context) => {
Expand All @@ -163,6 +172,7 @@ const evaluateAssign: SubReducerFn<"Assign"> = (expressionValue, context) => {
evaluate: context.evaluate,
inFunction: context.inFunction,
},
undefined,
];
};

Expand All @@ -171,11 +181,11 @@ const evaluateResolvedSymbol: SubReducerFn<"ResolvedSymbol"> = (
context
) => {
const value = context.stack.get(expressionValue.offset);
return [value, context];
return [value, context, undefined];
};

const evaluateValue: SubReducerFn<"Value"> = (expressionValue, context) => {
return [expressionValue, context];
return [expressionValue, context, undefined];
};

const evaluateTernary: SubReducerFn<"Ternary"> = (
Expand All @@ -199,7 +209,7 @@ const evaluateTernary: SubReducerFn<"Ternary"> = (
predicateResult.value ? expressionValue.ifTrue : expressionValue.ifFalse,
context
);
return [value, context];
return [value, context, undefined];
};

const evaluateLambda: SubReducerFn<"Lambda"> = (
Expand Down Expand Up @@ -246,7 +256,7 @@ const evaluateLambda: SubReducerFn<"Lambda"> = (
ast.location
)
);
return [value, context];
return [value, context, undefined];
};

const evaluateCall: SubReducerFn<"Call"> = (expressionValue, context, ast) => {
Expand All @@ -262,7 +272,7 @@ const evaluateCall: SubReducerFn<"Call"> = (expressionValue, context, ast) => {
context,
ast // we pass the ast of a current expression here, to put it on frameStack and in the resulting value
);
return [result, context];
return [result, context, undefined];
}
default:
return throwFrom(new RENotAFunction(lambda.toString()), context, ast);
Expand Down
Loading