forked from phpv8/v8js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v8js_v8object_class.cc
791 lines (641 loc) · 22.1 KB
/
v8js_v8object_class.cc
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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
/*
+----------------------------------------------------------------------+
| PHP Version 5 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2016 The PHP Group |
+----------------------------------------------------------------------+
| http://www.opensource.org/licenses/mit-license.php MIT License |
+----------------------------------------------------------------------+
| Author: Jani Taskinen <[email protected]> |
| Author: Patrick Reilly <[email protected]> |
| Author: Stefan Siegl <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php_v8js_macros.h"
#include "v8js_exceptions.h"
#include "v8js_v8.h"
#include "v8js_v8object_class.h"
extern "C" {
#include "ext/date/php_date.h"
#include "ext/standard/php_string.h"
#include "zend_interfaces.h"
#include "zend_closures.h"
#include "ext/spl/spl_exceptions.h"
#include "zend_exceptions.h"
}
/* {{{ Class Entries */
zend_class_entry *php_ce_v8object;
zend_class_entry *php_ce_v8function;
zend_class_entry *php_ce_v8generator;
/* }}} */
/* {{{ Object Handlers */
static zend_object_handlers v8js_v8object_handlers;
static zend_object_handlers v8js_v8generator_handlers;
/* }}} */
#define V8JS_V8_INVOKE_FUNC_NAME "V8Js::V8::Invoke"
/* V8 Object handlers */
static int v8js_v8object_has_property(zval *object, zval *member, int has_set_exists, void **cache_slot TSRMLS_DC) /* {{{ */
{
/* param has_set_exists:
* 0 (has) whether property exists and is not NULL - isset()
* 1 (set) whether property exists and is true-ish - empty()
* 2 (exists) whether property exists - property_exists()
*/
int retval = false;
v8js_v8object *obj = Z_V8JS_V8OBJECT_OBJ_P(object);
if (!obj->ctx) {
zend_throw_exception(php_ce_v8js_exception,
"Can't access V8Object after V8Js instance is destroyed!", 0 TSRMLS_CC);
return retval;
}
V8JS_CTX_PROLOGUE_EX(obj->ctx, retval);
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
if (Z_TYPE_P(member) == IS_STRING && v8obj->IsObject())
{
v8::Local<v8::Object> jsObj = v8obj->ToObject();
v8::Local<v8::String> jsKey = V8JS_ZSTR(Z_STR_P(member));
v8::Local<v8::Value> jsVal;
/* Skip any prototype properties */
if (jsObj->HasRealNamedProperty(jsKey) || jsObj->HasRealNamedCallbackProperty(jsKey)) {
if (has_set_exists == 2) {
/* property_exists(), that's enough! */
retval = true;
} else {
/* We need to look at the value. */
jsVal = jsObj->Get(jsKey);
if (has_set_exists == 0 ) {
/* isset(): We make 'undefined' equivalent to 'null' */
retval = !( jsVal->IsNull() || jsVal->IsUndefined() );
} else {
/* empty() */
retval = jsVal->BooleanValue();
/* for PHP compatibility, [] should also be empty */
if (jsVal->IsArray() && retval) {
v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(jsVal);
retval = (array->Length() != 0);
}
/* for PHP compatibility, '0' should also be empty */
if (jsVal->IsString() && retval) {
v8::Local<v8::String> str = jsVal->ToString();
if (str->Length() == 1) {
uint16_t c = 0;
str->Write(&c, 0, 1);
if (c == '0') {
retval = false;
}
}
}
}
}
}
}
return retval;
}
/* }}} */
static zval *v8js_v8object_read_property(zval *object, zval *member, int type, void **cache_slot, zval *rv TSRMLS_DC) /* {{{ */
{
zval *retval = rv;
v8js_v8object *obj = Z_V8JS_V8OBJECT_OBJ_P(object);
if (!obj->ctx) {
zend_throw_exception(php_ce_v8js_exception,
"Can't access V8Object after V8Js instance is destroyed!", 0 TSRMLS_CC);
return retval;
}
V8JS_CTX_PROLOGUE_EX(obj->ctx, retval);
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
if (Z_TYPE_P(member) == IS_STRING && v8obj->IsObject())
{
v8::Local<v8::Object> jsObj = v8obj->ToObject();
v8::Local<v8::String> jsKey = V8JS_ZSTR(Z_STR_P(member));
v8::Local<v8::Value> jsVal;
/* Skip any prototype properties */
if (jsObj->HasRealNamedProperty(jsKey) || jsObj->HasRealNamedCallbackProperty(jsKey)) {
jsVal = jsObj->Get(jsKey);
if (v8js_to_zval(jsVal, retval, obj->flags, isolate TSRMLS_CC) == SUCCESS) {
return retval;
}
}
}
return retval;
}
/* }}} */
static void v8js_v8object_write_property(zval *object, zval *member, zval *value, void **cache_slot TSRMLS_DC) /* {{{ */
{
v8js_v8object *obj = Z_V8JS_V8OBJECT_OBJ_P(object);
if (!obj->ctx) {
zend_throw_exception(php_ce_v8js_exception,
"Can't access V8Object after V8Js instance is destroyed!", 0 TSRMLS_CC);
return;
}
V8JS_CTX_PROLOGUE(obj->ctx);
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
if (v8obj->IsObject()) {
v8obj->ToObject()->ForceSet(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member)), zval_to_v8js(value, isolate TSRMLS_CC));
}
}
/* }}} */
static void v8js_v8object_unset_property(zval *object, zval *member, void **cache_slot TSRMLS_DC) /* {{{ */
{
v8js_v8object *obj = Z_V8JS_V8OBJECT_OBJ_P(object);
if (!obj->ctx) {
zend_throw_exception(php_ce_v8js_exception,
"Can't access V8Object after V8Js instance is destroyed!", 0 TSRMLS_CC);
return;
}
V8JS_CTX_PROLOGUE(obj->ctx);
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
if (v8obj->IsObject()) {
v8obj->ToObject()->Delete(V8JS_SYML(Z_STRVAL_P(member), Z_STRLEN_P(member)));
}
}
/* }}} */
static HashTable *v8js_v8object_get_properties(zval *object TSRMLS_DC) /* {{{ */
{
v8js_v8object *obj = Z_V8JS_V8OBJECT_OBJ_P(object);
if (obj->properties == NULL) {
if (GC_G(gc_active)) {
/* the garbage collector is running, don't create more zvals */
return NULL;
}
ALLOC_HASHTABLE(obj->properties);
zend_hash_init(obj->properties, 0, NULL, ZVAL_PTR_DTOR, 0);
if (!obj->ctx) {
/* Half-constructed object, probably due to unserialize call.
* Just pass back properties hash so unserialize can write to
* it (instead of crashing the engine). */
return obj->properties;
}
} else {
zend_hash_clean(obj->properties);
}
if (!obj->ctx) {
zend_throw_exception(php_ce_v8js_exception,
"Can't access V8Object after V8Js instance is destroyed!", 0 TSRMLS_CC);
return NULL;
}
V8JS_CTX_PROLOGUE_EX(obj->ctx, NULL);
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
if (v8js_get_properties_hash(v8obj, obj->properties, obj->flags, isolate TSRMLS_CC) == SUCCESS) {
return obj->properties;
}
return NULL;
}
/* }}} */
static HashTable *v8js_v8object_get_debug_info(zval *object, int *is_temp TSRMLS_DC) /* {{{ */
{
*is_temp = 0;
return v8js_v8object_get_properties(object TSRMLS_CC);
}
/* }}} */
static zend_function *v8js_v8object_get_method(zend_object **object_ptr, zend_string *method, const zval *key TSRMLS_DC) /* {{{ */
{
v8js_v8object *obj = v8js_v8object_fetch_object(*object_ptr);
zend_function *f;
if (!obj->ctx) {
zend_throw_exception(php_ce_v8js_exception,
"Can't access V8Object after V8Js instance is destroyed!", 0 TSRMLS_CC);
return NULL;
}
V8JS_CTX_PROLOGUE_EX(obj->ctx, NULL);
v8::Local<v8::String> jsKey = V8JS_ZSTR(method);
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
if (!obj->v8obj.IsEmpty() && v8obj->IsObject() && !v8obj->IsFunction()) {
v8::Local<v8::Object> jsObj = v8obj->ToObject();
if (jsObj->Has(jsKey) && jsObj->Get(jsKey)->IsFunction()) {
f = (zend_function *) ecalloc(1, sizeof(*f));
f->type = ZEND_OVERLOADED_FUNCTION_TEMPORARY;
f->common.function_name = zend_string_copy(method);
return f;
}
}
return NULL;
}
/* }}} */
static int v8js_v8object_call_method(zend_string *method, zend_object *object, INTERNAL_FUNCTION_PARAMETERS) /* {{{ */
{
zval *argv = NULL;
int argc = ZEND_NUM_ARGS();
v8js_v8object *obj = v8js_v8object_fetch_object(object);
if (!obj->ctx) {
zend_throw_exception(php_ce_v8js_exception,
"Can't access V8Object after V8Js instance is destroyed!", 0 TSRMLS_CC);
return FAILURE;
}
if (obj->v8obj.IsEmpty()) {
return FAILURE;
}
if (argc > 0) {
argv = (zval*)safe_emalloc(sizeof(zval), argc, 0);
zend_get_parameters_array_ex(argc, argv);
}
/* std::function relies on its dtor to be executed, otherwise it leaks
* some memory on bailout. */
{
std::function< v8::Local<v8::Value>(v8::Isolate *) > v8_call = [obj, method, argc, argv, object, &return_value TSRMLS_CC](v8::Isolate *isolate) {
int i = 0;
v8::Local<v8::String> method_name = V8JS_ZSYM(method);
v8::Local<v8::Object> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj)->ToObject();
v8::Local<v8::Object> thisObj;
v8::Local<v8::Function> cb;
if (method_name->Equals(V8JS_SYM(V8JS_V8_INVOKE_FUNC_NAME))) {
cb = v8::Local<v8::Function>::Cast(v8obj);
} else {
cb = v8::Local<v8::Function>::Cast(v8obj->Get(method_name));
}
// If a method is invoked on V8Object, then set the object itself as
// "this" on JS side. Otherwise fall back to global object.
if (obj->std.ce == php_ce_v8object) {
thisObj = v8obj;
}
else {
thisObj = V8JS_GLOBAL(isolate);
}
v8::Local<v8::Value> *jsArgv = static_cast<v8::Local<v8::Value> *>(alloca(sizeof(v8::Local<v8::Value>) * argc));
for (i = 0; i < argc; i++) {
new(&jsArgv[i]) v8::Local<v8::Value>;
jsArgv[i] = v8::Local<v8::Value>::New(isolate, zval_to_v8js(&argv[i], isolate TSRMLS_CC));
}
v8::Local<v8::Value> result = cb->Call(thisObj, argc, jsArgv);
if (obj->std.ce == php_ce_v8object && result->StrictEquals(thisObj)) {
/* JS code did "return this", retain object identity */
ZVAL_OBJ(return_value, object);
zval_copy_ctor(return_value);
result.Clear();
}
return result;
};
v8js_v8_call(obj->ctx, &return_value, obj->flags, obj->ctx->time_limit, obj->ctx->memory_limit, v8_call TSRMLS_CC);
}
if (argc > 0) {
efree(argv);
}
if(V8JSG(fatal_error_abort)) {
/* Check for fatal error marker possibly set by v8js_error_handler; just
* rethrow the error since we're now out of V8. */
zend_bailout();
}
return SUCCESS;
}
/* }}} */
static int v8js_v8object_get_closure(zval *object, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zend_object **zobj_ptr TSRMLS_DC) /* {{{ */
{
zend_function *invoke;
v8js_v8object *obj = Z_V8JS_V8OBJECT_OBJ_P(object);
if (!obj->ctx) {
zend_throw_exception(php_ce_v8js_exception,
"Can't access V8Object after V8Js instance is destroyed!", 0 TSRMLS_CC);
return FAILURE;
}
V8JS_CTX_PROLOGUE_EX(obj->ctx, FAILURE);
v8::Local<v8::Value> v8obj = v8::Local<v8::Value>::New(isolate, obj->v8obj);
if (!v8obj->IsFunction()) {
return FAILURE;
}
invoke = (zend_function *) ecalloc(1, sizeof(*invoke));
invoke->type = ZEND_OVERLOADED_FUNCTION_TEMPORARY;
invoke->common.function_name = zend_string_init(V8JS_V8_INVOKE_FUNC_NAME, sizeof(V8JS_V8_INVOKE_FUNC_NAME) - 1, 0);
*fptr_ptr = invoke;
if (zobj_ptr) {
*zobj_ptr = Z_OBJ_P(object);
}
*ce_ptr = NULL;
return SUCCESS;
}
/* }}} */
static void v8js_v8object_free_storage(zend_object *object TSRMLS_DC) /* {{{ */
{
v8js_v8object *c = v8js_v8object_fetch_object(object);
if (c->properties) {
zend_hash_destroy(c->properties);
FREE_HASHTABLE(c->properties);
c->properties = NULL;
}
zend_object_std_dtor(&c->std TSRMLS_CC);
if(c->ctx) {
c->v8obj.Reset();
c->ctx->v8js_v8objects.remove(c);
}
}
/* }}} */
static zend_object *v8js_v8object_new(zend_class_entry *ce TSRMLS_DC) /* {{{ */
{
v8js_v8object *c;
c = (v8js_v8object *) ecalloc(1, sizeof(v8js_v8object) + zend_object_properties_size(ce));
zend_object_std_init(&c->std, ce TSRMLS_CC);
c->std.handlers = &v8js_v8object_handlers;
new(&c->v8obj) v8::Persistent<v8::Value>();
return &c->std;
}
/* }}} */
/* NOTE: We could also override v8js_v8object_handlers.get_constructor to throw
* an exception when invoked, but doing so causes the half-constructed object
* to leak -- this seems to be a PHP bug. So we'll define magic __construct
* methods instead. */
/* {{{ proto V8Object::__construct()
*/
PHP_METHOD(V8Object,__construct)
{
zend_throw_exception(php_ce_v8js_exception,
"Can't directly construct V8 objects!", 0 TSRMLS_CC);
RETURN_FALSE;
}
/* }}} */
/* {{{ proto V8Object::__sleep()
*/
PHP_METHOD(V8Object, __sleep)
{
zend_throw_exception(php_ce_v8js_exception,
"You cannot serialize or unserialize V8Object instances", 0 TSRMLS_CC);
RETURN_FALSE;
}
/* }}} */
/* {{{ proto V8Object::__wakeup()
*/
PHP_METHOD(V8Object, __wakeup)
{
zend_throw_exception(php_ce_v8js_exception,
"You cannot serialize or unserialize V8Object instances", 0 TSRMLS_CC);
RETURN_FALSE;
}
/* }}} */
/* {{{ proto V8Function::__construct()
*/
PHP_METHOD(V8Function,__construct)
{
zend_throw_exception(php_ce_v8js_exception,
"Can't directly construct V8 objects!", 0 TSRMLS_CC);
RETURN_FALSE;
}
/* }}} */
/* {{{ proto V8Function::__sleep()
*/
PHP_METHOD(V8Function, __sleep)
{
zend_throw_exception(php_ce_v8js_exception,
"You cannot serialize or unserialize V8Function instances", 0 TSRMLS_CC);
RETURN_FALSE;
}
/* }}} */
/* {{{ proto V8Function::__wakeup()
*/
PHP_METHOD(V8Function, __wakeup)
{
zend_throw_exception(php_ce_v8js_exception,
"You cannot serialize or unserialize V8Function instances", 0 TSRMLS_CC);
RETURN_FALSE;
}
/* }}} */
static void v8js_v8generator_free_storage(zend_object *object) /* {{{ */
{
v8js_v8generator *c = v8js_v8generator_fetch_object(object);
zval_dtor(&c->value);
v8js_v8object_free_storage(object);
}
/* }}} */
static zend_object *v8js_v8generator_new(zend_class_entry *ce) /* {{{ */
{
v8js_v8generator *c;
c = (v8js_v8generator *) ecalloc(1, sizeof(v8js_v8generator) + zend_object_properties_size(ce));
zend_object_std_init(&c->v8obj.std, ce);
c->v8obj.std.handlers = &v8js_v8generator_handlers;
new(&c->v8obj.v8obj) v8::Persistent<v8::Value>();
return &c->v8obj.std;
}
/* }}} */
static void v8js_v8generator_next(v8js_v8generator *g) /* {{{ */
{
if (!g->v8obj.ctx) {
zend_throw_exception(php_ce_v8js_exception,
"Can't access V8Generator after V8Js instance is destroyed!", 0);
return;
}
/* std::function relies on its dtor to be executed, otherwise it leaks
* some memory on bailout. */
{
std::function< v8::Local<v8::Value>(v8::Isolate *) > v8_call = [g](v8::Isolate *isolate) {
int i = 0;
v8::Local<v8::String> method_name = V8JS_STR("next");
v8::Local<v8::Object> v8obj = v8::Local<v8::Value>::New(isolate, g->v8obj.v8obj)->ToObject();
v8::Local<v8::Function> cb = v8::Local<v8::Function>::Cast(v8obj->Get(method_name));;
v8::Local<v8::Value> result = cb->Call(v8obj, 0, NULL);
if(result.IsEmpty()) {
/* cb->Call probably threw (and already threw a zend exception), just return */
return V8JS_NULL;
}
if(!result->IsObject()) {
zend_throw_exception(php_ce_v8js_exception,
"V8Generator returned non-object on next()", 0);
return V8JS_NULL;
}
v8::Local<v8::Object> resultObj = result->ToObject();
v8::Local<v8::Value> val = resultObj->Get(V8JS_STR("value"));
v8::Local<v8::Value> done = resultObj->Get(V8JS_STR("done"));
zval_dtor(&g->value);
v8js_to_zval(val, &g->value, 0, isolate);
g->done = done->IsTrue();
g->primed = true;
return V8JS_NULL;
};
v8js_v8_call(g->v8obj.ctx, NULL, g->v8obj.flags, g->v8obj.ctx->time_limit, g->v8obj.ctx->memory_limit, v8_call);
}
if(V8JSG(fatal_error_abort)) {
/* Check for fatal error marker possibly set by v8js_error_handler; just
* rethrow the error since we're now out of V8. */
zend_bailout();
}
}
/* }}} */
static zend_function *v8js_v8generator_get_method(zend_object **object_ptr, zend_string *method, const zval *key) /* {{{ */
{
zend_function *result = std_object_handlers.get_method(object_ptr, method, key);
if(!result) {
result = v8js_v8object_get_method(object_ptr, method, key);
}
return result;
}
/* }}} */
/* {{{ proto V8Generator::__construct()
*/
PHP_METHOD(V8Generator,__construct)
{
zend_throw_exception(php_ce_v8js_exception,
"Can't directly construct V8 objects!", 0 TSRMLS_CC);
RETURN_FALSE;
}
/* }}} */
/* {{{ proto V8Generator::__sleep()
*/
PHP_METHOD(V8Generator, __sleep)
{
zend_throw_exception(php_ce_v8js_exception,
"You cannot serialize or unserialize V8Generator instances", 0 TSRMLS_CC);
RETURN_FALSE;
}
/* }}} */
/* {{{ proto V8Generator::__wakeup()
*/
PHP_METHOD(V8Generator, __wakeup)
{
zend_throw_exception(php_ce_v8js_exception,
"You cannot serialize or unserialize V8Generator instances", 0 TSRMLS_CC);
RETURN_FALSE;
}
/* }}} */
/* {{{ mixed V8Generator::current()
*/
PHP_METHOD(V8Generator, current)
{
v8js_v8generator *g = Z_V8JS_V8GENERATOR_OBJ_P(getThis());
if(!g->primed) {
v8js_v8generator_next(g);
}
RETVAL_ZVAL(&g->value, 1, 0);
}
/* }}} */
/* {{{ scalar V8Generator::key()
*/
PHP_METHOD(V8Generator, key)
{
RETURN_FALSE;
}
/* }}} */
/* {{{ void V8Generator::next()
*/
PHP_METHOD(V8Generator, next)
{
v8js_v8generator *g = Z_V8JS_V8GENERATOR_OBJ_P(getThis());
v8js_v8generator_next(g);
}
/* }}} */
/* {{{ void V8Generator::rewind()
*/
PHP_METHOD(V8Generator, rewind)
{
v8js_v8generator *g = Z_V8JS_V8GENERATOR_OBJ_P(getThis());
if(g->primed) {
zend_throw_exception(php_ce_v8js_exception,
"V8Generator::rewind not supported by ES6", 0 TSRMLS_CC);
}
RETURN_FALSE;
}
/* }}} */
/* {{{ boolean V8Generator::valid()
*/
PHP_METHOD(V8Generator, valid)
{
v8js_v8generator *g = Z_V8JS_V8GENERATOR_OBJ_P(getThis());
if(!g->primed) {
v8js_v8generator_next(g);
}
RETVAL_BOOL(!g->done);
}
/* }}} */
void v8js_v8object_create(zval *res, v8::Handle<v8::Value> value, int flags, v8::Isolate *isolate TSRMLS_DC) /* {{{ */
{
v8js_ctx *ctx = (v8js_ctx *) isolate->GetData(0);
if(value->IsGeneratorObject()) {
object_init_ex(res, php_ce_v8generator);
}
else if(value->IsFunction()) {
object_init_ex(res, php_ce_v8function);
}
else {
object_init_ex(res, php_ce_v8object);
}
v8js_v8object *c = Z_V8JS_V8OBJECT_OBJ_P(res);
c->v8obj.Reset(isolate, value);
c->flags = flags;
c->ctx = ctx;
ctx->v8js_v8objects.push_front(c);
}
/* }}} */
static const zend_function_entry v8js_v8object_methods[] = { /* {{{ */
PHP_ME(V8Object, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(V8Object, __sleep, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(V8Object, __wakeup, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
{NULL, NULL, NULL}
};
/* }}} */
static const zend_function_entry v8js_v8function_methods[] = { /* {{{ */
PHP_ME(V8Function, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(V8Function, __sleep, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(V8Function, __wakeup, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
{NULL, NULL, NULL}
};
/* }}} */
ZEND_BEGIN_ARG_INFO(arginfo_v8generator_current, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_v8generator_key, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_v8generator_next, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_v8generator_rewind, 0)
ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_INFO(arginfo_v8generator_valid, 0)
ZEND_END_ARG_INFO()
static const zend_function_entry v8js_v8generator_methods[] = { /* {{{ */
PHP_ME(V8Generator, __construct, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
PHP_ME(V8Generator, __sleep, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(V8Generator, __wakeup, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL)
PHP_ME(V8Generator, current, arginfo_v8generator_current, ZEND_ACC_PUBLIC)
PHP_ME(V8Generator, key, arginfo_v8generator_key, ZEND_ACC_PUBLIC)
PHP_ME(V8Generator, next, arginfo_v8generator_next, ZEND_ACC_PUBLIC)
PHP_ME(V8Generator, rewind, arginfo_v8generator_rewind, ZEND_ACC_PUBLIC)
PHP_ME(V8Generator, valid, arginfo_v8generator_valid, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
/* }}} */
PHP_MINIT_FUNCTION(v8js_v8object_class) /* {{{ */
{
zend_class_entry ce;
/* V8Object Class */
INIT_CLASS_ENTRY(ce, "V8Object", v8js_v8object_methods);
php_ce_v8object = zend_register_internal_class(&ce TSRMLS_CC);
php_ce_v8object->ce_flags |= ZEND_ACC_FINAL;
php_ce_v8object->create_object = v8js_v8object_new;
/* V8Function Class */
INIT_CLASS_ENTRY(ce, "V8Function", v8js_v8function_methods);
php_ce_v8function = zend_register_internal_class(&ce TSRMLS_CC);
php_ce_v8function->ce_flags |= ZEND_ACC_FINAL;
php_ce_v8function->create_object = v8js_v8object_new;
/* V8Generator Class */
INIT_CLASS_ENTRY(ce, "V8Generator", v8js_v8generator_methods);
php_ce_v8generator = zend_register_internal_class(&ce TSRMLS_CC);
php_ce_v8generator->ce_flags |= ZEND_ACC_FINAL;
php_ce_v8generator->create_object = v8js_v8generator_new;
zend_class_implements(php_ce_v8generator, 1, zend_ce_iterator);
/* V8<Object|Function> handlers */
memcpy(&v8js_v8object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
v8js_v8object_handlers.clone_obj = NULL;
v8js_v8object_handlers.cast_object = NULL;
v8js_v8object_handlers.get_property_ptr_ptr = NULL;
v8js_v8object_handlers.has_property = v8js_v8object_has_property;
v8js_v8object_handlers.read_property = v8js_v8object_read_property;
v8js_v8object_handlers.write_property = v8js_v8object_write_property;
v8js_v8object_handlers.unset_property = v8js_v8object_unset_property;
v8js_v8object_handlers.get_properties = v8js_v8object_get_properties;
v8js_v8object_handlers.get_method = v8js_v8object_get_method;
v8js_v8object_handlers.call_method = v8js_v8object_call_method;
v8js_v8object_handlers.get_debug_info = v8js_v8object_get_debug_info;
v8js_v8object_handlers.get_closure = v8js_v8object_get_closure;
v8js_v8object_handlers.offset = XtOffsetOf(struct v8js_v8object, std);
v8js_v8object_handlers.free_obj = v8js_v8object_free_storage;
/* V8Generator handlers */
memcpy(&v8js_v8generator_handlers, &v8js_v8object_handlers, sizeof(zend_object_handlers));
v8js_v8generator_handlers.get_method = v8js_v8generator_get_method;
v8js_v8generator_handlers.offset = XtOffsetOf(struct v8js_v8generator, v8obj.std);
v8js_v8generator_handlers.free_obj = v8js_v8generator_free_storage;
return SUCCESS;
} /* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode: t
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/