Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 620365b

Browse files
Merge pull request #314 from swiftwasm/yt/typed-array-init
Add `JSTypedArray.init(buffer:)` initializer
2 parents b5231dc + 443bc3c commit 620365b

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

‎Sources/JavaScriptKit/BasicObjects/JSTypedArray.swift‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,32 @@ public class JSTypedArray<Element>: JSBridgedClass, ExpressibleByArrayLiteral wh
4646
///
4747
/// - Parameter array: The array that will be copied to create a new instance of TypedArray
4848
public convenience init(_ array: [Element]) {
49-
let jsArrayRef = array.withUnsafeBufferPointer { ptr in
50-
// Retain the constructor function to avoid it being released before calling `swjs_create_typed_array`
51-
withExtendedLifetime(Self.constructor!) { ctor in
52-
swjs_create_typed_array(ctor.id, ptr.baseAddress, Int32(array.count))
53-
}
49+
let object = array.withUnsafeBufferPointer { buffer in
50+
Self.createTypedArray(from: buffer)
5451
}
55-
self.init(unsafelyWrapping: JSObject(id: jsArrayRef))
52+
self.init(unsafelyWrapping: object)
5653
}
5754

5855
/// Convenience initializer for `Sequence`.
5956
public convenience init<S: Sequence>(_ sequence: S) where S.Element == Element {
6057
self.init(Array(sequence))
6158
}
6259

60+
/// Initialize a new instance of TypedArray in JavaScript environment with given buffer contents.
61+
///
62+
/// - Parameter buffer: The buffer that will be copied to create a new instance of TypedArray
63+
public convenience init(buffer: UnsafeBufferPointer<Element>) {
64+
self.init(unsafelyWrapping: Self.createTypedArray(from: buffer))
65+
}
66+
67+
private static func createTypedArray(from buffer: UnsafeBufferPointer<Element>) -> JSObject {
68+
// Retain the constructor function to avoid it being released before calling `swjs_create_typed_array`
69+
let jsArrayRef = withExtendedLifetime(Self.constructor!) { ctor in
70+
swjs_create_typed_array(ctor.id, buffer.baseAddress, Int32(buffer.count))
71+
}
72+
return JSObject(id: jsArrayRef)
73+
}
74+
6375
/// Length (in bytes) of the typed array.
6476
/// The value is established when a TypedArray is constructed and cannot be changed.
6577
/// If the TypedArray is not specifying a `byteOffset` or a `length`, the `length` of the referenced `ArrayBuffer` will be returned.

‎Tests/JavaScriptKitTests/JSTypedArrayTests.swift‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,16 @@ final class JSTypedArrayTests: XCTestCase {
9797

9898
XCTAssertEqual(toString(array.jsValue.object!), jsStringify(Array(0..<100)))
9999
}
100+
101+
func testInitWithBufferPointer() {
102+
let buffer = UnsafeMutableBufferPointer<Float32>.allocate(capacity: 20)
103+
defer { buffer.deallocate() }
104+
for i in 0..<20 {
105+
buffer[i] = Float32(i)
106+
}
107+
let typedArray = JSTypedArray<Float32>(buffer: UnsafeBufferPointer(buffer))
108+
for i in 0..<20 {
109+
XCTAssertEqual(typedArray[i], Float32(i))
110+
}
111+
}
100112
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /