-
-
Notifications
You must be signed in to change notification settings - Fork 51
Add a helper method to copy an array of numbers to a JS TypedArray #31
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
Merged
Merged
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
fe68a31
Add a helper method to copy an array of numbers to a JS TypedArray
j-f1 0cb3f65
_copy_typed_array_content → _create_typed_array
j-f1 3eca18c
Merge remote-tracking branch 'upstream/master' into typed-array
j-f1 bff8568
Add globalVariable
j-f1 136315f
Remove broken test target
j-f1 5b875b2
Create JSTypedArray
j-f1 af57583
Reduce to just a single class
j-f1 e71fc56
Clean up types
j-f1 17d83f5
Fix tests
j-f1 64342d2
Formatting
j-f1 ab974af
Test all the array types
j-f1 0928da8
Fix test error
j-f1 a1f5b03
Add a test("name") { ... } helper that makes it easy to find out whic…
j-f1 7971185
Rename allocHeap and freeHeap to retain/release
j-f1 dde8cf2
Propagate names through to the Swift side
j-f1 74610c2
Add an explicit retain() function and fix a ref counting bug
j-f1 14ab088
Add error when reading invalid reference
j-f1 b6602c8
Actually fix the tests
j-f1 561b8a6
Explain why _retain is necessary
j-f1 b0ff949
Update _CJavaScriptKit.h
j-f1 7836ac2
Merge remote-tracking branch 'upstream/master' into typed-array
j-f1 e0ef55f
Merge branch 'master' into typed-array-change-proposal
kateinoigakukun d64def7
Remove manual reference counting
kateinoigakukun fab45e1
Fix test cases
kateinoigakukun f83a84c
Expose failable initializer
kateinoigakukun 2370a1f
Merge pull request #1 from kateinoigakukun/typed-array-change-proposal
j-f1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
// | ||
// Created by Manuel Burghard. Licensed unter MIT. | ||
// | ||
|
||
import _CJavaScriptKit | ||
|
||
public protocol TypedArrayElement: JSValueConvertible, JSValueConstructible { | ||
static var typedArrayKind: JavaScriptTypedArrayKind { get } | ||
static var typedArrayClass: JSFunctionRef { get } | ||
} | ||
|
||
public class JSTypedArray<Element>: JSValueConvertible, ExpressibleByArrayLiteral where Element: TypedArrayElement { | ||
let ref: JSObject | ||
public func jsValue() -> JSValue { | ||
.object(ref) | ||
} | ||
|
||
public subscript(_ index: Int) -> Element { | ||
get { | ||
return Element.construct(from: getJSValue(this: ref, index: Int32(index)))! | ||
} | ||
set { | ||
setJSValue(this: ref, index: Int32(index), value: newValue.jsValue()) | ||
} | ||
} | ||
|
||
// This private initializer assumes that the passed object is TypedArray | ||
private init(unsafe object: JSObject) { | ||
self.ref = object | ||
} | ||
|
||
public init?(_ object: JSObject) { | ||
guard object.isInstanceOf(Element.typedArrayClass) else { return nil } | ||
self.ref = object | ||
} | ||
|
||
public convenience init(length: Int) { | ||
let jsObject = Element.typedArrayClass.new(length) | ||
self.init(unsafe: jsObject) | ||
} | ||
|
||
required public convenience init(arrayLiteral elements: Element...) { | ||
self.init(elements) | ||
} | ||
|
||
public convenience init(_ array: [Element]) { | ||
var resultObj = JavaScriptObjectRef() | ||
array.withUnsafeBufferPointer { ptr in | ||
_create_typed_array(Element.typedArrayKind, ptr.baseAddress!, Int32(array.count), &resultObj) | ||
} | ||
self.init(unsafe: JSObject(id: resultObj)) | ||
} | ||
|
||
public convenience init(_ stride: StrideTo<Element>) where Element: Strideable { | ||
self.init(stride.map({ $0 })) | ||
} | ||
} | ||
|
||
// MARK: - Int and UInt support | ||
|
||
// FIXME: Should be updated to support wasm64 when that becomes available. | ||
func valueForBitWidth<T>(typeName: String, bitWidth: Int, when32: T) -> T { | ||
if bitWidth == 32 { | ||
return when32 | ||
} else if bitWidth == 64 { | ||
fatalError("64-bit \(typeName)s are not yet supported in JSTypedArray") | ||
} else { | ||
fatalError("Unsupported bit width for type \(typeName): \(bitWidth) (hint: stick to fixed-size \(typeName)s to avoid this issue)") | ||
} | ||
} | ||
|
||
extension Int: TypedArrayElement { | ||
public static var typedArrayClass: JSFunctionRef { | ||
valueForBitWidth(typeName: "Int", bitWidth: Int.bitWidth, when32: JSObjectRef.global.Int32Array).function! | ||
} | ||
public static var typedArrayKind: JavaScriptTypedArrayKind { | ||
valueForBitWidth(typeName: "Int", bitWidth: Int.bitWidth, when32: .int32) | ||
} | ||
} | ||
extension UInt: TypedArrayElement { | ||
public static var typedArrayClass: JSFunctionRef { | ||
valueForBitWidth(typeName: "UInt", bitWidth: Int.bitWidth, when32: JSObjectRef.global.Uint32Array).function! | ||
} | ||
public static var typedArrayKind: JavaScriptTypedArrayKind { | ||
valueForBitWidth(typeName: "UInt", bitWidth: UInt.bitWidth, when32: .uint32) | ||
} | ||
} | ||
|
||
// MARK: - Concrete TypedArray classes | ||
|
||
extension Int8: TypedArrayElement { | ||
public static var typedArrayClass: JSFunctionRef { JSObjectRef.global.Int8Array.function! } | ||
public static var typedArrayKind: JavaScriptTypedArrayKind { .int8 } | ||
} | ||
extension UInt8: TypedArrayElement { | ||
public static var typedArrayClass: JSFunctionRef { JSObjectRef.global.Uint8Array.function! } | ||
public static var typedArrayKind: JavaScriptTypedArrayKind { .uint8 } | ||
} | ||
// TODO: Support Uint8ClampedArray? | ||
|
||
extension Int16: TypedArrayElement { | ||
public static var typedArrayClass: JSFunctionRef { JSObjectRef.global.Int16Array.function! } | ||
public static var typedArrayKind: JavaScriptTypedArrayKind { .int16 } | ||
} | ||
extension UInt16: TypedArrayElement { | ||
public static var typedArrayClass: JSFunctionRef { JSObjectRef.global.Uint16Array.function! } | ||
public static var typedArrayKind: JavaScriptTypedArrayKind { .uint16 } | ||
} | ||
|
||
extension Int32: TypedArrayElement { | ||
public static var typedArrayClass: JSFunctionRef { JSObjectRef.global.Int32Array.function! } | ||
public static var typedArrayKind: JavaScriptTypedArrayKind { .int32 } | ||
} | ||
extension UInt32: TypedArrayElement { | ||
public static var typedArrayClass: JSFunctionRef { JSObjectRef.global.Uint32Array.function! } | ||
public static var typedArrayKind: JavaScriptTypedArrayKind { .uint32 } | ||
} | ||
|
||
// FIXME: Support passing BigInts across the bridge | ||
//extension Int64: TypedArrayElement { | ||
// public static var typedArrayClass: JSFunctionRef { JSObjectRef.global.BigInt64Array.function! } | ||
// public static var type: JavaScriptTypedArrayKind { .bigInt64 } | ||
//} | ||
//extension UInt64: TypedArrayElement { | ||
// public static var typedArrayClass: JSFunctionRef { JSObjectRef.global.BigUint64Array.function! } | ||
// public static var type: JavaScriptTypedArrayKind { .bigUint64 } | ||
//} | ||
|
||
extension Float32: TypedArrayElement { | ||
public static var typedArrayClass: JSFunctionRef { JSObjectRef.global.Float32Array.function! } | ||
public static var typedArrayKind: JavaScriptTypedArrayKind { .float32 } | ||
} | ||
extension Float64: TypedArrayElement { | ||
public static var typedArrayClass: JSFunctionRef { JSObjectRef.global.Float64Array.function! } | ||
public static var typedArrayKind: JavaScriptTypedArrayKind { .float64 } | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this variable can be capsulized in SwiftRuntimeHeap.