Skip to content

Commit

Permalink
Replace ReentrantQueue with RecursiveLock. (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
didiergarcia authored Mar 7, 2024
1 parent 3d8b1d8 commit a2912fe
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 46 deletions.
24 changes: 12 additions & 12 deletions Sources/Substrata/JSEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class JSEngine: JavascriptEngine {
public let context: JSContext = JSContext()

private var jsErrorHandler: JavascriptErrorHandler? = nil
private let jsQueue = ReentrantQueue(label: "com.segment.serial.javascript", key: "js")
private let jsQueue = RecursiveLock()

/**
Access the data bridge
Expand Down Expand Up @@ -123,7 +123,7 @@ public class JSEngine: JavascriptEngine {
}

if let jsSource = jsSource {
_ = jsQueue.reentrantSync { [weak self] in
_ = jsQueue.perform { [weak self] in
self?.context.evaluate(script: jsSource)
}
}
Expand All @@ -146,7 +146,7 @@ public class JSEngine: JavascriptEngine {
public func object(key: String) -> JSConvertible? {
var result: JSConvertible? = nil

jsQueue.reentrantSync { [weak self] in
jsQueue.perform { [weak self] in
guard let self = self else { return }
result = self.context[key].value
}
Expand All @@ -166,7 +166,7 @@ public class JSEngine: JavascriptEngine {
```
*/
public func setObject(key: String, value: JSConvertible) {
jsQueue.reentrantSync { [weak self] in
jsQueue.perform { [weak self] in
guard let self = self else { return }
let v = value.jsValue(context: self.context)
self.context[key] = v
Expand Down Expand Up @@ -212,7 +212,7 @@ public class JSEngine: JavascriptEngine {
```
*/
public func expose(name: String, classType: JavascriptClass.Type) throws {
try jsQueue.reentrantSync { [weak self] in
try jsQueue.perform { [weak self] in
guard let self = self else { return }
let obj = try JSObject(context: self.context, type: classType)
self.context[name] = obj
Expand All @@ -239,7 +239,7 @@ public class JSEngine: JavascriptEngine {
```
*/
public func expose(name: String, function: @escaping JavascriptFunction) {
jsQueue.reentrantSync { [weak self] in
jsQueue.perform { [weak self] in
guard let self = self else { return }
let fn = JSFunction(context: self.context) { [weak self] (context, this, params) in
let convertedParams = params.map { $0.value }
Expand Down Expand Up @@ -267,7 +267,7 @@ public class JSEngine: JavascriptEngine {
```
*/
public func extend(name: String, object: String, function: @escaping JavascriptFunction) throws {
try jsQueue.reentrantSync { [weak self] in
try jsQueue.perform { [weak self] in
guard let self = self else { return }
if let target = context[object] as? JSObject {
let fn = JSFunction(context: self.context) { context, this, params in
Expand Down Expand Up @@ -301,7 +301,7 @@ public class JSEngine: JavascriptEngine {
@discardableResult
public func call(functionName: String, params: JSConvertible?...) -> JSConvertible? {
var result: JSConvertible? = nil
jsQueue.reentrantSync { [weak self] in
jsQueue.perform { [weak self] in
guard let self = self else { return }
// let JS do the name resolution for us
if let f = self.context.evaluate(script: functionName).typed(JSFunction.self) {
Expand All @@ -328,7 +328,7 @@ public class JSEngine: JavascriptEngine {
@discardableResult
public func evaluate(script: String) -> JSConvertible? {
var result: JSPrimitive? = nil
jsQueue.reentrantSync { [weak self] in
jsQueue.perform { [weak self] in
guard let self = self else { return }
result = self.context.evaluate(script: script);
}
Expand All @@ -352,7 +352,7 @@ extension JSEngine {
}

private func setupExceptionHandler() {
jsQueue.reentrantSync { [weak self] in
jsQueue.perform { [weak self] in
guard let self = self else { return }
// setup javascript exception handling
self.context.exceptionHandler = { [weak self] (context, value) in
Expand All @@ -365,7 +365,7 @@ extension JSEngine {

private func setupDataBridge() {
bridge = JSDataBridge(engine: self)
jsQueue.reentrantSync { [weak self] in
jsQueue.perform { [weak self] in
// setup data bridge
guard let self = self else { return }
self.context.evaluate(script: "var \(JSDataBridge.dataBridgeKey) = {};")
Expand All @@ -377,7 +377,7 @@ extension JSEngine {
@discardableResult
public func syncRunEngine(closure: () -> JSConvertible?) -> JSConvertible? {
var result: JSConvertible? = nil
jsQueue.reentrantSync {
jsQueue.perform {
result = closure()
}
return result
Expand Down
26 changes: 26 additions & 0 deletions Sources/Substrata/Utilities/RecursiveLock.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// File.swift
//
//
// Created by Brandon Sneed on 6/9/22.
//

import Foundation

internal struct RecursiveLock {
private let lock = NSRecursiveLock()

@discardableResult
func perform<T>(closure: () -> T) -> T {
lock.lock()
defer { lock.unlock() }
return closure()
}

@discardableResult
func perform<T>(closure: () throws -> T ) throws -> T {
lock.lock()
defer { lock.unlock() }
return try closure()
}
}
33 changes: 0 additions & 33 deletions Sources/Substrata/Utilities/ReentrantQueue.swift

This file was deleted.

2 changes: 1 addition & 1 deletion Tests/SubstrataTests/EngineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class EngineTests: XCTestCase {
let r = engine.object(key: "a")
XCTAssertTrue(r is AnalyticsJS)

let o = engine.object(key: "a")?.typed(JSObject.self)
let o = engine.object(key: "a")?.typed(AnalyticsJS.self)
XCTAssertNotNil(o)

let result = engine.evaluate(script: "var annie = new Analytics('9876'); annie.track('booya');")
Expand Down

0 comments on commit a2912fe

Please sign in to comment.