@@ -246,10 +246,6 @@ jerry_cleanup (void)
246246 }
247247 }
248248
249- #if ENABLED (JERRY_MODULE_SYSTEM )
250- ecma_module_cleanup ();
251- #endif /* ENABLED (JERRY_MODULE_SYSTEM) */
252-
253249#if ENABLED (JERRY_BUILTIN_PROMISE )
254250 ecma_free_all_enqueued_jobs ();
255251#endif /* ENABLED (JERRY_BUILTIN_PROMISE) */
@@ -456,6 +452,15 @@ jerry_parse (const jerry_char_t *resource_name_p, /**< resource name (usually a
456452 }
457453#endif /* ENABLED (JERRY_RESOURCE_NAME) */
458454
455+ if ((parse_opts & JERRY_PARSE_MODULE ) != 0 )
456+ {
457+ #if ENABLED (JERRY_MODULE_SYSTEM )
458+ ecma_module_initialize_context (ecma_get_string_from_value (resource_name ));
459+ #else /* !ENABLED (JERRY_MODULE_SYSTEM) */
460+ return jerry_throw (ecma_raise_syntax_error (ECMA_ERR_MSG ("Module system has been disabled." )));
461+ #endif /* !ENABLED (JERRY_MODULE_SYSTEM) */
462+ }
463+
459464 ecma_compiled_code_t * bytecode_data_p = parser_parse_script (NULL ,
460465 0 ,
461466 source_p ,
@@ -465,16 +470,54 @@ jerry_parse (const jerry_char_t *resource_name_p, /**< resource name (usually a
465470
466471 if (JERRY_UNLIKELY (bytecode_data_p == NULL ))
467472 {
473+ #if ENABLED (JERRY_MODULE_SYSTEM )
474+ if ((parse_opts & JERRY_PARSE_MODULE ) != 0 )
475+ {
476+ ecma_module_cleanup_context ();
477+ }
478+ #endif /* ENABLED (JERRY_MODULE_SYSTEM) */
479+
468480 return ecma_create_error_reference_from_context ();
469481 }
470482
483+ #if ENABLED (JERRY_MODULE_SYSTEM )
484+ if ((parse_opts & JERRY_PARSE_MODULE ) != 0 )
485+ {
486+ if (ECMA_IS_VALUE_ERROR (ecma_module_parse_referenced_modules ()))
487+ {
488+ ecma_bytecode_deref (bytecode_data_p );
489+ ecma_module_cleanup_context ();
490+
491+ return ecma_create_error_reference_from_context ();
492+ }
493+
494+ ecma_object_t * obj_p = ecma_create_object (NULL , sizeof (ecma_extended_object_t ), ECMA_OBJECT_TYPE_CLASS );
495+
496+ ecma_extended_object_t * wrapper_p = (ecma_extended_object_t * ) obj_p ;
497+ wrapper_p -> u .class_prop .class_id = LIT_MAGIC_STRING_RUNNABLE_UL ;
498+ wrapper_p -> u .class_prop .extra_info = ECMA_RUNNABLE_FLAGS_MODULE ;
499+
500+ ecma_module_t * root_module_p = JERRY_CONTEXT (module_current_p );
501+ root_module_p -> compiled_code_p = bytecode_data_p ;
502+
503+ ECMA_SET_INTERNAL_VALUE_POINTER (wrapper_p -> u .class_prop .u .value , root_module_p );
504+ JERRY_CONTEXT (module_current_p ) = NULL ;
505+ JERRY_CONTEXT (module_list_p ) = NULL ;
506+
507+ return ecma_make_object_value (obj_p );
508+ }
509+ #endif /* ENABLED (JERRY_MODULE_SYSTEM) */
510+
471511 ecma_object_t * global_object_p = ecma_builtin_get_global ();
472512
473513#if ENABLED (JERRY_BUILTIN_REALMS )
474514 JERRY_ASSERT (global_object_p == (ecma_object_t * ) ecma_op_function_get_realm (bytecode_data_p ));
475515#endif /* ENABLED (JERRY_BUILTIN_REALMS) */
476516
477517 ecma_object_t * lex_env_p = ecma_get_global_environment (global_object_p );
518+
519+ /* TODO(dbatyai): For now Scripts continue to return Function objects due to backwards compatibility. This should be
520+ * changed to also return a Runnable object eventually. */
478521 ecma_object_t * func_obj_p = ecma_op_create_simple_function_object (lex_env_p , bytecode_data_p );
479522 ecma_bytecode_deref (bytecode_data_p );
480523
@@ -588,15 +631,35 @@ jerry_run (const jerry_value_t func_val) /**< function to run */
588631 return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG (wrong_args_msg_p )));
589632 }
590633
591- ecma_object_t * func_obj_p = ecma_get_object_from_value (func_val );
634+ ecma_object_t * obj_p = ecma_get_object_from_value (func_val );
635+
636+ #if ENABLED (JERRY_MODULE_SYSTEM )
637+ if (ecma_object_class_is (obj_p , LIT_MAGIC_STRING_RUNNABLE_UL ))
638+ {
639+ ecma_extended_object_t * wrapper_p = (ecma_extended_object_t * ) obj_p ;
640+ JERRY_ASSERT (wrapper_p -> u .class_prop .extra_info == ECMA_RUNNABLE_FLAGS_MODULE );
641+ ecma_module_t * root_module_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_module_t , wrapper_p -> u .class_prop .u .value );
642+
643+ #if ENABLED (JERRY_BUILTIN_REALMS )
644+ ecma_object_t * global_object_p = (ecma_object_t * ) ecma_op_function_get_realm (root_module_p -> compiled_code_p );
645+ #else /* !ENABLED (JERRY_BUILTIN_REALMS) */
646+ ecma_object_t * global_object_p = ecma_builtin_get_global ();
647+ #endif /* ENABLED (JERRY_BUILTIN_REALMS) */
648+
649+ ecma_create_global_lexical_block (global_object_p );
650+ root_module_p -> scope_p = ecma_get_global_scope (global_object_p );
651+
652+ return jerry_return (vm_run_module (root_module_p ));
653+ }
654+ #endif /* ENABLED (JERRY_MODULE_SYSTEM) */
592655
593- if (ecma_get_object_type (func_obj_p ) != ECMA_OBJECT_TYPE_FUNCTION
594- || ecma_get_object_is_builtin (func_obj_p ))
656+ if (ecma_get_object_type (obj_p ) != ECMA_OBJECT_TYPE_FUNCTION
657+ || ecma_get_object_is_builtin (obj_p ))
595658 {
596659 return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG (wrong_args_msg_p )));
597660 }
598661
599- ecma_extended_object_t * ext_func_p = (ecma_extended_object_t * ) func_obj_p ;
662+ ecma_extended_object_t * ext_func_p = (ecma_extended_object_t * ) obj_p ;
600663
601664 const ecma_compiled_code_t * bytecode_data_p = ecma_op_function_get_compiled_code (ext_func_p );
602665
0 commit comments