11
11
#include <dfs_file.h>
12
12
#include <dfs_fs.h>
13
13
14
+ #if WASM_ENABLE_LIBC_WASI != 0
15
+ #include "../common/libc_wasi.c"
16
+ #endif
17
+
14
18
#ifdef WAMR_ENABLE_RTT_EXPORT
15
19
16
20
#ifdef WAMR_RTT_EXPORT_VPRINTF
@@ -160,6 +164,15 @@ static NativeSymbol native_export_symbols[] = {
160
164
161
165
#endif /* WAMR_ENABLE_RTT_EXPORT */
162
166
167
+ static void *
168
+ app_instance_func (wasm_module_inst_t module_inst , const char * func_name ,
169
+ int app_argc , char * * app_argv )
170
+ {
171
+ wasm_application_execute_func (module_inst , func_name , app_argc - 1 ,
172
+ app_argv + 1 );
173
+ return wasm_runtime_get_exception (module_inst );
174
+ }
175
+
163
176
/**
164
177
* run WASM module instance.
165
178
* @param module_inst instance of wasm module
@@ -170,12 +183,8 @@ static NativeSymbol native_export_symbols[] = {
170
183
static void *
171
184
app_instance_main (wasm_module_inst_t module_inst , int app_argc , char * * app_argv )
172
185
{
173
- const char * exception ;
174
-
175
186
wasm_application_execute_main (module_inst , app_argc , app_argv );
176
- if ((exception = wasm_runtime_get_exception (module_inst )))
177
- rt_kprintf ("%s\n" , exception );
178
- return NULL ;
187
+ return wasm_runtime_get_exception (module_inst );
179
188
}
180
189
181
190
rt_uint8_t *
@@ -214,28 +223,36 @@ void
214
223
iwasm_help (void )
215
224
{
216
225
#ifdef WAMR_ENABLE_IWASM_PARAMS
217
- rt_kputs ("wrong input: iwasm [-t] [-m] [-s] <*.wasm> <wasm_args ...>\n"
218
- " iwasm [-h]\n" );
219
- rt_kputs ("\t -h: show this tips.\n" );
220
- rt_kputs ("\t -t: show time taking to run this app.\n" );
221
- rt_kputs ("\t -m: show memory taking to run this app\n" );
222
- rt_kputs ("\t wasm file name and exec params must behind of all vm-param\n" );
226
+ rt_kputs ("Usage: iwasm [-options] wasm_file [args...]\n" );
227
+ rt_kputs ("options:\n" );
228
+ rt_kputs (" -t Show time taking to run this app.\n" );
229
+ rt_kputs (" -m Show memory taking to run this app\n" );
230
+ rt_kputs (" -f|--function name Specify a function name of the module "
231
+ "to run rather than main\n" );
232
+ rt_kputs (" --max-threads=n Set maximum thread number per "
233
+ "cluster, default is 4\n" );
223
234
#else
224
- rt_kputs ("wrong input : iwasm <*.wasm> <wasm_args ...> \n" );
235
+ rt_kputs ("Usage : iwasm wasm_file [args ...] \n" );
225
236
#endif /* WAMR_ENABLE_PARAMS */
226
237
}
227
238
228
239
int
229
240
iwasm (int argc , char * * argv )
230
241
{
242
+ const char * exception = NULL ;
243
+ const char * func_name = NULL ;
231
244
rt_uint8_t * wasm_file_buf = NULL ;
232
245
rt_uint32_t wasm_file_size ;
233
- rt_uint32_t stack_size = 4 * 1024 , heap_size = 4 * 1024 ;
246
+ rt_uint32_t stack_size = 64 * 1024 , heap_size = 256 * 1024 ;
234
247
wasm_module_t wasm_module = NULL ;
235
248
wasm_module_inst_t wasm_module_inst = NULL ;
236
249
RuntimeInitArgs init_args ;
237
250
static char error_buf [128 ] = { 0 };
238
251
/* avoid stack overflow */
252
+ #if WASM_ENABLE_LIBC_WASI != 0
253
+ libc_wasi_parse_context_t wasi_parse_ctx ;
254
+ memset (& wasi_parse_ctx , 0 , sizeof (wasi_parse_ctx ));
255
+ #endif
239
256
240
257
#ifdef WAMR_ENABLE_IWASM_PARAMS
241
258
int i_arg_begin ;
@@ -260,6 +277,17 @@ iwasm(int argc, char **argv)
260
277
iwasm_help ();
261
278
return 0 ;
262
279
}
280
+ else if (argv [i_arg_begin ][1 ] == 'f' ) {
281
+ func_name = argv [++ i_arg_begin ];
282
+ }
283
+ else if (!strncmp (argv [i_arg_begin ], "--max-threads=" , 14 )) {
284
+ if (argv [0 ][14 ] != '\0' )
285
+ wasm_runtime_set_max_thread_num (atoi (argv [0 ] + 14 ));
286
+ else {
287
+ iwasm_help ();
288
+ return 0 ;
289
+ }
290
+ }
263
291
else if (argv [i_arg_begin ][1 ] == 0x00 ) {
264
292
continue ;
265
293
}
@@ -303,8 +331,8 @@ iwasm(int argc, char **argv)
303
331
rt_thread_t tid ;
304
332
if (show_stack ) {
305
333
tid = rt_thread_self ();
306
- printf ("thread stack addr: %p, size: %u, sp: %p\n" , tid -> stack_addr ,
307
- tid -> stack_size , tid -> sp );
334
+ rt_kprintf ("thread stack addr: %p, size: %u, sp: %p\n" , tid -> stack_addr ,
335
+ tid -> stack_size , tid -> sp );
308
336
}
309
337
#endif /* WAMR_ENABLE_PARAMS */
310
338
@@ -326,6 +354,10 @@ iwasm(int argc, char **argv)
326
354
rt_kprintf ("%s\n" , error_buf );
327
355
goto fail2 ;
328
356
}
357
+ #if WASM_ENABLE_LIBC_WASI != 0
358
+ libc_wasi_init (wasm_module , argc , argv , & wasi_parse_ctx );
359
+ #endif
360
+
329
361
rt_memset (error_buf , 0x00 , sizeof (error_buf ));
330
362
wasm_module_inst = wasm_runtime_instantiate (
331
363
wasm_module , stack_size , heap_size , error_buf , sizeof (error_buf ));
@@ -341,13 +373,31 @@ iwasm(int argc, char **argv)
341
373
}
342
374
#endif /* WAMR_ENABLE_PARAMS */
343
375
344
- app_instance_main (wasm_module_inst , argc - i_arg_begin , & argv [i_arg_begin ]);
376
+ if (func_name ) {
377
+ exception = app_instance_func (wasm_module_inst , func_name ,
378
+ argc - i_arg_begin , & argv [i_arg_begin ]);
379
+ }
380
+ else {
381
+ exception = app_instance_main (wasm_module_inst , argc - i_arg_begin ,
382
+ & argv [i_arg_begin ]);
383
+ rt_kprintf ("finshed run app_instance_main\n" );
384
+ }
385
+
386
+ if (exception )
387
+ rt_kprintf ("%s\n" , exception );
388
+
389
+ #if WASM_ENABLE_LIBC_WASI != 0
390
+ if (!exception ) {
391
+ /* propagate wasi exit code. */
392
+ wasm_runtime_get_wasi_exit_code (wasm_module_inst );
393
+ }
394
+ #endif
345
395
346
396
#ifdef WAMR_ENABLE_IWASM_PARAMS
347
397
if (show_time_exec ) {
348
398
ticks_exec = rt_tick_get () - ticks_exec ;
349
- printf ("[iwasm] execute ticks took: %u [ticks/s = %u]\n" , ticks_exec ,
350
- RT_TICK_PER_SECOND );
399
+ rt_kprintf ("[iwasm] execute ticks took: %u [ticks/s = %u]\n" ,
400
+ ticks_exec , RT_TICK_PER_SECOND );
351
401
}
352
402
#if defined(RT_USING_HEAP ) && defined(RT_USING_MEMHEAP_AS_HEAP )
353
403
if (show_mem ) {
@@ -361,8 +411,8 @@ iwasm(int argc, char **argv)
361
411
}
362
412
#endif
363
413
if (show_stack ) {
364
- printf ("[iwasm] thread stack addr: %p, size: %u, sp: %p\n" ,
365
- tid -> stack_addr , tid -> stack_size , tid -> sp );
414
+ rt_kprintf ("[iwasm] thread stack addr: %p, size: %u, sp: %p\n" ,
415
+ tid -> stack_addr , tid -> stack_size , tid -> sp );
366
416
}
367
417
368
418
#endif /* WAMR_ENABLE_PARAMS */
0 commit comments