Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NFR] Native array functions #847

Merged
merged 2 commits into from Jul 15, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions ext/kernel/array.c
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -1392,3 +1392,63 @@ void phalcon_array_merge_recursive_n(zval **a1, zval *a2)
zend_hash_move_forward_ex(ah2, &hp2);
}
}

/**
* @brief array_unshift($arr, $arg)
* @param arr
* @param arg
* @note Reefernce count of @c arg will be incremented
*/
void phalcon_array_unshift(zval *arr, zval *arg)
{
if (likely(Z_TYPE_P(arr) == IS_ARRAY)) {
zval** args[1] = { &arg };
HashTable *newhash = php_splice(Z_ARRVAL_P(arr), 0, 0, args, 1, NULL);
HashTable oldhash = *Z_ARRVAL_P(arr);
*Z_ARRVAL_P(arr) = *newhash;

FREE_HASHTABLE(newhash);
zend_hash_destroy(&oldhash);
}
}

void phalcon_array_values(zval *return_value, zval *arr)
{
if (likely(Z_TYPE_P(arr) == IS_ARRAY)) {
zval **entry;
HashPosition pos;

array_init_size(return_value, zend_hash_num_elements(Z_ARRVAL_P(arr)));
for (
zend_hash_internal_pointer_reset_ex(Z_ARRVAL_P(arr), &pos);
zend_hash_get_current_data_ex(Z_ARRVAL_P(arr), (void **)&entry, &pos) == SUCCESS;
zend_hash_move_forward_ex(Z_ARRVAL_P(arr), &pos)
) {
Z_ADDREF_PP(entry);
zend_hash_next_index_insert(Z_ARRVAL_P(return_value), entry, sizeof(zval*), NULL);
}
}
}

int phalcon_array_key_exists(zval *arr, zval *key TSRMLS_DC)
{
HashTable *h = HASH_OF(arr);
if (h) {
switch (Z_TYPE_P(key)) {
case IS_STRING:
return zend_symtable_exists(h, Z_STRVAL_P(key), Z_STRLEN_P(key) + 1);

case IS_LONG:
return zend_hash_index_exists(h, Z_LVAL_P(key));

case IS_NULL:
return zend_hash_exists(h, "", 1);

default:
zend_error(E_WARNING, "The key should be either a string or an integer");
return 0;
}
}

return 0;
}
4 changes: 4 additions & 0 deletions ext/kernel/array.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,7 @@ extern void phalcon_fast_array_merge(zval *return_value, zval **array1, zval **a

/** Recursive merge */
extern void phalcon_array_merge_recursive_n(zval **a1, zval *a2);

extern void phalcon_array_unshift(zval *arr, zval *arg);
extern void phalcon_array_values(zval *return_value, zval *arr);
extern int phalcon_array_key_exists(zval *arr, zval *key TSRMLS_DC);
7 changes: 1 addition & 6 deletions ext/mvc/view/engine/volt/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -1126,12 +1126,7 @@ PHP_METHOD(Phalcon_Mvc_View_Engine_Volt_Compiler, resolveFilter){
phalcon_array_update_string(&resolved_param, SL("file"), &file, PH_COPY | PH_SEPARATE);
phalcon_array_update_string(&resolved_param, SL("line"), &line, PH_COPY | PH_SEPARATE);

/**
* TODO: Implement this function directly
*/
Z_SET_ISREF_P(func_arguments);
phalcon_call_func_p2_noret("array_unshift", func_arguments, resolved_param);
Z_UNSET_ISREF_P(func_arguments);
phalcon_array_unshift(func_arguments, resolved_param);
}

phalcon_call_method_p1(arguments, this_ptr, "expression", func_arguments);
Expand Down