• 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 <stdlib.h>
17 #include <string.h>
18 
19 #include "jerryscript.h"
20 #include "jerryscript-port.h"
21 
22 /**
23  * Maximum size of source code / snapshots buffer
24  */
25 #define JERRY_BUFFER_SIZE (1048576)
26 
27 /**
28  * Standalone Jerry exit codes
29  */
30 #define JERRY_STANDALONE_EXIT_CODE_OK   (0)
31 #define JERRY_STANDALONE_EXIT_CODE_FAIL (1)
32 
33 static uint8_t buffer[ JERRY_BUFFER_SIZE ];
34 
35 static const uint8_t *
read_file(const char * file_name,size_t * out_size_p)36 read_file (const char *file_name,
37            size_t *out_size_p)
38 {
39   FILE *file = fopen (file_name, "rb");
40   if (file == NULL)
41   {
42     jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to open file: %s\n", file_name);
43     return NULL;
44   }
45 
46   size_t bytes_read = fread (buffer, 1u, sizeof (buffer), file);
47   if (!bytes_read)
48   {
49     jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to read file: %s\n", file_name);
50     fclose (file);
51     return NULL;
52   }
53 
54   fclose (file);
55 
56   *out_size_p = bytes_read;
57   return (const uint8_t *) buffer;
58 } /* read_file */
59 
60 static void
print_help(char * name)61 print_help (char *name)
62 {
63   printf ("Usage: %s [OPTION]... [FILE]...\n"
64           "\n"
65           "Options:\n"
66           "  -h, --help\n"
67           "\n",
68           name);
69 } /* print_help */
70 
71 int
main(int argc,char ** argv)72 main (int argc,
73       char **argv)
74 {
75   union
76   {
77     double d;
78     unsigned u;
79   } now = { .d = jerry_port_get_current_time () };
80   srand (now.u);
81   if (argc <= 1 || (argc == 2 && (!strcmp ("-h", argv[1]) || !strcmp ("--help", argv[1]))))
82   {
83     print_help (argv[0]);
84     return JERRY_STANDALONE_EXIT_CODE_OK;
85   }
86 
87   jerry_init (JERRY_INIT_EMPTY);
88   jerry_value_t ret_value = jerry_create_undefined ();
89 
90   for (int i = 1; i < argc; i++)
91   {
92     const char *file_name = argv[i];
93     size_t source_size;
94 
95     const jerry_char_t *source_p = read_file (file_name, &source_size);
96 
97     if (source_p == NULL)
98     {
99       ret_value = jerry_create_error (JERRY_ERROR_COMMON, (jerry_char_t *) "");
100       break;
101     }
102     else
103     {
104       ret_value = jerry_parse (NULL, 0, source_p, source_size, JERRY_PARSE_NO_OPTS);
105 
106       if (!jerry_value_is_error (ret_value))
107       {
108         jerry_value_t func_val = ret_value;
109         ret_value = jerry_run (func_val);
110         jerry_release_value (func_val);
111       }
112     }
113 
114     if (jerry_value_is_error (ret_value))
115     {
116       break;
117     }
118 
119     jerry_release_value (ret_value);
120     ret_value = jerry_create_undefined ();
121   }
122 
123   int ret_code = JERRY_STANDALONE_EXIT_CODE_OK;
124 
125   if (jerry_value_is_error (ret_value))
126   {
127     jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Unhandled exception: Script Error!\n");
128     ret_code = JERRY_STANDALONE_EXIT_CODE_FAIL;
129   }
130 
131   jerry_release_value (ret_value);
132   jerry_cleanup ();
133 
134   return ret_code;
135 } /* main */
136