-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphp_closure_invoke.v
More file actions
141 lines (128 loc) · 3.05 KB
/
php_closure_invoke.v
File metadata and controls
141 lines (128 loc) · 3.05 KB
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
module vphp
import vphp.zend
// ======== 闭包桥接 ========
@[inline]
fn save_closure_value[T](v_cb T) voidptr {
mut saved_cb := unsafe { &T(zend.emalloc(usize(sizeof(T)))) }
unsafe {
*saved_cb = v_cb
}
return voidptr(saved_cb)
}
pub fn (ctx Context) create_saved_closure[T](v_cb T, bridge voidptr, arity int) {
zend.create_closure_with_arity_ptr(ctx.return().raw_ptr(), save_closure_value[T](v_cb), bridge,
arity, arity)
}
pub fn (ctx Context) create_saved_variadic_closure[T](v_cb T, bridge voidptr) {
zend.create_variadic_closure_ptr(ctx.return().raw_ptr(), save_closure_value[T](v_cb), bridge)
}
pub fn (ctx Context) invoke_struct_closure[T, A, R](cb T, args A) {
$match T {
fn (A) R {
res := cb(args)
ctx.return().v[R](res)
}
$else {
throw_exception('unsupported struct closure type', 0)
}
}
}
pub fn (ctx Context) invoke_struct_closure_void[T, A](cb T, args A) {
$match T {
fn (A) {
cb(args)
ctx.return().null()
}
$else {
throw_exception('unsupported struct closure type', 0)
}
}
}
pub fn (ctx Context) invoke_variadic_closure[T, R](cb T) {
$match T {
fn (...ZVal) R {
args := ctx.variadic_zval_args()
res := cb(...args)
ctx.return().v[R](res)
}
fn (...PhpValue) R {
args := ctx.variadic_php_value_args()
res := cb(...args)
ctx.return().v[R](res)
}
fn (...RequestBorrowedZBox) R {
args := ctx.variadic_borrowed_zbox_args()
res := cb(...args)
ctx.return().v[R](res)
}
fn (...VScalarValue) R {
args := ctx.variadic_v_scalar_args() or {
throw_exception(err.msg(), 0)
return
}
res := cb(...args)
ctx.return().v[R](res)
}
$else {
throw_exception('unsupported variadic closure type', 0)
}
}
}
pub fn (ctx Context) invoke_variadic_closure_void[T](cb T) {
$match T {
fn (...ZVal) {
args := ctx.variadic_zval_args()
cb(...args)
ctx.return().null()
}
fn (...PhpValue) {
args := ctx.variadic_php_value_args()
cb(...args)
ctx.return().null()
}
fn (...RequestBorrowedZBox) {
args := ctx.variadic_borrowed_zbox_args()
cb(...args)
ctx.return().null()
}
fn (...VScalarValue) {
args := ctx.variadic_v_scalar_args() or {
throw_exception(err.msg(), 0)
return
}
cb(...args)
ctx.return().null()
}
$else {
throw_exception('unsupported variadic closure type', 0)
}
}
}
fn (ctx Context) variadic_zval_args() []vphp.ZVal {
mut args := []ZVal{cap: ctx.num_args()}
for i in 0 .. ctx.num_args() {
args << ctx.arg_val(i)
}
return args
}
fn (ctx Context) variadic_php_value_args() []vphp.PhpValue {
mut args := []PhpValue{cap: ctx.num_args()}
for i in 0 .. ctx.num_args() {
args << ctx.arg_value(i)
}
return args
}
fn (ctx Context) variadic_borrowed_zbox_args() []vphp.RequestBorrowedZBox {
mut args := []RequestBorrowedZBox{cap: ctx.num_args()}
for i in 0 .. ctx.num_args() {
args << ctx.arg_borrowed_zbox(i)
}
return args
}
fn (ctx Context) variadic_v_scalar_args() ![]vphp.VScalarValue {
mut args := []VScalarValue{cap: ctx.num_args()}
for i in 0 .. ctx.num_args() {
args << ctx.arg_v_scalar(i)!
}
return args
}