Skip to content

Commit 6eb1b09

Browse files
Merge pull request swiftwasm#3 from kateinoigakukun/feat/float
Floating Point
2 parents d0aaa49 + 3d8d15d commit 6eb1b09

File tree

12 files changed

+596
-137
lines changed

12 files changed

+596
-137
lines changed

IntegrationTests/JavaScriptKitExec/Sources/JavaScriptKitExec/UnitTestUtils.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@ func expectObject(_ value: JSValue, file: StaticString = #file, line: UInt = #li
3030
}
3131
}
3232

33+
func expectArray(_ value: JSValue, file: StaticString = #file, line: UInt = #line, column: UInt = #column) throws -> JSArrayRef {
34+
guard let array = value.array else {
35+
throw MessageError("Type of \(value) should be \"object\"", file: file, line: line, column: column)
36+
}
37+
return array
38+
}
39+
3340
func expectFunction(_ value: JSValue, file: StaticString = #file, line: UInt = #line, column: UInt = #column) throws -> JSFunctionRef {
3441
switch value {
3542
case .function(let ref): return ref
@@ -46,7 +53,7 @@ func expectBoolean(_ value: JSValue, file: StaticString = #file, line: UInt = #l
4653
}
4754
}
4855

49-
func expectNumber(_ value: JSValue, file: StaticString = #file, line: UInt = #line, column: UInt = #column) throws -> Int32 {
56+
func expectNumber(_ value: JSValue, file: StaticString = #file, line: UInt = #line, column: UInt = #column) throws -> Double {
5057
switch value {
5158
case .number(let number): return number
5259
default:

IntegrationTests/JavaScriptKitExec/Sources/JavaScriptKitExec/main.swift

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,24 @@ Literal_Conversion: do {
88
.string("foobar"),
99
.string("👨‍👩‍👧‍👧 Family Emoji"),
1010
.number(0),
11-
.number(.max),
12-
.number(.min),
11+
.number(Double(Int32.max)),
12+
.number(Double(Int32.min)),
13+
.number(Double.infinity),
14+
.number(Double.nan),
1315
.null,
1416
.undefined,
1517
]
1618
for (index, input) in inputs.enumerated() {
1719
let prop = "prop_\(index)"
1820
setJSValue(this: global, name: prop, value: input)
1921
let got = getJSValue(this: global, name: prop)
20-
try expectEqual(got, input)
22+
switch (got, input) {
23+
case let (.number(lhs), .number(rhs)):
24+
// Compare bitPattern because nan == nan is always false
25+
try expectEqual(lhs.bitPattern, rhs.bitPattern)
26+
default:
27+
try expectEqual(got, input)
28+
}
2129
}
2230
} catch {
2331
print(error)
@@ -67,6 +75,50 @@ Object_Conversion: do {
6775
print(error)
6876
}
6977

78+
Value_Construction: do {
79+
let globalObject1 = getJSValue(this: .global, name: "globalObject1")
80+
let globalObject1Ref = try expectObject(globalObject1)
81+
let prop_2 = getJSValue(this: globalObject1Ref, name: "prop_2")
82+
try expectEqual(Int.construct(from: prop_2), 2)
83+
let prop_3 = getJSValue(this: globalObject1Ref, name: "prop_3")
84+
try expectEqual(Bool.construct(from: prop_3), true)
85+
let prop_7 = getJSValue(this: globalObject1Ref, name: "prop_7")
86+
try expectEqual(Double.construct(from: prop_7), 3.14)
87+
try expectEqual(Float.construct(from: prop_7), 3.14)
88+
} catch {
89+
print(error)
90+
}
91+
92+
Array_Iterator: do {
93+
let globalObject1 = getJSValue(this: .global, name: "globalObject1")
94+
let globalObject1Ref = try expectObject(globalObject1)
95+
let prop_4 = getJSValue(this: globalObject1Ref, name: "prop_4")
96+
let array = try expectArray(prop_4)
97+
let expectedProp_4: [JSValue] = [
98+
.number(3), .number(4), .string("str_elm_1"), .number(5)
99+
]
100+
try expectEqual(Array(array), expectedProp_4)
101+
}
102+
103+
Value_Decoder: do {
104+
struct GlobalObject1: Codable {
105+
struct Prop1: Codable {
106+
let nested_prop: Int
107+
}
108+
let prop_1: Prop1
109+
let prop_2: Int
110+
let prop_3: Bool
111+
let prop_7: Float
112+
}
113+
let decoder = JSValueDecoder()
114+
let rawGlobalObject1 = getJSValue(this: .global, name: "globalObject1")
115+
let globalObject1 = try decoder.decode(GlobalObject1.self, from: rawGlobalObject1)
116+
try expectEqual(globalObject1.prop_1.nested_prop, 1)
117+
try expectEqual(globalObject1.prop_2, 2)
118+
try expectEqual(globalObject1.prop_3, true)
119+
try expectEqual(globalObject1.prop_7, 3.14)
120+
}
121+
70122
Function_Call: do {
71123
// Notes: globalObject1 is defined in JavaScript environment
72124
//
@@ -154,10 +206,8 @@ Host_Function_Registration: do {
154206
}
155207

156208
try expectEqual(hostFunc2(3), .number(6))
157-
// FIXME: Crash with latest toolchain
158-
/*
159209
_ = try expectString(hostFunc2(true))
160-
*/
210+
161211
} catch {
162212
print(error)
163213
}

IntegrationTests/index.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,20 @@ const readFile = promisify(fs.readFile);
99
const swift = new SwiftRuntime();
1010
// Instantiate a new WASI Instance
1111
const wasmFs = new WasmFs();
12+
// Output stdout and stderr to console
13+
const originalWriteSync = wasmFs.fs.writeSync;
14+
wasmFs.fs.writeSync = (fd, buffer, offset, length, position) => {
15+
const text = new TextDecoder("utf-8").decode(buffer);
16+
switch (fd) {
17+
case 1:
18+
console.log(text);
19+
break;
20+
case 2:
21+
console.error(text);
22+
break;
23+
}
24+
return originalWriteSync(fd, buffer, offset, length, position);
25+
};
1226
let wasi = new WASI({
1327
args: [],
1428
env: {},
@@ -41,7 +55,8 @@ global.globalObject1 = {
4155
"call_host_1": () => {
4256
return global.globalObject1.prop_6.host_func_1()
4357
}
44-
}
58+
},
59+
"prop_7": 3.14,
4560
}
4661

4762
global.Animal = function(name, age, isCat) {
@@ -69,10 +84,6 @@ const startWasiTask = async () => {
6984
swift.setInsance(instance);
7085
// Start the WebAssembly WASI instance!
7186
wasi.start(instance);
72-
73-
// Output what's inside of /dev/stdout!
74-
const stdout = await wasmFs.getStdOut();
75-
console.log(stdout);
7687
};
7788
startWasiTask().catch(err => {
7889
console.log(err)

0 commit comments

Comments
 (0)