Exemple #1
0
  /**
   * Utility method which dynamically binds a Context to the current thread, if none already exists.
   */
  public static Object callMethod(
      ContextFactory factory,
      final Scriptable thisObj,
      final Function f,
      final Object[] args,
      final long argsToWrap) {
    if (f == null) {
      // See comments in getFunction
      return null;
    }
    if (factory == null) {
      factory = ContextFactory.getGlobal();
    }

    final Scriptable scope = f.getParentScope();
    if (argsToWrap == 0) {
      return Context.call(factory, f, scope, thisObj, args);
    }

    Context cx = Context.getCurrentContext();
    if (cx != null) {
      return doCall(cx, scope, thisObj, f, args, argsToWrap);
    } else {
      return factory.call(
          new ContextAction() {
            public Object run(Context cx) {
              return doCall(cx, scope, thisObj, f, args, argsToWrap);
            }
          });
    }
  }
  void testWriteReadOnly(final boolean acceptWriteReadOnly) throws Exception {
    final Method readMethod = Foo.class.getMethod("getMyProp", null);
    final Foo foo = new Foo("hello");
    foo.defineProperty("myProp", null, readMethod, null, ScriptableObject.EMPTY);

    final String script = "foo.myProp = 123; foo.myProp";

    final ContextAction action =
        new ContextAction() {
          public Object run(final Context cx) {

            final ScriptableObject top = cx.initStandardObjects();
            ScriptableObject.putProperty(top, "foo", foo);

            cx.evaluateString(top, script, "script", 0, null);
            return null;
          }
        };

    final ContextFactory contextFactory =
        new ContextFactory() {
          @Override
          protected boolean hasFeature(final Context cx, final int featureIndex) {
            if (Context.FEATURE_STRICT_MODE == featureIndex) {
              return !acceptWriteReadOnly;
            }
            return super.hasFeature(cx, featureIndex);
          }
        };
    contextFactory.call(action);
  }
 public Object invoke(
     ContextFactory cf,
     final Object target,
     final Scriptable topScope,
     final Method method,
     final Object[] args) {
   ContextAction action =
       new ContextAction() {
         public Object run(Context cx) {
           return invokeImpl(cx, target, topScope, method, args);
         }
       };
   return cf.call(action);
 }
Exemple #4
0
 @NeedsContext
 private void doLint(final String javaScript) {
   contextFactory.call(
       new ContextAction() {
         public Object run(Context cx) {
           String src = javaScript == null ? "" : javaScript;
           Object[] args = new Object[] {src, optionsAsJavaScriptObject()};
           Function lintFunc = (Function) scope.get("JSLINT", scope);
           // JSLINT actually returns a boolean, but we ignore it as we always go
           // and look at the errors in more detail.
           lintFunc.call(cx, scope, scope, args);
           return null;
         }
       });
 }
  public void Serialize(final Object obj) {
    ContextFactory factory = new JsContextFactory();

    final GaeScriptableSerializer self = this;
    factory.call(
        new ContextAction() {
          @Override
          public Object run(Context cx) {
            Serialize((Scriptable) obj, obj, "/", 0);
            return null;
          }
        });

    pr.flush();
  }
Exemple #6
0
 /**
  * Turn the set of options into a JavaScript object, where the key is the name of the option and
  * the value is true.
  */
 @NeedsContext
 private Scriptable optionsAsJavaScriptObject() {
   return (Scriptable)
       contextFactory.call(
           new ContextAction() {
             public Object run(Context cx) {
               applyDefaultOptions();
               Scriptable opts = cx.newObject(scope);
               for (Entry<Option, Object> entry : options.entrySet()) {
                 String key = entry.getKey().getLowerName();
                 // Use our "custom" version in order to get native arrays.
                 Object value = Util.javaToJS(entry.getValue(), opts);
                 opts.put(key, opts, value);
               }
               return opts;
             }
           });
 }
Exemple #7
0
 @NeedsContext
 private String callReport(final boolean errorsOnly) {
   return (String)
       contextFactory.call(
           new ContextAction() {
             public Object run(Context cx) {
               Object[] args = new Object[] {Boolean.valueOf(errorsOnly)};
               Scriptable lintScope = (Scriptable) scope.get("JSLINT", scope);
               Object report = lintScope.get("report", lintScope);
               // Shouldn't happen ordinarily, but some of my tests don't have it.
               if (report == UniqueTag.NOT_FOUND) {
                 return "";
               }
               Function reportFunc = (Function) report;
               return reportFunc.call(cx, scope, scope, args);
             }
           });
 }
Exemple #8
0
  /** Assemble the {@link JSLintResult} object. */
  @NeedsContext
  private JSLintResult buildResults(
      final String systemId, final long startNanos, final long endNanos) {
    return (JSLintResult)
        contextFactory.call(
            new ContextAction() {
              public Object run(Context cx) {
                ResultBuilder b = new JSLintResult.ResultBuilder(systemId);
                b.duration(TimeUnit.NANOSECONDS.toMillis(endNanos - startNanos));
                for (Issue issue : readErrors(systemId)) {
                  b.addIssue(issue);
                }

                // Collect a report on what we've just linted.
                b.report(callReport(false));

                // Extract JSLINT.data() output and set it on the result.
                Scriptable lintScope = (Scriptable) scope.get("JSLINT", scope);
                Object o = lintScope.get("data", lintScope);
                // Real JSLINT will always have this, but some of my test stubs don't.
                if (o != UniqueTag.NOT_FOUND) {
                  Function reportFunc = (Function) o;
                  Scriptable data = (Scriptable) reportFunc.call(cx, scope, scope, new Object[] {});
                  for (String global : Util.listValueOfType("globals", String.class, data)) {
                    b.addGlobal(global);
                  }
                  for (String url : Util.listValueOfType("urls", String.class, data)) {
                    b.addUrl(url);
                  }
                  for (Entry<String, Integer> member : getDataMembers(data).entrySet()) {
                    b.addMember(member.getKey(), member.getValue());
                  }
                  b.json(Util.booleanValue("json", data));
                  for (JSFunction f :
                      Util.listValue("functions", data, new JSFunctionConverter())) {
                    b.addFunction(f);
                  }
                }
                return b.build();
              }
            });
  }