Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty TypedArray creation #278

Merged
merged 1 commit into from
Dec 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,15 @@ export class SwiftRuntime {
) => {
const ArrayType: TypedArray =
this.memory.getObject(constructor_ref);
if (length == 0) {
// The elementsPtr can be unaligned in Swift's Array
// implementation when the array is empty. However,
// TypedArray requires the pointer to be aligned.
// So, we need to create a new empty array without
// using the elementsPtr.
// See https://github.com/swiftwasm/swift/issues/5599
return this.memory.retain(new ArrayType());
}
const array = new ArrayType(
this.memory.rawMemory.buffer,
elementsPtr,
Expand Down
9 changes: 9 additions & 0 deletions Sources/JavaScriptKit/Runtime/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Sources/JavaScriptKit/Runtime/index.mjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions Tests/JavaScriptKitTests/JSTypedArrayTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import XCTest
import JavaScriptKit

final class JSTypedArrayTests: XCTestCase {
func testEmptyArray() {
_ = JSTypedArray<Int>([])
_ = JSTypedArray<UInt>([])
_ = JSTypedArray<Int8>([Int8]())
_ = JSTypedArray<UInt8>([UInt8]())
_ = JSUInt8ClampedArray([UInt8]())
_ = JSTypedArray<Int16>([Int16]())
_ = JSTypedArray<UInt16>([UInt16]())
_ = JSTypedArray<Int32>([Int32]())
_ = JSTypedArray<UInt32>([UInt32]())
_ = JSTypedArray<Float32>([Float32]())
_ = JSTypedArray<Float64>([Float64]())
}
}
Loading