J2V8: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
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> | ||
==Example 2== | |||
<source lang="java"> | <source lang="java"> | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
Line 30: | Line 43: | ||
</source> | </source> | ||
==Example 3== | |||
<source lang="java"> | <source lang="java"> | ||
public static void main(String[] args) { | public static void main(String[] args) { | ||
Line 40: | Line 54: | ||
runtime.release(); | 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> | ||
Line 57: | Line 82: | ||
int size = hockeyTeam.executeIntegerFunction("addPlayer", parameters); | int size = hockeyTeam.executeIntegerFunction("addPlayer", parameters); | ||
parameters.release(); | parameters.release(); | ||
</source> | </source> | ||
Revision as of 09:45, 18 April 2021
<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();