Example usage for jdk.nashorn.internal.objects NativeArray getLength

List of usage examples for jdk.nashorn.internal.objects NativeArray getLength

Introduction

In this page you can find the example usage for jdk.nashorn.internal.objects NativeArray getLength.

Prototype

@Override
    public Object getLength() 

Source Link

Usage

From source file:org.siphon.common.js.JsTypeUtil.java

License:Open Source License

public static int getArrayLength(NativeArray arr) {
    Object length = arr.getLength();
    if (length instanceof Long) {
        return ((Long) length).intValue();
    } else if (length instanceof Integer) {
        return (Integer) length;
    } else {/*  w  ww .j  ava 2 s .c om*/
        return Integer.parseInt(length.toString());
    }
}

From source file:reactor.js.test.RequireFunction.java

License:Open Source License

@Override
public Object call(Object thiz, Object... args) {
    if (args.length > 1 || args.length == 0) {
        throw new IllegalArgumentException("USAGE: require(moduleId);");
    }//from   w w w  .  ja v a 2  s.  c  o m

    String moduleId = (String) args[0];
    if (!moduleId.endsWith(".js")) {
        moduleId = moduleId + ".js";
    }

    if (moduleId.startsWith("classpath:")) {
        try {
            return engine.eval("load(\"" + moduleId + "\");", bindings);
        } catch (ScriptException e) {
            throw new IllegalStateException(e.getMessage(), e);
        }
    }

    Path modulePath = null;

    if (hasMember("paths")) {
        NativeArray paths = (NativeArray) getMember("paths");
        int len = ((Long) paths.getLength()).intValue();
        for (int i = 0; i < len; i++) {
            if (null != (modulePath = findPath(paths.get(i).toString(), moduleId))) {
                break;
            }
        }
    } else {
        modulePath = findPath(".", moduleId);
    }

    if (null == modulePath) {
        throw new IllegalArgumentException("Cannot find module \"" + args[0] + "\"");
    }

    try {
        return engine.eval("load(\"" + modulePath.toAbsolutePath().toString() + "\");", bindings);
    } catch (ScriptException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}