Skip to content

Commit 7f2f7c6

Browse files
committed
less embedded conditions and added comments
1 parent 77bbc8c commit 7f2f7c6

File tree

4 files changed

+12
-13
lines changed

4 files changed

+12
-13
lines changed

Sources/JavaScriptKit/FundamentalObjects/JSFunction.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ public class JSFunction: JSObject, _JSFunctionProtocol {
8484
public var `throws`: JSThrowingFunction {
8585
JSThrowingFunction(self)
8686
}
87+
#endif
8788

88-
#else
8989
@discardableResult
9090
public func callAsFunction(arguments: [JSValue]) -> JSValue {
9191
invokeNonThrowingJSFunction(arguments: arguments).jsValue
@@ -98,7 +98,6 @@ public class JSFunction: JSObject, _JSFunctionProtocol {
9898
}
9999
}
100100
}
101-
#endif
102101

103102
@available(*, unavailable, message: "Please use JSClosure instead")
104103
public static func from(_: @escaping ([JSValue]) -> JSValue) -> JSFunction {
@@ -109,15 +108,15 @@ public class JSFunction: JSObject, _JSFunctionProtocol {
109108
.function(self)
110109
}
111110

112-
#if hasFeature(Embedded)
113111
final func invokeNonThrowingJSFunction(arguments: [JSValue]) -> RawJSValue {
114112
arguments.withRawJSValues { invokeNonThrowingJSFunction(rawValues: $0) }
115113
}
116114

117115
final func invokeNonThrowingJSFunction(arguments: [JSValue], this: JSObject) -> RawJSValue {
118116
arguments.withRawJSValues { invokeNonThrowingJSFunction(rawValues: $0, this: this) }
119117
}
120-
#else
118+
119+
#if !hasFeature(Embedded)
121120
final func invokeNonThrowingJSFunction(arguments: [ConvertibleToJSValue]) -> RawJSValue {
122121
arguments.withRawJSValues { invokeNonThrowingJSFunction(rawValues: $0) }
123122
}
@@ -164,11 +163,14 @@ public class JSFunction: JSObject, _JSFunctionProtocol {
164163
}
165164
}
166165

166+
/// Internal protocol to support generic arguments for `JSFunction`.
167+
///
168+
/// In Swift Embedded, non-final classes cannot have generic methods.
167169
public protocol _JSFunctionProtocol: JSFunction {}
168170

169171
#if hasFeature(Embedded)
172+
// NOTE: once embedded supports variadic generics, we can remove these overloads
170173
public extension _JSFunctionProtocol {
171-
// hand-made "varidacs" for Embedded
172174

173175
@discardableResult
174176
func callAsFunction(this: JSObject) -> JSValue {

Sources/JavaScriptKit/FundamentalObjects/JSObject.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,10 +231,14 @@ public class JSThrowingObject {
231231
}
232232
#endif
233233

234+
/// Internal protocol to support generic arguments for `JSObject`.
235+
///
236+
/// In Swift Embedded, non-final classes cannot have generic methods.
234237
public protocol _JSObjectProtocol: JSObject {
235238
}
236239

237240
#if hasFeature(Embedded)
241+
// NOTE: once embedded supports variadic generics, we can remove these overloads
238242
public extension _JSObjectProtocol {
239243
@_disfavoredOverload
240244
subscript(dynamicMember name: String) -> (() -> JSValue)? {

Sources/JavaScriptKit/FundamentalObjects/JSString.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,9 @@ public struct JSString: LosslessStringConvertible, Equatable {
3131
var bytesRef: JavaScriptObjectRef = 0
3232
let bytesLength = Int(swjs_encode_string(jsRef, &bytesRef))
3333
// +1 for null terminator
34-
// TODO: revert this back to malloc and free
35-
// let buffer = malloc(Int(bytesLength + 1))!.assumingMemoryBound(to: UInt8.self)
36-
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: Int(bytesLength + 1))
34+
let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: bytesLength + 1)
3735
defer {
3836
buffer.deallocate()
39-
// free(buffer)
4037
swjs_release(bytesRef)
4138
}
4239
swjs_load_string(bytesRef, buffer)

Sources/JavaScriptKit/FundamentalObjects/JSSymbol.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ public class JSSymbol: JSObject {
1010

1111
public init(_ description: JSString) {
1212
// can’t do `self =` so we have to get the ID manually
13-
#if hasFeature(Embedded)
1413
let result = Symbol.invokeNonThrowingJSFunction(arguments: [description.jsValue])
15-
#else
16-
let result = Symbol.invokeNonThrowingJSFunction(arguments: [description])
17-
#endif
1814
precondition(result.kind == .symbol)
1915
super.init(id: UInt32(result.payload1))
2016
}

0 commit comments

Comments
 (0)