Skip to content

Allow an evaluator to be set. #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/Substrata/DataBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ extension JSDataBridge {
// we already have one fool!
if self.engine != nil { return }
self.engine = engine
engine.evaluate(script: "var \(JSDataBridge.dataBridgeKey) = {};")
engine.evaluate(script: "var \(JSDataBridge.dataBridgeKey) = {};", evaluator: "JSDataBridge.setEngine")
}
}
18 changes: 14 additions & 4 deletions Sources/Substrata/Engine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,28 @@ public class JSEngine {
}

if let jsSource = jsSource {
evaluate(script: jsSource)
evaluate(script: jsSource, evaluator: "JSEngine.loadBundle")
}

completion?(jsError)
}

@discardableResult
public func evaluate(script: String) -> JSConvertible? {
return evaluate(script: script, evaluator: Constants.Evaluator)
}

/// Evaluates a script and returns a result.
///
/// params:
/// - script: The script to evaluate
/// - evaluator: An optional identifying name of the evaluator, useful in debugging.
@discardableResult
public func evaluate(script: String, evaluator: String) -> JSConvertible? {
var outerResult: JSConvertible? = nil
context.performThreadSafe { [weak self] in
guard let self else { return }
let result = JS_Eval(context.ref, script, script.lengthOfBytes(using: .utf8), Constants.Evaluator, 0)
let result = JS_Eval(context.ref, script, script.lengthOfBytes(using: .utf8), evaluator, 0)
outerResult = result.toJSConvertible(context: context)
result.free(context)
}
Expand All @@ -86,7 +96,7 @@ public class JSEngine {

public func value(for keyPath: String) -> JSConvertible? {
guard keyPath.count > 0 else { return nil }
let result = evaluate(script: keyPath)
let result = evaluate(script: keyPath, evaluator: "JSEngine.value")
return result
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/Substrata/Objects/Builtins.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ internal class Builtins {
}
return output;
}
""")
""", evaluator: "JSEngine.Builtins")

if let getInstanceMethodNames = engine.evaluate(script: "_getInstanceMethodNames")?.typed(as: JSFunction.self) {
if let getInstanceMethodNames = engine.evaluate(script: "_getInstanceMethodNames", evaluator: "JSEngine.Builtins.evaluate")?.typed(as: JSFunction.self) {
functions["_getInstanceMethodNames"] = getInstanceMethodNames
}
}
Expand Down