Forráskód Böngészése

Handle non-construct call

Stefan Siegl 12 éve
szülő
commit
286b0d8ac0
3 módosított fájl, 46 hozzáadás és 1 törlés
  1. 1 0
      php_v8js_macros.h
  2. 40 0
      tests/js-construct-direct-call.phpt
  3. 5 1
      v8js_convert.cc

+ 1 - 0
php_v8js_macros.h

@@ -49,6 +49,7 @@ extern "C" {
 #define V8JS_FLOAT(v)		v8::Number::New(v)
 #define V8JS_BOOL(v)		v8::Boolean::New(v)
 #define V8JS_NULL			v8::Null()
+#define V8JS_UNDEFINED		v8::Undefined()
 #define V8JS_MN(name)		v8js_method_##name
 #define V8JS_METHOD(name)	void V8JS_MN(name)(const v8::FunctionCallbackInfo<v8::Value>& info)
 #define V8JS_THROW(type, message, message_len)	v8::ThrowException(v8::Exception::type(V8JS_STRL(message, message_len)))

+ 40 - 0
tests/js-construct-direct-call.phpt

@@ -0,0 +1,40 @@
+--TEST--
+Test V8::executeString() : Test PHP object construction controlled by JavaScript (non-construction call)
+--SKIPIF--
+<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
+--FILE--
+<?php
+$v8 = new V8Js();
+
+class Greeter {
+    function sayHello($a) {
+        echo "Hello $a\n";
+    }
+}
+
+$v8->greeter = new Greeter();
+$v8->executeString('
+    function JsGreeter() { };
+    JsGreeter.prototype.sayHello = function(a) {
+        print("Hello " + a + "\n");
+    };
+
+    jsGreeter = new JsGreeter();
+    jsGreeter.sayHello("Paul");
+    print(jsGreeter.constructor());
+    print("\n");
+
+    // -----  now the same using v8Js  -----
+
+    PHP.greeter.sayHello("John");
+    print(PHP.greeter.constructor());
+    print("\n");
+');
+?>
+===EOF===
+--EXPECT--
+Hello Paul
+undefined
+Hello John
+undefined
+===EOF===

+ 5 - 1
v8js_convert.cc

@@ -159,13 +159,17 @@ static void php_v8js_php_callback(const v8::FunctionCallbackInfo<v8::Value>& inf
 static void php_v8js_construct_callback(const v8::FunctionCallbackInfo<v8::Value>& info) /* {{{ */
 {
 	v8::Isolate *isolate = v8::Isolate::GetCurrent();
-	info.GetReturnValue().Set(V8JS_NULL);
+	info.GetReturnValue().Set(V8JS_UNDEFINED);
 
 	// @todo assert constructor call
 	v8::Handle<v8::Object> newobj = info.This();
 	zval *value;
 	TSRMLS_FETCH();
 
+	if (!info.IsConstructCall()) {
+		return;
+	}
+
 	if (info[0]->IsExternal()) {
 		// Object created by v8js in php_v8js_hash_to_jsobj, PHP object passed as v8::External.
 		value = static_cast<zval *>(v8::External::Cast(*info[0])->Value());