diff --git a/php_v8js_macros.h b/php_v8js_macros.h index c04c41e..fe3d2d3 100644 --- a/php_v8js_macros.h +++ b/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& info) #define V8JS_THROW(type, message, message_len) v8::ThrowException(v8::Exception::type(V8JS_STRL(message, message_len))) diff --git a/tests/js-construct-direct-call.phpt b/tests/js-construct-direct-call.phpt new file mode 100644 index 0000000..43a6cef --- /dev/null +++ b/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-- + +--FILE-- +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=== diff --git a/v8js_convert.cc b/v8js_convert.cc index fb180f0..a5ee99b 100644 --- a/v8js_convert.cc +++ b/v8js_convert.cc @@ -159,13 +159,17 @@ static void php_v8js_php_callback(const v8::FunctionCallbackInfo& inf static void php_v8js_construct_callback(const v8::FunctionCallbackInfo& info) /* {{{ */ { v8::Isolate *isolate = v8::Isolate::GetCurrent(); - info.GetReturnValue().Set(V8JS_NULL); + info.GetReturnValue().Set(V8JS_UNDEFINED); // @todo assert constructor call v8::Handle 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(v8::External::Cast(*info[0])->Value());