JavaScriptCore.cr logo

JavaScriptCore.cr

JavaScriptCore bindings for Crystal


Code Of Conduct BSD-2-Clause Licensed ci action status

What is this?

JavaScriptCore is a framework that provides a JavaScript engine for WebKit implementations, and provides this type of scripting in other contexts within macOS. JavaScriptCore is originally derived from KDE's JavaScript engine (KJS) library (which is part of the KDE project) and the PCRE regular expression library. Since forking from KJS and PCRE, JavaScriptCore has been improved with many new features and greatly improved performance.

from: https://en.wikipedia.org/wiki/WebKit#JavaScriptCore

This shard provides bindings for it, allowing you to evaluate JS from Crystal!

Bindings are being generated using gi-crystal.

Dependencies

You need JavaScriptCore 5.0. Your distro might provide it as libjavascriptcoregtk-5_0 or typelib-1_0-JavaScriptCore-5_0. If it doesn't then try installing webkit2gtk (the latest, -dev or -devel version if available). Doc generation will probably only be availble by installing one of the webkit2gtk packages.

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      javascriptcore:
        github: GeopJr/JavaScriptCore.cr
  2. Run shards install

  3. Run ./bin/gi-crystal to generate the bindings

Usage

There's a lot you can do with the bindings, but here's a simple evaluation:

require "javascriptcore"

context = JavaScriptCore::Context.new
script = "['Java', 'Script', 'Core'].join('')"
result = context.evaluate(script, -1)

p result.to_s #=> "JavaScriptCore"

There might be more examples on ./spec.

Contributing

  1. Read the Code of Conduct
  2. Fork it ( https://github.com/GeopJr/JavaScriptCore.cr/fork )
  3. Create your feature branch (git checkout -b my-new-feature)
  4. Commit your changes (git commit -am 'Add some feature')
  5. Push to the branch (git push origin my-new-feature)
  6. Create a new Pull Request