Skip to content
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

Add a helper method to copy an array of numbers to a JS TypedArray #31

Merged
merged 26 commits into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from 20 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
import JavaScriptKit

let printTestNames = false

func test(_ name: String, testBlock: () throws -> Void) {
if printTestNames { print(name) }
do {
try testBlock()
} catch {
print("Error in \(name)")
print(error)
}
}

struct MessageError: Error {
let message: String
let file: StaticString
Expand Down
98 changes: 62 additions & 36 deletions IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import JavaScriptKit

Literal_Conversion: do {
test("Literal Conversion") {
let global = JSObjectRef.global
let inputs: [JSValue] = [
.boolean(true),
Expand All @@ -27,11 +27,9 @@ Literal_Conversion: do {
try expectEqual(got, input)
}
}
} catch {
print(error)
}

Object_Conversion: do {
test("Object Conversion") {
// Notes: globalObject1 is defined in JavaScript environment
//
// ```js
Expand Down Expand Up @@ -70,12 +68,9 @@ Object_Conversion: do {
}

try expectEqual(getJSValue(this: globalObject1Ref, name: "undefined_prop"), .undefined)

} catch {
print(error)
}

Value_Construction: do {
test("Value Construction") {
let globalObject1 = getJSValue(this: .global, name: "globalObject1")
let globalObject1Ref = try expectObject(globalObject1)
let prop_2 = getJSValue(this: globalObject1Ref, name: "prop_2")
Expand All @@ -85,11 +80,9 @@ Value_Construction: do {
let prop_7 = getJSValue(this: globalObject1Ref, name: "prop_7")
try expectEqual(Double.construct(from: prop_7), 3.14)
try expectEqual(Float.construct(from: prop_7), 3.14)
} catch {
print(error)
}

Array_Iterator: do {
test("Array Iterator") {
let globalObject1 = getJSValue(this: .global, name: "globalObject1")
let globalObject1Ref = try expectObject(globalObject1)
let prop_4 = getJSValue(this: globalObject1Ref, name: "prop_4")
Expand All @@ -100,7 +93,7 @@ Array_Iterator: do {
try expectEqual(Array(array), expectedProp_4)
}

Array_RandomAccessCollection: do {
test("Array RandomAccessCollection") {
let globalObject1 = getJSValue(this: .global, name: "globalObject1")
let globalObject1Ref = try expectObject(globalObject1)
let prop_4 = getJSValue(this: globalObject1Ref, name: "prop_4")
Expand All @@ -111,7 +104,7 @@ Array_RandomAccessCollection: do {
try expectEqual([array[0], array[1], array[2], array[3]], expectedProp_4)
}

Value_Decoder: do {
test("Value Decoder") {
struct GlobalObject1: Codable {
struct Prop1: Codable {
let nested_prop: Int
Expand All @@ -131,7 +124,7 @@ Value_Decoder: do {
try expectEqual(globalObject1.prop_7, 3.14)
}

Function_Call: do {
test("Function Call") {
// Notes: globalObject1 is defined in JavaScript environment
//
// ```js
Expand Down Expand Up @@ -173,12 +166,9 @@ Function_Call: do {
try expectEqual(func6(true, 1, 2), .number(1))
try expectEqual(func6(false, 1, 2), .number(2))
try expectEqual(func6(true, "OK", 2), .string("OK"))

} catch {
print(error)
}

Host_Function_Registration: do {
test("Host Function Registration") {
// ```js
// global.globalObject1 = {
// ...
Expand Down Expand Up @@ -221,11 +211,9 @@ Host_Function_Registration: do {
try expectEqual(hostFunc2(3), .number(6))
_ = try expectString(hostFunc2(true))
hostFunc2.release()
} catch {
print(error)
}

New_Object_Construction: do {
test("New Object Construction") {
// ```js
// global.Animal = function(name, age, isCat) {
// this.name = name
Expand All @@ -247,11 +235,9 @@ New_Object_Construction: do {
let dog1 = objectConstructor.new("Pochi", 3, false)
let dog1Bark = try expectFunction(getJSValue(this: dog1, name: "bark"))
try expectEqual(dog1Bark(), .string("wan"))
} catch {
print(error)
}

Call_Function_With_This: do {
test("Call Function With This") {
// ```js
// global.Animal = function(name, age, isCat) {
// this.name = name
Expand All @@ -275,12 +261,9 @@ Call_Function_With_This: do {
// Call with this
let gotIsCat = getIsCat(this: cat1)
try expectEqual(gotIsCat, .boolean(true))

} catch {
print(error)
}

Object_Conversion: do {
test("Object Conversion") {
let array1 = [1, 2, 3]
let jsArray1 = array1.jsValue().object!
try expectEqual(jsArray1.length, .number(3))
Expand All @@ -307,11 +290,9 @@ Object_Conversion: do {
let jsDict1 = dict1.jsValue().object!
try expectEqual(jsDict1.prop1, .number(1))
try expectEqual(jsDict1.prop2, .string("foo"))
} catch {
print(error)
}

ObjectRef_Lifetime: do {
test("ObjectRef Lifetime") {
// ```js
// global.globalObject1 = {
// "prop_1": {
Expand All @@ -332,8 +313,6 @@ ObjectRef_Lifetime: do {
try expectEqual(ref1.prop_2, .number(2))
try expectEqual(ref2.prop_2, .number(2))
identity.release()
} catch {
print(error)
}

func closureScope() -> ObjectIdentifier {
Expand All @@ -343,10 +322,57 @@ func closureScope() -> ObjectIdentifier {
return result
}

Closure_Identifiers: do {
test("Closure Identifiers") {
let oid1 = closureScope()
let oid2 = closureScope()
try expectEqual(oid1, oid2)
} catch {
print(error)
}

func checkArray<T>(_ array: [T]) throws where T: TypedArrayElement {
try expectEqual(JSTypedArray(array).toString!(), .string(jsStringify(array)))
}

func jsStringify(_ array: [Any]) -> String {
array.map({ String(describing: $0) }).joined(separator: ",")
}

test("TypedArray") {
let numbers = [UInt8](0 ... 255)
let typedArray = JSTypedArray(numbers)
try expectEqual(typedArray[12], .number(12))

try checkArray([0, .max, 127, 1] as [UInt8])
try checkArray([0, 1, .max, .min, -1] as [Int8])

try checkArray([0, .max, 255, 1] as [UInt16])
try checkArray([0, 1, .max, .min, -1] as [Int16])

try checkArray([0, .max, 255, 1] as [UInt32])
try checkArray([0, 1, .max, .min, -1] as [Int32])

try checkArray([0, .max, 255, 1] as [UInt])
try checkArray([0, 1, .max, .min, -1] as [Int])

let float32Array: [Float32] = [0, 1, .pi, .greatestFiniteMagnitude, .infinity, .leastNonzeroMagnitude, .leastNormalMagnitude, 42]
let jsFloat32Array = JSTypedArray(float32Array)
for (i, num) in float32Array.enumerated() {
try expectEqual(num, jsFloat32Array[i])
}

let float64Array: [Float64] = [0, 1, .pi, .greatestFiniteMagnitude, .infinity, .leastNonzeroMagnitude, .leastNormalMagnitude, 42]
let jsFloat64Array = JSTypedArray(float64Array)
for (i, num) in float64Array.enumerated() {
try expectEqual(num, jsFloat64Array[i])
}
}

test("TypedArray_Mutation") {
let array = JSTypedArray<Int>(length: 100)
for i in 0..<100 {
array[i] = i
}
for i in 0..<100 {
try expectEqual(i, array[i])
}
try expectEqual(array.toString!(), .string(jsStringify(Array(0..<100))))
}
6 changes: 1 addition & 5 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ let package = Package(
.when(platforms: [.wasi])
),
]
),
.testTarget(
name: "JavaScriptKitTests",
dependencies: ["JavaScriptKit"]
),
)
]
)
Loading