Giter VIP home page Giter VIP logo

Comments (2)

in-fke avatar in-fke commented on August 23, 2024

oracle/graaljs#272

from graal.

in-fke avatar in-fke commented on August 23, 2024

I will probably use this workaround (targetTypeMapping), question is: is it valid, that Graal does not do this out-of-the-box?

    public static class TakesInt {
        @Export
        public void method(int arg) {};
    }

    @Test
    public void testTakesIntWithCoercion() {
        // https://github.com/oracle/graal/issues/9096
        AtomicInteger predicateSucceededCount = new AtomicInteger();
        AtomicInteger predicateFailedCount = new AtomicInteger();
        AtomicInteger conversionCount = new AtomicInteger();        
        HostAccess hostAccess = HostAccess.newBuilder(HostAccess.ALL)
                // Rhino would do the same!?
                .targetTypeMapping(String.class, Integer.class, target -> {
                    boolean result;
                    try {
                        double d = Double.valueOf(target);
                        if (Math.floor(d) != Math.ceil(d)) {
                            result = false;
                        } else if (d > Integer.MAX_VALUE) {
                            result = false;
                        } else if (d < Integer.MIN_VALUE) {
                            result = false;
                        } else {
                            result = true;
                        }
                    } catch (NumberFormatException e) {
                        result = false;
                    }
                    if (result) {
                        predicateSucceededCount.incrementAndGet();
                    } else {
                        predicateFailedCount.incrementAndGet();
                    }
                    return result;
                }, target -> {
                    conversionCount.incrementAndGet();
                    return Double.valueOf(target).intValue();
                })
                //
                .build();        
        Context context = Context.newBuilder("js")
                .allowHostAccess(hostAccess)
                // required to declare class with Java.type
                .allowHostClassLookup(s -> true).build();                
        Value bindings = context.getBindings("js");
        bindings.putMember("takesInt", new TakesInt());
        context.eval("js", "const Integer = Java.type('" + Integer.class.getName() + "');");
        // these must not throw
        context.eval("js", "takesInt.method('0');");
        context.eval("js", "takesInt.method('' + Integer.MAX_VALUE);");
        context.eval("js", "takesInt.method('' + Integer.MIN_VALUE);");
        assertEquals(3, predicateSucceededCount.get());
        assertEquals(0, predicateFailedCount.get());        
        assertEquals(3, conversionCount.get());
        List.of(
                // too small
                "takesInt.method('' + (Integer.MIN_VALUE - 1));",
                // too large
                "takesInt.method('' + (Integer.MAX_VALUE + 1));",
                // not integer
                "takesInt.method('1.5');")
                //
                .forEach((script) -> {
                    try {
                        context.eval("js", script);
                        fail("for script [" + script + "]: expected excpetion to be thrown");
                    } catch (Exception e) {
                        assertEquals("for script [" + script + "]: exception class mismatch", PolyglotException.class, e.getClass());
                        assertTrue("for script [" + script + "]: unexpected message: [" + e.getMessage() + "]", StringUtils.contains(e.getMessage(), "Invalid or lossy primitive coercion"));
                    }
                });
        assertEquals(3, predicateSucceededCount.get());
        assertEquals(3, predicateFailedCount.get());        
        assertEquals(3, conversionCount.get());
    } 

from graal.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.