Skip to content

Commit

Permalink
Proper support for arrays done and working
Browse files Browse the repository at this point in the history
  • Loading branch information
christopherobin committed May 10, 2014
1 parent bb4a861 commit f5906db
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 46 deletions.
77 changes: 62 additions & 15 deletions spidermonkey.cc
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ void _jsval_to_zval(zval *return_value, JSContext *ctx, JS::MutableHandle<JS::Va
php_jscontext_object *intern;
php_jsobject_ref *jsref;
zval *zobj;
php_jsparent jsthis;
php_jsparent jsthis;

obj = rval.toObjectOrNull();

Expand All @@ -359,12 +359,12 @@ void _jsval_to_zval(zval *return_value, JSContext *ctx, JS::MutableHandle<JS::Va
RETURN_NULL();
}

/* your shouldn't be able to reference the global object */
if (obj == JS_GetGlobalForScopeChain(ctx)) {
PHPJS_END(ctx);
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Trying to reference global object", 0 TSRMLS_CC);
return;
}
/* your shouldn't be able to reference the global object */
if (obj == JS_GetGlobalForScopeChain(ctx)) {
PHPJS_END(ctx);
zend_throw_exception(zend_exception_get_default(TSRMLS_C), "Trying to reference global object", 0 TSRMLS_CC);
return;
}

intern = (php_jscontext_object*)JS_GetContextPrivate(ctx);

Expand All @@ -386,13 +386,49 @@ void _jsval_to_zval(zval *return_value, JSContext *ctx, JS::MutableHandle<JS::Va
jsval_to_zval(fval, ctx, JS::MutableHandleValue::fromMarkedLocation(&value));
/* Add property to our array */
add_index_zval(return_value, i, fval);
/* Destroy pointer to zval */
zval_ptr_dtor(&fval);

}
}
}
}

/* then iterate on each property */
it = JS_NewPropertyIterator(ctx, obj);

jsid id;
while (JS_NextProperty(ctx, it, &id) == JS_TRUE)
{
jsval val;

if (JSID_IS_VOID(id)) {
break;
}

if (JS_IdToValue(ctx, id, &val) == JS_TRUE)
{
JSString *str;
jsval item_val;
char *name;

str = JS_ValueToString(ctx, val);

/* Retrieve property name */
name = JS_EncodeString(ctx, str);

/* Try to read property */
if (JS_GetProperty(ctx, obj, name, &item_val) == JS_TRUE)
{
zval *fval;

/* alloc memory for this zval */
MAKE_STD_ZVAL(fval);
/* Call this function to convert a jsval to a zval */
jsval_to_zval(fval, ctx, JS::MutableHandleValue::fromMarkedLocation(&item_val));
/* Add property to our stdClass */
add_assoc_zval(return_value, name, fval);
}
JS_free(ctx, name);
}
}
} else {
if ((jsref = (php_jsobject_ref*)JS_GetPrivate(obj)) == NULL || jsref->obj == NULL)
{
Expand All @@ -416,15 +452,13 @@ void _jsval_to_zval(zval *return_value, JSContext *ctx, JS::MutableHandle<JS::Va

/* then iterate on each property */
it = JS_NewPropertyIterator(ctx, obj);
php_printf("iterating on %p\n", obj);

jsid id;
while (JS_NextProperty(ctx, it, &id) == JS_TRUE)
{
jsval val;

if (JSID_IS_VOID(id)) {
php_printf("id is void, breaking\n");
break;
}

Expand Down Expand Up @@ -614,7 +648,10 @@ void zval_to_jsval(zval *val, JSContext *ctx, jsval *jval TSRMLS_DC)
ht = HASH_OF(val);

/* create JSObject */
jobj = JS_NewObject(ctx, NULL, NULL, NULL);
jobj = JS_NewArrayObject(ctx, 0, nullptr);

// prevent GC
JS_AddObjectRoot(ctx, &jobj);

/* foreach item */
for(zend_hash_internal_pointer_reset(ht); zend_hash_has_more_elements(ht) == SUCCESS; zend_hash_move_forward(ht))
Expand All @@ -636,8 +673,18 @@ void zval_to_jsval(zval *val, JSContext *ctx, jsval *jval TSRMLS_DC)

if (type == HASH_KEY_IS_LONG)
{
sprintf(intIdx, "%ld", idx);
php_jsobject_set_property(ctx, jobj, intIdx, *ppzval TSRMLS_CC);
//sprintf(intIdx, "%ld", idx);
//php_jsobject_set_property(ctx, jobj, intIdx, *ppzval TSRMLS_CC);
jsval jarrval;

/* first convert zval to jsval */
zval_to_jsval(*ppzval, ctx, &jarrval TSRMLS_CC);

/* no ref behavior, just set a property */
//JSBool res = JS_SetProperty(ctx, obj, property_name, &jval);
//JSBool res = JS_SetElement(ctx, jobj, idx, &jarrval);
JSBool res = JS_DefineElement(ctx, jobj, idx, jarrval, nullptr, nullptr, 0);

}
else
{
Expand Down
2 changes: 1 addition & 1 deletion spidermonkey_external.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void php_jsobject_set_property(JSContext *ctx, JSObject *obj, char *property_nam

/* no ref behavior, just set a property */
JSBool res = JS_SetProperty(ctx, obj, property_name, &jval);
php_printf("set prop %s on obj %p: %d\n", property_name, obj, res);
//php_printf("set prop %s on obj %p: %d\n", property_name, obj, res);

PHPJS_END(ctx);
}
Expand Down
20 changes: 10 additions & 10 deletions tests/bug16890.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,24 @@ JS;
$ctx->evaluateScript($js);
?>
--EXPECTF--
object(stdClass)#2 (4) {
["0"]=>
array(4) {
[0]=>
int(1)
["1"]=>
[1]=>
NULL
["2"]=>
[2]=>
int(0)
["3"]=>
[3]=>
string(3) "str"
}
object(stdClass)#2 (4) {
["0"]=>
array(4) {
[0]=>
int(1)
["1"]=>
[1]=>
NULL
["2"]=>
[2]=>
int(0)
["3"]=>
[3]=>
string(3) "str"
}
object(stdClass)#2 (1) {
Expand Down
40 changes: 20 additions & 20 deletions tests/js_types.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,34 @@ $js->assign('test', $a);
var_dump($js->evaluateScript('test'));
?>
--EXPECTF--
object(stdClass)#2 (6) {
["str"]=>
string(8) "a string"
["long"]=>
int(1337)
["flt"]=>
float(13.37)
["bool"]=>
bool(false)
["null"]=>
NULL
array(6) {
["numarr"]=>
object(stdClass)#3 (8) {
["0"]=>
array(8) {
[0]=>
int(1)
["1"]=>
[1]=>
int(2)
["2"]=>
[2]=>
int(3)
["3"]=>
[3]=>
int(5)
["4"]=>
[4]=>
int(7)
["5"]=>
[5]=>
int(11)
["6"]=>
[6]=>
int(13)
["7"]=>
[7]=>
int(17)
}
["null"]=>
NULL
["bool"]=>
bool(false)
["flt"]=>
float(13.37)
["long"]=>
int(1337)
["str"]=>
string(8) "a string"
}

0 comments on commit f5906db

Please sign in to comment.