Skip to content

Instantly share code, notes, and snippets.

@adamloving
Created June 25, 2015 19:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamloving/da5fc0917cb5f0d22519 to your computer and use it in GitHub Desktop.
Save adamloving/da5fc0917cb5f0d22519 to your computer and use it in GitHub Desktop.
running Javascript from Swift using JavaScriptCore (try this in a playground)
import JavaScriptCore
let context = JSContext()
context.evaluateScript("var num = 5 + 5")
context.evaluateScript("var names = ['Grace', 'Ada', 'Margaret']")
context.evaluateScript("var triple = function(value) { return value * 3 }")
context.evaluateScript("names.sort()")
let tripleNum: JSValue = context.evaluateScript("triple(num)")
let x = context.evaluateScript(
"x = {a: 1, b: ['blah'], c: { cat: 1.0 }}")
x.valueForProperty("a")
x.valueForProperty("b")
let o: JSValue = x.valueForProperty("c")
@adamloving
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment