From 2642607a58dd832cc494a7e42deb8fa73d64708d Mon Sep 17 00:00:00 2001 From: Robert Krimen Date: Sat, 20 Apr 2013 20:03:34 -0700 Subject: [PATCH] Fix HasInstance to recurse the prototype hierarchy --- error_test.go | 15 +++++++++++++++ type_function.go | 13 +++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 error_test.go diff --git a/error_test.go b/error_test.go new file mode 100644 index 0000000..ab3ca48 --- /dev/null +++ b/error_test.go @@ -0,0 +1,15 @@ +package otto + +import ( + . "./terst" + "testing" +) + +func TestError_instanceof(t *testing.T) { + Terst(t) + + test := runTest() + test(` + (new TypeError()) instanceof Error + `, "true") +} diff --git a/type_function.go b/type_function.go index e9da4cb..9d6ea11 100644 --- a/type_function.go +++ b/type_function.go @@ -86,11 +86,16 @@ func (self *_object) HasInstance(of Value) bool { if !prototype.IsObject() { panic(newTypeError()) } - ofPrototype := of._object().prototype - if ofPrototype == nil { - return false + prototypeObject := prototype._object() + + value := of._object().prototype + for value != nil { + if value == prototypeObject { + return true + } + value = value.prototype } - return ofPrototype == prototype._object() + return false } type _functionSignature string