Skip to content

Commit 39b15f4

Browse files
committed
Remove JSFunctionRef.apply and JSFunctionRef.new
1 parent f11fc32 commit 39b15f4

File tree

5 files changed

+34
-34
lines changed

5 files changed

+34
-34
lines changed

IntegrationTests/TestSuites/Sources/BenchmarkTests/main.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ let objectHeap = Benchmark("Object heap")
2525

2626
let global = JSObjectRef.global
2727
let Object = global.Object.function!
28-
global.objectHeapDummy = .object(Object.new())
28+
global.objectHeapDummy = .object(Object(.new))
2929
objectHeap.testSuite("Increment and decrement RC") {
3030
for _ in 0 ..< 100 {
3131
_ = global.objectHeapDummy

IntegrationTests/TestSuites/Sources/PrimaryTests/main.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -225,15 +225,15 @@ New_Object_Construction: do {
225225
// }
226226
// ```
227227
let objectConstructor = try expectFunction(getJSValue(this: .global, name: "Animal"))
228-
let cat1 = objectConstructor.new("Tama", 3, true)
228+
let cat1 = objectConstructor(new: "Tama", 3, true)
229229
try expectEqual(getJSValue(this: cat1, name: "name"), .string("Tama"))
230230
try expectEqual(getJSValue(this: cat1, name: "age"), .number(3))
231231
try expectEqual(JSObjectRef.instanceof(cat1, constructor: objectConstructor), true)
232232
try expectEqual(JSObjectRef.instanceof(cat1, constructor: try expectFunction(getJSValue(this: .global, name: "Array"))), false)
233233
let cat1Bark = try expectFunction(getJSValue(this: cat1, name: "bark"))
234234
try expectEqual(cat1Bark(), .string("nyan"))
235235

236-
let dog1 = objectConstructor.new("Pochi", 3, false)
236+
let dog1 = objectConstructor(new: "Pochi", 3, false)
237237
let dog1Bark = try expectFunction(getJSValue(this: dog1, name: "bark"))
238238
try expectEqual(dog1Bark(), .string("wan"))
239239
} catch {
@@ -255,14 +255,14 @@ Call_Function_With_This: do {
255255
// }
256256
// ```
257257
let objectConstructor = try expectFunction(getJSValue(this: .global, name: "Animal"))
258-
let cat1 = objectConstructor.new("Tama", 3, true)
258+
let cat1 = objectConstructor(new: "Tama", 3, true)
259259
let getIsCat = try expectFunction(getJSValue(this: cat1, name: "getIsCat"))
260260

261261
// Direct call without this
262262
try expectEqual(getIsCat(), .undefined)
263263

264264
// Call with this
265-
let gotIsCat = getIsCat.apply(this: cat1)
265+
let gotIsCat = getIsCat(this: cat1)
266266
try expectEqual(gotIsCat, .boolean(true))
267267

268268
} catch {

Sources/JavaScriptKit/JSFunction.swift

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,40 @@
11
import _CJavaScriptKit
22

3-
@dynamicCallable
3+
public enum _JSFunctionConstructorSymbol {
4+
case new
5+
}
6+
47
public class JSFunctionRef: JSObjectRef {
58
@discardableResult
6-
public func dynamicallyCall(withArguments arguments: [JSValueConvertible]) -> JSValue {
7-
let result = arguments.withRawJSValues { rawValues -> RawJSValue in
9+
public func callAsFunction(this: JSObjectRef? = nil, _ arguments: [JSValueConvertible]) -> JSValue {
10+
let result = arguments.withRawJSValues { rawValues in
811
rawValues.withUnsafeBufferPointer { bufferPointer -> RawJSValue in
912
let argv = bufferPointer.baseAddress
1013
let argc = bufferPointer.count
1114
var result = RawJSValue()
12-
_call_function(
13-
self.id, argv, Int32(argc),
14-
&result.kind, &result.payload1, &result.payload2, &result.payload3
15-
)
15+
if let thisId = this?.id {
16+
_call_function_with_this(thisId,
17+
self.id, argv, Int32(argc),
18+
&result.kind, &result.payload1, &result.payload2, &result.payload3)
19+
} else {
20+
_call_function(
21+
self.id, argv, Int32(argc),
22+
&result.kind, &result.payload1, &result.payload2, &result.payload3
23+
)
24+
}
1625
return result
1726
}
1827
}
1928
return JSValue(from: result)
2029
}
2130

22-
public func apply(this: JSObjectRef, arguments: JSValueConvertible...) -> JSValue {
23-
apply(this: this, argumentList: arguments)
24-
}
25-
26-
public func apply(this: JSObjectRef, argumentList: [JSValueConvertible]) -> JSValue {
27-
let result = argumentList.withRawJSValues { rawValues in
28-
rawValues.withUnsafeBufferPointer { bufferPointer -> RawJSValue in
29-
let argv = bufferPointer.baseAddress
30-
let argc = bufferPointer.count
31-
var result = RawJSValue()
32-
_call_function_with_this(this.id,
33-
self.id, argv, Int32(argc),
34-
&result.kind, &result.payload1, &result.payload2, &result.payload3)
35-
return result
36-
}
37-
}
38-
return JSValue(from: result)
31+
@discardableResult
32+
public func callAsFunction(this: JSObjectRef? = nil, _ arguments: JSValueConvertible...) -> JSValue {
33+
self(this: this, arguments)
3934
}
4035

41-
public func new(_ arguments: JSValueConvertible...) -> JSObjectRef {
42-
return arguments.withRawJSValues { rawValues in
36+
public func callAsFunction(new arguments: JSValueConvertible...) -> JSObjectRef {
37+
arguments.withRawJSValues { rawValues in
4338
rawValues.withUnsafeBufferPointer { bufferPointer in
4439
let argv = bufferPointer.baseAddress
4540
let argc = bufferPointer.count
@@ -52,6 +47,11 @@ public class JSFunctionRef: JSObjectRef {
5247
}
5348
}
5449
}
50+
public func callAsFunction(_: _JSFunctionConstructorSymbol) -> JSObjectRef {
51+
var resultObj = JavaScriptObjectRef()
52+
_call_new(self.id, nil, 0, &resultObj)
53+
return JSObjectRef(id: resultObj)
54+
}
5555

5656
@available(*, unavailable, message: "Please use JSClosure instead")
5757
public static func from(_: @escaping ([JSValue]) -> JSValue) -> JSFunctionRef {

Sources/JavaScriptKit/JSObject.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class JSObjectRef: Equatable {
1111
public subscript(dynamicMember name: String) -> ((JSValueConvertible...) -> JSValue)? {
1212
guard let function = self[name].function else { return nil }
1313
return { (arguments: JSValueConvertible...) in
14-
function.apply(this: self, argumentList: arguments)
14+
function(this: self, arguments)
1515
}
1616
}
1717

Sources/JavaScriptKit/JSValueConvertible.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ extension Dictionary where Value: JSValueConvertible, Key == String {
7070

7171
extension Dictionary: JSValueConvertible where Value == JSValueConvertible, Key == String {
7272
public subscript(jsValue _: ()) -> JSValue {
73-
let object = Object.new()
73+
let object = Object(.new)
7474
for (key, value) in self {
7575
object[key] = JSValue(from: value)
7676
}
@@ -88,7 +88,7 @@ extension Array where Element: JSValueConvertible {
8888

8989
extension Array: JSValueConvertible where Element == JSValueConvertible {
9090
public subscript(jsValue _: ()) -> JSValue {
91-
let array = Array.new(count)
91+
let array = Array(new: count)
9292
for (index, element) in enumerated() {
9393
array[index] = JSValue(from: element)
9494
}

0 commit comments

Comments
 (0)