Skip to content

Commit

Permalink
Merge pull request #126 from saimeunt/feat-better-runtime-error-handl…
Browse files Browse the repository at this point in the history
…ing-99

highlight runtime errors
  • Loading branch information
mazurroman authored Mar 28, 2024
2 parents 6ac51cc + ab48e53 commit b671a45
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 10 deletions.
3 changes: 3 additions & 0 deletions components/Editor/ExtraColumn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const ExtraColumn = ({
const {
casmInstructions,
activeCasmInstructionIndex,
errorCasmInstructionIndex,
sierraStatements,
casmToSierraMap,
currentSierraVariables,
Expand All @@ -45,13 +46,15 @@ const ExtraColumn = ({
instructions={casmInstructions}
codeType={codeType}
activeIndexes={[activeCasmInstructionIndex]}
errorIndexes={[errorCasmInstructionIndex]}
variables={{}}
/>
) : codeType === CodeType.Sierra ? (
<InstructionsTable
instructions={sierraStatements}
codeType={codeType}
activeIndexes={casmToSierraMap[activeCasmInstructionIndex] ?? []}
errorIndexes={casmToSierraMap[errorCasmInstructionIndex] ?? []}
variables={currentSierraVariables || {}}
/>
) : (
Expand Down
11 changes: 8 additions & 3 deletions components/Editor/InstructionsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ const sierraVariableRe = /\[(\d+)\]/
export const InstructionsTable = ({
instructions,
activeIndexes,
errorIndexes,
variables,
codeType,
}: {
instructions: IInstruction[]
activeIndexes: number[]
errorIndexes: number[]
variables: SierraVariables
codeType: CodeType
}) => {
Expand Down Expand Up @@ -126,15 +128,18 @@ export const InstructionsTable = ({
<tbody>
{instructions.map((instruction, index) => {
const isActive = activeIndexes.includes(index)
const isError = errorIndexes.includes(index)
return (
<tr
ref={(el) => (rowRefs.current[index] = el)}
key={index}
className={cn(
'border-b border-gray-200 dark:border-black-500',
isActive
? 'text-gray-900 dark:text-gray-200'
: 'text-gray-400 dark:text-gray-600',
{
'text-gray-900 dark:text-gray-200': isActive,
'text-gray-400 dark:text-gray-600': !isActive,
'bg-red-100 dark:bg-red-500/10': isError,
},
)}
>
<td className={`pl-6 pr-1 px-2 whitespace-nowrap w-[1%]`}>
Expand Down
5 changes: 5 additions & 0 deletions components/Editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const Editor = ({ readOnly = false }: Props) => {
serializedOutput,
casmInstructions,
activeCasmInstructionIndex,
errorCasmInstructionIndex,
sierraStatements,
casmToSierraMap,
currentSierraVariables,
Expand Down Expand Up @@ -364,6 +365,7 @@ const Editor = ({ readOnly = false }: Props) => {
instructions={casmInstructions}
codeType={codeType}
activeIndexes={[activeCasmInstructionIndex]}
errorIndexes={[errorCasmInstructionIndex]}
variables={{}}
/>
) : codeType === CodeType.Sierra ? (
Expand All @@ -373,6 +375,9 @@ const Editor = ({ readOnly = false }: Props) => {
activeIndexes={
casmToSierraMap[activeCasmInstructionIndex] ?? []
}
errorIndexes={
casmToSierraMap[errorCasmInstructionIndex] ?? []
}
variables={currentSierraVariables || {}}
/>
) : (
Expand Down
33 changes: 27 additions & 6 deletions components/Tracer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { useContext, useEffect, useState, useRef, useReducer } from 'react'
import cn from 'classnames'
import { Priority, useRegisterActions } from 'kbar'

import { CairoVMApiContext, BreakPoints } from 'context/cairoVMApiContext'
import {
CairoVMApiContext,
BreakPoints,
ProgramExecutionState,
} from 'context/cairoVMApiContext'

import Console from '../Editor/Console'

Expand Down Expand Up @@ -54,6 +58,7 @@ enum IConsoleTab {

export const Tracer = () => {
const {
executionState,
tracerData,
breakPoints,
onExecutionStepChange,
Expand All @@ -65,6 +70,10 @@ export const Tracer = () => {

const trace = tracerData?.trace
const currentTraceEntry = tracerData?.trace[executionTraceStepNumber]
const errorTraceEntry =
executionState === ProgramExecutionState.Error
? tracerData?.trace.at(-2)
: null
const currentCallstackEntry = tracerData?.callstack[executionTraceStepNumber]

const [selectedConsoleTab, setSelectedConsoleTab] = useState<IConsoleTab>(
Expand Down Expand Up @@ -190,6 +199,11 @@ export const Tracer = () => {
currentFocus={currentFocus.idx}
breakpoints={breakPoints}
toogleBreakPoint={toogleBreakPoint}
errorTraceEntry={
executionState === ProgramExecutionState.Error
? errorTraceEntry
: null
}
/>
</div>
)}
Expand Down Expand Up @@ -351,15 +365,18 @@ function InstructionsTable({
currentFocus,
breakpoints,
toogleBreakPoint,
errorTraceEntry,
}: {
memory: TracerData['memory']
pcInstMap: TracerData['pcInstMap']
currentTraceEntry: TraceEntry
currentFocus: number
breakpoints: BreakPoints
toogleBreakPoint: (addr: string) => void
errorTraceEntry?: TraceEntry | null
}) {
const { pc, ap, fp } = currentTraceEntry
const errorPc = errorTraceEntry?.pc || 0

const [hoveredAddr, setHoveredAddr] = useState<string>('')

Expand Down Expand Up @@ -387,18 +404,22 @@ function InstructionsTable({
<tbody>
{Object.keys(memory).map((addr) => {
const isCurrent = pc.toString() == addr
const isError = errorPc.toString() == addr
const addrNum = Number(addr)
const isFocus = currentFocus == addrNum
const hasBreakpoint = breakpoints[addr]
return (
<tr
key={addr}
id={isFocus ? 'focus_row' : undefined}
className={`relative border-b border-gray-200 dark:border-black-500 ${
isCurrent
? 'text-gray-900 dark:text-gray-200'
: 'text-gray-400 dark:text-gray-600'
}`}
className={cn(
'relative border-b border-gray-200 dark:border-black-500',
{
'text-gray-900 dark:text-gray-200': isCurrent,
'text-gray-400 dark:text-gray-600': !isCurrent,
'bg-red-100 dark:bg-red-500/10': isError,
},
)}
onMouseEnter={() => setHoveredAddr(addr)}
onMouseLeave={() => setHoveredAddr('')}
>
Expand Down
15 changes: 14 additions & 1 deletion context/cairoVMApiContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type ContextProps = {
tracerData?: TracerData
executionTraceStepNumber: number
activeCasmInstructionIndex: number
errorCasmInstructionIndex: number
sierraStatements: IInstruction[]
casmToSierraMap: CasmToSierraMap
currentTraceEntry?: TraceEntry
Expand All @@ -63,6 +64,7 @@ export const CairoVMApiContext = createContext<ContextProps>({
executionPanicMessage: '',
executionTraceStepNumber: 0,
activeCasmInstructionIndex: 0,
errorCasmInstructionIndex: 0,
sierraStatements: [],
casmToSierraMap: {},
breakPoints: {},
Expand Down Expand Up @@ -105,6 +107,12 @@ export const CairoVMApiProvider: React.FC<PropsWithChildren> = ({
tracerData?.entryToSierraVarsMap[executionTraceStepNumber]
const activeCasmInstructionIndex =
tracerData?.pcToInstIndexesMap[(currentTraceEntry?.pc ?? 0).toString()] ?? 0
const errorTraceEntry =
executionState === ProgramExecutionState.Error
? tracerData?.trace.at(-2)
: null
const errorCasmInstructionIndex =
tracerData?.pcToInstIndexesMap[(errorTraceEntry?.pc ?? 0).toString()] ?? -1

function onExecutionStepChange(stepNumber: number) {
setExecutionTraceStepNumber(stepNumber)
Expand Down Expand Up @@ -169,7 +177,11 @@ export const CairoVMApiProvider: React.FC<PropsWithChildren> = ({
: ProgramExecutionState.Error,
)

setExecutionTraceStepNumber(0)
setExecutionTraceStepNumber(
data.is_execution_successful === true
? 0
: data.tracer_data.trace.length - 2,
)
setCasmCode(data.casm_program_code)
setSierraCode(data.sierra_program_code)
setCairoLangCompilerVersion(data.cairo_lang_compiler_version)
Expand Down Expand Up @@ -225,6 +237,7 @@ export const CairoVMApiProvider: React.FC<PropsWithChildren> = ({
currentTraceEntry,
currentSierraVariables,
activeCasmInstructionIndex,
errorCasmInstructionIndex,
sierraStatements,
casmToSierraMap,
breakPoints,
Expand Down

0 comments on commit b671a45

Please sign in to comment.