forked from RogerGee/php-git2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
php-object.h
750 lines (595 loc) · 20.3 KB
/
php-object.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
/*
* php-object.h
*
* Copyright (C) Roger P. Gee
*/
#ifndef PHPGIT2_PHPOBJECT_H
#define PHPGIT2_PHPOBJECT_H
#include "php-type.h"
#include "php-callback.h"
#include <new>
extern "C" {
#include <git2/sys/odb_backend.h>
#include <git2/sys/refdb_backend.h>
#include <git2/sys/config.h>
}
// Helper macros
#define is_subclass_of(a,b) \
(a == b || instanceof_function(a,b))
#define PHP_EMPTY_METHOD(C,M) \
PHP_METHOD(C,M) { }
namespace php_git2
{
// Define the custom object types provided by php-git2. The value of each
// element in this enumeration corresponds to the index of the class entry
// structure in php_git2::class_entry.
enum php_git2_object_t
{
php_git2_odb_backend_obj,
php_git2_odb_backend_internal_obj,
php_git2_odb_writepack_obj,
php_git2_odb_writepack_internal_obj,
php_git2_odb_stream_obj,
php_git2_odb_stream_internal_obj,
php_git2_writestream_obj,
php_git2_config_backend_obj,
php_git2_refdb_backend_obj,
php_git2_refdb_backend_internal_obj,
php_git2_closure_obj,
_php_git2_obj_top_
};
// Store class entry for global lookup by php_git2_object_t enumerator.
extern zend_class_entry* class_entry[];
// Define generic class for custom zend_object. This type must employ
// standard layout.
template<typename StorageType>
struct php_zend_object
{
php_zend_object(zend_class_entry* ce)
{
void* ptr = emalloc(sizeof(StorageType));
storage = new(ptr) StorageType();
zend_object_std_init(&std,ce);
object_properties_init(&std,ce);
std.handlers = &handlers;
}
~php_zend_object()
{
zend_object_std_dtor(&std);
storage->~StorageType();
efree(storage);
}
StorageType* storage; // indirect to force standard layout
zend_object std; // last
static zend_object_handlers handlers;
static void init(zend_class_entry* ce);
static constexpr size_t offset()
{
return offsetof(php_zend_object,std);
}
static inline php_zend_object* get_wrapper(zend_object* zo)
{
return reinterpret_cast<php_zend_object*>(
reinterpret_cast<char*>(zo) - offset());
}
static inline StorageType* get_storage(zend_object* zo)
{
return reinterpret_cast<php_zend_object*>(
reinterpret_cast<char*>(zo) - offset()
)->storage;
}
static inline StorageType* get_storage(zval* zv)
{
return reinterpret_cast<php_zend_object*>(
reinterpret_cast<char*>(Z_OBJ_P(zv)) - offset()
)->storage;
}
};
// Define base class for object PHP values.
template<typename StorageType>
class php_object:
public php_value_base
{
using wrapper_t = php_zend_object<StorageType>;
public:
StorageType* get_storage()
{
return wrapper_t::get_storage(Z_OBJ(value));
}
private:
virtual void parse_impl(zval* zvp,int argno)
{
int result;
zval* dummy;
zend_class_entry* ce = php_git2::class_entry[StorageType::get_type()];
result = zend_parse_parameter(ZEND_PARSE_PARAMS_THROW,
argno,
zvp,
"O",
&dummy,
ce);
if (result == FAILURE) {
throw php_git2_propagated_exception();
}
ZVAL_COPY_VALUE(&value,zvp);
}
};
// Define a type for wrapping method calls.
template<typename BackendType,typename StorageType>
class php_object_wrapper
{
using wrapper_t = php_zend_object<StorageType>;
public:
php_object_wrapper(typename BackendType::base_class* base):
backendObject(static_cast<BackendType*>(base))
{
}
zval* thisobj()
{
return &backendObject->thisobj;
}
BackendType* object()
{
return backendObject;
}
StorageType* backing()
{
return wrapper_t::get_storage(Z_OBJ(backendObject->thisobj));
}
private:
BackendType* backendObject;
};
template<typename BackendType,typename StorageType>
class php_method_wrapper:
public php_object_wrapper<BackendType,StorageType>
{
public:
using object_wrapper = php_object_wrapper<BackendType,StorageType>;
php_method_wrapper(const char* methodName,typename BackendType::base_class* base):
object_wrapper(base)
{
ZVAL_STRING(&zmethod,methodName);
ZVAL_NULL(&zretval);
}
~php_method_wrapper()
{
zval_dtor(&zmethod);
zval_dtor(&zretval);
}
int call()
{
return php_git2_invoke_callback(
object_wrapper::thisobj(),
&zmethod,
&zretval,
0,
nullptr);
}
template<unsigned Count>
int call(zval_array<Count>& params)
{
return params.call(object_wrapper::thisobj(),&zmethod,&zretval);
}
zval* retval()
{
return &zretval;
}
private:
zval zmethod;
zval zretval;
};
// Define custom storage types for custom classes.
struct php_odb_backend_object
{
enum backend_kind
{
unset,
conventional,
user,
custom
};
php_odb_backend_object();
~php_odb_backend_object();
git_odb_backend* backend;
backend_kind kind;
php_git_odb* owner;
void create_custom_backend(zval* obj);
void create_conventional_backend(php_git_odb* newOwner)
{
assign_owner(newOwner);
kind = conventional;
}
void assign_owner(php_git_odb* newOwner);
void unset_backend(zval* obj);
constexpr static php_git2_object_t get_type()
{
return php_git2_odb_backend_obj;
}
private:
// Provide a custom odb_backend derivation that remembers the PHP object
// to which it's attached.
struct git_odb_backend_php:
git_odb_backend
{
typedef git_odb_backend base_class;
git_odb_backend_php(zend_object* obj);
~git_odb_backend_php();
zval thisobj;
};
using method_wrapper = php_method_wrapper<
php_odb_backend_object::git_odb_backend_php,
php_odb_backend_object
>;
// Function entries for custom odb_backend implementations provided from
// PHP userspace.
static int read(void** bufferp,
size_t* sizep,
git_object_t* typep,
git_odb_backend* backend,
const git_oid* oid);
static int read_prefix(git_oid* oidp,
void** bufferp,
size_t* sizep,
git_object_t* typep,
git_odb_backend* backend,
const git_oid* prefix,
size_t len);
static int read_header(size_t* sizep,
git_object_t* typep,
git_odb_backend* backend,
const git_oid* oid);
static int write(git_odb_backend* backend,
const git_oid* oid,
const void* data,
size_t size,
git_object_t type);
static int writestream(git_odb_stream** streamp,
git_odb_backend* backend,
git_object_size_t length,
git_object_t type);
static int readstream(git_odb_stream** streamp,
size_t* sizep,
git_object_t* typep,
git_odb_backend* backend,
const git_oid* oid);
static int exists(git_odb_backend* backend,const git_oid* oid);
static int exists_prefix(git_oid* oidp,
git_odb_backend* backend,
const git_oid* prefix,
size_t len);
static int refresh(git_odb_backend* backend);
static int foreach(git_odb_backend* backend,
git_odb_foreach_cb cb,
void* payload);
static int writepack(git_odb_writepack** writepackp,
git_odb_backend* backend,
git_odb* odb,
git_transfer_progress_cb progress_cb,
void* progress_payload);
static void free(git_odb_backend* backend);
};
struct php_odb_backend_internal_object : php_odb_backend_object
{
php_odb_backend_internal_object();
constexpr static php_git2_object_t get_type()
{
return php_git2_odb_backend_internal_obj;
}
};
struct php_odb_writepack_object
{
php_odb_writepack_object();
~php_odb_writepack_object();
git_odb_writepack* writepack;
php_git_odb* owner;
void create_custom_writepack(zval* zobj,zval* backend);
void assign_owner(php_git_odb* newOwner);
void unset_writepack()
{
writepack = nullptr;
}
constexpr static php_git2_object_t get_type()
{
return php_git2_odb_writepack_obj;
}
private:
// Provide a custom derivation to handle subclasses.
struct git_odb_writepack_php:
git_odb_writepack
{
typedef git_odb_writepack base_class;
git_odb_writepack_php(zend_object* obj);
~git_odb_writepack_php();
zval thisobj;
};
using method_wrapper = php_method_wrapper<
php_odb_writepack_object::git_odb_writepack_php,
php_odb_writepack_object
>;
static int append(git_odb_writepack* writepack,
const void* data,
size_t length,
git_transfer_progress* prog);
static int commit(git_odb_writepack* writepack,git_transfer_progress* prog);
static void free(git_odb_writepack* writepack);
};
struct php_odb_writepack_internal_object : php_odb_writepack_object
{
php_odb_writepack_internal_object();
~php_odb_writepack_internal_object();
git_transfer_progress prog;
php_callback_sync* cb;
zval backend;
constexpr static php_git2_object_t get_type()
{
return php_git2_odb_writepack_internal_obj;
}
};
struct php_odb_stream_object
{
enum stream_kind
{
unset,
conventional,
user,
custom
};
php_odb_stream_object();
~php_odb_stream_object();
git_odb_stream* stream;
stream_kind kind;
php_git_odb* owner;
zval backend;
void create_custom_stream(zval* zobj,zval* backendObject,unsigned int mode);
void assign_owner(php_git_odb* owner);
void unset_stream()
{
stream = nullptr;
kind = unset;
}
constexpr static php_git2_object_t get_type()
{
return php_git2_odb_stream_obj;
}
private:
// Provide a custom derivation to handle subclasses.
struct git_odb_stream_php:
git_odb_stream
{
typedef git_odb_stream base_class;
git_odb_stream_php(zend_object* obj,unsigned int mode);
~git_odb_stream_php();
zval thisobj;
};
using method_wrapper = php_method_wrapper<
php_odb_stream_object::git_odb_stream_php,
php_odb_stream_object
>;
static int read(git_odb_stream *stream,char *buffer,size_t len);
static int write(git_odb_stream *stream,const char *buffer,size_t len);
static int finalize_write(git_odb_stream *stream,const git_oid *oid);
static void free(git_odb_stream *stream);
};
struct php_odb_stream_internal_object : php_odb_stream_object
{
php_odb_stream_internal_object();
constexpr static php_git2_object_t get_type()
{
return php_git2_odb_stream_internal_obj;
}
};
struct php_writestream_object
{
php_writestream_object();
~php_writestream_object();
git_writestream* ws;
constexpr static php_git2_object_t get_type()
{
return php_git2_writestream_obj;
}
};
struct php_config_backend_object
{
php_config_backend_object();
git_config_backend* backend;
php_git_config* owner;
void create_custom_backend(zval* zobj,php_git_config* owner);
constexpr static php_git2_object_t get_type()
{
return php_git2_config_backend_obj;
}
private:
// Provide a custom config_backend derivation that remembers the PHP
// object to which it's attached.
struct git_config_backend_php:
git_config_backend
{
typedef git_config_backend base_class;
git_config_backend_php(zend_object* obj);
~git_config_backend_php();
zval thisobj;
};
using method_wrapper = php_method_wrapper<
php_config_backend_object::git_config_backend_php,
php_config_backend_object
>;
// Function entries for custom config_backend implementations provided
// from PHP userspace.
static int open(git_config_backend* cfg,git_config_level_t level,const git_repository* repo);
static int get(git_config_backend* cfg,const char* key,git_config_entry** out);
static int set(git_config_backend* cfg,const char* name,const char* value);
static int set_multivar(git_config_backend* cfg,
const char* name,
const char* regexp,
const char* value);
static int del(git_config_backend* cfg,const char* name);
static int del_multivar(git_config_backend* cfg,const char* name,const char* regexp);
static int iterator(git_config_iterator** iter,git_config_backend* cfg);
static int snapshot(git_config_backend** out,git_config_backend* cfg);
static int lock(git_config_backend* cfg);
static int unlock(git_config_backend* cfg,int success);
static void free(git_config_backend* cfg);
};
struct php_refdb_backend_object
{
enum backend_kind
{
unset,
conventional,
user,
custom
};
php_refdb_backend_object();
~php_refdb_backend_object();
git_refdb_backend* backend;
backend_kind kind;
php_git_refdb* owner;
void create_custom_backend(zval* zobj);
void create_conventional_backend(php_git_refdb* newOwner)
{
assign_owner(newOwner);
kind = conventional;
}
void assign_owner(php_git_refdb* owner);
constexpr static php_git2_object_t get_type()
{
return php_git2_refdb_backend_obj;
}
private:
struct git_refdb_backend_php:
git_refdb_backend
{
typedef git_refdb_backend base_class;
git_refdb_backend_php(zend_object* obj);
~git_refdb_backend_php();
zval thisobj;
zval lockobj;
};
using method_wrapper = php_method_wrapper<
php_refdb_backend_object::git_refdb_backend_php,
php_refdb_backend_object
>;
static int exists(
int *exists,
git_refdb_backend *backend,
const char *ref_name);
static int lookup(
git_reference **out,
git_refdb_backend *backend,
const char *ref_name);
static int iterator(
git_reference_iterator **iter,
struct git_refdb_backend *backend,
const char *glob);
static int write(git_refdb_backend *backend,
const git_reference *ref,
int force,
const git_signature *who,
const char *message,
const git_oid *old,
const char *old_target);
static int rename(
git_reference **out,
git_refdb_backend *backend,
const char *old_name,
const char *new_name,
int force,
const git_signature *who,
const char *message);
static int del(
git_refdb_backend *backend,
const char *ref_name,
const git_oid *old_id,
const char *old_target);
static int compress(git_refdb_backend *backend);
static int has_log(git_refdb_backend *backend, const char *refname);
static int ensure_log(git_refdb_backend *backend, const char *refname);
static int reflog_read(git_reflog **out,
git_refdb_backend *backend,
const char *name);
static int reflog_write(git_refdb_backend *backend, git_reflog *reflog);
static int reflog_rename(git_refdb_backend *backend,
const char *old_name,
const char *new_name);
static int reflog_delete(git_refdb_backend *backend, const char *name);
static int lock(void **payload_out, git_refdb_backend *backend, const char *refname);
static int unlock(git_refdb_backend *backend,
void *payload,
int success,
int update_reflog,
const git_reference *ref,
const git_signature *sig,
const char *message);
static void free(git_refdb_backend *backend);
};
struct php_refdb_backend_internal_object : php_refdb_backend_object
{
php_refdb_backend_internal_object();
~php_refdb_backend_internal_object();
git_reference_iterator* iter;
constexpr static php_git2_object_t get_type()
{
return php_git2_refdb_backend_internal_obj;
}
};
struct php_closure_object
{
typedef void (*closure_dstor)(void*);
php_closure_object();
~php_closure_object();
zend_function func;
void* payload;
bool hasPayload;
closure_dstor payloadDestructor;
constexpr static php_git2_object_t get_type()
{
return php_git2_closure_obj;
}
};
// Provide a routine to call during MINIT for registering the custom
// classes.
void php_git2_register_classes();
// Provide functions for creating objects easily from the extension. These
// *should* be used when instantiating an object from the extension. In the
// case of abstract backend classes, these functions create an object of a
// derived, internal type.
void php_git2_make_object(zval* zp,php_git2_object_t type);
void php_git2_make_odb_backend(zval* zp,git_odb_backend* backend,php_git_odb* owner);
void php_git2_make_odb_writepack(zval* zp,
git_odb_writepack* writepack,
php_callback_sync* cb,
zval* backend,
php_git_odb* owner);
void php_git2_make_odb_stream(zval* zp,
git_odb_stream* stream,
php_git_odb* owner);
void php_git2_make_writestream(zval* zp,
git_writestream* ws);
void php_git2_make_refdb_backend(zval* zp,
git_refdb_backend* backend,
php_git_refdb* owner);
// Useful helpers
bool is_method_overridden(zend_class_entry* ce,const char* method,int len);
zend_function* not_allowed_get_constructor(zend_object* object);
zend_function* disallow_base_get_constructor(zend_object* object);
// Function entry lists for class methods.
extern zend_function_entry odb_backend_methods[];
extern zend_function_entry odb_backend_internal_methods[];
extern zend_function_entry odb_writepack_methods[];
extern zend_function_entry odb_writepack_internal_methods[];
extern zend_function_entry odb_stream_methods[];
extern zend_function_entry odb_stream_internal_methods[];
extern zend_function_entry writestream_methods[];
extern zend_function_entry config_backend_methods[];
extern zend_function_entry refdb_backend_methods[];
extern zend_function_entry refdb_backend_internal_methods[];
extern zend_function_entry closure_methods[];
}
#endif
/*
* Local Variables:
* mode:c++
* indent-tabs-mode:nil
* tab-width:4
* End:
*/