https://github.com/theolampert/ecmaswift
A tiny Javascript runtime for iOS and macOS
https://github.com/theolampert/ecmaswift
codable ecmascript javascript javascriptcore swift
Last synced: 3 months ago
JSON representation
A tiny Javascript runtime for iOS and macOS
- Host: GitHub
- URL: https://github.com/theolampert/ecmaswift
- Owner: theolampert
- License: mit
- Created: 2023-05-13T06:35:28.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-09-26T08:25:05.000Z (9 months ago)
- Last Synced: 2025-03-14T06:46:57.031Z (3 months ago)
- Topics: codable, ecmascript, javascript, javascriptcore, swift
- Language: Swift
- Homepage:
- Size: 87.9 KB
- Stars: 27
- Watchers: 2
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
### ECMASwift
ECMASwift intends to implement a tiny subset of Browser APIs (mostly networking related) to make code sharing between iOS/macOS apps and the web easier.
### Features
ECMASwift exposes the following browser APIs to JavascriptCore, some of these are incomplete, contributions welcome.
- Blob
- Console
- Crypto
- Fetch
- FormData
- Headers
- Request
- TextEncoder
- Timers
- URL
- URLSearchParams### Examples
In Javascript:
```js
// Define an async function to fetch some dummy data in Javascript
async function fetchProducts() {
try {
const res = await fetch("https://google.com")
return await res.json()
} catch(error) {
console.log(error)
}
}
```In Swift:
```swift
import ECMASwift
import JavaScriptCore// Initialise the runtime
let runtime = JSRuntime()// Load the javascript source file defined above, alternatively JS can be written inline.
let javascriptSource = try! String(contentsOfFile: "./example.js")// Evaluate the script
_ = runtime.context.evaluateScript(javascriptSource)// Call the `fetchProducts` function defined in the source file.
let result = try! await runtime.context.callAsyncFunction(key: "fetchProducts")// Print the result
print(result.toString())
```