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.
Example:
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
end
Js::UNDEF # Return undefined
}))
# A classic
js.eval("print('Hello, World!');")
# You can loop
js.eval("
for (let i=0; i<5; i++) {
print(i);
}")
# Eval returns the last value
puts js.eval("1+1") # => 2