Skip to content

Commit 2de2ab8

Browse files
committed
[config] add callback
1 parent 9e6e921 commit 2de2ab8

File tree

8 files changed

+413
-271
lines changed

8 files changed

+413
-271
lines changed

Diff for: g_config.c

+207-136
Large diffs are not rendered by default.

Diff for: g_config.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ ZEND_END_ARG_INFO()
163163
ZEND_BEGIN_ARG_INFO_EX(arginfo_git_config_foreach, 0, 0, 3)
164164
ZEND_ARG_INFO(0, cfg)
165165
ZEND_ARG_INFO(0, callback)
166-
ZEND_ARG_INFO(0, payload)
166+
ZEND_ARG_INFO(1, payload)
167167
ZEND_END_ARG_INFO()
168168

169169
ZEND_BEGIN_ARG_INFO_EX(arginfo_git_config_iterator_new, 0, 0, 2)
@@ -179,7 +179,7 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_git_config_foreach_match, 0, 0, 4)
179179
ZEND_ARG_INFO(0, cfg)
180180
ZEND_ARG_INFO(0, regexp)
181181
ZEND_ARG_INFO(0, callback)
182-
ZEND_ARG_INFO(0, payload)
182+
ZEND_ARG_INFO(1, payload)
183183
ZEND_END_ARG_INFO()
184184

185185
ZEND_BEGIN_ARG_INFO_EX(arginfo_git_config_get_mapped, 0, 0, 5)

Diff for: helper.c

-116
Original file line numberDiff line numberDiff line change
@@ -137,122 +137,6 @@ void php_git2_strarray_to_array(git_strarray *array, zval **out TSRMLS_DC)
137137
*out = result;
138138
}
139139

140-
int php_git2_make_resource(php_git2_t **out, enum php_git2_resource_type type, void *resource, int should_free TSRMLS_DC)
141-
{
142-
php_git2_t *result = NULL;
143-
144-
PHP_GIT2_MAKE_RESOURCE_NOCHECK(result);
145-
if (result == NULL) {
146-
return 1;
147-
}
148-
149-
switch (type) {
150-
case PHP_GIT2_TYPE_REPOSITORY:
151-
PHP_GIT2_V(result, repository) = (git_repository*)resource;
152-
break;
153-
case PHP_GIT2_TYPE_COMMIT:
154-
PHP_GIT2_V(result, commit) = (git_commit*)resource;
155-
break;
156-
case PHP_GIT2_TYPE_TREE:
157-
PHP_GIT2_V(result, commit) = (git_tree*)resource;
158-
break;
159-
case PHP_GIT2_TYPE_TREE_ENTRY:
160-
PHP_GIT2_V(result, tree_entry) = (git_tree_entry*)resource;
161-
break;
162-
case PHP_GIT2_TYPE_BLOB:
163-
PHP_GIT2_V(result, blob) = (git_blob*)resource;
164-
break;
165-
case PHP_GIT2_TYPE_REVWALK:
166-
PHP_GIT2_V(result, revwalk) = (git_revwalk*)resource;
167-
break;
168-
case PHP_GIT2_TYPE_TREEBUILDER:
169-
PHP_GIT2_V(result, treebuilder) = (git_treebuilder*)resource;
170-
break;
171-
case PHP_GIT2_TYPE_REFERENCE:
172-
PHP_GIT2_V(result, reference) = (git_reference*)resource;
173-
break;
174-
case PHP_GIT2_TYPE_REFERENCE_ITERATOR:
175-
PHP_GIT2_V(result, reference_iterator) = (git_reference_iterator*)resource;
176-
break;
177-
case PHP_GIT2_TYPE_CONFIG:
178-
PHP_GIT2_V(result, config) = (git_config*)resource;
179-
break;
180-
case PHP_GIT2_TYPE_OBJECT:
181-
PHP_GIT2_V(result, object) = (git_object*)resource;
182-
break;
183-
case PHP_GIT2_TYPE_INDEX:
184-
PHP_GIT2_V(result, index) = (git_index*)resource;
185-
break;
186-
case PHP_GIT2_TYPE_ODB:
187-
PHP_GIT2_V(result, odb) = (git_odb*)resource;
188-
break;
189-
case PHP_GIT2_TYPE_REFDB:
190-
PHP_GIT2_V(result, refdb) = (git_refdb*)resource;
191-
break;
192-
case PHP_GIT2_TYPE_STATUS_LIST:
193-
PHP_GIT2_V(result, status_list) = (git_status_list*)resource;
194-
break;
195-
case PHP_GIT2_TYPE_BRANCH_ITERATOR:
196-
PHP_GIT2_V(result, branch_iterator) = (git_branch_iterator*)resource;
197-
break;
198-
case PHP_GIT2_TYPE_TAG:
199-
PHP_GIT2_V(result, tag) = (git_tag*)resource;
200-
break;
201-
case PHP_GIT2_TYPE_CRED:
202-
PHP_GIT2_V(result, cred) = (git_cred*)resource;
203-
break;
204-
case PHP_GIT2_TYPE_TRANSPORT:
205-
PHP_GIT2_V(result, transport) = (git_transport*)resource;
206-
break;
207-
case PHP_GIT2_TYPE_REMOTE:
208-
PHP_GIT2_V(result, remote) = (git_remote*)resource;
209-
break;
210-
case PHP_GIT2_TYPE_DIFF:
211-
PHP_GIT2_V(result, diff) = (git_diff*)resource;
212-
break;
213-
case PHP_GIT2_TYPE_MERGE_RESULT:
214-
PHP_GIT2_V(result, merge_result) = (git_merge_result*)resource;
215-
break;
216-
case PHP_GIT2_TYPE_MERGE_HEAD:
217-
PHP_GIT2_V(result, merge_head) = (git_merge_head*)resource;
218-
break;
219-
case PHP_GIT2_TYPE_PATHSPEC:
220-
PHP_GIT2_V(result, pathspec) = (git_pathspec*)resource;
221-
break;
222-
case PHP_GIT2_TYPE_PATHSPEC_MATCH_LIST:
223-
PHP_GIT2_V(result, pathspec_match_list) = (git_pathspec_match_list*)resource;
224-
break;
225-
case PHP_GIT2_TYPE_PATCH:
226-
PHP_GIT2_V(result, patch) = (git_patch*)resource;
227-
break;
228-
case PHP_GIT2_TYPE_DIFF_HUNK:
229-
PHP_GIT2_V(result, diff_hunk) = (git_diff_hunk*)resource;
230-
break;
231-
case PHP_GIT2_TYPE_BUF:
232-
PHP_GIT2_V(result, buf) = (git_buf*)resource;
233-
break;
234-
case PHP_GIT2_TYPE_FILTER_LIST:
235-
PHP_GIT2_V(result, filter_list) = (git_filter_list*)resource;
236-
break;
237-
case PHP_GIT2_TYPE_FILTER_SOURCE:
238-
PHP_GIT2_V(result, filter_source) = (git_filter_source*)resource;
239-
break;
240-
case PHP_GIT2_TYPE_DIFF_LINE:
241-
PHP_GIT2_V(result, diff_line) = (git_diff_line*)resource;
242-
break;
243-
default:
244-
php_error_docref(NULL TSRMLS_CC, E_ERROR, "passed resource type does not support. probably bug.");
245-
}
246-
247-
result->type = type;
248-
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
249-
result->should_free_v = should_free;
250-
251-
*out = result;
252-
return 0;
253-
}
254-
255-
256140
int php_git2_call_function_v(
257141
zend_fcall_info *fci, zend_fcall_info_cache *fcc TSRMLS_DC, zval **retval_ptr_ptr, zend_uint param_count, ...)
258142
{

Diff for: helper.h

-2
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ void php_git2_signature_to_array(const git_signature *signature, zval **out TSRM
4242

4343
void php_git2_strarray_to_array(git_strarray *array, zval **out TSRMLS_DC);
4444

45-
int php_git2_make_resource(php_git2_t **out, enum php_git2_resource_type type, void *resource, int should_free TSRMLS_DC);
46-
4745
int php_git2_call_function_v(
4846
zend_fcall_info *fci, zend_fcall_info_cache *fcc TSRMLS_DC, zval **retval_ptr_ptr, zend_uint param_count, ...);
4947

Diff for: ng.php

+82-15
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ public function isSavior()
8484
}
8585
}
8686

87+
public function isCallbacker()
88+
{
89+
foreach($this->args as $arg) {
90+
if (preg_match("/_cb$/", $arg->getType())) {
91+
return true;
92+
}
93+
}
94+
return false;
95+
}
96+
8797
public function getArguments()
8898
{
8999
return $this->args;
@@ -137,6 +147,11 @@ public function getType()
137147
return $this->type;
138148
}
139149

150+
public function isCallback()
151+
{
152+
return preg_match("/_cb$/", $this->type);
153+
}
154+
140155
public function getPtr()
141156
{
142157
return str_repeat("*", $this->ptr - 1);
@@ -164,6 +179,8 @@ public function getZendType()
164179
return "char";
165180
} else if (preg_match("/^git_/", $this->type)) {
166181
return "zval";
182+
} else if (preg_match("/payload/", $this->name)) {
183+
return "zval";
167184
} else {
168185
error_log(sprintf("%s (zendtype)", $this->type));
169186
}
@@ -360,6 +377,7 @@ public function shouldResource(Arg $arg)
360377
"git_filter_source",
361378
"git_diff_line",
362379
"git_reference_iterator",
380+
"git_config_iterator",
363381
);
364382
}
365383

@@ -425,6 +443,8 @@ public function generateProto(Printer $printer, Func $f)
425443
$printer->put("array");
426444
} else if (preg_match("/(git_strarray)/", $arg->getType())) {
427445
$printer->put("array");
446+
} else if (preg_match("/_cb$/", $arg->getType())) {
447+
$printer->put("Callable");
428448
} else {
429449
error_log(sprintf("# unknown type (%s)", $arg->getType()));
430450
}
@@ -551,6 +571,20 @@ public function generateDeclarations(Printer $printer, Func $f)
551571
"value" => "NULL",
552572
);
553573
}
574+
if ($f->isCallbacker()) {
575+
$tables["zend_fcall_info"][] = array(
576+
"name" => "fci",
577+
"value" => "empty_fcall_info",
578+
);
579+
$tables["zend_fcall_info_cache"][] = array(
580+
"name" => "fcc",
581+
"value" => "empty_fcall_info_cache",
582+
);
583+
$tables["php_git2_cb_t"][] = array(
584+
"name" => "*cb",
585+
"value" => "NULL",
586+
);
587+
}
554588

555589

556590
foreach ($tables as $type => $values) {
@@ -594,6 +628,10 @@ public function generateParse(Printer $printer, Func $f)
594628
$printer->put("a");
595629
} else if ($this->shouldResource($arg)) {
596630
$printer->put("r");
631+
} else if ($arg->isCallback()) {
632+
$printer->put("f");
633+
} else if ($f->isCallbacker() && preg_match("/payload/", $arg->getName())) {
634+
$printer->put("z");
597635
} else {
598636
$printer->put("<{$arg->getType()}>");
599637
}
@@ -612,7 +650,12 @@ public function generateParse(Printer $printer, Func $f)
612650
continue;
613651
}
614652

615-
$printer->put("&`name`", "name", $arg->getName());
653+
if ($arg->isCallback()) {
654+
$printer->put("&fci, ");
655+
$printer->put("&fcc");
656+
} else {
657+
$printer->put("&`name`", "name", $arg->getName());
658+
}
616659
if (preg_match("/char/", $arg->getZendType())) {
617660
$printer->put(", ");
618661
$printer->put("&`name`_len", "name", $arg->getName());
@@ -648,26 +691,45 @@ public function generateFetchResourceIfNeeded(Printer $printer, Func $f)
648691
}
649692
}
650693

651-
public function generateOidTranslation(Printer $printer, Func $f)
652-
{
653-
foreach ($f->getArguments() as $arg) {
654-
/** @var Arg $arg */
655-
if ($arg->getType() == "git_oid") {
656-
$printer->put("if (git_oid_fromstrn(&__`name`, `name`, `name`_len)) {\n",
657-
"name", $arg->getName()
658-
);
659-
$printer->block(function(Printer $printer) use($arg) {
660-
$printer->put("RETURN_FALSE;\n");
661-
});
662-
$printer->put("}\n");
663-
$arg->setName("__" . $arg->getName());
664-
}
694+
public function generateOidTranslation(Printer $printer, Func $f)
695+
{
696+
foreach ($f->getArguments() as $arg) {
697+
/** @var Arg $arg */
698+
if ($arg->getType() == "git_oid") {
699+
$printer->put("if (git_oid_fromstrn(&__`name`, `name`, `name`_len)) {\n",
700+
"name", $arg->getName()
701+
);
702+
$printer->block(function(Printer $printer) use($arg) {
703+
$printer->put("RETURN_FALSE;\n");
704+
});
705+
$printer->put("}\n");
706+
$arg->setName("__" . $arg->getName());
665707
}
666708
}
709+
}
710+
711+
public function generateCallbackInit(Printer $printer, Func $f)
712+
{
713+
if ($f->isCallbacker()) {
714+
$printer->put("if (php_git2_cb_init(&cb, &fci, &fcc, payload TSRMLS_CC)) {\n");
715+
$printer->block(function(Printer $printer) {
716+
$printer->put("RETURN_FALSE;\n");
717+
});
718+
$printer->put("}\n");
719+
}
720+
}
721+
722+
public function generateCallbackFree(Printer $printer, Func $f)
723+
{
724+
if ($f->isCallbacker()) {
725+
$printer->put("php_git2_cb_free(cb);\n");
726+
}
727+
}
667728

668729
public function generateFunctionCall(Printer $printer, Func $f)
669730
{
670731
$this->generateOidTranslation($printer, $f);
732+
$this->generateCallbackInit($printer, $f);
671733
if ($f->getReturnType() == "int" && $f->isResourceCreator()) {
672734
$printer->put("error = `function`",
673735
"function", $f->getName()
@@ -801,6 +863,10 @@ public function generateFunctionCall(Printer $printer, Func $f)
801863
);
802864
} else if ($arg->shouldWrite()) {
803865
$printer->put("&`name`", "name", $arg->getName());
866+
} else if ($arg->isCallback()) {
867+
$printer->put("<CHANGEME>");
868+
} else if (preg_match("/payload/", $arg->getName())) {
869+
$printer->put("cb");
804870
} else {
805871
$printer->put("`name`", "name", $arg->getName());
806872
}
@@ -812,6 +878,7 @@ public function generateFunctionCall(Printer $printer, Func $f)
812878
}
813879
$printer->put(");\n");
814880

881+
$this->generateCallbackFree($printer, $f);
815882
if (preg_match("/_is_/", $f->getName())) {
816883
$printer->put("RETURN_BOOL(`name`);\n", "name", "result");
817884
} else {

0 commit comments

Comments
 (0)