@@ -38,62 +38,6 @@ static bool exit_condition = true;
3838
3939/* -- Methods -- */
4040
41- // void application::print(void *v)
42- // {
43- // /* TODO: Delete this, implement command line arguments parser in js */
44- // if (v == NULL)
45- // {
46- // std::cout << "null" << std::endl;
47- // }
48- // else
49- // {
50- // if (metacall_value_id(v) == METACALL_THROWABLE)
51- // {
52- // std::cout << "TODO: Throwable" << std::endl;
53- // }
54- // else
55- // {
56- // size_t size = 0;
57- // struct metacall_allocator_std_type std_ctx = { &std::malloc, &std::realloc, &std::free };
58- // void *allocator = metacall_allocator_create(METACALL_ALLOCATOR_STD, (void *)&std_ctx);
59- // char *value_str = metacall_serialize(metacall_serial(), v, &size, allocator);
60-
61- // std::cout << value_str << std::endl;
62-
63- // metacall_allocator_free(allocator, value_str);
64- // }
65-
66- // metacall_value_destroy(v);
67- // }
68- // }
69-
70- // void application::invoke(const char *func, void *args[], size_t size)
71- // {
72- // /* TODO: Delete this, implement command line arguments parser in js */
73- // void *ret = metacallhv_s(plugin_cli_handle, func, args, size);
74-
75- // if (metacall_value_id(ret) == METACALL_INT)
76- // {
77- // int result = metacall_value_to_int(ret);
78-
79- // if (result != 0)
80- // {
81- // std::cout << "Failed to execute '" << func << "' command, return code: " << result;
82- // }
83-
84- // metacall_value_destroy(ret);
85- // }
86- // else
87- // {
88- // print(ret);
89- // }
90-
91- // for (size_t it = 0; it < size; ++it)
92- // {
93- // metacall_value_destroy(args[it]);
94- // }
95- // }
96-
9741// void application::parameter_iterator::operator()(const char *parameter)
9842// {
9943// // TODO: Implement a new plugin for parsing command line options
@@ -177,25 +121,6 @@ application::application(int argc, char *argv[]) :
177121 /* Print MetaCall information */
178122 metacall_print_info ();
179123
180- // TODO: Implement a new plugin for parsing command line options
181- // TODO: Implement a new plugin for parsing command line options
182- // TODO: Implement a new plugin for parsing command line options
183-
184- // /* TODO: This has been updated, review it: */
185- // /* Parse program arguments if any (e.g metacall (0) a.py (1) b.js (2) c.rb (3)) */
186- // if (argc > 1)
187- // {
188- // parameter_iterator param_it(*this);
189-
190- // /* TODO: This has been refactored in order to pass the arguments to the runtimes */
191- // /* Using argv + 2 by now, but this should be deleted in a near future or review the implementation */
192-
193- // /* Parse program parameters */
194- // std::for_each(&argv[1], argv + /*argc*/ 2, param_it);
195-
196- // return;
197- // }
198-
199124 /* Initialize REPL plugins */
200125 if (!load_path (" repl" , &plugin_repl_handle))
201126 {
@@ -205,6 +130,34 @@ application::application(int argc, char *argv[]) :
205130 return ;
206131 }
207132
133+ if (argc == 1 )
134+ {
135+ void *ret = metacallhv_s (plugin_repl_handle, " initialize" , metacall_null_args, 0 );
136+
137+ check_for_exception (ret);
138+ }
139+ else
140+ {
141+ void *arguments_parse_func = metacall_handle_function (plugin_repl_handle, " arguments_parse" );
142+
143+ if (arguments_parse_func == NULL )
144+ {
145+ /* Use fallback parser, it can execute files but does not support command line arguments as options (i.e: -h, --help) */
146+ /* Parse program arguments if any (e.g metacall (0) a.py (1) b.js (2) c.rb (3)) */
147+
148+ // TODO
149+ exit_condition = true ;
150+ return ;
151+ }
152+ else
153+ {
154+ /* TODO: Implement a new plugin for parsing command line options */
155+ std::cout << " TODO: CLI Arguments Parser Plugin is not implemented yet" << std::endl;
156+ exit_condition = true ;
157+ return ;
158+ }
159+ }
160+
208161 /* Initialize CLI plugins */
209162 if (load_path (" cli" , &plugin_cli_handle))
210163 {
@@ -383,8 +336,7 @@ void application::run()
383336 /* Invoke next iteration of the REPL */
384337 void *ret = metacallfv_s (metacall_value_to_function (results[1 ]), args, 2 );
385338
386- /* TODO: Do something with ret? */
387- metacall_value_destroy (ret);
339+ check_for_exception (ret);
388340
389341 metacall_value_destroy (args[0 ]);
390342 metacall_value_destroy (args[1 ]);
@@ -398,7 +350,7 @@ void application::run()
398350 {
399351 void *ret = metacallhv_s (plugin_repl_handle, " close" , metacall_null_args, 0 );
400352
401- metacall_value_destroy (ret);
353+ check_for_exception (ret);
402354 }
403355}
404356
@@ -418,3 +370,16 @@ void *application::execute(void *tokens)
418370 return metacallhv_s (plugin_cli_handle, metacall_value_to_string (key), &tokens_array[1 ], size - 1 );
419371 }
420372}
373+
374+ void application::check_for_exception (void *v)
375+ {
376+ struct metacall_exception_type ex;
377+
378+ if (metacall_error_from_value (v, &ex) == 0 )
379+ {
380+ std::cout << " Exception: " << ex.message << std::endl
381+ << ex.stacktrace << std::endl;
382+ }
383+
384+ metacall_value_destroy (v);
385+ }
0 commit comments