Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ contributors to follow so we have a chance to keep on top of things.

## Code contributions

Code contributions are greatly appreciated. We currently target V8 3.26, so
Code contributions are greatly appreciated. We currently target V8 3.26, soo
all contributions must link against that version of V8. Code contributions
should be accompanied by a set of unit tests. All tests are written in Java
and executed using JUnit. See `src/test/java` for examples.
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/com/eclipsesource/v8/debug/mirror/code.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import com.eclipsesource.v8.*;

public class J2V8ConstructorExample {
public static void main(String[] args) {
// Create a new V8 runtime
V8 v8 = V8.createV8Runtime();

try {
// Define a JS constructor function
v8.executeVoidScript(
"function Person(name) { " +
" this.name = name;" +
" this.greet = function() { return 'Hello ' + this.name; };" +
"}"
);

// Get the constructor function
V8Function personConstructor = (V8Function) v8.get("Person");

// Call as constructor (like new Person("Alice"))
V8Object alice = v8.executeObjectScript("new Person('Alice');");

// Call a method on the JS object
String greeting = alice.executeStringFunction("greet", null);
System.out.println(greeting); // Output: Hello Alice

// Release resources
alice.release();
personConstructor.release();
} finally {
v8.release();
}
}
}