Skip to content

Commit d4f24ae

Browse files
Merge pull request #278 from swiftwasm/katei/fix-empty-typed-array
Fix empty TypedArray creation
2 parents f551225 + 8bf446b commit d4f24ae

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

Runtime/src/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,15 @@ export class SwiftRuntime {
536536
) => {
537537
const ArrayType: TypedArray =
538538
this.memory.getObject(constructor_ref);
539+
if (length == 0) {
540+
// The elementsPtr can be unaligned in Swift's Array
541+
// implementation when the array is empty. However,
542+
// TypedArray requires the pointer to be aligned.
543+
// So, we need to create a new empty array without
544+
// using the elementsPtr.
545+
// See https://github.com/swiftwasm/swift/issues/5599
546+
return this.memory.retain(new ArrayType());
547+
}
539548
const array = new ArrayType(
540549
this.memory.rawMemory.buffer,
541550
elementsPtr,

Sources/JavaScriptKit/Runtime/index.js

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/JavaScriptKit/Runtime/index.mjs

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import XCTest
2+
import JavaScriptKit
3+
4+
final class JSTypedArrayTests: XCTestCase {
5+
func testEmptyArray() {
6+
_ = JSTypedArray<Int>([])
7+
_ = JSTypedArray<UInt>([])
8+
_ = JSTypedArray<Int8>([Int8]())
9+
_ = JSTypedArray<UInt8>([UInt8]())
10+
_ = JSUInt8ClampedArray([UInt8]())
11+
_ = JSTypedArray<Int16>([Int16]())
12+
_ = JSTypedArray<UInt16>([UInt16]())
13+
_ = JSTypedArray<Int32>([Int32]())
14+
_ = JSTypedArray<UInt32>([UInt32]())
15+
_ = JSTypedArray<Float32>([Float32]())
16+
_ = JSTypedArray<Float64>([Float64]())
17+
}
18+
}

0 commit comments

Comments
 (0)