Code Monkey home page Code Monkey logo

clj-jsr223-v8's Introduction

clj-jsr223-v8

This is a Clojure wrapper around clj-v8, exposing a compliant* JSR-223 javax.script API to use the V8 ECMAScript engine from the JVM.

The javax.script API defines a generic interface for scripting engines, including ECMAScript engines for the JVM.

clj-v8 provides a Java-based interface to (a specific and probably dated version of) V8, but this is not compliant with javax.scripting. Projects depending on clj-v8 may want the option of loading pluggable script engines using javax.scripting, so clj-jsr223-v8 aims to bridge that gap.

(* Eventually.)

Usage

Add [clj-jsr223-v8 "0.1.7"] to :dependencies in project.clj.

Service API (recommended)

The javax.script API allows run-time discovery of registered engine services. clj-jsr223-v8 registers its service under the names "clj-v8", "v8", and "V8" (in that order of preference).

Get a V8ScriptEngine instance from clj-jsr223-v8 using the JSR-223 API:

(def engines (javax.script.ScriptingManager.))

(def v8-engine (.getEngineByName engines "clj-v8")) ;; A V8ScriptEngine instance
                                                    ;; (You might use "nashorn" on JDK8 here
                                                    ;;  for a different engine.)

(.eval v8-engine "123 + 456") ;; => 579

;; Context persists in each engine instance...

(.eval v8-engine "var foo = 1 + 1;") ;; => nil

(.eval v8-engine "foo") ;; => 2

Like in Nashorn, values returned by (.eval ...) are Clojure/Java objects (though not guaranteed to be exactly the same at this time!). These are constructed from a JSON-stringified marshalled representation of the underlying V8 value. This means e.g. Object instances are represented as maps:

(.eval v8-engine "var o = new Object(); o.foo = 1; o.bar = 2; o;") ;; => {"foo" 1, "bar" 2}

undefined and null ECMAScript values each appear as nil.

Cleaning up

V8ScriptEngine instances implement a cleanup method in addition to ScriptEngine extensions. This can be used to free the resources held by the underlying native V8 engine attached to the instance. Note: cleaning up is final and the instance will be unusable afterwards.

V8ScriptEngine also triggers cleanup automatically on finalize (i.e. just before being garbage-collected), so it is not required to manually clean up resources.

Direct API

To load the V8 engine directly (not recommended):

Import the script engine or the script engine factory classes as needed:

(import '[clj_jsr223_v8 V8ScriptEngineFactory V8ScriptEngine]) ;; Note the underscores!

Once imported, instantiate V8ScriptEngineFactory to get started.

(def my-factory (V8ScriptEngineFactory.))

(def my-engine (.getEngine my-factory)) ;; a V8ScriptEngine instance

(.eval my-engine "123 + 456") ;; => "579"

License

Copyright © 2015-2016 Abhishek Reddy

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.

clj-jsr223-v8's People

Stargazers

Nampoina Andriamilanto avatar Douglas P. Fields, Jr. avatar

Watchers

radhika reddy avatar James Cloos avatar

clj-jsr223-v8's Issues

Don't share context

Hi there,

Is it possible to clear the context before running a script, or have the ScriptEngine of V8 not remember context between evaluations?

In other words, if I execute these two scripts in sequence with the same ScriptEngine object:

var x = 3; x
x

I want the second one to give a V8 ReferenceError.

Looks like the regular clj-v8 does this, and you have to have shared context explicitly:

https://github.com/circleci/clj-v8/blob/master/clj-v8/test/v8/core_test.clj

Thoughts?

Thanks for your thoughts, and thanks for this super-easy-to-use utility for v8.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.