Elkjs: a binding for a tiny JS engine

This is the initial release of elkjs, a Crystal binding for the Elk Javascript engine, available at GitHub - ralsina/elkjs: Crystal wrapper for the elk js engine

While Elk is very limited, I think it may be useful as a very lightweight tool for
developers who need a tiny language to make their tool more extensive.

It is not a replacement for duktape or anything else, it has no objects, no integers, no functions, and no lots of other things you take for granted.

What it does have is a very simple and ergonomic way to call to Crystal code from the interpreter, and you can integrate it in literally 5 lines of code.


require "elkjs"

# Create a new engine
js = Js::Engine.new

# Define a global function called "print" that will print to stdout
js.set_global("print", Js.func(->(args : Js::Args) {
  args.each do |arg|
    puts arg
  Js::UNDEF # Return undefined

# A classic
js.eval("print('Hello, World!');")

# You can loop
    for (let i=0; i<5; i++) {

# Eval returns the last value
puts js.eval("1+1") # => 2

Iā€™m curious what this is supposed to mean when the examples apparently use integers and function calls? :thinking:

Internally all numbers are floats, and they are also converted to float when you pass them to the engine or get them back out.

I meant functions defined in JavaScript.

I was wrong about functions tho!

Found out after the release/announcement. Most of the JS syntax to define functions fail but this works

let f=function(ā€¦){ā€¦};