Skip to content

Commit 565608b

Browse files
committed
feat: add JSTracing hook for tracing property get and set
1 parent 3eff48a commit 565608b

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

Sources/JavaScriptKit/FundamentalObjects/JSObject.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,18 @@ public class JSObject: Equatable, ExpressibleByDictionaryLiteral {
153153
public subscript(_ name: String) -> JSValue {
154154
get {
155155
assertOnOwnerThread(hint: "reading '\(name)' property")
156+
#if Tracing
157+
let traceEnd = JSTracingHooks.beginJSCall(.propertyGet(receiver: self, propertyName: name))
158+
defer { traceEnd?() }
159+
#endif
156160
return getJSValue(this: self, name: JSString(name))
157161
}
158162
set {
159163
assertOnOwnerThread(hint: "writing '\(name)' property")
164+
#if Tracing
165+
let traceEnd = JSTracingHooks.beginJSCall(.propertySet(receiver: self, propertyName: name, value: newValue))
166+
defer { traceEnd?() }
167+
#endif
160168
setJSValue(this: self, name: JSString(name), value: newValue)
161169
}
162170
}
@@ -167,10 +175,18 @@ public class JSObject: Equatable, ExpressibleByDictionaryLiteral {
167175
public subscript(_ name: JSString) -> JSValue {
168176
get {
169177
assertOnOwnerThread(hint: "reading '<<JSString>>' property")
178+
#if Tracing
179+
let traceEnd = JSTracingHooks.beginJSCall(.propertyGet(receiver: self, propertyName: String(name)))
180+
defer { traceEnd?() }
181+
#endif
170182
return getJSValue(this: self, name: name)
171183
}
172184
set {
173185
assertOnOwnerThread(hint: "writing '<<JSString>>' property")
186+
#if Tracing
187+
let traceEnd = JSTracingHooks.beginJSCall(.propertySet(receiver: self, propertyName: String(name), value: newValue))
188+
defer { traceEnd?() }
189+
#endif
174190
setJSValue(this: self, name: name, value: newValue)
175191
}
176192
}
@@ -181,10 +197,18 @@ public class JSObject: Equatable, ExpressibleByDictionaryLiteral {
181197
public subscript(_ index: Int) -> JSValue {
182198
get {
183199
assertOnOwnerThread(hint: "reading '\(index)' property")
200+
#if Tracing
201+
let traceEnd = JSTracingHooks.beginJSCall(.propertyGet(receiver: self, propertyName: String(index)))
202+
defer { traceEnd?() }
203+
#endif
184204
return getJSValue(this: self, index: Int32(index))
185205
}
186206
set {
187207
assertOnOwnerThread(hint: "writing '\(index)' property")
208+
#if Tracing
209+
let traceEnd = JSTracingHooks.beginJSCall(.propertySet(receiver: self, propertyName: String(index), value: newValue))
210+
defer { traceEnd?() }
211+
#endif
188212
setJSValue(this: self, index: Int32(index), value: newValue)
189213
}
190214
}
@@ -195,10 +219,18 @@ public class JSObject: Equatable, ExpressibleByDictionaryLiteral {
195219
public subscript(_ name: JSSymbol) -> JSValue {
196220
get {
197221
assertOnOwnerThread(hint: "reading '<<JSSymbol>>' property")
222+
#if Tracing
223+
let traceEnd = JSTracingHooks.beginJSCall(.propertyGet(receiver: self, propertyName: "<<JSSymbol>>"))
224+
defer { traceEnd?() }
225+
#endif
198226
return getJSValue(this: self, symbol: name)
199227
}
200228
set {
201229
assertOnOwnerThread(hint: "writing '<<JSSymbol>>' property")
230+
#if Tracing
231+
let traceEnd = JSTracingHooks.beginJSCall(.propertySet(receiver: self, propertyName: "<<JSSymbol>>", value: newValue))
232+
defer { traceEnd?() }
233+
#endif
202234
setJSValue(this: self, symbol: name, value: newValue)
203235
}
204236
}

Sources/JavaScriptKit/JSTracing.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ public struct JSTracing: Sendable {
77
public enum JSCallInfo {
88
case function(function: JSObject, arguments: [JSValue])
99
case method(receiver: JSObject, methodName: String?, arguments: [JSValue])
10+
case propertyGet(receiver: JSObject, propertyName: String)
11+
case propertySet(receiver: JSObject, propertyName: String, value: JSValue)
1012
}
1113

1214
/// Register a hook for Swift to JavaScript calls.

0 commit comments

Comments
 (0)