1
0
mirror of https://github.com/robertkrimen/otto synced 2025-10-12 20:27:30 +08:00

Fix HasInstace (instanceof) to return false

Should not TypeError panic on a non-Object
This commit is contained in:
Robert Krimen 2013-02-04 15:07:49 -08:00
parent 795cc4935d
commit aaa90bf65c
3 changed files with 13 additions and 4 deletions

View File

@ -250,7 +250,7 @@ func (self *_runtime) calculateBinaryOperation(operator string, left Value, righ
case "instanceof": case "instanceof":
rightValue := self.GetValue(right) rightValue := self.GetValue(right)
if !rightValue.IsObject() { if !rightValue.IsObject() {
panic(newTypeError()) panic(newTypeError("Expecting a function in instanceof check, but got: %v", rightValue))
} }
return toValue(rightValue._object().HasInstance(leftValue)) return toValue(rightValue._object().HasInstance(leftValue))

View File

@ -1159,15 +1159,24 @@ func TestParenthesizing(t *testing.T) {
test("jkl", "true") test("jkl", "true")
} }
func TestInstanceOf(t *testing.T) { func Test_instanceof(t *testing.T) {
Terst(t) Terst(t)
test := runTest() test := runTest()
test(` test(`
abc = {} instanceof Object abc = {} instanceof Object;
`) `)
test("abc", "true") 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) { func TestIn(t *testing.T) {

View File

@ -69,7 +69,7 @@ func (self *_object) CallSet(name string, value Value) {
// 15.3.5.3 // 15.3.5.3
func (self *_object) HasInstance(of Value) bool { func (self *_object) HasInstance(of Value) bool {
if !of.IsObject() { if !of.IsObject() {
panic(newTypeError()) return false
} }
prototype := self.get("prototype") prototype := self.get("prototype")
if !prototype.IsObject() { if !prototype.IsObject() {