J2V8: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
<source lang="xml">
<dependencies>
    <dependency>
        <groupId>com.eclipsesource.j2v8</groupId>
        <artifactId>j2v8_macosx_x86_64</artifactId>
        <version>2.0</version>
        <scope>compile</scope>
    </dependency>
</dependencies>
</source>
==Example 1==
<source lang="java">
<source lang="java">
package com.example;
package com.example;
Line 21: Line 33:
</source>
</source>


<source lang="xml">
==Example 2==
<dependencies>
<source lang="java">
     <dependency>
public static void main(String[] args) {
        <groupId>com.eclipsesource.j2v8</groupId>
    V8 v8 = V8.createV8Runtime();
        <artifactId>j2v8_macosx_x86_64</artifactId>
    int x = (Integer) v8.executeScript("var func = x => x * x; func(5);");
        <version>2.0</version>
    System.out.println(x);
        <scope>compile</scope>
    v8.release();
     </dependency>
}
</dependencies>
</source>
 
==Example 3==
<source lang="java">
public static void main(String[] args) {
     V8 runtime = V8.createV8Runtime();
    int result = runtime.executeIntegerScript(""
        + "var hello = 'hello, ';\n"
        + "var world = 'world!';\n"
        + "hello.concat(world).length;\n");
    System.out.println(result);
    runtime.release();
}
</source>
 
==Example 4==
<source lang="java">
V8Object player1 = new V8Object(runtime).add("name", "John");
V8Object player2 = new V8Object(runtime).add("name", "Chris");
V8Array players = new V8Array(runtime).push(player1).push(player2);
hockeyTeam.add("players", players);
player1.release();
player2.release();
players.release();
</source>
 
<source lang="js">
var hockeyTeam = {
    name      : 'WolfPack',
    players  : [],
    addPlayer : function(player) {
        this.players.push(player);
        return this.players.size();
    }
}
</source>
 
<source lang="java">
V8Array parameters = new V8Array(runtime).push(player1);
int size = hockeyTeam.executeIntegerFunction("addPlayer", parameters);
parameters.release();
</source>
 
==Example 5==
{|
| valign="top" |
<source lang="js" highlight="9-14,17">
var Chorke=Chorke||(function(Chorke,$me){
    $me=new Chorke();
    $me.$package='';
    $me.$namespace='';
    $me.$class='Chorke';
    return $me;
}(function(){}));
 
;Chorke.Academia||(function($,Academia,$me){
    $me=new Academia();
    $me.$package=$.$class;
    $me.$namespace=$.$class;
    $me.$class='Academia';
 
    $.Academia=$me;
    return $me;
}(Chorke,function(){}));
</source>
 
| valign="top" |
<source lang="js" highlight="9-14,17">
var Chorke=Chorke||(function(Chorke,$me){
    $me=new Chorke();
    $me.$package='';
    $me.$namespace='';
    $me.$class='Chorke';
    return $me;
}(function(){}));
 
;Chorke.Academia||(function($,$me){
 
     $me.$package=$.$class;
    $me.$namespace=$.$class;
    $me.$class='Academia';
 
    $.Academia=$me;
    return $me;
}(Chorke,{}));
</source>
 
| valign="top" |
<source lang="js" highlight="9-14,17">
var Chorke=Chorke||(function(Chorke,$me){
    $me=new Chorke();
    $me.$package='';
    $me.$namespace='';
    $me.$class='Chorke';
    return $me;
}(function(){}));
 
;Chorke.Academia||(function($,$me){
    $me={
        $package:$.$class,
        $namespace:$.$class,
        $class:'Academia'
    };
    $.Academia=$me;
    return $me;
}(Chorke));
</source>
</source>
|-
| colspan="3" |
----
|-
| valign="top" colspan="3" |
<source lang="java" highlight="2,6-8">
public static void main(String[] args) {
    String oopjs= "var Chorke=Chorke||function(a,e){return(e=new function(){}).$package='',e.$namespace='',e.$class='Chorke',e}();Chorke.Academia||function(a,e){e={$package:a.$class,$namespace:a.$class,$class:'Academia'},a.Academia=e}(Chorke);";
    V8 runtime  = V8.createV8Runtime();
    runtime.executeVoidScript(oopjs);


    V8Object chorke  = runtime.getObject("Chorke");
    V8Object academia = chorke.getObject("Academia");
    System.out.println(academia.getString("$class"));


    academia.release();
    chorke.release();
    runtime.release();
}
</source>
|}


==References==
==References==
{|
| valign="top" |
* [http://eclipsesource.com/blogs/2015/06/06/registering-java-callbacks-with-j2v8/ Registering Java Callbacks with J2V8]
* [http://eclipsesource.com/blogs/2015/05/12/multithreaded-javascript-with-j2v8/ Multithreaded JavaScript with J2V8]
* [https://stackoverflow.com/questions/38848274/ Execute .js function with J2V8]
* [https://stackoverflow.com/questions/38848274/ Execute .js function with J2V8]
* [http://eclipsesource.com/blogs/2016/07/20/running-node-js-on-the-jvm/ Running Node JS on the JVM]
* [http://eclipsesource.com/blogs/2016/07/20/running-node-js-on-the-jvm/ Running Node JS on the JVM]
Line 43: Line 183:
* [https://github.com/alicorn-systems/v8-adapter V8 Adapter]
* [https://github.com/alicorn-systems/v8-adapter V8 Adapter]
* [https://www.programcreek.com/java-api-examples/?api=com.eclipsesource.v8.V8Object V8Object]
* [https://www.programcreek.com/java-api-examples/?api=com.eclipsesource.v8.V8Object V8Object]
| valign="top" |
* [https://www.eclipsecon.org/na2015/session/j2v8-highly-efficient-js-runtime-java J2V8 A Highly Efficient JS Runtime For Java]
* [https://github.com/eclipsesource/J2V8/issues/441 Reduce the scope of the J2V8 project]
* [http://eclipsesource.com/blogs/2015/11/04/shipping-j2v8-as-an-aar/ Shipping J2V8 as an AAR]
* [[Google Closure Compiler]]
* [[JSON Schema Validation]]
* [[Rhino]]
|}

Latest revision as of 02:12, 8 July 2022

<dependencies>
    <dependency>
        <groupId>com.eclipsesource.j2v8</groupId>
        <artifactId>j2v8_macosx_x86_64</artifactId>
        <version>2.0</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

Example 1

package com.example;

import com.eclipsesource.v8.V8;

public class EclipseCon_snippet5 {

    public static class Printer {
        public void print(String string) {
            System.out.println(string);
        }
    }

    public static void main(String[] args) {
        V8 v8 = V8.createV8Runtime();
        v8.registerJavaMethod(new Printer(), "print", "print", new Class<?>[]{String.class});
        v8.executeVoidScript( "print('Hello, World!');" );
        v8.release(true);
    }
}

Example 2

public static void main(String[] args) {
    V8 v8 = V8.createV8Runtime();
    int x = (Integer) v8.executeScript("var func = x => x * x; func(5);");
    System.out.println(x);
    v8.release();
}

Example 3

public static void main(String[] args) {
    V8 runtime = V8.createV8Runtime();
    int result = runtime.executeIntegerScript(""
        + "var hello = 'hello, ';\n"
        + "var world = 'world!';\n"
        + "hello.concat(world).length;\n");
    System.out.println(result);
    runtime.release();
}

Example 4

V8Object player1 = new V8Object(runtime).add("name", "John");
V8Object player2 = new V8Object(runtime).add("name", "Chris");
V8Array players = new V8Array(runtime).push(player1).push(player2);
hockeyTeam.add("players", players);
player1.release();
player2.release();
players.release();
var hockeyTeam = {
    name      : 'WolfPack',
    players   : [],
    addPlayer : function(player) {
        this.players.push(player);
        return this.players.size();
    }
}
V8Array parameters = new V8Array(runtime).push(player1);
int size = hockeyTeam.executeIntegerFunction("addPlayer", parameters);
parameters.release();

Example 5

var Chorke=Chorke||(function(Chorke,$me){
    $me=new Chorke();
    $me.$package='';
    $me.$namespace='';
    $me.$class='Chorke';
    return $me;
}(function(){}));

;Chorke.Academia||(function($,Academia,$me){
    $me=new Academia();
    $me.$package=$.$class;
    $me.$namespace=$.$class;
    $me.$class='Academia';

    $.Academia=$me;
    return $me;
}(Chorke,function(){}));
var Chorke=Chorke||(function(Chorke,$me){
    $me=new Chorke();
    $me.$package='';
    $me.$namespace='';
    $me.$class='Chorke';
    return $me;
}(function(){}));

;Chorke.Academia||(function($,$me){

    $me.$package=$.$class;
    $me.$namespace=$.$class;
    $me.$class='Academia';

    $.Academia=$me;
    return $me;
}(Chorke,{}));
var Chorke=Chorke||(function(Chorke,$me){
    $me=new Chorke();
    $me.$package='';
    $me.$namespace='';
    $me.$class='Chorke';
    return $me;
}(function(){}));

;Chorke.Academia||(function($,$me){
    $me={
        $package:$.$class,
        $namespace:$.$class,
        $class:'Academia'
    };
    $.Academia=$me;
    return $me;
}(Chorke));

public static void main(String[] args) {
    String oopjs= "var Chorke=Chorke||function(a,e){return(e=new function(){}).$package='',e.$namespace='',e.$class='Chorke',e}();Chorke.Academia||function(a,e){e={$package:a.$class,$namespace:a.$class,$class:'Academia'},a.Academia=e}(Chorke);";
    V8 runtime  = V8.createV8Runtime();
    runtime.executeVoidScript(oopjs);

    V8Object chorke   = runtime.getObject("Chorke");
    V8Object academia = chorke.getObject("Academia");
    System.out.println(academia.getString("$class"));

    academia.release();
    chorke.release();
    runtime.release();
}

References