https://github.com/buke/quickjs-go-polyfill
quickjs-go polyfill library
https://github.com/buke/quickjs-go-polyfill
Last synced: 15 days ago
JSON representation
quickjs-go polyfill library
- Host: GitHub
- URL: https://github.com/buke/quickjs-go-polyfill
- Owner: buke
- License: mit
- Created: 2022-06-25T10:10:42.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-02-01T13:20:00.000Z (about 1 year ago)
- Last Synced: 2025-02-01T14:25:07.640Z (about 1 year ago)
- Language: Go
- Homepage:
- Size: 86.9 KB
- Stars: 16
- Watchers: 1
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Polyfill for [quickjs-go](https://github.com/buke/quickjs-go)
English | [简体中文](README_zh-cn.md)
[](https://github.com/buke/quickjs-go-polyfill/actions?query=workflow%3ATest)
[](https://codecov.io/gh/buke/quickjs-go-polyfill)
[](https://goreportcard.com/report/github.com/buke/quickjs-go-polyfill)
[](https://pkg.go.dev/github.com/buke/quickjs-go-polyfill?tab=doc)
[](https://app.fossa.com/projects/git%2Bgithub.com%2Fbuke%2Fquickjs-go-polyfill?ref=badge_shield)
## Features
* fetch: `fetch`
* base64: `atob` and `btoa`
* window: `window`
* console: `console.log` and `console.error` and `console.warn` and `console.info` and `console.debug` and `console.trace`
* timers: `setTimeout` and `clearTimeout` alreay supported by quickjs-go
### Usage
```go
package main
import (
"time"
"github.com/buke/quickjs-go"
polyfill "github.com/buke/quickjs-go-polyfill"
)
func main() {
// Create a new runtime
rt := quickjs.NewRuntime()
defer rt.Close()
// Create a new context
ctx := rt.NewContext()
defer ctx.Close()
// Inject polyfills to the context
polyfill.InjectAll(ctx)
ret, _ := ctx.Eval(`
setTimeout(() => {
fetch('https://api.github.com/users/buke', {Method: 'GET'}).then(response => response.json()).then(data => {
console.log(data.login);
});
}, 50);
`)
defer ret.Free()
// Wait for the timeout to finish
time.Sleep(time.Millisecond * 100)
rt.ExecuteAllPendingJobs()
// Output:
// buke
}
```
## Documentation
Go Reference & more examples: https://pkg.go.dev/github.com/buke/quickjs-go-polyfill
## License
[MIT](./LICENSE)
[](https://app.fossa.com/projects/git%2Bgithub.com%2Fbuke%2Fquickjs-go-polyfill?ref=badge_large)