Skip to content

Fixes #410 #416

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions tests/issue_410_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
--TEST--
Test V8::executeString() : Method access from multiple derived classes
--SKIPIF--
<?php require_once(dirname(__FILE__) . '/skipif.inc'); ?>
--FILE--
<?php

class BaseClass {
public function bla() {
printf('print bla, called class: %s' . PHP_EOL, get_called_class());
}
}

class Foo extends BaseClass {}

class Bar extends BaseClass {}

$v8 = new V8Js('PHP');
$v8->Foo = new Foo();
$v8->Bar = new Bar();

$code = <<<EOT
var_dump(PHP.Foo);
PHP.Foo.bla();
var_dump(PHP.Bar);
PHP.Bar.bla();
EOT;

$v8->executeString($code);

?>
===EOF===
--EXPECTF--
object(Foo)#%d (1) {
["bla"] =>
object(Closure)#%d {
function () { [native code] }
}
}
print bla, called class: Foo
object(Bar)#%d (1) {
["bla"] =>
object(Closure)#%d {
function () { [native code] }
}
}
print bla, called class: Bar
===EOF===

6 changes: 3 additions & 3 deletions v8js_class.cc
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ static void v8js_free_storage(zend_object *object) /* {{{ */
}
c->call_impls.~map();

for (std::map<zend_function *, v8js_function_tmpl_t>::iterator it = c->method_tmpls.begin();
for (std::map<std::pair<zend_class_entry *, zend_function *>, v8js_function_tmpl_t>::iterator it = c->method_tmpls.begin();
it != c->method_tmpls.end(); ++it) {
it->second.Reset();
}
Expand Down Expand Up @@ -232,7 +232,7 @@ static zend_object* v8js_new(zend_class_entry *ce) /* {{{ */
new(&c->weak_closures) std::map<v8js_function_tmpl_t *, v8js_persistent_obj_t>();
new(&c->weak_objects) std::map<zend_object *, v8js_persistent_obj_t>();
new(&c->call_impls) std::map<v8js_function_tmpl_t *, v8js_function_tmpl_t>();
new(&c->method_tmpls) std::map<zend_function *, v8js_function_tmpl_t>();
new(&c->method_tmpls) std::map<std::pair<zend_class_entry *, zend_function *>, v8js_function_tmpl_t>();

new(&c->v8js_v8objects) std::list<v8js_v8object *>();
new(&c->script_objects) std::vector<v8js_script *>();
Expand Down Expand Up @@ -589,7 +589,7 @@ static PHP_METHOD(V8Js, __construct)
ft = v8::FunctionTemplate::New(isolate, v8js_php_callback,
v8::External::New((isolate), method_ptr));
// @fixme add/check Signature v8::Signature::New((isolate), tmpl));
v8js_function_tmpl_t *persistent_ft = &c->method_tmpls[method_ptr];
v8js_function_tmpl_t *persistent_ft = &c->method_tmpls[std::make_pair(ce, method_ptr)];
persistent_ft->Reset(isolate, ft);

php_obj->CreateDataProperty(context, method_name, ft->GetFunction(context).ToLocalChecked());
Expand Down
2 changes: 1 addition & 1 deletion v8js_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ struct v8js_ctx {
std::map<zend_object *, v8js_persistent_obj_t> weak_objects;
std::map<v8js_function_tmpl_t *, v8js_persistent_obj_t> weak_closures;
std::map<v8js_function_tmpl_t *, v8js_function_tmpl_t> call_impls;
std::map<zend_function *, v8js_function_tmpl_t> method_tmpls;
std::map<std::pair<zend_class_entry *, zend_function *>, v8js_function_tmpl_t> method_tmpls;

std::list<v8js_v8object *> v8js_v8objects;

Expand Down
4 changes: 2 additions & 2 deletions v8js_object_export.cc
Original file line number Diff line number Diff line change
Expand Up @@ -701,13 +701,13 @@ v8::Local<v8::Value> v8js_named_property_callback(v8::Local<v8::Name> property_n
v8::Local<v8::FunctionTemplate> ft;
try {
ft = v8::Local<v8::FunctionTemplate>::New
(isolate, ctx->method_tmpls.at(method_ptr));
(isolate, ctx->method_tmpls.at(std::make_pair(ce, method_ptr)));
}
catch (const std::out_of_range &) {
ft = v8::FunctionTemplate::New(isolate, v8js_php_callback,
v8::External::New((isolate), method_ptr),
v8::Signature::New((isolate), tmpl));
v8js_function_tmpl_t *persistent_ft = &ctx->method_tmpls[method_ptr];
v8js_function_tmpl_t *persistent_ft = &ctx->method_tmpls[std::make_pair(ce, method_ptr)];
persistent_ft->Reset(isolate, ft);
}
ft->GetFunction(v8_context).ToLocal(&ret_value);
Expand Down