Norris Boyd wrote:
> On Aug 7, 7:11 pm, "Edwin S. Ramirez" <[EMAIL PROTECTED]> wrote:
>> I don't seem to be able to get warnings from Rhino (1.7R1). I have the
>> following code in my startup class:
>>
>>     static class MyFactory extends ContextFactory {
>>         protected boolean hasFeature(Context cx, int featureIndex) {
>>                 switch(featureIndex) {
>>                 case Context.FEATURE_DYNAMIC_SCOPE:
>>                 case Context.FEATURE_STRICT_MODE:
>>                 case Context.FEATURE_STRICT_VARS:
>>                 case Context.FEATURE_STRICT_EVAL:
>>                         return true;
>>                 }
>>                 return super.hasFeature(cx, featureIndex);
>>         }
>>      }
>>
>>      static {
>>         ContextFactory.initGlobal(new MyFactory());
>>      }
>>
>> I am executing the following JavaScript function, and I don't receive
>> any warnings.  What I am missing.
>>
>> function strict() {
>>      var a = {};
>>      debug(a.name);
>>
>>      blue = "12354";
>>      debug("Color is: " + blue);
>>
>> }
>>
>> function debug(msg) {
>>      java.System.out.println(msg+"");
>>
>> }
>>
>> Thanks,
>> Edwin S. Ramirez
> 
> I created a JUnit test just now to demonstrate this feature and for
> regression tests in the future. (It works fine.)
> 
> package org.mozilla.javascript.tests;
> 
> import junit.framework.TestCase;
> 
> import org.mozilla.javascript.Context;
> import org.mozilla.javascript.ContextAction;
> import org.mozilla.javascript.ContextFactory;
> import org.mozilla.javascript.ScriptableObject;
> import org.mozilla.javascript.EvaluatorException;
> 
> /**
>  * Test of strict mode APIs.
>  *
>  * @author Norris Boyd
>  */
> public class StrictModeApiTest extends TestCase {
> 
>   private ScriptableObject global;
>   private ContextFactory contextFactory;
> 
>   static class MyContextFactory extends ContextFactory {
>     @Override
>     protected boolean hasFeature(Context cx, int featureIndex) {
>         switch (featureIndex) {
>             case Context.FEATURE_STRICT_MODE:
>             case Context.FEATURE_STRICT_VARS:
>             case Context.FEATURE_STRICT_EVAL:
>             case Context.FEATURE_WARNING_AS_ERROR:
>                 return true;
>         }
>         return super.hasFeature(cx, featureIndex);
>     }
>   };
> 
>   public void testStrictModeError() {
>     contextFactory = new MyContextFactory();
>     Context cx = contextFactory.enterContext();
>     try {
>         global = cx.initStandardObjects();
>         try {
>             runScript("({}.nonexistent)");
>             fail();
>         } catch (EvaluatorException e) {
>             assertTrue(e.getMessage().startsWith("Reference to
> undefined property"));
>         }
>     } finally {
>         Context.exit();
>     }
>   }
> 
>   private Object runScript(final String scriptSourceText) {
>     return this.contextFactory.call(new ContextAction() {
>       public Object run(Context context) {
>           return context.evaluateString(global, scriptSourceText,
>                   "test source", 1, null);
>       }
>     });
>   }
> }
> 

I added "Context.FEATURE_WARNING_AS_ERROR" and then I get exceptions. 
However, without it, no messages are generated.  Are messages supposed 
to be generated?  Or do I have to retrieve them somehow?

Thanks,
Edwin S. Ramirez
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino

Reply via email to