Giter Club home page Giter Club logo

jv8's Introduction

JV8 needs maintainers. Please [email me](mailto:[email protected]) or [file an issue](http://github.com/namuol/jv8/issues) if you are interested.

JV8

A simple way to run V8 JavaScript instances from Java.

Warning: This project is **highly experimental**.

Example

V8Runner v8 = new V8Runner();

v8.map("javaSum", new V8MappableMethod {
  @Override
  public V8Value methodToRun(V8Value[] args) {
    double sum = 0;
    for (V8Value val : args) {
      sum += val.toNumber();
    }
    return new v8.val(sum);
  }
});

double sum = v8.runJS("(program)", "javaSum(6, 3, 12, 17, 4);").toNumber();

// sum == 42.0

Supported types

  • Boolean
  • Number
  • String
  • Function

Function passing and calling

Javascript can pass a Function to the Java context, and Java can call this function back.

v8.map("doSomethingWithFunction", new V8MappableMethod {
  @Override
  public V8Value methodToRun(V8Value[] args) {
  
    // Get the function from JS parameters
    V8Function function = (V8Function)args[0];
    
    // Create a table of arguments to pass
    V8Value newArgs[] = { new V8Number(8) };
    
    // Execute the function with one argument
    V8Value result = v8.callFunction(function, newArgs);
    
    // Get the result
    if (result.isNumber()) {
      double number = result.toNumber(); // 64
    }    
  }
});

v8.runJS("(program)", "doSomethingWithFunction(function(n){ return n * n; })");

Platform support

Currently, only the Android platform is supported, but I intend to support major desktop platforms as well (Linux/Windows/OS X).

This pre-alpha software still in highly experimental stages. Do not use this in production software.

Binaries

A nightly(ish) Android build can be downloaded here.

The tarball should be extracted into the root of a standard android project, and contains the following:

libs/
  jv8.jar
  armeabi/
    gdbserver # FOR DEBUG
    gdb.setup # FOR DEBUG
    libgnustl_shared.so
    libjv8.so

Note: These are debug-only builds for now; i.e. not-optimized and containing debug symbols.

Building

Requirements

You need to be able to build V8 for Android.

  • svn
  • git
  • python
  • More stuff I'm probably not aware of.

Instructions

First, you must initialize and pull down the v8 git submodule:

# From the root of the project folder:
git submodule init
git submodule update # This will take a while.

Now use the (hackish) build script to create everything you need:

./build.sh -j4 # Use the `-h` flag to see options

Have a snack while you wait. This will result in 3 essential files:

dist/
jv8.tar.gz/
  libs/
    jv8.jar               # Java code
    armeabi/
      libgnustl_shared.so # Android C++ STL support 
      libjv8.so           # JV8 JNI code

There will also be GDB stuff in here; the build script does not generate production binaries, currently, and I Do Not Recommend using this in production.


Analytics

jv8's People

Contributors

namuol avatar ldiqual avatar

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.