Skip to content

Commit 12acaf5

Browse files
mjburghardj-f1
authored andcommitted
Changes related to WebIDL support.
1 parent 18adf42 commit 12acaf5

File tree

5 files changed

+516
-52
lines changed

5 files changed

+516
-52
lines changed

Sources/JavaScriptKit/JSFunction.swift

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@ import _CJavaScriptKit
22

33
@dynamicCallable
44
public class JSFunctionRef: JSObjectRef {
5+
6+
public override class func canDecode(from jsValue: JSValue) -> Bool {
7+
return jsValue.isFunction
8+
}
9+
510
@discardableResult
6-
public func dynamicallyCall(withArguments arguments: [JSValueConvertible]) -> JSValue {
11+
public func dynamicallyCall(withArguments arguments: [JSValueEncodable]) -> JSValue {
712
let result = arguments.withRawJSValues { rawValues -> RawJSValue in
813
rawValues.withUnsafeBufferPointer { bufferPointer -> RawJSValue in
914
let argv = bufferPointer.baseAddress
@@ -19,11 +24,11 @@ public class JSFunctionRef: JSObjectRef {
1924
return result.jsValue()
2025
}
2126

22-
public func apply(this: JSObjectRef, arguments: JSValueConvertible...) -> JSValue {
27+
public func apply(this: JSObjectRef, arguments: JSValueEncodable...) -> JSValue {
2328
apply(this: this, argumentList: arguments)
2429
}
2530

26-
public func apply(this: JSObjectRef, argumentList: [JSValueConvertible]) -> JSValue {
31+
public func apply(this: JSObjectRef, argumentList: [JSValueEncodable]) -> JSValue {
2732
let result = argumentList.withRawJSValues { rawValues in
2833
rawValues.withUnsafeBufferPointer { bufferPointer -> RawJSValue in
2934
let argv = bufferPointer.baseAddress
@@ -38,7 +43,7 @@ public class JSFunctionRef: JSObjectRef {
3843
return result.jsValue()
3944
}
4045

41-
public func new(_ arguments: JSValueConvertible...) -> JSObjectRef {
46+
public func new(_ arguments: JSValueEncodable...) -> JSObjectRef {
4247
return arguments.withRawJSValues { rawValues in
4348
rawValues.withUnsafeBufferPointer { bufferPointer in
4449
let argv = bufferPointer.baseAddress
@@ -58,6 +63,15 @@ public class JSFunctionRef: JSObjectRef {
5863
fatalError("unavailable")
5964
}
6065

66+
public convenience required init(jsValue: JSValue) {
67+
switch jsValue {
68+
case .function(let value):
69+
self.init(id: value.id)
70+
default:
71+
fatalError()
72+
}
73+
}
74+
6175
override public func jsValue() -> JSValue {
6276
.function(self)
6377
}
@@ -81,6 +95,10 @@ public class JSClosure: JSFunctionRef {
8195
id = objectRef
8296
}
8397

98+
public convenience required init(jsValue: JSValue) {
99+
self.init(JSFunctionRef(jsValue: jsValue).dynamicallyCall)
100+
}
101+
84102
public func release() {
85103
Self.sharedFunctions[hostFuncRef] = nil
86104
}

Sources/JavaScriptKit/JSObject.swift

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,19 @@ import _CJavaScriptKit
22

33
@dynamicMemberLookup
44
public class JSObjectRef: Equatable {
5+
public class func canDecode(from jsValue: JSValue) -> Bool {
6+
return jsValue.isObject
7+
}
8+
59
internal var id: UInt32
610
init(id: UInt32) {
711
self.id = id
812
}
913

1014
@_disfavoredOverload
11-
public subscript(dynamicMember name: String) -> ((JSValueConvertible...) -> JSValue)? {
15+
public subscript(dynamicMember name: String) -> ((JSValueEncodable...) -> JSValue)? {
1216
guard let function = self[dynamicMember: name].function else { return nil }
13-
return { (arguments: JSValueConvertible...) in
17+
return { (arguments: JSValueEncodable...) in
1418
function.apply(this: self, argumentList: arguments)
1519
}
1620
}
@@ -54,6 +58,15 @@ public class JSObjectRef: Equatable {
5458
return lhs.id == rhs.id
5559
}
5660

61+
public convenience required init(jsValue: JSValue) {
62+
switch jsValue {
63+
case .object(let value):
64+
self.init(id: value.id)
65+
default:
66+
fatalError()
67+
}
68+
}
69+
5770
public func jsValue() -> JSValue {
5871
.object(self)
5972
}

Sources/JavaScriptKit/JSValue.swift

Lines changed: 63 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,51 @@ public enum JSValue: Equatable {
4141
object.flatMap { JSArrayRef($0) }
4242
}
4343

44-
public var isNull: Bool { return self == .null }
45-
public var isUndefined: Bool { return self == .undefined }
4644
public var function: JSFunctionRef? {
4745
switch self {
4846
case let .function(function): return function
4947
default: return nil
5048
}
5149
}
50+
51+
public var isBoolean: Bool {
52+
guard case .boolean = self else { return false }
53+
return true
54+
}
55+
56+
public var isString: Bool {
57+
guard case .string = self else { return false }
58+
return true
59+
}
60+
61+
public var isNumber: Bool {
62+
guard case .number = self else { return false }
63+
return true
64+
}
65+
66+
public var isObject: Bool {
67+
guard case .object = self else { return false }
68+
return true
69+
}
70+
71+
public var isNull: Bool {
72+
return self == .null
73+
}
74+
75+
public var isUndefined: Bool {
76+
return self == .undefined
77+
}
78+
79+
public var isFunction: Bool {
80+
guard case .function = self else { return false }
81+
return true
82+
}
83+
}
84+
85+
extension JSValue {
86+
public func fromJSValue<Type>() -> Type where Type: JSValueDecodable {
87+
return Type(jsValue: self)
88+
}
5289
}
5390

5491
extension JSValue {
@@ -64,7 +101,13 @@ extension JSValue: ExpressibleByStringLiteral {
64101
}
65102

66103
extension JSValue: ExpressibleByIntegerLiteral {
67-
public init(integerLiteral value: Double) {
104+
public init(integerLiteral value: Int32) {
105+
self = .number(Double(value))
106+
}
107+
}
108+
109+
extension JSValue: ExpressibleByFloatLiteral {
110+
public init(floatLiteral value: Double) {
68111
self = .number(value)
69112
}
70113
}
@@ -98,3 +141,20 @@ public func setJSValue(this: JSObjectRef, index: Int32, value: JSValue) {
98141
rawValue.payload1, rawValue.payload2, rawValue.payload3)
99142
}
100143
}
144+
145+
extension JSValue {
146+
147+
public func instanceof(_ constructor: String) -> Bool {
148+
149+
switch self {
150+
case .boolean, .string, .number:
151+
return false
152+
case .object(let ref):
153+
return ref.instanceof(constructor)
154+
case .function(let ref):
155+
return ref.instanceof(constructor)
156+
case .null, .undefined:
157+
fatalError()
158+
}
159+
}
160+
}

0 commit comments

Comments
 (0)