Skip to content

Floating Point #3

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 10 commits into from
Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Expand Up @@ -30,6 +30,13 @@ func expectObject(_ value: JSValue, file: StaticString = #file, line: UInt = #li
}
}

func expectArray(_ value: JSValue, file: StaticString = #file, line: UInt = #line, column: UInt = #column) throws -> JSArrayRef {
guard let array = value.array else {
throw MessageError("Type of \(value) should be \"object\"", file: file, line: line, column: column)
}
return array
}

func expectFunction(_ value: JSValue, file: StaticString = #file, line: UInt = #line, column: UInt = #column) throws -> JSFunctionRef {
switch value {
case .function(let ref): return ref
Expand All @@ -46,7 +53,7 @@ func expectBoolean(_ value: JSValue, file: StaticString = #file, line: UInt = #l
}
}

func expectNumber(_ value: JSValue, file: StaticString = #file, line: UInt = #line, column: UInt = #column) throws -> Int32 {
func expectNumber(_ value: JSValue, file: StaticString = #file, line: UInt = #line, column: UInt = #column) throws -> Double {
switch value {
case .number(let number): return number
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,24 @@ Literal_Conversion: do {
.string("foobar"),
.string("👨‍👩‍👧‍👧 Family Emoji"),
.number(0),
.number(.max),
.number(.min),
.number(Double(Int32.max)),
.number(Double(Int32.min)),
.number(Double.infinity),
.number(Double.nan),
.null,
.undefined,
]
for (index, input) in inputs.enumerated() {
let prop = "prop_\(index)"
setJSValue(this: global, name: prop, value: input)
let got = getJSValue(this: global, name: prop)
try expectEqual(got, input)
switch (got, input) {
case let (.number(lhs), .number(rhs)):
// Compare bitPattern because nan == nan is always false
try expectEqual(lhs.bitPattern, rhs.bitPattern)
default:
try expectEqual(got, input)
}
}
} catch {
print(error)
Expand Down Expand Up @@ -67,6 +75,50 @@ Object_Conversion: do {
print(error)
}

Value_Construction: do {
let globalObject1 = getJSValue(this: .global, name: "globalObject1")
let globalObject1Ref = try expectObject(globalObject1)
let prop_2 = getJSValue(this: globalObject1Ref, name: "prop_2")
try expectEqual(Int.construct(from: prop_2), 2)
let prop_3 = getJSValue(this: globalObject1Ref, name: "prop_3")
try expectEqual(Bool.construct(from: prop_3), true)
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 {
let globalObject1 = getJSValue(this: .global, name: "globalObject1")
let globalObject1Ref = try expectObject(globalObject1)
let prop_4 = getJSValue(this: globalObject1Ref, name: "prop_4")
let array = try expectArray(prop_4)
let expectedProp_4: [JSValue] = [
.number(3), .number(4), .string("str_elm_1"), .number(5)
]
try expectEqual(Array(array), expectedProp_4)
}

Value_Decoder: do {
struct GlobalObject1: Codable {
struct Prop1: Codable {
let nested_prop: Int
}
let prop_1: Prop1
let prop_2: Int
let prop_3: Bool
let prop_7: Float
}
let decoder = JSValueDecoder()
let rawGlobalObject1 = getJSValue(this: .global, name: "globalObject1")
let globalObject1 = try decoder.decode(GlobalObject1.self, from: rawGlobalObject1)
try expectEqual(globalObject1.prop_1.nested_prop, 1)
try expectEqual(globalObject1.prop_2, 2)
try expectEqual(globalObject1.prop_3, true)
try expectEqual(globalObject1.prop_7, 3.14)
}

Function_Call: do {
// Notes: globalObject1 is defined in JavaScript environment
//
Expand Down Expand Up @@ -154,10 +206,8 @@ Host_Function_Registration: do {
}

try expectEqual(hostFunc2(3), .number(6))
// FIXME: Crash with latest toolchain
/*
_ = try expectString(hostFunc2(true))
*/

} catch {
print(error)
}
Expand Down
21 changes: 16 additions & 5 deletions IntegrationTests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,20 @@ const readFile = promisify(fs.readFile);
const swift = new SwiftRuntime();
// Instantiate a new WASI Instance
const wasmFs = new WasmFs();
// Output stdout and stderr to console
const originalWriteSync = wasmFs.fs.writeSync;
wasmFs.fs.writeSync = (fd, buffer, offset, length, position) => {
const text = new TextDecoder("utf-8").decode(buffer);
switch (fd) {
case 1:
console.log(text);
break;
case 2:
console.error(text);
break;
}
return originalWriteSync(fd, buffer, offset, length, position);
};
let wasi = new WASI({
args: [],
env: {},
Expand Down Expand Up @@ -41,7 +55,8 @@ global.globalObject1 = {
"call_host_1": () => {
return global.globalObject1.prop_6.host_func_1()
}
}
},
"prop_7": 3.14,
}

global.Animal = function(name, age, isCat) {
Expand Down Expand Up @@ -69,10 +84,6 @@ const startWasiTask = async () => {
swift.setInsance(instance);
// Start the WebAssembly WASI instance!
wasi.start(instance);

// Output what's inside of /dev/stdout!
const stdout = await wasmFs.getStdOut();
console.log(stdout);
};
startWasiTask().catch(err => {
console.log(err)
Expand Down
Loading