use of org.mozilla.javascript.ScriptableObject in project stetho by facebook.
the class JsRuntimeReplFactoryBuilder method importVariables.
private void importVariables(@NonNull ScriptableObject scope) throws StethoJsException {
// Define the variables
for (Map.Entry<String, Object> entrySet : mVariables.entrySet()) {
String varName = entrySet.getKey();
Object varValue = entrySet.getValue();
try {
Object jsValue;
if (varValue instanceof Scriptable || varValue instanceof Undefined) {
jsValue = varValue;
} else {
jsValue = Context.javaToJS(varValue, scope);
}
ScriptableObject.putProperty(scope, varName, jsValue);
} catch (Exception e) {
throw new StethoJsException(e, "Failed to setup variable: %s", varName);
}
}
}
use of org.mozilla.javascript.ScriptableObject in project gocd by gocd.
the class JsonTester method javascriptParse.
public static Object javascriptParse(String other) {
try {
Context ctx = Context.enter();
ScriptableObject scope = ctx.initStandardObjects();
return parse(ctx.evaluateString(scope, "json = " + other, "JsonTester", 1, null));
} catch (Exception evaluator) {
evaluator.printStackTrace();
System.err.println("Invalid json:\n" + other);
throw bomb("Invalid javascript", evaluator);
} finally {
Context.exit();
}
}
use of org.mozilla.javascript.ScriptableObject in project gocd by gocd.
the class JsonTester method parseList.
private static List parseList(ScriptableObject o) {
List jsonList = new ArrayList();
for (Object basicId : o.getIds()) {
Integer id = (Integer) basicId;
jsonList.add(parse(o.get(id, o)));
}
return jsonList;
}
use of org.mozilla.javascript.ScriptableObject in project jaggery by wso2.
the class WebAppSessionListener method sessionDestroyed.
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
ServletContext ctx = httpSessionEvent.getSession().getServletContext();
List<Object> jsListeners = (List<Object>) ctx.getAttribute(JaggeryCoreConstants.JS_DESTROYED_LISTENERS);
if (jsListeners == null) {
return;
}
JaggeryContext shared = WebAppManager.sharedJaggeryContext(ctx);
Context cx = shared.getEngine().enterContext();
JaggeryContext context = CommonManager.getJaggeryContext();
if (CommonManager.getJaggeryContext() == null) {
context = WebAppManager.clonedJaggeryContext(ctx);
CommonManager.setJaggeryContext(context);
}
RhinoEngine engine = context.getEngine();
ScriptableObject clonedScope = context.getScope();
JavaScriptProperty session = new JavaScriptProperty("session");
session.setValue(cx.newObject(clonedScope, "Session", new Object[] { httpSessionEvent.getSession() }));
session.setAttribute(ScriptableObject.READONLY);
RhinoEngine.defineProperty(clonedScope, session);
for (Object jsListener : jsListeners) {
CommonManager.getCallstack(context).push((String) jsListener);
try {
ScriptReader sr = new ScriptReader(ctx.getResourceAsStream((String) jsListener)) {
@Override
protected void build() throws IOException {
try {
sourceReader = new StringReader(HostObjectUtil.streamToString(sourceIn));
} catch (ScriptException e) {
throw new IOException(e);
}
}
};
engine.exec(sr, clonedScope, null);
} catch (ScriptException e) {
log.error(e.getMessage(), e);
} finally {
CommonManager.getCallstack(context).pop();
}
}
Context.exit();
}
use of org.mozilla.javascript.ScriptableObject in project jaggery by wso2.
the class CommandLineExecutor method parseJaggeryScript.
/**
* Parse Jaggery scripts resides in the file path
*
* @param fileURL url of the file
*/
@SuppressFBWarnings("PATH_TRAVERSAL_IN")
static void parseJaggeryScript(final String fileURL) {
FileInputStream fstream = null;
try {
//Initialize the Rhino context
RhinoEngine.enterGlobalContext();
fstream = new FileInputStream(fileURL);
final RhinoEngine engine = CommandLineManager.getCommandLineEngine();
final ScriptableObject scope = engine.getRuntimeScope();
//initialize JaggeryContext
final JaggeryContext jaggeryContext = new JaggeryContext();
jaggeryContext.setTenantDomain(DEFAULT_TENANTDOMAIN);
jaggeryContext.setEngine(engine);
jaggeryContext.setScope(scope);
jaggeryContext.addProperty(CommonManager.JAGGERY_OUTPUT_STREAM, System.out);
RhinoEngine.putContextProperty("jaggeryContext", jaggeryContext);
//Parsing the script
final Reader source = new ScriptReader(new BufferedInputStream(fstream));
out.println("\n");
ShellUtilityService.initializeUtilityServices();
engine.exec(source, scope, null);
ShellUtilityService.destroyUtilityServices();
out.flush();
out.println("\n");
} catch (Exception e) {
out.println("\n");
out.println("Error: " + e.getMessage());
out.println("\n");
} finally {
if (fstream != null) {
try {
fstream.close();
} catch (IOException ignored) {
}
}
}
}
Aggregations