diff --git a/evaluate_expression.go b/evaluate_expression.go index 52274df..7e5a00c 100644 --- a/evaluate_expression.go +++ b/evaluate_expression.go @@ -250,7 +250,7 @@ func (self *_runtime) calculateBinaryOperation(operator string, left Value, righ case "instanceof": rightValue := self.GetValue(right) if !rightValue.IsObject() { - panic(newTypeError()) + panic(newTypeError("Expecting a function in instanceof check, but got: %v", rightValue)) } return toValue(rightValue._object().HasInstance(leftValue)) diff --git a/otto_test.go b/otto_test.go index 704c0ad..1c6c055 100644 --- a/otto_test.go +++ b/otto_test.go @@ -1159,15 +1159,24 @@ func TestParenthesizing(t *testing.T) { test("jkl", "true") } -func TestInstanceOf(t *testing.T) { +func Test_instanceof(t *testing.T) { Terst(t) test := runTest() test(` - abc = {} instanceof Object + abc = {} instanceof Object; `) test("abc", "true") + + test(` + abc = "abc" instanceof Object; + `) + test("abc", "false") + + test(`raise: + abc = {} instanceof "abc"; + `, "TypeError: Expecting a function in instanceof check, but got: abc") } func TestIn(t *testing.T) { diff --git a/type_function.go b/type_function.go index 9f404a0..b2845b3 100644 --- a/type_function.go +++ b/type_function.go @@ -69,7 +69,7 @@ func (self *_object) CallSet(name string, value Value) { // 15.3.5.3 func (self *_object) HasInstance(of Value) bool { if !of.IsObject() { - panic(newTypeError()) + return false } prototype := self.get("prototype") if !prototype.IsObject() {