本文整理匯總了Java中jdk.nashorn.api.scripting.ScriptObjectMirror.setMember方法的典型用法代碼示例。如果您正苦於以下問題:Java ScriptObjectMirror.setMember方法的具體用法?Java ScriptObjectMirror.setMember怎麽用?Java ScriptObjectMirror.setMember使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類jdk.nashorn.api.scripting.ScriptObjectMirror
的用法示例。
在下文中一共展示了ScriptObjectMirror.setMember方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: evalFunctionCall
import jdk.nashorn.api.scripting.ScriptObjectMirror; //導入方法依賴的package包/類
public static ScriptValue evalFunctionCall(ScriptObjectMirror som, Object callArg, ScriptContext context) {
// injects the 'karate' variable into the js function body
// also ensure that things like 'karate.get' operate on the latest variable state
som.setMember(ScriptBindings.KARATE, context.bindings.bridge);
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);
}
}
示例2: jsobjectTest
import jdk.nashorn.api.scripting.ScriptObjectMirror; //導入方法依賴的package包/類
@Test
public void jsobjectTest() {
final ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine e = m.getEngineByName("nashorn");
try {
e.eval("var obj = { '1': 'world', func: function() { return this.bar; }, bar: 'hello' }");
final ScriptObjectMirror obj = (ScriptObjectMirror) e.get("obj");
// try basic get on existing properties
if (!obj.getMember("bar").equals("hello")) {
fail("obj.bar != 'hello'");
}
if (!obj.getSlot(1).equals("world")) {
fail("obj[1] != 'world'");
}
if (!obj.callMember("func", new Object[0]).equals("hello")) {
fail("obj.func() != 'hello'");
}
// try setting properties
obj.setMember("bar", "new-bar");
obj.setSlot(1, "new-element-1");
if (!obj.getMember("bar").equals("new-bar")) {
fail("obj.bar != 'new-bar'");
}
if (!obj.getSlot(1).equals("new-element-1")) {
fail("obj[1] != 'new-element-1'");
}
// try adding properties
obj.setMember("prop", "prop-value");
obj.setSlot(12, "element-12");
if (!obj.getMember("prop").equals("prop-value")) {
fail("obj.prop != 'prop-value'");
}
if (!obj.getSlot(12).equals("element-12")) {
fail("obj[12] != 'element-12'");
}
// delete properties
obj.removeMember("prop");
if ("prop-value".equals(obj.getMember("prop"))) {
fail("obj.prop is not deleted!");
}
// Simple eval tests
assertEquals(obj.eval("typeof Object"), "function");
assertEquals(obj.eval("'nashorn'.substring(3)"), "horn");
} catch (final Exception exp) {
exp.printStackTrace();
fail(exp.getMessage());
}
}
示例3: setProp
import jdk.nashorn.api.scripting.ScriptObjectMirror; //導入方法依賴的package包/類
public static void setProp(ScriptObjectMirror context, String prop, Object value) {
context.setMember(prop, value);
}