Nanojs is a minimal subset of javascript.
Nanojs is fast and secure because it's compiled/executed as bytecode on stack-based VM that's written in native Go.
/* The Nanojs Language */
var fmt = import("fmt")
var each = function(seq, fn) {
for (x in seq) { fn(x) }
}
var sum = function(init, seq) {
each(seq, function(x) { init += x })
return init
}
fmt.println(sum(0, [1, 2, 3])) // "6"
fmt.println(sum("", [1, 2, 3])) // "123"
- Simple and highly readable
Syntax
- Dynamic typing with type coercion
- Higher-order functions and closures
- Immutable values
- Securely Embeddable and Extensible
- Compiler/runtime written in native Go (no external deps or cgo)
- Executable as a standalone language / REPL
go get github.com/zeaphoo/nanojs/v2
A simple Go example code that compiles/runs Nanojs script code with some input/output values:
package main
import (
"context"
"fmt"
"github.com/zeaphoo/nanojs/v2"
)
func main() {
// Nanojs script code
src := `
var each = function(seq, fn) {
for (var x in seq) { fn(x) }
}
var sum = 0
var mul = 1
each([a, b, c, d], function(x) {
sum += x
mul *= x
})`
// create a new Script instance
script := nanojs.NewScript([]byte(src))
// set values
_ = script.Add("a", 1)
_ = script.Add("b", 9)
_ = script.Add("c", 8)
_ = script.Add("d", 4)
// run the script
compiled, err := script.RunContext(context.Background())
if err != nil {
panic(err)
}
// retrieve values
sum := compiled.Get("sum")
mul := compiled.Get("mul")
fmt.Println(sum, mul) // "22 288"
}