Skip to content

herudi/vjs

Folders and files

NameName
Last commit message
Last commit date

Latest commit

5cb1bf3 · Jul 30, 2024
Jul 30, 2024
Mar 14, 2024
Jul 30, 2024
Jul 22, 2024
Mar 14, 2024
Jul 30, 2024
Mar 3, 2024
Jul 30, 2024
Mar 18, 2024
Mar 6, 2024
Mar 17, 2024
Mar 6, 2024
Jul 30, 2024
Jul 28, 2024
Jul 28, 2024
Jul 30, 2024
Mar 17, 2024
Mar 17, 2024
Mar 19, 2024
Mar 17, 2024
Mar 19, 2024
Mar 17, 2024
Mar 9, 2024
Jul 30, 2024
Jul 28, 2024

Repository files navigation

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 await support. using vjs.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

It's Fun Project. PRs Wellcome :)