diff --git a/kotlin-native/backend.native/tests/build.gradle b/kotlin-native/backend.native/tests/build.gradle index de3ba9ae1e4..e15d7ed564c 100644 --- a/kotlin-native/backend.native/tests/build.gradle +++ b/kotlin-native/backend.native/tests/build.gradle @@ -2865,6 +2865,10 @@ task initializers7(type: KonanLocalTest) { source = "runtime/basic/initializers7.kt" } +task initializers8(type: KonanLocalTest) { + source = "runtime/basic/initializers8.kt" +} + task expression_as_statement(type: KonanLocalTest) { expectedFail = (project.testTarget == 'wasm32') // uses exceptions. goldValue = "Ok\n" diff --git a/kotlin-native/backend.native/tests/runtime/basic/initializers8.kt b/kotlin-native/backend.native/tests/runtime/basic/initializers8.kt new file mode 100644 index 00000000000..d6d77a681b0 --- /dev/null +++ b/kotlin-native/backend.native/tests/runtime/basic/initializers8.kt @@ -0,0 +1,14 @@ +/* + * Copyright 2010-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license + * that can be found in the LICENSE file. + */ + +package runtime.basic.initializers8 + +import kotlin.test.* + +var globalString = "abc" + +@Test fun runTest() { + assertEquals("abc", globalString) +} diff --git a/kotlin-native/runtime/src/mm/cpp/Memory.cpp b/kotlin-native/runtime/src/mm/cpp/Memory.cpp index 7e43061f1ef..eb9f424fe57 100644 --- a/kotlin-native/runtime/src/mm/cpp/Memory.cpp +++ b/kotlin-native/runtime/src/mm/cpp/Memory.cpp @@ -144,7 +144,10 @@ extern "C" ALWAYS_INLINE OBJ_GETTER(InitSingleton, ObjHeader** location, const T extern "C" RUNTIME_NOTHROW void InitAndRegisterGlobal(ObjHeader** location, const ObjHeader* initialValue) { auto* threadData = mm::ThreadRegistry::Instance().CurrentThreadData(); mm::GlobalsRegistry::Instance().RegisterStorageForGlobal(threadData, location); - mm::SetHeapRef(location, const_cast(initialValue)); + // Null `initialValue` means that the appropriate value was already set by static initialization. + if (initialValue != nullptr) { + mm::SetHeapRef(location, const_cast(initialValue)); + } } extern "C" const MemoryModel CurrentMemoryModel = MemoryModel::kExperimental;