From 9b7fda00dfbedc341ac64dac8bbca109f57157d6 Mon Sep 17 00:00:00 2001 From: Yuta Saito Date: Thu, 28 Nov 2024 16:21:13 +0900 Subject: [PATCH] Build fix for wasm32-unknown-wasi --- .../FundamentalObjects/JSObject.swift | 2 ++ Sources/JavaScriptKit/ThreadLocal.swift | 4 ++- .../JavaScriptKitTests/ThreadLocalTests.swift | 29 +++++++++---------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/Sources/JavaScriptKit/FundamentalObjects/JSObject.swift b/Sources/JavaScriptKit/FundamentalObjects/JSObject.swift index 9fd1b124..c5eed713 100644 --- a/Sources/JavaScriptKit/FundamentalObjects/JSObject.swift +++ b/Sources/JavaScriptKit/FundamentalObjects/JSObject.swift @@ -36,7 +36,9 @@ public class JSObject: Equatable { @_spi(JSObject_id) public init(id: JavaScriptObjectRef) { self.id = id +#if compiler(>=6.1) && _runtime(_multithreaded) self.ownerThread = pthread_self() +#endif } /// Asserts that the object is being accessed from the owner thread. diff --git a/Sources/JavaScriptKit/ThreadLocal.swift b/Sources/JavaScriptKit/ThreadLocal.swift index 146c0c06..0ad0188b 100644 --- a/Sources/JavaScriptKit/ThreadLocal.swift +++ b/Sources/JavaScriptKit/ThreadLocal.swift @@ -1,5 +1,7 @@ +#if os(WASI) #if canImport(wasi_pthread) import wasi_pthread +#endif #elseif canImport(Darwin) import Darwin #elseif canImport(Glibc) @@ -77,7 +79,7 @@ final class ThreadLocal: Sendable { #else // Fallback implementation for platforms that don't support pthread - var wrappedValue: Value? + nonisolated(unsafe) var wrappedValue: Value? init() where Value: AnyObject { wrappedValue = nil diff --git a/Tests/JavaScriptKitTests/ThreadLocalTests.swift b/Tests/JavaScriptKitTests/ThreadLocalTests.swift index 761e82b5..55fcdadb 100644 --- a/Tests/JavaScriptKitTests/ThreadLocalTests.swift +++ b/Tests/JavaScriptKitTests/ThreadLocalTests.swift @@ -9,19 +9,17 @@ final class ThreadLocalTests: XCTestCase { func testLeak() throws { struct Check { - @ThreadLocal - static var value: MyHeapObject? - @ThreadLocal(boxing: ()) - static var value2: MyStruct? + static let value = ThreadLocal() + static let value2 = ThreadLocal(boxing: ()) } weak var weakObject: MyHeapObject? do { let object = MyHeapObject() weakObject = object - Check.value = object - XCTAssertNotNil(Check.value) - XCTAssertTrue(Check.value === object) - Check.value = nil + Check.value.wrappedValue = object + XCTAssertNotNil(Check.value.wrappedValue) + XCTAssertTrue(Check.value.wrappedValue === object) + Check.value.wrappedValue = nil } XCTAssertNil(weakObject) @@ -29,21 +27,20 @@ final class ThreadLocalTests: XCTestCase { do { let object = MyHeapObject() weakObject2 = object - Check.value2 = MyStruct(object: object) - XCTAssertNotNil(Check.value2) - XCTAssertTrue(Check.value2!.object === object) - Check.value2 = nil + Check.value2.wrappedValue = MyStruct(object: object) + XCTAssertNotNil(Check.value2.wrappedValue) + XCTAssertTrue(Check.value2.wrappedValue!.object === object) + Check.value2.wrappedValue = nil } XCTAssertNil(weakObject2) } func testLazyThreadLocal() throws { struct Check { - @LazyThreadLocal(initialize: { MyHeapObject() }) - static var value: MyHeapObject + static let value = LazyThreadLocal(initialize: { MyHeapObject() }) } - let object1 = Check.value - let object2 = Check.value + let object1 = Check.value.wrappedValue + let object2 = Check.value.wrappedValue XCTAssertTrue(object1 === object2) } }