Переглянути джерело

Don't export methods of V8Js object to V8

Stefan Siegl 9 роки тому
батько
коміт
1e86e2c9f7
2 змінених файлів з 74 додано та 1 видалено
  1. 57 0
      tests/issue_183_003.phpt
  2. 17 1
      v8js_class.cc

+ 57 - 0
tests/issue_183_003.phpt

@@ -0,0 +1,57 @@
+--TEST--
+Test V8::executeString() : Method access on derived classes (V8Js methods)
+--SKIPIF--
+<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
+--FILE--
+<?php
+
+class Foo extends \V8Js
+{
+    public function hello()
+    {
+	print("Hello World\n");
+    }
+}
+
+$JS = <<< EOT
+var_dump(typeof PHP.hello);
+var_dump(typeof PHP.executeString);
+var_dump(typeof PHP.compileString);
+var_dump(typeof PHP.executeScript);
+var_dump(typeof PHP.checkString);
+var_dump(typeof PHP.getPendingException);
+var_dump(typeof PHP.setModuleNormaliser);
+var_dump(typeof PHP.setModuleLoader);
+var_dump(typeof PHP.registerExtension);
+var_dump(typeof PHP.getExtensions);
+var_dump(typeof PHP.setTimeLimit);
+var_dump(typeof PHP.setMemoryLimit);
+
+try {
+    PHP.setTimeLimit(100);
+}
+catch(e) {
+    var_dump('caught');
+}
+EOT;
+
+$v8 = new Foo();
+$v8->executeString($JS);
+
+?>
+===EOF===
+--EXPECTF--
+string(8) "function"
+string(9) "undefined"
+string(9) "undefined"
+string(9) "undefined"
+string(9) "undefined"
+string(9) "undefined"
+string(9) "undefined"
+string(9) "undefined"
+string(9) "undefined"
+string(9) "undefined"
+string(9) "undefined"
+string(9) "undefined"
+string(6) "caught"
+===EOF===

+ 17 - 1
v8js_class.cc

@@ -46,6 +46,9 @@ static zend_class_entry *php_ce_v8js;
 static zend_object_handlers v8js_object_handlers;
 /* }}} */
 
+/* Forward declare v8js_methods, actually "static" but not possible in C++ */
+extern const zend_function_entry v8js_methods[];
+
 typedef struct _v8js_script {
 	char *name;
 	v8js_ctx *ctx;
@@ -526,6 +529,19 @@ static PHP_METHOD(V8Js, __construct)
 			continue;
 		}
 
+		const zend_function_entry *fe;
+		for (fe = v8js_methods; fe->fname; fe ++) {
+			if (fe->fname == method_ptr->common.function_name) {
+				break;
+			}
+		}
+
+		if(fe->fname) {
+			/* Method belongs to \V8Js class itself, never export to V8, even if
+			 * it is overriden in a derived class. */
+			continue;
+		}
+
 		v8::Local<v8::String> method_name = V8JS_STR(method_ptr->common.function_name);
 		v8::Local<v8::FunctionTemplate> ft;
 
@@ -1119,7 +1135,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_v8js_setmemorylimit, 0, 0, 1)
 ZEND_END_ARG_INFO()
 
 
-static const zend_function_entry v8js_methods[] = { /* {{{ */
+const zend_function_entry v8js_methods[] = { /* {{{ */
 	PHP_ME(V8Js,	__construct,			arginfo_v8js_construct,				ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
 	PHP_ME(V8Js,	__sleep,				arginfo_v8js_sleep,					ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
 	PHP_ME(V8Js,	__wakeup,				arginfo_v8js_sleep,					ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)