Skip to content

Commit

Permalink
Build fix for wasm32-unknown-wasi
Browse files Browse the repository at this point in the history
  • Loading branch information
kateinoigakukun committed Nov 28, 2024
1 parent d4e0ee8 commit 9b7fda0
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
2 changes: 2 additions & 0 deletions Sources/JavaScriptKit/FundamentalObjects/JSObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion Sources/JavaScriptKit/ThreadLocal.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#if os(WASI)
#if canImport(wasi_pthread)
import wasi_pthread
#endif
#elseif canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
Expand Down Expand Up @@ -77,7 +79,7 @@ final class ThreadLocal<Value>: 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
Expand Down
29 changes: 13 additions & 16 deletions Tests/JavaScriptKitTests/ThreadLocalTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,38 @@ final class ThreadLocalTests: XCTestCase {

func testLeak() throws {
struct Check {
@ThreadLocal
static var value: MyHeapObject?
@ThreadLocal(boxing: ())
static var value2: MyStruct?
static let value = ThreadLocal<MyHeapObject>()
static let value2 = ThreadLocal<MyStruct>(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)

weak var weakObject2: MyHeapObject?
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)
}
}

0 comments on commit 9b7fda0

Please sign in to comment.