Javet icon indicating copy to clipboard operation
Javet copied to clipboard

RangeError: Maximum call stack size exceeded

Open octwyh opened this issue 2 months ago • 6 comments

For the method: com.caoccao.javet.values.reference.IV8ValueArray.push(Object... objects), if the length of the objects array is too large, it will throw an exception: Maximum call stack size exceeded.

Test data: objects=[{"k":1},{"k":2},...,{"k":500000}]

octwyh avatar Oct 24 '25 09:10 octwyh

Please post the whole code. It could be a misuse of the API.

caoccao avatar Oct 24 '25 12:10 caoccao

public static void main(String[] args) throws Exception {
    try (V8Runtime v8Runtime = V8Host.getV8Instance().createV8Runtime()) {
        try (V8ValueObject v8ValueObject = v8Runtime.createV8ValueObject()) {
            v8Runtime.getGlobalObject().set("intercept", v8ValueObject);
            v8ValueObject.bind(new TestIntercept());
        }
        IV8Executor executor = v8Runtime.getExecutor("intercept.handle();");
        // this line will throw Exception
        executor.executeObject();
    }
}

public static class TestIntercept {
    @V8Function
    public List<Integer> handle() {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 500000; i++) {
            list.add(i);
        }
        return list;
    }
}

octwyh avatar Oct 24 '25 13:10 octwyh

@caoccao Is this a bug, or am I having an issue with the API usage?

octwyh avatar Oct 28 '25 06:10 octwyh

Sorry, I haven't had time evaluating your code.

caoccao avatar Oct 28 '25 09:10 caoccao

Alright, totally understand... When you have time later, please don’t forget to take a look at the code, it’s basically the same as the example in the Interception section of the Javet Document Portal documentation.

octwyh avatar Oct 28 '25 10:10 octwyh

The error is from V8.

Basically, Array.prototype.push() accepts multiple arguments and there is no limit to the number of arguments.

In reality, V8 has a limit. In your case, 500,000 items is way beyond V8's limit.

Suggestions:

  1. Reduce the number of items.
  2. Javet can split the push() call into multiple calls to workaround that.

caoccao avatar Oct 28 '25 10:10 caoccao