Skip to content
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
29 changes: 29 additions & 0 deletions Sources/Substrata/Internals/Conversion.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,35 @@ extension Decimal: JSConvertible, JSInternalConvertible {
}
}

extension Date: JSConvertible, JSInternalConvertible {
var string: String {
return self.description
}

internal static func fromJSValue(_ value: JSValue, context: JSContext) -> Date? {
// If it's an object, verify it's a Date instance
if JS_IsObject(value) > 0 {
let globalObj = JS_GetGlobalObject(context.ref)
let dateCtor = JS_GetPropertyStr(context.ref, globalObj, "Date")
let isDate = JS_IsInstanceOf(context.ref, value, dateCtor) > 0
JS_FreeValue(context.ref, dateCtor)
JS_FreeValue(context.ref, globalObj)
if !isDate { return nil }
}
var ms = 0.0
if JS_ToFloat64(context.ref, &ms, value) == 0, ms.isFinite {
return Date(timeIntervalSince1970: ms / 1000.0)
}
return nil
}

internal func toJSValue(context: JSContext) -> JSValue? {
let ms = timeIntervalSince1970 * 1000.0 // seconds → ms
let v = JS_NewDate(context.ref, ms)
return v
}
}

extension Dictionary: JSConvertible, JSInternalConvertible where Key == String, Value == JSConvertible {
static func fromJSValue(_ value: JSValue, context: JSContext) -> Dictionary<Key, Value>? {
if JS_IsObject(value) > 0 {
Expand Down
1 change: 1 addition & 0 deletions Sources/Substrata/Utilities/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ extension JSValue {
}
}

if let v = Date.fromJSValue(value, context: context) { return v }
if let v = Array<JSConvertible>.fromJSValue(value, context: context) { return v }
if let v = JSFunction.fromJSValue(value, context: context) { return v }
if let v = JSError.fromJSValue(value, context: context) { return v }
Expand Down
24 changes: 23 additions & 1 deletion Tests/SubstrataTests/ConversionTests.swift
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ final class ConversionTests: XCTestCase {
XCTAssertTrue((samples!["anArray"]! as! [JSConvertible]).count == 6)
XCTAssertTrue((samples!["aDictionary"]!.typed(as: Dictionary.self))!.keys.count == 7)

// test date ...
let date = samples!["aDate"]!.typed(as: Date.self)!
XCTAssertEqual(
Int64(date.timeIntervalSince1970 * 1000), 1714564800000 // expected epoch ms for 2024-05-01T12:00:00Z
)

// test nested array stuff ...
let array = samples!["anArray"]!.typed(as: Array.self)
let subArray = array![4] as! [JSConvertible]
Expand All @@ -123,5 +129,21 @@ final class ConversionTests: XCTestCase {
XCTAssertTrue((nestedDict["anArray"] as! [JSConvertible]).count == 4)
}


func testMassConversionIn() throws {
// expand this later ... just doing date for now.
let engine = JSEngine()
engine.exceptionHandler = { error in
XCTFail()
print(error)
}

// test date ...
let isoFormatter = ISO8601DateFormatter()
let date = isoFormatter.date(from: "2024-05-01T12:00:00Z")!
engine.setValue(date, for: "aDate")
let jsdate = engine.value(for: "aDate")!.typed(as: Date.self)!
XCTAssertEqual(
Int64(jsdate.timeIntervalSince1970 * 1000), 1714564800000 // expected epoch ms for 2024-05-01T12:00:00Z
)
}
}
1 change: 1 addition & 0 deletions Tests/SubstrataTests/Support/ConversionTestData.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const conversionSamples = {
aDate: new Date("2024-05-01T12:00:00Z"),
anInt: 1234,
aBool: true,
anotherBool: false,
Expand Down