Skip to content

Commit 5858a26

Browse files
committed
Fixing properties str parse failed.
1 parent d299cc1 commit 5858a26

File tree

10 files changed

+61
-50
lines changed

10 files changed

+61
-50
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ jobs:
4444
- name: Install Rust
4545
uses: actions-rs/toolchain@v1
4646
with:
47-
toolchain: nightly-2020-11-09
47+
toolchain: nightly
4848
override: true
4949
components: rustfmt, clippy
5050

@@ -64,12 +64,13 @@ jobs:
6464
uses: actions-rs/cargo@v1
6565
with:
6666
command: build
67+
args: --release
6768

6869
- name: Cargo test
6970
uses: actions-rs/cargo@v1
7071
with:
7172
command: test
72-
args: -- --nocapture
73+
args: --release -- --nocapture
7374

7475
- name: Cargo doc
7576
uses: actions-rs/cargo@v1

examples/simple/Makefile.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ TARGET_DIR = "${CARGO_MAKE_WORKING_DIRECTORY}/../../target"
44

55
[env.development]
66
TARGET_BUILD_DIR = "${TARGET_DIR}/debug"
7-
BUILD_ARGS = ""
8-
TEST_ARGS = ""
7+
BUILD_ARGS = "--"
8+
TEST_ARGS = "--"
99

1010
[env.production]
1111
TARGET_BUILD_DIR = "${TARGET_DIR}/release"

examples/simple/src/lib.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use phper::{
1212
compile::{create_zend_arg_info, MultiInternalArgInfo},
1313
ini::IniEntries,
1414
modules::{create_zend_module_entry, ModuleArgs, ModuleEntry},
15-
types::{ClassEntry, ExecuteData, SetVal, Val},
15+
types::{ClassEntry, ExecuteData, SetVal, Val, Value},
1616
},
1717
zend_get_module,
1818
};
@@ -35,7 +35,7 @@ static INI_ENTRIES: IniEntries<2> = IniEntries::new([
3535
fn m_init_simple(args: ModuleArgs) -> bool {
3636
args.register_ini_entries(&INI_ENTRIES);
3737
MY_CLASS_CE.init(c_str_ptr!("MyClass"), &MY_CLASS_METHODS);
38-
MY_CLASS_CE.declare_property("foo", "3", ZEND_ACC_PUBLIC);
38+
MY_CLASS_CE.declare_property("foo", "foo", ZEND_ACC_PUBLIC);
3939
true
4040
}
4141

@@ -131,8 +131,13 @@ pub fn my_class_foo(execute_data: ExecuteData) -> impl SetVal {
131131
zend_read_property(MY_CLASS_CE.get(), this, c_str_ptr!("foo"), 3, 1, null_mut())
132132
};
133133
let foo = Val::from_raw(foo);
134-
let foo = foo.as_c_str().unwrap().to_str().unwrap();
135-
format!("{}{}", prefix, foo)
134+
let value = foo.try_into_value().unwrap();
135+
136+
if let Value::CStr(foo) = value {
137+
Some(format!("{}{}", prefix, foo.to_str().unwrap()))
138+
} else {
139+
None
140+
}
136141
})
137142
}
138143

examples/simple/tests/php/test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
assert_eq(get_extension_funcs('simple'), ["test_simple"]);
88
assert_eq(test_simple("aaa", "bbb"), "a = aaa, a_len = 3, b = bbb, b_len = 3");
9-
assert_eq((new MyClass())->foo("foo-"), "foo-3");
9+
assert_eq((new MyClass())->foo("bar-"), "bar-foo");
1010

1111
function assert_eq($left, $right) {
1212
if ($left !== $right) {

phper-sys/php_wrapper.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@ zend_uchar phper_zval_get_type(const zval* pz) {
2525
void phper_zval_stringl(zval *return_value, const char *s, size_t len) {
2626
ZVAL_STRINGL(return_value, s, len);
2727
}
28+
29+
char *phper_z_strval_p(const zval *v) {
30+
return Z_STRVAL_P(v);
31+
}

phper-sys/php_wrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ zend_class_entry phper_init_class_entry(const char *class_name, const zend_funct
1212
void phper_zval_string(zval *return_value, const char *s);
1313
zend_uchar phper_zval_get_type(const zval* pz);
1414
void phper_zval_stringl(zval *return_value, const char *s, size_t len);
15+
char *phper_z_strval_p(const zval *v);
1516

1617
#endif //PHPER_PHP_WRAPPER_H

phper-test/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ fn php_context() -> &'static Context {
8282
ini_content.push_str(&read_to_string(ini_file).unwrap());
8383
for file in ini_files.split(',') {
8484
let file = file.trim();
85-
ini_content.push_str(&read_to_string(file).unwrap());
85+
if !file.is_empty() {
86+
ini_content.push_str(&read_to_string(file).unwrap());
87+
}
8688
}
8789

8890
Context {

phper/src/error.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1+
use std::str::Utf8Error;
2+
13
pub type Result<T> = std::result::Result<T, self::Error>;
24

35
#[derive(thiserror::Error, Debug)]
4-
pub enum Error {}
6+
pub enum Error {
7+
#[error(transparent)]
8+
Utf8(#[from] Utf8Error),
9+
10+
#[error("unknown value type `{0}`")]
11+
UnKnownValueType(u32),
12+
}

phper/src/zend/types.rs

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use crate::{
22
c_str_ptr,
33
sys::{
4-
phper_init_class_entry, phper_zval_get_type, phper_zval_stringl, zend_class_entry,
5-
zend_declare_property, zend_execute_data, zend_parse_parameters,
6-
zend_register_internal_class, zend_throw_exception, zval, IS_FALSE, IS_LONG, IS_NULL,
7-
IS_STRING, IS_TRUE, ZEND_RESULT_CODE_SUCCESS,
4+
self, phper_init_class_entry, phper_z_strval_p, phper_zval_get_type, phper_zval_stringl,
5+
zend_class_entry, zend_declare_property, zend_execute_data, zend_parse_parameters,
6+
zend_register_internal_class, zend_throw_exception, zval, IS_DOUBLE, IS_FALSE, IS_LONG,
7+
IS_NULL, IS_STRING, IS_STRING_EX, IS_TRUE, ZEND_RESULT_CODE_SUCCESS,
88
},
99
zend::{api::FunctionEntries, exceptions::Throwable},
1010
};
@@ -309,21 +309,6 @@ fn zend_parse_fixed_parameters(
309309
b == ZEND_RESULT_CODE_SUCCESS
310310
}
311311

312-
#[repr(u32)]
313-
pub enum ValType {
314-
UNDEF = crate::sys::IS_UNDEF,
315-
NULL = crate::sys::IS_NULL,
316-
FALSE = crate::sys::IS_FALSE,
317-
TRUE = crate::sys::IS_TRUE,
318-
LONG = crate::sys::IS_LONG,
319-
DOUBLE = crate::sys::IS_DOUBLE,
320-
STRING = crate::sys::IS_STRING,
321-
ARRAY = crate::sys::IS_ARRAY,
322-
OBJECT = crate::sys::IS_OBJECT,
323-
RESOURCE = crate::sys::IS_RESOURCE,
324-
REFERENCE = crate::sys::IS_REFERENCE,
325-
}
326-
327312
pub struct Val {
328313
raw: *mut zval,
329314
}
@@ -337,16 +322,8 @@ impl Val {
337322
self.raw
338323
}
339324

340-
pub fn as_c_str(&self) -> Option<&CStr> {
341-
unsafe {
342-
if phper_zval_get_type(self.raw) == IS_STRING as u8 {
343-
Some(CStr::from_ptr(
344-
(&((*(*self.raw).value.str).val)).as_ptr().cast(),
345-
))
346-
} else {
347-
None
348-
}
349-
}
325+
pub fn try_into_value<'a>(self) -> crate::Result<Value<'a>> {
326+
Value::from_zval(self.raw)
350327
}
351328

352329
unsafe fn type_info(&mut self) -> &mut u32 {
@@ -432,20 +409,33 @@ impl<T: SetVal, E: Throwable> SetVal for Result<T, E> {
432409
}
433410
}
434411

412+
#[derive(Debug)]
435413
pub enum Value<'a> {
436414
Null,
437415
Bool(bool),
438-
Str(&'a str),
439-
String(String),
416+
Long(i64),
417+
Double(f64),
418+
CStr(&'a CStr),
419+
Array(()),
420+
Object(()),
421+
Resource(()),
440422
}
441423

442-
impl SetVal for Value<'_> {
443-
fn set_val(self, val: &mut Val) {
444-
match self {
445-
Value::Null => ().set_val(val),
446-
Value::Bool(b) => b.set_val(val),
447-
Value::Str(s) => s.set_val(val),
448-
Value::String(s) => s.set_val(val),
424+
impl<'a> Value<'a> {
425+
pub fn from_zval(v: *const zval) -> crate::Result<Self> {
426+
unsafe {
427+
match phper_zval_get_type(v) as u32 {
428+
sys::IS_NULL => Ok(Self::Null),
429+
sys::IS_FALSE => Ok(Self::Bool(false)),
430+
sys::IS_TRUE => Ok(Self::Bool(true)),
431+
sys::IS_LONG => Ok(Self::Long((*v).value.lval)),
432+
sys::IS_DOUBLE => Ok(Self::Double((*v).value.dval)),
433+
sys::IS_STRING | sys::IS_STRING_EX => {
434+
let s = phper_z_strval_p(v);
435+
Ok(Self::CStr(CStr::from_ptr(s)))
436+
}
437+
t => Err(crate::Error::UnKnownValueType(t)),
438+
}
449439
}
450440
}
451441
}

rust-toolchain

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
nightly-2020-11-09
1+
nightly

0 commit comments

Comments
 (0)