Skip to content
This repository was archived by the owner on Apr 30, 2019. It is now read-only.

Commit 0765e45

Browse files
authored
Introduce ValueError and use it for panic messages (#16)
1 parent 690af83 commit 0765e45

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

js/js_notwasm.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ func ValueOf(x interface{}) Value {
189189
}
190190

191191
func (v Value) Bool() bool {
192+
if vType := v.Type(); vType != TypeBoolean {
193+
panic(&ValueError{"Value.Bool", vType})
194+
}
192195
return v.v.Bool()
193196
}
194197

@@ -202,10 +205,19 @@ func convertArgs(args []interface{}) []interface{} {
202205
}
203206

204207
func (v Value) Call(m string, args ...interface{}) Value {
208+
if vType := v.Type(); vType != TypeObject && vType != TypeFunction {
209+
panic(&ValueError{"Value.Call", vType})
210+
}
211+
if propType := v.Get(m).Type(); propType != TypeFunction {
212+
panic("js: Value.Call: property " + m + " is not a function, got " + propType.String())
213+
}
205214
return Value{v: v.v.Call(m, convertArgs(args)...)}
206215
}
207216

208217
func (v Value) Float() float64 {
218+
if vType := v.Type(); vType != TypeNumber {
219+
panic(&ValueError{"Value.Float", vType})
220+
}
209221
return v.v.Float()
210222
}
211223

@@ -218,10 +230,16 @@ func (v Value) Index(i int) Value {
218230
}
219231

220232
func (v Value) Int() int {
233+
if vType := v.Type(); vType != TypeNumber {
234+
panic(&ValueError{"Value.Int", vType})
235+
}
221236
return v.v.Int()
222237
}
223238

224239
func (v Value) Invoke(args ...interface{}) Value {
240+
if vType := v.Type(); vType != TypeFunction {
241+
panic(&ValueError{"Value.Invoke", vType})
242+
}
225243
return Value{v: v.v.Invoke(convertArgs(args)...)}
226244
}
227245

@@ -273,3 +291,12 @@ func (t *TypedArray) Release() {
273291
func GetInternalObject(v Value) interface{} {
274292
return v.v
275293
}
294+
295+
type ValueError struct {
296+
Method string
297+
Type Type
298+
}
299+
300+
func (e *ValueError) Error() string {
301+
return "syscall/js: call of " + e.Method + " on " + e.Type.String()
302+
}

js/js_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ func TestString(t *testing.T) {
5454
if want := "Hello"; got != want {
5555
t.Errorf("got %#v, want %#v", got, want)
5656
}
57+
58+
obj = js.Global().Call("eval", "123")
59+
got = obj.String()
60+
if want := "123"; got != want {
61+
t.Errorf("got %#v, want %#v", got, want)
62+
}
5763
}
5864

5965
func TestInt64(t *testing.T) {

0 commit comments

Comments
 (0)