diff --git a/examples/prime.php b/examples/prime.php new file mode 100644 index 00000000..cd7aa939 --- /dev/null +++ b/examples/prime.php @@ -0,0 +1,51 @@ + + +using namespace php; + +class VectorBox : public Box { + public: + std::vector vec; + VectorBox(size_t size, bool init) { + vec.resize(size, init); + } + void checkOffset(Int offset) { + if (offset >= vec.size()) { + zend_throw_error(NULL, "index[%ld] is out of range()", offset); + } + } +}; + +var php_vector_new(Int size, Bool init) { + return {new VectorBox(size, init)}; +} + +Bool php_vector_get(var box, Int offset) { + auto vecbox = box.toBox(); + vecbox->checkOffset(offset); + return vecbox->vec.at(offset); +} + +void php_vector_set(var box, Int offset, Bool value) { + auto vecbox = box.toBox(); + vecbox->checkOffset(offset); + vecbox->vec.at(offset) = value; +} diff --git a/examples/prime/vector.stub.php b/examples/prime/vector.stub.php new file mode 100644 index 00000000..e2db0f8f --- /dev/null +++ b/examples/prime/vector.stub.php @@ -0,0 +1,15 @@ +returnType ? $this->getTypeFromZendType($this->parseIdentifier($v->returnType)) : self::TYPE_VOID; // .stub 存根定义 C++ Native 函数,必须设置返回值类型 - if ($returnType === self::TYPE_VOID && $this->stubFile) { + if (!$v->returnType && $this->stubFile) { throw new Exception('No return type for ' . $v->name); } + $returnType = $v->returnType ? $this->getTypeFromZendType($this->parseIdentifier($v->returnType)) : self::TYPE_VOID; $functionDef = new FunctionDef($this->parseIdentifier($v->name), $returnType); $this->functionDef = $functionDef; $this->parseParams($v->params, $functionDef); @@ -1182,6 +1182,11 @@ class CompilerBase extends \PhpAot\Core\Translator case 'void': $this->fatalError($param, 'Cannot use `void` as a parameter type.'); break; + case 'mixed': + return self::TYPE_VAR; + case 'resource': + $this->fatalError($param, 'Cannot use `resource` as a parameter type.'); + break; default: $this->objects[$var] = $name; return self::TYPE_OBJECT; @@ -1509,7 +1514,7 @@ class CompilerBase extends \PhpAot\Core\Translator } $nativeFn = $this->findNativeFunction($name); if ($nativeFn) { - return self::PREFIX . $nativeFn . '(' . $this->parseCallArgs($expr->args, $name) . ')'; + return self::PREFIX . $nativeFn . '(' . $this->parseNativeCallArgs($expr->args, $nativeFn) . ')'; } if ($this->isInternalFunction($name)) { $fn = 'php::' . $name; diff --git a/src/cpp/main.cc b/src/cpp/main.cc index 65b4708d..56c81119 100644 --- a/src/cpp/main.cc +++ b/src/cpp/main.cc @@ -35,6 +35,7 @@ int main(int cpp_argc, char **cpp_argv) { ProfilerStart("profile.out"); #endif zend_first_try { + php::request_init(); php_app_init(); php::eval("main();"); }