Skip to content

Commit

Permalink
Merge pull request #5 from dmalusev/feat/c-translation-units
Browse files Browse the repository at this point in the history
feat: C modules into separate translation units
  • Loading branch information
Dusan Malusev authored Jan 16, 2024
2 parents 45e4410 + c8dcbad commit d89a67c
Show file tree
Hide file tree
Showing 14 changed files with 167 additions and 182 deletions.
24 changes: 12 additions & 12 deletions examples/http-client/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,23 @@ pub fn make_response_class() -> ClassEntity {
});

class.add_method("status", Visibility::Public, |this, _arguments| {
let response =
this.as_state::<Option<Response>>()
.as_ref()
.ok_or_else(|| HttpClientError::ResponseAfterRead {
method_name: "status".to_owned(),
})?;
let response = this
.as_state::<Option<Response>>()
.as_ref()
.ok_or_else(|| HttpClientError::ResponseAfterRead {
method_name: "status".to_owned(),
})?;

Ok::<_, HttpClientError>(response.status().as_u16() as i64)
});

class.add_method("headers", Visibility::Public, |this, _arguments| {
let response =
this.as_state::<Option<Response>>()
.as_ref()
.ok_or_else(|| HttpClientError::ResponseAfterRead {
method_name: "headers".to_owned(),
})?;
let response = this
.as_state::<Option<Response>>()
.as_ref()
.ok_or_else(|| HttpClientError::ResponseAfterRead {
method_name: "headers".to_owned(),
})?;
let headers_map = response
.headers()
.iter()
Expand Down
9 changes: 4 additions & 5 deletions examples/http-server/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ pub fn make_request_class() -> ClassEntity {

// Register the constructor method with public visibility, initialize the
// headers with empty array.
class
.add_method("__construct", Visibility::Public, |this, _arguments| {
this.set_property("headers", ZArray::new());
Ok::<_, Infallible>(())
});
class.add_method("__construct", Visibility::Public, |this, _arguments| {
this.set_property("headers", ZArray::new());
Ok::<_, Infallible>(())
});

class
}
Expand Down
21 changes: 21 additions & 0 deletions phper-sys/c/php_classes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <phper.h>

zend_class_entry phper_init_class_entry(const char *class_name,
size_t class_name_len) {

zend_class_entry class_ce = {0};
INIT_CLASS_ENTRY_EX(class_ce, class_name, class_name_len, NULL);
return class_ce;
}

zend_class_entry *
phper_register_class_entry(zend_class_entry *ce, zend_class_entry *parent,
const zend_function_entry *functions) {
ce->info.internal.builtin_functions = functions;

if (parent == NULL) {
return zend_register_internal_class(ce);
}

return zend_register_internal_class_ex(ce, parent);
}
38 changes: 38 additions & 0 deletions phper-sys/c/php_functions.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <phper.h>

zend_string *phper_get_function_or_method_name(const zend_function *func) {
return get_function_or_method_name(func);
}

zend_string *phper_get_function_name(const zend_function *func) {
return func->common.function_name;
}

bool phper_call_user_function(zval *object, zval *function_name,
zval *retval_ptr, const zval *params,
uint32_t param_count,
const HashTable *named_params) {

_call_user_function_impl(object, function_name, retval_ptr, param_count,
(zval *)params,
(HashTable *)named_params) == SUCCESS;
}

const zval *phper_zend_call_var_num(const zend_execute_data *execute_data,
int index) {
return ZEND_CALL_VAR_NUM(execute_data, index);
}

const zval *phper_zend_call_arg(const zend_execute_data *execute_data,
int index) {
return ZEND_CALL_ARG(execute_data, index);
}

uint32_t phper_zend_num_args(const zend_execute_data *execute_data) {
return ZEND_NUM_ARGS();
}

bool phper_zend_get_parameters_array_ex(uint32_t param_count,
zval *argument_array) {
return zend_get_parameters_array_ex(param_count, argument_array) == SUCCESS;
}
20 changes: 20 additions & 0 deletions phper-sys/c/php_interfaces.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <phper.h>

zend_class_entry phper_init_interface_entry(const char *class_name,
size_t class_name_len) {
zend_class_entry class_ce = {0};
INIT_CLASS_ENTRY_EX(class_ce, class_name, class_name_len, NULL);
return class_ce;
}

zend_class_entry *
phper_register_interface_entry(zend_class_entry *ce,
const zend_function_entry *functions) {
ce->info.internal.builtin_functions = functions;
return zend_register_internal_interface(ce);
}

bool phper_instanceof_function(const zend_class_entry *instance_ce,
const zend_class_entry *ce) {
return instanceof_function(instance_ce, ce) != 0;
}
25 changes: 25 additions & 0 deletions phper-sys/c/php_objects.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <phper.h>

zval *phper_get_this(const zend_execute_data *execute_data) {
return getThis();
}

size_t phper_zend_object_properties_size(const zend_class_entry *ce) {
return zend_object_properties_size((zend_class_entry *)ce);
}

void *phper_zend_object_alloc(size_t obj_size, const zend_class_entry *ce) {
return zend_object_alloc(obj_size, (zend_class_entry *)ce);
}

bool phper_object_init_ex(zval *arg, const zend_class_entry *class_type) {
return object_init_ex(arg, (zend_class_entry *)class_type) == SUCCESS;
}

void phper_zend_object_release(zend_object *obj) {
zend_object_release(obj);
}

uint32_t phper_zend_object_gc_refcount(const zend_object *obj) {
return GC_REFCOUNT(obj);
}
115 changes: 0 additions & 115 deletions phper-sys/c/php_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,121 +10,6 @@

#include <phper.h>

// ==================================================
// object apis:
// ==================================================

zval *phper_get_this(zend_execute_data *execute_data) {
return getThis();
}

size_t phper_zend_object_properties_size(zend_class_entry *ce) {
return zend_object_properties_size(ce);
}

void *phper_zend_object_alloc(size_t obj_size, zend_class_entry *ce) {
return zend_object_alloc(obj_size, ce);
}

zend_object *(**phper_get_create_object(zend_class_entry *ce))(
zend_class_entry *class_type) {
return &ce->create_object;
}

bool phper_object_init_ex(zval *arg, zend_class_entry *class_type) {
return object_init_ex(arg, class_type) == SUCCESS;
}

void phper_zend_object_release(zend_object *obj) {
zend_object_release(obj);
}

uint32_t phper_zend_object_gc_refcount(const zend_object *obj) {
return GC_REFCOUNT(obj);
}

// ==================================================
// class apis:
// ==================================================

zend_class_entry phper_init_class_entry_ex(const char *class_name,
size_t class_name_len) {
zend_class_entry class_ce = {0};
class_ce.name = zend_string_init_interned(class_name, class_name_len, true);
class_ce.default_object_handlers = &std_object_handlers;
return class_ce;
}

zend_class_entry *
phper_register_class_entry_ex(zend_class_entry *ce, zend_class_entry *parent,
const zend_function_entry *functions) {
ce->info.internal.builtin_functions = functions;

if (parent == NULL) {
return zend_register_internal_class(ce);
}

return zend_register_internal_class_ex(ce, parent);
}

zend_class_entry phper_init_interface_entry_ex(const char *class_name,
size_t class_name_len) {
zend_class_entry class_ce = {0};
class_ce.name = zend_string_init_interned(class_name, class_name_len, true);
class_ce.default_object_handlers = &std_object_handlers;

return class_ce;
}

zend_class_entry *
phper_register_interface_entry_ex(zend_class_entry *ce,
const zend_function_entry *functions) {
ce->info.internal.builtin_functions = functions;
return zend_register_internal_interface(ce);
}

bool phper_instanceof_function(const zend_class_entry *instance_ce,
const zend_class_entry *ce) {
return instanceof_function(instance_ce, ce) != 0;
}

// ==================================================
// function apis:
// ==================================================

zend_string *phper_get_function_or_method_name(const zend_function *func) {
return get_function_or_method_name(func);
}

zend_string *phper_get_function_name(const zend_function *func) {
return func->common.function_name;
}

bool phper_call_user_function(HashTable *function_table, zval *object,
zval *function_name, zval *retval_ptr,
uint32_t param_count, zval params[]) {
(void)function_table; // suppress "unused parameter" warnings.
return call_user_function(function_table, object, function_name, retval_ptr,
param_count, params) == SUCCESS;
}

zval *phper_zend_call_var_num(zend_execute_data *execute_data, int index) {
return ZEND_CALL_VAR_NUM(execute_data, index);
}

zval *phper_zend_call_arg(zend_execute_data *execute_data, int index) {
return ZEND_CALL_ARG(execute_data, index);
}

uint32_t phper_zend_num_args(const zend_execute_data *execute_data) {
return ZEND_NUM_ARGS();
}

bool phper_zend_get_parameters_array_ex(uint32_t param_count,
zval *argument_array) {
return zend_get_parameters_array_ex(param_count, argument_array) != 0;
}

// ==================================================
// module apis:
// ==================================================
Expand Down
47 changes: 25 additions & 22 deletions phper-sys/include/phper.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,42 +192,44 @@ bool phper_zend_str_exists(HashTable *ht, const char *str, size_t len);
// object apis:
// ==================================================

zval *phper_get_this(zend_execute_data *execute_data);
size_t phper_zend_object_properties_size(zend_class_entry *ce);
void *phper_zend_object_alloc(size_t obj_size, zend_class_entry *ce);
zend_object *(**phper_get_create_object(zend_class_entry *ce))(
zend_class_entry *class_type);
bool phper_object_init_ex(zval *arg, zend_class_entry *class_type);
zval *phper_get_this(const zend_execute_data *execute_data);
size_t phper_zend_object_properties_size(const zend_class_entry *ce);
void *phper_zend_object_alloc(size_t obj_size, const zend_class_entry *ce);
bool phper_object_init_ex(zval *arg, const zend_class_entry *class_type);
void phper_zend_object_release(zend_object *obj);
uint32_t phper_zend_object_gc_refcount(const zend_object *obj);

// ==================================================
// class apis:
// ==================================================
zend_class_entry phper_init_class_entry_ex(const char *class_name,
size_t class_name_len);

zend_class_entry phper_init_class_entry(const char *class_name,
size_t class_name_len);
zend_class_entry *
phper_register_class_entry_ex(zend_class_entry *ce, zend_class_entry *parent,
const zend_function_entry *functions);
phper_register_class_entry(zend_class_entry *ce, zend_class_entry *parent,
const zend_function_entry *functions);

zend_class_entry phper_init_interface_entry_ex(const char *class_name,
size_t class_name_len);
// ==================================================
// interface apis:
// ==================================================
zend_class_entry phper_init_interface_entry(const char *class_name,
size_t class_name_len);
zend_class_entry *
phper_register_interface_entry(zend_class_entry *ce,
const zend_function_entry *functions);

bool phper_instanceof_function(const zend_class_entry *instance_ce,
const zend_class_entry *ce);

zend_class_entry *
phper_register_interface_entry_ex(zend_class_entry *ce,
const zend_function_entry *functions);

zend_string *phper_get_function_or_method_name(const zend_function *func);
zend_string *phper_get_function_name(const zend_function *func);
bool phper_call_user_function(HashTable *function_table, zval *object,
zval *function_name, zval *retval_ptr,
uint32_t param_count, zval params[]);
zval *phper_zend_call_var_num(zend_execute_data *execute_data, int index);
zval *phper_zend_call_arg(zend_execute_data *execute_data, int index);
bool phper_call_user_function(zval *object, zval *function_name,
zval *retval_ptr, const zval *params,
uint32_t param_count,
const HashTable *named_params);
const zval *phper_zend_call_var_num(const zend_execute_data *execute_data,
int index);
const zval *phper_zend_call_arg(const zend_execute_data *execute_data,
int index);
uint32_t phper_zend_num_args(const zend_execute_data *execute_data);
bool phper_zend_get_parameters_array_ex(uint32_t param_count,
zval *argument_array);
Expand Down Expand Up @@ -259,6 +261,7 @@ zend_internal_arg_info
phper_zend_begin_arg_info_ex(bool return_reference,
uintptr_t required_num_args);
zend_internal_arg_info phper_zend_arg_info(bool pass_by_ref, const char *name);

// ==================================================
// Constants API:
// ==================================================
Expand Down
10 changes: 5 additions & 5 deletions phper/src/classes/entity.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::{any::Any, marker::PhantomData, mem::zeroed, ptr::null_mut, rc::Rc};

use phper_sys::{
phper_get_create_object, phper_init_class_entry_ex, phper_register_class_entry_ex,
zend_class_entry, zend_class_implements, zend_function_entry,
phper_init_class_entry, phper_register_class_entry, zend_class_entry, zend_class_implements,
zend_function_entry,
};

use crate::{
Expand Down Expand Up @@ -76,7 +76,7 @@ impl ClassEntity {
let class_name_len = class_name.len();

Self {
class: unsafe { phper_init_class_entry_ex(class_name.as_ptr().cast(), class_name_len) },
class: unsafe { phper_init_class_entry(class_name.as_ptr().cast(), class_name_len) },
state_constructor: Rc::new(move || {
let state = state_constructor();
let boxed = Box::new(state) as Box<dyn Any>;
Expand Down Expand Up @@ -311,7 +311,7 @@ impl crate::modules::Registerer for ClassEntity {
.unwrap_or(null_mut());

let class_ce =
phper_register_class_entry_ex(&mut self.class, parent, self.function_entries());
phper_register_class_entry(&mut self.class, parent, self.function_entries());

if let Some(bind_class) = self.bind_class {
bind_class.bind(class_ce);
Expand All @@ -322,7 +322,7 @@ impl crate::modules::Registerer for ClassEntity {
zend_class_implements(class_ce, 1, interface_ce);
}

*phper_get_create_object(class_ce) = Some(create_object);
(*class_ce).__bindgen_anon_2.create_object = Some(create_object);

for property in &self.property_entities {
property.declare(class_ce);
Expand Down
Loading

0 comments on commit d89a67c

Please sign in to comment.