diff --git a/jerry-core/ecma/base/ecma-helpers.c b/jerry-core/ecma/base/ecma-helpers.c index 72b5277ed..fb8fae130 100644 --- a/jerry-core/ecma/base/ecma-helpers.c +++ b/jerry-core/ecma/base/ecma-helpers.c @@ -633,7 +633,6 @@ ecma_create_named_reference_property (ecma_object_t *object_p, /**< object */ ecma_property_t *property_p) /**< referenced property */ { JERRY_ASSERT (object_p != NULL && name_p != NULL && property_p != NULL); - JERRY_ASSERT (*property_p & ECMA_PROPERTY_FLAG_DATA); JERRY_ASSERT (ecma_find_named_property (object_p, name_p) == NULL); JERRY_ASSERT ((ecma_is_lexical_environment (object_p) && ecma_get_lex_env_type (object_p) == ECMA_LEXICAL_ENVIRONMENT_CLASS @@ -642,26 +641,33 @@ ecma_create_named_reference_property (ecma_object_t *object_p, /**< object */ uint8_t type_and_flags = ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE; - ecma_property_value_t *value_p = ECMA_PROPERTY_VALUE_PTR (property_p); + ecma_property_value_t *referenced_value_p = ECMA_PROPERTY_VALUE_PTR (property_p); + ecma_property_value_t value; - jmem_cpointer_tag_t offset = (jmem_cpointer_tag_t) (((uintptr_t) property_p) & 0x1); + if (*property_p & ECMA_PROPERTY_FLAG_DATA) + { + jmem_cpointer_tag_t offset = (jmem_cpointer_tag_t) (((uintptr_t) property_p) & 0x1); #if JERRY_CPOINTER_32_BIT - if (offset != 0) - { - --value_p; - } + if (offset != 0) + { + --referenced_value_p; + } #else /* !JERRY_CPOINTER_32_BIT */ - if (offset == 0) - { - ++value_p; - } + if (offset == 0) + { + ++referenced_value_p; + } #endif /* JERRY_CPOINTER_32_BIT */ - JERRY_ASSERT ((((uintptr_t) value_p) & (((uintptr_t) 1 << JMEM_ALIGNMENT_LOG) - 1)) == 0); + JERRY_ASSERT ((((uintptr_t) referenced_value_p) & (((uintptr_t) 1 << JMEM_ALIGNMENT_LOG) - 1)) == 0); - ecma_property_value_t value; - ECMA_SET_NON_NULL_POINTER_TAG (value.value, value_p, offset); + ECMA_SET_NON_NULL_POINTER_TAG (value.value, referenced_value_p, offset); + } + else + { + value.value = referenced_value_p->value; + } ecma_create_property (object_p, name_p, type_and_flags, value, NULL); } /* ecma_create_named_reference_property */ diff --git a/jerry-core/ecma/base/ecma-module.c b/jerry-core/ecma/base/ecma-module.c index 3a7a1c879..966586da5 100644 --- a/jerry-core/ecma/base/ecma-module.c +++ b/jerry-core/ecma/base/ecma-module.c @@ -367,7 +367,7 @@ ecma_module_resolve_export (ecma_module_t *const module_p, /**< base module */ } else { - ret_value = ecma_raise_syntax_error (ECMA_ERR_MSG ("Unexported or circular import request")); + ret_value = ecma_raise_syntax_error (ECMA_ERR_MSG ("Export not found")); } return ret_value; @@ -454,13 +454,14 @@ ecma_module_evaluate (ecma_module_t *module_p) /**< module */ */ static ecma_value_t ecma_module_namespace_object_add_export_if_needed (ecma_module_t *module_p, /**< module */ - ecma_string_t *export_name_p) /**< export name */ + ecma_string_t *export_name_p, /**< export name */ + bool allow_default) /**< allow default export */ { JERRY_ASSERT (module_p->namespace_object_p != NULL); ecma_value_t result = ECMA_VALUE_EMPTY; /* Default exports should not be added to the namespace object. */ - if (ecma_compare_ecma_string_to_magic_id (export_name_p, LIT_MAGIC_STRING_DEFAULT) + if ((!allow_default && ecma_compare_ecma_string_to_magic_id (export_name_p, LIT_MAGIC_STRING_DEFAULT)) || ecma_find_named_property (module_p->namespace_object_p, export_name_p) != NULL) { /* This export name has already been handled. */ @@ -524,7 +525,10 @@ ecma_module_create_namespace_object (ecma_module_t *module_p) /**< module */ ecma_deref_object (namespace_object_p); ecma_module_resolve_stack_push (&stack_p, module_p, ecma_get_magic_string (LIT_MAGIC_STRING_ASTERIX_CHAR)); - while (stack_p != NULL) + + bool allow_default = true; + + do { ecma_module_resolve_stack_t *current_frame_p = stack_p; ecma_module_t *current_module_p = current_frame_p->module_p; @@ -537,6 +541,7 @@ ecma_module_create_namespace_object (ecma_module_t *module_p) /**< module */ ecma_get_magic_string (LIT_MAGIC_STRING_ASTERIX_CHAR))) { /* Circular import. */ + JERRY_ASSERT (!allow_default); continue; } @@ -548,7 +553,8 @@ ecma_module_create_namespace_object (ecma_module_t *module_p) /**< module */ do { result = ecma_module_namespace_object_add_export_if_needed (module_p, - export_names_p->imex_name_p); + export_names_p->imex_name_p, + allow_default); export_names_p = export_names_p->next_p; } while (export_names_p != NULL && ecma_is_value_empty (result)); @@ -565,7 +571,8 @@ ecma_module_create_namespace_object (ecma_module_t *module_p) /**< module */ do { result = ecma_module_namespace_object_add_export_if_needed (module_p, - export_names_p->imex_name_p); + export_names_p->imex_name_p, + allow_default); export_names_p = export_names_p->next_p; } while (export_names_p != NULL && ecma_is_value_empty (result)); @@ -573,6 +580,8 @@ ecma_module_create_namespace_object (ecma_module_t *module_p) /**< module */ indirect_export_p = indirect_export_p->next_p; } + allow_default = false; + /* 15.2.1.16.2 / 7 */ ecma_module_node_t *star_export_p = current_module_p->star_exports_p; while (star_export_p != NULL && ecma_is_value_empty (result)) @@ -592,6 +601,7 @@ ecma_module_create_namespace_object (ecma_module_t *module_p) /**< module */ break; } } + while (stack_p != NULL); /* Clean up. */ ecma_module_resolve_set_cleanup (resolve_set_p); diff --git a/tests/jerry/es.next/module-namespace-01.mjs b/tests/jerry/es.next/module-namespace-01.mjs new file mode 100644 index 000000000..c6636eda8 --- /dev/null +++ b/tests/jerry/es.next/module-namespace-01.mjs @@ -0,0 +1,30 @@ +/* Copyright JS Foundation and other contributors, http://js.foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import * as o from "./module-namespace-02.mjs" +import def from "./module-namespace-02.mjs" + +assert(o.default === 8.5) +assert(def === 8.5) + +var val = {} + +assert(o.a === 2.5); +assert(o.b === undefined); +assert(o.c(val) === "c") +assert(o.d === "d") + +assert(o.default === val) +assert(def === val) diff --git a/tests/jerry/es.next/module-namespace-02.mjs b/tests/jerry/es.next/module-namespace-02.mjs new file mode 100644 index 000000000..a8e960a6c --- /dev/null +++ b/tests/jerry/es.next/module-namespace-02.mjs @@ -0,0 +1,28 @@ +/* Copyright JS Foundation and other contributors, http://js.foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import def, {a} from "./module-namespace-03.mjs" +export default def +export {a} + +export * from "./module-namespace-03.mjs" +export * from "./module-namespace-04.mjs" + +import * as o1 from "./module-namespace-03.mjs" +import * as o2 from "./module-namespace-04.mjs" + +assert(def === 8.5) +assert(o1.default === 8.5) +assert(o2.default === undefined) diff --git a/tests/jerry/es.next/module-namespace-03.mjs b/tests/jerry/es.next/module-namespace-03.mjs new file mode 100644 index 000000000..59a4ef558 --- /dev/null +++ b/tests/jerry/es.next/module-namespace-03.mjs @@ -0,0 +1,21 @@ +/* Copyright JS Foundation and other contributors, http://js.foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export var a = 2.5 +export var b = 4 +export var c = function(v) { def = v; return "c" } + +var def = 8.5 +export default def diff --git a/tests/jerry/es.next/module-namespace-04.mjs b/tests/jerry/es.next/module-namespace-04.mjs new file mode 100644 index 000000000..d3380d4eb --- /dev/null +++ b/tests/jerry/es.next/module-namespace-04.mjs @@ -0,0 +1,18 @@ +/* Copyright JS Foundation and other contributors, http://js.foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export var a = 5 +export var b = 6 +export var d = "d"