diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index eeb5b10e7..823984b55 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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. diff --git a/src/main/java/com/eclipsesource/v8/debug/mirror/code.java b/src/main/java/com/eclipsesource/v8/debug/mirror/code.java new file mode 100644 index 000000000..913f6383d --- /dev/null +++ b/src/main/java/com/eclipsesource/v8/debug/mirror/code.java @@ -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(); + } + } +}