V bindings to QuickJS javascript engine. Run JS in V.
- 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
await
support. usingvjs.type_module
.
v install herudi.vjs
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()
}
v run main.v
Explore examples
Currently support linux/mac/win (x64).
in windows, requires
-cc gcc
.
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
glob := ctx.js_global()
glob.set('foo', 'bar')
value := ctx.eval('foo') or { panic(err) }
ctx.end()
println(value)
// bar
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()
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..
...
}
- Console
- setTimeout, clearTimeout
- setInterval, clearInterval
- btoa, atob
- URL
- URLSearchParams
- URLPattern
- Encoding API
- Crypto API
- SubtleCrypto
- digest
- encrypt
- decrypt
- sign
- verify
- Streams API
- Event
- FormData
- Blob
- File
- Performance
- Navigator
- Fetch API
- More...