Merge branch 'reducer-dev-F-debugging' into Umur-reducer-dev

This commit is contained in:
Umur Ozkul 2022-04-23 15:21:11 +02:00
commit e456726e53
2 changed files with 40 additions and 4 deletions

View File

@ -0,0 +1,17 @@
open Jest
open Reducer_TestHelpers
/*
You can wrap around any expression with inspect(expr) to log the value of that expression.
This is useful for debugging. inspect(expr) returns the value of expr, but also prints it out.
There is a second version of inspect that takes a label, which will print out the label and the value.
inpsectPerformace(expr, label) will print out the value of expr, the label, and the time it took to evaluate expr.
*/
describe("Debugging", () => {
testEvalToBe("inspect(1)", "Ok(1)")
testEvalToBe("inspect(1, \"one\")", "Ok(1)")
testEvalToBe("inspect(1, \"one\")", "Ok(1)")
testEvalToBe("inspectPerformance(1, \"one\")", "Ok(1)")
})

View File

@ -43,16 +43,35 @@ let callInternal = (call: functionCall): result<'b, errorValue> => {
| None => RERecordPropertyNotFound("Record property not found", sIndex)->Error
}
let inspect = (value: expressionValue) => {
Js.log(`${value->toString}`)
value->Ok
}
let inspectLabel = (value: expressionValue, label: string) => {
Js.log(`${label}: ${value->toString}`)
value->Ok
}
let inspectPerformance = (value: expressionValue, label: string) => {
let _ = %raw("{performance} = require('perf_hooks')")
let start = %raw(`performance.now()`)
let finish = %raw(`performance.now()`)
let performance = finish - start
Js.log(`${label}: ${value->toString} performance: ${Js.String.make(performance)}ms`)
value->Ok
}
switch call {
// | ("$constructRecord", pairArray)
// | ("$atIndex", [EvArray(anArray), EvNumber(fIndex)]) => arrayAtIndex(anArray, fIndex)
// | ("$atIndex", [EvRecord(aRecord), EvString(sIndex)]) => recordAtIndex(aRecord, sIndex)
| ("$constructRecord", [EvArray(arrayOfPairs)]) => constructRecord(arrayOfPairs)
| ("$atIndex", [EvArray(aValueArray), EvArray([EvNumber(fIndex)])]) =>
arrayAtIndex(aValueArray, fIndex)
| ("$atIndex", [EvRecord(dict), EvArray([EvString(sIndex)])]) => recordAtIndex(dict, sIndex)
| ("$atIndex", [obj, index]) =>
(toStringWithType(obj) ++ "??~~~~" ++ toStringWithType(index))->EvString->Ok
| ("$constructRecord", [EvArray(arrayOfPairs)]) => constructRecord(arrayOfPairs)
| ("inspect", [value, EvString(label)]) => inspectLabel(value, label)
| ("inspect", [value]) => inspect(value)
| ("inspectPerformance", [value, EvString(label)]) => inspectPerformance(value, label)
| call => callMathJs(call)
}
}