Coder Social home page Coder Social logo

dumganhar / libjsapi Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ripcordsoftware/libjsapi

0.0 3.0 0.0 2.57 MB

A SpiderMonkey (Mozilla) JSAPI wrapper for C++ 11

License: MIT License

Makefile 23.96% Shell 2.74% C++ 69.74% C 0.41% JavaScript 3.15%

libjsapi's Introduction

Build Status Coverage Status License

libjsapi

A SpiderMonkey 38 (Mozilla) JSAPI wrapper for C++ 11.

The JSAPI interface to the SpiderMonkey JavaScript VM can be difficult to integrate into applications without an intermediate abstraction layer managing object creation, object lifetimes and type mapping. This library is an attempt to hide the complexity of dealing with SpiderMonkey from modern C++ 11 applications.

With libjsapi you will be able to:

  • Execute any valid JavaScript
  • Invoke JavaScript methods from C++
  • Expose C++ methods to JavaScript
  • Expose C++ objects to JavaScript
  • Consume JavaScript objects in C++
  • Handle errors with C++ exceptions
  • Create any number of Runtimes and Contexts - simultaneously on multiple threads

##Gratuitous Screenshot

The image below shows libjsapi hosting a GTK+ application which emulates enough browser bits to render a funky JavaScript Mandelbrot application ported from https://github.com/cslarsen/mandelbrot-js.

A JS generated Mandelbrot running under libjsapi

The window on the left is Firefox rendering a Mandelbrot fractal, on the right is the libjsapi application running (mainly) the same JavaScript code.

##Examples:

The simplest thing that could possibly work:

#include <iostream>
#include "libjsapi.h"

int main() {
    // create the runtime which hosts spidermonkey
    rs::jsapi::Runtime rt;
    
    // execute a script in the context of the runtime, getting the result
    rs::jsapi::Value result(rt);
    rt.Evaluate("(function(){return 42;})();", result);
    
    // output the result to the console
    std::cout << result << std::endl;
    return 0;
}

Among other things we can expose C++ lambdas (and methods) to JS:

#include <iostream>
#include "libjsapi.h"

int main() {
    // create the runtime which hosts spidermonkey
    rs::jsapi::Runtime rt;
    
    // define a function in global scope implemented by a C++ lambda
    rs::jsapi::Global::DefineFunction(rt, "getTheAnswer", 
        [](const std::vector<rs::jsapi::Value>& args, rs::jsapi::Value& result) { 
            result = 42; 
    });
    
    // call the native function from JS
    rs::jsapi::Value result(rt);
    rt.Evaluate("(function(){return getTheAnswer();})();", result);
    
    // output the result to the console
    std::cout << result << std::endl;
    return 0;
}

The wiki contains more background on JSAPI, libjsapi and further examples.

Building

Requires:

  • GCC 4.8.1+
  • GNU Make
  • lcov and Ruby (for test coverage)
  • valgrind (for memory analysis)

To build type:

$ make

The first libjsapi build may take a long time to complete since it will invoke a build of SpiderMonkey and GoogleTest. Subsequent builds do not require this step and will be much faster.

To build tests with coverage:

$ make test

To create a coverage report:

$ ./coverage.sh

To create a valgrind report:

$ ./grind.sh

You will find examples in the examples directory. You can build these after building libjsapi by running:

$ cd examples
$ make

If building is slow on your system then you can speed it up by stripping the debugging information from the mozjs library by running ./strip_mozjs.sh.

The wiki contains details on building on different systems.

libjsapi's People

Contributors

craigminihan avatar

Watchers

James Chen avatar James Cloos avatar  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.