Skip to content

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 26 commits into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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 Aug 3, 2020
0cb3f65
_copy_typed_array_content → _create_typed_array
j-f1 Aug 4, 2020
3eca18c
Merge remote-tracking branch 'upstream/master' into typed-array
j-f1 Aug 12, 2020
bff8568
Add globalVariable
j-f1 Aug 12, 2020
136315f
Remove broken test target
j-f1 Aug 13, 2020
5b875b2
Create JSTypedArray
j-f1 Aug 13, 2020
af57583
Reduce to just a single class
j-f1 Aug 13, 2020
e71fc56
Clean up types
j-f1 Aug 13, 2020
17d83f5
Fix tests
j-f1 Aug 13, 2020
64342d2
Formatting
j-f1 Aug 13, 2020
ab974af
Test all the array types
j-f1 Aug 13, 2020
0928da8
Fix test error
j-f1 Aug 13, 2020
a1f5b03
Add a test("name") { ... } helper that makes it easy to find out whic…
j-f1 Aug 13, 2020
7971185
Rename allocHeap and freeHeap to retain/release
j-f1 Aug 13, 2020
dde8cf2
Propagate names through to the Swift side
j-f1 Aug 13, 2020
74610c2
Add an explicit retain() function and fix a ref counting bug
j-f1 Aug 13, 2020
14ab088
Add error when reading invalid reference
j-f1 Aug 13, 2020
b6602c8
Actually fix the tests
j-f1 Aug 13, 2020
561b8a6
Explain why _retain is necessary
j-f1 Aug 14, 2020
b0ff949
Update _CJavaScriptKit.h
j-f1 Aug 14, 2020
7836ac2
Merge remote-tracking branch 'upstream/master' into typed-array
j-f1 Aug 22, 2020
e0ef55f
Merge branch 'master' into typed-array-change-proposal
kateinoigakukun Sep 9, 2020
d64def7
Remove manual reference counting
kateinoigakukun Sep 9, 2020
fab45e1
Fix test cases
kateinoigakukun Sep 9, 2020
f83a84c
Expose failable initializer
kateinoigakukun Sep 10, 2020
2370a1f
Merge pull request #1 from kateinoigakukun/typed-array-change-proposal
j-f1 Sep 10, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,10 @@ ObjectRef_Lifetime: do {
} catch {
print(error)
}

TypedArray: do {
let numbers = [UInt8](0 ... 255)
let typedArray = JSObjectRef.createTypedArray(numbers)
try expectEqual(typedArray[12], .number(12))
try expectEqual(typedArray.toString!(), .string(numbers.map(String.init).joined(separator: ",")))
}
36 changes: 36 additions & 0 deletions Runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,32 @@ enum JavaScriptValueKind {
Function = 6,
}

enum JavaScriptTypedArrayKind {
Int8 = 0,
Uint8 = 1,
Int16 = 2,
Uint16 = 3,
Int32 = 4,
Uint32 = 5,
BigInt64 = 6,
BigUint64 = 7,
Float32 = 8,
Float64 = 9,
}

type TypedArray =
| Int8ArrayConstructor
| Uint8ArrayConstructor
| Int16ArrayConstructor
| Uint16ArrayConstructor
| Int32ArrayConstructor
| Uint32ArrayConstructor
// | BigInt64ArrayConstructor
// | BigUint64ArrayConstructor
| Float32ArrayConstructor
| Float64ArrayConstructor


type SwiftRuntimeHeapEntry = {
id: number,
rc: number,
Expand Down Expand Up @@ -370,6 +396,16 @@ export class SwiftRuntime {
const constructor = this.heap.referenceHeap(constructor_ref)
return obj instanceof constructor
},
swjs_create_typed_array: (
kind: JavaScriptTypedArrayKind,
elementsPtr: pointer, length: number,
result_obj: pointer
) => {
const ArrayType: TypedArray = this.heap.referenceHeap(0)[JavaScriptTypedArrayKind[kind] + 'Array']
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't abuse address 0 just to access global context.
It would be better to store global or window as a member of SwiftRuntime and access it directly 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created a globalVariable variable as a replacement. Is that a good name or should I go with something else?

const array = new ArrayType(memory().buffer, elementsPtr, length);
// Call `.slice()` to copy the memory
writeUint32(result_obj, this.heap.allocHeap(array.slice()));
},
swjs_destroy_ref: (ref: ref) => {
this.heap.freeHeap(ref)
}
Expand Down
63 changes: 63 additions & 0 deletions Sources/JavaScriptKit/JSObject.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,67 @@ public class JSObjectRef: Equatable {
public func jsValue() -> JSValue {
.object(self)
}

public class func createTypedArray<Type>(_ array: [Type]) -> JSObjectRef where Type: TypedArrayElement {
let type: JavaScriptTypedArrayKind
switch Type.self {
case is Int8.Type:
type = .int8
case is UInt8.Type:
type = .uint8
case is Int16.Type:
type = .int16
case is UInt16.Type:
type = .uint16
case is Int32.Type:
type = .int32
case is UInt32.Type:
type = .uint32
case is Int64.Type:
type = .bigInt64
case is UInt64.Type:
type = .bigUint64
case is Float32.Type:
type = .float32
case is Float64.Type:
type = .float64
default:
if Type.self is UInt.Type || Type.self is Int.Type {
if UInt.bitWidth == 32 {
if Type.self is UInt.Type {
type = .uint32
} else {
type = .int32
}
} else if UInt.bitWidth == 64 {
if Type.self is UInt.Type {
type = .bigUint64
} else {
type = .bigInt64
}
} else {
fatalError("Unsupported bit width type for UInt: \(UInt.bitWidth) (hint: stick to fixed-size ints to avoid this issue)")
}
} else {
fatalError("Unsupported Swift type for TypedArray: \(Type.self)")
}
}
var resultObj = JavaScriptObjectRef()
array.withUnsafeBufferPointer { ptr in
_create_typed_array(type, ptr.baseAddress!, Int32(array.count), &resultObj)
}
return JSObjectRef(id: resultObj)
}
}

public protocol TypedArrayElement {}
extension Int8: TypedArrayElement {}
extension UInt8: TypedArrayElement {}
extension Int16: TypedArrayElement {}
extension UInt16: TypedArrayElement {}
extension Int32: TypedArrayElement {}
extension UInt32: TypedArrayElement {}
extension Int64: TypedArrayElement {}
extension UInt64: TypedArrayElement {}
extension Float32: TypedArrayElement {}
extension Float64: TypedArrayElement {}
7 changes: 7 additions & 0 deletions Sources/JavaScriptKit/XcodeSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,11 @@ import _CJavaScriptKit
_: UnsafePointer<JavaScriptObjectRef>!
) { fatalError() }
func _destroy_ref(_: JavaScriptObjectRef) { fatalError() }
func _create_typed_array<T: TypedArrayElement>(
_: JavaScriptTypedArrayKind,
_: UnsafePointer<T>,
_: Int32,
_: UnsafeMutablePointer<JavaScriptObjectRef>!
) { fatalError() }

#endif
22 changes: 22 additions & 0 deletions Sources/_CJavaScriptKit/include/_CJavaScriptKit.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@ typedef enum __attribute__((enum_extensibility(closed))) {
JavaScriptValueKindFunction = 6,
} JavaScriptValueKind;

typedef enum __attribute__((enum_extensibility(closed))) {
JavaScriptTypedArrayKindInt8 = 0,
JavaScriptTypedArrayKindUint8 = 1,
JavaScriptTypedArrayKindInt16 = 2,
JavaScriptTypedArrayKindUint16 = 3,
JavaScriptTypedArrayKindInt32 = 4,
JavaScriptTypedArrayKindUint32 = 5,
JavaScriptTypedArrayKindBigInt64 = 6,
JavaScriptTypedArrayKindBigUint64 = 7,
JavaScriptTypedArrayKindFloat32 = 8,
JavaScriptTypedArrayKindFloat64 = 9,
} JavaScriptTypedArrayKind;


typedef unsigned JavaScriptPayload1;
typedef unsigned JavaScriptPayload2;
typedef double JavaScriptPayload3;
Expand Down Expand Up @@ -97,6 +111,14 @@ __attribute__((__import_module__("javascript_kit"),
__import_name__("swjs_destroy_ref"))) extern void
_destroy_ref(const JavaScriptObjectRef ref);

__attribute__((
__import_module__("javascript_kit"),
__import_name__("swjs_create_typed_array")
))
extern void _create_typed_array(const JavaScriptTypedArrayKind kind,
const void *elementsPtr, const int length,
JavaScriptObjectRef *result_obj);

#endif

#endif /* _CJavaScriptKit_h */