From 6788cd6f3da47f4fa05ea80b26ecb2bf3ac20f67 Mon Sep 17 00:00:00 2001 From: Scott Kyle Date: Mon, 12 Oct 2015 15:25:23 -0700 Subject: [PATCH] Out of bounds list item getters return undefined Resolves #50 --- src/RJSArray.cpp | 4 ++++ tests/ArrayTests.js | 18 +++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/RJSArray.cpp b/src/RJSArray.cpp index 3196c26e..ca24a36a 100644 --- a/src/RJSArray.cpp +++ b/src/RJSArray.cpp @@ -87,6 +87,10 @@ JSValueRef ArrayGetProperty(JSContextRef ctx, JSObjectRef object, JSStringRef pr return RJSObjectCreate(ctx, Object(array->realm, array->object_schema, array->get(RJSVerifiedPositiveIndex(indexStr)))); } + catch (std::out_of_range &exp) { + // getters for nonexistent properties in JS should always return undefined + return JSValueMakeUndefined(ctx); + } catch (std::invalid_argument &exp) { // for stol failure this could be another property that is handled externally, so ignore return NULL; diff --git a/tests/ArrayTests.js b/tests/ArrayTests.js index 7a4c4f7f..85a93df5 100644 --- a/tests/ArrayTests.js +++ b/tests/ArrayTests.js @@ -30,6 +30,10 @@ var ArrayTests = { obj.arrayCol = [[1], [2]]; TestCase.assertEqual(obj.arrayCol.length, 2); + + TestCase.assertThrows(function() { + obj.arrayCol.length = 0; + }, 'cannot set length property on lists'); }); }, @@ -44,8 +48,8 @@ var ArrayTests = { TestCase.assertEqual(array[0].doubleCol, 3); TestCase.assertEqual(array[1].doubleCol, 4); - TestCase.assertThrows(function() { array[2]; }, 'Invalid index'); - TestCase.assertThrows(function() { array[-1]; }, 'Invalid index'); + TestCase.assertEqual(array[2], undefined); + TestCase.assertEqual(array[-1], undefined); }, testArraySubscriptSetters: function() { @@ -62,13 +66,13 @@ var ArrayTests = { TestCase.assertEqual(array[0].doubleCol, 5); TestCase.assertEqual(array[1].doubleCol, 6); - TestCase.assertThrows(function() { - array.length = 0; - }, 'cannot set length property on lists'); - TestCase.assertThrows(function() { array[2] = [1]; }, 'cannot set list item beyond its bounds'); + + TestCase.assertThrows(function() { + array[-1] = [1]; + }, 'cannot set list item with negative index'); }); TestCase.assertThrows(function() { @@ -133,7 +137,7 @@ var ArrayTests = { TestCase.assertThrows(function() { array.push(); }); - }); + }); TestCase.assertEqual(array.length, 4); TestCase.assertThrows(function() {