Skip to content

Commit 5ea8cca

Browse files
committed
Add Zend String and Array.
1 parent 7443c14 commit 5ea8cca

File tree

15 files changed

+370
-247
lines changed

15 files changed

+370
-247
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ on:
77
branches: [ "**" ]
88

99
env:
10-
CARGO_TERM_COLOR: always
11-
# RUST_BACKTRACE: 1
1210
# RUST_LOG: debug
11+
CARGO_TERM_COLOR: always
12+
RUST_BACKTRACE: 1
1313
RUSTFLAGS: "-D warnings"
1414

1515
jobs:
@@ -49,12 +49,6 @@ jobs:
4949
override: true
5050
components: rustfmt, clippy
5151

52-
# - name: Install Cargo make
53-
# uses: actions-rs/cargo@v1
54-
# with:
55-
# command: install
56-
# args: cargo-make
57-
5852
- name: Cargo fmt
5953
uses: actions-rs/cargo@v1
6054
with:

examples/hello/Makefile.toml

Lines changed: 0 additions & 30 deletions
This file was deleted.

examples/hello/src/lib.rs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
use std::{fs::OpenOptions, io::Write};
12
use phper::{
23
c_str_ptr,
3-
classes::{Class, MethodEntity, StdClass, This},
4+
classes::{Class, StdClass, This},
45
functions::{create_zend_arg_info, Argument},
56
ini::Policy,
67
modules::{read_global_module, write_global_module, Module, ModuleArgs},
@@ -9,18 +10,23 @@ use phper::{
910
php_info_print_table_end, php_info_print_table_row, php_info_print_table_start,
1011
zend_function_entry, OnUpdateBool, PHP_INI_SYSTEM,
1112
},
12-
throws::{Exception, Throwable},
1313
values::{ExecuteData, Val},
14+
Throwable,
1415
};
15-
use std::{fs::OpenOptions, io::Write};
16+
use phper::arrays::Array;
17+
use phper::values::SetVal;
1618

1719
fn module_init(_args: ModuleArgs) -> bool {
1820
true
1921
}
2022

2123
fn say_hello(arguments: &mut [Val]) -> String {
2224
let name = arguments[0].as_string();
23-
format!("Hello, {}\n", name)
25+
format!("Hello, {}!\n", name)
26+
}
27+
28+
fn throw_exception(_: &mut [Val]) -> phper::Result<()> {
29+
Err(phper::Error::other("I am sorry"))
2430
}
2531

2632
#[php_get_module]
@@ -44,25 +50,34 @@ pub extern "C" fn get_module(module: &mut Module) {
4450

4551
// register functions
4652
module.add_function("hello_say_hello", say_hello, vec![Argument::by_val("name")]);
53+
module.add_function("hello_throw_exception", throw_exception, vec![]);
4754
module.add_function(
4855
"hello_get_all_ini",
49-
|_: &mut [Val]| -> Result<String, Exception> {
50-
let hello_enable = Module::get_bool_ini("hello.enable");
51-
dbg!(hello_enable);
56+
|_: &mut [Val]| -> Array {
57+
let mut arr = Array::new();
5258

53-
let hello_description = Module::get_str_ini("hello.description");
54-
dbg!(hello_description);
59+
let mut hello_enable = Val::null();
60+
Module::get_bool_ini("hello.enable").set_val(&mut hello_enable);
61+
arr.insert("hello.enable", &mut hello_enable);
5562

56-
Ok(String::new())
63+
let mut hello_description = Val::null();
64+
Module::get_str_ini("hello.description").set_val(&mut hello_description);
65+
arr.insert("hello.description", &mut hello_description);
66+
67+
arr
5768
},
5869
vec![],
5970
);
6071

6172
// register classes
6273
let mut std_class = StdClass::new();
6374
std_class.add_property("foo", 100);
64-
std_class.add_method("test1", |_: &mut This| {
65-
println!("hello test1");
66-
});
75+
std_class.add_method(
76+
"test1",
77+
|_: &mut This, _: &mut [Val]| {
78+
println!("hello test1");
79+
},
80+
vec![],
81+
);
6782
module.add_class("Test1", std_class);
6883
}

phper-macros/src/inner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub(crate) fn php_get_module(_attr: TokenStream, input: TokenStream) -> TokenStr
3131

3232
::phper::modules::write_global_module(internal);
3333
unsafe {
34-
::phper::modules::read_global_module(|module| {
34+
::phper::modules::write_global_module(|module| {
3535
module.module_entry()
3636
})
3737
}

phper-sys/php_wrapper.c

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
#include "php_wrapper.h"
22

3-
zend_string *phper_zend_string_init(const char *str, size_t len, int persistent) {
4-
return zend_string_init(str, len, persistent);
5-
}
6-
73
zend_string *phper_zend_new_interned_string(zend_string *str) {
84
return zend_new_interned_string(str);
95
}
@@ -22,6 +18,10 @@ zend_uchar phper_zval_get_type(const zval* pz) {
2218
return zval_get_type(pz);
2319
}
2420

21+
void phper_zval_arr(zval *return_value, zend_array *arr) {
22+
ZVAL_ARR(return_value, arr);
23+
}
24+
2525
void phper_zval_stringl(zval *return_value, const char *s, size_t len) {
2626
ZVAL_STRINGL(return_value, s, len);
2727
}
@@ -42,10 +42,18 @@ zend_string *phper_zval_get_string(zval *op) {
4242
return zval_get_string(op);
4343
}
4444

45-
void phper_zend_string_release(zend_string *s) {
46-
return zend_string_release(s);
47-
}
48-
4945
zend_long phper_zval_get_long(zval *op) {
5046
return zval_get_long(op);
5147
}
48+
49+
zend_string *phper_zend_string_init(const char *str, size_t len, int persistent) {
50+
return zend_string_init(str, len, persistent);
51+
}
52+
53+
zend_string *phper_zend_string_alloc(size_t len, int persistent) {
54+
return zend_string_alloc(len, persistent);
55+
}
56+
57+
void phper_zend_string_release(zend_string *s) {
58+
return zend_string_release(s);
59+
}

phper-sys/php_wrapper.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88

99
typedef ZEND_INI_MH(phper_zend_ini_mh);
1010

11-
zend_string *zend_string_init_(const char *str, size_t len, int persistent);
1211
zend_string *zend_new_interned_string_(zend_string *str);
1312
zend_class_entry phper_init_class_entry_ex(const char *class_name, size_t class_name_len, const zend_function_entry *functions);
1413
void phper_zval_string(zval *return_value, const char *s);
1514
zend_uchar phper_zval_get_type(const zval* pz);
15+
void phper_zval_arr(zval *return_value, zend_array *arr);
1616
void phper_zval_stringl(zval *return_value, const char *s, size_t len);
1717
char *phper_z_strval_p(const zval *v);
1818
zval *phper_get_this(zend_execute_data *execute_data);
@@ -21,4 +21,8 @@ zend_string *phper_zval_get_string(zval *op);
2121
void phper_zend_string_release(zend_string *s);
2222
zend_long phper_zval_get_long(zval *op);
2323

24+
zend_string *phper_zend_string_init(const char *str, size_t len, int persistent);
25+
zend_string *phper_zend_string_alloc(size_t len, int persistent);
26+
void phper_zend_string_release(zend_string *s);
27+
2428
#endif //PHPER_PHP_WRAPPER_H

phper/src/arrays.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
use crate::sys::*;
2+
use std::ptr::null_mut;
3+
use std::mem::zeroed;
4+
use crate::values::Val;
5+
use std::ops::{Deref, DerefMut};
6+
7+
pub struct Array {
8+
inner: Box<zend_array>,
9+
}
10+
11+
impl Array {
12+
pub fn new() -> Self {
13+
let mut inner = Box::new(unsafe { zeroed::<zend_array>() });
14+
unsafe {
15+
_zend_hash_init(&mut *inner, 0, None, 1);
16+
}
17+
Self {
18+
inner,
19+
}
20+
}
21+
22+
pub fn insert(&mut self, key: impl AsRef<str>, value: &mut Val) {
23+
let key = key.as_ref();
24+
unsafe {
25+
zend_hash_str_update(&mut *self.inner, key.as_ptr().cast(), key.len(), value.as_mut());
26+
}
27+
}
28+
29+
pub fn get(&mut self, key: impl AsRef<str>) -> &mut Val {
30+
let key = key.as_ref();
31+
unsafe {
32+
let value = zend_hash_str_find(&mut *self.inner, key.as_ptr().cast(), key.len());
33+
Val::from_mut(value)
34+
}
35+
}
36+
37+
pub fn len(&mut self) -> usize {
38+
unsafe {
39+
zend_array_count(&mut *self.inner) as usize
40+
}
41+
}
42+
}
43+
44+
impl AsRef<zend_array> for Array {
45+
fn as_ref(&self) -> &zend_array {
46+
self.inner.deref()
47+
}
48+
}
49+
50+
impl AsMut<zend_array> for Array {
51+
fn as_mut(&mut self) -> &mut zend_array {
52+
self.inner.deref_mut()
53+
}
54+
}
55+
56+
impl Drop for Array {
57+
fn drop(&mut self) {
58+
unsafe {
59+
zend_hash_destroy(&mut *self.inner);
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)