org.mozilla.javascript.EcmaError Java Examples

The following examples show how to use org.mozilla.javascript.EcmaError. 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 File: DomBasedScriptingEngineFactory.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * load
 * @param response
 */
public void load( WebResponse response ) {
		Function onLoadEvent=null;
    try {
        Context context = Context.enter();
        context.initStandardObjects( null );

        HTMLDocument htmlDocument = ((DomWindow) response.getScriptingHandler()).getDocument();
        if (!(htmlDocument instanceof HTMLDocumentImpl)) return;

        HTMLBodyElementImpl body = (HTMLBodyElementImpl) htmlDocument.getBody();
        if (body == null) return;
        onLoadEvent = body.getOnloadEvent();
        if (onLoadEvent == null) return;
        onLoadEvent.call( context, body, body, new Object[0] );
    } catch (JavaScriptException e) {
    	ScriptingEngineImpl.handleScriptException(e, onLoadEvent.toString());
    	// HttpUnitUtils.handleException(e);
    } catch (EcmaError ee) {
    	//throw ee;
    	ScriptingEngineImpl.handleScriptException(ee, onLoadEvent.toString());        	
    } finally {
        Context.exit();
    }
}
 
Example #2
Source File: WriteReadOnlyPropertyTest.java    From rhino-android with Apache License 2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void testWriteReadOnly_throws() throws Exception {
	try {
		testWriteReadOnly(false);
		Assert.fail();
	}
	catch (EcmaError e) {
		Assert.assertTrue(e.getMessage(), e.getMessage().contains("Cannot set property myProp that has only a getter"));
	}
}
 
Example #3
Source File: JsScriptEngine.java    From spork with Apache License 2.0 5 votes vote down vote up
/**
 * evaluate a javascript String
 * @param name the name of the script (for error messages)
 * @param script the content of the script
 * @return the result
 */
public Object jsEval(String name, String script) {
    try {
        return getContext().evaluateString(scope, script, name, 1, null);
    } catch (EcmaError e) {
        throw new RuntimeException("can't evaluate "+name+": "+script,e);
    }
}
 
Example #4
Source File: WriteReadOnlyPropertyTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @throws Exception if the test fails
 */
@Test
public void testWriteReadOnly_throws() throws Exception {
	try {
		testWriteReadOnly(false);
		Assert.fail();
	}
	catch (EcmaError e) {
		Assert.assertTrue(e.getMessage(), e.getMessage().contains("Cannot set property myProp that has only a getter"));
	}
}
 
Example #5
Source File: EcmaScriptDataModel.java    From JVoiceXML with GNU Lesser General Public License v2.1 5 votes vote down vote up
private <T extends Object> T evaluateExpression(final String expr,
        final Scope scope, final Scriptable start, final Class<T> type)
        throws SemanticError {
    if (start == null) {
        throw new SemanticError("no scope '" + scope
                + "' present to evaluate '" + expr + "'");
    }
    final String preparedExpression = prepareExpression(expr);
    if (preparedExpression == null) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("ignoring empty value expr");
        }
        return null;
    }
    try {
        final Context context = getContext();
        final Object value = context.evaluateString(start,
                preparedExpression, "expr", 1, null);
        if (value == getUndefinedValue()) {
            return null;
        }
        if (LOGGER.isDebugEnabled()) {
            final String json = toString(value);
            LOGGER.debug("evaluated '" + preparedExpression + "' to '"
                    + json + "'");
        }
        @SuppressWarnings("unchecked")
        final T t = (T) Context.jsToJava(value, type);
        return t;
    } catch (EcmaError | EvaluatorException e) {
        final String message = "error evaluating '" + preparedExpression
                + "'";
        LOGGER.warn(message, e);
        final String concatenatedMessage = getConcatenadedErrorMessage(
                message, e);
        throw new SemanticError(concatenatedMessage, e);
    }
}
 
Example #6
Source File: Test262RegExpTest.java    From rhino-android with Apache License 2.0 4 votes vote down vote up
@Test(expected = EcmaError.class)
public void testS15_10_2_15_A1_T7() {
    String source = "(new RegExp('[\\\\wb-G]').exec('a'))";
    String sourceName = "Conformance/15_Native/15.10_RegExp_Objects/15.10.2_Pattern_Semantics/15.10.2.15_NonemptyClassRanges/S15.10.2.15_A1_T7.js";
    cx.evaluateString(scope, source, sourceName, 0, null);
}
 
Example #7
Source File: Test262RegExpTest.java    From rhino-android with Apache License 2.0 4 votes vote down vote up
@Test(expected = EcmaError.class)
public void testS15_10_2_15_A1_T8() {
    String source = "(new RegExp('[\\\\Wb-G]').exec('a'))";
    String sourceName = "Conformance/15_Native/15.10_RegExp_Objects/15.10.2_Pattern_Semantics/15.10.2.15_NonemptyClassRanges/S15.10.2.15_A1_T8.js";
    cx.evaluateString(scope, source, sourceName, 0, null);
}
 
Example #8
Source File: Test262RegExpTest.java    From rhino-android with Apache License 2.0 4 votes vote down vote up
@Test(expected = EcmaError.class)
public void testS15_10_2_15_A1_T6() {
    String source = "(new RegExp('[\\\\Sb-G]').exec('a'))";
    String sourceName = "Conformance/15_Native/15.10_RegExp_Objects/15.10.2_Pattern_Semantics/15.10.2.15_NonemptyClassRanges/S15.10.2.15_A1_T6.js";
    cx.evaluateString(scope, source, sourceName, 0, null);
}
 
Example #9
Source File: TransformEngineImpl.java    From p3-batchrefine with Apache License 2.0 4 votes vote down vote up
/**
     * This method runs part of the core module controller.js script so that we
     * can register all operations without having to duplicate configuration
     * here.
     */

    public void loadModule(String name, String path, String... initFunctions)
            throws IOException {

        ButterflyModule core = new ButterflyModuleStub(name);
//        File controllerFile = new File(Configurations.get("refine.root",
//                "../OpenRefine"), path);

        String script = IOUtils.toString(getClass().getClassLoader().getResourceAsStream(path));

        if (script == null) {
            fLogger.warn(String.format(
                    "Can't find controller script for module %s at %s -- "
                            + "module may not work as expected.", name,
                    name));
            return;
        }

        // Compiles and "executes" the controller script. The script basically
        // contains function declarations.
        Context context = ContextFactory.getGlobal().enterContext();
        Script controller = context.compileString(
                script, "init.js", 1, null);

        // Initializes the scope.
        ScriptableObject scope = new ImporterTopLevel(context);

        scope.put("module", scope, core);
        controller.exec(context, scope);

        for (String function : initFunctions) {
            // Runs the function that initializes the OperationRegistry.
            try {
                Object fun = context.compileString(function, null, 1, null)
                        .exec(context, scope);
                if (fun != null && fun instanceof Function) {
                    ((Function) fun).call(context, scope, scope,
                            new Object[]{});
                }
            } catch (EcmaError ex) {
                fLogger.error("Error running controller script.", ex);
            }
        }
    }
 
Example #10
Source File: Test262RegExpTest.java    From rhino-android with Apache License 2.0 4 votes vote down vote up
@Test(expected = EcmaError.class)
public void testS15_10_2_15_A1_T5() {
    String source = "(new RegExp('[\\\\sb-G]').exec('a'))";
    String sourceName = "Conformance/15_Native/15.10_RegExp_Objects/15.10.2_Pattern_Semantics/15.10.2.15_NonemptyClassRanges/S15.10.2.15_A1_T5.js";
    cx.evaluateString(scope, source, sourceName, 0, null);
}
 
Example #11
Source File: Test262RegExpTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected = EcmaError.class)
public void testS15_10_2_15_A1_T4() {
    String source = "(new RegExp('[\\\\Db-G]').exec('a'))";
    String sourceName = "Conformance/15_Native/15.10_RegExp_Objects/15.10.2_Pattern_Semantics/15.10.2.15_NonemptyClassRanges/S15.10.2.15_A1_T4.js";
    cx.evaluateString(scope, source, sourceName, 0, null);
}
 
Example #12
Source File: Test262RegExpTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected = EcmaError.class)
public void testS15_10_2_15_A1_T5() {
    String source = "(new RegExp('[\\\\sb-G]').exec('a'))";
    String sourceName = "Conformance/15_Native/15.10_RegExp_Objects/15.10.2_Pattern_Semantics/15.10.2.15_NonemptyClassRanges/S15.10.2.15_A1_T5.js";
    cx.evaluateString(scope, source, sourceName, 0, null);
}
 
Example #13
Source File: Test262RegExpTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected = EcmaError.class)
public void testS15_10_2_15_A1_T6() {
    String source = "(new RegExp('[\\\\Sb-G]').exec('a'))";
    String sourceName = "Conformance/15_Native/15.10_RegExp_Objects/15.10.2_Pattern_Semantics/15.10.2.15_NonemptyClassRanges/S15.10.2.15_A1_T6.js";
    cx.evaluateString(scope, source, sourceName, 0, null);
}
 
Example #14
Source File: Test262RegExpTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected = EcmaError.class)
public void testS15_10_2_15_A1_T7() {
    String source = "(new RegExp('[\\\\wb-G]').exec('a'))";
    String sourceName = "Conformance/15_Native/15.10_RegExp_Objects/15.10.2_Pattern_Semantics/15.10.2.15_NonemptyClassRanges/S15.10.2.15_A1_T7.js";
    cx.evaluateString(scope, source, sourceName, 0, null);
}
 
Example #15
Source File: Test262RegExpTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test(expected = EcmaError.class)
public void testS15_10_2_15_A1_T8() {
    String source = "(new RegExp('[\\\\Wb-G]').exec('a'))";
    String sourceName = "Conformance/15_Native/15.10_RegExp_Objects/15.10.2_Pattern_Semantics/15.10.2.15_NonemptyClassRanges/S15.10.2.15_A1_T8.js";
    cx.evaluateString(scope, source, sourceName, 0, null);
}
 
Example #16
Source File: Test262RegExpTest.java    From rhino-android with Apache License 2.0 4 votes vote down vote up
@Test(expected = EcmaError.class)
public void testS15_10_2_15_A1_T4() {
    String source = "(new RegExp('[\\\\Db-G]').exec('a'))";
    String sourceName = "Conformance/15_Native/15.10_RegExp_Objects/15.10.2_Pattern_Semantics/15.10.2.15_NonemptyClassRanges/S15.10.2.15_A1_T4.js";
    cx.evaluateString(scope, source, sourceName, 0, null);
}
 
Example #17
Source File: ExtensionErrorConstructor.java    From io with Apache License 2.0 2 votes vote down vote up
/**
 * エラーオブジェクトを生成する.
 * @param message エラーメッセージ
 * @return エラーオブジェクト
 */
public static EcmaError construct(String message) {
    return ScriptRuntime.constructError("Error", message);
}