1425bb815Sopenharmony_ci/* Copyright JS Foundation and other contributors, http://js.foundation 2425bb815Sopenharmony_ci * 3425bb815Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4425bb815Sopenharmony_ci * you may not use this file except in compliance with the License. 5425bb815Sopenharmony_ci * You may obtain a copy of the License at 6425bb815Sopenharmony_ci * 7425bb815Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8425bb815Sopenharmony_ci * 9425bb815Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10425bb815Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS 11425bb815Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12425bb815Sopenharmony_ci * See the License for the specific language governing permissions and 13425bb815Sopenharmony_ci * limitations under the License. 14425bb815Sopenharmony_ci */ 15425bb815Sopenharmony_ci 16425bb815Sopenharmony_ci#include "config.h" 17425bb815Sopenharmony_ci#include "jerryscript.h" 18425bb815Sopenharmony_ci#include "jerryscript-port.h" 19425bb815Sopenharmony_ci#include "jerryscript-port-default.h" 20425bb815Sopenharmony_ci#include "test-common.h" 21425bb815Sopenharmony_ci#include <gtest/gtest.h> 22425bb815Sopenharmony_ci 23425bb815Sopenharmony_cistatic jerry_value_t 24425bb815Sopenharmony_ciresource_name_handler (const jerry_value_t function_obj, /**< function object */ 25425bb815Sopenharmony_ci const jerry_value_t this_val, /**< this value */ 26425bb815Sopenharmony_ci const jerry_value_t args_p[], /**< argument list */ 27425bb815Sopenharmony_ci const jerry_length_t args_count) /**< argument count */ 28425bb815Sopenharmony_ci{ 29425bb815Sopenharmony_ci (void) function_obj; 30425bb815Sopenharmony_ci (void) this_val; 31425bb815Sopenharmony_ci 32425bb815Sopenharmony_ci jerry_value_t undefined_value = jerry_create_undefined (); 33425bb815Sopenharmony_ci jerry_value_t resource_name = jerry_get_resource_name (args_count > 0 ? args_p[0] : undefined_value); 34425bb815Sopenharmony_ci jerry_release_value (undefined_value); 35425bb815Sopenharmony_ci 36425bb815Sopenharmony_ci return resource_name; 37425bb815Sopenharmony_ci} /* resource_name_handler */ 38425bb815Sopenharmony_ci 39425bb815Sopenharmony_ciclass ResourceNameTest : public testing::Test{ 40425bb815Sopenharmony_cipublic: 41425bb815Sopenharmony_ci static void SetUpTestCase() 42425bb815Sopenharmony_ci { 43425bb815Sopenharmony_ci GTEST_LOG_(INFO) << "ResourceNameTest SetUpTestCase"; 44425bb815Sopenharmony_ci } 45425bb815Sopenharmony_ci 46425bb815Sopenharmony_ci static void TearDownTestCase() 47425bb815Sopenharmony_ci { 48425bb815Sopenharmony_ci GTEST_LOG_(INFO) << "ResourceNameTest TearDownTestCase"; 49425bb815Sopenharmony_ci } 50425bb815Sopenharmony_ci 51425bb815Sopenharmony_ci void SetUp() override {} 52425bb815Sopenharmony_ci void TearDown() override {} 53425bb815Sopenharmony_ci 54425bb815Sopenharmony_ci}; 55425bb815Sopenharmony_ci 56425bb815Sopenharmony_cistatic constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024; 57425bb815Sopenharmony_cistatic void* context_alloc_fn(size_t size, void* cb_data) 58425bb815Sopenharmony_ci{ 59425bb815Sopenharmony_ci (void)cb_data; 60425bb815Sopenharmony_ci size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size; 61425bb815Sopenharmony_ci return malloc(newSize); 62425bb815Sopenharmony_ci} 63425bb815Sopenharmony_ci 64425bb815Sopenharmony_ciHWTEST_F(ResourceNameTest, Test001, testing::ext::TestSize.Level1) 65425bb815Sopenharmony_ci{ 66425bb815Sopenharmony_ci TEST_INIT (); 67425bb815Sopenharmony_ci 68425bb815Sopenharmony_ci if (!jerry_is_feature_enabled (JERRY_FEATURE_LINE_INFO)) 69425bb815Sopenharmony_ci { 70425bb815Sopenharmony_ci jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Line info support is disabled!\n"); 71425bb815Sopenharmony_ci } 72425bb815Sopenharmony_ci jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL); 73425bb815Sopenharmony_ci jerry_port_default_set_current_context (ctx_p); 74425bb815Sopenharmony_ci jerry_init (JERRY_INIT_EMPTY); 75425bb815Sopenharmony_ci 76425bb815Sopenharmony_ci jerry_value_t global = jerry_get_global_object (); 77425bb815Sopenharmony_ci 78425bb815Sopenharmony_ci /* Register the "resourceName" method. */ 79425bb815Sopenharmony_ci { 80425bb815Sopenharmony_ci jerry_value_t func = jerry_create_external_function (resource_name_handler); 81425bb815Sopenharmony_ci jerry_value_t name = jerry_create_string ((const jerry_char_t *) "resourceName"); 82425bb815Sopenharmony_ci jerry_value_t result = jerry_set_property (global, name, func); 83425bb815Sopenharmony_ci jerry_release_value (result); 84425bb815Sopenharmony_ci jerry_release_value (name); 85425bb815Sopenharmony_ci jerry_release_value (func); 86425bb815Sopenharmony_ci } 87425bb815Sopenharmony_ci 88425bb815Sopenharmony_ci jerry_release_value (global); 89425bb815Sopenharmony_ci 90425bb815Sopenharmony_ci const char *source_1 = ("function f1 () {\n" 91425bb815Sopenharmony_ci " if (resourceName() !== 'demo1.js') return false; \n" 92425bb815Sopenharmony_ci " if (resourceName(f1) !== 'demo1.js') return false; \n" 93425bb815Sopenharmony_ci " if (resourceName(5) !== '<anonymous>') return false; \n" 94425bb815Sopenharmony_ci " return f1; \n" 95425bb815Sopenharmony_ci "} \n" 96425bb815Sopenharmony_ci "f1();"); 97425bb815Sopenharmony_ci const char *resource_1 = "demo1.js"; 98425bb815Sopenharmony_ci 99425bb815Sopenharmony_ci jerry_value_t program = jerry_parse ((const jerry_char_t *) resource_1, 100425bb815Sopenharmony_ci strlen (resource_1), 101425bb815Sopenharmony_ci (const jerry_char_t *) source_1, 102425bb815Sopenharmony_ci strlen (source_1), 103425bb815Sopenharmony_ci JERRY_PARSE_NO_OPTS); 104425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (program)); 105425bb815Sopenharmony_ci 106425bb815Sopenharmony_ci jerry_value_t run_result = jerry_run (program); 107425bb815Sopenharmony_ci TEST_ASSERT (!jerry_value_is_error (run_result)); 108425bb815Sopenharmony_ci TEST_ASSERT (jerry_value_is_object (run_result)); 109425bb815Sopenharmony_ci 110425bb815Sopenharmony_ci jerry_value_t resource_value = jerry_get_resource_name (run_result); 111425bb815Sopenharmony_ci jerry_value_t resource1_name_value = jerry_create_string ((const jerry_char_t *) resource_1); 112425bb815Sopenharmony_ci TEST_ASSERT (jerry_binary_operation (JERRY_BIN_OP_STRICT_EQUAL, resource_value, resource1_name_value)); 113425bb815Sopenharmony_ci jerry_release_value (resource1_name_value); 114425bb815Sopenharmony_ci jerry_release_value (resource_value); 115425bb815Sopenharmony_ci 116425bb815Sopenharmony_ci jerry_release_value (run_result); 117425bb815Sopenharmony_ci jerry_release_value (program); 118425bb815Sopenharmony_ci 119425bb815Sopenharmony_ci const char *source_2 = ("function f2 () { \n" 120425bb815Sopenharmony_ci " if (resourceName() !== 'demo2.js') return false; \n" 121425bb815Sopenharmony_ci " if (resourceName(f2) !== 'demo2.js') return false; \n" 122425bb815Sopenharmony_ci " if (resourceName(f1) !== 'demo1.js') return false; \n" 123425bb815Sopenharmony_ci " if (resourceName(Object.prototype) !== '<anonymous>') return false; \n" 124425bb815Sopenharmony_ci " if (resourceName(Function) !== '<anonymous>') return false; \n" 125425bb815Sopenharmony_ci " return f2; \n" 126425bb815Sopenharmony_ci "} \n" 127425bb815Sopenharmony_ci "f2(); \n"); 128425bb815Sopenharmony_ci const char *resource_2 = "demo2.js"; 129425bb815Sopenharmony_ci 130425bb815Sopenharmony_ci program = jerry_parse ((const jerry_char_t *) resource_2, 131425bb815Sopenharmony_ci strlen (resource_2), 132425bb815Sopenharmony_ci (const jerry_char_t *) source_2, 133425bb815Sopenharmony_ci strlen (source_2), 134425bb815Sopenharmony_ci JERRY_PARSE_NO_OPTS); 135425bb815Sopenharmony_ci ASSERT_TRUE (jerry_value_is_error (program)); 136425bb815Sopenharmony_ci 137425bb815Sopenharmony_ci run_result = jerry_run (program); 138425bb815Sopenharmony_ci ASSERT_TRUE (jerry_value_is_error (run_result)); 139425bb815Sopenharmony_ci ASSERT_TRUE (!(jerry_value_is_object (run_result))); 140425bb815Sopenharmony_ci 141425bb815Sopenharmony_ci resource_value = jerry_get_resource_name (run_result); 142425bb815Sopenharmony_ci jerry_value_t resource2_name_value = jerry_create_string ((const jerry_char_t *) resource_2); 143425bb815Sopenharmony_ci TEST_ASSERT (jerry_binary_operation (JERRY_BIN_OP_STRICT_EQUAL, resource_value, resource2_name_value)); 144425bb815Sopenharmony_ci jerry_release_value (resource2_name_value); 145425bb815Sopenharmony_ci jerry_release_value (resource_value); 146425bb815Sopenharmony_ci 147425bb815Sopenharmony_ci jerry_release_value (run_result); 148425bb815Sopenharmony_ci jerry_release_value (program); 149425bb815Sopenharmony_ci 150425bb815Sopenharmony_ci jerry_cleanup (); 151425bb815Sopenharmony_ci free (ctx_p); 152425bb815Sopenharmony_ci} 153