@@ -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.
0 commit comments