Java Code Examples for jdk.nashorn.api.scripting.ScriptObjectMirror

The following examples show how to use jdk.nashorn.api.scripting.ScriptObjectMirror. These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: karate   Source File: Script.java    License: MIT License 6 votes vote down vote up
public static ScriptValue evalJsFunctionCall(ScriptObjectMirror som, Object callArg, ScenarioContext context) {
    Object result;
    try {
        if (callArg != null) {
            result = som.call(som, callArg);
        } else {
            result = som.call(som);
        }
        return new ScriptValue(result);
    } catch (Exception e) {
        String message = "javascript function call failed: " + e.getMessage();
        context.logger.error(message);
        context.logger.error("failed function body: " + som);
        throw new KarateException(message);
    }
}
 
Example 2
Source Project: hottub   Source File: WithObject.java    License: GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private static Object bindToExpression(final Object fn, final Object receiver) {
    if (fn instanceof ScriptFunction) {
        return bindToExpression((ScriptFunction) fn, receiver);
    } else if (fn instanceof ScriptObjectMirror) {
        final ScriptObjectMirror mirror = (ScriptObjectMirror)fn;
        if (mirror.isFunction()) {
            // We need to make sure correct 'this' is used for calls with Ident call
            // expressions. We do so here using an AbstractJSObject instance.
            return new AbstractJSObject() {
                @Override
                public Object call(final Object thiz, final Object... args) {
                    return mirror.call(withFilterExpression(receiver), args);
                }
            };
        }
    }

    return fn;
}
 
Example 3
@Test
public void indexPropertiesExternalBufferTest() throws ScriptException {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final ScriptObjectMirror obj = (ScriptObjectMirror)e.eval("var obj = {}; obj");
    final ByteBuffer buf = ByteBuffer.allocate(5);
    int i;
    for (i = 0; i < 5; i++) {
        buf.put(i, (byte)(i+10));
    }
    obj.setIndexedPropertiesToExternalArrayData(buf);

    for (i = 0; i < 5; i++) {
        assertEquals((byte)(i+10), ((Number)e.eval("obj[" + i + "]")).byteValue());
    }

    e.eval("for (i = 0; i < 5; i++) obj[i] = 0");
    for (i = 0; i < 5; i++) {
        assertEquals((byte)0, ((Number)e.eval("obj[" + i + "]")).byteValue());
        assertEquals((byte)0, buf.get(i));
    }
}
 
Example 4
Source Project: openjdk-jdk8u   Source File: WithObject.java    License: GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unused")
private static Object bindToExpression(final Object fn, final Object receiver) {
    if (fn instanceof ScriptFunction) {
        return bindToExpression((ScriptFunction) fn, receiver);
    } else if (fn instanceof ScriptObjectMirror) {
        final ScriptObjectMirror mirror = (ScriptObjectMirror)fn;
        if (mirror.isFunction()) {
            // We need to make sure correct 'this' is used for calls with Ident call
            // expressions. We do so here using an AbstractJSObject instance.
            return new AbstractJSObject() {
                @Override
                public Object call(final Object thiz, final Object... args) {
                    return mirror.call(withFilterExpression(receiver), args);
                }
            };
        }
    }

    return fn;
}
 
Example 5
@Test
public void functionalInterfaceObjectTest() throws Exception {
    final ScriptEngineManager manager = new ScriptEngineManager();
    final ScriptEngine e = manager.getEngineByName("nashorn");
    final AtomicBoolean invoked = new AtomicBoolean(false);
    e.put("c", new Consumer<Object>() {
        @Override
        public void accept(Object t) {
            assertTrue(t instanceof ScriptObjectMirror);
            assertEquals(((ScriptObjectMirror)t).get("a"), "xyz");
            invoked.set(true);
        }
    });
    e.eval("var x = 'xy'; x += 'z';c({a:x})");
    assertTrue(invoked.get());
}
 
Example 6
/**
 * ECMA 15.2.3.2 Object.getPrototypeOf ( O )
 *
 * @param  self self reference
 * @param  obj object to get prototype from
 * @return the prototype of an object
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object getPrototypeOf(final Object self, final Object obj) {
    if (obj instanceof ScriptObject) {
        return ((ScriptObject)obj).getProto();
    } else if (obj instanceof ScriptObjectMirror) {
        return ((ScriptObjectMirror)obj).getProto();
    } else {
        final JSType type = JSType.of(obj);
        if (type == JSType.OBJECT) {
            // host (Java) objects have null __proto__
            return null;
        }

        // must be some JS primitive
        throw notAnObject(obj);
    }
}
 
Example 7
@Test
public void functionalInterfaceObjectTest() throws Exception {
    final ScriptEngineManager manager = new ScriptEngineManager();
    final ScriptEngine e = manager.getEngineByName("nashorn");
    final AtomicBoolean invoked = new AtomicBoolean(false);
    e.put("c", new Consumer<Object>() {
        @Override
        public void accept(Object t) {
            assertTrue(t instanceof ScriptObjectMirror);
            assertEquals(((ScriptObjectMirror)t).get("a"), "xyz");
            invoked.set(true);
        }
    });
    e.eval("var x = 'xy'; x += 'z';c({a:x})");
    assertTrue(invoked.get());
}
 
Example 8
Source Project: HeavenMS   Source File: EventManager.java    License: GNU Affero General Public License v3.0 6 votes vote down vote up
private List<Integer> getLobbyRange() {
    try {
        if (!ServerConstants.JAVA_8) {
            return convertToIntegerArray((List<Object>)iv.invokeFunction("setLobbyRange", (Object) null));
        } else {  // java 8 support here thanks to MedicOP
            ScriptObjectMirror object = (ScriptObjectMirror) iv.invokeFunction("setLobbyRange", (Object) null);
            int[] to = object.to(int[].class);
            List<Integer> list = new ArrayList<>();
            for (int i : to) {
                list.add(i);
            }
            return list;

        }
    } catch (ScriptException | NoSuchMethodException ex) { // they didn't define a lobby range
        List<Integer> defaultRange = new ArrayList<>();
        defaultRange.add(0);
        defaultRange.add(maxLobbys);
        
        return defaultRange;
    }
}
 
Example 9
@Test
public void mirrorNewObjectInstanceFunctionTest() throws ScriptException {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final ScriptEngine e2 = m.getEngineByName("nashorn");

    e.eval("function func() {}");
    e2.put("func", e.get("func"));
    final ScriptObjectMirror e2obj = (ScriptObjectMirror)e2.eval("({ foo: func })");
    final Object newObj = ((ScriptObjectMirror)e2obj.getMember("foo")).newObject();
    assertTrue(newObj instanceof ScriptObjectMirror);
}
 
Example 10
Source Project: nashorn   Source File: NativeObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.2.3.9 Object.freeze ( O )
 *
 * @param self self reference
 * @param obj object to freeze
 * @return frozen object
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object freeze(final Object self, final Object obj) {
    if (obj instanceof ScriptObject) {
        return ((ScriptObject)obj).freeze();
    } else if (obj instanceof ScriptObjectMirror) {
        return ((ScriptObjectMirror)obj).freeze();
    } else {
        throw notAnObject(obj);
    }
}
 
Example 11
Source Project: hottub   Source File: NativeObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.2.3.10 Object.preventExtensions ( O )
 *
 * @param self self reference
 * @param obj  object, for which to set the internal extensible property to false
 * @return object
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object preventExtensions(final Object self, final Object obj) {
    if (obj instanceof ScriptObject) {
        return ((ScriptObject)obj).preventExtensions();
    } else if (obj instanceof ScriptObjectMirror) {
        return ((ScriptObjectMirror)obj).preventExtensions();
    } else {
        throw notAnObject(obj);
    }
}
 
Example 12
Source Project: openjdk-jdk8u   Source File: Context.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Implementation of {@code loadWithNewGlobal} Nashorn extension. Load a script file from a source
 * expression, after creating a new global scope.
 *
 * @param from source expression for script
 * @param args (optional) arguments to be passed to the loaded script
 *
 * @return return value for load call (undefined)
 *
 * @throws IOException if source cannot be found or loaded
 */
public Object loadWithNewGlobal(final Object from, final Object...args) throws IOException {
    final Global oldGlobal = getGlobal();
    final Global newGlobal = AccessController.doPrivileged(new PrivilegedAction<Global>() {
       @Override
       public Global run() {
           try {
               return newGlobal();
           } catch (final RuntimeException e) {
               if (Context.DEBUG) {
                   e.printStackTrace();
               }
               throw e;
           }
       }
    }, CREATE_GLOBAL_ACC_CTXT);
    // initialize newly created Global instance
    initGlobal(newGlobal);
    setGlobal(newGlobal);

    final Object[] wrapped = args == null? ScriptRuntime.EMPTY_ARRAY :  ScriptObjectMirror.wrapArray(args, oldGlobal);
    newGlobal.put("arguments", newGlobal.wrapAsObject(wrapped), env._strict);

    try {
        // wrap objects from newGlobal's world as mirrors - but if result
        // is from oldGlobal's world, unwrap it!
        return ScriptObjectMirror.unwrap(ScriptObjectMirror.wrap(load(newGlobal, from), newGlobal), oldGlobal);
    } finally {
        setGlobal(oldGlobal);
    }
}
 
Example 13
Source Project: TencentKona-8   Source File: NativeObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.2.3.10 Object.preventExtensions ( O )
 *
 * @param self self reference
 * @param obj  object, for which to set the internal extensible property to false
 * @return object
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object preventExtensions(final Object self, final Object obj) {
    if (obj instanceof ScriptObject) {
        return ((ScriptObject)obj).preventExtensions();
    } else if (obj instanceof ScriptObjectMirror) {
        return ((ScriptObjectMirror)obj).preventExtensions();
    } else {
        throw notAnObject(obj);
    }
}
 
Example 14
@Test
public void mirrorNewObjectGlobalFunctionTest() throws ScriptException {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final ScriptEngine e2 = m.getEngineByName("nashorn");

    e.eval("function func() {}");
    e2.put("foo", e.get("func"));
    final ScriptObjectMirror e2global = (ScriptObjectMirror)e2.eval("this");
    final Object newObj = ((ScriptObjectMirror)e2global.getMember("foo")).newObject();
    assertTrue(newObj instanceof ScriptObjectMirror);
}
 
Example 15
Source Project: vertx-codetrans   Source File: ConversionTestBase.java    License: Apache License 2.0 5 votes vote down vote up
public JsonObject unwrapJsonObject(ScriptObjectMirror obj) {
  JsonObject unwrapped = new JsonObject();
  for (String key : obj.getOwnKeys(true)) {
    Object value = obj.get(key);
    if (value instanceof ScriptObjectMirror) {
      value = unwrapJsonElement((ScriptObjectMirror) value);
    }
    unwrapped.put(key, value);
  }
  return unwrapped;
}
 
Example 16
Source Project: TencentKona-8   Source File: NativeObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.2.3.14 Object.keys ( O )
 *
 * @param self self reference
 * @param obj  object from which to extract keys
 * @return array of keys in object
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static ScriptObject keys(final Object self, final Object obj) {
    if (obj instanceof ScriptObject) {
        final ScriptObject sobj = (ScriptObject)obj;
        return new NativeArray(sobj.getOwnKeys(false));
    } else if (obj instanceof ScriptObjectMirror) {
        final ScriptObjectMirror sobjMirror = (ScriptObjectMirror)obj;
        return new NativeArray(sobjMirror.getOwnKeys(false));
    } else {
        throw notAnObject(obj);
    }
}
 
Example 17
Source Project: TencentKona-8   Source File: JSObjectLinker.java    License: GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unused")
private static Object jsObjectScopeCall(final JSObject jsObj, final Object thiz, final Object[] args) {
    final Object modifiedThiz;
    if (thiz == ScriptRuntime.UNDEFINED && !jsObj.isStrictFunction()) {
        final Global global = Context.getGlobal();
        modifiedThiz = ScriptObjectMirror.wrap(global, global);
    } else {
        modifiedThiz = thiz;
    }
    return jsObj.call(modifiedThiz, args);
}
 
Example 18
Source Project: smockin   Source File: JavaScriptResponseHandlerImpl.java    License: Apache License 2.0 5 votes vote down vote up
Set<Map.Entry<String, String>> convertResponseHeaders(final ScriptObjectMirror response) {

        final Object headersJS = response.get("headers");

        final Map<String, String> responseHeaders = new HashMap<>();

        if (headersJS instanceof ScriptObjectMirror) {
            ((ScriptObjectMirror) headersJS)
                    .entrySet()
                    .forEach(e ->
                            responseHeaders.put(e.getKey(), (String)e.getValue()));
        }

        return responseHeaders.entrySet();
    }
 
Example 19
Source Project: nashorn   Source File: NativeObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.2.3.12 Object.isFrozen ( O )
 *
 * @param self self reference
 * @param obj check whether an object
 * @return true if object is frozen, false otherwise
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object isFrozen(final Object self, final Object obj) {
    if (obj instanceof ScriptObject) {
        return ((ScriptObject)obj).isFrozen();
    } else if (obj instanceof ScriptObjectMirror) {
        return ((ScriptObjectMirror)obj).isFrozen();
    } else {
        throw notAnObject(obj);
    }
}
 
Example 20
Source Project: TencentKona-8   Source File: ScriptRuntime.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 11.4.1 - delete operation, generic implementation
 *
 * @param obj       object with property to delete
 * @param property  property to delete
 * @param strict    are we in strict mode
 *
 * @return true if property was successfully found and deleted
 */
public static boolean DELETE(final Object obj, final Object property, final Object strict) {
    if (obj instanceof ScriptObject) {
        return ((ScriptObject)obj).delete(property, Boolean.TRUE.equals(strict));
    }

    if (obj instanceof Undefined) {
        return ((Undefined)obj).delete(property, false);
    }

    if (obj == null) {
        throw typeError("cant.delete.property", safeToString(property), "null");
    }

    if (obj instanceof ScriptObjectMirror) {
        return ((ScriptObjectMirror)obj).delete(property);
    }

    if (JSType.isPrimitive(obj)) {
        return ((ScriptObject) JSType.toScriptObject(obj)).delete(property, Boolean.TRUE.equals(strict));
    }

    if (obj instanceof JSObject) {
        ((JSObject)obj).removeMember(Objects.toString(property));
        return true;
    }

    // if object is not reference type, vacuously delete is successful.
    return true;
}
 
Example 21
Source Project: openjdk-jdk9   Source File: NativeObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.2.3.10 Object.preventExtensions ( O )
 *
 * @param self self reference
 * @param obj  object, for which to set the internal extensible property to false
 * @return object
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object preventExtensions(final Object self, final Object obj) {
    if (obj instanceof ScriptObject) {
        return ((ScriptObject)obj).preventExtensions();
    } else if (obj instanceof ScriptObjectMirror) {
        return ((ScriptObjectMirror)obj).preventExtensions();
    } else {
        throw notAnObject(obj);
    }
}
 
Example 22
Source Project: jdk8u_nashorn   Source File: JSObjectLinker.java    License: GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unused")
private static Object jsObjectScopeCall(final JSObject jsObj, final Object thiz, final Object[] args) {
    final Object modifiedThiz;
    if (thiz == ScriptRuntime.UNDEFINED && !jsObj.isStrictFunction()) {
        final Global global = Context.getGlobal();
        modifiedThiz = ScriptObjectMirror.wrap(global, global);
    } else {
        modifiedThiz = thiz;
    }
    return jsObj.call(modifiedThiz, args);
}
 
Example 23
Source Project: openjdk-jdk9   Source File: ListAdapter.java    License: GNU General Public License v2.0 5 votes vote down vote up
private static JSObject getJSObject(final Object obj, final Global global) {
    if (obj instanceof ScriptObject) {
        return (JSObject)ScriptObjectMirror.wrap(obj, global);
    } else if (obj instanceof JSObject) {
        return (JSObject)obj;
    }
    throw new IllegalArgumentException("ScriptObject or JSObject expected");
}
 
Example 24
Source Project: openjdk-jdk8u   Source File: NativeObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.2.3.11 Object.isSealed ( O )
 *
 * @param self self reference
 * @param obj check whether an object is sealed
 * @return true if sealed, false otherwise
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static boolean isSealed(final Object self, final Object obj) {
    if (obj instanceof ScriptObject) {
        return ((ScriptObject)obj).isSealed();
    } else if (obj instanceof ScriptObjectMirror) {
        return ((ScriptObjectMirror)obj).isSealed();
    } else {
        throw notAnObject(obj);
    }
}
 
Example 25
Source Project: openjdk-8   Source File: NativeObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.2.3.13 Object.isExtensible ( O )
 *
 * @param self self reference
 * @param obj check whether an object is extensible
 * @return true if object is extensible, false otherwise
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object isExtensible(final Object self, final Object obj) {
    if (obj instanceof ScriptObject) {
        return ((ScriptObject)obj).isExtensible();
    } else if (obj instanceof ScriptObjectMirror) {
        return ((ScriptObjectMirror)obj).isExtensible();
    } else {
        throw notAnObject(obj);
    }
}
 
Example 26
public void test1(final String x, final Object y, final ScriptObject w) {
    Assert.assertEquals(x, "true");

    Assert.assertTrue(y instanceof ScriptObjectMirror);
    Assert.assertEquals(((ScriptObjectMirror)y).get("foo"), 1);

    Assert.assertEquals(w.get("bar"), 2);
}
 
Example 27
@Test
public void checkMirrorToObject() throws Exception {
    final ScriptEngineManager engineManager = new ScriptEngineManager();
    final ScriptEngine engine = engineManager.getEngineByName("nashorn");
    final Invocable invocable = (Invocable)engine;

    engine.eval("function test1(arg) { return { arg: arg }; }");
    engine.eval("function test2(arg) { return arg; }");
    engine.eval("function compare(arg1, arg2) { return arg1 == arg2; }");

    final Map<String, Object> map = new HashMap<>();
    map.put("option", true);

    final MirrorCheckExample example = invocable.getInterface(MirrorCheckExample.class);

    final Object value1 = invocable.invokeFunction("test1", map);
    final Object value2 = example.test1(map);
    final Object value3 = invocable.invokeFunction("test2", value2);
    final Object value4 = example.test2(value2);

    // check that Object type argument receives a ScriptObjectMirror
    // when ScriptObject is passed
    assertEquals(ScriptObjectMirror.class, value1.getClass());
    assertEquals(ScriptObjectMirror.class, value2.getClass());
    assertEquals(ScriptObjectMirror.class, value3.getClass());
    assertEquals(ScriptObjectMirror.class, value4.getClass());
    assertTrue((boolean)invocable.invokeFunction("compare", value1, value1));
    assertTrue(example.compare(value1, value1));
    assertTrue((boolean)invocable.invokeFunction("compare", value3, value4));
    assertTrue(example.compare(value3, value4));
}
 
Example 28
Source Project: hottub   Source File: NativeObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.2.3.12 Object.isFrozen ( O )
 *
 * @param self self reference
 * @param obj check whether an object
 * @return true if object is frozen, false otherwise
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static boolean isFrozen(final Object self, final Object obj) {
    if (obj instanceof ScriptObject) {
        return ((ScriptObject)obj).isFrozen();
    } else if (obj instanceof ScriptObjectMirror) {
        return ((ScriptObjectMirror)obj).isFrozen();
    } else {
        throw notAnObject(obj);
    }
}
 
Example 29
Source Project: jdk8u_nashorn   Source File: NativeObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.2.3.8 Object.seal ( O )
 *
 * @param self self reference
 * @param obj  object to seal
 * @return sealed object
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object seal(final Object self, final Object obj) {
    if (obj instanceof ScriptObject) {
        return ((ScriptObject)obj).seal();
    } else if (obj instanceof ScriptObjectMirror) {
        return ((ScriptObjectMirror)obj).seal();
    } else {
        throw notAnObject(obj);
    }
}
 
Example 30
Source Project: rawhttp   Source File: JsTestReporter.java    License: Apache License 2.0 5 votes vote down vote up
public void failure(ScriptObjectMirror objectMirror) {
    long endTime = System.currentTimeMillis();
    httpTestsReporter.report(new HttpTestResult(
            (String) objectMirror.get("name"),
            (long) objectMirror.get("time"),
            endTime,
            objectMirror.get("error").toString()));
}
 
Example 31
Source Project: openjdk-jdk9   Source File: NativeObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.2.3.13 Object.isExtensible ( O )
 *
 * @param self self reference
 * @param obj check whether an object is extensible
 * @return true if object is extensible, false otherwise
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static boolean isExtensible(final Object self, final Object obj) {
    if (obj instanceof ScriptObject) {
        return ((ScriptObject)obj).isExtensible();
    } else if (obj instanceof ScriptObjectMirror) {
        return ((ScriptObjectMirror)obj).isExtensible();
    } else {
        throw notAnObject(obj);
    }
}
 
Example 32
Source Project: openjdk-8   Source File: NativeObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.2.3.12 Object.isFrozen ( O )
 *
 * @param self self reference
 * @param obj check whether an object
 * @return true if object is frozen, false otherwise
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object isFrozen(final Object self, final Object obj) {
    if (obj instanceof ScriptObject) {
        return ((ScriptObject)obj).isFrozen();
    } else if (obj instanceof ScriptObjectMirror) {
        return ((ScriptObjectMirror)obj).isFrozen();
    } else {
        throw notAnObject(obj);
    }
}
 
Example 33
Source Project: hottub   Source File: ListAdapter.java    License: GNU General Public License v2.0 5 votes vote down vote up
private static JSObject getJSObject(final Object obj, final Global global) {
    if (obj instanceof ScriptObject) {
        return (JSObject)ScriptObjectMirror.wrap(obj, global);
    } else if (obj instanceof JSObject) {
        return (JSObject)obj;
    }
    throw new IllegalArgumentException("ScriptObject or JSObject expected");
}
 
Example 34
/**
 * ECMA 15.2.3.9 Object.freeze ( O )
 *
 * @param self self reference
 * @param obj object to freeze
 * @return frozen object
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object freeze(final Object self, final Object obj) {
    if (obj instanceof ScriptObject) {
        return ((ScriptObject)obj).freeze();
    } else if (obj instanceof ScriptObjectMirror) {
        return ((ScriptObjectMirror)obj).freeze();
    } else {
        throw notAnObject(obj);
    }
}
 
Example 35
@Test
public void mirrorNewObjectInstanceFunctionTest() throws ScriptException {
    final ScriptEngineManager m = new ScriptEngineManager();
    final ScriptEngine e = m.getEngineByName("nashorn");
    final ScriptEngine e2 = m.getEngineByName("nashorn");

    e.eval("function func() {}");
    e2.put("func", e.get("func"));
    final ScriptObjectMirror e2obj = (ScriptObjectMirror)e2.eval("({ foo: func })");
    final Object newObj = ((ScriptObjectMirror)e2obj.getMember("foo")).newObject();
    assertTrue(newObj instanceof ScriptObjectMirror);
}
 
Example 36
Source Project: hottub   Source File: NativeObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.2.3.8 Object.seal ( O )
 *
 * @param self self reference
 * @param obj  object to seal
 * @return sealed object
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static Object seal(final Object self, final Object obj) {
    if (obj instanceof ScriptObject) {
        return ((ScriptObject)obj).seal();
    } else if (obj instanceof ScriptObjectMirror) {
        return ((ScriptObjectMirror)obj).seal();
    } else {
        throw notAnObject(obj);
    }
}
 
Example 37
Source Project: hottub   Source File: Jdk8072596TestSubject.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void test2(String x, final Object y, final ScriptObject w, final Object... z) {
    test1(x, y, w);

    Assert.assertEquals(z.length, 2);

    Assert.assertTrue(z[0] instanceof ScriptObjectMirror);
    Assert.assertEquals(((ScriptObjectMirror)z[0]).get("baz"), 3);

    Assert.assertTrue(z[1] instanceof ScriptObjectMirror);
    Assert.assertEquals(((ScriptObjectMirror)z[1]).get("bing"), 4);
}
 
Example 38
Source Project: SikuliNG   Source File: Do.java    License: MIT License 5 votes vote down vote up
public static void showcase(Object... args) {
  Element eShowcase = new Element();
  eShowcase.setName("SHOWCASE");
  for (Object arg : args) {
    log.trace("arg: %s", arg);
    if (((ScriptObjectMirror) arg).isFunction()) {
      ((ScriptObjectMirror) arg).call(arg, eShowcase);
    }
  }
}
 
Example 39
Source Project: jdk8u60   Source File: NativeObject.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * ECMA 15.2.3.4 Object.getOwnPropertyNames ( O )
 *
 * @param self self reference
 * @param obj  object to query for property names
 * @return array of property names
 */
@Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR)
public static ScriptObject getOwnPropertyNames(final Object self, final Object obj) {
    if (obj instanceof ScriptObject) {
        return new NativeArray(((ScriptObject)obj).getOwnKeys(true));
    } else if (obj instanceof ScriptObjectMirror) {
        return new NativeArray(((ScriptObjectMirror)obj).getOwnKeys(true));
    } else {
        throw notAnObject(obj);
    }
}
 
Example 40
@SuppressWarnings("unused")
private static Object jsObjectScopeCall(final JSObject jsObj, final Object thiz, final Object[] args) {
    final Object modifiedThiz;
    if (thiz == ScriptRuntime.UNDEFINED && !jsObj.isStrictFunction()) {
        final Global global = Context.getGlobal();
        modifiedThiz = ScriptObjectMirror.wrap(global, global);
    } else {
        modifiedThiz = thiz;
    }
    return jsObj.call(modifiedThiz, args);
}