• 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 "jerryscript.h"
17 #include "test-common.h"
18 
19 static const char instanceof_source[] = "var x = function(o, c) {return (o instanceof c);}; x";
20 
21 static jerry_value_t
external_function(const jerry_value_t function_obj,const jerry_value_t this_arg,const jerry_value_t args_p[],const jerry_size_t args_count)22 external_function (const jerry_value_t function_obj,
23                    const jerry_value_t this_arg,
24                    const jerry_value_t args_p[],
25                    const jerry_size_t args_count)
26 {
27   (void) function_obj;
28   (void) this_arg;
29   (void) args_p;
30   (void) args_count;
31 
32   return jerry_create_undefined ();
33 } /* external_function */
34 
35 static void
test_instanceof(jerry_value_t instanceof,jerry_value_t constructor)36 test_instanceof (jerry_value_t instanceof,
37                  jerry_value_t constructor)
38 {
39   jerry_value_t instance = jerry_construct_object (constructor, NULL, 0);
40   jerry_value_t args[2] =
41   {
42     instance, constructor
43   };
44 
45   jerry_value_t undefined = jerry_create_undefined ();
46   jerry_value_t result = jerry_call_function (instanceof, undefined, args, 2);
47   jerry_release_value (undefined);
48 
49   TEST_ASSERT (!jerry_value_is_error (result));
50   TEST_ASSERT (jerry_value_is_boolean (result));
51 
52   TEST_ASSERT (jerry_get_boolean_value (result));
53 
54   jerry_release_value (instance);
55   jerry_release_value (result);
56 } /* test_instanceof */
57 
58 int
main(void)59 main (void)
60 {
61   jerry_init (JERRY_INIT_EMPTY);
62 
63   jerry_value_t instanceof = jerry_eval ((jerry_char_t *) instanceof_source, sizeof (instanceof_source) - 1, true);
64 
65   /* Test for a native-backed function. */
66   jerry_value_t constructor = jerry_create_external_function (external_function);
67 
68   test_instanceof (instanceof, constructor);
69   jerry_release_value (constructor);
70 
71   /* Test for a JS constructor. */
72   jerry_value_t global = jerry_get_global_object ();
73   jerry_value_t object_name = jerry_create_string ((jerry_char_t *) "Object");
74   constructor = jerry_get_property (global, object_name);
75   jerry_release_value (object_name);
76   jerry_release_value (global);
77 
78   test_instanceof (instanceof, constructor);
79   jerry_release_value (constructor);
80 
81   jerry_release_value (instanceof);
82 
83   jerry_cleanup ();
84 
85   return 0;
86 } /* main */
87