shithub: libmujs

Download patch

ref: a65416046c993ed35a7e46c8b096eefc8fb7b2c7
parent: 82a42ef1e626b4826f0a6e64344766f4258b571c
author: Tor Andersson <tor@ccxvii.net>
date: Thu Jan 23 04:12:39 EST 2014

Make instanceof a public function.

--- a/js.h
+++ b/js.h
@@ -138,5 +138,6 @@
 int js_compare(js_State *J);
 int js_equal(js_State *J);
 int js_strictequal(js_State *J);
+int js_instanceof(js_State *J);
 
 #endif
--- a/jsrun.c
+++ b/jsrun.c
@@ -751,7 +751,9 @@
 		case OP_GE: b = js_compare(J); js_pushboolean(J, b >= 0); break;
 
 		case OP_INSTANCEOF:
-			js_instanceof(J);
+			b = js_instanceof(J);
+			js_pop(J, 2);
+			js_pushboolean(J, b);
 			break;
 
 		/* Equality */
--- a/jsvalue.c
+++ b/jsvalue.c
@@ -296,7 +296,7 @@
 
 /* Non-trivial operations on values. These are implemented using the stack. */
 
-void js_instanceof(js_State *J)
+int js_instanceof(js_State *J)
 {
 	js_Object *O, *V;
 
@@ -303,29 +303,23 @@
 	if (!js_iscallable(J, -1))
 		js_typeerror(J, "instanceof: invalid operand");
 
-	if (!js_isobject(J, -2)) {
-		js_pop(J, 2);
-		js_pushboolean(J, 0);
-		return;
-	}
-	V = js_toobject(J, -2);
+	if (!js_isobject(J, -2))
+		return 0;
 
 	js_getproperty(J, -1, "prototype");
 	if (!js_isobject(J, -1))
 		js_typeerror(J, "instanceof: 'prototype' property is not an object");
 	O = js_toobject(J, -1);
+	js_pop(J, 1);
 
+	V = js_toobject(J, -2);
 	while (V) {
 		V = V->prototype;
-		if (O == V) {
-			js_pop(J, 3);
-			js_pushboolean(J, 1);
-			return;
-		}
+		if (O == V)
+			return 1;
 	}
 
-	js_pop(J, 3);
-	js_pushboolean(J, 0);
+	return 0;
 }
 
 void js_concat(js_State *J)
--- a/jsvalue.h
+++ b/jsvalue.h
@@ -92,8 +92,6 @@
 const char *jsV_numbertostring(js_State *J, double number);
 double jsV_stringtonumber(js_State *J, const char *string);
 
-void js_instanceof(js_State *J);
-
 /* jsproperty.c */
 js_Object *jsV_newobject(js_State *J, js_Class type, js_Object *prototype);
 js_Property *jsV_getownproperty(js_State *J, js_Object *obj, const char *name);