Skip to content

Commit 2a15bf7

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 e54dde5 commit 2a15bf7

7 files changed

+289
-58
lines changed

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@
2525
#include "ecma-builtin-internal-routines-template.inc.h"
2626

2727
/** \addtogroup ecma ECMA
28-
* @{
29-
*
30-
* \addtogroup ecmabuiltins
31-
* @{
32-
*
33-
* \addtogroup float32arrayprototype ECMA Float32Array.prototype object built-in
34-
* @{
35-
*/
28+
* @{
29+
*
30+
* \addtogroup ecmabuiltins
31+
* @{
32+
*
33+
* \addtogroup float32arrayprototype ECMA Float32Array.prototype object built-in
34+
* @{
35+
*/
3636

3737
/**
38-
* @}
39-
* @}
40-
* @}
41-
*/
38+
* @}
39+
* @}
40+
* @}
41+
*/
4242

4343
#endif /* !CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN */

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 */
Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
/* 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-
*/
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+
*/
1515

1616
#include "ecma-builtins.h"
1717

@@ -25,19 +25,19 @@
2525
#include "ecma-builtin-internal-routines-template.inc.h"
2626

2727
/** \addtogroup ecma ECMA
28-
* @{
29-
*
30-
* \addtogroup ecmabuiltins
31-
* @{
32-
*
33-
* \addtogroup int8arrayprototype ECMA Int8Array.prototype object built-in
34-
* @{
35-
*/
28+
* @{
29+
*
30+
* \addtogroup ecmabuiltins
31+
* @{
32+
*
33+
* \addtogroup int8arrayprototype ECMA Int8Array.prototype object built-in
34+
* @{
35+
*/
3636

3737
/**
38-
* @}
39-
* @}
40-
* @}
41-
*/
38+
* @}
39+
* @}
40+
* @}
41+
*/
4242

4343
#endif /* !CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN */

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

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,209 @@ 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+
787+
ECMA_TRY_CATCH (index_value,
788+
ecma_op_object_get (obj_p, index_string_p),
789+
ret_value);
790+
791+
if (ecma_is_value_undefined (index_value)
792+
|| ecma_is_value_null (index_value))
793+
{
794+
ret_value = ecma_make_magic_string_value (LIT_MAGIC_STRING__EMPTY);
795+
}
796+
else
797+
{
798+
ret_value = ecma_op_to_string (index_value);
799+
}
800+
801+
ECMA_FINALIZE (index_value);
802+
803+
ecma_deref_ecma_string (index_string_p);
804+
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+
ecma_value_t ret_value = ECMA_VALUE_EMPTY;
844+
845+
/* 1. */
846+
ECMA_TRY_CATCH (obj_value,
847+
ecma_op_to_object (this_arg),
848+
ret_value);
849+
850+
ecma_object_t *obj_p = ecma_get_object_from_value (obj_value);
851+
852+
/* 2. */
853+
ECMA_TRY_CATCH (length_value,
854+
ecma_op_object_get_by_magic_id (obj_p, LIT_MAGIC_STRING_LENGTH),
855+
ret_value);
856+
857+
ECMA_OP_TO_NUMBER_TRY_CATCH (length_number,
858+
length_value,
859+
ret_value);
860+
861+
/* 3. */
862+
uint32_t length = ecma_number_to_uint32 (length_number);
863+
864+
/* 4-5. */
865+
ECMA_TRY_CATCH (separator_value,
866+
ecma_op_typedarray_get_separator_string (separator_arg),
867+
ret_value);
868+
869+
if (length == 0)
870+
{
871+
/* 6. */
872+
ret_value = ecma_make_magic_string_value (LIT_MAGIC_STRING__EMPTY);
873+
}
874+
else
875+
{
876+
ecma_string_t *separator_string_p = ecma_get_string_from_value (separator_value);
877+
878+
/* 7-8. */
879+
ECMA_TRY_CATCH (first_value,
880+
ecma_op_typedarray_get_to_string_at_index (obj_p, 0),
881+
ret_value);
882+
883+
ecma_string_t *return_string_p = ecma_get_string_from_value (first_value);
884+
ecma_ref_ecma_string (return_string_p);
885+
886+
/* 9-10. */
887+
for (uint32_t k = 1; ecma_is_value_empty (ret_value) && (k < length); k++)
888+
{
889+
/* 10.a */
890+
return_string_p = ecma_concat_ecma_strings (return_string_p, separator_string_p);
891+
892+
/* 10.b, 10.c */
893+
ECMA_TRY_CATCH (next_string_value,
894+
ecma_op_typedarray_get_to_string_at_index (obj_p, k),
895+
ret_value);
896+
897+
/* 10.d */
898+
ecma_string_t *next_string_p = ecma_get_string_from_value (next_string_value);
899+
return_string_p = ecma_concat_ecma_strings (return_string_p, next_string_p);
900+
901+
ECMA_FINALIZE (next_string_value);
902+
}
903+
904+
if (ecma_is_value_empty (ret_value))
905+
{
906+
ret_value = ecma_make_string_value (return_string_p);
907+
}
908+
else
909+
{
910+
ecma_deref_ecma_string (return_string_p);
911+
}
912+
913+
ECMA_FINALIZE (first_value);
914+
}
915+
916+
ECMA_FINALIZE (separator_value);
917+
918+
ECMA_OP_TO_NUMBER_FINALIZE (length_number);
919+
920+
ECMA_FINALIZE (length_value);
921+
ECMA_FINALIZE (obj_value);
922+
923+
return ret_value;
924+
} /* ecma_builtin_typedarray_prototype_join */
925+
926+
927+
/**
928+
* The TypedArray.prototype object's 'toString' routine basen on
929+
* the Array.porottype object's 'toString'
930+
*
931+
* See also:
932+
* ECMA-262 v5, 15.4.4.2
933+
*
934+
* @return ecma value
935+
* Returned value must be freed with ecma_free_value.
936+
*/
937+
static ecma_value_t
938+
ecma_builtin_typedarray_prototype_object_to_string (ecma_value_t this_arg) /**< this argument */
939+
{
940+
ecma_value_t ret_value = ECMA_VALUE_EMPTY;
941+
942+
/* 1. */
943+
ECMA_TRY_CATCH (obj_this_value,
944+
ecma_op_to_object (this_arg),
945+
ret_value);
946+
947+
ecma_object_t *obj_p = ecma_get_object_from_value (obj_this_value);
948+
949+
/* 2. */
950+
ECMA_TRY_CATCH (join_value,
951+
ecma_op_object_get_by_magic_id (obj_p, LIT_MAGIC_STRING_JOIN),
952+
ret_value);
953+
954+
if (!ecma_op_is_callable (join_value))
955+
{
956+
/* 3. */
957+
ret_value = ecma_builtin_helper_object_to_string (this_arg);
958+
}
959+
else
960+
{
961+
/* 4. */
962+
ecma_object_t *join_func_obj_p = ecma_get_object_from_value (join_value);
963+
964+
ret_value = ecma_op_function_call (join_func_obj_p, this_arg, NULL, 0);
965+
}
966+
967+
ECMA_FINALIZE (join_value);
968+
ECMA_FINALIZE (obj_this_value);
969+
970+
return ret_value;
971+
} /* ecma_builtin_typedarray_prototype_object_to_string */
972+
770973
/**
771974
* @}
772975
* @}

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/ecma/builtin-objects/typedarray/ecma-builtin-uint8array-prototype.c

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
/* 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-
*/
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+
*/
1515

1616
#include "ecma-builtins.h"
1717

@@ -25,14 +25,14 @@
2525
#include "ecma-builtin-internal-routines-template.inc.h"
2626

2727
/** \addtogroup ecma ECMA
28-
* @{
29-
*
30-
* \addtogroup ecmabuiltins
31-
* @{
32-
*
33-
* \addtogroup uint8arrayprototype ECMA Uint8Array.prototype object built-in
34-
* @{
35-
*/
28+
* @{
29+
*
30+
* \addtogroup ecmabuiltins
31+
* @{
32+
*
33+
* \addtogroup uint8arrayprototype ECMA Uint8Array.prototype object built-in
34+
* @{
35+
*/
3636

3737
/**
3838
* @}

0 commit comments

Comments
 (0)