jdk.nashorn.internal.runtime.ECMAErrors Java Examples

The following examples show how to use jdk.nashorn.internal.runtime.ECMAErrors. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: TencentKona-8   Author: Tencent   File: NashornException.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns array javascript stack frames from the given exception object.
 *
 * @param exception exception from which stack frames are retrieved and filtered
 * @return array of javascript stack frames
 */
public static StackTraceElement[] getScriptFrames(final Throwable exception) {
    final StackTraceElement[] frames = exception.getStackTrace();
    final List<StackTraceElement> filtered = new ArrayList<>();
    for (final StackTraceElement st : frames) {
        if (ECMAErrors.isScriptFrame(st)) {
            final String className = "<" + st.getFileName() + ">";
            String methodName = st.getMethodName();
            if (methodName.equals(CompilerConstants.PROGRAM.symbolName())) {
                methodName = "<program>";
            } else {
                methodName = stripMethodName(methodName);
            }

            filtered.add(new StackTraceElement(className, methodName,
                    st.getFileName(), st.getLineNumber()));
        }
    }
    return filtered.toArray(new StackTraceElement[filtered.size()]);
}
 
Example #2
Source Project: jdk8u60   Author: chenghanpeng   File: NashornException.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructor
 *
 * @param msg       exception message
 * @param cause     exception cause
 */
protected NashornException(final String msg, final Throwable cause) {
    super(msg, cause == null ? null : cause);
    // This is not so pretty - but it gets the job done. Note that the stack
    // trace has been already filled by "fillInStackTrace" call from
    // Throwable
    // constructor and so we don't pay additional cost for it.

    // Hard luck - no column number info
    this.column = -1;

    // Find the first JavaScript frame by walking and set file, line from it
    // Usually, we should be able to find it in just few frames depth.
    for (final StackTraceElement ste : getStackTrace()) {
        if (ECMAErrors.isScriptFrame(ste)) {
            // Whatever here is compiled from JavaScript code
            this.fileName = ste.getFileName();
            this.line = ste.getLineNumber();
            return;
        }
    }

    this.fileName = null;
    this.line = 0;
}
 
Example #3
Source Project: jdk8u60   Author: chenghanpeng   File: NashornException.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns array javascript stack frames from the given exception object.
 *
 * @param exception exception from which stack frames are retrieved and filtered
 * @return array of javascript stack frames
 */
public static StackTraceElement[] getScriptFrames(final Throwable exception) {
    final StackTraceElement[] frames = exception.getStackTrace();
    final List<StackTraceElement> filtered = new ArrayList<>();
    for (final StackTraceElement st : frames) {
        if (ECMAErrors.isScriptFrame(st)) {
            final String className = "<" + st.getFileName() + ">";
            String methodName = st.getMethodName();
            if (methodName.equals(CompilerConstants.PROGRAM.symbolName())) {
                methodName = "<program>";
            }

            if (methodName.contains(CompilerConstants.ANON_FUNCTION_PREFIX.symbolName())) {
                methodName = "<anonymous>";
            }

            filtered.add(new StackTraceElement(className, methodName,
                    st.getFileName(), st.getLineNumber()));
        }
    }
    return filtered.toArray(new StackTraceElement[filtered.size()]);
}
 
Example #4
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: NashornException.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns array javascript stack frames from the given exception object.
 *
 * @param exception exception from which stack frames are retrieved and filtered
 * @return array of javascript stack frames
 */
public static StackTraceElement[] getScriptFrames(final Throwable exception) {
    final StackTraceElement[] frames = exception.getStackTrace();
    final List<StackTraceElement> filtered = new ArrayList<>();
    for (final StackTraceElement st : frames) {
        if (ECMAErrors.isScriptFrame(st)) {
            final String className = "<" + st.getFileName() + ">";
            String methodName = st.getMethodName();
            if (methodName.equals(CompilerConstants.PROGRAM.symbolName())) {
                methodName = "<program>";
            } else {
                methodName = stripMethodName(methodName);
            }

            filtered.add(new StackTraceElement(className, methodName,
                    st.getFileName(), st.getLineNumber()));
        }
    }
    return filtered.toArray(new StackTraceElement[filtered.size()]);
}
 
Example #5
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: NashornException.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns array javascript stack frames from the given exception object.
 *
 * @param exception exception from which stack frames are retrieved and filtered
 * @return array of javascript stack frames
 */
public static StackTraceElement[] getScriptFrames(final Throwable exception) {
    final StackTraceElement[] frames = exception.getStackTrace();
    final List<StackTraceElement> filtered = new ArrayList<>();
    for (final StackTraceElement st : frames) {
        if (ECMAErrors.isScriptFrame(st)) {
            final String className = "<" + st.getFileName() + ">";
            String methodName = st.getMethodName();
            if (methodName.equals(CompilerConstants.PROGRAM.symbolName())) {
                methodName = "<program>";
            } else {
                methodName = stripMethodName(methodName);
            }

            filtered.add(new StackTraceElement(className, methodName,
                    st.getFileName(), st.getLineNumber()));
        }
    }
    return filtered.toArray(new StackTraceElement[filtered.size()]);
}
 
Example #6
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: NashornException.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns array javascript stack frames from the given exception object.
 *
 * @param exception exception from which stack frames are retrieved and filtered
 * @return array of javascript stack frames
 */
public static StackTraceElement[] getScriptFrames(final Throwable exception) {
    final StackTraceElement[] frames = exception.getStackTrace();
    final List<StackTraceElement> filtered = new ArrayList<>();
    for (final StackTraceElement st : frames) {
        if (ECMAErrors.isScriptFrame(st)) {
            final String className = "<" + st.getFileName() + ">";
            String methodName = st.getMethodName();
            if (methodName.equals(CompilerConstants.PROGRAM.symbolName())) {
                methodName = "<program>";
            } else {
                methodName = stripMethodName(methodName);
            }

            filtered.add(new StackTraceElement(className, methodName,
                    st.getFileName(), st.getLineNumber()));
        }
    }
    return filtered.toArray(new StackTraceElement[0]);
}
 
Example #7
Source Project: nashorn   Author: ynsn   File: NashornException.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns array javascript stack frames from the given exception object.
 *
 * @param exception exception from which stack frames are retrieved and filtered
 * @return array of javascript stack frames
 */
public static StackTraceElement[] getScriptFrames(final Throwable exception) {
    final StackTraceElement[] frames = exception.getStackTrace();
    final List<StackTraceElement> filtered = new ArrayList<>();
    for (final StackTraceElement st : frames) {
        if (ECMAErrors.isScriptFrame(st)) {
            final String className = "<" + st.getFileName() + ">";
            String methodName = st.getMethodName();
            if (methodName.equals(CompilerConstants.RUN_SCRIPT.symbolName())) {
                methodName = "<program>";
            }
            filtered.add(new StackTraceElement(className, methodName,
                    st.getFileName(), st.getLineNumber()));
        }
    }
    return filtered.toArray(new StackTraceElement[filtered.size()]);
}
 
Example #8
Source Project: hottub   Author: dsrg-uoft   File: NashornException.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns array javascript stack frames from the given exception object.
 *
 * @param exception exception from which stack frames are retrieved and filtered
 * @return array of javascript stack frames
 */
public static StackTraceElement[] getScriptFrames(final Throwable exception) {
    final StackTraceElement[] frames = exception.getStackTrace();
    final List<StackTraceElement> filtered = new ArrayList<>();
    for (final StackTraceElement st : frames) {
        if (ECMAErrors.isScriptFrame(st)) {
            final String className = "<" + st.getFileName() + ">";
            String methodName = st.getMethodName();
            if (methodName.equals(CompilerConstants.PROGRAM.symbolName())) {
                methodName = "<program>";
            }

            if (methodName.contains(CompilerConstants.ANON_FUNCTION_PREFIX.symbolName())) {
                methodName = "<anonymous>";
            }

            filtered.add(new StackTraceElement(className, methodName,
                    st.getFileName(), st.getLineNumber()));
        }
    }
    return filtered.toArray(new StackTraceElement[filtered.size()]);
}
 
Example #9
Source Project: openjdk-8-source   Author: keerath   File: NashornException.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructor
 *
 * @param msg       exception message
 * @param cause     exception cause
 */
protected NashornException(final String msg, final Throwable cause) {
    super(msg, cause == null ? null : cause);
    // This is not so pretty - but it gets the job done. Note that the stack
    // trace has been already filled by "fillInStackTrace" call from
    // Throwable
    // constructor and so we don't pay additional cost for it.

    // Hard luck - no column number info
    this.column = -1;

    // Find the first JavaScript frame by walking and set file, line from it
    // Usually, we should be able to find it in just few frames depth.
    for (final StackTraceElement ste : getStackTrace()) {
        if (ECMAErrors.isScriptFrame(ste)) {
            // Whatever here is compiled from JavaScript code
            this.fileName = ste.getFileName();
            this.line = ste.getLineNumber();
            return;
        }
    }

    this.fileName = null;
    this.line = 0;
}
 
Example #10
Source Project: openjdk-8-source   Author: keerath   File: NashornException.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns array javascript stack frames from the given exception object.
 *
 * @param exception exception from which stack frames are retrieved and filtered
 * @return array of javascript stack frames
 */
public static StackTraceElement[] getScriptFrames(final Throwable exception) {
    final StackTraceElement[] frames = exception.getStackTrace();
    final List<StackTraceElement> filtered = new ArrayList<>();
    for (final StackTraceElement st : frames) {
        if (ECMAErrors.isScriptFrame(st)) {
            final String className = "<" + st.getFileName() + ">";
            String methodName = st.getMethodName();
            if (methodName.equals(CompilerConstants.RUN_SCRIPT.symbolName())) {
                methodName = "<program>";
            }
            filtered.add(new StackTraceElement(className, methodName,
                    st.getFileName(), st.getLineNumber()));
        }
    }
    return filtered.toArray(new StackTraceElement[filtered.size()]);
}
 
Example #11
Source Project: openjdk-8   Author: bpupadhyaya   File: NashornException.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructor
 *
 * @param msg       exception message
 * @param cause     exception cause
 */
protected NashornException(final String msg, final Throwable cause) {
    super(msg, cause == null ? null : cause);
    // This is not so pretty - but it gets the job done. Note that the stack
    // trace has been already filled by "fillInStackTrace" call from
    // Throwable
    // constructor and so we don't pay additional cost for it.

    // Hard luck - no column number info
    this.column = -1;

    // Find the first JavaScript frame by walking and set file, line from it
    // Usually, we should be able to find it in just few frames depth.
    for (final StackTraceElement ste : getStackTrace()) {
        if (ECMAErrors.isScriptFrame(ste)) {
            // Whatever here is compiled from JavaScript code
            this.fileName = ste.getFileName();
            this.line = ste.getLineNumber();
            return;
        }
    }

    this.fileName = null;
    this.line = 0;
}
 
Example #12
Source Project: openjdk-8   Author: bpupadhyaya   File: NashornException.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns array javascript stack frames from the given exception object.
 *
 * @param exception exception from which stack frames are retrieved and filtered
 * @return array of javascript stack frames
 */
public static StackTraceElement[] getScriptFrames(final Throwable exception) {
    final StackTraceElement[] frames = exception.getStackTrace();
    final List<StackTraceElement> filtered = new ArrayList<>();
    for (final StackTraceElement st : frames) {
        if (ECMAErrors.isScriptFrame(st)) {
            final String className = "<" + st.getFileName() + ">";
            String methodName = st.getMethodName();
            if (methodName.equals(CompilerConstants.RUN_SCRIPT.symbolName())) {
                methodName = "<program>";
            }
            filtered.add(new StackTraceElement(className, methodName,
                    st.getFileName(), st.getLineNumber()));
        }
    }
    return filtered.toArray(new StackTraceElement[filtered.size()]);
}
 
Example #13
Source Project: jdk8u_nashorn   Author: JetBrains   File: NashornException.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Returns array javascript stack frames from the given exception object.
 *
 * @param exception exception from which stack frames are retrieved and filtered
 * @return array of javascript stack frames
 */
public static StackTraceElement[] getScriptFrames(final Throwable exception) {
    final StackTraceElement[] frames = exception.getStackTrace();
    final List<StackTraceElement> filtered = new ArrayList<>();
    for (final StackTraceElement st : frames) {
        if (ECMAErrors.isScriptFrame(st)) {
            final String className = "<" + st.getFileName() + ">";
            String methodName = st.getMethodName();
            if (methodName.equals(CompilerConstants.PROGRAM.symbolName())) {
                methodName = "<program>";
            } else {
                methodName = stripMethodName(methodName);
            }

            filtered.add(new StackTraceElement(className, methodName,
                    st.getFileName(), st.getLineNumber()));
        }
    }
    return filtered.toArray(new StackTraceElement[filtered.size()]);
}
 
Example #14
Source Project: nashorn   Author: ynsn   File: NashornException.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Constructor
 *
 * @param msg       exception message
 * @param cause     exception cause
 */
protected NashornException(final String msg, final Throwable cause) {
    super(msg, cause == null ? null : cause);
    // This is not so pretty - but it gets the job done. Note that the stack
    // trace has been already filled by "fillInStackTrace" call from
    // Throwable
    // constructor and so we don't pay additional cost for it.

    // Hard luck - no column number info
    this.column = -1;

    // Find the first JavaScript frame by walking and set file, line from it
    // Usually, we should be able to find it in just few frames depth.
    for (final StackTraceElement ste : getStackTrace()) {
        if (ECMAErrors.isScriptFrame(ste)) {
            // Whatever here is compiled from JavaScript code
            this.fileName = ste.getFileName();
            this.line = ste.getLineNumber();
            return;
        }
    }

    this.fileName = null;
    this.line = 0;
}
 
Example #15
Source Project: TencentKona-8   Author: Tencent   File: NashornException.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void ensureLineAndFileName() {
    if (lineAndFileNameUnknown) {
        for (final StackTraceElement ste : getStackTrace()) {
            if (ECMAErrors.isScriptFrame(ste)) {
                // Whatever here is compiled from JavaScript code
                fileName = ste.getFileName();
                line = ste.getLineNumber();
                return;
            }
        }

        lineAndFileNameUnknown = false;
    }
}
 
Example #16
Source Project: jdk8u_nashorn   Author: JetBrains   File: NashornStaticClassLinker.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
    final LinkRequest request = linkRequest.withoutRuntimeContext(); // Nashorn has no runtime context
    final Object self = request.getReceiver();
    if (self.getClass() != StaticClass.class) {
        return null;
    }
    final Class<?> receiverClass = ((StaticClass) self).getRepresentedClass();

    Bootstrap.checkReflectionAccess(receiverClass, true);
    final CallSiteDescriptor desc = request.getCallSiteDescriptor();
    // We intercept "new" on StaticClass instances to provide additional capabilities
    if ("new".equals(desc.getNameToken(CallSiteDescriptor.OPERATOR))) {
        if (! Modifier.isPublic(receiverClass.getModifiers())) {
            throw ECMAErrors.typeError("new.on.nonpublic.javatype", receiverClass.getName());
        }

        // make sure new is on accessible Class
        Context.checkPackageAccess(receiverClass);

        // Is the class abstract? (This includes interfaces.)
        if (NashornLinker.isAbstractClass(receiverClass)) {
            // Change this link request into a link request on the adapter class.
            final Object[] args = request.getArguments();
            args[0] = JavaAdapterFactory.getAdapterClassFor(new Class<?>[] { receiverClass }, null,
                    linkRequest.getCallSiteDescriptor().getLookup());
            final LinkRequest adapterRequest = request.replaceArguments(request.getCallSiteDescriptor(), args);
            final GuardedInvocation gi = checkNullConstructor(delegate(linkerServices, adapterRequest), receiverClass);
            // Finally, modify the guard to test for the original abstract class.
            return gi.replaceMethods(gi.getInvocation(), Guards.getIdentityGuard(self));
        }
        // If the class was not abstract, just delegate linking to the standard StaticClass linker. Make an
        // additional check to ensure we have a constructor. We could just fall through to the next "return"
        // statement, except we also insert a call to checkNullConstructor() which throws an ECMAScript TypeError
        // with a more intuitive message when no suitable constructor is found.
        return checkNullConstructor(delegate(linkerServices, request), receiverClass);
    }
    // In case this was not a "new" operation, just delegate to the the standard StaticClass linker.
    return delegate(linkerServices, request);
}
 
Example #17
Source Project: TencentKona-8   Author: Tencent   File: NashornStaticClassLinker.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
    final LinkRequest request = linkRequest.withoutRuntimeContext(); // Nashorn has no runtime context
    final Object self = request.getReceiver();
    if (self.getClass() != StaticClass.class) {
        return null;
    }
    final Class<?> receiverClass = ((StaticClass) self).getRepresentedClass();

    Bootstrap.checkReflectionAccess(receiverClass, true);
    final CallSiteDescriptor desc = request.getCallSiteDescriptor();
    // We intercept "new" on StaticClass instances to provide additional capabilities
    if ("new".equals(desc.getNameToken(CallSiteDescriptor.OPERATOR))) {
        if (! Modifier.isPublic(receiverClass.getModifiers())) {
            throw ECMAErrors.typeError("new.on.nonpublic.javatype", receiverClass.getName());
        }

        // make sure new is on accessible Class
        Context.checkPackageAccess(receiverClass);

        // Is the class abstract? (This includes interfaces.)
        if (NashornLinker.isAbstractClass(receiverClass)) {
            // Change this link request into a link request on the adapter class.
            final Object[] args = request.getArguments();
            args[0] = JavaAdapterFactory.getAdapterClassFor(new Class<?>[] { receiverClass }, null,
                    linkRequest.getCallSiteDescriptor().getLookup());
            final LinkRequest adapterRequest = request.replaceArguments(request.getCallSiteDescriptor(), args);
            final GuardedInvocation gi = checkNullConstructor(delegate(linkerServices, adapterRequest), receiverClass);
            // Finally, modify the guard to test for the original abstract class.
            return gi.replaceMethods(gi.getInvocation(), Guards.getIdentityGuard(self));
        }
        // If the class was not abstract, just delegate linking to the standard StaticClass linker. Make an
        // additional check to ensure we have a constructor. We could just fall through to the next "return"
        // statement, except we also insert a call to checkNullConstructor() which throws an ECMAScript TypeError
        // with a more intuitive message when no suitable constructor is found.
        return checkNullConstructor(delegate(linkerServices, request), receiverClass);
    }
    // In case this was not a "new" operation, just delegate to the the standard StaticClass linker.
    return delegate(linkerServices, request);
}
 
Example #18
Source Project: TencentKona-8   Author: Tencent   File: AssignSymbols.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void throwUnprotectedSwitchError(final VarNode varNode) {
    // Block scoped declarations in switch statements without explicit blocks should be declared
    // in a common block that contains all the case clauses. We cannot support this without a
    // fundamental rewrite of how switch statements are handled (case nodes contain blocks and are
    // directly contained by switch node). As a temporary solution we throw a reference error here.
    final String msg = ECMAErrors.getMessage("syntax.error.unprotected.switch.declaration", varNode.isLet() ? "let" : "const");
    throwParserException(msg, varNode);
}
 
Example #19
Source Project: jdk8u60   Author: chenghanpeng   File: NashornStaticClassLinker.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
    final LinkRequest request = linkRequest.withoutRuntimeContext(); // Nashorn has no runtime context
    final Object self = request.getReceiver();
    if (self.getClass() != StaticClass.class) {
        return null;
    }
    final Class<?> receiverClass = ((StaticClass) self).getRepresentedClass();

    Bootstrap.checkReflectionAccess(receiverClass, true);
    final CallSiteDescriptor desc = request.getCallSiteDescriptor();
    // We intercept "new" on StaticClass instances to provide additional capabilities
    if ("new".equals(desc.getNameToken(CallSiteDescriptor.OPERATOR))) {
        if (! Modifier.isPublic(receiverClass.getModifiers())) {
            throw ECMAErrors.typeError("new.on.nonpublic.javatype", receiverClass.getName());
        }

        // make sure new is on accessible Class
        Context.checkPackageAccess(receiverClass);

        // Is the class abstract? (This includes interfaces.)
        if (NashornLinker.isAbstractClass(receiverClass)) {
            // Change this link request into a link request on the adapter class.
            final Object[] args = request.getArguments();
            args[0] = JavaAdapterFactory.getAdapterClassFor(new Class<?>[] { receiverClass }, null,
                    linkRequest.getCallSiteDescriptor().getLookup());
            final LinkRequest adapterRequest = request.replaceArguments(request.getCallSiteDescriptor(), args);
            final GuardedInvocation gi = checkNullConstructor(delegate(linkerServices, adapterRequest), receiverClass);
            // Finally, modify the guard to test for the original abstract class.
            return gi.replaceMethods(gi.getInvocation(), Guards.getIdentityGuard(self));
        }
        // If the class was not abstract, just delegate linking to the standard StaticClass linker. Make an
        // additional check to ensure we have a constructor. We could just fall through to the next "return"
        // statement, except we also insert a call to checkNullConstructor() which throws an ECMAScript TypeError
        // with a more intuitive message when no suitable constructor is found.
        return checkNullConstructor(delegate(linkerServices, request), receiverClass);
    }
    // In case this was not a "new" operation, just delegate to the the standard StaticClass linker.
    return delegate(linkerServices, request);
}
 
Example #20
Source Project: jdk8u60   Author: chenghanpeng   File: AssignSymbols.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void throwUnprotectedSwitchError(final VarNode varNode) {
    // Block scoped declarations in switch statements without explicit blocks should be declared
    // in a common block that contains all the case clauses. We cannot support this without a
    // fundamental rewrite of how switch statements are handled (case nodes contain blocks and are
    // directly contained by switch node). As a temporary solution we throw a reference error here.
    final String msg = ECMAErrors.getMessage("syntax.error.unprotected.switch.declaration", varNode.isLet() ? "let" : "const");
    throwParserException(msg, varNode);
}
 
Example #21
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: NashornException.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void ensureLineAndFileName() {
    if (lineAndFileNameUnknown) {
        for (final StackTraceElement ste : getStackTrace()) {
            if (ECMAErrors.isScriptFrame(ste)) {
                // Whatever here is compiled from JavaScript code
                fileName = ste.getFileName();
                line = ste.getLineNumber();
                return;
            }
        }

        lineAndFileNameUnknown = false;
    }
}
 
Example #22
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: NashornStaticClassLinker.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
    final LinkRequest request = linkRequest.withoutRuntimeContext(); // Nashorn has no runtime context
    final Object self = request.getReceiver();
    if (self.getClass() != StaticClass.class) {
        return null;
    }
    final Class<?> receiverClass = ((StaticClass) self).getRepresentedClass();

    Bootstrap.checkReflectionAccess(receiverClass, true);
    final CallSiteDescriptor desc = request.getCallSiteDescriptor();
    // We intercept "new" on StaticClass instances to provide additional capabilities
    if ("new".equals(desc.getNameToken(CallSiteDescriptor.OPERATOR))) {
        if (! Modifier.isPublic(receiverClass.getModifiers())) {
            throw ECMAErrors.typeError("new.on.nonpublic.javatype", receiverClass.getName());
        }

        // make sure new is on accessible Class
        Context.checkPackageAccess(receiverClass);

        // Is the class abstract? (This includes interfaces.)
        if (NashornLinker.isAbstractClass(receiverClass)) {
            // Change this link request into a link request on the adapter class.
            final Object[] args = request.getArguments();
            args[0] = JavaAdapterFactory.getAdapterClassFor(new Class<?>[] { receiverClass }, null,
                    linkRequest.getCallSiteDescriptor().getLookup());
            final LinkRequest adapterRequest = request.replaceArguments(request.getCallSiteDescriptor(), args);
            final GuardedInvocation gi = checkNullConstructor(delegate(linkerServices, adapterRequest), receiverClass);
            // Finally, modify the guard to test for the original abstract class.
            return gi.replaceMethods(gi.getInvocation(), Guards.getIdentityGuard(self));
        }
        // If the class was not abstract, just delegate linking to the standard StaticClass linker. Make an
        // additional check to ensure we have a constructor. We could just fall through to the next "return"
        // statement, except we also insert a call to checkNullConstructor() which throws an ECMAScript TypeError
        // with a more intuitive message when no suitable constructor is found.
        return checkNullConstructor(delegate(linkerServices, request), receiverClass);
    }
    // In case this was not a "new" operation, just delegate to the the standard StaticClass linker.
    return delegate(linkerServices, request);
}
 
Example #23
Source Project: openjdk-jdk8u   Author: AdoptOpenJDK   File: AssignSymbols.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void throwUnprotectedSwitchError(final VarNode varNode) {
    // Block scoped declarations in switch statements without explicit blocks should be declared
    // in a common block that contains all the case clauses. We cannot support this without a
    // fundamental rewrite of how switch statements are handled (case nodes contain blocks and are
    // directly contained by switch node). As a temporary solution we throw a reference error here.
    final String msg = ECMAErrors.getMessage("syntax.error.unprotected.switch.declaration", varNode.isLet() ? "let" : "const");
    throwParserException(msg, varNode);
}
 
Example #24
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: NashornException.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void ensureLineAndFileName() {
    if (lineAndFileNameUnknown) {
        for (final StackTraceElement ste : getStackTrace()) {
            if (ECMAErrors.isScriptFrame(ste)) {
                // Whatever here is compiled from JavaScript code
                fileName = ste.getFileName();
                line = ste.getLineNumber();
                return;
            }
        }

        lineAndFileNameUnknown = false;
    }
}
 
Example #25
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: NashornStaticClassLinker.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public GuardedInvocation getGuardedInvocation(final LinkRequest linkRequest, final LinkerServices linkerServices) throws Exception {
    final LinkRequest request = linkRequest.withoutRuntimeContext(); // Nashorn has no runtime context
    final Object self = request.getReceiver();
    if (self.getClass() != StaticClass.class) {
        return null;
    }
    final Class<?> receiverClass = ((StaticClass) self).getRepresentedClass();

    Bootstrap.checkReflectionAccess(receiverClass, true);
    final CallSiteDescriptor desc = request.getCallSiteDescriptor();
    // We intercept "new" on StaticClass instances to provide additional capabilities
    if ("new".equals(desc.getNameToken(CallSiteDescriptor.OPERATOR))) {
        if (! Modifier.isPublic(receiverClass.getModifiers())) {
            throw ECMAErrors.typeError("new.on.nonpublic.javatype", receiverClass.getName());
        }

        // make sure new is on accessible Class
        Context.checkPackageAccess(receiverClass);

        // Is the class abstract? (This includes interfaces.)
        if (NashornLinker.isAbstractClass(receiverClass)) {
            // Change this link request into a link request on the adapter class.
            final Object[] args = request.getArguments();
            args[0] = JavaAdapterFactory.getAdapterClassFor(new Class<?>[] { receiverClass }, null,
                    linkRequest.getCallSiteDescriptor().getLookup());
            final LinkRequest adapterRequest = request.replaceArguments(request.getCallSiteDescriptor(), args);
            final GuardedInvocation gi = checkNullConstructor(delegate(linkerServices, adapterRequest), receiverClass);
            // Finally, modify the guard to test for the original abstract class.
            return gi.replaceMethods(gi.getInvocation(), Guards.getIdentityGuard(self));
        }
        // If the class was not abstract, just delegate linking to the standard StaticClass linker. Make an
        // additional check to ensure we have a constructor. We could just fall through to the next "return"
        // statement, except we also insert a call to checkNullConstructor() which throws an ECMAScript TypeError
        // with a more intuitive message when no suitable constructor is found.
        return checkNullConstructor(delegate(linkerServices, request), receiverClass);
    }
    // In case this was not a "new" operation, just delegate to the the standard StaticClass linker.
    return delegate(linkerServices, request);
}
 
Example #26
Source Project: openjdk-jdk8u-backup   Author: AdoptOpenJDK   File: AssignSymbols.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void throwUnprotectedSwitchError(final VarNode varNode) {
    // Block scoped declarations in switch statements without explicit blocks should be declared
    // in a common block that contains all the case clauses. We cannot support this without a
    // fundamental rewrite of how switch statements are handled (case nodes contain blocks and are
    // directly contained by switch node). As a temporary solution we throw a reference error here.
    final String msg = ECMAErrors.getMessage("syntax.error.unprotected.switch.declaration", varNode.isLet() ? "let" : "const");
    throwParserException(msg, varNode);
}
 
Example #27
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: NashornException.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void ensureLineAndFileName() {
    if (lineAndFileNameUnknown) {
        for (final StackTraceElement ste : getStackTrace()) {
            if (ECMAErrors.isScriptFrame(ste)) {
                // Whatever here is compiled from JavaScript code
                fileName = ste.getFileName();
                line = ste.getLineNumber();
                return;
            }
        }

        lineAndFileNameUnknown = false;
    }
}
 
Example #28
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: Lower.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void throwNotImplementedYet(final String msgId, final Node node) {
    final long token = node.getToken();
    final int line = source.getLine(node.getStart());
    final int column = source.getColumn(node.getStart());
    final String message = ECMAErrors.getMessage("unimplemented." + msgId);
    final String formatted = ErrorManager.format(message, source, line, column, token);
    throw new RuntimeException(formatted);
}
 
Example #29
Source Project: hottub   Author: dsrg-uoft   File: NashornException.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void ensureLineAndFileName() {
    if (lineAndFileNameUnknown) {
        for (final StackTraceElement ste : getStackTrace()) {
            if (ECMAErrors.isScriptFrame(ste)) {
                // Whatever here is compiled from JavaScript code
                fileName = ste.getFileName();
                line = ste.getLineNumber();
                return;
            }
        }

        lineAndFileNameUnknown = false;
    }
}
 
Example #30
Source Project: jdk8u_nashorn   Author: JetBrains   File: AssignSymbols.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void throwUnprotectedSwitchError(final VarNode varNode) {
    // Block scoped declarations in switch statements without explicit blocks should be declared
    // in a common block that contains all the case clauses. We cannot support this without a
    // fundamental rewrite of how switch statements are handled (case nodes contain blocks and are
    // directly contained by switch node). As a temporary solution we throw a reference error here.
    final String msg = ECMAErrors.getMessage("syntax.error.unprotected.switch.declaration", varNode.isLet() ? "let" : "const");
    throwParserException(msg, varNode);
}