mirror of
https://github.com/jerryscript-project/jerryscript.git
synced 2025-12-15 16:29:21 +00:00
Related to #4186. Some notable changes: - The term 'Error' now strictly refers to native Error objects defined in the ECMA standard, which are ordinary objects. All other uses of 'error' or 'error reference' where the term refers to a thrown value is now called 'exception'. - Simplified the naming scheme of many String API functions. These functions will now also take an 'encoding' argument to specify the desired encoding in which to operate. - Removed the substring-copy-to-buffer functions. These functions behaved awkwardly, as they use character index to specify the start/end positions, and were mostly used incorrectly with byte offsets instead. The functionality can still be replicated with other functions if necessary. - String-to-buffer functions will no longer fail if the buffer is not sufficiently large, the string will instead be cropped. - Fixed the usage of the '_sz' prefix in many API functions. The term 'sz' means zero-terminated string in hungarian notation, this was used incorrectly in many cases. - Renamed most of the public API functions to have shorter, more on-point names, rather than the often too long descriptive names. Functions are now also grouped by the type of value they operate on, where this makes sense. JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu
106 lines
3.8 KiB
C
106 lines
3.8 KiB
C
/* Copyright JS Foundation and other contributors, http://js.foundation
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
#include "jerryscript-port-default.h"
|
|
#include "jerryscript-port.h"
|
|
#include "jerryscript.h"
|
|
|
|
#include "test-common.h"
|
|
|
|
int
|
|
main (void)
|
|
{
|
|
if (!jerry_feature_enabled (JERRY_FEATURE_DATAVIEW))
|
|
{
|
|
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "DataView support is disabled!\n");
|
|
return 0;
|
|
}
|
|
|
|
/* DataView builtin requires the TypedArray builtin */
|
|
TEST_ASSERT (jerry_feature_enabled (JERRY_FEATURE_TYPEDARRAY));
|
|
|
|
jerry_init (JERRY_INIT_EMPTY);
|
|
|
|
/* Test accessors */
|
|
jerry_value_t arraybuffer = jerry_arraybuffer (16);
|
|
jerry_value_t view1 = jerry_dataview (arraybuffer, 0, 16);
|
|
TEST_ASSERT (!jerry_value_is_exception (view1));
|
|
TEST_ASSERT (jerry_value_is_dataview (view1));
|
|
|
|
jerry_length_t byteOffset = 0;
|
|
jerry_length_t byteLength = 0;
|
|
;
|
|
jerry_value_t internal_buffer = jerry_dataview_buffer (view1, &byteOffset, &byteLength);
|
|
TEST_ASSERT (jerry_binary_op (JERRY_BIN_OP_STRICT_EQUAL, internal_buffer, arraybuffer));
|
|
TEST_ASSERT (byteOffset == 0);
|
|
TEST_ASSERT (byteLength == 16);
|
|
jerry_value_free (internal_buffer);
|
|
|
|
jerry_value_t view2 = jerry_dataview (arraybuffer, 12, 4);
|
|
TEST_ASSERT (!jerry_value_is_exception (view1));
|
|
TEST_ASSERT (jerry_value_is_dataview (view2));
|
|
internal_buffer = jerry_dataview_buffer (view2, &byteOffset, &byteLength);
|
|
TEST_ASSERT (jerry_binary_op (JERRY_BIN_OP_STRICT_EQUAL, internal_buffer, arraybuffer));
|
|
TEST_ASSERT (byteOffset == 12);
|
|
TEST_ASSERT (byteLength == 4);
|
|
jerry_value_free (internal_buffer);
|
|
|
|
/* Test invalid construction */
|
|
jerry_value_t empty_object = jerry_object ();
|
|
jerry_value_t view3 = jerry_dataview (empty_object, 20, 10);
|
|
TEST_ASSERT (jerry_value_is_exception (view3));
|
|
jerry_value_t error_obj = jerry_exception_value (view3, true);
|
|
TEST_ASSERT (jerry_error_type (error_obj) == JERRY_ERROR_TYPE);
|
|
jerry_value_free (error_obj);
|
|
jerry_value_free (empty_object);
|
|
|
|
jerry_value_t view4 = jerry_dataview (arraybuffer, 20, 10);
|
|
TEST_ASSERT (jerry_value_is_exception (view3));
|
|
error_obj = jerry_exception_value (view4, true);
|
|
TEST_ASSERT (jerry_error_type (error_obj) == JERRY_ERROR_RANGE);
|
|
jerry_value_free (error_obj);
|
|
|
|
/* Test getting/setting values */
|
|
jerry_value_t global_obj = jerry_current_realm ();
|
|
jerry_value_t view1_str = jerry_string_sz ("view1");
|
|
jerry_value_t view2_str = jerry_string_sz ("view2");
|
|
TEST_ASSERT (jerry_object_set (global_obj, view1_str, view1));
|
|
TEST_ASSERT (jerry_object_set (global_obj, view2_str, view2));
|
|
|
|
jerry_value_free (view1_str);
|
|
jerry_value_free (view2_str);
|
|
jerry_value_free (global_obj);
|
|
|
|
const jerry_char_t set_src[] = "view1.setInt16 (12, 255)";
|
|
TEST_ASSERT (jerry_value_is_undefined (jerry_eval (set_src, sizeof (set_src) - 1, JERRY_PARSE_NO_OPTS)));
|
|
|
|
const jerry_char_t get_src[] = "view2.getInt16 (0)";
|
|
TEST_ASSERT (jerry_value_as_number (jerry_eval (get_src, sizeof (get_src) - 1, JERRY_PARSE_NO_OPTS)) == 255);
|
|
|
|
const jerry_char_t get_src_little_endian[] = "view2.getInt16 (0, true)";
|
|
TEST_ASSERT (
|
|
jerry_value_as_number (jerry_eval (get_src_little_endian, sizeof (get_src_little_endian) - 1, JERRY_PARSE_NO_OPTS))
|
|
== -256);
|
|
|
|
/* Cleanup */
|
|
jerry_value_free (view2);
|
|
jerry_value_free (view1);
|
|
jerry_value_free (arraybuffer);
|
|
|
|
jerry_cleanup ();
|
|
|
|
return 0;
|
|
} /* main */
|