J2V8: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 40: Line 40:
     runtime.release();
     runtime.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>
</source>


Line 52: Line 69:
</dependencies>
</dependencies>
</source>
</source>


==References==
==References==

Revision as of 09:40, 18 April 2021

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);
    }
}
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();
}
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();
}
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();
<dependencies>
    <dependency>
        <groupId>com.eclipsesource.j2v8</groupId>
        <artifactId>j2v8_macosx_x86_64</artifactId>
        <version>2.0</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

References