J2V8: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
Line 188: Line 188:
* [https://github.com/eclipsesource/J2V8/issues/441 Reduce the scope of the J2V8 project]
* [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]
* [http://eclipsesource.com/blogs/2015/11/04/shipping-j2v8-as-an-aar/ Shipping J2V8 as an AAR]
* [[Google Closure Compiler]]
* [[JSON Schema Validation]]


|}
|}

Revision as of 01:50, 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