• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "config.h"
17 #include "jerryscript.h"
18 #include "jerryscript-port.h"
19 #include "jerryscript-port-default.h"
20 #include "test-common.h"
21 #include <gtest/gtest.h>
22 
23 static jerry_value_t
resource_name_handler(const jerry_value_t function_obj,const jerry_value_t this_val,const jerry_value_t args_p[],const jerry_length_t args_count)24 resource_name_handler (const jerry_value_t function_obj, /**< function object */
25                        const jerry_value_t this_val, /**< this value */
26                        const jerry_value_t args_p[], /**< argument list */
27                        const jerry_length_t args_count) /**< argument count */
28 {
29   (void) function_obj;
30   (void) this_val;
31 
32   jerry_value_t undefined_value = jerry_create_undefined ();
33   jerry_value_t resource_name = jerry_get_resource_name (args_count > 0 ? args_p[0] : undefined_value);
34   jerry_release_value (undefined_value);
35 
36   return resource_name;
37 } /* resource_name_handler */
38 
39 class ResourceNameTest : public testing::Test{
40 public:
SetUpTestCase()41     static void SetUpTestCase()
42     {
43         GTEST_LOG_(INFO) << "ResourceNameTest SetUpTestCase";
44     }
45 
TearDownTestCase()46     static void TearDownTestCase()
47     {
48         GTEST_LOG_(INFO) << "ResourceNameTest TearDownTestCase";
49     }
50 
SetUp()51     void SetUp() override {}
TearDown()52     void TearDown() override {}
53 
54 };
55 
56 static constexpr size_t JERRY_SCRIPT_MEM_SIZE = 50 * 1024 * 1024;
context_alloc_fn(size_t size,void * cb_data)57 static void* context_alloc_fn(size_t size, void* cb_data)
58 {
59     (void)cb_data;
60     size_t newSize = size > JERRY_SCRIPT_MEM_SIZE ? JERRY_SCRIPT_MEM_SIZE : size;
61     return malloc(newSize);
62 }
63 
64 HWTEST_F(ResourceNameTest, Test001, testing::ext::TestSize.Level1)
65 {
66   TEST_INIT ();
67 
68   if (!jerry_is_feature_enabled (JERRY_FEATURE_LINE_INFO))
69   {
70     jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Line info support is disabled!\n");
71   }
72   jerry_context_t *ctx_p = jerry_create_context (1024, context_alloc_fn, NULL);
73   jerry_port_default_set_current_context (ctx_p);
74   jerry_init (JERRY_INIT_EMPTY);
75 
76   jerry_value_t global = jerry_get_global_object ();
77 
78   /* Register the "resourceName" method. */
79   {
80     jerry_value_t func = jerry_create_external_function (resource_name_handler);
81     jerry_value_t name = jerry_create_string ((const jerry_char_t *) "resourceName");
82     jerry_value_t result = jerry_set_property (global, name, func);
83     jerry_release_value (result);
84     jerry_release_value (name);
85     jerry_release_value (func);
86   }
87 
88   jerry_release_value (global);
89 
90   const char *source_1 = ("function f1 () {\n"
91                           "  if (resourceName() !== 'demo1.js') return false; \n"
92                           "  if (resourceName(f1) !== 'demo1.js') return false; \n"
93                           "  if (resourceName(5) !== '<anonymous>') return false; \n"
94                           "  return f1; \n"
95                           "} \n"
96                           "f1();");
97   const char *resource_1 = "demo1.js";
98 
99   jerry_value_t program = jerry_parse ((const jerry_char_t *) resource_1,
100                                        strlen (resource_1),
101                                        (const jerry_char_t *) source_1,
102                                        strlen (source_1),
103                                        JERRY_PARSE_NO_OPTS);
104   TEST_ASSERT (!jerry_value_is_error (program));
105 
106   jerry_value_t run_result = jerry_run (program);
107   TEST_ASSERT (!jerry_value_is_error (run_result));
108   TEST_ASSERT (jerry_value_is_object (run_result));
109 
110   jerry_value_t resource_value = jerry_get_resource_name (run_result);
111   jerry_value_t resource1_name_value = jerry_create_string ((const jerry_char_t *) resource_1);
112   TEST_ASSERT (jerry_binary_operation (JERRY_BIN_OP_STRICT_EQUAL, resource_value, resource1_name_value));
113   jerry_release_value (resource1_name_value);
114   jerry_release_value (resource_value);
115 
116   jerry_release_value (run_result);
117   jerry_release_value (program);
118 
119   const char *source_2 = ("function f2 () { \n"
120                           "  if (resourceName() !== 'demo2.js') return false; \n"
121                           "  if (resourceName(f2) !== 'demo2.js') return false; \n"
122                           "  if (resourceName(f1) !== 'demo1.js') return false; \n"
123                           "  if (resourceName(Object.prototype) !== '<anonymous>') return false; \n"
124                           "  if (resourceName(Function) !== '<anonymous>') return false; \n"
125                           "  return f2; \n"
126                           "} \n"
127                           "f2(); \n");
128   const char *resource_2 = "demo2.js";
129 
130   program = jerry_parse ((const jerry_char_t *) resource_2,
131                          strlen (resource_2),
132                          (const jerry_char_t *) source_2,
133                          strlen (source_2),
134                          JERRY_PARSE_NO_OPTS);
135   ASSERT_TRUE (jerry_value_is_error (program));
136 
137   run_result = jerry_run (program);
138   ASSERT_TRUE (jerry_value_is_error (run_result));
139   ASSERT_TRUE (!(jerry_value_is_object (run_result)));
140 
141   resource_value = jerry_get_resource_name (run_result);
142   jerry_value_t resource2_name_value = jerry_create_string ((const jerry_char_t *) resource_2);
143   TEST_ASSERT (jerry_binary_operation (JERRY_BIN_OP_STRICT_EQUAL, resource_value, resource2_name_value));
144   jerry_release_value (resource2_name_value);
145   jerry_release_value (resource_value);
146 
147   jerry_release_value (run_result);
148   jerry_release_value (program);
149 
150   jerry_cleanup ();
151   free (ctx_p);
152 }
153