Search in sources :

Example 6 with ScriptableObject

use of org.mozilla.javascript.ScriptableObject in project hackpad by dropbox.

the class Bug466207Test method setUp.

@SuppressWarnings("unchecked")
@Override
protected void setUp() {
    // set up a reference map
    reference = new ArrayList<Object>();
    reference.add("a");
    reference.add(Boolean.TRUE);
    reference.add(new HashMap<Object, Object>());
    reference.add(new Integer(42));
    reference.add("a");
    // get a js object as map
    Context context = Context.enter();
    ScriptableObject scope = context.initStandardObjects();
    list = (List<Object>) context.evaluateString(scope, "(['a', true, new java.util.HashMap(), 42, 'a']);", "testsrc", 1, null);
    Context.exit();
}
Also used : Context(org.mozilla.javascript.Context) ScriptableObject(org.mozilla.javascript.ScriptableObject) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 7 with ScriptableObject

use of org.mozilla.javascript.ScriptableObject in project scriptographer by scriptographer.

the class RhinoEngine method observe.

@Override
public boolean observe(Map object, Object key, PropertyObserver observer) {
    if (object instanceof ScriptableObject) {
        ScriptableObject obj = (ScriptableObject) object;
        Context cx = Context.getCurrentContext();
        PropertyDescriptor desc = obj.getOwnPropertyDescriptor(cx, key);
        if (desc != null && desc.isDataDescriptor()) {
            ObserverGetterSetter getterSetter = new ObserverGetterSetter(obj, key, desc, observer);
            obj.defineOwnProperty(cx, key, new PropertyDescriptor(getterSetter, getterSetter, desc.isEnumerable(), desc.isConfigurable(), true));
        }
        return true;
    }
    return false;
}
Also used : Context(org.mozilla.javascript.Context) ScriptableObject(org.mozilla.javascript.ScriptableObject) PropertyDescriptor(org.mozilla.javascript.PropertyDescriptor)

Example 8 with ScriptableObject

use of org.mozilla.javascript.ScriptableObject in project scriptographer by scriptographer.

the class RhinoScope method put.

public Object put(String name, Object value, boolean readOnly) {
    Object prev = get(name);
    value = Context.javaToJS(value, scope);
    if (scope instanceof ScriptableObject) {
        // Remove READONLY attribute first if the field already existed,
        // to make sure new value can be set
        ScriptableObject scriptable = (ScriptableObject) scope;
        if (scriptable.has(name, scriptable))
            scriptable.setAttributes(name, ScriptableObject.EMPTY);
        if (readOnly) {
            scriptable.defineProperty(name, value, ScriptableObject.READONLY);
            return prev;
        }
    }
    scope.put(name, scope, value);
    return prev;
}
Also used : ScriptableObject(org.mozilla.javascript.ScriptableObject) ScriptableObject(org.mozilla.javascript.ScriptableObject)

Example 9 with ScriptableObject

use of org.mozilla.javascript.ScriptableObject in project scriptographer by scriptographer.

the class TopLevel method initStandardObjects.

public void initStandardObjects(Context cx, boolean sealed) {
    super.initStandardObjects(cx, sealed);
    for (int i = 0; i != topPackages.length; i += 2) new LazilyLoadedCtor(this, topPackages[i], topPackages[i + 1], false);
    // define some global functions and objects:
    String[] names = { "print", "evaluate" };
    defineFunctionProperties(names, TopLevel.class, ScriptableObject.DONTENUM);
    ScriptableObject objProto = (ScriptableObject) getObjectPrototype(this);
    objProto.defineFunctionProperties(new String[] { "dontEnum", "toJava", "print", "evaluate" }, TopLevel.class, DONTENUM);
}
Also used : ScriptableObject(org.mozilla.javascript.ScriptableObject) LazilyLoadedCtor(org.mozilla.javascript.LazilyLoadedCtor)

Example 10 with ScriptableObject

use of org.mozilla.javascript.ScriptableObject in project scriptographer by scriptographer.

the class TopLevel method defineProperty.

public static void defineProperty(ScriptableObject obj, String name, String getter, String setter) throws SecurityException, NoSuchMethodException {
    Class<? extends ScriptableObject> cls = obj.getClass();
    Method getterMethod = getter != null ? cls.getDeclaredMethod(getter, new Class[] { Scriptable.class }) : null;
    Method setterMethod = setter != null ? cls.getDeclaredMethod(setter, new Class[] { Scriptable.class, Object.class }) : null;
    obj.defineProperty(name, null, getterMethod, setterMethod, ScriptableObject.DONTENUM);
}
Also used : NativeJavaObject(org.mozilla.javascript.NativeJavaObject) ScriptableObject(org.mozilla.javascript.ScriptableObject) Method(java.lang.reflect.Method) Scriptable(org.mozilla.javascript.Scriptable)

Aggregations

ScriptableObject (org.mozilla.javascript.ScriptableObject)44 Context (org.mozilla.javascript.Context)20 ScriptReader (org.jaggeryjs.jaggery.core.ScriptReader)8 ScriptException (org.jaggeryjs.scriptengine.exceptions.ScriptException)8 ContextAction (org.mozilla.javascript.ContextAction)6 ContextFactory (org.mozilla.javascript.ContextFactory)6 Scriptable (org.mozilla.javascript.Scriptable)6 JaggeryContext (org.jaggeryjs.scriptengine.engine.JaggeryContext)5 RhinoEngine (org.jaggeryjs.scriptengine.engine.RhinoEngine)5 IOException (java.io.IOException)4 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)3 StringReader (java.io.StringReader)3 Method (java.lang.reflect.Method)3 List (java.util.List)3 ServletContext (javax.servlet.ServletContext)3 Function (org.mozilla.javascript.Function)3 Function (com.google.common.base.Function)2 PrintWriter (java.io.PrintWriter)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2