forked from MagicalTux/goro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exception.go
56 lines (49 loc) · 1.67 KB
/
exception.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package phpobj
import (
"github.com/MagicalTux/goro/core/phperr"
"github.com/MagicalTux/goro/core/phpv"
)
//> class Exception
var Exception = &ZClass{
Name: "Exception",
Implements: []*ZClass{Throwable},
Methods: map[phpv.ZString]*phpv.ZClassMethod{
"__construct": &phpv.ZClassMethod{Name: "__construct", Method: NativeMethod(exceptionConstruct)},
},
}
func SpawnException(ctx phpv.Context, l *phpv.Loc, msg phpv.ZString, code phpv.ZInt, prev *ZObject) (*ZObject, error) {
o, err := NewZObject(ctx, Exception)
if err != nil {
return nil, err
}
o.ObjectSet(ctx, phpv.ZString("message").ZVal(), msg.ZVal())
o.ObjectSet(ctx, phpv.ZString("code").ZVal(), code.ZVal())
o.ObjectSet(ctx, phpv.ZString("file").ZVal(), phpv.ZString(l.Filename).ZVal())
o.ObjectSet(ctx, phpv.ZString("line").ZVal(), phpv.ZInt(l.Line).ZVal())
o.ObjectSet(ctx, phpv.ZString("char").ZVal(), phpv.ZInt(l.Char).ZVal())
if prev != nil {
o.ObjectSet(ctx, phpv.ZString("previous").ZVal(), prev.ZVal())
}
return o, nil
}
func ThrowException(ctx phpv.Context, l *phpv.Loc, msg phpv.ZString, code phpv.ZInt) error {
o, err := SpawnException(ctx, l, msg, code, nil)
if err != nil {
return err
}
return &phperr.PhpThrow{o}
}
// public __construct ([ string $message = "" [, int $code = 0 [, Throwable $previous = NULL ]]] )
func exceptionConstruct(ctx phpv.Context, o *ZObject, args []*phpv.ZVal) (*phpv.ZVal, error) {
switch len(args) {
case 3:
o.ObjectSet(ctx, phpv.ZString("previous").ZVal(), args[2])
fallthrough
case 2:
o.ObjectSet(ctx, phpv.ZString("code").ZVal(), args[1])
fallthrough
case 1:
o.ObjectSet(ctx, phpv.ZString("message").ZVal(), args[0])
}
return phpv.ZNULL.ZVal(), nil
}