ac1c48eeff
Notable changes:
- Updated and the port API interface, new functions have been added
and some have been changed. The port library is now cleaned up to
not have any dependency on jerry-core, as it should be. The port library
is now strictly a collection of functions that implement
embedding/platform specific behavior.
- The default port implementation has been split for windows and unix.
Implemented port functions have been categorized and reorganized,
and marked with attribute((weak)) for better reusability.
- External context allocation has been moved to the port API instead
of a core API callback. The iterface has also been extended with a
function to free the allocated context. When external context is
enabled, jerry_init now automatically calls the port implementation
to allocate the context and jerry_cleanup automatically calls the port
to free the context.
- jerry_port_log has been changed to no longer require formatting to
be implemented by the port. The reason beind this is that it was vague what
format specifiers were used by the engine, and in what manner. The port
function now takes a zero-terminated string, and should only implement
how the string should be logged.
- Logging and log message formatting is now handled by the core jerry library
where it can be implemented as necessary. Logging can be done through a new
core API function, which uses the port to output the final log message.
- Log level has been moved into jerry-core, and an API function has
been added to set the log level. It should be the library that
filters log messages based on the requested log level, instead of
logging everything and requiring the user to do so.
- Module resolving logic has been moved into jerry-core. There's no
reason to have it in the port library and requiring embedders to
duplicate the code. It also added an unnecessary dependency on
jerry-core to the port. Platform specific behavior is still used through
the port API, like resolving module specifiers, and reading source file
contents. If necessary, the resolving logic can still be overridden as
previously.
- The jerry-ext library has also been cleaned up, and many utility
functions have been added that previously were implemented in
jerry-main. This allows easier reusability for some common operations,
like printing unhandled exceptions or providing a repl console.
- Debugger interaction with logged/printed messages has been fixed, so
that it's no longer the port implementations responsibility to send
the output to the debugger, as the port should have no notion of what a
debugger is. The printing and logging functions will now pass the
result message to the debugger, if connected.
- Cleaned up TZA handling in the date port implementation, and simplified
the API function prototype.
- Moved property access helper functions that use ASCII strings as
keys from jerry-ext to the core API.
JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu
302 lines
10 KiB
C
302 lines
10 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.
|
|
*/
|
|
|
|
/**
|
|
* Unit test for jerry-ext/handler property registration
|
|
*/
|
|
|
|
#include <string.h>
|
|
|
|
#include "jerryscript.h"
|
|
|
|
#include "jerryscript-ext/properties.h"
|
|
#include "test-common.h"
|
|
|
|
static jerry_value_t
|
|
method_hello (const jerry_call_info_t *call_info_p, /**< call information */
|
|
const jerry_value_t jargv[], /**< arguments */
|
|
const jerry_length_t jargc) /**< number of arguments */
|
|
{
|
|
(void) call_info_p;
|
|
(void) jargv;
|
|
return jerry_number (jargc);
|
|
} /* method_hello */
|
|
|
|
/**
|
|
* Helper method to create a non-configurable property on an object
|
|
*/
|
|
static void
|
|
freeze_property (jerry_value_t target_obj, /**< target object */
|
|
const char *target_prop) /**< target property name */
|
|
{
|
|
// "freeze" property
|
|
jerry_property_descriptor_t prop_desc = jerry_property_descriptor ();
|
|
prop_desc.flags |= JERRY_PROP_IS_CONFIGURABLE_DEFINED;
|
|
|
|
jerry_value_t prop_name = jerry_string_sz (target_prop);
|
|
jerry_value_t return_value = jerry_object_define_own_prop (target_obj, prop_name, &prop_desc);
|
|
TEST_ASSERT (jerry_value_is_boolean (return_value));
|
|
jerry_value_free (return_value);
|
|
jerry_value_free (prop_name);
|
|
|
|
jerry_property_descriptor_free (&prop_desc);
|
|
} /* freeze_property */
|
|
|
|
/**
|
|
* Test registration of various property values.
|
|
*/
|
|
static void
|
|
test_simple_registration (void)
|
|
{
|
|
jerry_init (JERRY_INIT_EMPTY);
|
|
|
|
jerry_value_t target_object = jerry_object ();
|
|
|
|
// Test simple registration
|
|
jerryx_property_entry methods[] = {
|
|
JERRYX_PROPERTY_FUNCTION ("hello", method_hello), JERRYX_PROPERTY_NUMBER ("my_number", 42.5),
|
|
JERRYX_PROPERTY_STRING_SZ ("my_str", "super_str"), JERRYX_PROPERTY_STRING ("my_str_sz", "super_str", 6),
|
|
JERRYX_PROPERTY_BOOLEAN ("my_bool", true), JERRYX_PROPERTY_BOOLEAN ("my_bool_false", false),
|
|
JERRYX_PROPERTY_UNDEFINED ("my_non_value"), JERRYX_PROPERTY_LIST_END (),
|
|
};
|
|
|
|
jerryx_register_result register_result = jerryx_set_properties (target_object, methods);
|
|
|
|
TEST_ASSERT (register_result.registered == 7);
|
|
TEST_ASSERT (jerry_value_is_undefined (register_result.result));
|
|
|
|
jerryx_release_property_entry (methods, register_result);
|
|
jerry_value_free (register_result.result);
|
|
|
|
jerry_value_t global_obj = jerry_current_realm ();
|
|
jerry_object_set_sz (global_obj, "test", target_object);
|
|
jerry_value_free (target_object);
|
|
jerry_value_free (global_obj);
|
|
|
|
{
|
|
const char *test_A = "test.my_number";
|
|
jerry_value_t result = jerry_eval ((const jerry_char_t *) test_A, strlen (test_A), 0);
|
|
TEST_ASSERT (jerry_value_is_number (result));
|
|
TEST_ASSERT (jerry_value_as_number (result) == 42.5);
|
|
jerry_value_free (result);
|
|
}
|
|
|
|
{
|
|
const char *test_A = "test.my_str_sz === 'super_'";
|
|
jerry_value_t result = jerry_eval ((const jerry_char_t *) test_A, strlen (test_A), 0);
|
|
TEST_ASSERT (jerry_value_is_boolean (result));
|
|
TEST_ASSERT (jerry_value_is_true (result));
|
|
jerry_value_free (result);
|
|
}
|
|
|
|
{
|
|
const char *test_A = "test.my_str === 'super_str'";
|
|
jerry_value_t result = jerry_eval ((const jerry_char_t *) test_A, strlen (test_A), 0);
|
|
TEST_ASSERT (jerry_value_is_boolean (result));
|
|
TEST_ASSERT (jerry_value_is_true (result));
|
|
jerry_value_free (result);
|
|
}
|
|
|
|
{
|
|
const char *test_A = "test.my_bool";
|
|
jerry_value_t result = jerry_eval ((const jerry_char_t *) test_A, strlen (test_A), 0);
|
|
TEST_ASSERT (jerry_value_is_boolean (result));
|
|
TEST_ASSERT (jerry_value_is_true (result));
|
|
jerry_value_free (result);
|
|
}
|
|
|
|
{
|
|
const char *test_A = "test.my_bool_false";
|
|
jerry_value_t result = jerry_eval ((const jerry_char_t *) test_A, strlen (test_A), 0);
|
|
TEST_ASSERT (jerry_value_is_boolean (result));
|
|
TEST_ASSERT (jerry_value_is_true (result) == false);
|
|
jerry_value_free (result);
|
|
}
|
|
|
|
{
|
|
const char *test_A = "test.my_non_value";
|
|
jerry_value_t result = jerry_eval ((const jerry_char_t *) test_A, strlen (test_A), 0);
|
|
TEST_ASSERT (jerry_value_is_undefined (result));
|
|
jerry_value_free (result);
|
|
}
|
|
|
|
{
|
|
const char *test_A = "test.hello(33, 42, 2);";
|
|
jerry_value_t result = jerry_eval ((const jerry_char_t *) test_A, strlen (test_A), 0);
|
|
TEST_ASSERT (jerry_value_is_number (result));
|
|
TEST_ASSERT ((uint32_t) jerry_value_as_number (result) == 3u);
|
|
jerry_value_free (result);
|
|
}
|
|
|
|
{
|
|
const char *test_A = "test.hello();";
|
|
jerry_value_t result = jerry_eval ((const jerry_char_t *) test_A, strlen (test_A), 0);
|
|
TEST_ASSERT (jerry_value_is_number (result));
|
|
TEST_ASSERT ((uint32_t) jerry_value_as_number (result) == 0u);
|
|
jerry_value_free (result);
|
|
}
|
|
|
|
jerry_cleanup ();
|
|
} /* test_simple_registration */
|
|
|
|
/**
|
|
* Test registration error.
|
|
*
|
|
* Trying to register a property which is already a non-configurable property
|
|
* should result in an error.
|
|
*/
|
|
static void
|
|
test_error_setvalue (void)
|
|
{
|
|
jerry_init (JERRY_INIT_EMPTY);
|
|
|
|
const char *target_prop = "test_err";
|
|
jerry_value_t global_obj = jerry_current_realm ();
|
|
freeze_property (global_obj, target_prop);
|
|
|
|
jerry_value_t new_object = jerry_object ();
|
|
jerry_value_t set_result = jerry_object_set_sz (global_obj, target_prop, new_object);
|
|
TEST_ASSERT (jerry_value_is_exception (set_result));
|
|
|
|
jerry_value_free (set_result);
|
|
jerry_value_free (new_object);
|
|
jerry_value_free (global_obj);
|
|
|
|
jerry_cleanup ();
|
|
} /* test_error_setvalue */
|
|
|
|
/**
|
|
* Test registration error with jerryx_set_properties.
|
|
*
|
|
* Trying to register a property which is already a non-configurable property
|
|
* should result in an error.
|
|
*/
|
|
static void
|
|
test_error_single_function (void)
|
|
{
|
|
jerry_init (JERRY_INIT_EMPTY);
|
|
|
|
const char *target_prop = "test_err";
|
|
jerry_value_t target_object = jerry_object ();
|
|
freeze_property (target_object, target_prop);
|
|
|
|
jerryx_property_entry methods[] = {
|
|
JERRYX_PROPERTY_FUNCTION (target_prop, method_hello), // This registration should fail
|
|
JERRYX_PROPERTY_LIST_END (),
|
|
};
|
|
|
|
jerryx_register_result register_result = jerryx_set_properties (target_object, methods);
|
|
|
|
TEST_ASSERT (register_result.registered == 0);
|
|
TEST_ASSERT (jerry_value_is_exception (register_result.result));
|
|
jerryx_release_property_entry (methods, register_result);
|
|
jerry_value_free (register_result.result);
|
|
|
|
jerry_value_free (target_object);
|
|
|
|
jerry_cleanup ();
|
|
} /* test_error_single_function */
|
|
|
|
/**
|
|
* Test to see if jerryx_set_properties exits at the first error.
|
|
*/
|
|
static void
|
|
test_error_multiple_functions (void)
|
|
{
|
|
jerry_init (JERRY_INIT_EMPTY);
|
|
|
|
const char *prop_ok = "prop_ok";
|
|
const char *prop_err = "prop_err";
|
|
const char *prop_not = "prop_not";
|
|
jerry_value_t target_object = jerry_object ();
|
|
freeze_property (target_object, prop_err);
|
|
|
|
jerryx_property_entry methods[] = {
|
|
JERRYX_PROPERTY_FUNCTION (prop_ok, method_hello), // This registration is ok
|
|
JERRYX_PROPERTY_FUNCTION (prop_err, method_hello), // This registration should fail
|
|
JERRYX_PROPERTY_FUNCTION (prop_not, method_hello), // This registration is not done
|
|
JERRYX_PROPERTY_LIST_END (),
|
|
};
|
|
|
|
jerryx_register_result register_result = jerryx_set_properties (target_object, methods);
|
|
|
|
TEST_ASSERT (register_result.registered == 1);
|
|
TEST_ASSERT (jerry_value_is_exception (register_result.result));
|
|
|
|
jerryx_release_property_entry (methods, register_result);
|
|
jerry_value_free (register_result.result);
|
|
|
|
{
|
|
// Test if property "prop_ok" is correctly registered.
|
|
jerry_value_t prop_ok_val = jerry_string_sz (prop_ok);
|
|
jerry_value_t prop_ok_exists = jerry_object_has_own (target_object, prop_ok_val);
|
|
TEST_ASSERT (jerry_value_is_true (prop_ok_exists));
|
|
jerry_value_free (prop_ok_exists);
|
|
|
|
// Try calling the method
|
|
jerry_value_t prop_ok_func = jerry_object_get (target_object, prop_ok_val);
|
|
TEST_ASSERT (jerry_value_is_function (prop_ok_func) == true);
|
|
jerry_value_t args[2] = {
|
|
jerry_number (22),
|
|
jerry_number (-3),
|
|
};
|
|
jerry_size_t args_cnt = sizeof (args) / sizeof (jerry_value_t);
|
|
jerry_value_t func_result = jerry_call (prop_ok_func, jerry_undefined (), args, args_cnt);
|
|
TEST_ASSERT (jerry_value_is_number (func_result) == true);
|
|
TEST_ASSERT ((uint32_t) jerry_value_as_number (func_result) == 2u);
|
|
jerry_value_free (func_result);
|
|
for (jerry_size_t idx = 0; idx < args_cnt; idx++)
|
|
{
|
|
jerry_value_free (args[idx]);
|
|
}
|
|
jerry_value_free (prop_ok_func);
|
|
jerry_value_free (prop_ok_val);
|
|
}
|
|
|
|
{
|
|
// The "prop_err" should exist - as it was "freezed" - but it should not be a function
|
|
jerry_value_t prop_err_val = jerry_string_sz (prop_err);
|
|
jerry_value_t prop_err_exists = jerry_object_has_own (target_object, prop_err_val);
|
|
TEST_ASSERT (jerry_value_is_true (prop_err_exists));
|
|
jerry_value_free (prop_err_exists);
|
|
|
|
jerry_value_t prop_err_func = jerry_value_is_function (prop_err_val);
|
|
TEST_ASSERT (jerry_value_is_function (prop_err_func) == false);
|
|
jerry_value_free (prop_err_val);
|
|
}
|
|
|
|
{ // The "prop_not" is not available on the target object
|
|
jerry_value_t prop_not_val = jerry_string_sz (prop_not);
|
|
jerry_value_t prop_not_exists = jerry_object_has_own (target_object, prop_not_val);
|
|
TEST_ASSERT (jerry_value_is_true (prop_not_exists) == false);
|
|
jerry_value_free (prop_not_exists);
|
|
jerry_value_free (prop_not_val);
|
|
}
|
|
|
|
jerry_value_free (target_object);
|
|
|
|
jerry_cleanup ();
|
|
} /* test_error_multiple_functions */
|
|
|
|
int
|
|
main (void)
|
|
{
|
|
test_simple_registration ();
|
|
test_error_setvalue ();
|
|
test_error_single_function ();
|
|
test_error_multiple_functions ();
|
|
return 0;
|
|
} /* main */
|