forked from tommie/v8go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exception.go
109 lines (91 loc) · 2.66 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Copyright 2021 Roger Chapman and the v8go contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package v8go
import (
// #include <stdlib.h>
// #include "v8go.h"
"C"
"fmt"
"unsafe"
)
// NewRangeError creates a RangeError.
func NewRangeError(iso *Isolate, msg string) *Exception {
return newExceptionError(iso, C.ERROR_RANGE, msg)
}
// NewReferenceError creates a ReferenceError.
func NewReferenceError(iso *Isolate, msg string) *Exception {
return newExceptionError(iso, C.ERROR_REFERENCE, msg)
}
// NewSyntaxError creates a SyntaxError.
func NewSyntaxError(iso *Isolate, msg string) *Exception {
return newExceptionError(iso, C.ERROR_SYNTAX, msg)
}
// NewTypeError creates a TypeError.
func NewTypeError(iso *Isolate, msg string) *Exception {
return newExceptionError(iso, C.ERROR_TYPE, msg)
}
// NewWasmCompileError creates a WasmCompileError.
func NewWasmCompileError(iso *Isolate, msg string) *Exception {
return newExceptionError(iso, C.ERROR_WASM_COMPILE, msg)
}
// NewWasmLinkError creates a WasmLinkError.
func NewWasmLinkError(iso *Isolate, msg string) *Exception {
return newExceptionError(iso, C.ERROR_WASM_LINK, msg)
}
// NewWasmRuntimeError creates a WasmRuntimeError.
func NewWasmRuntimeError(iso *Isolate, msg string) *Exception {
return newExceptionError(iso, C.ERROR_WASM_RUNTIME, msg)
}
// NewError creates an Error, which is the common thing to throw from
// user code.
func NewError(iso *Isolate, msg string) *Exception {
return newExceptionError(iso, C.ERROR_GENERIC, msg)
}
func newExceptionError(iso *Isolate, typ C.ErrorTypeIndex, msg string) *Exception {
cmsg := C.CString(msg)
defer C.free(unsafe.Pointer(cmsg))
eptr := C.NewValueError(iso.ptr, typ, cmsg)
if eptr == nil {
panic(fmt.Errorf("invalid error type index: %d", typ))
}
return &Exception{&Value{ptr: eptr}}
}
// An Exception is a JavaScript exception.
type Exception struct {
*Value
}
// value implements Valuer.
func (e *Exception) value() *Value {
return e.Value
}
// Error implements error.
func (e *Exception) Error() string {
return e.String()
}
// As provides support for errors.As.
func (e *Exception) As(target interface{}) bool {
ep, ok := target.(**Exception)
if !ok {
return false
}
*ep = e
return true
}
// Is provides support for errors.Is.
func (e *Exception) Is(err error) bool {
eerr, ok := err.(*Exception)
if !ok {
return false
}
return eerr.String() == e.String()
}
// String implements fmt.Stringer.
func (e *Exception) String() string {
if e.Value == nil {
return "<nil>"
}
s := C.ExceptionGetMessageString(e.ptr)
defer C.free(unsafe.Pointer(s))
return C.GoString(s)
}