Skip to content

Commit

Permalink
Fix empty TypedArray creation
Browse files Browse the repository at this point in the history
  • Loading branch information
kateinoigakukun committed Dec 8, 2024
1 parent b7f361c commit 8bf446b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
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]())
}
}

0 comments on commit 8bf446b

Please sign in to comment.