Commit Graph

29 Commits

Author SHA1 Message Date
David
73526d62b5
Drop object contents rather than deallocating whole object (#57)
The object memory is automatically deallocated, however, anything that
has been allocated on the heap (strings, vectors etc.) must be
deallocated while we are freeing the object.
2021-09-05 00:53:29 +12:00
David
faad01b59d
Add ability to define properties when using macros (#56)
* Panic instead of return result when adding property

This will almost always be unwrapped

* Remove unused `set_refcount` function

Wasn't valid either way

* Add ability to get and set properties on class objects

* Updated documentation adding properties
2021-08-28 15:54:26 +12:00
David
3ea6f0c0bc
Allow returning of object references (#55)
* Allow returning of object references

`TYPE` is now also on `IntoZval` which means `FromZval` does not need to
be implemented on return types

* Clippy lints
2021-08-27 14:04:22 +12:00
David
1193cc0537
Replace struct derive macro with attribute macro (#54)
* Replace `ZendObjectOverride` derive macro with `#[php_class]`

* Updated guide with `#[php_class]` macro

* Panic rather than return error when implementing interface

It's gonna panic either way so might as well make it easier for the
builder

* Remove `libc` dependency
2021-08-26 01:04:33 +12:00
David
bd84f28054
Added ext-php-rs guide (#52)
* Started work on guide, added types

* Rewrite argument parser to allow referencs

Primarly so that `&str` is a valid parameter type.

* Remove generic `Into<String>` conversion to exception

* Worked on guide, added macros

* Build guide when building docs

* Allow manual trigger of docs build

* `cargo fmt`
2021-08-22 01:00:14 +12:00
David Cole
81dacc8cb0 Add From<Vec<T>> for binary types, tidy up 2021-08-20 16:19:07 +12:00
David
626c944218
Added #[php_extern] macro (#51)
* Fixed memory leak with `phpinfo()` table

* Added `#[php_extern]` macro
2021-08-19 12:36:33 +12:00
David
a2781b794a
Modify string parameters to be consistent (#50)
* Fix memory leaks with strings and zvals

Add PhpAllocator - not meant for usage, only for memory leak debugging.

* Replace `impl AsRef<str>` with `&str`

* Added `ZendString::as_str()`

* Tidy up

* Replace owned string parameters with `Into<String>`

* Remove backtrace feature, un-import allocator
2021-08-18 23:25:35 +12:00
David
84aaa07d3e
Add abstraction macros (#49)
* Remove uses of `unwrap`, improve library safety

* Started work on `#[php_function]` attribute

* Added `PhantomData` to `ZendHashTable`

Proper lifetimes for `HashTable` type

* `#[php_function]` now accepts `Vec<T>`

Refactored `ZendHashTable` iterators - there is now `Iter` and
`IntoIter` depending on whether it will consume the HashTable or not.

* Add support for nullable types

* Allow `optional` parameter on attribute

* Support primitive and `Option` return types

* Tidied and refactored attribute

* Added documentation for `#[php_function]`

Implemented `IntoZval` for `Option<T>` when `IntoZval` is also
implemented for `T`. `None` resolves to `null`.

* Added `#[php_method]` attribute

* Added `Callable` type, implemented `Drop` on `Zval`

While implementing `Drop`, the `Copy` derivation was removed, however,
this should not have been there in the first place (`Zval` is not valid
for `Copy` if it contains a string).

* Added some macro functions to example

* Add support for boolean arguments

* Added wrapper around binary data

Future support for binary arguments with `#[php_function]` macros.
Unpacking binary data is no longer unsafe. It was never really unsafe in
the beginning, as we were always reading valid memory, just the contents
of the data could not be trusted.

* Added `#[php_module]` attribute, added support for binary arguments

* Added defaults for functions

* Add defaults for methods

* Add startup function macro and prelude

* Refactored method adding

Now done through impl attribute

* Don't rename functions - generate another internal fn

Also hide all generated functions from docs

* Generate startup function when not already defined

* Add support for class and global constants

* Updated `skel` project

* Updated macro documentation, added executor globals

* Remove `Copy` bound for HashMap to Zval

* Updated documentation

* Add `FromZval` trait, updated docs

* Fixed clippy lints

* Fixed ZTS executor globals

* Fix clippy lint
2021-08-18 13:15:25 +12:00
David
2a0e1f8086
Remove uses of unwrap, improve library safety (#47)
* Remove uses of `unwrap`, improve library safety

* `set_array` doesn't return `Result`
2021-08-11 12:34:08 +12:00
David
b23b678a3d
Better interactions with objects (#41)
* Object functions now return references to objects

Added ability to get object class name as well as hashtable of
properties

* Return mutable reference to objects

* Optimized `unwrap_or_else` calls
2021-05-21 17:11:15 +12:00
David
de326e3171
Added ability to get and set properties (#39)
* Fixed adding properties to classes

* Removed unused function from wrapper header

* Moved property functions into `ZendObject`

* Introduced `ZendString` wrapper for raw pointer

* Set refcount of Zval to 0 when setting string properties

* Added ability to check if a property exists

Made some lifetimes explicit, check if property exists before attempting
to get.

* Removed unused file

* Removed unused lifetimes

Clippy was complaining, they were added to ensure that a `'static`
lifetime wasn't returned.
2021-05-21 15:08:10 +12:00
David
a75fcb50d2
Added ability to unpack and pack from Rust (#32)
* Added ability to `unpack` and `pack` from Rust

* Updated `Pack::pack_into` documentation

* Replaced constant with new flags, fixed example

* Accept slices/arrays when using `Zval::set_binary`
2021-05-05 22:43:06 +12:00
David
042c70af97
Fixed memory leak when returning an array (#34) 2021-05-03 19:59:29 +12:00
David
62a43e64d9
Added Debug and Clone implementations (#30)
* Implemented Debug and Clone for most types

* Added `throw!` macro to throw and return
2021-04-24 13:28:05 +12:00
David
815452f799
Refactored ZendHashTable conversions (#23)
We shouldn't really be returning Zval objects, rather references to Zval
objects. There is currently a memory leak with arrays that need to be
resolved.
2021-04-18 21:57:40 +12:00
David
472e26e8fb
Added ability to register constants (#22) 2021-04-18 16:48:58 +12:00
David
349d497793
Added ability to throw exceptions (#21)
* Exceptions have static lifetimes

* Added functions for throwing exceptions

Changed the `ClassEntry` implementation to unwrap the result before
returning, as these types are guaranteed to be valid. Also replaced the
'a lifetime with 'static lifetimes.
2021-04-18 15:52:02 +12:00
David Cole
45c7242f1e Added parse_args! macro for parsing arguments 2021-04-18 14:35:04 +12:00
David
860ffb3587
Created derive macro for ZendObjectHandler (#19)
* Initialize handlers on first touch

In the process of turning the two macros into one derive proc macro

* Changed the object handler macros into derive
2021-04-05 01:43:08 +12:00
David Cole
c6aa081a02 Rename crate to ext-php-rs 2021-04-02 18:12:30 +13:00
David
b6b4974236
Added ability to call functions given from PHP (#18)
* Added ability to call functions given from PHP

* Return an `Option` rather than a `Result`
2021-04-02 18:02:55 +13:00
David
e4deb1f93f
Implemented classes (#17)
* Add PHP thread safety to build matrix

* Revert "Add PHP thread safety to build matrix"

This reverts commit 7de868e6b6e329b87c412af8daf61cba1a3e29c5.

* Fixed arguments not being reconized by PHP

* Added function to create interned Zend string

* Changed statup/shutdown function types to Rust types

Prevents users having to import from bindings

* Added flags for classes

* WIP: class builder

* Added properties to classes

* Added class constants

* Added PHP base extensions
Added class inheritance

* Fixed memory leak with constants

Created C wrapper which is compiled and linked in the `build.rs`
script. In the process removed the `build_id()` function as now
we can just export the macro via a function which is defined in
`wrapper.c`.

* Fixed lint issue

* Added functions to set zval as different strings

Regular strings, persistent strings and interned strings

* Updated README with requirements

* Fixed lint issues

* Changed properties to use proper function

* Free zend string when it is changed for a persistent one

* WIP: overriding zend objects

* Added comments to the top of modules.

* Fixed some comments

* Initialize hash map with initial size when converting from HashMap and
Vec

* Removed unused imports

* WIP: object overrides

* Added DerefMut implementation for ZendClassObject

Tidied up lints that clippy was complaining about, removed debugging
statements.

* Fixed pointer dereference error in macro

* Added missing comments for trait

* All functions now take mutable references vs ptr

Removed SetZval implementation for *mut Zval.
Made bindings public, removed duplicate bindings.
Fixed `create_object` handler not working.
Note to self: smaller commits.

* Showcase ability to use shared object

* WIP: debugging property errors

Changed `function` to `method` in classes
Fixed doctest for `c_str`

* Disabled class properties temporarily

See #16

* Initialize args array with size
2021-04-02 14:18:45 +13:00
David
8685b1bf1d
Added support for PHP arrays (#13)
* Refactored types

Moved types into their own files within the types directory

* Moved Zval set logic into object
Updated example

* WIP: Implementation of hashtable

* Added ability to retrieve zend hashtable from Zval

* Change `ZendHashTable::push` to work with zend functions

Implement iterator for ZendHashTable

* Implemented immutable iterator for ZendHashTable

* Added implementations for converting HashMap and vectors to
ZendHashTable

* Changed ZendHashTable to a wrapper, zval array

Better design, allows for `Drop` implementation for arrays that
are created and unused.

* Allow `Zval::set_array` to be passed Vec and HashMap

* Added methods to retrieve values from Zend hashtable

* Added methods to remove values from Zend hashtable
2021-03-12 21:10:39 +13:00
David
8f108a61c3
Returning from functions (#9)
* Added ability to set value of zval

This allows the return value to now be set.
See the example in `example/skel/src.lib`.

* Added PHP internals book to resources

* Pass self as pointer to keep consistency
2021-03-11 01:06:58 +13:00
David
0dd1a3f80f
Added argument overloading (#8)
* Added argument overloading

If a double is requested but a long is available, we can cast the
long into a double. This is required if the user gives an integer
into a double field.

As well as this, if a string is requested but a long or double is
available, we can cast both of these into a string.

* Check if zval is present before setting in arg

* Added optional argument example

* Updated documentation for ZendLong

* WIP: added null checks
2021-03-10 21:09:18 +13:00
David
9fbe186098
Added argument parsing for functions (#6)
* Moved not required argument logic into function

Seemed more logical, as you can't have a required argument after the
not required argument. Usage:

```rs
FunctionBuilder::new("skeleton_version", skeleton_version)
    .arg(Arg::new("req_arg1", DataType::String))
    .arg(Arg::new("req_arg1", DataType::String))
    .not_required()
    .arg(Arg::new("not_req_arg1", DataType::String))
    .build()
```

* Added zend_value implementation

Helper functions to retrieve the value from zvals

* Started work on argument parser

* Added PHP version support notice

Due to the way the Zend API changes and the fact that the
stable interface is exposed via C macros, there is no
support for versions lower than PHP 8.0. In the future
we can support PHP 7.4 through Rust features.

* Added TryFrom implementations for Zval -> Types

* Added ability to select arguments from ExecutionData

* Added ability to parse arguments and retrieve val

See `example/skel/src/lib.rs` for usage.
2021-03-10 19:50:44 +13:00
David
52f9d40050
Added support for functions (#1)
* started work on functions

can now add functions, but can't return from them

* arg defaults to required
2021-03-10 11:43:17 +13:00
David Cole
7587dccd0d added module builder, example project, documentation 2021-03-09 20:47:20 +13:00