Skip to content

Commit 76fc5fa

Browse files
committedJan 13, 2014
refactor objects
1 parent 56f675a commit 76fc5fa

File tree

2 files changed

+141
-147
lines changed

2 files changed

+141
-147
lines changed
 

Diff for: ‎ng.php

+4
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ public function getZendType()
193193
return "long";
194194
} else if (preg_match("/git_oid/", $this->type)) {
195195
return "char";
196+
} else if (preg_match("/git_otype/", $this->type)) {
197+
return "long";
196198
} else if (preg_match("/^git_/", $this->type)) {
197199
return "zval";
198200
} else if (preg_match("/payload/", $this->name)) {
@@ -457,6 +459,8 @@ public function generateProto(Printer $printer, Func $f)
457459
$printer->put("array");
458460
} else if (preg_match("/_cb$/", $arg->getType())) {
459461
$printer->put("Callable");
462+
} else if (preg_match("/git_otype/", $arg->getType())) {
463+
$printer->put("long");
460464
} else {
461465
error_log(sprintf("# unknown type (%s)", $arg->getType()));
462466
}

Diff for: ‎object.c

+137-147
Original file line numberDiff line numberDiff line change
@@ -2,94 +2,91 @@
22
#include "php_git2_priv.h"
33
#include "object.h"
44

5-
/* {{{ proto resource git_object_lookup(repo, id, type)
6-
*/
5+
/* {{{ proto long git_object_lookup(resource $repo, string $id, long $type)
6+
*/
77
PHP_FUNCTION(git_object_lookup)
88
{
9-
zval *repo;
10-
php_git2_t *_repo, *result;
11-
char *id = {0};
12-
int id_len;
13-
zval *type;
14-
php_git2_t *_type;
15-
int error = 0;
16-
git_object *object;
17-
git_oid oid;
9+
int result = 0, id_len = 0, error = 0;
10+
git_object *object = NULL;
11+
zval *repo = NULL;
12+
php_git2_t *_repo = NULL;
13+
char *id = NULL;
14+
git_oid __id = {0};
15+
long type = 0;
1816

1917
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
20-
"rsr", &repo, &id, &id_len, &type) == FAILURE) {
18+
"rsl", &repo, &id, &id_len, &type) == FAILURE) {
2119
return;
2220
}
21+
2322
ZEND_FETCH_RESOURCE(_repo, php_git2_t*, &repo, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
24-
if (git_oid_fromstrn(&oid, id, id_len) != GIT_OK) {
25-
return;
23+
if (git_oid_fromstrn(&__id, id, id_len)) {
24+
RETURN_FALSE;
2625
}
27-
error = git_object_lookup(&object, PHP_GIT2_V(_repo, repository), &oid, type);
28-
if (php_git2_check_error(error, "git_object_lookup" TSRMLS_CC)) {
29-
RETURN_FALSE
30-
}
31-
PHP_GIT2_MAKE_RESOURCE(result);
32-
PHP_GIT2_V(result, object) = object;
33-
result->type = PHP_GIT2_TYPE_OBJECT;
34-
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
35-
result->should_free_v = 1;
36-
37-
ZVAL_RESOURCE(return_value, result->resource_id);
26+
result = git_object_lookup(&object, PHP_GIT2_V(_repo, repository), &__id, type);
27+
RETURN_LONG(result);
3828
}
29+
/* }}} */
3930

40-
/* {{{ proto resource git_object_lookup_prefix(repo, id, len, type)
41-
*/
31+
/* {{{ proto resource git_object_lookup_prefix(resource $repo, string $id, long $len, long $type)
32+
*/
4233
PHP_FUNCTION(git_object_lookup_prefix)
4334
{
44-
zval *repo;
45-
php_git2_t *_repo;
46-
char *id = {0};
47-
int id_len;
48-
zval *type;
49-
php_git2_t *_type;
50-
51-
/* TODO(chobie): implement this */
52-
php_error_docref(NULL TSRMLS_CC, E_WARNING, "git_object_lookup_prefix not implemented yet");
53-
return;
54-
55-
// if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
56-
// "rsr", &repo, &id, &id_len, &len, &type) == FAILURE) {
57-
// return;
58-
// }
35+
php_git2_t *result = NULL, *_repo = NULL;
36+
git_object *object_out = NULL;
37+
zval *repo = NULL;
38+
char *id = NULL;
39+
int id_len = 0, error = 0;
40+
git_oid __id = {0};
41+
long len = 0, type = 0;
42+
43+
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
44+
"rsll", &repo, &id, &id_len, &len, &type) == FAILURE) {
45+
return;
46+
}
47+
5948
ZEND_FETCH_RESOURCE(_repo, php_git2_t*, &repo, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
49+
if (git_oid_fromstrn(&__id, id, id_len)) {
50+
RETURN_FALSE;
51+
}
52+
error = git_object_lookup_prefix(&object_out, PHP_GIT2_V(_repo, repository), &__id, len, type);
53+
if (php_git2_check_error(error, "git_object_lookup_prefix" TSRMLS_CC)) {
54+
RETURN_FALSE;
55+
}
56+
if (php_git2_make_resource(&result, PHP_GIT2_TYPE_OBJECT, object_out, 1 TSRMLS_CC)) {
57+
RETURN_FALSE;
58+
}
59+
ZVAL_RESOURCE(return_value, GIT2_RVAL_P(result));
6060
}
61+
/* }}} */
6162

62-
/* {{{ proto resource git_object_lookup_bypath(treeish, path, type)
63-
*/
63+
/* {{{ proto resource git_object_lookup_bypath(resource $treeish, string $path, long $type)
64+
*/
6465
PHP_FUNCTION(git_object_lookup_bypath)
6566
{
66-
zval *treeish;
67-
php_git2_t *_treeish, *result;
68-
char *path = {0};
69-
int path_len;
70-
zval *type;
71-
php_git2_t *_type;
72-
git_object *object;
73-
int error;
67+
php_git2_t *result = NULL, *_treeish = NULL;
68+
git_object *out = NULL;
69+
zval *treeish = NULL;
70+
char *path = NULL;
71+
int path_len = 0, error = 0;
72+
long type = 0;
7473

7574
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
76-
"rsr", &treeish, &path, &path_len, &type) == FAILURE) {
75+
"rsl", &treeish, &path, &path_len, &type) == FAILURE) {
7776
return;
7877
}
78+
7979
ZEND_FETCH_RESOURCE(_treeish, php_git2_t*, &treeish, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
80-
// TODO: cast object
81-
error = git_object_lookup_bypath(&object, PHP_GIT2_V(_treeish, tree), path, type);
80+
error = git_object_lookup_bypath(&out, PHP_GIT2_V(_treeish, object), path, type);
8281
if (php_git2_check_error(error, "git_object_lookup_bypath" TSRMLS_CC)) {
83-
RETURN_FALSE
82+
RETURN_FALSE;
8483
}
85-
PHP_GIT2_MAKE_RESOURCE(result);
86-
PHP_GIT2_V(result, object) = object;
87-
result->type = PHP_GIT2_TYPE_OBJECT;
88-
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
89-
result->should_free_v = 0;
90-
91-
ZVAL_RESOURCE(return_value, result->resource_id);
84+
if (php_git2_make_resource(&result, PHP_GIT2_TYPE_OBJECT, out, 1 TSRMLS_CC)) {
85+
RETURN_FALSE;
86+
}
87+
ZVAL_RESOURCE(return_value, GIT2_RVAL_P(result));
9288
}
89+
/* }}} */
9390

9491
/* {{{ proto resource git_object_id(obj)
9592
*/
@@ -109,84 +106,86 @@ PHP_FUNCTION(git_object_id)
109106
git_oid_fmt(buf, id);
110107
RETURN_STRING(buf, 1);
111108
}
109+
/* }}} */
112110

113-
/* {{{ proto resource git_object_type(obj)
114-
*/
111+
/* {{{ proto resource git_object_type(resource $obj)
112+
*/
115113
PHP_FUNCTION(git_object_type)
116114
{
117-
zval *obj;
118-
php_git2_t *_obj;
119-
const git_otype *type;
115+
git_otype *result = NULL;
116+
zval *obj = NULL;
117+
php_git2_t *_obj = NULL;
120118

121119
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
122120
"r", &obj) == FAILURE) {
123121
return;
124122
}
123+
125124
ZEND_FETCH_RESOURCE(_obj, php_git2_t*, &obj, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
126-
type = git_object_type(PHP_GIT2_V(_obj, object));
127-
RETURN_LONG(type);
125+
result = git_object_type(PHP_GIT2_V(_obj, object));
126+
RETURN_LONG(result);
128127
}
128+
/* }}} */
129129

130-
/* {{{ proto resource git_object_owner(obj)
131-
*/
130+
/* {{{ proto resource git_object_owner(resource $obj)
131+
*/
132132
PHP_FUNCTION(git_object_owner)
133133
{
134-
zval *obj;
135-
php_git2_t *_obj, *result;
136-
git_repository *repository;
134+
git_repository *result = NULL;
135+
zval *obj = NULL;
136+
php_git2_t *_obj = NULL, *__result = NULL;
137137

138138
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
139139
"r", &obj) == FAILURE) {
140140
return;
141141
}
142+
142143
ZEND_FETCH_RESOURCE(_obj, php_git2_t*, &obj, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
143-
// TODO: consider cast
144-
repository = git_object_owner(PHP_GIT2_V(_obj, object));
145-
PHP_GIT2_MAKE_RESOURCE(result);
146-
PHP_GIT2_V(result, repository) = repository;
147-
result->type = PHP_GIT2_TYPE_REPOSITORY;
148-
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
149-
result->should_free_v = 0;
150-
151-
ZVAL_RESOURCE(return_value, result->resource_id);
144+
result = git_object_owner(PHP_GIT2_V(_obj, object));
145+
if (php_git2_make_resource(&__result, PHP_GIT2_TYPE_OBJECT, result, 1 TSRMLS_CC)) {
146+
RETURN_FALSE;
147+
}
148+
ZVAL_RESOURCE(return_value, GIT2_RVAL_P(__result));
152149
}
150+
/* }}} */
153151

154-
/* {{{ proto void git_object_free(object)
155-
*/
152+
/* {{{ proto void git_object_free(resource $object)
153+
*/
156154
PHP_FUNCTION(git_object_free)
157155
{
158-
zval *object;
159-
php_git2_t *_object;
156+
zval *object = NULL;
157+
php_git2_t *_object = NULL;
160158

161159
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
162160
"r", &object) == FAILURE) {
163161
return;
164162
}
163+
165164
ZEND_FETCH_RESOURCE(_object, php_git2_t*, &object, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
166-
if (_object->should_free_v) {
165+
if (GIT2_SHOULD_FREE(_object)) {
167166
git_object_free(PHP_GIT2_V(_object, object));
168-
_object->should_free_v = 0;
169-
}
167+
GIT2_SHOULD_FREE(_object) = 0;
168+
};
170169
zval_ptr_dtor(&object);
171170
}
171+
/* }}} */
172172

173-
/* {{{ proto resource git_object_type2string(type)
174-
*/
173+
/* {{{ proto string git_object_type2string(long $type)
174+
*/
175175
PHP_FUNCTION(git_object_type2string)
176176
{
177-
zval *type;
178-
php_git2_t *_type;
179-
const char *result;
177+
const char *result = NULL;
178+
long type = 0;
180179

181180
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
182-
"r", &type) == FAILURE) {
181+
"l", &type) == FAILURE) {
183182
return;
184183
}
185-
ZEND_FETCH_RESOURCE(_type, php_git2_t*, &type, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
186-
// TODO: consider cast
187-
result = git_object_type2string(PHP_GIT2_V(_type, object));
184+
185+
result = git_object_type2string(type);
188186
RETURN_STRING(result, 1);
189187
}
188+
/* }}} */
190189

191190
/* {{{ proto resource git_object_string2type(str)
192191
*/
@@ -203,26 +202,24 @@ PHP_FUNCTION(git_object_string2type)
203202
type = git_object_string2type(str);
204203
RETURN_LONG(type);
205204
}
205+
/* }}} */
206206

207-
/* {{{ proto long git_object_typeisloose(type)
208-
*/
207+
/* {{{ proto long git_object_typeisloose(long $type)
208+
*/
209209
PHP_FUNCTION(git_object_typeisloose)
210210
{
211-
zval *type;
212-
php_git2_t *_type;
213-
int error = 0;
211+
int result = 0, error = 0;
212+
long type = 0;
214213

215214
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
216-
"r", &type) == FAILURE) {
215+
"l", &type) == FAILURE) {
217216
return;
218217
}
219-
ZEND_FETCH_RESOURCE(_type, php_git2_t*, &type, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
220-
error = git_object_typeisloose(PHP_GIT2_V(_type, object));
221-
if (php_git2_check_error(error, "git_object_typeisloose" TSRMLS_CC)) {
222-
RETURN_FALSE
223-
}
224-
RETURN_TRUE;
218+
219+
result = git_object_typeisloose(type);
220+
RETURN_LONG(result);
225221
}
222+
/* }}} */
226223

227224
/* {{{ proto resource git_object__size(type)
228225
*/
@@ -239,56 +236,49 @@ PHP_FUNCTION(git_object__size)
239236
RETURN_LONG(size);
240237
}
241238

242-
/* {{{ proto resource git_object_peel(object, target_type)
243-
*/
239+
/* {{{ proto long git_object_peel(resource $object, long $target_type)
240+
*/
244241
PHP_FUNCTION(git_object_peel)
245242
{
246-
zval *object;
247-
php_git2_t *_object, *result;
248-
zval *target_type;
249-
php_git2_t *_target_type;
250-
int error = 0;
251-
git_object *out;
252-
243+
int result = 0, error = 0;
244+
git_object *peeled = NULL;
245+
zval *object = NULL;
246+
php_git2_t *_object = NULL, *_result;
247+
long target_type = 0;
248+
253249
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
254-
"rr", &object, &target_type) == FAILURE) {
250+
"rl", &object, &target_type) == FAILURE) {
255251
return;
256252
}
253+
257254
ZEND_FETCH_RESOURCE(_object, php_git2_t*, &object, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
258-
error = git_object_peel(&out, PHP_GIT2_V(_object, object), target_type);
259-
if (php_git2_check_error(error, "git_object_peel" TSRMLS_CC)) {
260-
RETURN_FALSE
255+
result = git_object_peel(&peeled, PHP_GIT2_V(_object, object), target_type);
256+
if (php_git2_make_resource(&_result, PHP_GIT2_TYPE_OBJECT, peeled, 1 TSRMLS_CC)) {
257+
RETURN_FALSE;
261258
}
262-
PHP_GIT2_MAKE_RESOURCE(result);
263-
PHP_GIT2_V(result, object) = object;
264-
result->type = PHP_GIT2_TYPE_OBJECT;
265-
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
266-
result->should_free_v = 1;
267-
268-
ZVAL_RESOURCE(return_value, result->resource_id);
259+
ZVAL_RESOURCE(return_value, GIT_RVAL_P(_result));
269260
}
261+
/* }}} */
270262

271-
/* {{{ proto resource git_object_dup(source)
272-
*/
263+
/* {{{ proto long git_object_dup(resource $source)
264+
*/
273265
PHP_FUNCTION(git_object_dup)
274266
{
275-
zval *source;
276-
php_git2_t *_source, *result;
277-
git_object *dest;
278-
int error = 0;
267+
int result = 0, error = 0;
268+
git_object *dest = NULL;
269+
zval *source = NULL;
270+
php_git2_t *_source = NULL, *_result = NULL;
279271

280272
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
281273
"r", &source) == FAILURE) {
282274
return;
283275
}
276+
284277
ZEND_FETCH_RESOURCE(_source, php_git2_t*, &source, -1, PHP_GIT2_RESOURCE_NAME, git2_resource_handle);
285-
error = git_object_dup(&dest, PHP_GIT2_V(_source, object));
286-
PHP_GIT2_MAKE_RESOURCE(result);
287-
PHP_GIT2_V(result, object) = dest;
288-
result->type = PHP_GIT2_TYPE_OBJECT;
289-
result->resource_id = PHP_GIT2_LIST_INSERT(result, git2_resource_handle);
290-
result->should_free_v = 1;
291-
292-
ZVAL_RESOURCE(return_value, result->resource_id);
278+
result = git_object_dup(&dest, PHP_GIT2_V(_source, object));
279+
if (php_git2_make_resource(&_result, PHP_GIT2_TYPE_OBJECT, dest, 1 TSRMLS_CC)) {
280+
RETURN_FALSE;
281+
}
282+
ZVAL_RESOURCE(return_value, GIT_RVAL_P(_result));
293283
}
294-
284+
/* }}} */

0 commit comments

Comments
 (0)
Please sign in to comment.