Vjs
V Bindings to QuickJS Javascript Engine.
Install / Use
/learn @herudi/VjsREADME
VJS
V bindings to QuickJS javascript engine. Run JS in V.
Features
- Evaluate js (code, file, module, etc).
- Multi evaluate support.
- Callback function support.
- Set-Globals support.
- Set-Module support.
- Call V from JS.
- Call JS from V.
- Top-Level
awaitsupport. usingvjs.type_module.
Install
v install herudi.vjs
Basic Usage
Create file main.v and copy-paste this code.
import herudi.vjs
fn main() {
rt := vjs.new_runtime()
ctx := rt.new_context()
value := ctx.eval('1 + 2') or { panic(err) }
ctx.end()
assert value.is_number() == true
assert value.is_string() == false
assert value.to_int() == 3
println(value)
// 3
// free
value.free()
ctx.free()
rt.free()
}
Run
v run main.v
Explore examples
Currently support linux/mac/win (x64).
in windows, requires
-cc gcc.
Multi Evaluate
ctx.eval('const sum = (a, b) => a + b') or { panic(err) }
ctx.eval('const mul = (a, b) => a * b') or { panic(err) }
sum := ctx.eval('sum(${1}, ${2})') or { panic(err) }
mul := ctx.eval('mul(${1}, ${2})') or { panic(err) }
ctx.end()
println(sum)
// 3
println(mul)
// 2
Add Global
glob := ctx.js_global()
glob.set('foo', 'bar')
value := ctx.eval('foo') or { panic(err) }
ctx.end()
println(value)
// bar
Add Module
mut mod := ctx.js_module('my-module')
mod.export('foo', 'foo')
mod.export('bar', 'bar')
mod.export_default(mod.to_object())
mod.create()
code := '
import mod, { foo, bar } from "my-module";
console.log(foo, bar);
console.log(mod);
'
ctx.eval(code, vjs.type_module) or { panic(err) }
ctx.end()
Web Platform APIs
Inject Web API to vjs.
import herudi.vjs
import herudi.vjs.web
fn main() {
rt := vjs.new_runtime()
ctx := rt.new_context()
// inject all
web.inject(ctx)
// or inject one by one
// web.console_api(ctx)
// web.encoding_api(ctx)
// more..
...
}
List Web Platform APIs
- [x] Console
- [x] setTimeout, clearTimeout
- [x] setInterval, clearInterval
- [x] btoa, atob
- [x] URL
- [x] URLSearchParams
- [x] URLPattern
- [x] Encoding API
- [x] TextEncoder
- [x] TextDecoder
- [x] TextEncoderStream
- [x] TextDecoderStream
- [x] Crypto API
- [x] randomUUID
- [x] getRandomValues
- [x] SubtleCrypto
- [x] digest
- [ ] encrypt
- [ ] decrypt
- [ ] sign
- [ ] verify
- [x] Streams API
- [x] Event
- [x] FormData
- [x] Blob
- [x] File
- [x] Performance
- [x] Navigator
- [x] Fetch API
- <i>More...</i>
