Skip to content

Commit f2a053a

Browse files
committed
feat(core): 添加Object类exec方法支持直接调用zend_function
- 实现了Object::exec方法用于直接执行zend_function指针 - 提供了无参数和带参数两种重载版本的exec方法 - 添加了完整的单元测试验证exec功能的正确性 - 扩展了PHPX库的功能以支持更底层的函数调用操作
1 parent b486e39 commit f2a053a

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

include/phpx.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,6 +1125,8 @@ class Object : public Variant {
11251125
return call_impl(unwrap_ptr(), fn.const_ptr());
11261126
}
11271127
Variant exec(const Variant &fn, const ArgList &args);
1128+
Variant exec(zend_function *fn);
1129+
Variant exec(zend_function *fn, const ArgList &args);
11281130

11291131
Reference attrRef(const String &name);
11301132
Variant attr(const Variant &name, bool update = false) const;

src/core/object.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@ Variant Object::exec(const Variant &fn, const ArgList &args) {
5656
return call_impl(unwrap_ptr(), fn.const_ptr(), _args);
5757
}
5858

59+
Variant Object::exec(zend_function *fn) {
60+
Variant retval{};
61+
zend_call_known_function(fn, object(), ce(), retval.ptr(), 0, nullptr, nullptr);
62+
throwErrorIfOccurred();
63+
return retval;
64+
}
65+
66+
Variant Object::exec(zend_function *fn, const ArgList &args) {
67+
Variant retval{};
68+
Args _args(args);
69+
zend_call_known_function(fn, object(), ce(), retval.ptr(), _args.count(), _args.ptr(), nullptr);
70+
throwErrorIfOccurred();
71+
return retval;
72+
}
73+
5974
bool Object::instanceOf(const String &name) const {
6075
auto cls_ce = getClassEntry(name);
6176
if (!cls_ce) {

tests/src/object.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,3 +599,14 @@ TEST(object, enum_class) {
599599
auto name = getEnumCaseName(case1);
600600
ASSERT_STREQ(name.data(), "Spades");
601601
}
602+
603+
TEST(object, exec) {
604+
auto obj = newObject("DateTime", "2000-01-01");
605+
auto fn = getMethod(obj.ce(), "format");
606+
auto rs = obj.exec(fn, {"Y-m-d H:i:s"});
607+
ASSERT_STREQ(rs.toCString(), "2000-01-01 00:00:00");
608+
609+
auto fn2 = getMethod(obj.ce(), "getTimestamp");
610+
auto rs2 = obj.exec(fn2);
611+
ASSERT_GT(rs2.toInt(), 100000000);
612+
}

0 commit comments

Comments
 (0)