Skip to content

Commit b5a7ee7

Browse files
author
Bela Toth
committed
Implement toString and join for TypedArrays.
JerryScript-DCO-1.0-Signed-off-by: Bela Toth [email protected]
1 parent a0e3157 commit b5a7ee7

File tree

6 files changed

+280
-1
lines changed

6 files changed

+280
-1
lines changed

jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-float64array-prototype.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,6 @@
4040
* @}
4141
* @}
4242
*/
43+
4344
#endif /* CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT64 */
4445
#endif /* !CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN */

jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray-prototype.c

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,231 @@ ecma_builtin_typedarray_prototype_set (ecma_value_t this_arg, /**< this argument
767767
return ret_val;
768768
} /* ecma_builtin_typedarray_prototype_set */
769769

770+
/**
771+
* TypedArray.prototype's 'toString' single element operation routine based
772+
* on the Array.prototype's 'toString' single element operation routine
773+
*
774+
* See also:
775+
* ECMA-262 v5.1, 15.4.4.2
776+
*
777+
* @return ecma_value_t value
778+
* Returned value must be freed with ecma_free_value.
779+
*/
780+
static ecma_value_t
781+
ecma_op_typedarray_get_to_string_at_index (ecma_object_t *obj_p, /**< this object */
782+
uint32_t index) /**< array index */
783+
{
784+
ecma_value_t ret_value = ECMA_VALUE_EMPTY;
785+
ecma_string_t *index_string_p = ecma_new_ecma_string_from_uint32 (index);
786+
ecma_value_t index_value = ecma_op_object_get (obj_p,index_string_p);
787+
788+
if (ECMA_IS_VALUE_ERROR (index_value))
789+
{
790+
return index_value;
791+
}
792+
793+
if (ecma_is_value_undefined (index_value)
794+
|| ecma_is_value_null (index_value))
795+
{
796+
ret_value = ecma_make_magic_string_value (LIT_MAGIC_STRING__EMPTY);
797+
}
798+
else
799+
{
800+
ret_value = ecma_op_to_string (index_value);
801+
}
802+
803+
ecma_free_value (index_value);
804+
ecma_deref_ecma_string (index_string_p);
805+
return ret_value;
806+
} /* ecma_op_typedarray_get_to_string_at_index */
807+
808+
/**
809+
* The TypedArray.prototype.toString's separator creation routine based on
810+
* the Array.prototype.toString's separator routine
811+
*
812+
* See also:
813+
* ECMA-262 v5.1, 15.4.4.2 4th step
814+
*
815+
* @return ecma value
816+
* Returned value must be freed with ecma_free_value.
817+
*/
818+
static ecma_value_t
819+
ecma_op_typedarray_get_separator_string (ecma_value_t separator) /**< possible separator */
820+
{
821+
if (ecma_is_value_undefined (separator))
822+
{
823+
return ecma_make_magic_string_value (LIT_MAGIC_STRING_COMMA_CHAR);
824+
}
825+
826+
return ecma_op_to_string (separator);
827+
} /* ecma_op_typedarray_get_separator_string */
828+
829+
/**
830+
* The TypedArray.prototype object's 'join' routine basen on
831+
* the Array.porottype object's 'join'
832+
*
833+
* See also:
834+
* ECMA-262 v5, 15.4.4.5
835+
*
836+
* @return ecma value
837+
* Returned value must be freed with ecma_free_value.
838+
*/
839+
static ecma_value_t
840+
ecma_builtin_typedarray_prototype_join (const ecma_value_t this_arg, /**< this argument */
841+
const ecma_value_t separator_arg) /**< separator argument */
842+
{
843+
/* 1. */
844+
ecma_value_t obj_value = ecma_op_to_object (this_arg);
845+
846+
if (ECMA_IS_VALUE_ERROR (obj_value))
847+
{
848+
return obj_value;
849+
}
850+
ecma_object_t *obj_p = ecma_get_object_from_value (obj_value);
851+
852+
/* 2. */
853+
ecma_value_t length_value = ecma_op_object_get_by_magic_id (obj_p, LIT_MAGIC_STRING_LENGTH);
854+
855+
if (ECMA_IS_VALUE_ERROR (length_value))
856+
{
857+
ecma_free_value (obj_value);
858+
return length_value;
859+
}
860+
861+
ecma_value_t ret_value = ECMA_VALUE_EMPTY;
862+
863+
ECMA_OP_TO_NUMBER_TRY_CATCH (length_number,
864+
length_value,
865+
ret_value);
866+
867+
/* 3. */
868+
uint32_t length = ecma_number_to_uint32 (length_number);
869+
870+
/* 4-5. */
871+
ecma_value_t separator_value = ecma_op_typedarray_get_separator_string (separator_arg);
872+
if (ECMA_IS_VALUE_ERROR (separator_value))
873+
{
874+
ecma_free_value (obj_value);
875+
ecma_free_value (length_value);
876+
return separator_value;
877+
}
878+
879+
if (length == 0)
880+
{
881+
/* 6. */
882+
ret_value = ecma_make_magic_string_value (LIT_MAGIC_STRING__EMPTY);
883+
}
884+
else
885+
{
886+
ecma_string_t *separator_string_p = ecma_get_string_from_value (separator_value);
887+
888+
/* 7-8. */
889+
ecma_value_t first_value = ecma_op_typedarray_get_to_string_at_index (obj_p, 0);
890+
if (ECMA_IS_VALUE_ERROR (first_value))
891+
{
892+
ecma_free_value (obj_value);
893+
ecma_free_value (length_value);
894+
ecma_free_value (separator_value);
895+
return first_value;
896+
}
897+
898+
ecma_string_t *return_string_p = ecma_get_string_from_value (first_value);
899+
ecma_ref_ecma_string (return_string_p);
900+
if (ecma_is_value_empty (ret_value))
901+
{
902+
/* 9-10. */
903+
for (uint32_t k = 1; k < length; k++)
904+
{
905+
/* 10.a */
906+
return_string_p = ecma_concat_ecma_strings (return_string_p, separator_string_p);
907+
908+
/* 10.b, 10.c */
909+
ecma_value_t next_string_value = ecma_op_typedarray_get_to_string_at_index (obj_p, k);
910+
if (ECMA_IS_VALUE_ERROR (next_string_value))
911+
{
912+
ecma_free_value (obj_value);
913+
ecma_free_value (length_value);
914+
ecma_free_value (separator_value);
915+
ecma_free_value (first_value);
916+
return next_string_value;
917+
}
918+
919+
/* 10.d */
920+
ecma_string_t *next_string_p = ecma_get_string_from_value (next_string_value);
921+
return_string_p = ecma_concat_ecma_strings (return_string_p, next_string_p);
922+
923+
ecma_free_value (next_string_value);
924+
}
925+
ret_value = ecma_make_string_value (return_string_p);
926+
}
927+
else
928+
{
929+
ecma_deref_ecma_string (return_string_p);
930+
}
931+
932+
ecma_free_value (first_value);
933+
}
934+
935+
ecma_free_value (separator_value);
936+
937+
ECMA_OP_TO_NUMBER_FINALIZE (length_number);
938+
939+
ecma_free_value (length_value);
940+
ecma_free_value (obj_value);
941+
942+
return ret_value;
943+
} /* ecma_builtin_typedarray_prototype_join */
944+
945+
946+
/**
947+
* The TypedArray.prototype object's 'toString' routine basen on
948+
* the Array.porottype object's 'toString'
949+
*
950+
* See also:
951+
* ECMA-262 v5, 15.4.4.2
952+
*
953+
* @return ecma value
954+
* Returned value must be freed with ecma_free_value.
955+
*/
956+
static ecma_value_t
957+
ecma_builtin_typedarray_prototype_object_to_string (ecma_value_t this_arg) /**< this argument */
958+
{
959+
ecma_value_t ret_value = ECMA_VALUE_EMPTY;
960+
961+
/* 1. */
962+
ecma_value_t obj_this_value = ecma_op_to_object (this_arg);
963+
if (ECMA_IS_VALUE_ERROR (obj_this_value))
964+
{
965+
return obj_this_value;
966+
}
967+
ecma_object_t *obj_p = ecma_get_object_from_value (obj_this_value);
968+
969+
/* 2. */
970+
ecma_value_t join_value = ecma_op_object_get_by_magic_id (obj_p, LIT_MAGIC_STRING_JOIN);
971+
if (ECMA_IS_VALUE_ERROR (join_value))
972+
{
973+
ecma_free_value (obj_this_value);
974+
return join_value;
975+
}
976+
if (!ecma_op_is_callable (join_value))
977+
{
978+
/* 3. */
979+
ret_value = ecma_builtin_helper_object_to_string (this_arg);
980+
}
981+
else
982+
{
983+
/* 4. */
984+
ecma_object_t *join_func_obj_p = ecma_get_object_from_value (join_value);
985+
986+
ret_value = ecma_op_function_call (join_func_obj_p, this_arg, NULL, 0);
987+
}
988+
989+
ecma_free_value (join_value);
990+
ecma_free_value (obj_this_value);
991+
992+
return ret_value;
993+
} /* ecma_builtin_typedarray_prototype_object_to_string */
994+
770995
/**
771996
* @}
772997
* @}

jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray-prototype.inc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ ACCESSOR_READ_ONLY (LIT_MAGIC_STRING_LENGTH,
4545
ecma_builtin_typedarray_prototype_length_getter,
4646
ECMA_PROPERTY_FIXED)
4747

48+
/* Routine properties:
49+
* (property name, C routine name, arguments number or NON_FIXED, value of the routine's length property) */
50+
ROUTINE (LIT_MAGIC_STRING_TO_STRING_UL, ecma_builtin_typedarray_prototype_object_to_string, 0, 0)
51+
ROUTINE (LIT_MAGIC_STRING_JOIN, ecma_builtin_typedarray_prototype_join, 1, 1)
4852
ROUTINE (LIT_MAGIC_STRING_EVERY, ecma_builtin_typedarray_prototype_every, 2, 1)
4953
ROUTINE (LIT_MAGIC_STRING_SOME, ecma_builtin_typedarray_prototype_some, 2, 1)
5054
ROUTINE (LIT_MAGIC_STRING_FOR_EACH_UL, ecma_builtin_typedarray_prototype_for_each, 2, 1)

jerry-core/lit/lit-magic-strings.inc.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_EXEC, "exec")
110110
#if !defined (CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN)
111111
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_FROM, "from")
112112
#endif
113-
#if !defined (CONFIG_DISABLE_ARRAY_BUILTIN)
113+
#if !defined (CONFIG_DISABLE_ARRAY_BUILTIN) \
114+
|| !defined (CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN)
114115
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_JOIN, "join")
115116
#endif
116117
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_KEYS, "keys")

tests/jerry/es2015/typedArray-join.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* Copyright JS Foundation and other contributors, http://js.foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
var float_array = new Float32Array([1.125, 5.5, -1.25, -0.0]);
17+
var int_array = new Int8Array([3, 2, 1, 100, -30]);
18+
var uint_array = new Uint8Array([3, 2, 1, 100, -30]);
19+
var empty_array = new Uint32Array();
20+
21+
assert(float_array.join() === float_array.toString());
22+
assert(int_array.join('-') === "3-2-1-100--30");
23+
assert(uint_array.join('=') === "3=2=1=100=226");
24+
assert(empty_array.join('_') === "");
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/* Copyright JS Foundation and other contributors, http://js.foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
var float_array = new Float32Array([1.125, 5.5, -1.25, -0.0]);
17+
var int_array = new Int8Array([3, 2, 1, 100, -30])
18+
var uint_array = new Uint8Array([3, 2, 1, 100, -30])
19+
var empty_array = new Uint32Array();
20+
21+
assert(float_array.toString() === "1.125,5.5,-1.25,0");
22+
assert(int_array.toString() === "3,2,1,100,-30");
23+
assert(uint_array.toString() === "3,2,1,100,226");
24+
assert(empty_array.toString() === "");

0 commit comments

Comments
 (0)