Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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 (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-09-26T08:25:05.000Z (4 months ago)
- Last Synced: 2024-10-15T08:37:33.242Z (4 months ago)
- Topics: codable, ecmascript, javascript, javascriptcore, swift
- Language: Swift
- Homepage:
- Size: 87.9 KB
- Stars: 19
- Watchers: 2
- Forks: 3
- 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())
```