Skip to content

Commit cfba93d

Browse files
Applied the review feedback: hooked tracing to the “Tracing” trait and removed the extra compdef, made JSClosure tracing use String file IDs, and avoided unknown placeholders for method names by allowing nil method names.
- Updated trait name and removed redundant define; tracing is now gated by trait “Tracing” and uses `#if Tracing` (`Package.swift`). - Hooks now accept optional method names and closure file IDs as `String`, with all tracing conditionals using the trait flag (`Sources/JavaScriptKit/JSTracing.swift`, `Sources/JavaScriptKit/FundamentalObjects/JSObject+CallAsFunction.swift`, `Sources/JavaScriptKit/FundamentalObjects/JSThrowingFunction.swift`, `Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift`, `Sources/JavaScriptKit/FundamentalObjects/JSObject.swift`). - Documentation updated to instruct enabling tracing via `--traits Tracing` (`Sources/JavaScriptKit/Documentation.docc/Articles/Debugging.md`). Tests not run. Suggest running `swift build --traits Tracing` to verify the tracing variant compiles.
1 parent b939aad commit cfba93d

File tree

7 files changed

+33
-38
lines changed

7 files changed

+33
-38
lines changed

Package.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ let useLegacyResourceBundling =
99
Context.environment["JAVASCRIPTKIT_USE_LEGACY_RESOURCE_BUNDLING"].flatMap(Bool.init) ?? false
1010

1111
let tracingTrait = Trait(
12-
name: "JavaScriptKitTracing",
12+
name: "Tracing",
1313
description: "Enable opt-in Swift <-> JavaScript bridge tracing hooks.",
1414
enabledTraits: []
1515
)
@@ -57,8 +57,7 @@ let package = Package(
5757
.unsafeFlags(["-fdeclspec"])
5858
] : nil,
5959
swiftSettings: [
60-
.enableExperimentalFeature("Extern"),
61-
.define("JAVASCRIPTKIT_ENABLE_TRACING", .when(traits: ["JavaScriptKitTracing"])),
60+
.enableExperimentalFeature("Extern")
6261
]
6362
+ (shouldBuildForEmbedded
6463
? [

Sources/JavaScriptKit/Documentation.docc/Articles/Debugging.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ See [the DevTools team's official introduction](https://developer.chrome.com/blo
6060

6161
## Bridge Call Tracing
6262

63-
Enable the `JavaScriptKitTracing` package trait to compile lightweight hook points for Swift <-> JavaScript calls. Tracing is off by default and adds no runtime overhead unless the trait is enabled:
63+
Enable the `Tracing` package trait to compile lightweight hook points for Swift <-> JavaScript calls. Tracing is off by default and adds no runtime overhead unless the trait is enabled:
6464

6565
```bash
66-
swift build --traits JavaScriptKitTracing
66+
swift build --traits Tracing
6767
```
6868

6969
The hooks are invoked at the start and end of each bridge crossing without collecting data for you. For example:

Sources/JavaScriptKit/FundamentalObjects/JSClosure.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class JSOneshotClosure: JSObject, JSClosureProtocol {
1919
private var hostFuncRef: JavaScriptHostFuncRef = 0
2020

2121
public init(
22-
file: StaticString = #fileID,
22+
file: String = #fileID,
2323
line: UInt32 = #line,
2424
_ body: @escaping (sending [JSValue]) -> JSValue
2525
) {
@@ -28,7 +28,7 @@ public class JSOneshotClosure: JSObject, JSClosureProtocol {
2828

2929
// 2. Create a new JavaScript function which calls the given Swift function.
3030
hostFuncRef = JavaScriptHostFuncRef(bitPattern: ObjectIdentifier(self))
31-
_id = withExtendedLifetime(JSString(String(file))) { file in
31+
_id = withExtendedLifetime(JSString(file)) { file in
3232
swjs_create_oneshot_function(hostFuncRef, line, file.asInternalJSRef())
3333
}
3434

@@ -60,7 +60,7 @@ public class JSOneshotClosure: JSObject, JSClosureProtocol {
6060
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
6161
public static func async(
6262
priority: TaskPriority? = nil,
63-
file: StaticString = #fileID,
63+
file: String = #fileID,
6464
line: UInt32 = #line,
6565
_ body: @escaping (sending [JSValue]) async throws(JSException) -> JSValue
6666
) -> JSOneshotClosure {
@@ -79,7 +79,7 @@ public class JSOneshotClosure: JSObject, JSClosureProtocol {
7979
public static func async(
8080
executorPreference taskExecutor: (any TaskExecutor)? = nil,
8181
priority: TaskPriority? = nil,
82-
file: StaticString = #fileID,
82+
file: String = #fileID,
8383
line: UInt32 = #line,
8484
_ body: @escaping (sending [JSValue]) async throws(JSException) -> JSValue
8585
) -> JSOneshotClosure {
@@ -122,7 +122,7 @@ public class JSClosure: JSObject, JSClosureProtocol {
122122
struct Entry {
123123
let object: JSObject
124124
let body: (sending [JSValue]) -> JSValue
125-
let fileID: StaticString
125+
let fileID: String
126126
let line: UInt32
127127
}
128128
private var storage: [JavaScriptHostFuncRef: Entry] = [:]
@@ -160,7 +160,7 @@ public class JSClosure: JSObject, JSClosureProtocol {
160160
}
161161

162162
public init(
163-
file: StaticString = #fileID,
163+
file: String = #fileID,
164164
line: UInt32 = #line,
165165
_ body: @escaping (sending [JSValue]) -> JSValue
166166
) {
@@ -169,7 +169,7 @@ public class JSClosure: JSObject, JSClosureProtocol {
169169

170170
// 2. Create a new JavaScript function which calls the given Swift function.
171171
hostFuncRef = JavaScriptHostFuncRef(bitPattern: ObjectIdentifier(self))
172-
_id = withExtendedLifetime(JSString(String(file))) { file in
172+
_id = withExtendedLifetime(JSString(file)) { file in
173173
swjs_create_function(hostFuncRef, line, file.asInternalJSRef())
174174
}
175175

@@ -198,7 +198,7 @@ public class JSClosure: JSObject, JSClosureProtocol {
198198
@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
199199
public static func async(
200200
priority: TaskPriority? = nil,
201-
file: StaticString = #fileID,
201+
file: String = #fileID,
202202
line: UInt32 = #line,
203203
_ body: @escaping @isolated(any) (sending [JSValue]) async throws(JSException) -> JSValue
204204
) -> JSClosure {
@@ -217,7 +217,7 @@ public class JSClosure: JSObject, JSClosureProtocol {
217217
public static func async(
218218
executorPreference taskExecutor: (any TaskExecutor)? = nil,
219219
priority: TaskPriority? = nil,
220-
file: StaticString = #fileID,
220+
file: String = #fileID,
221221
line: UInt32 = #line,
222222
_ body: @escaping (sending [JSValue]) async throws(JSException) -> JSValue
223223
) -> JSClosure {
@@ -338,7 +338,7 @@ func _call_host_function_impl(
338338
guard let entry = JSClosure.sharedClosures.wrappedValue[hostFuncRef] else {
339339
return true
340340
}
341-
#if JAVASCRIPTKIT_ENABLE_TRACING
341+
#if Tracing
342342
let traceEnd = JSTracingHooks.beginJSClosureCall(
343343
JSTracing.JSClosureCallInfo(fileID: entry.fileID, line: UInt(entry.line))
344344
)
@@ -347,7 +347,7 @@ func _call_host_function_impl(
347347
for i in 0..<Int(argc) {
348348
arguments.append(argv[i].jsValue)
349349
}
350-
#if JAVASCRIPTKIT_ENABLE_TRACING
350+
#if Tracing
351351
defer { traceEnd?() }
352352
#endif
353353
let result = entry.body(arguments)

Sources/JavaScriptKit/FundamentalObjects/JSObject+CallAsFunction.swift

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ extension JSObject {
5252
/// - Parameter arguments: Arguments to be passed to this constructor function.
5353
/// - Returns: A new instance of this constructor.
5454
public func new(arguments: [ConvertibleToJSValue]) -> JSObject {
55-
#if JAVASCRIPTKIT_ENABLE_TRACING
55+
#if Tracing
5656
let jsValues = arguments.map { $0.jsValue }
5757
return new(arguments: jsValues)
5858
#else
@@ -105,7 +105,7 @@ extension JSObject {
105105
/// - Parameter arguments: Arguments to be passed to this constructor function.
106106
/// - Returns: A new instance of this constructor.
107107
public func new(arguments: [JSValue]) -> JSObject {
108-
#if JAVASCRIPTKIT_ENABLE_TRACING
108+
#if Tracing
109109
let traceEnd = JSTracingHooks.beginJSCall(.function(function: self, arguments: arguments))
110110
defer { traceEnd?() }
111111
#endif
@@ -122,11 +122,11 @@ extension JSObject {
122122
}
123123

124124
final func invokeNonThrowingJSFunction(arguments: [JSValue]) -> RawJSValue {
125-
#if JAVASCRIPTKIT_ENABLE_TRACING
125+
#if Tracing
126126
let traceEnd = JSTracingHooks.beginJSCall(.function(function: self, arguments: arguments))
127127
#endif
128128
let result = arguments.withRawJSValues { invokeNonThrowingJSFunction(rawValues: $0) }
129-
#if JAVASCRIPTKIT_ENABLE_TRACING
129+
#if Tracing
130130
traceEnd?()
131131
#endif
132132
return result
@@ -135,17 +135,13 @@ extension JSObject {
135135
final func invokeNonThrowingJSFunction(
136136
arguments: [JSValue],
137137
this: JSObject
138-
#if JAVASCRIPTKIT_ENABLE_TRACING
138+
#if Tracing
139139
, tracedMethodName: String? = nil
140140
#endif
141141
) -> RawJSValue {
142-
#if JAVASCRIPTKIT_ENABLE_TRACING
142+
#if Tracing
143143
let traceEnd = JSTracingHooks.beginJSCall(
144-
.method(
145-
receiver: this,
146-
methodName: tracedMethodName ?? "<unknown>",
147-
arguments: arguments
148-
)
144+
.method(receiver: this, methodName: tracedMethodName, arguments: arguments)
149145
)
150146
#endif
151147
let result = arguments.withRawJSValues {
@@ -154,15 +150,15 @@ extension JSObject {
154150
this: this
155151
)
156152
}
157-
#if JAVASCRIPTKIT_ENABLE_TRACING
153+
#if Tracing
158154
traceEnd?()
159155
#endif
160156
return result
161157
}
162158

163159
#if !hasFeature(Embedded)
164160
final func invokeNonThrowingJSFunction(arguments: [ConvertibleToJSValue]) -> RawJSValue {
165-
#if JAVASCRIPTKIT_ENABLE_TRACING
161+
#if Tracing
166162
let jsValues = arguments.map { $0.jsValue }
167163
return invokeNonThrowingJSFunction(arguments: jsValues)
168164
#else
@@ -173,11 +169,11 @@ extension JSObject {
173169
final func invokeNonThrowingJSFunction(
174170
arguments: [ConvertibleToJSValue],
175171
this: JSObject
176-
#if JAVASCRIPTKIT_ENABLE_TRACING
172+
#if Tracing
177173
, tracedMethodName: String? = nil
178174
#endif
179175
) -> RawJSValue {
180-
#if JAVASCRIPTKIT_ENABLE_TRACING
176+
#if Tracing
181177
let jsValues = arguments.map { $0.jsValue }
182178
return invokeNonThrowingJSFunction(
183179
arguments: jsValues,

Sources/JavaScriptKit/FundamentalObjects/JSObject.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public class JSObject: Equatable, ExpressibleByDictionaryLiteral {
9494
public subscript(_ name: String) -> ((ConvertibleToJSValue...) -> JSValue)? {
9595
guard let function = self[name].function else { return nil }
9696
return { (arguments: ConvertibleToJSValue...) in
97-
#if JAVASCRIPTKIT_ENABLE_TRACING
97+
#if Tracing
9898
return function.invokeNonThrowingJSFunction(
9999
arguments: arguments,
100100
this: self,
@@ -120,7 +120,7 @@ public class JSObject: Equatable, ExpressibleByDictionaryLiteral {
120120
public subscript(_ name: JSString) -> ((ConvertibleToJSValue...) -> JSValue)? {
121121
guard let function = self[name].function else { return nil }
122122
return { (arguments: ConvertibleToJSValue...) in
123-
#if JAVASCRIPTKIT_ENABLE_TRACING
123+
#if Tracing
124124
return function.invokeNonThrowingJSFunction(
125125
arguments: arguments,
126126
this: self,

Sources/JavaScriptKit/FundamentalObjects/JSThrowingFunction.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ private func invokeJSFunction(
7777
arguments: [ConvertibleToJSValue],
7878
this: JSObject?
7979
) throws -> JSValue {
80-
#if JAVASCRIPTKIT_ENABLE_TRACING
80+
#if Tracing
8181
let jsValues = arguments.map { $0.jsValue }
8282
let traceEnd = JSTracingHooks.beginJSCall(
8383
this.map {
84-
.method(receiver: $0, methodName: "<unknown>", arguments: jsValues)
84+
.method(receiver: $0, methodName: nil, arguments: jsValues)
8585
} ?? .function(function: jsFunc, arguments: jsValues)
8686
)
8787
defer { traceEnd?() }

Sources/JavaScriptKit/JSTracing.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
#if JAVASCRIPTKIT_ENABLE_TRACING
1+
#if Tracing
22

33
/// Hooks for tracing Swift <-> JavaScript bridge calls.
44
public struct JSTracing {
55
public static let `default` = JSTracing()
66

77
public enum JSCallInfo {
88
case function(function: JSObject, arguments: [JSValue])
9-
case method(receiver: JSObject, methodName: String, arguments: [JSValue])
9+
case method(receiver: JSObject, methodName: String?, arguments: [JSValue])
1010
}
1111

1212
/// Register a hook for Swift to JavaScript calls.
@@ -24,7 +24,7 @@ public struct JSTracing {
2424

2525
public struct JSClosureCallInfo {
2626
/// The file identifier where the called `JSClosure` was created.
27-
public let fileID: StaticString
27+
public let fileID: String
2828
/// The line number where the called `JSClosure` was created.
2929
public let line: UInt
3030
}

0 commit comments

Comments
 (0)