@@ -84,12 +84,16 @@ func panicOrGoexit(message interface{}, panicking panicState) {
8484
8585// Cause a runtime panic, which is (currently) always a string.
8686func runtimePanic (msg string ) {
87- // As long as this function is inined , llvm.returnaddress(0) will return
87+ // As long as this function is inlined , llvm.returnaddress(0) will return
8888 // something sensible.
89- runtimePanicAt (returnAddress (0 ), msg )
89+ runtimePanicAtMsg (returnAddress (0 ), & errRuntime , msg )
9090}
9191
92- func runtimePanicAt (addr unsafe.Pointer , msg string ) {
92+ func runtimePanicAt (addr unsafe.Pointer , err * plainError ) {
93+ runtimePanicAtMsg (addr , err , err .Error ())
94+ }
95+
96+ func runtimePanicAtMsg (addr unsafe.Pointer , err * plainError , msg string ) {
9397 if panicStrategy () == tinygo .PanicStrategyTrap {
9498 trap ()
9599 }
@@ -98,7 +102,7 @@ func runtimePanicAt(addr unsafe.Pointer, msg string) {
98102 if frame != nil {
99103 // Use the normal panic mechanism so that this runtime error
100104 // can be recovered with recover().
101- frame .PanicValue = plainError ( msg )
105+ frame .PanicValue = err
102106 frame .Panicking = panicTrue
103107 tinygo_longjmp (frame )
104108 // unreachable
@@ -131,7 +135,7 @@ func setupDeferFrame(frame *deferFrame, jumpSP unsafe.Pointer) {
131135 // Defer is not currently allowed in interrupts.
132136 // We could add support for this, but since defer might also allocate
133137 // (especially in loops) it might not be a good idea anyway.
134- runtimePanicAt (returnAddress (0 ), "defer in interrupt" )
138+ runtimePanicAt (returnAddress (0 ), & errDeferInInterrupt )
135139 }
136140 currentTask := task .Current ()
137141 frame .Previous = (* deferFrame )(currentTask .DeferFrame )
@@ -201,54 +205,54 @@ func _recover(useParentFrame bool) interface{} {
201205
202206// Panic when trying to dereference a nil pointer.
203207func nilPanic () {
204- runtimePanicAt (returnAddress (0 ), "nil pointer dereference" )
208+ runtimePanicAt (returnAddress (0 ), & errNilPointerDereference )
205209}
206210
207211// Panic when trying to add an entry to a nil map
208212func nilMapPanic () {
209- runtimePanicAt (returnAddress (0 ), "assignment to entry in nil map" )
213+ runtimePanicAt (returnAddress (0 ), & errAssignmentToEntryInNilMap )
210214}
211215
212216// Panic when trying to access an array or slice out of bounds.
213217func lookupPanic () {
214- runtimePanicAt (returnAddress (0 ), "index out of range" )
218+ runtimePanicAt (returnAddress (0 ), & errIndexOutOfRange )
215219}
216220
217221// Panic when trying to slice a slice out of bounds.
218222func slicePanic () {
219- runtimePanicAt (returnAddress (0 ), "slice out of range" )
223+ runtimePanicAt (returnAddress (0 ), & errSliceOutOfRange )
220224}
221225
222226// Panic when trying to convert a slice to an array pointer (Go 1.17+) and the
223227// slice is shorter than the array.
224228func sliceToArrayPointerPanic () {
225- runtimePanicAt (returnAddress (0 ), "slice smaller than array" )
229+ runtimePanicAt (returnAddress (0 ), & errSliceSmallerThanArray )
226230}
227231
228232// Panic when calling unsafe.Slice() (Go 1.17+) or unsafe.String() (Go 1.20+)
229233// with a len that's too large (which includes if the ptr is nil and len is
230234// nonzero).
231235func unsafeSlicePanic () {
232- runtimePanicAt (returnAddress (0 ), "unsafe.Slice/String: len out of range" )
236+ runtimePanicAt (returnAddress (0 ), & errUnsafeSliceStringLenOutOfRange )
233237}
234238
235239// Panic when trying to create a new channel that is too big.
236240func chanMakePanic () {
237- runtimePanicAt (returnAddress (0 ), "new channel is too big" )
241+ runtimePanicAt (returnAddress (0 ), & errNewChannelIsTooBig )
238242}
239243
240244// Panic when a shift value is negative.
241245func negativeShiftPanic () {
242- runtimePanicAt (returnAddress (0 ), "negative shift" )
246+ runtimePanicAt (returnAddress (0 ), & errNegativeShift )
243247}
244248
245249// Panic when there is a divide by zero.
246250func divideByZeroPanic () {
247- runtimePanicAt (returnAddress (0 ), "divide by zero" )
251+ runtimePanicAt (returnAddress (0 ), & errDivideByZero )
248252}
249253
250254func blockingPanic () {
251- runtimePanicAt (returnAddress (0 ), "trying to do blocking operation in exported function" )
255+ runtimePanicAt (returnAddress (0 ), & errTryingToDoBlockingOperationInExported )
252256}
253257
254258//go:linkname fips_fatal crypto/internal/fips140.fatal
0 commit comments