Skip to content

Commit 2f5995a

Browse files
committed
panic 参数限制为 string
1 parent 92ec97e commit 2f5995a

File tree

5 files changed

+39
-4
lines changed

5 files changed

+39
-4
lines changed

internal/backends/compiler_wat/wir/instruction_emitter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func (m *Module) EmitUnOp(x Value, op wat.OpCode) (insts []wat.Inst, ret_type Va
5353
insts = append(insts, wat.NewInstEqz(toWatType(ret_type)))
5454

5555
default:
56-
logger.Fatal("Todo: %[1]v: %[1]T", op)
56+
logger.Fatalf("Todo: %[1]v: %[1]T", op)
5757

5858
}
5959
return

internal/types/builtins.go

+14
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,20 @@ func (check *Checker) builtin(x *operand, call *ast.CallExpr, id builtinId) (_ b
457457
}
458458

459459
case _Panic:
460+
filename := check.fset.Position(x.pos()).Filename
461+
462+
// panic 语义变化: func panic(msg: string)
463+
if strings.HasSuffix(filename, ".wa") {
464+
// 放松检查, bool 变量放到运行时处理
465+
if !isString(x.typ) {
466+
check.invalidArg(x.pos(), "%s is not a string type", x)
467+
return
468+
}
469+
if nargs != 1 {
470+
check.errorf(call.Pos(), "%v expects %d or %d arguments; found %d", call, 1, 2, nargs)
471+
return
472+
}
473+
}
460474
// panic(x)
461475
// record panic call if inside a function with result parameters
462476
// (for use in Checker.isTerminating)

waroot/examples/math-bits/main.wa

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// 版权 @2023 凹语言 作者。保留所有权利。
2+
3+
import "math/bits" => _
4+
5+
func main {
6+
println("hello math")
7+
}

waroot/src/math/bits/bits.wa

+3-3
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ func Div(hi, lo, y: uint) => (quo, rem: uint) {
494494
// Div32 panics for y == 0 (division by zero) or y <= hi (quotient overflow).
495495
func Div32(hi, lo, y: uint32) => (quo, rem: uint32) {
496496
if y != 0 && y <= hi {
497-
panic(overflowError)
497+
// panic(overflowError)
498498
}
499499
z := uint64(hi)<<32 | uint64(lo)
500500
quo, rem = uint32(z/uint64(y)), uint32(z%uint64(y))
@@ -511,10 +511,10 @@ func Div64(hi, lo, y: uint64) => (quo, rem: uint64) {
511511
mask32 = two32 - 1
512512
)
513513
if y == 0 {
514-
panic(divideError)
514+
// panic(divideError)
515515
}
516516
if y <= hi {
517-
panic(overflowError)
517+
// panic(overflowError)
518518
}
519519

520520
s := uint(LeadingZeros64(y))

waroot/src/runtime/runtime_wasi.wa

+14
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ func waPrintI32(i: i32) {
6969
print_i32(i)
7070
}
7171

72+
#wa:linkname $runtime.waPrintU32
73+
func waPrintU32(i: u32) {
74+
print_u32(i)
75+
}
76+
7277
#wa:linkname $runtime.waPrintRune
7378
func waPrintRune(ch: i32) {
7479
putchar(ch)
@@ -96,3 +101,12 @@ func print_i32(i: i32) {
96101
print_i32(i/10)
97102
print_i32(i%10)
98103
}
104+
105+
func print_u32(i: u32) {
106+
if i < 10 {
107+
putchar(i32(i)+'0')
108+
return
109+
}
110+
print_u32(i/10)
111+
print_u32(i%10)
112+
}

0 commit comments

Comments
 (0)