• 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 <string.h>
17 
18 #include "jerryscript.h"
19 #include "test-common.h"
20 #include "jerryscript-ext/module.h"
21 
22 #define ACTUAL_NAME "alice"
23 #define ALIAS_NAME "bob"
24 
25 static jerry_value_t
get_canonical_name(const jerry_value_t name)26 get_canonical_name (const jerry_value_t name)
27 {
28   jerry_size_t name_size = jerry_get_string_size (name);
29   JERRY_VLA (jerry_char_t, name_string, name_size + 1);
30   jerry_string_to_char_buffer (name, name_string, name_size);
31   name_string[name_size] = 0;
32 
33   if (!strcmp ((char *) name_string, ACTUAL_NAME))
34   {
35     return jerry_acquire_value (name);
36   }
37   else if (!strcmp ((char *) name_string, ALIAS_NAME))
38   {
39     return jerry_create_string ((jerry_char_t *) ACTUAL_NAME);
40   }
41   else
42   {
43     return jerry_create_undefined ();
44   }
45 } /* get_canonical_name */
46 
47 static bool
resolve(const jerry_value_t canonical_name,jerry_value_t * result)48 resolve (const jerry_value_t canonical_name, jerry_value_t *result)
49 {
50   jerry_size_t name_size = jerry_get_string_size (canonical_name);
51   JERRY_VLA (jerry_char_t, name_string, name_size + 1);
52   jerry_string_to_char_buffer (canonical_name, name_string, name_size);
53   name_string[name_size] = 0;
54 
55   if (!strcmp ((char *) name_string, ACTUAL_NAME))
56   {
57     *result = jerry_create_object ();
58     return true;
59   }
60   return false;
61 } /* resolve */
62 
63 static const jerryx_module_resolver_t canonical_test =
64 {
65   .get_canonical_name_p = get_canonical_name,
66   .resolve_p = resolve
67 };
68 
69 #define TEST_VALUE 95.0
70 
71 int
main(int argc,char ** argv)72 main (int argc, char **argv)
73 {
74   (void) argc;
75   (void) argv;
76 
77   const jerryx_module_resolver_t *resolver = &canonical_test;
78 
79   jerry_init (JERRY_INIT_EMPTY);
80 
81   jerry_value_t actual_name = jerry_create_string ((jerry_char_t *) ACTUAL_NAME);
82   jerry_value_t alias_name = jerry_create_string ((jerry_char_t *) ALIAS_NAME);
83 
84   /* It's important that we resolve by the non-canonical name first. */
85   jerry_value_t result2 = jerryx_module_resolve (alias_name, &resolver, 1);
86   jerry_value_t result1 = jerryx_module_resolve (actual_name, &resolver, 1);
87   jerry_release_value (actual_name);
88   jerry_release_value (alias_name);
89 
90   /* An elaborate way of doing strict equal - set a property on one object and it "magically" appears on the other. */
91   jerry_value_t prop_name = jerry_create_string ((jerry_char_t *) "something");
92   jerry_value_t prop_value = jerry_create_number (TEST_VALUE);
93   jerry_release_value (jerry_set_property (result1, prop_name, prop_value));
94   jerry_release_value (prop_value);
95 
96   prop_value = jerry_get_property (result2, prop_name);
97   TEST_ASSERT (jerry_get_number_value (prop_value) == TEST_VALUE);
98   jerry_release_value (prop_value);
99 
100   jerry_release_value (prop_name);
101   jerry_release_value (result1);
102   jerry_release_value (result2);
103 
104   jerry_cleanup ();
105 
106   return 0;
107 } /* main */
108