本文整理匯總了Java中org.mozilla.javascript.Context.initStandardObjects方法的典型用法代碼示例。如果您正苦於以下問題:Java Context.initStandardObjects方法的具體用法?Java Context.initStandardObjects怎麽用?Java Context.initStandardObjects使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.mozilla.javascript.Context
的用法示例。
在下文中一共展示了Context.initStandardObjects方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testDeletePropInPrototype
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
/**
* delete should not delete anything in the prototype chain.
*/
@Test
public void testDeletePropInPrototype() throws Exception {
final String script = "Array.prototype.foo = function() {};\n"
+ "Array.prototype[1] = function() {};\n"
+ "var t = [];\n"
+ "[].foo();\n"
+ "for (var i in t) delete t[i];\n"
+ "[].foo();\n"
+ "[][1]();\n";
final ContextAction action = new ContextAction()
{
public Object run(final Context _cx)
{
final ScriptableObject scope = _cx.initStandardObjects();
final Object result = _cx.evaluateString(scope, script, "test script", 0, null);
return null;
}
};
Utils.runWithAllOptimizationLevels(action);
}
示例2: ensureScopePresent
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
private void ensureScopePresent()
{
if (scope == null)
{
// Create a scope for the value conversion. This scope will be an empty scope exposing basic Object and Function, sufficient for value-conversion.
// In case no context is active for the current thread, we can safely enter end exit one to get hold of a scope
Context ctx = Context.getCurrentContext();
boolean closeContext = false;
if (ctx == null)
{
ctx = Context.enter();
closeContext = true;
}
scope = ctx.initStandardObjects();
scope.setParentScope(null);
if (closeContext)
{
// Only an exit call should be done when context didn't exist before
Context.exit();
}
}
}
示例3: testWithTwoScopes
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
private void testWithTwoScopes(final String scriptScope1,
final String scriptScope2)
{
final ContextAction action = new ContextAction()
{
public Object run(final Context cx)
{
final Scriptable scope1 = cx.initStandardObjects(
new MySimpleScriptableObject("scope1"));
final Scriptable scope2 = cx.initStandardObjects(
new MySimpleScriptableObject("scope2"));
cx.evaluateString(scope2, scriptScope2, "source2", 1, null);
scope1.put("scope2", scope1, scope2);
return cx.evaluateString(scope1, scriptScope1, "source1", 1,
null);
}
};
Utils.runWithAllOptimizationLevels(action);
}
示例4: testArrayConcat
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
public void testArrayConcat() {
final String script = "var a = ['a0', 'a1'];\n"
+ "a[3] = 'a3';\n"
+ "var b = ['b1', 'b2'];\n"
+ "b.concat(a)";
final ContextAction action = new ContextAction()
{
public Object run(final Context _cx)
{
final ScriptableObject scope = _cx.initStandardObjects();
final Object result = _cx.evaluateString(scope, script, "test script", 0, null);
assertEquals("b1,b2,a0,a1,,a3", Context.toString(result));
return null;
}
};
Utils.runWithAllOptimizationLevels(action);
}
示例5: execute
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
@Override
public boolean execute(Context javascriptContext, Scriptable scope) throws EngineException {
if (isEnabled()) {
if (super.execute(javascriptContext, scope)) {
for (FunctionStatement function : getFunctions()) {
if (function.getName().equals(functionName)) {
Scriptable curScope = javascriptContext.initStandardObjects();
curScope.setParentScope(scope);
boolean ret = function.execute(javascriptContext, curScope);
Object returnedValue = function.getReturnedValue();
if (returnedValue != null) {
ReturnStatement.returnLoop(this.parent, returnedValue);
}
return ret;
}
}
}
}
return false;
}
示例6: load
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
/**
* load
* @param response
*/
public void load( WebResponse response ) {
Function onLoadEvent=null;
try {
Context context = Context.enter();
context.initStandardObjects( null );
HTMLDocument htmlDocument = ((DomWindow) response.getScriptingHandler()).getDocument();
if (!(htmlDocument instanceof HTMLDocumentImpl)) return;
HTMLBodyElementImpl body = (HTMLBodyElementImpl) htmlDocument.getBody();
if (body == null) return;
onLoadEvent = body.getOnloadEvent();
if (onLoadEvent == null) return;
onLoadEvent.call( context, body, body, new Object[0] );
} catch (JavaScriptException e) {
ScriptingEngineImpl.handleScriptException(e, onLoadEvent.toString());
// HttpUnitUtils.handleException(e);
} catch (EcmaError ee) {
//throw ee;
ScriptingEngineImpl.handleScriptException(ee, onLoadEvent.toString());
} finally {
Context.exit();
}
}
示例7: helper
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
public void helper(String source) {
Context cx = Context.enter();
Context.ClassShutterSetter setter = cx.getClassShutterSetter();
try {
Scriptable globalScope = cx.initStandardObjects();
if (setter == null) {
setter = classShutterSetter;
} else {
classShutterSetter = setter;
}
setter.setClassShutter(new OpaqueShutter());
cx.evaluateString(globalScope, source, "test source", 1, null);
} finally {
setter.setClassShutter(null);
Context.exit();
}
}
示例8: createTest
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
private static Test createTest(final File testDir, final String name) {
return new TestCase(name) {
@Override
public int countTestCases() {
return 1;
}
@Override
public void runBare() throws Throwable {
final Context cx = Context.enter();
try {
cx.setOptimizationLevel(-1);
final Scriptable scope = cx.initStandardObjects();
ScriptableObject.putProperty(scope, "print", new Print(scope));
createRequire(testDir, cx, scope).requireMain(cx, "program");
}
finally {
Context.exit();
}
}
};
}
示例9: setUp
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
@SuppressWarnings("unchecked")
@Override
protected void setUp() {
// set up a reference map
reference = new LinkedHashMap<Object, Object>();
reference.put("a", "a");
reference.put("b", Boolean.TRUE);
reference.put("c", new HashMap<Object, Object>());
reference.put(new Integer(1), new Integer(42));
// get a js object as map
Context context = Context.enter();
ScriptableObject scope = context.initStandardObjects();
map = (Map<Object, Object>) context.evaluateString(scope,
"({ a: 'a', b: true, c: new java.util.HashMap(), 1: 42});",
"testsrc", 1, null);
Context.exit();
}
示例10: testAttributeName
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
@Test
public void testAttributeName() {
Context context = Context.enter();
Scriptable scriptable = context.initStandardObjects();
context.setOptimizationLevel(-1);
Object o = context.evaluateString(scriptable, "XML.ignoreProcessingInstructions = true; (<xml id=\"foo\"></xml>).attributes()[0].name()", "<e4x>", 1, null);
System.out.println(o);
}
示例11: functionObjectPrimitiveToObject
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
/**
* Test that FunctionObject use the right top scope to convert a primitive
* to an object
*/
@Test
public void functionObjectPrimitiveToObject() throws Exception {
final String scriptScope2 = "function f() {\n"
+ "String.prototype.foo = 'from 2'; \n"
+ "var s2 = 's2';\n"
+ "var s2Foo = s2.foo;\n"
+ "var s2FooReadByFunction = myObject.readPropFoo(s2);\n"
+ "if (s2Foo != s2FooReadByFunction)\n"
+ "throw 's2 got: ' + s2FooReadByFunction;\n"
+ "}";
// define object with custom method
final MyObject myObject = new MyObject();
final String[] functionNames = { "readPropFoo" };
myObject.defineFunctionProperties(functionNames, MyObject.class,
ScriptableObject.EMPTY);
final String scriptScope1 = "String.prototype.foo = 'from 1'; scope2.f()";
final ContextAction action = new ContextAction()
{
public Object run(final Context cx)
{
final Scriptable scope1 = cx.initStandardObjects(
new MySimpleScriptableObject("scope1"));
final Scriptable scope2 = cx.initStandardObjects(
new MySimpleScriptableObject("scope2"));
scope2.put("myObject", scope2, myObject);
cx.evaluateString(scope2, scriptScope2, "source2", 1, null);
scope1.put("scope2", scope1, scope2);
return cx.evaluateString(scope1, scriptScope1, "source1", 1, null);
}
};
Utils.runWithAllOptimizationLevels(action);
}
示例12: LogManager
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
public LogManager() {
Context js_context = Context.enter();
try {
js_utils = js_context.initStandardObjects();
js_context.compileString(
"String.prototype.startsWith = function(str) {return this.indexOf(str)==0};"
+ "String.prototype.endsWith = function(str) {return this.lastIndexOf(str)+str.length==this.length};"
+ "String.prototype.contains = function(str) {return this.indexOf(str)!=-1};"
, "utils", 0, null).exec(js_context, js_utils);
} finally {
Context.exit();
}
}
示例13: addCandidate
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
private void addCandidate(Context js_context, JSONArray result) {
if (candidate != null) {
if (js_filter != null) {
try {
Scriptable scope = js_context.initStandardObjects(null, true);
scope.put("category", scope, candidate.getString(0));
scope.put("time", scope, candidate.getString(1));
scope.put("level", scope, candidate.getString(2));
scope.put("thread", scope, candidate.getString(3));
scope.put("message", scope, candidate.getString(4));
StringBuffer extra = new StringBuffer();
for (int i = 5; i < candidate.length(); i++) {
String sub_extra = candidate.getString(i);
extra.append(sub_extra).append(' ');
delim_extra.reset(sub_extra);
if (delim_extra.matches() && delim_extra.groupCount() == 2) {
scope.put(delim_extra.group(1), scope, delim_extra.group(2));
}
}
if (extra.length() > 0) {
scope.put("extra", scope, extra.substring(0, extra.length() - 1));
}
scope.setParentScope(js_utils);
if (Context.toBoolean(js_filter.exec(js_context, scope))) {
result.put(candidate);
}
} catch (Exception e) {
// TODO: handle exception
}
} else {
result.put(candidate);
}
candidate = null;
}
}
示例14: testCustomizeTypeOf
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
private void testCustomizeTypeOf(final String expected, final Scriptable obj)
{
final ContextAction action = new ContextAction()
{
public Object run(final Context context)
{
final Scriptable scope = context.initStandardObjects();
scope.put("myObj", scope, obj);
return context.evaluateString(scope, "typeof myObj", "test script", 1, null);
}
};
doTest(expected, action);
}
示例15: copyScope
import org.mozilla.javascript.Context; //導入方法依賴的package包/類
static public Scriptable copyScope(Context context, Scriptable scope) {
Scriptable scopeCopy = context.initStandardObjects();
for (Object id : scope.getIds()) {
scopeCopy.put(id.toString(), scopeCopy, scope.get(id.toString(), scope));
}
return scopeCopy;
}