Skip to content

Commit

Permalink
feat: C modules into separate translation units
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeLieutenant committed Jan 16, 2024
1 parent 45e4410 commit de50152
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 138 deletions.
30 changes: 30 additions & 0 deletions phper-sys/c/php_classes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <phper.h>

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;
}
36 changes: 36 additions & 0 deletions phper-sys/c/php_functions.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#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, zval params[static 1],
uint32_t param_count, HashTable *named_params) {

_call_user_function_impl(object, function_name, retval_ptr, param_count,
params, 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;
}
13 changes: 13 additions & 0 deletions phper-sys/c/php_interfaces.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <phper.h>

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;
}
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
23 changes: 12 additions & 11 deletions phper-sys/include/phper.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,10 @@ 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);

Expand All @@ -223,11 +221,13 @@ phper_register_interface_entry_ex(zend_class_entry *ce,

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, zval params[static 1],
uint32_t param_count, 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 +259,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
4 changes: 2 additions & 2 deletions phper/src/classes/entity.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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,
phper_init_class_entry_ex, phper_register_class_entry_ex,
zend_class_entry, zend_class_implements, zend_function_entry,
};

Expand Down Expand Up @@ -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
5 changes: 2 additions & 3 deletions phper/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
//! TODO Add lambda.
use crate::{
cg,
classes::{entry::ClassEntry, RawVisibility, Visibility},
errors::{throw, ArgumentCountError, ExceptionGuard, ThrowObject, Throwable},
objects::{StateObj, ZObj, ZObject},
Expand Down Expand Up @@ -469,15 +468,15 @@ pub(crate) fn call_internal(

call_raw_common(|ret| unsafe {
phper_call_user_function(
cg!(function_table),
object_val
.as_mut()
.map(|o| o.as_mut_ptr())
.unwrap_or(null_mut()),
func_ptr,
ret.as_mut_ptr(),
arguments.len() as u32,
arguments.as_mut_ptr().cast(),
arguments.len() as u32,
null_mut(),
);
})
}
Expand Down
8 changes: 1 addition & 7 deletions phper/src/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,7 @@ impl ExecuteData {
}
}

/// Gets mutable parameter by index.
pub fn get_mut_parameter(&mut self, index: usize) -> &mut ZVal {
unsafe {
let val = phper_zend_call_var_num(self.as_mut_ptr(), index.try_into().unwrap());
ZVal::from_mut_ptr(val)
}
}

}

/// Wrapper of [zval].
Expand Down

0 comments on commit de50152

Please sign in to comment.