Skip to content

Commit 0c9666d

Browse files
committed
Implement "array access" getters, as well as the "property access" getters.
1 parent 30ca3f9 commit 0c9666d

File tree

3 files changed

+55
-14
lines changed

3 files changed

+55
-14
lines changed

src/node_php_jsobject_class.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,27 @@ PHP_METHOD(JsObject, __get) {
186186
msg.retval_.ToPhp(obj->channel, return_value, return_value_ptr TSRMLS_CC);
187187
}
188188

189+
/* Use (slightly thunked) versions of the has/read/write property handlers
190+
* for dimensions as well, so that $obj['foo'] acts like $obj->foo. */
191+
static int node_php_jsobject_has_dimension(zval *obj, zval *idx, int chk_type TSRMLS_DC) {
192+
// thunk!
193+
if (chk_type == 0) { chk_type = 2; }
194+
// use standard has_property method with new chk_type
195+
return node_php_jsobject_handlers.has_property(obj, idx, chk_type ZEND_HASH_KEY_NULL TSRMLS_CC);
196+
}
197+
static zval *node_php_jsobject_read_dimension(zval *obj, zval *off, int type TSRMLS_DC) {
198+
// use standard read_property method
199+
return node_php_jsobject_handlers.read_property(obj, off, type ZEND_HASH_KEY_NULL TSRMLS_CC);
200+
}
201+
static void node_php_jsobject_write_dimension(zval *obj, zval *off, zval *val TSRMLS_DC) {
202+
// use standard write_property method
203+
node_php_jsobject_handlers.write_property(obj, off, val ZEND_HASH_KEY_NULL TSRMLS_CC);
204+
}
205+
static void node_php_jsobject_unset_dimension(zval *obj, zval *off TSRMLS_DC) {
206+
// use standard unset_property method
207+
node_php_jsobject_handlers.unset_property(obj, off ZEND_HASH_KEY_NULL TSRMLS_CC);
208+
}
209+
189210
static void node_php_jsobject_free_storage(void *object, zend_object_handle handle TSRMLS_DC) {
190211
node_php_jsobject *c = (node_php_jsobject *) object;
191212

@@ -310,5 +331,11 @@ PHP_MINIT_FUNCTION(node_php_jsobject_class) {
310331
node_php_jsobject_handlers.get_closure = node_php_jsobject_get_closure;
311332
*/
312333

334+
/* Array access handlers: slightly thunked versions of property handlers. */
335+
node_php_jsobject_handlers.read_dimension = node_php_jsobject_read_dimension;
336+
node_php_jsobject_handlers.write_dimension = node_php_jsobject_write_dimension;
337+
node_php_jsobject_handlers.has_dimension = node_php_jsobject_has_dimension;
338+
node_php_jsobject_handlers.unset_dimension = node_php_jsobject_unset_dimension;
339+
313340
return SUCCESS;
314341
}

test/context.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,37 +67,44 @@ describe('Passing context object from JS to PHP', function() {
6767
}
6868
}).then(function(v) {
6969
out.toString().should.equal(
70-
'a: int(0)\n' +
70+
'->a: int(0)\n' +
71+
'[\'a\']: int(0)\n' +
7172
'isset: bool(true)\n' +
7273
'empty: bool(true)\n' +
7374
'exists: bool(true)\n' +
7475
'\n' +
75-
'b: int(42)\n' +
76+
'->b: int(42)\n' +
77+
'[\'b\']: int(42)\n' +
7678
'isset: bool(true)\n' +
7779
'empty: bool(false)\n' +
7880
'exists: bool(true)\n' +
7981
'\n' +
80-
'c: NULL\n' +
82+
'->c: NULL\n' +
83+
'[\'c\']: NULL\n' +
8184
'isset: bool(false)\n' +
8285
'empty: bool(true)\n' +
8386
'exists: bool(true)\n' +
8487
'\n' +
85-
'd: NULL\n' +
88+
'->d: NULL\n' +
89+
'[\'d\']: NULL\n' +
8690
'isset: bool(false)\n' +
8791
'empty: bool(true)\n' +
8892
'exists: bool(true)\n' +
8993
'\n' +
90-
'e: string(1) "0"\n' +
94+
'->e: string(1) "0"\n' +
95+
'[\'e\']: string(1) "0"\n' +
9196
'isset: bool(true)\n' +
9297
'empty: bool(true)\n' +
9398
'exists: bool(true)\n' +
9499
'\n' +
95-
'f: string(1) "1"\n' +
100+
'->f: string(1) "1"\n' +
101+
'[\'f\']: string(1) "1"\n' +
96102
'isset: bool(true)\n' +
97103
'empty: bool(false)\n' +
98104
'exists: bool(true)\n' +
99105
'\n' +
100-
'g: NULL\n' +
106+
'->g: NULL\n' +
107+
'[\'g\']: NULL\n' +
101108
'isset: bool(false)\n' +
102109
'empty: bool(true)\n' +
103110
'exists: bool(false)\n' +

test/context2.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,50 @@
11
<?php
22
$c = $_SERVER['CONTEXT'];
33

4-
echo "a: "; var_dump($c->a);
4+
echo "->a: "; var_dump($c->a);
5+
echo "['a']: "; var_dump($c['a']);
56
echo "isset: "; var_dump(isset($c->a));
67
echo "empty: "; var_dump(empty($c->a));
78
echo "exists: "; var_dump(property_exists($c, "a"));
89
echo "\n";
910

10-
echo "b: "; var_dump($c->b);
11+
echo "->b: "; var_dump($c->b);
12+
echo "['b']: "; var_dump($c['b']);
1113
echo "isset: "; var_dump(isset($c->b));
1214
echo "empty: "; var_dump(empty($c->b));
1315
echo "exists: "; var_dump(property_exists($c, "b"));
1416
echo "\n";
1517

16-
echo "c: "; var_dump($c->c);
18+
echo "->c: "; var_dump($c->c);
19+
echo "['c']: "; var_dump($c['c']);
1720
echo "isset: "; var_dump(isset($c->c));
1821
echo "empty: "; var_dump(empty($c->c));
1922
echo "exists: "; var_dump(property_exists($c, "c"));
2023
echo "\n";
2124

22-
echo "d: "; var_dump($c->d);
25+
echo "->d: "; var_dump($c->d);
26+
echo "['d']: "; var_dump($c['d']);
2327
echo "isset: "; var_dump(isset($c->d));
2428
echo "empty: "; var_dump(empty($c->d));
2529
echo "exists: "; var_dump(property_exists($c, "d"));
2630
echo "\n";
2731

28-
echo "e: "; var_dump($c->e);
32+
echo "->e: "; var_dump($c->e);
33+
echo "['e']: "; var_dump($c['e']);
2934
echo "isset: "; var_dump(isset($c->e));
3035
echo "empty: "; var_dump(empty($c->e));
3136
echo "exists: "; var_dump(property_exists($c, "e"));
3237
echo "\n";
3338

34-
echo "f: "; var_dump($c->f);
39+
echo "->f: "; var_dump($c->f);
40+
echo "['f']: "; var_dump($c['f']);
3541
echo "isset: "; var_dump(isset($c->f));
3642
echo "empty: "; var_dump(empty($c->f));
3743
echo "exists: "; var_dump(property_exists($c, "f"));
3844
echo "\n";
3945

40-
echo "g: "; var_dump($c->g);
46+
echo "->g: "; var_dump($c->g);
47+
echo "['g']: "; var_dump($c['g']);
4148
echo "isset: "; var_dump(isset($c->g));
4249
echo "empty: "; var_dump(empty($c->g));
4350
echo "exists: "; var_dump(property_exists($c, "g"));

0 commit comments

Comments
 (0)